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