1 //===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the source-level debug info generator for llvm translation.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H
17 #include "CGBuilder.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/Type.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Frontend/CodeGenOptions.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/IR/DIBuilder.h"
24 #include "llvm/IR/DebugInfo.h"
25 #include "llvm/IR/ValueHandle.h"
26 #include "llvm/Support/Allocator.h"
35 class ObjCInterfaceDecl;
37 class ClassTemplateSpecializationDecl;
43 class CodeGenFunction;
46 /// \brief This class gathers all debug information during compilation
47 /// and is responsible for emitting to llvm globals or pass directly to
50 friend class ApplyDebugLocation;
51 friend class SaveAndRestoreLocation;
53 const CodeGenOptions::DebugInfoKind DebugKind;
54 llvm::DIBuilder DBuilder;
55 llvm::DICompileUnit *TheCU = nullptr;
56 SourceLocation CurLoc;
57 llvm::DIType *VTablePtrType = nullptr;
58 llvm::DIType *ClassTy = nullptr;
59 llvm::DICompositeType *ObjTy = nullptr;
60 llvm::DIType *SelTy = nullptr;
61 llvm::DIType *OCLImage1dDITy = nullptr;
62 llvm::DIType *OCLImage1dArrayDITy = nullptr;
63 llvm::DIType *OCLImage1dBufferDITy = nullptr;
64 llvm::DIType *OCLImage2dDITy = nullptr;
65 llvm::DIType *OCLImage2dArrayDITy = nullptr;
66 llvm::DIType *OCLImage3dDITy = nullptr;
67 llvm::DIType *OCLEventDITy = nullptr;
68 llvm::DIType *BlockLiteralGeneric = nullptr;
70 /// \brief Cache of previously constructed Types.
71 llvm::DenseMap<const void *, llvm::TrackingMDRef> TypeCache;
73 struct ObjCInterfaceCacheEntry {
74 const ObjCInterfaceType *Type;
77 ObjCInterfaceCacheEntry(const ObjCInterfaceType *Type, llvm::DIType *Decl,
79 : Type(Type), Decl(Decl), Unit(Unit) {}
82 /// \brief Cache of previously constructed interfaces
84 llvm::SmallVector<ObjCInterfaceCacheEntry, 32> ObjCInterfaceCache;
86 /// \brief Cache of references to AST files such as PCHs or modules.
87 llvm::DenseMap<uint64_t, llvm::DIModule *> ModuleRefCache;
89 /// \brief list of interfaces we want to keep even if orphaned.
90 std::vector<void *> RetainedTypes;
92 /// \brief Cache of forward declared types to RAUW at the end of
94 std::vector<std::pair<const TagType *, llvm::TrackingMDRef>> ReplaceMap;
96 /// \brief Cache of replaceable forward declarartions (functions and
97 /// variables) to RAUW at the end of compilation.
98 std::vector<std::pair<const DeclaratorDecl *, llvm::TrackingMDRef>>
101 // LexicalBlockStack - Keep track of our current nested lexical block.
102 std::vector<llvm::TypedTrackingMDRef<llvm::DIScope>> LexicalBlockStack;
103 llvm::DenseMap<const Decl *, llvm::TrackingMDRef> RegionMap;
104 // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
105 // beginning of a function. This is used to pop unbalanced regions at
106 // the end of a function.
107 std::vector<unsigned> FnBeginRegionCount;
109 /// \brief This is a storage for names that are
110 /// constructed on demand. For example, C++ destructors, C++ operators etc..
111 llvm::BumpPtrAllocator DebugInfoNames;
114 llvm::DenseMap<const char *, llvm::TrackingMDRef> DIFileCache;
115 llvm::DenseMap<const FunctionDecl *, llvm::TrackingMDRef> SPCache;
116 /// \brief Cache declarations relevant to DW_TAG_imported_declarations (C++
117 /// using declarations) that aren't covered by other more specific caches.
118 llvm::DenseMap<const Decl *, llvm::TrackingMDRef> DeclCache;
119 llvm::DenseMap<const NamespaceDecl *, llvm::TrackingMDRef> NameSpaceCache;
120 llvm::DenseMap<const NamespaceAliasDecl *, llvm::TrackingMDRef>
122 llvm::DenseMap<const Decl *, llvm::TrackingMDRef> StaticDataMemberCache;
124 /// Helper functions for getOrCreateType.
125 unsigned Checksum(const ObjCInterfaceDecl *InterfaceDecl);
126 llvm::DIType *CreateType(const BuiltinType *Ty);
127 llvm::DIType *CreateType(const ComplexType *Ty);
128 llvm::DIType *CreateQualifiedType(QualType Ty, llvm::DIFile *Fg);
129 llvm::DIType *CreateType(const TypedefType *Ty, llvm::DIFile *Fg);
130 llvm::DIType *CreateType(const TemplateSpecializationType *Ty,
132 llvm::DIType *CreateType(const ObjCObjectPointerType *Ty, llvm::DIFile *F);
133 llvm::DIType *CreateType(const PointerType *Ty, llvm::DIFile *F);
134 llvm::DIType *CreateType(const BlockPointerType *Ty, llvm::DIFile *F);
135 llvm::DIType *CreateType(const FunctionType *Ty, llvm::DIFile *F);
136 llvm::DIType *CreateType(const RecordType *Tyg);
137 llvm::DIType *CreateTypeDefinition(const RecordType *Ty);
138 llvm::DICompositeType *CreateLimitedType(const RecordType *Ty);
139 void CollectContainingType(const CXXRecordDecl *RD,
140 llvm::DICompositeType *CT);
141 llvm::DIType *CreateType(const ObjCInterfaceType *Ty, llvm::DIFile *F);
142 llvm::DIType *CreateTypeDefinition(const ObjCInterfaceType *Ty,
144 llvm::DIType *CreateType(const ObjCObjectType *Ty, llvm::DIFile *F);
145 llvm::DIType *CreateType(const VectorType *Ty, llvm::DIFile *F);
146 llvm::DIType *CreateType(const ArrayType *Ty, llvm::DIFile *F);
147 llvm::DIType *CreateType(const LValueReferenceType *Ty, llvm::DIFile *F);
148 llvm::DIType *CreateType(const RValueReferenceType *Ty, llvm::DIFile *Unit);
149 llvm::DIType *CreateType(const MemberPointerType *Ty, llvm::DIFile *F);
150 llvm::DIType *CreateType(const AtomicType *Ty, llvm::DIFile *F);
151 llvm::DIType *CreateEnumType(const EnumType *Ty);
152 llvm::DIType *CreateTypeDefinition(const EnumType *Ty);
153 llvm::DIType *CreateSelfType(const QualType &QualTy, llvm::DIType *Ty);
154 llvm::DIType *getTypeOrNull(const QualType);
155 llvm::DISubroutineType *getOrCreateMethodType(const CXXMethodDecl *Method,
157 llvm::DISubroutineType *
158 getOrCreateInstanceMethodType(QualType ThisPtr, const FunctionProtoType *Func,
160 llvm::DISubroutineType *
161 getOrCreateFunctionType(const Decl *D, QualType FnType, llvm::DIFile *F);
162 llvm::DIType *getOrCreateVTablePtrType(llvm::DIFile *F);
163 llvm::DINamespace *getOrCreateNameSpace(const NamespaceDecl *N);
164 llvm::DIType *getOrCreateTypeDeclaration(QualType PointeeTy, llvm::DIFile *F);
165 llvm::DIType *CreatePointerLikeType(llvm::dwarf::Tag Tag, const Type *Ty,
166 QualType PointeeTy, llvm::DIFile *F);
168 llvm::Value *getCachedInterfaceTypeOrNull(const QualType Ty);
169 llvm::DIType *getOrCreateStructPtrType(StringRef Name, llvm::DIType *&Cache);
171 llvm::DISubprogram *CreateCXXMemberFunction(const CXXMethodDecl *Method,
173 llvm::DIType *RecordTy);
175 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl, llvm::DIFile *F,
176 SmallVectorImpl<llvm::Metadata *> &E,
179 void CollectCXXBases(const CXXRecordDecl *Decl, llvm::DIFile *F,
180 SmallVectorImpl<llvm::Metadata *> &EltTys,
181 llvm::DIType *RecordTy);
183 llvm::DINodeArray CollectTemplateParams(const TemplateParameterList *TPList,
184 ArrayRef<TemplateArgument> TAList,
186 llvm::DINodeArray CollectFunctionTemplateParams(const FunctionDecl *FD,
189 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
192 llvm::DIType *createFieldType(StringRef name, QualType type,
193 uint64_t sizeInBitsOverride, SourceLocation loc,
194 AccessSpecifier AS, uint64_t offsetInBits,
195 llvm::DIFile *tunit, llvm::DIScope *scope,
196 const RecordDecl *RD = nullptr);
198 // Helpers for collecting fields of a record.
199 void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
200 SmallVectorImpl<llvm::Metadata *> &E,
201 llvm::DIType *RecordTy);
202 llvm::DIDerivedType *CreateRecordStaticField(const VarDecl *Var,
203 llvm::DIType *RecordTy,
204 const RecordDecl *RD);
205 void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits,
207 SmallVectorImpl<llvm::Metadata *> &E,
208 llvm::DIType *RecordTy, const RecordDecl *RD);
209 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile *F,
210 SmallVectorImpl<llvm::Metadata *> &E,
211 llvm::DICompositeType *RecordTy);
213 void CollectVTableInfo(const CXXRecordDecl *Decl, llvm::DIFile *F,
214 SmallVectorImpl<llvm::Metadata *> &EltTys);
216 // CreateLexicalBlock - Create a new lexical block node and push it on
218 void CreateLexicalBlock(SourceLocation Loc);
221 CGDebugInfo(CodeGenModule &CGM);
226 /// \brief Update the current source location. If \arg loc is
227 /// invalid it is ignored.
228 void setLocation(SourceLocation Loc);
230 /// \brief Emit metadata to indicate a change in line/column
231 /// information in the source file.
232 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
234 /// \brief Emit a call to llvm.dbg.function.start to indicate
235 /// start of a new function.
236 /// \param Loc The location of the function header.
237 /// \param ScopeLoc The location of the function body.
238 void EmitFunctionStart(GlobalDecl GD,
239 SourceLocation Loc, SourceLocation ScopeLoc,
240 QualType FnType, llvm::Function *Fn,
241 CGBuilderTy &Builder);
243 /// \brief Constructs the debug code for exiting a function.
244 void EmitFunctionEnd(CGBuilderTy &Builder);
246 /// \brief Emit metadata to indicate the beginning of a
247 /// new lexical block and push the block onto the stack.
248 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
250 /// \brief Emit metadata to indicate the end of a new lexical
251 /// block and pop the current block.
252 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
254 /// \brief Emit call to llvm.dbg.declare for an automatic
255 /// variable declaration.
256 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
257 CGBuilderTy &Builder);
259 /// \brief Emit call to llvm.dbg.declare for an
260 /// imported variable declaration in a block.
261 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
262 llvm::Value *storage,
263 CGBuilderTy &Builder,
264 const CGBlockInfo &blockInfo,
265 llvm::Instruction *InsertPoint = 0);
267 /// \brief Emit call to llvm.dbg.declare for an argument
268 /// variable declaration.
269 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
270 unsigned ArgNo, CGBuilderTy &Builder);
272 /// \brief Emit call to
273 /// llvm.dbg.declare for the block-literal argument to a block
274 /// invocation function.
275 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
276 llvm::Value *Arg, unsigned ArgNo,
277 llvm::Value *LocalAddr,
278 CGBuilderTy &Builder);
280 /// \brief Emit information about a global variable.
281 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
283 /// \brief Emit global variable's debug info.
284 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
286 /// \brief Emit C++ using directive.
287 void EmitUsingDirective(const UsingDirectiveDecl &UD);
289 /// \brief Emit the type explicitly casted to.
290 void EmitExplicitCastType(QualType Ty);
292 /// \brief Emit C++ using declaration.
293 void EmitUsingDecl(const UsingDecl &UD);
295 /// \brief Emit an @import declaration.
296 void EmitImportDecl(const ImportDecl &ID);
298 /// \brief Emit C++ namespace alias.
299 llvm::DIImportedEntity *EmitNamespaceAlias(const NamespaceAliasDecl &NA);
301 /// \brief Emit record type's standalone debug info.
302 llvm::DIType *getOrCreateRecordType(QualType Ty, SourceLocation L);
304 /// \brief Emit an objective c interface type standalone
306 llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc);
308 void completeType(const EnumDecl *ED);
309 void completeType(const RecordDecl *RD);
310 void completeRequiredType(const RecordDecl *RD);
311 void completeClassData(const RecordDecl *RD);
313 void completeTemplateDefinition(const ClassTemplateSpecializationDecl &SD);
316 /// \brief Emit call to llvm.dbg.declare for a variable declaration.
317 /// Tag accepts custom types DW_TAG_arg_variable and DW_TAG_auto_variable,
318 /// otherwise would be of type llvm::dwarf::Tag.
319 void EmitDeclare(const VarDecl *decl, llvm::dwarf::Tag Tag, llvm::Value *AI,
320 unsigned ArgNo, CGBuilderTy &Builder);
322 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
323 // See BuildByRefType.
324 llvm::DIType *EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
327 /// \brief Get context info for the decl.
328 llvm::DIScope *getContextDescriptor(const Decl *Decl);
330 llvm::DIScope *getCurrentContextDescriptor(const Decl *Decl);
332 /// \brief Create a forward decl for a RecordType in a given context.
333 llvm::DICompositeType *getOrCreateRecordFwdDecl(const RecordType *,
336 /// \brief Return current directory name.
337 StringRef getCurrentDirname();
339 /// \brief Create new compile unit.
340 void CreateCompileUnit();
342 /// \brief Get the file debug info descriptor for the input
344 llvm::DIFile *getOrCreateFile(SourceLocation Loc);
346 /// \brief Get the file info for main compile unit.
347 llvm::DIFile *getOrCreateMainFile();
349 /// \brief Get the type from the cache or create a new type if
351 llvm::DIType *getOrCreateType(QualType Ty, llvm::DIFile *Fg);
353 /// \brief Get a reference to a clang module.
355 getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod);
357 /// \brief Get the type from the cache or create a new
358 /// partial type if necessary.
359 llvm::DIType *getOrCreateLimitedType(const RecordType *Ty, llvm::DIFile *F);
361 /// \brief Create type metadata for a source language type.
362 llvm::DIType *CreateTypeNode(QualType Ty, llvm::DIFile *Fg);
364 /// \brief return the underlying ObjCInterfaceDecl
365 /// if Ty is an ObjCInterface or a pointer to one.
366 ObjCInterfaceDecl* getObjCInterfaceDecl(QualType Ty);
368 /// \brief Create new member and increase Offset by FType's size.
369 llvm::DIType *CreateMemberType(llvm::DIFile *Unit, QualType FType,
370 StringRef Name, uint64_t *Offset);
372 /// \brief Retrieve the DIDescriptor, if any, for the canonical form of this
374 llvm::DINode *getDeclarationOrDefinition(const Decl *D);
376 /// \brief Return debug info descriptor to describe method
377 /// declaration for the given method definition.
378 llvm::DISubprogram *getFunctionDeclaration(const Decl *D);
380 /// Return debug info descriptor to describe in-class static data member
381 /// declaration for the given out-of-class definition.
382 llvm::DIDerivedType *
383 getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D);
385 /// \brief Create a subprogram describing the forward
386 /// decalration represented in the given FunctionDecl.
387 llvm::DISubprogram *getFunctionForwardDeclaration(const FunctionDecl *FD);
389 /// \brief Create a global variable describing the forward decalration
390 /// represented in the given VarDecl.
391 llvm::DIGlobalVariable *
392 getGlobalVariableForwardDeclaration(const VarDecl *VD);
394 /// Return a global variable that represents one of the collection of
395 /// global variables created for an anonmyous union.
396 llvm::DIGlobalVariable *
397 CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile *Unit,
398 unsigned LineNo, StringRef LinkageName,
399 llvm::GlobalVariable *Var, llvm::DIScope *DContext);
401 /// \brief Get function name for the given FunctionDecl. If the
402 /// name is constructed on demand (e.g. C++ destructor) then the name
403 /// is stored on the side.
404 StringRef getFunctionName(const FunctionDecl *FD);
406 /// \brief Returns the unmangled name of an Objective-C method.
407 /// This is the display name for the debugging info.
408 StringRef getObjCMethodName(const ObjCMethodDecl *FD);
410 /// \brief Return selector name. This is used for debugging
412 StringRef getSelectorName(Selector S);
414 /// \brief Get class name including template argument list.
415 StringRef getClassName(const RecordDecl *RD);
417 /// \brief Get vtable name for the given Class.
418 StringRef getVTableName(const CXXRecordDecl *Decl);
420 /// \brief Get line number for the location. If location is invalid
421 /// then use current location.
422 unsigned getLineNumber(SourceLocation Loc);
424 /// \brief Get column number for the location. If location is
425 /// invalid then use current location.
426 /// \param Force Assume DebugColumnInfo option is true.
427 unsigned getColumnNumber(SourceLocation Loc, bool Force=false);
429 /// \brief Collect various properties of a FunctionDecl.
430 /// \param GD A GlobalDecl whose getDecl() must return a FunctionDecl.
431 void collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
432 StringRef &Name, StringRef &LinkageName,
433 llvm::DIScope *&FDContext,
434 llvm::DINodeArray &TParamsArray,
437 /// \brief Collect various properties of a VarDecl.
438 void collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
439 unsigned &LineNo, QualType &T, StringRef &Name,
440 StringRef &LinkageName, llvm::DIScope *&VDContext);
442 /// \brief Allocate a copy of \p A using the DebugInfoNames allocator
443 /// and return a reference to it. If multiple arguments are given the strings
444 /// are concatenated.
445 StringRef internString(StringRef A, StringRef B = StringRef()) {
446 char *Data = DebugInfoNames.Allocate<char>(A.size() + B.size());
447 std::memcpy(Data, A.data(), A.size());
448 std::memcpy(Data + A.size(), B.data(), B.size());
449 return StringRef(Data, A.size() + B.size());
453 /// \brief A scoped helper to set the current debug location to the specified
454 /// location or preferred location of the specified Expr.
455 class ApplyDebugLocation {
457 void init(SourceLocation TemporaryLocation, bool DefaultToEmpty = false);
458 ApplyDebugLocation(CodeGenFunction &CGF, bool DefaultToEmpty,
459 SourceLocation TemporaryLocation);
461 llvm::DebugLoc OriginalLocation;
462 CodeGenFunction &CGF;
465 /// \brief Set the location to the (valid) TemporaryLocation.
466 ApplyDebugLocation(CodeGenFunction &CGF, SourceLocation TemporaryLocation);
467 ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E);
468 ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc);
470 ~ApplyDebugLocation();
472 /// \brief Apply TemporaryLocation if it is valid. Otherwise switch to an
473 /// artificial debug location that has a valid scope, but no line information.
475 /// Artificial locations are useful when emitting compiler-generated helper
476 /// functions that have no source location associated with them. The DWARF
477 /// specification allows the compiler to use the special line number 0 to
478 /// indicate code that can not be attributed to any source location. Note that
479 /// passing an empty SourceLocation to CGDebugInfo::setLocation() will result
480 /// in the last valid location being reused.
481 static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF) {
482 return ApplyDebugLocation(CGF, false, SourceLocation());
484 /// \brief Apply TemporaryLocation if it is valid. Otherwise switch to an
485 /// artificial debug location that has a valid scope, but no line information.
486 static ApplyDebugLocation CreateDefaultArtificial(CodeGenFunction &CGF,
487 SourceLocation TemporaryLocation) {
488 return ApplyDebugLocation(CGF, false, TemporaryLocation);
491 /// \brief Set the IRBuilder to not attach debug locations. Note that passing
492 /// an empty SourceLocation to CGDebugInfo::setLocation() will result in the
493 /// last valid location being reused. Note that all instructions that do not
494 /// have a location at the beginning of a function are counted towards to
495 /// funciton prologue.
496 static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF) {
497 return ApplyDebugLocation(CGF, true, SourceLocation());
500 /// \brief Apply TemporaryLocation if it is valid. Otherwise set the IRBuilder
501 /// to not attach debug locations.
502 static ApplyDebugLocation CreateDefaultEmpty(CodeGenFunction &CGF,
503 SourceLocation TemporaryLocation) {
504 return ApplyDebugLocation(CGF, true, TemporaryLocation);
510 } // namespace CodeGen