LLVM  mainline
DebugInfo.h
Go to the documentation of this file.
00001 //===- DebugInfo.h - Debug Information Helpers ------------------*- 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 // This file defines a bunch of datatypes that are useful for creating and
00011 // walking debug info in LLVM IR form. They essentially provide wrappers around
00012 // the information in the global variables that's needed when constructing the
00013 // DWARF information.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_IR_DEBUGINFO_H
00018 #define LLVM_IR_DEBUGINFO_H
00019 
00020 #include "llvm/ADT/DenseMap.h"
00021 #include "llvm/ADT/SmallPtrSet.h"
00022 #include "llvm/ADT/SmallVector.h"
00023 #include "llvm/ADT/StringRef.h"
00024 #include "llvm/ADT/iterator_range.h"
00025 #include "llvm/IR/DebugInfoMetadata.h"
00026 #include "llvm/Support/Casting.h"
00027 #include "llvm/Support/Dwarf.h"
00028 #include "llvm/Support/ErrorHandling.h"
00029 #include <iterator>
00030 
00031 namespace llvm {
00032 class BasicBlock;
00033 class Constant;
00034 class Function;
00035 class GlobalVariable;
00036 class Module;
00037 class Type;
00038 class Value;
00039 class DbgDeclareInst;
00040 class DbgValueInst;
00041 class Instruction;
00042 class Metadata;
00043 class MDNode;
00044 class MDString;
00045 class NamedMDNode;
00046 class LLVMContext;
00047 class raw_ostream;
00048 
00049 class DIFile;
00050 class DISubprogram;
00051 class DILexicalBlock;
00052 class DILexicalBlockFile;
00053 class DIVariable;
00054 class DIType;
00055 class DIScope;
00056 class DIObjCProperty;
00057 
00058 /// \brief Maps from type identifier to the actual MDNode.
00059 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
00060 
00061 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC)                                      \
00062   class DESC;                                                                  \
00063   template <> struct simplify_type<const DESC>;                                \
00064   template <> struct simplify_type<DESC>;
00065 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
00066 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
00067 DECLARE_SIMPLIFY_DESCRIPTOR(DIScope)
00068 DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
00069 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
00070 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
00071 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
00072 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
00073 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
00074 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
00075 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
00076 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
00077 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
00078 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
00079 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
00080 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
00081 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
00082 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
00083 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
00084 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
00085 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
00086 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
00087 #undef DECLARE_SIMPLIFY_DESCRIPTOR
00088 
00089 typedef DebugNodeArray DIArray;
00090 typedef MDTypeRefArray DITypeArray;
00091 typedef DebugNodeRef DIDescriptorRef;
00092 typedef MDScopeRef DIScopeRef;
00093 typedef MDTypeRef DITypeRef;
00094 
00095 class DISubrange {
00096   MDSubrange *N;
00097 
00098 public:
00099   DISubrange(const MDSubrange *N = nullptr) : N(const_cast<MDSubrange *>(N)) {}
00100 
00101   operator MDSubrange *() const { return N; }
00102   MDSubrange *operator->() const { return N; }
00103   MDSubrange &operator*() const { return *N; }
00104 };
00105 
00106 class DIEnumerator {
00107   MDEnumerator *N;
00108 
00109 public:
00110   DIEnumerator(const MDEnumerator *N = nullptr)
00111       : N(const_cast<MDEnumerator *>(N)) {}
00112 
00113   operator MDEnumerator *() const { return N; }
00114   MDEnumerator *operator->() const { return N; }
00115   MDEnumerator &operator*() const { return *N; }
00116 };
00117 
00118 class DIScope {
00119   MDScope *N;
00120 
00121 public:
00122   DIScope(const MDScope *N = nullptr) : N(const_cast<MDScope *>(N)) {}
00123 
00124   operator MDScope *() const { return N; }
00125   MDScope *operator->() const { return N; }
00126   MDScope &operator*() const { return *N; }
00127 };
00128 
00129 class DIType {
00130   MDType *N;
00131 
00132 public:
00133   DIType(const MDType *N = nullptr) : N(const_cast<MDType *>(N)) {}
00134 
00135   operator DIScope() const { return N; }
00136   operator MDType *() const { return N; }
00137   MDType *operator->() const { return N; }
00138   MDType &operator*() const { return *N; }
00139 };
00140 
00141 class DIBasicType {
00142   MDBasicType *N;
00143 
00144 public:
00145   DIBasicType(const MDBasicType *N = nullptr)
00146       : N(const_cast<MDBasicType *>(N)) {}
00147 
00148   operator DIType() const { return N; }
00149   operator MDBasicType *() const { return N; }
00150   MDBasicType *operator->() const { return N; }
00151   MDBasicType &operator*() const { return *N; }
00152 };
00153 
00154 class DIDerivedType {
00155   MDDerivedTypeBase *N;
00156 
00157 public:
00158   DIDerivedType(const MDDerivedTypeBase *N = nullptr)
00159       : N(const_cast<MDDerivedTypeBase *>(N)) {}
00160 
00161   operator DIType() const { return N; }
00162   operator MDDerivedTypeBase *() const { return N; }
00163   MDDerivedTypeBase *operator->() const { return N; }
00164   MDDerivedTypeBase &operator*() const { return *N; }
00165 };
00166 
00167 class DICompositeType {
00168   MDCompositeTypeBase *N;
00169 
00170 public:
00171   DICompositeType(const MDCompositeTypeBase *N = nullptr)
00172       : N(const_cast<MDCompositeTypeBase *>(N)) {}
00173 
00174   operator DIType() const { return N; }
00175   operator MDCompositeTypeBase *() const { return N; }
00176   MDCompositeTypeBase *operator->() const { return N; }
00177   MDCompositeTypeBase &operator*() const { return *N; }
00178 };
00179 
00180 class DISubroutineType {
00181   MDSubroutineType *N;
00182 
00183 public:
00184   DISubroutineType(const MDSubroutineType *N = nullptr)
00185       : N(const_cast<MDSubroutineType *>(N)) {}
00186 
00187   operator DIType() const { return N; }
00188   operator DICompositeType() const { return N; }
00189   operator MDSubroutineType *() const { return N; }
00190   MDSubroutineType *operator->() const { return N; }
00191   MDSubroutineType &operator*() const { return *N; }
00192 };
00193 
00194 class DIFile {
00195   MDFile *N;
00196 
00197 public:
00198   DIFile(const MDFile *N = nullptr) : N(const_cast<MDFile *>(N)) {}
00199 
00200   operator DIScope() const { return N; }
00201   operator MDFile *() const { return N; }
00202   MDFile *operator->() const { return N; }
00203   MDFile &operator*() const { return *N; }
00204 };
00205 
00206 class DICompileUnit {
00207   MDCompileUnit *N;
00208 
00209 public:
00210   DICompileUnit(const MDCompileUnit *N = nullptr)
00211       : N(const_cast<MDCompileUnit *>(N)) {}
00212 
00213   operator DIScope() const { return N; }
00214   operator MDCompileUnit *() const { return N; }
00215   MDCompileUnit *operator->() const { return N; }
00216   MDCompileUnit &operator*() const { return *N; }
00217 };
00218 
00219 class DISubprogram {
00220   MDSubprogram *N;
00221 
00222 public:
00223   DISubprogram(const MDSubprogram *N = nullptr)
00224       : N(const_cast<MDSubprogram *>(N)) {}
00225 
00226   operator DIScope() const { return N; }
00227   operator MDSubprogram *() const { return N; }
00228   MDSubprogram *operator->() const { return N; }
00229   MDSubprogram &operator*() const { return *N; }
00230 };
00231 
00232 class DILexicalBlock {
00233   MDLexicalBlockBase *N;
00234 
00235 public:
00236   DILexicalBlock(const MDLexicalBlockBase *N = nullptr)
00237       : N(const_cast<MDLexicalBlockBase *>(N)) {}
00238 
00239   operator MDLexicalBlockBase *() const { return N; }
00240   MDLexicalBlockBase *operator->() const { return N; }
00241   MDLexicalBlockBase &operator*() const { return *N; }
00242 };
00243 
00244 class DILexicalBlockFile {
00245   MDLexicalBlockFile *N;
00246 
00247 public:
00248   DILexicalBlockFile(const MDLexicalBlockFile *N = nullptr)
00249       : N(const_cast<MDLexicalBlockFile *>(N)) {}
00250 
00251   operator MDLexicalBlockFile *() const { return N; }
00252   MDLexicalBlockFile *operator->() const { return N; }
00253   MDLexicalBlockFile &operator*() const { return *N; }
00254 };
00255 
00256 class DINameSpace {
00257   MDNamespace *N;
00258 
00259 public:
00260   DINameSpace(const MDNamespace *N = nullptr)
00261       : N(const_cast<MDNamespace *>(N)) {}
00262 
00263   operator DIScope() const { return N; }
00264   operator MDNamespace *() const { return N; }
00265   MDNamespace *operator->() const { return N; }
00266   MDNamespace &operator*() const { return *N; }
00267 };
00268 
00269 class DITemplateTypeParameter {
00270   MDTemplateTypeParameter *N;
00271 
00272 public:
00273   DITemplateTypeParameter(const MDTemplateTypeParameter *N = nullptr)
00274       : N(const_cast<MDTemplateTypeParameter *>(N)) {}
00275 
00276   operator MDTemplateTypeParameter *() const { return N; }
00277   MDTemplateTypeParameter *operator->() const { return N; }
00278   MDTemplateTypeParameter &operator*() const { return *N; }
00279 };
00280 
00281 class DITemplateValueParameter {
00282   MDTemplateValueParameter *N;
00283 
00284 public:
00285   DITemplateValueParameter(const MDTemplateValueParameter *N = nullptr)
00286       : N(const_cast<MDTemplateValueParameter *>(N)) {}
00287 
00288   operator MDTemplateValueParameter *() const { return N; }
00289   MDTemplateValueParameter *operator->() const { return N; }
00290   MDTemplateValueParameter &operator*() const { return *N; }
00291 };
00292 
00293 class DIGlobalVariable {
00294   MDGlobalVariable *N;
00295 
00296 public:
00297   DIGlobalVariable(const MDGlobalVariable *N = nullptr)
00298       : N(const_cast<MDGlobalVariable *>(N)) {}
00299 
00300   operator MDGlobalVariable *() const { return N; }
00301   MDGlobalVariable *operator->() const { return N; }
00302   MDGlobalVariable &operator*() const { return *N; }
00303 };
00304 
00305 class DIVariable {
00306   MDLocalVariable *N;
00307 
00308 public:
00309   DIVariable(const MDLocalVariable *N = nullptr)
00310       : N(const_cast<MDLocalVariable *>(N)) {}
00311 
00312   operator MDLocalVariable *() const { return N; }
00313   MDLocalVariable *operator->() const { return N; }
00314   MDLocalVariable &operator*() const { return *N; }
00315 };
00316 
00317 class DIExpression {
00318   MDExpression *N;
00319 
00320 public:
00321   DIExpression(const MDExpression *N = nullptr)
00322       : N(const_cast<MDExpression *>(N)) {}
00323 
00324   operator MDExpression *() const { return N; }
00325   MDExpression *operator->() const { return N; }
00326   MDExpression &operator*() const { return *N; }
00327 };
00328 
00329 class DILocation {
00330   MDLocation *N;
00331 
00332 public:
00333   DILocation(const MDLocation *N = nullptr) : N(const_cast<MDLocation *>(N)) {}
00334 
00335   operator MDLocation *() const { return N; }
00336   MDLocation *operator->() const { return N; }
00337   MDLocation &operator*() const { return *N; }
00338 };
00339 
00340 class DIObjCProperty {
00341   MDObjCProperty *N;
00342 
00343 public:
00344   DIObjCProperty(const MDObjCProperty *N = nullptr)
00345       : N(const_cast<MDObjCProperty *>(N)) {}
00346 
00347   operator MDObjCProperty *() const { return N; }
00348   MDObjCProperty *operator->() const { return N; }
00349   MDObjCProperty &operator*() const { return *N; }
00350 };
00351 
00352 class DIImportedEntity {
00353   MDImportedEntity *N;
00354 
00355 public:
00356   DIImportedEntity(const MDImportedEntity *N = nullptr)
00357       : N(const_cast<MDImportedEntity *>(N)) {}
00358 
00359   operator MDImportedEntity *() const { return N; }
00360   MDImportedEntity *operator->() const { return N; }
00361   MDImportedEntity &operator*() const { return *N; }
00362 };
00363 
00364 #define SIMPLIFY_DESCRIPTOR(DESC)                                              \
00365   template <> struct simplify_type<const DESC> {                               \
00366     typedef Metadata *SimpleType;                                              \
00367     static SimpleType getSimplifiedValue(const DESC &DI) { return DI; }        \
00368   };                                                                           \
00369   template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
00370 SIMPLIFY_DESCRIPTOR(DISubrange)
00371 SIMPLIFY_DESCRIPTOR(DIEnumerator)
00372 SIMPLIFY_DESCRIPTOR(DIScope)
00373 SIMPLIFY_DESCRIPTOR(DIType)
00374 SIMPLIFY_DESCRIPTOR(DIBasicType)
00375 SIMPLIFY_DESCRIPTOR(DIDerivedType)
00376 SIMPLIFY_DESCRIPTOR(DICompositeType)
00377 SIMPLIFY_DESCRIPTOR(DISubroutineType)
00378 SIMPLIFY_DESCRIPTOR(DIFile)
00379 SIMPLIFY_DESCRIPTOR(DICompileUnit)
00380 SIMPLIFY_DESCRIPTOR(DISubprogram)
00381 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
00382 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
00383 SIMPLIFY_DESCRIPTOR(DINameSpace)
00384 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
00385 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
00386 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
00387 SIMPLIFY_DESCRIPTOR(DIVariable)
00388 SIMPLIFY_DESCRIPTOR(DIExpression)
00389 SIMPLIFY_DESCRIPTOR(DILocation)
00390 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
00391 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
00392 #undef SIMPLIFY_DESCRIPTOR
00393 
00394 /// \brief Find subprogram that is enclosing this scope.
00395 MDSubprogram *getDISubprogram(const MDNode *Scope);
00396 
00397 /// \brief Find debug info for a given function.
00398 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
00399 /// DISubprogram.
00400 MDSubprogram *getDISubprogram(const Function *F);
00401 
00402 /// \brief Find underlying composite type.
00403 MDCompositeTypeBase *getDICompositeType(MDType *T);
00404 
00405 /// \brief Generate map by visiting all retained types.
00406 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
00407 
00408 /// \brief Strip debug info in the module if it exists.
00409 ///
00410 /// To do this, we remove all calls to the debugger intrinsics and any named
00411 /// metadata for debugging. We also remove debug locations for instructions.
00412 /// Return true if module is modified.
00413 bool StripDebugInfo(Module &M);
00414 bool stripDebugInfo(Function &F);
00415 
00416 /// \brief Return Debug Info Metadata Version by checking module flags.
00417 unsigned getDebugMetadataVersionFromModule(const Module &M);
00418 
00419 /// \brief Utility to find all debug info in a module.
00420 ///
00421 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
00422 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
00423 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
00424 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
00425 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
00426 /// used by the CUs.
00427 class DebugInfoFinder {
00428 public:
00429   DebugInfoFinder() : TypeMapInitialized(false) {}
00430 
00431   /// \brief Process entire module and collect debug info anchors.
00432   void processModule(const Module &M);
00433 
00434   /// \brief Process DbgDeclareInst.
00435   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
00436   /// \brief Process DbgValueInst.
00437   void processValue(const Module &M, const DbgValueInst *DVI);
00438   /// \brief Process DILocation.
00439   void processLocation(const Module &M, const MDLocation *Loc);
00440 
00441   /// \brief Clear all lists.
00442   void reset();
00443 
00444 private:
00445   void InitializeTypeMap(const Module &M);
00446 
00447   void processType(MDType *DT);
00448   void processSubprogram(MDSubprogram *SP);
00449   void processScope(MDScope *Scope);
00450   bool addCompileUnit(MDCompileUnit *CU);
00451   bool addGlobalVariable(MDGlobalVariable *DIG);
00452   bool addSubprogram(MDSubprogram *SP);
00453   bool addType(MDType *DT);
00454   bool addScope(MDScope *Scope);
00455 
00456 public:
00457   typedef SmallVectorImpl<MDCompileUnit *>::const_iterator
00458       compile_unit_iterator;
00459   typedef SmallVectorImpl<MDSubprogram *>::const_iterator subprogram_iterator;
00460   typedef SmallVectorImpl<MDGlobalVariable *>::const_iterator
00461       global_variable_iterator;
00462   typedef SmallVectorImpl<MDType *>::const_iterator type_iterator;
00463   typedef SmallVectorImpl<MDScope *>::const_iterator scope_iterator;
00464 
00465   iterator_range<compile_unit_iterator> compile_units() const {
00466     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
00467   }
00468 
00469   iterator_range<subprogram_iterator> subprograms() const {
00470     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
00471   }
00472 
00473   iterator_range<global_variable_iterator> global_variables() const {
00474     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
00475   }
00476 
00477   iterator_range<type_iterator> types() const {
00478     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
00479   }
00480 
00481   iterator_range<scope_iterator> scopes() const {
00482     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
00483   }
00484 
00485   unsigned compile_unit_count() const { return CUs.size(); }
00486   unsigned global_variable_count() const { return GVs.size(); }
00487   unsigned subprogram_count() const { return SPs.size(); }
00488   unsigned type_count() const { return TYs.size(); }
00489   unsigned scope_count() const { return Scopes.size(); }
00490 
00491 private:
00492   SmallVector<MDCompileUnit *, 8> CUs;
00493   SmallVector<MDSubprogram *, 8> SPs;
00494   SmallVector<MDGlobalVariable *, 8> GVs;
00495   SmallVector<MDType *, 8> TYs;
00496   SmallVector<MDScope *, 8> Scopes;
00497   SmallPtrSet<const MDNode *, 64> NodesSeen;
00498   DITypeIdentifierMap TypeIdentifierMap;
00499 
00500   /// \brief Specify if TypeIdentifierMap is initialized.
00501   bool TypeMapInitialized;
00502 };
00503 
00504 DenseMap<const Function *, MDSubprogram *> makeSubprogramMap(const Module &M);
00505 
00506 } // end namespace llvm
00507 
00508 #endif