1 //===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This is the source-level debug info generator for llvm translation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H 15 #define LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H 16 17 #include "CGBuilder.h" 18 #include "clang/AST/DeclCXX.h" 19 #include "clang/AST/Expr.h" 20 #include "clang/AST/ExternalASTSource.h" 21 #include "clang/AST/Type.h" 22 #include "clang/Basic/SourceLocation.h" 23 #include "clang/Frontend/CodeGenOptions.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 45 namespace CodeGen { 46 class CodeGenModule; 47 class CodeGenFunction; 48 class CGBlockInfo; 49 50 /// This class gathers all debug information during compilation and is 51 /// responsible for emitting to llvm globals or pass directly to the 52 /// backend. 53 class CGDebugInfo { 54 friend class ApplyDebugLocation; 55 friend class SaveAndRestoreLocation; 56 CodeGenModule &CGM; 57 const codegenoptions::DebugInfoKind DebugKind; 58 bool DebugTypeExtRefs; 59 llvm::DIBuilder DBuilder; 60 llvm::DICompileUnit *TheCU = nullptr; 61 ModuleMap *ClangModuleMap = nullptr; 62 ExternalASTSource::ASTSourceDescriptor PCHDescriptor; 63 SourceLocation CurLoc; 64 llvm::DIType *VTablePtrType = nullptr; 65 llvm::DIType *ClassTy = nullptr; 66 llvm::DICompositeType *ObjTy = nullptr; 67 llvm::DIType *SelTy = nullptr; 68 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 69 llvm::DIType *SingletonId = nullptr; 70 #include "clang/Basic/OpenCLImageTypes.def" 71 llvm::DIType *OCLSamplerDITy = nullptr; 72 llvm::DIType *OCLEventDITy = nullptr; 73 llvm::DIType *OCLClkEventDITy = nullptr; 74 llvm::DIType *OCLQueueDITy = nullptr; 75 llvm::DIType *OCLNDRangeDITy = nullptr; 76 llvm::DIType *OCLReserveIDDITy = nullptr; 77 78 /// Cache of previously constructed Types. 79 llvm::DenseMap<const void *, llvm::TrackingMDRef> TypeCache; 80 81 llvm::SmallDenseMap<llvm::StringRef, llvm::StringRef> DebugPrefixMap; 82 83 struct ObjCInterfaceCacheEntry { 84 const ObjCInterfaceType *Type; 85 llvm::DIType *Decl; 86 llvm::DIFile *Unit; 87 ObjCInterfaceCacheEntry(const ObjCInterfaceType *Type, llvm::DIType *Decl, 88 llvm::DIFile *Unit) 89 : Type(Type), Decl(Decl), Unit(Unit) {} 90 }; 91 92 /// Cache of previously constructed interfaces which may change. 93 llvm::SmallVector<ObjCInterfaceCacheEntry, 32> ObjCInterfaceCache; 94 95 /// Cache of references to clang modules and precompiled headers. 96 llvm::DenseMap<const Module *, llvm::TrackingMDRef> ModuleCache; 97 98 /// List of interfaces we want to keep even if orphaned. 99 std::vector<void *> RetainedTypes; 100 101 /// Cache of forward declared types to RAUW at the end of compilation. 102 std::vector<std::pair<const TagType *, llvm::TrackingMDRef>> ReplaceMap; 103 104 /// Cache of replaceable forward declarations (functions and 105 /// variables) to RAUW at the end of compilation. 106 std::vector<std::pair<const DeclaratorDecl *, llvm::TrackingMDRef>> 107 FwdDeclReplaceMap; 108 109 /// Keep track of our current nested lexical block. 110 std::vector<llvm::TypedTrackingMDRef<llvm::DIScope>> LexicalBlockStack; 111 llvm::DenseMap<const Decl *, llvm::TrackingMDRef> RegionMap; 112 /// Keep track of LexicalBlockStack counter at the beginning of a 113 /// function. This is used to pop unbalanced regions at the end of a 114 /// function. 115 std::vector<unsigned> FnBeginRegionCount; 116 117 /// This is a storage for names that are constructed on demand. For 118 /// example, C++ destructors, C++ operators etc.. 119 llvm::BumpPtrAllocator DebugInfoNames; 120 StringRef CWDName; 121 122 llvm::DenseMap<const char *, llvm::TrackingMDRef> DIFileCache; 123 llvm::DenseMap<const FunctionDecl *, llvm::TrackingMDRef> SPCache; 124 /// Cache declarations relevant to DW_TAG_imported_declarations (C++ 125 /// using declarations) that aren't covered by other more specific caches. 126 llvm::DenseMap<const Decl *, llvm::TrackingMDRef> DeclCache; 127 llvm::DenseMap<const NamespaceDecl *, llvm::TrackingMDRef> NameSpaceCache; 128 llvm::DenseMap<const NamespaceAliasDecl *, llvm::TrackingMDRef> 129 NamespaceAliasCache; 130 llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIDerivedType>> 131 StaticDataMemberCache; 132 133 /// Helper functions for getOrCreateType. 134 /// @{ 135 /// Currently the checksum of an interface includes the number of 136 /// ivars and property accessors. 137 llvm::DIType *CreateType(const BuiltinType *Ty); 138 llvm::DIType *CreateType(const ComplexType *Ty); 139 llvm::DIType *CreateQualifiedType(QualType Ty, llvm::DIFile *Fg); 140 llvm::DIType *CreateType(const TypedefType *Ty, llvm::DIFile *Fg); 141 llvm::DIType *CreateType(const TemplateSpecializationType *Ty, 142 llvm::DIFile *Fg); 143 llvm::DIType *CreateType(const ObjCObjectPointerType *Ty, llvm::DIFile *F); 144 llvm::DIType *CreateType(const PointerType *Ty, llvm::DIFile *F); 145 llvm::DIType *CreateType(const BlockPointerType *Ty, llvm::DIFile *F); 146 llvm::DIType *CreateType(const FunctionType *Ty, llvm::DIFile *F); 147 /// Get structure or union type. 148 llvm::DIType *CreateType(const RecordType *Tyg); 149 llvm::DIType *CreateTypeDefinition(const RecordType *Ty); 150 llvm::DICompositeType *CreateLimitedType(const RecordType *Ty); 151 void CollectContainingType(const CXXRecordDecl *RD, 152 llvm::DICompositeType *CT); 153 /// Get Objective-C interface type. 154 llvm::DIType *CreateType(const ObjCInterfaceType *Ty, llvm::DIFile *F); 155 llvm::DIType *CreateTypeDefinition(const ObjCInterfaceType *Ty, 156 llvm::DIFile *F); 157 /// Get Objective-C object type. 158 llvm::DIType *CreateType(const ObjCObjectType *Ty, llvm::DIFile *F); 159 llvm::DIType *CreateType(const ObjCTypeParamType *Ty, llvm::DIFile *Unit); 160 161 llvm::DIType *CreateType(const VectorType *Ty, llvm::DIFile *F); 162 llvm::DIType *CreateType(const ArrayType *Ty, llvm::DIFile *F); 163 llvm::DIType *CreateType(const LValueReferenceType *Ty, llvm::DIFile *F); 164 llvm::DIType *CreateType(const RValueReferenceType *Ty, llvm::DIFile *Unit); 165 llvm::DIType *CreateType(const MemberPointerType *Ty, llvm::DIFile *F); 166 llvm::DIType *CreateType(const AtomicType *Ty, llvm::DIFile *F); 167 llvm::DIType *CreateType(const PipeType *Ty, llvm::DIFile *F); 168 /// Get enumeration type. 169 llvm::DIType *CreateEnumType(const EnumType *Ty); 170 llvm::DIType *CreateTypeDefinition(const EnumType *Ty); 171 /// Look up the completed type for a self pointer in the TypeCache and 172 /// create a copy of it with the ObjectPointer and Artificial flags 173 /// set. If the type is not cached, a new one is created. This should 174 /// never happen though, since creating a type for the implicit self 175 /// argument implies that we already parsed the interface definition 176 /// and the ivar declarations in the implementation. 177 llvm::DIType *CreateSelfType(const QualType &QualTy, llvm::DIType *Ty); 178 /// @} 179 180 /// Get the type from the cache or return null type if it doesn't 181 /// exist. 182 llvm::DIType *getTypeOrNull(const QualType); 183 /// Return the debug type for a C++ method. 184 /// \arg CXXMethodDecl is of FunctionType. This function type is 185 /// not updated to include implicit \c this pointer. Use this routine 186 /// to get a method type which includes \c this pointer. 187 llvm::DISubroutineType *getOrCreateMethodType(const CXXMethodDecl *Method, 188 llvm::DIFile *F); 189 llvm::DISubroutineType * 190 getOrCreateInstanceMethodType(QualType ThisPtr, const FunctionProtoType *Func, 191 llvm::DIFile *Unit); 192 llvm::DISubroutineType * 193 getOrCreateFunctionType(const Decl *D, QualType FnType, llvm::DIFile *F); 194 /// \return debug info descriptor for vtable. 195 llvm::DIType *getOrCreateVTablePtrType(llvm::DIFile *F); 196 /// \return namespace descriptor for the given namespace decl. 197 llvm::DINamespace *getOrCreateNameSpace(const NamespaceDecl *N); 198 llvm::DIType *CreatePointerLikeType(llvm::dwarf::Tag Tag, const Type *Ty, 199 QualType PointeeTy, llvm::DIFile *F); 200 llvm::DIType *getOrCreateStructPtrType(StringRef Name, llvm::DIType *&Cache); 201 202 /// A helper function to create a subprogram for a single member 203 /// function GlobalDecl. 204 llvm::DISubprogram *CreateCXXMemberFunction(const CXXMethodDecl *Method, 205 llvm::DIFile *F, 206 llvm::DIType *RecordTy); 207 208 /// A helper function to collect debug info for C++ member 209 /// functions. This is used while creating debug info entry for a 210 /// Record. 211 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl, llvm::DIFile *F, 212 SmallVectorImpl<llvm::Metadata *> &E, 213 llvm::DIType *T); 214 215 /// A helper function to collect debug info for C++ base 216 /// classes. This is used while creating debug info entry for a 217 /// Record. 218 void CollectCXXBases(const CXXRecordDecl *Decl, llvm::DIFile *F, 219 SmallVectorImpl<llvm::Metadata *> &EltTys, 220 llvm::DIType *RecordTy); 221 222 /// Helper function for CollectCXXBases. 223 /// Adds debug info entries for types in Bases that are not in SeenTypes. 224 void CollectCXXBasesAux(const CXXRecordDecl *RD, llvm::DIFile *Unit, 225 SmallVectorImpl<llvm::Metadata *> &EltTys, 226 llvm::DIType *RecordTy, 227 const CXXRecordDecl::base_class_const_range &Bases, 228 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes, 229 llvm::DINode::DIFlags StartingFlags); 230 231 /// A helper function to collect template parameters. 232 llvm::DINodeArray CollectTemplateParams(const TemplateParameterList *TPList, 233 ArrayRef<TemplateArgument> TAList, 234 llvm::DIFile *Unit); 235 /// A helper function to collect debug info for function template 236 /// parameters. 237 llvm::DINodeArray CollectFunctionTemplateParams(const FunctionDecl *FD, 238 llvm::DIFile *Unit); 239 240 /// A helper function to collect debug info for template 241 /// parameters. 242 llvm::DINodeArray 243 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS, 244 llvm::DIFile *F); 245 246 llvm::DIType *createFieldType(StringRef name, QualType type, 247 SourceLocation loc, AccessSpecifier AS, 248 uint64_t offsetInBits, 249 uint32_t AlignInBits, 250 llvm::DIFile *tunit, llvm::DIScope *scope, 251 const RecordDecl *RD = nullptr); 252 253 llvm::DIType *createFieldType(StringRef name, QualType type, 254 SourceLocation loc, AccessSpecifier AS, 255 uint64_t offsetInBits, llvm::DIFile *tunit, 256 llvm::DIScope *scope, 257 const RecordDecl *RD = nullptr) { 258 return createFieldType(name, type, loc, AS, offsetInBits, 0, tunit, scope, 259 RD); 260 } 261 262 /// Create new bit field member. 263 llvm::DIType *createBitFieldType(const FieldDecl *BitFieldDecl, 264 llvm::DIScope *RecordTy, 265 const RecordDecl *RD); 266 267 /// Helpers for collecting fields of a record. 268 /// @{ 269 void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl, 270 SmallVectorImpl<llvm::Metadata *> &E, 271 llvm::DIType *RecordTy); 272 llvm::DIDerivedType *CreateRecordStaticField(const VarDecl *Var, 273 llvm::DIType *RecordTy, 274 const RecordDecl *RD); 275 void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits, 276 llvm::DIFile *F, 277 SmallVectorImpl<llvm::Metadata *> &E, 278 llvm::DIType *RecordTy, const RecordDecl *RD); 279 void CollectRecordNestedRecord(const RecordDecl *RD, 280 SmallVectorImpl<llvm::Metadata *> &E); 281 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile *F, 282 SmallVectorImpl<llvm::Metadata *> &E, 283 llvm::DICompositeType *RecordTy); 284 285 /// If the C++ class has vtable info then insert appropriate debug 286 /// info entry in EltTys vector. 287 void CollectVTableInfo(const CXXRecordDecl *Decl, llvm::DIFile *F, 288 SmallVectorImpl<llvm::Metadata *> &EltTys, 289 llvm::DICompositeType *RecordTy); 290 /// @} 291 292 /// Create a new lexical block node and push it on the stack. 293 void CreateLexicalBlock(SourceLocation Loc); 294 295 public: 296 CGDebugInfo(CodeGenModule &CGM); 297 ~CGDebugInfo(); 298 299 void finalize(); 300 301 /// Module debugging: Support for building PCMs. 302 /// @{ 303 /// Set the main CU's DwoId field to \p Signature. 304 void setDwoId(uint64_t Signature); 305 306 /// When generating debug information for a clang module or 307 /// precompiled header, this module map will be used to determine 308 /// the module of origin of each Decl. 309 void setModuleMap(ModuleMap &MMap) { ClangModuleMap = &MMap; } 310 311 /// When generating debug information for a clang module or 312 /// precompiled header, this module map will be used to determine 313 /// the module of origin of each Decl. 314 void setPCHDescriptor(ExternalASTSource::ASTSourceDescriptor PCH) { 315 PCHDescriptor = PCH; 316 } 317 /// @} 318 319 /// Update the current source location. If \arg loc is invalid it is 320 /// ignored. 321 void setLocation(SourceLocation Loc); 322 323 /// Emit metadata to indicate a change in line/column information in 324 /// the source file. If the location is invalid, the previous 325 /// location will be reused. 326 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc); 327 328 /// Emit a call to llvm.dbg.function.start to indicate 329 /// start of a new function. 330 /// \param Loc The location of the function header. 331 /// \param ScopeLoc The location of the function body. 332 void EmitFunctionStart(GlobalDecl GD, SourceLocation Loc, 333 SourceLocation ScopeLoc, QualType FnType, 334 llvm::Function *Fn, CGBuilderTy &Builder); 335 336 /// Emit debug info for a function declaration. 337 void EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, QualType FnType); 338 339 /// Constructs the debug code for exiting a function. 340 void EmitFunctionEnd(CGBuilderTy &Builder); 341 342 /// Emit metadata to indicate the beginning of a new lexical block 343 /// and push the block onto the stack. 344 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc); 345 346 /// Emit metadata to indicate the end of a new lexical block and pop 347 /// the current block. 348 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc); 349 350 /// Emit call to \c llvm.dbg.declare for an automatic variable 351 /// declaration. 352 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI, 353 CGBuilderTy &Builder); 354 355 /// Emit call to \c llvm.dbg.declare for an imported variable 356 /// declaration in a block. 357 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable, 358 llvm::Value *storage, 359 CGBuilderTy &Builder, 360 const CGBlockInfo &blockInfo, 361 llvm::Instruction *InsertPoint = nullptr); 362 363 /// Emit call to \c llvm.dbg.declare for an argument variable 364 /// declaration. 365 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI, 366 unsigned ArgNo, CGBuilderTy &Builder); 367 368 /// Emit call to \c llvm.dbg.declare for the block-literal argument 369 /// to a block invocation function. 370 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, 371 llvm::Value *Arg, unsigned ArgNo, 372 llvm::Value *LocalAddr, 373 CGBuilderTy &Builder); 374 375 /// Emit information about a global variable. 376 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl); 377 378 /// Emit a constant global variable's debug info. 379 void EmitGlobalVariable(const ValueDecl *VD, const APValue &Init); 380 381 /// Emit C++ using directive. 382 void EmitUsingDirective(const UsingDirectiveDecl &UD); 383 384 /// Emit the type explicitly casted to. 385 void EmitExplicitCastType(QualType Ty); 386 387 /// Emit C++ using declaration. 388 void EmitUsingDecl(const UsingDecl &UD); 389 390 /// Emit an @import declaration. 391 void EmitImportDecl(const ImportDecl &ID); 392 393 /// Emit C++ namespace alias. 394 llvm::DIImportedEntity *EmitNamespaceAlias(const NamespaceAliasDecl &NA); 395 396 /// Emit record type's standalone debug info. 397 llvm::DIType *getOrCreateRecordType(QualType Ty, SourceLocation L); 398 399 /// Emit an Objective-C interface type standalone debug info. 400 llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc); 401 402 /// Emit standalone debug info for a type. 403 llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc); 404 405 void completeType(const EnumDecl *ED); 406 void completeType(const RecordDecl *RD); 407 void completeRequiredType(const RecordDecl *RD); 408 void completeClassData(const RecordDecl *RD); 409 410 void completeTemplateDefinition(const ClassTemplateSpecializationDecl &SD); 411 412 private: 413 /// Emit call to llvm.dbg.declare for a variable declaration. 414 void EmitDeclare(const VarDecl *decl, llvm::Value *AI, 415 llvm::Optional<unsigned> ArgNo, CGBuilderTy &Builder); 416 417 /// Build up structure info for the byref. See \a BuildByRefType. 418 llvm::DIType *EmitTypeForVarWithBlocksAttr(const VarDecl *VD, 419 uint64_t *OffSet); 420 421 /// Get context info for the DeclContext of \p Decl. 422 llvm::DIScope *getDeclContextDescriptor(const Decl *D); 423 /// Get context info for a given DeclContext \p Decl. 424 llvm::DIScope *getContextDescriptor(const Decl *Context, 425 llvm::DIScope *Default); 426 427 llvm::DIScope *getCurrentContextDescriptor(const Decl *Decl); 428 429 /// Create a forward decl for a RecordType in a given context. 430 llvm::DICompositeType *getOrCreateRecordFwdDecl(const RecordType *, 431 llvm::DIScope *); 432 433 /// Return current directory name. 434 StringRef getCurrentDirname(); 435 436 /// Create new compile unit. 437 void CreateCompileUnit(); 438 439 /// Remap a given path with the current debug prefix map 440 std::string remapDIPath(StringRef) const; 441 442 /// Get the file debug info descriptor for the input location. 443 llvm::DIFile *getOrCreateFile(SourceLocation Loc); 444 445 /// Get the file info for main compile unit. 446 llvm::DIFile *getOrCreateMainFile(); 447 448 /// Get the type from the cache or create a new type if necessary. 449 llvm::DIType *getOrCreateType(QualType Ty, llvm::DIFile *Fg); 450 451 /// Get a reference to a clang module. If \p CreateSkeletonCU is true, 452 /// this also creates a split dwarf skeleton compile unit. 453 llvm::DIModule * 454 getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod, 455 bool CreateSkeletonCU); 456 457 /// DebugTypeExtRefs: If \p D originated in a clang module, return it. 458 llvm::DIModule *getParentModuleOrNull(const Decl *D); 459 460 /// Get the type from the cache or create a new partial type if 461 /// necessary. 462 llvm::DICompositeType *getOrCreateLimitedType(const RecordType *Ty, 463 llvm::DIFile *F); 464 465 /// Create type metadata for a source language type. 466 llvm::DIType *CreateTypeNode(QualType Ty, llvm::DIFile *Fg); 467 468 /// Create new member and increase Offset by FType's size. 469 llvm::DIType *CreateMemberType(llvm::DIFile *Unit, QualType FType, 470 StringRef Name, uint64_t *Offset); 471 472 /// Retrieve the DIDescriptor, if any, for the canonical form of this 473 /// declaration. 474 llvm::DINode *getDeclarationOrDefinition(const Decl *D); 475 476 /// \return debug info descriptor to describe method 477 /// declaration for the given method definition. 478 llvm::DISubprogram *getFunctionDeclaration(const Decl *D); 479 480 /// \return debug info descriptor to describe in-class static data 481 /// member declaration for the given out-of-class definition. If D 482 /// is an out-of-class definition of a static data member of a 483 /// class, find its corresponding in-class declaration. 484 llvm::DIDerivedType * 485 getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D); 486 487 /// Create a subprogram describing the forward declaration 488 /// represented in the given FunctionDecl. 489 llvm::DISubprogram *getFunctionForwardDeclaration(const FunctionDecl *FD); 490 491 /// Create a global variable describing the forward decalration 492 /// represented in the given VarDecl. 493 llvm::DIGlobalVariable * 494 getGlobalVariableForwardDeclaration(const VarDecl *VD); 495 496 /// \brief Return a global variable that represents one of the 497 /// collection of global variables created for an anonmyous union. 498 /// 499 /// Recursively collect all of the member fields of a global 500 /// anonymous decl and create static variables for them. The first 501 /// time this is called it needs to be on a union and then from 502 /// there we can have additional unnamed fields. 503 llvm::DIGlobalVariable * 504 CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile *Unit, 505 unsigned LineNo, StringRef LinkageName, 506 llvm::GlobalVariable *Var, llvm::DIScope *DContext); 507 508 /// Get function name for the given FunctionDecl. If the name is 509 /// constructed on demand (e.g., C++ destructor) then the name is 510 /// stored on the side. 511 StringRef getFunctionName(const FunctionDecl *FD); 512 513 /// Returns the unmangled name of an Objective-C method. 514 /// This is the display name for the debugging info. 515 StringRef getObjCMethodName(const ObjCMethodDecl *FD); 516 517 /// Return selector name. This is used for debugging 518 /// info. 519 StringRef getSelectorName(Selector S); 520 521 /// Get class name including template argument list. 522 StringRef getClassName(const RecordDecl *RD); 523 524 /// Get the vtable name for the given class. 525 StringRef getVTableName(const CXXRecordDecl *Decl); 526 527 /// Get line number for the location. If location is invalid 528 /// then use current location. 529 unsigned getLineNumber(SourceLocation Loc); 530 531 /// Get column number for the location. If location is 532 /// invalid then use current location. 533 /// \param Force Assume DebugColumnInfo option is true. 534 unsigned getColumnNumber(SourceLocation Loc, bool Force = false); 535 536 /// Collect various properties of a FunctionDecl. 537 /// \param GD A GlobalDecl whose getDecl() must return a FunctionDecl. 538 void collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit, 539 StringRef &Name, StringRef &LinkageName, 540 llvm::DIScope *&FDContext, 541 llvm::DINodeArray &TParamsArray, 542 llvm::DINode::DIFlags &Flags); 543 544 /// Collect various properties of a VarDecl. 545 void collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit, 546 unsigned &LineNo, QualType &T, StringRef &Name, 547 StringRef &LinkageName, llvm::DIScope *&VDContext); 548 549 /// Allocate a copy of \p A using the DebugInfoNames allocator 550 /// and return a reference to it. If multiple arguments are given the strings 551 /// are concatenated. 552 StringRef internString(StringRef A, StringRef B = StringRef()) { 553 char *Data = DebugInfoNames.Allocate<char>(A.size() + B.size()); 554 if (!A.empty()) 555 std::memcpy(Data, A.data(), A.size()); 556 if (!B.empty()) 557 std::memcpy(Data + A.size(), B.data(), B.size()); 558 return StringRef(Data, A.size() + B.size()); 559 } 560 }; 561 562 /// A scoped helper to set the current debug location to the specified 563 /// location or preferred location of the specified Expr. 564 class ApplyDebugLocation { 565 private: 566 void init(SourceLocation TemporaryLocation, bool DefaultToEmpty = false); 567 ApplyDebugLocation(CodeGenFunction &CGF, bool DefaultToEmpty, 568 SourceLocation TemporaryLocation); 569 570 llvm::DebugLoc OriginalLocation; 571 CodeGenFunction *CGF; 572 573 public: 574 /// Set the location to the (valid) TemporaryLocation. 575 ApplyDebugLocation(CodeGenFunction &CGF, SourceLocation TemporaryLocation); 576 ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E); 577 ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc); 578 ApplyDebugLocation(ApplyDebugLocation &&Other) : CGF(Other.CGF) { 579 Other.CGF = nullptr; 580 } 581 582 ~ApplyDebugLocation(); 583 584 /// \brief Apply TemporaryLocation if it is valid. Otherwise switch 585 /// to an artificial debug location that has a valid scope, but no 586 /// line information. 587 /// 588 /// Artificial locations are useful when emitting compiler-generated 589 /// helper functions that have no source location associated with 590 /// them. The DWARF specification allows the compiler to use the 591 /// special line number 0 to indicate code that can not be 592 /// attributed to any source location. Note that passing an empty 593 /// SourceLocation to CGDebugInfo::setLocation() will result in the 594 /// last valid location being reused. 595 static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF) { 596 return ApplyDebugLocation(CGF, false, SourceLocation()); 597 } 598 /// \brief Apply TemporaryLocation if it is valid. Otherwise switch 599 /// to an artificial debug location that has a valid scope, but no 600 /// line information. 601 static ApplyDebugLocation 602 CreateDefaultArtificial(CodeGenFunction &CGF, 603 SourceLocation TemporaryLocation) { 604 return ApplyDebugLocation(CGF, false, TemporaryLocation); 605 } 606 607 /// Set the IRBuilder to not attach debug locations. Note that 608 /// passing an empty SourceLocation to \a CGDebugInfo::setLocation() 609 /// will result in the last valid location being reused. Note that 610 /// all instructions that do not have a location at the beginning of 611 /// a function are counted towards to function prologue. 612 static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF) { 613 return ApplyDebugLocation(CGF, true, SourceLocation()); 614 } 615 616 }; 617 618 } // namespace CodeGen 619 } // namespace clang 620 621 #endif // LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H 622