1 //===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This is the source-level debug info generator for llvm translation. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H 14 #define LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H 15 16 #include "CGBuilder.h" 17 #include "clang/AST/DeclCXX.h" 18 #include "clang/AST/Expr.h" 19 #include "clang/AST/ExternalASTSource.h" 20 #include "clang/AST/Type.h" 21 #include "clang/AST/TypeOrdering.h" 22 #include "clang/Basic/CodeGenOptions.h" 23 #include "clang/Basic/Module.h" 24 #include "clang/Basic/SourceLocation.h" 25 #include "llvm/ADT/DenseMap.h" 26 #include "llvm/ADT/DenseSet.h" 27 #include "llvm/ADT/Optional.h" 28 #include "llvm/IR/DIBuilder.h" 29 #include "llvm/IR/DebugInfo.h" 30 #include "llvm/IR/ValueHandle.h" 31 #include "llvm/Support/Allocator.h" 32 33 namespace llvm { 34 class MDNode; 35 } 36 37 namespace clang { 38 class ClassTemplateSpecializationDecl; 39 class GlobalDecl; 40 class ModuleMap; 41 class ObjCInterfaceDecl; 42 class ObjCIvarDecl; 43 class UsingDecl; 44 class VarDecl; 45 enum class DynamicInitKind : unsigned; 46 47 namespace CodeGen { 48 class CodeGenModule; 49 class CodeGenFunction; 50 class CGBlockInfo; 51 52 /// This class gathers all debug information during compilation and is 53 /// responsible for emitting to llvm globals or pass directly to the 54 /// backend. 55 class CGDebugInfo { 56 friend class ApplyDebugLocation; 57 friend class SaveAndRestoreLocation; 58 CodeGenModule &CGM; 59 const codegenoptions::DebugInfoKind DebugKind; 60 bool DebugTypeExtRefs; 61 llvm::DIBuilder DBuilder; 62 llvm::DICompileUnit *TheCU = nullptr; 63 ModuleMap *ClangModuleMap = nullptr; 64 ASTSourceDescriptor PCHDescriptor; 65 SourceLocation CurLoc; 66 llvm::MDNode *CurInlinedAt = nullptr; 67 llvm::DIType *VTablePtrType = nullptr; 68 llvm::DIType *ClassTy = nullptr; 69 llvm::DICompositeType *ObjTy = nullptr; 70 llvm::DIType *SelTy = nullptr; 71 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 72 llvm::DIType *SingletonId = nullptr; 73 #include "clang/Basic/OpenCLImageTypes.def" 74 llvm::DIType *OCLSamplerDITy = nullptr; 75 llvm::DIType *OCLEventDITy = nullptr; 76 llvm::DIType *OCLClkEventDITy = nullptr; 77 llvm::DIType *OCLQueueDITy = nullptr; 78 llvm::DIType *OCLNDRangeDITy = nullptr; 79 llvm::DIType *OCLReserveIDDITy = nullptr; 80 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 81 llvm::DIType *Id##Ty = nullptr; 82 #include "clang/Basic/OpenCLExtensionTypes.def" 83 84 /// Cache of previously constructed Types. 85 llvm::DenseMap<const void *, llvm::TrackingMDRef> TypeCache; 86 87 std::map<llvm::StringRef, llvm::StringRef, std::greater<llvm::StringRef>> 88 DebugPrefixMap; 89 90 /// Cache that maps VLA types to size expressions for that type, 91 /// represented by instantiated Metadata nodes. 92 llvm::SmallDenseMap<QualType, llvm::Metadata *> SizeExprCache; 93 94 /// Callbacks to use when printing names and types. 95 class PrintingCallbacks final : public clang::PrintingCallbacks { 96 const CGDebugInfo &Self; 97 98 public: 99 PrintingCallbacks(const CGDebugInfo &Self) : Self(Self) {} 100 std::string remapPath(StringRef Path) const override { 101 return Self.remapDIPath(Path); 102 } 103 }; 104 PrintingCallbacks PrintCB = {*this}; 105 106 struct ObjCInterfaceCacheEntry { 107 const ObjCInterfaceType *Type; 108 llvm::DIType *Decl; 109 llvm::DIFile *Unit; 110 ObjCInterfaceCacheEntry(const ObjCInterfaceType *Type, llvm::DIType *Decl, 111 llvm::DIFile *Unit) 112 : Type(Type), Decl(Decl), Unit(Unit) {} 113 }; 114 115 /// Cache of previously constructed interfaces which may change. 116 llvm::SmallVector<ObjCInterfaceCacheEntry, 32> ObjCInterfaceCache; 117 118 /// Cache of forward declarations for methods belonging to the interface. 119 /// The extra bit on the DISubprogram specifies whether a method is 120 /// "objc_direct". 121 llvm::DenseMap<const ObjCInterfaceDecl *, 122 std::vector<llvm::PointerIntPair<llvm::DISubprogram *, 1>>> 123 ObjCMethodCache; 124 125 /// Cache of references to clang modules and precompiled headers. 126 llvm::DenseMap<const Module *, llvm::TrackingMDRef> ModuleCache; 127 128 /// List of interfaces we want to keep even if orphaned. 129 std::vector<void *> RetainedTypes; 130 131 /// Cache of forward declared types to RAUW at the end of compilation. 132 std::vector<std::pair<const TagType *, llvm::TrackingMDRef>> ReplaceMap; 133 134 /// Cache of replaceable forward declarations (functions and 135 /// variables) to RAUW at the end of compilation. 136 std::vector<std::pair<const DeclaratorDecl *, llvm::TrackingMDRef>> 137 FwdDeclReplaceMap; 138 139 /// Keep track of our current nested lexical block. 140 std::vector<llvm::TypedTrackingMDRef<llvm::DIScope>> LexicalBlockStack; 141 llvm::DenseMap<const Decl *, llvm::TrackingMDRef> RegionMap; 142 /// Keep track of LexicalBlockStack counter at the beginning of a 143 /// function. This is used to pop unbalanced regions at the end of a 144 /// function. 145 std::vector<unsigned> FnBeginRegionCount; 146 147 /// This is a storage for names that are constructed on demand. For 148 /// example, C++ destructors, C++ operators etc.. 149 llvm::BumpPtrAllocator DebugInfoNames; 150 StringRef CWDName; 151 152 llvm::DenseMap<const char *, llvm::TrackingMDRef> DIFileCache; 153 llvm::DenseMap<const FunctionDecl *, llvm::TrackingMDRef> SPCache; 154 /// Cache declarations relevant to DW_TAG_imported_declarations (C++ 155 /// using declarations) that aren't covered by other more specific caches. 156 llvm::DenseMap<const Decl *, llvm::TrackingMDRef> DeclCache; 157 llvm::DenseMap<const NamespaceDecl *, llvm::TrackingMDRef> NamespaceCache; 158 llvm::DenseMap<const NamespaceAliasDecl *, llvm::TrackingMDRef> 159 NamespaceAliasCache; 160 llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIDerivedType>> 161 StaticDataMemberCache; 162 163 /// Helper functions for getOrCreateType. 164 /// @{ 165 /// Currently the checksum of an interface includes the number of 166 /// ivars and property accessors. 167 llvm::DIType *CreateType(const BuiltinType *Ty); 168 llvm::DIType *CreateType(const ComplexType *Ty); 169 llvm::DIType *CreateType(const AutoType *Ty); 170 llvm::DIType *CreateQualifiedType(QualType Ty, llvm::DIFile *Fg); 171 llvm::DIType *CreateType(const TypedefType *Ty, llvm::DIFile *Fg); 172 llvm::DIType *CreateType(const TemplateSpecializationType *Ty, 173 llvm::DIFile *Fg); 174 llvm::DIType *CreateType(const ObjCObjectPointerType *Ty, llvm::DIFile *F); 175 llvm::DIType *CreateType(const PointerType *Ty, llvm::DIFile *F); 176 llvm::DIType *CreateType(const BlockPointerType *Ty, llvm::DIFile *F); 177 llvm::DIType *CreateType(const FunctionType *Ty, llvm::DIFile *F); 178 /// Get structure or union type. 179 llvm::DIType *CreateType(const RecordType *Tyg); 180 llvm::DIType *CreateTypeDefinition(const RecordType *Ty); 181 llvm::DICompositeType *CreateLimitedType(const RecordType *Ty); 182 void CollectContainingType(const CXXRecordDecl *RD, 183 llvm::DICompositeType *CT); 184 /// Get Objective-C interface type. 185 llvm::DIType *CreateType(const ObjCInterfaceType *Ty, llvm::DIFile *F); 186 llvm::DIType *CreateTypeDefinition(const ObjCInterfaceType *Ty, 187 llvm::DIFile *F); 188 /// Get Objective-C object type. 189 llvm::DIType *CreateType(const ObjCObjectType *Ty, llvm::DIFile *F); 190 llvm::DIType *CreateType(const ObjCTypeParamType *Ty, llvm::DIFile *Unit); 191 192 llvm::DIType *CreateType(const VectorType *Ty, llvm::DIFile *F); 193 llvm::DIType *CreateType(const ArrayType *Ty, llvm::DIFile *F); 194 llvm::DIType *CreateType(const LValueReferenceType *Ty, llvm::DIFile *F); 195 llvm::DIType *CreateType(const RValueReferenceType *Ty, llvm::DIFile *Unit); 196 llvm::DIType *CreateType(const MemberPointerType *Ty, llvm::DIFile *F); 197 llvm::DIType *CreateType(const AtomicType *Ty, llvm::DIFile *F); 198 llvm::DIType *CreateType(const PipeType *Ty, llvm::DIFile *F); 199 /// Get enumeration type. 200 llvm::DIType *CreateEnumType(const EnumType *Ty); 201 llvm::DIType *CreateTypeDefinition(const EnumType *Ty); 202 /// Look up the completed type for a self pointer in the TypeCache and 203 /// create a copy of it with the ObjectPointer and Artificial flags 204 /// set. If the type is not cached, a new one is created. This should 205 /// never happen though, since creating a type for the implicit self 206 /// argument implies that we already parsed the interface definition 207 /// and the ivar declarations in the implementation. 208 llvm::DIType *CreateSelfType(const QualType &QualTy, llvm::DIType *Ty); 209 /// @} 210 211 /// Get the type from the cache or return null type if it doesn't 212 /// exist. 213 llvm::DIType *getTypeOrNull(const QualType); 214 /// Return the debug type for a C++ method. 215 /// \arg CXXMethodDecl is of FunctionType. This function type is 216 /// not updated to include implicit \c this pointer. Use this routine 217 /// to get a method type which includes \c this pointer. 218 llvm::DISubroutineType *getOrCreateMethodType(const CXXMethodDecl *Method, 219 llvm::DIFile *F, bool decl); 220 llvm::DISubroutineType * 221 getOrCreateInstanceMethodType(QualType ThisPtr, const FunctionProtoType *Func, 222 llvm::DIFile *Unit, bool decl); 223 llvm::DISubroutineType * 224 getOrCreateFunctionType(const Decl *D, QualType FnType, llvm::DIFile *F); 225 /// \return debug info descriptor for vtable. 226 llvm::DIType *getOrCreateVTablePtrType(llvm::DIFile *F); 227 228 /// \return namespace descriptor for the given namespace decl. 229 llvm::DINamespace *getOrCreateNamespace(const NamespaceDecl *N); 230 llvm::DIType *CreatePointerLikeType(llvm::dwarf::Tag Tag, const Type *Ty, 231 QualType PointeeTy, llvm::DIFile *F); 232 llvm::DIType *getOrCreateStructPtrType(StringRef Name, llvm::DIType *&Cache); 233 234 /// A helper function to create a subprogram for a single member 235 /// function GlobalDecl. 236 llvm::DISubprogram *CreateCXXMemberFunction(const CXXMethodDecl *Method, 237 llvm::DIFile *F, 238 llvm::DIType *RecordTy); 239 240 /// A helper function to collect debug info for C++ member 241 /// functions. This is used while creating debug info entry for a 242 /// Record. 243 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl, llvm::DIFile *F, 244 SmallVectorImpl<llvm::Metadata *> &E, 245 llvm::DIType *T); 246 247 /// A helper function to collect debug info for C++ base 248 /// classes. This is used while creating debug info entry for a 249 /// Record. 250 void CollectCXXBases(const CXXRecordDecl *Decl, llvm::DIFile *F, 251 SmallVectorImpl<llvm::Metadata *> &EltTys, 252 llvm::DIType *RecordTy); 253 254 /// Helper function for CollectCXXBases. 255 /// Adds debug info entries for types in Bases that are not in SeenTypes. 256 void CollectCXXBasesAux( 257 const CXXRecordDecl *RD, llvm::DIFile *Unit, 258 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy, 259 const CXXRecordDecl::base_class_const_range &Bases, 260 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes, 261 llvm::DINode::DIFlags StartingFlags); 262 263 /// A helper function to collect template parameters. 264 llvm::DINodeArray CollectTemplateParams(const TemplateParameterList *TPList, 265 ArrayRef<TemplateArgument> TAList, 266 llvm::DIFile *Unit); 267 /// A helper function to collect debug info for function template 268 /// parameters. 269 llvm::DINodeArray CollectFunctionTemplateParams(const FunctionDecl *FD, 270 llvm::DIFile *Unit); 271 272 /// A helper function to collect debug info for function template 273 /// parameters. 274 llvm::DINodeArray CollectVarTemplateParams(const VarDecl *VD, 275 llvm::DIFile *Unit); 276 277 /// A helper function to collect debug info for template 278 /// parameters. 279 llvm::DINodeArray 280 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS, 281 llvm::DIFile *F); 282 283 llvm::DIType *createFieldType(StringRef name, QualType type, 284 SourceLocation loc, AccessSpecifier AS, 285 uint64_t offsetInBits, uint32_t AlignInBits, 286 llvm::DIFile *tunit, llvm::DIScope *scope, 287 const RecordDecl *RD = nullptr); 288 289 llvm::DIType *createFieldType(StringRef name, QualType type, 290 SourceLocation loc, AccessSpecifier AS, 291 uint64_t offsetInBits, llvm::DIFile *tunit, 292 llvm::DIScope *scope, 293 const RecordDecl *RD = nullptr) { 294 return createFieldType(name, type, loc, AS, offsetInBits, 0, tunit, scope, 295 RD); 296 } 297 298 /// Create new bit field member. 299 llvm::DIType *createBitFieldType(const FieldDecl *BitFieldDecl, 300 llvm::DIScope *RecordTy, 301 const RecordDecl *RD); 302 303 /// Helpers for collecting fields of a record. 304 /// @{ 305 void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl, 306 SmallVectorImpl<llvm::Metadata *> &E, 307 llvm::DIType *RecordTy); 308 llvm::DIDerivedType *CreateRecordStaticField(const VarDecl *Var, 309 llvm::DIType *RecordTy, 310 const RecordDecl *RD); 311 void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits, 312 llvm::DIFile *F, 313 SmallVectorImpl<llvm::Metadata *> &E, 314 llvm::DIType *RecordTy, const RecordDecl *RD); 315 void CollectRecordNestedType(const TypeDecl *RD, 316 SmallVectorImpl<llvm::Metadata *> &E); 317 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile *F, 318 SmallVectorImpl<llvm::Metadata *> &E, 319 llvm::DICompositeType *RecordTy); 320 321 /// If the C++ class has vtable info then insert appropriate debug 322 /// info entry in EltTys vector. 323 void CollectVTableInfo(const CXXRecordDecl *Decl, llvm::DIFile *F, 324 SmallVectorImpl<llvm::Metadata *> &EltTys, 325 llvm::DICompositeType *RecordTy); 326 /// @} 327 328 /// Create a new lexical block node and push it on the stack. 329 void CreateLexicalBlock(SourceLocation Loc); 330 331 /// If target-specific LLVM \p AddressSpace directly maps to target-specific 332 /// DWARF address space, appends extended dereferencing mechanism to complex 333 /// expression \p Expr. Otherwise, does nothing. 334 /// 335 /// Extended dereferencing mechanism is has the following format: 336 /// DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef 337 void AppendAddressSpaceXDeref(unsigned AddressSpace, 338 SmallVectorImpl<int64_t> &Expr) const; 339 340 /// A helper function to collect debug info for the default elements of a 341 /// block. 342 /// 343 /// \returns The next available field offset after the default elements. 344 uint64_t collectDefaultElementTypesForBlockPointer( 345 const BlockPointerType *Ty, llvm::DIFile *Unit, 346 llvm::DIDerivedType *DescTy, unsigned LineNo, 347 SmallVectorImpl<llvm::Metadata *> &EltTys); 348 349 /// A helper function to collect debug info for the default fields of a 350 /// block. 351 void collectDefaultFieldsForBlockLiteralDeclare( 352 const CGBlockInfo &Block, const ASTContext &Context, SourceLocation Loc, 353 const llvm::StructLayout &BlockLayout, llvm::DIFile *Unit, 354 SmallVectorImpl<llvm::Metadata *> &Fields); 355 356 public: 357 CGDebugInfo(CodeGenModule &CGM); 358 ~CGDebugInfo(); 359 360 void finalize(); 361 362 /// Remap a given path with the current debug prefix map 363 std::string remapDIPath(StringRef) const; 364 365 /// Register VLA size expression debug node with the qualified type. 366 void registerVLASizeExpression(QualType Ty, llvm::Metadata *SizeExpr) { 367 SizeExprCache[Ty] = SizeExpr; 368 } 369 370 /// Module debugging: Support for building PCMs. 371 /// @{ 372 /// Set the main CU's DwoId field to \p Signature. 373 void setDwoId(uint64_t Signature); 374 375 /// When generating debug information for a clang module or 376 /// precompiled header, this module map will be used to determine 377 /// the module of origin of each Decl. 378 void setModuleMap(ModuleMap &MMap) { ClangModuleMap = &MMap; } 379 380 /// When generating debug information for a clang module or 381 /// precompiled header, this module map will be used to determine 382 /// the module of origin of each Decl. 383 void setPCHDescriptor(ASTSourceDescriptor PCH) { PCHDescriptor = PCH; } 384 /// @} 385 386 /// Update the current source location. If \arg loc is invalid it is 387 /// ignored. 388 void setLocation(SourceLocation Loc); 389 390 /// Return the current source location. This does not necessarily correspond 391 /// to the IRBuilder's current DebugLoc. 392 SourceLocation getLocation() const { return CurLoc; } 393 394 /// Update the current inline scope. All subsequent calls to \p EmitLocation 395 /// will create a location with this inlinedAt field. 396 void setInlinedAt(llvm::MDNode *InlinedAt) { CurInlinedAt = InlinedAt; } 397 398 /// \return the current inline scope. 399 llvm::MDNode *getInlinedAt() const { return CurInlinedAt; } 400 401 // Converts a SourceLocation to a DebugLoc 402 llvm::DebugLoc SourceLocToDebugLoc(SourceLocation Loc); 403 404 /// Emit metadata to indicate a change in line/column information in 405 /// the source file. If the location is invalid, the previous 406 /// location will be reused. 407 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc); 408 409 /// Emit a call to llvm.dbg.function.start to indicate 410 /// start of a new function. 411 /// \param Loc The location of the function header. 412 /// \param ScopeLoc The location of the function body. 413 void EmitFunctionStart(GlobalDecl GD, SourceLocation Loc, 414 SourceLocation ScopeLoc, QualType FnType, 415 llvm::Function *Fn, bool CurFnIsThunk, 416 CGBuilderTy &Builder); 417 418 /// Start a new scope for an inlined function. 419 void EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD); 420 /// End an inlined function scope. 421 void EmitInlineFunctionEnd(CGBuilderTy &Builder); 422 423 /// Emit debug info for a function declaration. 424 /// \p Fn is set only when a declaration for a debug call site gets created. 425 void EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, 426 QualType FnType, llvm::Function *Fn = nullptr); 427 428 /// Emit debug info for an extern function being called. 429 /// This is needed for call site debug info. 430 void EmitFuncDeclForCallSite(llvm::CallBase *CallOrInvoke, 431 QualType CalleeType, 432 const FunctionDecl *CalleeDecl); 433 434 /// Constructs the debug code for exiting a function. 435 void EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn); 436 437 /// Emit metadata to indicate the beginning of a new lexical block 438 /// and push the block onto the stack. 439 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc); 440 441 /// Emit metadata to indicate the end of a new lexical block and pop 442 /// the current block. 443 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc); 444 445 /// Emit call to \c llvm.dbg.declare for an automatic variable 446 /// declaration. 447 /// Returns a pointer to the DILocalVariable associated with the 448 /// llvm.dbg.declare, or nullptr otherwise. 449 llvm::DILocalVariable * 450 EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI, 451 CGBuilderTy &Builder, 452 const bool UsePointerValue = false); 453 454 /// Emit call to \c llvm.dbg.label for an label. 455 void EmitLabel(const LabelDecl *D, CGBuilderTy &Builder); 456 457 /// Emit call to \c llvm.dbg.declare for an imported variable 458 /// declaration in a block. 459 void EmitDeclareOfBlockDeclRefVariable( 460 const VarDecl *variable, llvm::Value *storage, CGBuilderTy &Builder, 461 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint = nullptr); 462 463 /// Emit call to \c llvm.dbg.declare for an argument variable 464 /// declaration. 465 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI, 466 unsigned ArgNo, CGBuilderTy &Builder); 467 468 /// Emit call to \c llvm.dbg.declare for the block-literal argument 469 /// to a block invocation function. 470 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, 471 StringRef Name, unsigned ArgNo, 472 llvm::AllocaInst *LocalAddr, 473 CGBuilderTy &Builder); 474 475 /// Emit information about a global variable. 476 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl); 477 478 /// Emit a constant global variable's debug info. 479 void EmitGlobalVariable(const ValueDecl *VD, const APValue &Init); 480 481 /// Emit information about an external variable. 482 void EmitExternalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl); 483 484 /// Emit C++ using directive. 485 void EmitUsingDirective(const UsingDirectiveDecl &UD); 486 487 /// Emit the type explicitly casted to. 488 void EmitExplicitCastType(QualType Ty); 489 490 /// Emit C++ using declaration. 491 void EmitUsingDecl(const UsingDecl &UD); 492 493 /// Emit an @import declaration. 494 void EmitImportDecl(const ImportDecl &ID); 495 496 /// Emit C++ namespace alias. 497 llvm::DIImportedEntity *EmitNamespaceAlias(const NamespaceAliasDecl &NA); 498 499 /// Emit record type's standalone debug info. 500 llvm::DIType *getOrCreateRecordType(QualType Ty, SourceLocation L); 501 502 /// Emit an Objective-C interface type standalone debug info. 503 llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc); 504 505 /// Emit standalone debug info for a type. 506 llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc); 507 508 /// Add heapallocsite metadata for MSAllocator calls. 509 void addHeapAllocSiteMetadata(llvm::Instruction *CallSite, QualType Ty, 510 SourceLocation Loc); 511 512 void completeType(const EnumDecl *ED); 513 void completeType(const RecordDecl *RD); 514 void completeRequiredType(const RecordDecl *RD); 515 void completeClassData(const RecordDecl *RD); 516 void completeClass(const RecordDecl *RD); 517 518 void completeTemplateDefinition(const ClassTemplateSpecializationDecl &SD); 519 void completeUnusedClass(const CXXRecordDecl &D); 520 521 /// Create debug info for a macro defined by a #define directive or a macro 522 /// undefined by a #undef directive. 523 llvm::DIMacro *CreateMacro(llvm::DIMacroFile *Parent, unsigned MType, 524 SourceLocation LineLoc, StringRef Name, 525 StringRef Value); 526 527 /// Create debug info for a file referenced by an #include directive. 528 llvm::DIMacroFile *CreateTempMacroFile(llvm::DIMacroFile *Parent, 529 SourceLocation LineLoc, 530 SourceLocation FileLoc); 531 532 private: 533 /// Emit call to llvm.dbg.declare for a variable declaration. 534 /// Returns a pointer to the DILocalVariable associated with the 535 /// llvm.dbg.declare, or nullptr otherwise. 536 llvm::DILocalVariable *EmitDeclare(const VarDecl *decl, llvm::Value *AI, 537 llvm::Optional<unsigned> ArgNo, 538 CGBuilderTy &Builder, 539 const bool UsePointerValue = false); 540 541 struct BlockByRefType { 542 /// The wrapper struct used inside the __block_literal struct. 543 llvm::DIType *BlockByRefWrapper; 544 /// The type as it appears in the source code. 545 llvm::DIType *WrappedType; 546 }; 547 548 /// Build up structure info for the byref. See \a BuildByRefType. 549 BlockByRefType EmitTypeForVarWithBlocksAttr(const VarDecl *VD, 550 uint64_t *OffSet); 551 552 /// Get context info for the DeclContext of \p Decl. 553 llvm::DIScope *getDeclContextDescriptor(const Decl *D); 554 /// Get context info for a given DeclContext \p Decl. 555 llvm::DIScope *getContextDescriptor(const Decl *Context, 556 llvm::DIScope *Default); 557 558 llvm::DIScope *getCurrentContextDescriptor(const Decl *Decl); 559 560 /// Create a forward decl for a RecordType in a given context. 561 llvm::DICompositeType *getOrCreateRecordFwdDecl(const RecordType *, 562 llvm::DIScope *); 563 564 /// Return current directory name. 565 StringRef getCurrentDirname(); 566 567 /// Create new compile unit. 568 void CreateCompileUnit(); 569 570 /// Compute the file checksum debug info for input file ID. 571 Optional<llvm::DIFile::ChecksumKind> 572 computeChecksum(FileID FID, SmallString<32> &Checksum) const; 573 574 /// Get the source of the given file ID. 575 Optional<StringRef> getSource(const SourceManager &SM, FileID FID); 576 577 /// Convenience function to get the file debug info descriptor for the input 578 /// location. 579 llvm::DIFile *getOrCreateFile(SourceLocation Loc); 580 581 /// Create a file debug info descriptor for a source file. 582 llvm::DIFile * 583 createFile(StringRef FileName, 584 Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo, 585 Optional<StringRef> Source); 586 587 /// Get the type from the cache or create a new type if necessary. 588 llvm::DIType *getOrCreateType(QualType Ty, llvm::DIFile *Fg); 589 590 /// Get a reference to a clang module. If \p CreateSkeletonCU is true, 591 /// this also creates a split dwarf skeleton compile unit. 592 llvm::DIModule *getOrCreateModuleRef(ASTSourceDescriptor Mod, 593 bool CreateSkeletonCU); 594 595 /// DebugTypeExtRefs: If \p D originated in a clang module, return it. 596 llvm::DIModule *getParentModuleOrNull(const Decl *D); 597 598 /// Get the type from the cache or create a new partial type if 599 /// necessary. 600 llvm::DICompositeType *getOrCreateLimitedType(const RecordType *Ty, 601 llvm::DIFile *F); 602 603 /// Create type metadata for a source language type. 604 llvm::DIType *CreateTypeNode(QualType Ty, llvm::DIFile *Fg); 605 606 /// Create new member and increase Offset by FType's size. 607 llvm::DIType *CreateMemberType(llvm::DIFile *Unit, QualType FType, 608 StringRef Name, uint64_t *Offset); 609 610 /// Retrieve the DIDescriptor, if any, for the canonical form of this 611 /// declaration. 612 llvm::DINode *getDeclarationOrDefinition(const Decl *D); 613 614 /// \return debug info descriptor to describe method 615 /// declaration for the given method definition. 616 llvm::DISubprogram *getFunctionDeclaration(const Decl *D); 617 618 /// \return debug info descriptor to the describe method declaration 619 /// for the given method definition. 620 /// \param FnType For Objective-C methods, their type. 621 /// \param LineNo The declaration's line number. 622 /// \param Flags The DIFlags for the method declaration. 623 /// \param SPFlags The subprogram-spcific flags for the method declaration. 624 llvm::DISubprogram * 625 getObjCMethodDeclaration(const Decl *D, llvm::DISubroutineType *FnType, 626 unsigned LineNo, llvm::DINode::DIFlags Flags, 627 llvm::DISubprogram::DISPFlags SPFlags); 628 629 /// \return debug info descriptor to describe in-class static data 630 /// member declaration for the given out-of-class definition. If D 631 /// is an out-of-class definition of a static data member of a 632 /// class, find its corresponding in-class declaration. 633 llvm::DIDerivedType * 634 getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D); 635 636 /// Helper that either creates a forward declaration or a stub. 637 llvm::DISubprogram *getFunctionFwdDeclOrStub(GlobalDecl GD, bool Stub); 638 639 /// Create a subprogram describing the forward declaration 640 /// represented in the given FunctionDecl wrapped in a GlobalDecl. 641 llvm::DISubprogram *getFunctionForwardDeclaration(GlobalDecl GD); 642 643 /// Create a DISubprogram describing the function 644 /// represented in the given FunctionDecl wrapped in a GlobalDecl. 645 llvm::DISubprogram *getFunctionStub(GlobalDecl GD); 646 647 /// Create a global variable describing the forward declaration 648 /// represented in the given VarDecl. 649 llvm::DIGlobalVariable * 650 getGlobalVariableForwardDeclaration(const VarDecl *VD); 651 652 /// Return a global variable that represents one of the collection of global 653 /// variables created for an anonmyous union. 654 /// 655 /// Recursively collect all of the member fields of a global 656 /// anonymous decl and create static variables for them. The first 657 /// time this is called it needs to be on a union and then from 658 /// there we can have additional unnamed fields. 659 llvm::DIGlobalVariableExpression * 660 CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile *Unit, 661 unsigned LineNo, StringRef LinkageName, 662 llvm::GlobalVariable *Var, llvm::DIScope *DContext); 663 664 665 /// Return flags which enable debug info emission for call sites, provided 666 /// that it is supported and enabled. 667 llvm::DINode::DIFlags getCallSiteRelatedAttrs() const; 668 669 /// Get the printing policy for producing names for debug info. 670 PrintingPolicy getPrintingPolicy() const; 671 672 /// Get function name for the given FunctionDecl. If the name is 673 /// constructed on demand (e.g., C++ destructor) then the name is 674 /// stored on the side. 675 StringRef getFunctionName(const FunctionDecl *FD); 676 677 /// Returns the unmangled name of an Objective-C method. 678 /// This is the display name for the debugging info. 679 StringRef getObjCMethodName(const ObjCMethodDecl *FD); 680 681 /// Return selector name. This is used for debugging 682 /// info. 683 StringRef getSelectorName(Selector S); 684 685 /// Get class name including template argument list. 686 StringRef getClassName(const RecordDecl *RD); 687 688 /// Get the vtable name for the given class. 689 StringRef getVTableName(const CXXRecordDecl *Decl); 690 691 /// Get the name to use in the debug info for a dynamic initializer or atexit 692 /// stub function. 693 StringRef getDynamicInitializerName(const VarDecl *VD, 694 DynamicInitKind StubKind, 695 llvm::Function *InitFn); 696 697 /// Get line number for the location. If location is invalid 698 /// then use current location. 699 unsigned getLineNumber(SourceLocation Loc); 700 701 /// Get column number for the location. If location is 702 /// invalid then use current location. 703 /// \param Force Assume DebugColumnInfo option is true. 704 unsigned getColumnNumber(SourceLocation Loc, bool Force = false); 705 706 /// Collect various properties of a FunctionDecl. 707 /// \param GD A GlobalDecl whose getDecl() must return a FunctionDecl. 708 void collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit, 709 StringRef &Name, StringRef &LinkageName, 710 llvm::DIScope *&FDContext, 711 llvm::DINodeArray &TParamsArray, 712 llvm::DINode::DIFlags &Flags); 713 714 /// Collect various properties of a VarDecl. 715 void collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit, 716 unsigned &LineNo, QualType &T, StringRef &Name, 717 StringRef &LinkageName, 718 llvm::MDTuple *&TemplateParameters, 719 llvm::DIScope *&VDContext); 720 721 /// Allocate a copy of \p A using the DebugInfoNames allocator 722 /// and return a reference to it. If multiple arguments are given the strings 723 /// are concatenated. 724 StringRef internString(StringRef A, StringRef B = StringRef()) { 725 char *Data = DebugInfoNames.Allocate<char>(A.size() + B.size()); 726 if (!A.empty()) 727 std::memcpy(Data, A.data(), A.size()); 728 if (!B.empty()) 729 std::memcpy(Data + A.size(), B.data(), B.size()); 730 return StringRef(Data, A.size() + B.size()); 731 } 732 }; 733 734 /// A scoped helper to set the current debug location to the specified 735 /// location or preferred location of the specified Expr. 736 class ApplyDebugLocation { 737 private: 738 void init(SourceLocation TemporaryLocation, bool DefaultToEmpty = false); 739 ApplyDebugLocation(CodeGenFunction &CGF, bool DefaultToEmpty, 740 SourceLocation TemporaryLocation); 741 742 llvm::DebugLoc OriginalLocation; 743 CodeGenFunction *CGF; 744 745 public: 746 /// Set the location to the (valid) TemporaryLocation. 747 ApplyDebugLocation(CodeGenFunction &CGF, SourceLocation TemporaryLocation); 748 ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E); 749 ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc); 750 ApplyDebugLocation(ApplyDebugLocation &&Other) : CGF(Other.CGF) { 751 Other.CGF = nullptr; 752 } 753 ApplyDebugLocation &operator=(ApplyDebugLocation &&) = default; 754 755 ~ApplyDebugLocation(); 756 757 /// Apply TemporaryLocation if it is valid. Otherwise switch 758 /// to an artificial debug location that has a valid scope, but no 759 /// line information. 760 /// 761 /// Artificial locations are useful when emitting compiler-generated 762 /// helper functions that have no source location associated with 763 /// them. The DWARF specification allows the compiler to use the 764 /// special line number 0 to indicate code that can not be 765 /// attributed to any source location. Note that passing an empty 766 /// SourceLocation to CGDebugInfo::setLocation() will result in the 767 /// last valid location being reused. 768 static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF) { 769 return ApplyDebugLocation(CGF, false, SourceLocation()); 770 } 771 /// Apply TemporaryLocation if it is valid. Otherwise switch 772 /// to an artificial debug location that has a valid scope, but no 773 /// line information. 774 static ApplyDebugLocation 775 CreateDefaultArtificial(CodeGenFunction &CGF, 776 SourceLocation TemporaryLocation) { 777 return ApplyDebugLocation(CGF, false, TemporaryLocation); 778 } 779 780 /// Set the IRBuilder to not attach debug locations. Note that 781 /// passing an empty SourceLocation to \a CGDebugInfo::setLocation() 782 /// will result in the last valid location being reused. Note that 783 /// all instructions that do not have a location at the beginning of 784 /// a function are counted towards to function prologue. 785 static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF) { 786 return ApplyDebugLocation(CGF, true, SourceLocation()); 787 } 788 }; 789 790 /// A scoped helper to set the current debug location to an inlined location. 791 class ApplyInlineDebugLocation { 792 SourceLocation SavedLocation; 793 CodeGenFunction *CGF; 794 795 public: 796 /// Set up the CodeGenFunction's DebugInfo to produce inline locations for the 797 /// function \p InlinedFn. The current debug location becomes the inlined call 798 /// site of the inlined function. 799 ApplyInlineDebugLocation(CodeGenFunction &CGF, GlobalDecl InlinedFn); 800 /// Restore everything back to the original state. 801 ~ApplyInlineDebugLocation(); 802 }; 803 804 } // namespace CodeGen 805 } // namespace clang 806 807 #endif // LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H 808