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