LLVM API Documentation

EventListenerCommon.h
Go to the documentation of this file.
00001 //===-- JIT.h - Abstract Execution Engine Interface -------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Common functionality for JITEventListener implementations
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef EVENT_LISTENER_COMMON_H
00015 #define EVENT_LISTENER_COMMON_H
00016 
00017 #include "llvm/ADT/DenseMap.h"
00018 #include "llvm/IR/DebugInfo.h"
00019 #include "llvm/IR/Metadata.h"
00020 #include "llvm/IR/ValueHandle.h"
00021 #include "llvm/Support/Path.h"
00022 
00023 namespace llvm {
00024 
00025 namespace jitprofiling {
00026 
00027 class FilenameCache {
00028   // Holds the filename of each Scope, so that we can pass a null-terminated
00029   // string into oprofile.  Use an AssertingVH rather than a ValueMap because we
00030   // shouldn't be modifying any MDNodes while this map is alive.
00031   DenseMap<AssertingVH<MDNode>, std::string> Filenames;
00032   DenseMap<AssertingVH<MDNode>, std::string> Paths;
00033 
00034  public:
00035   const char *getFilename(MDNode *Scope) {
00036     std::string &Filename = Filenames[Scope];
00037     if (Filename.empty()) {
00038       DIScope DIScope(Scope);
00039       Filename = DIScope.getFilename();
00040     }
00041     return Filename.c_str();
00042   }
00043 
00044   const char *getFullPath(MDNode *Scope) {
00045     std::string &P = Paths[Scope];
00046     if (P.empty()) {
00047       DIScope DIScope(Scope);
00048       StringRef DirName = DIScope.getDirectory();
00049       StringRef FileName = DIScope.getFilename();
00050       SmallString<256> FullPath;
00051       if (DirName != "." && DirName != "") {
00052         FullPath = DirName;
00053       }
00054       if (FileName != "") {
00055         sys::path::append(FullPath, FileName);
00056       }
00057       P = FullPath.str();
00058     }
00059     return P.c_str();
00060   }
00061 };
00062 
00063 } // namespace jitprofiling
00064 
00065 } // namespace llvm
00066 
00067 #endif //EVENT_LISTENER_COMMON_H