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