CGDebugInfo.h revision 218893
1//===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This is the source level debug info generator for llvm translation. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef CLANG_CODEGEN_CGDEBUGINFO_H 15#define CLANG_CODEGEN_CGDEBUGINFO_H 16 17#include "clang/AST/Type.h" 18#include "clang/AST/Expr.h" 19#include "clang/Basic/SourceLocation.h" 20#include "llvm/ADT/DenseMap.h" 21#include "llvm/Analysis/DebugInfo.h" 22#include "llvm/Analysis/DIBuilder.h" 23#include "llvm/Support/ValueHandle.h" 24#include "llvm/Support/Allocator.h" 25 26#include "CGBuilder.h" 27 28namespace llvm { 29 class MDNode; 30} 31 32namespace clang { 33 class VarDecl; 34 class ObjCInterfaceDecl; 35 36namespace CodeGen { 37 class CodeGenModule; 38 class CodeGenFunction; 39 class GlobalDecl; 40 class CGBlockInfo; 41 42/// CGDebugInfo - This class gathers all debug information during compilation 43/// and is responsible for emitting to llvm globals or pass directly to 44/// the backend. 45class CGDebugInfo { 46 CodeGenModule &CGM; 47 llvm::DIBuilder DBuilder; 48 llvm::DICompileUnit TheCU; 49 SourceLocation CurLoc, PrevLoc; 50 llvm::DIType VTablePtrType; 51 52 /// TypeCache - Cache of previously constructed Types. 53 llvm::DenseMap<void *, llvm::WeakVH> TypeCache; 54 55 bool BlockLiteralGenericSet; 56 llvm::DIType BlockLiteralGeneric; 57 58 std::vector<llvm::TrackingVH<llvm::MDNode> > RegionStack; 59 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap; 60 // FnBeginRegionCount - Keep track of RegionStack counter at the beginning 61 // of a function. This is used to pop unbalanced regions at the end of a 62 // function. 63 std::vector<unsigned> FnBeginRegionCount; 64 65 /// LineDirectiveFiles - This stack is used to keep track of 66 /// scopes introduced by #line directives. 67 std::vector<const char *> LineDirectiveFiles; 68 69 /// DebugInfoNames - This is a storage for names that are 70 /// constructed on demand. For example, C++ destructors, C++ operators etc.. 71 llvm::BumpPtrAllocator DebugInfoNames; 72 llvm::StringRef CWDName; 73 74 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache; 75 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache; 76 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache; 77 78 /// Helper functions for getOrCreateType. 79 llvm::DIType CreateType(const BuiltinType *Ty); 80 llvm::DIType CreateType(const ComplexType *Ty); 81 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F); 82 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F); 83 llvm::DIType CreateType(const ObjCObjectPointerType *Ty, 84 llvm::DIFile F); 85 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F); 86 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F); 87 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F); 88 llvm::DIType CreateType(const TagType *Ty); 89 llvm::DIType CreateType(const RecordType *Ty); 90 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F); 91 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F); 92 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F); 93 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F); 94 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F); 95 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit); 96 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F); 97 llvm::DIType CreateEnumType(const EnumDecl *ED); 98 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method, 99 llvm::DIFile F); 100 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F); 101 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N); 102 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F); 103 llvm::DIType CreatePointerLikeType(unsigned Tag, 104 const Type *Ty, QualType PointeeTy, 105 llvm::DIFile F); 106 107 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method, 108 llvm::DIFile F, 109 llvm::DIType RecordTy); 110 111 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl, 112 llvm::DIFile F, 113 llvm::SmallVectorImpl<llvm::Value *> &E, 114 llvm::DIType T); 115 116 void CollectCXXFriends(const CXXRecordDecl *Decl, 117 llvm::DIFile F, 118 llvm::SmallVectorImpl<llvm::Value *> &EltTys, 119 llvm::DIType RecordTy); 120 121 void CollectCXXBases(const CXXRecordDecl *Decl, 122 llvm::DIFile F, 123 llvm::SmallVectorImpl<llvm::Value *> &EltTys, 124 llvm::DIType RecordTy); 125 126 127 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F, 128 llvm::SmallVectorImpl<llvm::Value *> &E); 129 130 void CollectVTableInfo(const CXXRecordDecl *Decl, 131 llvm::DIFile F, 132 llvm::SmallVectorImpl<llvm::Value *> &EltTys); 133 134public: 135 CGDebugInfo(CodeGenModule &CGM); 136 ~CGDebugInfo(); 137 138 /// setLocation - Update the current source location. If \arg loc is 139 /// invalid it is ignored. 140 void setLocation(SourceLocation Loc); 141 142 /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of 143 /// source line. 144 void EmitStopPoint(CGBuilderTy &Builder); 145 146 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate 147 /// start of a new function. 148 void EmitFunctionStart(GlobalDecl GD, QualType FnType, 149 llvm::Function *Fn, CGBuilderTy &Builder); 150 151 /// EmitFunctionEnd - Constructs the debug code for exiting a function. 152 void EmitFunctionEnd(CGBuilderTy &Builder); 153 154 /// UpdateLineDirectiveRegion - Update region stack only if #line directive 155 /// has introduced scope change. 156 void UpdateLineDirectiveRegion(CGBuilderTy &Builder); 157 158 /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start 159 /// of a new block. 160 void EmitRegionStart(CGBuilderTy &Builder); 161 162 /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a 163 /// block. 164 void EmitRegionEnd(CGBuilderTy &Builder); 165 166 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic 167 /// variable declaration. 168 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI, 169 CGBuilderTy &Builder); 170 171 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an 172 /// imported variable declaration in a block. 173 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable, 174 llvm::Value *storage, 175 CGBuilderTy &Builder, 176 const CGBlockInfo &blockInfo); 177 178 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument 179 /// variable declaration. 180 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI, 181 CGBuilderTy &Builder); 182 183 /// EmitGlobalVariable - Emit information about a global variable. 184 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl); 185 186 /// EmitGlobalVariable - Emit information about an objective-c interface. 187 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl); 188 189 /// EmitGlobalVariable - Emit global variable's debug info. 190 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init); 191 192 /// getOrCreateRecordType - Emit record type's standalone debug info. 193 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L); 194private: 195 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration. 196 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI, 197 CGBuilderTy &Builder); 198 199 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable 200 /// declaration from an enclosing block. 201 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI, 202 CGBuilderTy &Builder, const CGBlockInfo &blockInfo); 203 204 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. 205 // See BuildByRefType. 206 llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD, 207 uint64_t *OffSet); 208 209 /// getContextDescriptor - Get context info for the decl. 210 llvm::DIDescriptor getContextDescriptor(const Decl *Decl); 211 212 /// getCurrentDirname - Return current directory name. 213 llvm::StringRef getCurrentDirname(); 214 215 /// CreateCompileUnit - Create new compile unit. 216 void CreateCompileUnit(); 217 218 /// getOrCreateFile - Get the file debug info descriptor for the input 219 /// location. 220 llvm::DIFile getOrCreateFile(SourceLocation Loc); 221 222 /// getOrCreateMainFile - Get the file info for main compile unit. 223 llvm::DIFile getOrCreateMainFile(); 224 225 /// getOrCreateType - Get the type from the cache or create a new type if 226 /// necessary. 227 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F); 228 229 /// CreateTypeNode - Create type metadata for a source language type. 230 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F); 231 232 /// CreateMemberType - Create new member and increase Offset by FType's size. 233 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType, 234 llvm::StringRef Name, uint64_t *Offset); 235 236 /// getFunctionName - Get function name for the given FunctionDecl. If the 237 /// name is constructred on demand (e.g. C++ destructor) then the name 238 /// is stored on the side. 239 llvm::StringRef getFunctionName(const FunctionDecl *FD); 240 241 /// getObjCMethodName - Returns the unmangled name of an Objective-C method. 242 /// This is the display name for the debugging info. 243 llvm::StringRef getObjCMethodName(const ObjCMethodDecl *FD); 244 245 /// getClassName - Get class name including template argument list. 246 llvm::StringRef getClassName(RecordDecl *RD); 247 248 /// getVTableName - Get vtable name for the given Class. 249 llvm::StringRef getVTableName(const CXXRecordDecl *Decl); 250 251 /// getLineNumber - Get line number for the location. If location is invalid 252 /// then use current location. 253 unsigned getLineNumber(SourceLocation Loc); 254 255 /// getColumnNumber - Get column number for the location. If location is 256 /// invalid then use current location. 257 unsigned getColumnNumber(SourceLocation Loc); 258}; 259} // namespace CodeGen 260} // namespace clang 261 262 263#endif 264