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