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