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; ObjCInterfaceCacheEntryObjCInterfaceCacheEntry92 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 /// Get enumeration type. 172 llvm::DIType *CreateEnumType(const EnumType *Ty); 173 llvm::DIType *CreateTypeDefinition(const EnumType *Ty); 174 /// Look up the completed type for a self pointer in the TypeCache and 175 /// create a copy of it with the ObjectPointer and Artificial flags 176 /// set. If the type is not cached, a new one is created. This should 177 /// never happen though, since creating a type for the implicit self 178 /// argument implies that we already parsed the interface definition 179 /// and the ivar declarations in the implementation. 180 llvm::DIType *CreateSelfType(const QualType &QualTy, llvm::DIType *Ty); 181 /// @} 182 183 /// Get the type from the cache or return null type if it doesn't 184 /// exist. 185 llvm::DIType *getTypeOrNull(const QualType); 186 /// Return the debug type for a C++ method. 187 /// \arg CXXMethodDecl is of FunctionType. This function type is 188 /// not updated to include implicit \c this pointer. Use this routine 189 /// to get a method type which includes \c this pointer. 190 llvm::DISubroutineType *getOrCreateMethodType(const CXXMethodDecl *Method, 191 llvm::DIFile *F); 192 llvm::DISubroutineType * 193 getOrCreateInstanceMethodType(QualType ThisPtr, const FunctionProtoType *Func, 194 llvm::DIFile *Unit); 195 llvm::DISubroutineType * 196 getOrCreateFunctionType(const Decl *D, QualType FnType, llvm::DIFile *F); 197 /// \return debug info descriptor for vtable. 198 llvm::DIType *getOrCreateVTablePtrType(llvm::DIFile *F); 199 /// \return namespace descriptor for the given namespace decl. 200 llvm::DINamespace *getOrCreateNameSpace(const NamespaceDecl *N); 201 llvm::DIType *CreatePointerLikeType(llvm::dwarf::Tag Tag, const Type *Ty, 202 QualType PointeeTy, llvm::DIFile *F); 203 llvm::DIType *getOrCreateStructPtrType(StringRef Name, llvm::DIType *&Cache); 204 205 /// A helper function to create a subprogram for a single member 206 /// function GlobalDecl. 207 llvm::DISubprogram *CreateCXXMemberFunction(const CXXMethodDecl *Method, 208 llvm::DIFile *F, 209 llvm::DIType *RecordTy); 210 211 /// A helper function to collect debug info for C++ member 212 /// functions. This is used while creating debug info entry for a 213 /// Record. 214 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl, llvm::DIFile *F, 215 SmallVectorImpl<llvm::Metadata *> &E, 216 llvm::DIType *T); 217 218 /// A helper function to collect debug info for C++ base 219 /// classes. This is used while creating debug info entry for a 220 /// Record. 221 void CollectCXXBases(const CXXRecordDecl *Decl, llvm::DIFile *F, 222 SmallVectorImpl<llvm::Metadata *> &EltTys, 223 llvm::DIType *RecordTy); 224 225 /// A helper function to collect template parameters. 226 llvm::DINodeArray CollectTemplateParams(const TemplateParameterList *TPList, 227 ArrayRef<TemplateArgument> TAList, 228 llvm::DIFile *Unit); 229 /// A helper function to collect debug info for function template 230 /// parameters. 231 llvm::DINodeArray CollectFunctionTemplateParams(const FunctionDecl *FD, 232 llvm::DIFile *Unit); 233 234 /// A helper function to collect debug info for template 235 /// parameters. 236 llvm::DINodeArray 237 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS, 238 llvm::DIFile *F); 239 240 llvm::DIType *createFieldType(StringRef name, QualType type, 241 uint64_t sizeInBitsOverride, SourceLocation loc, 242 AccessSpecifier AS, uint64_t offsetInBits, 243 llvm::DIFile *tunit, llvm::DIScope *scope, 244 const RecordDecl *RD = nullptr); 245 246 /// Helpers for collecting fields of a record. 247 /// @{ 248 void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl, 249 SmallVectorImpl<llvm::Metadata *> &E, 250 llvm::DIType *RecordTy); 251 llvm::DIDerivedType *CreateRecordStaticField(const VarDecl *Var, 252 llvm::DIType *RecordTy, 253 const RecordDecl *RD); 254 void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits, 255 llvm::DIFile *F, 256 SmallVectorImpl<llvm::Metadata *> &E, 257 llvm::DIType *RecordTy, const RecordDecl *RD); 258 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile *F, 259 SmallVectorImpl<llvm::Metadata *> &E, 260 llvm::DICompositeType *RecordTy); 261 262 /// If the C++ class has vtable info then insert appropriate debug 263 /// info entry in EltTys vector. 264 void CollectVTableInfo(const CXXRecordDecl *Decl, llvm::DIFile *F, 265 SmallVectorImpl<llvm::Metadata *> &EltTys); 266 /// @} 267 268 /// Create a new lexical block node and push it on the stack. 269 void CreateLexicalBlock(SourceLocation Loc); 270 271 public: 272 CGDebugInfo(CodeGenModule &CGM); 273 ~CGDebugInfo(); 274 275 void finalize(); 276 277 /// Set the main CU's DwoId field to \p Signature. 278 void setDwoId(uint64_t Signature); 279 280 /// When generating debug information for a clang module or 281 /// precompiled header, this module map will be used to determine 282 /// the module of origin of each Decl. setModuleMap(ModuleMap & MMap)283 void setModuleMap(ModuleMap &MMap) { ClangModuleMap = &MMap; } 284 285 /// Update the current source location. If \arg loc is invalid it is 286 /// ignored. 287 void setLocation(SourceLocation Loc); 288 289 /// Emit metadata to indicate a change in line/column information in 290 /// the source file. If the location is invalid, the previous 291 /// location will be reused. 292 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc); 293 294 /// Emit a call to llvm.dbg.function.start to indicate 295 /// start of a new function. 296 /// \param Loc The location of the function header. 297 /// \param ScopeLoc The location of the function body. 298 void EmitFunctionStart(GlobalDecl GD, SourceLocation Loc, 299 SourceLocation ScopeLoc, QualType FnType, 300 llvm::Function *Fn, CGBuilderTy &Builder); 301 302 /// Emit debug info for a function declaration. 303 void EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, QualType FnType); 304 305 /// Constructs the debug code for exiting a function. 306 void EmitFunctionEnd(CGBuilderTy &Builder); 307 308 /// Emit metadata to indicate the beginning of a new lexical block 309 /// and push the block onto the stack. 310 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc); 311 312 /// Emit metadata to indicate the end of a new lexical block and pop 313 /// the current block. 314 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc); 315 316 /// Emit call to \c llvm.dbg.declare for an automatic variable 317 /// declaration. 318 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI, 319 CGBuilderTy &Builder); 320 321 /// Emit call to \c llvm.dbg.declare for an imported variable 322 /// declaration in a block. 323 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable, 324 llvm::Value *storage, 325 CGBuilderTy &Builder, 326 const CGBlockInfo &blockInfo, 327 llvm::Instruction *InsertPoint = nullptr); 328 329 /// Emit call to \c llvm.dbg.declare for an argument variable 330 /// declaration. 331 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI, 332 unsigned ArgNo, CGBuilderTy &Builder); 333 334 /// Emit call to \c llvm.dbg.declare for the block-literal argument 335 /// to a block invocation function. 336 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, 337 llvm::Value *Arg, unsigned ArgNo, 338 llvm::Value *LocalAddr, 339 CGBuilderTy &Builder); 340 341 /// Emit information about a global variable. 342 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl); 343 344 /// Emit global variable's debug info. 345 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init); 346 347 /// Emit C++ using directive. 348 void EmitUsingDirective(const UsingDirectiveDecl &UD); 349 350 /// Emit the type explicitly casted to. 351 void EmitExplicitCastType(QualType Ty); 352 353 /// Emit C++ using declaration. 354 void EmitUsingDecl(const UsingDecl &UD); 355 356 /// Emit an @import declaration. 357 void EmitImportDecl(const ImportDecl &ID); 358 359 /// Emit C++ namespace alias. 360 llvm::DIImportedEntity *EmitNamespaceAlias(const NamespaceAliasDecl &NA); 361 362 /// Emit record type's standalone debug info. 363 llvm::DIType *getOrCreateRecordType(QualType Ty, SourceLocation L); 364 365 /// Emit an Objective-C interface type standalone debug info. 366 llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc); 367 368 /// Emit standalone debug info for a type. 369 llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc); 370 371 void completeType(const EnumDecl *ED); 372 void completeType(const RecordDecl *RD); 373 void completeRequiredType(const RecordDecl *RD); 374 void completeClassData(const RecordDecl *RD); 375 376 void completeTemplateDefinition(const ClassTemplateSpecializationDecl &SD); 377 378 private: 379 /// Emit call to llvm.dbg.declare for a variable declaration. 380 void EmitDeclare(const VarDecl *decl, llvm::Value *AI, 381 llvm::Optional<unsigned> ArgNo, CGBuilderTy &Builder); 382 383 /// Build up structure info for the byref. See \a BuildByRefType. 384 llvm::DIType *EmitTypeForVarWithBlocksAttr(const VarDecl *VD, 385 uint64_t *OffSet); 386 387 /// Get context info for the DeclContext of \p Decl. 388 llvm::DIScope *getDeclContextDescriptor(const Decl *D); 389 /// Get context info for a given DeclContext \p Decl. 390 llvm::DIScope *getContextDescriptor(const Decl *Context, 391 llvm::DIScope *Default); 392 393 llvm::DIScope *getCurrentContextDescriptor(const Decl *Decl); 394 395 /// Create a forward decl for a RecordType in a given context. 396 llvm::DICompositeType *getOrCreateRecordFwdDecl(const RecordType *, 397 llvm::DIScope *); 398 399 /// Return current directory name. 400 StringRef getCurrentDirname(); 401 402 /// Create new compile unit. 403 void CreateCompileUnit(); 404 405 /// Remap a given path with the current debug prefix map 406 std::string remapDIPath(StringRef) const; 407 408 /// Get the file debug info descriptor for the input location. 409 llvm::DIFile *getOrCreateFile(SourceLocation Loc); 410 411 /// Get the file info for main compile unit. 412 llvm::DIFile *getOrCreateMainFile(); 413 414 /// Get the type from the cache or create a new type if necessary. 415 llvm::DIType *getOrCreateType(QualType Ty, llvm::DIFile *Fg); 416 417 /// Get a reference to a clang module. If \p CreateSkeletonCU is true, 418 /// this also creates a split dwarf skeleton compile unit. 419 llvm::DIModule * 420 getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod, 421 bool CreateSkeletonCU); 422 423 /// DebugTypeExtRefs: If \p D originated in a clang module, return it. 424 llvm::DIModule *getParentModuleOrNull(const Decl *D); 425 426 /// Get the type from the cache or create a new partial type if 427 /// necessary. 428 llvm::DICompositeType *getOrCreateLimitedType(const RecordType *Ty, 429 llvm::DIFile *F); 430 431 /// Create type metadata for a source language type. 432 llvm::DIType *CreateTypeNode(QualType Ty, llvm::DIFile *Fg); 433 434 /// Create new member and increase Offset by FType's size. 435 llvm::DIType *CreateMemberType(llvm::DIFile *Unit, QualType FType, 436 StringRef Name, uint64_t *Offset); 437 438 /// Retrieve the DIDescriptor, if any, for the canonical form of this 439 /// declaration. 440 llvm::DINode *getDeclarationOrDefinition(const Decl *D); 441 442 /// \return debug info descriptor to describe method 443 /// declaration for the given method definition. 444 llvm::DISubprogram *getFunctionDeclaration(const Decl *D); 445 446 /// \return debug info descriptor to describe in-class static data 447 /// member declaration for the given out-of-class definition. If D 448 /// is an out-of-class definition of a static data member of a 449 /// class, find its corresponding in-class declaration. 450 llvm::DIDerivedType * 451 getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D); 452 453 /// Create a subprogram describing the forward declaration 454 /// represented in the given FunctionDecl. 455 llvm::DISubprogram *getFunctionForwardDeclaration(const FunctionDecl *FD); 456 457 /// Create a global variable describing the forward decalration 458 /// represented in the given VarDecl. 459 llvm::DIGlobalVariable * 460 getGlobalVariableForwardDeclaration(const VarDecl *VD); 461 462 /// \brief Return a global variable that represents one of the 463 /// collection of global variables created for an anonmyous union. 464 /// 465 /// Recursively collect all of the member fields of a global 466 /// anonymous decl and create static variables for them. The first 467 /// time this is called it needs to be on a union and then from 468 /// there we can have additional unnamed fields. 469 llvm::DIGlobalVariable * 470 CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile *Unit, 471 unsigned LineNo, StringRef LinkageName, 472 llvm::GlobalVariable *Var, llvm::DIScope *DContext); 473 474 /// Get function name for the given FunctionDecl. If the name is 475 /// constructed on demand (e.g., C++ destructor) then the name is 476 /// stored on the side. 477 StringRef getFunctionName(const FunctionDecl *FD); 478 479 /// Returns the unmangled name of an Objective-C method. 480 /// This is the display name for the debugging info. 481 StringRef getObjCMethodName(const ObjCMethodDecl *FD); 482 483 /// Return selector name. This is used for debugging 484 /// info. 485 StringRef getSelectorName(Selector S); 486 487 /// Get class name including template argument list. 488 StringRef getClassName(const RecordDecl *RD); 489 490 /// Get the vtable name for the given class. 491 StringRef getVTableName(const CXXRecordDecl *Decl); 492 493 /// Get line number for the location. If location is invalid 494 /// then use current location. 495 unsigned getLineNumber(SourceLocation Loc); 496 497 /// Get column number for the location. If location is 498 /// invalid then use current location. 499 /// \param Force Assume DebugColumnInfo option is true. 500 unsigned getColumnNumber(SourceLocation Loc, bool Force = false); 501 502 /// Collect various properties of a FunctionDecl. 503 /// \param GD A GlobalDecl whose getDecl() must return a FunctionDecl. 504 void collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit, 505 StringRef &Name, StringRef &LinkageName, 506 llvm::DIScope *&FDContext, 507 llvm::DINodeArray &TParamsArray, 508 unsigned &Flags); 509 510 /// Collect various properties of a VarDecl. 511 void collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit, 512 unsigned &LineNo, QualType &T, StringRef &Name, 513 StringRef &LinkageName, llvm::DIScope *&VDContext); 514 515 /// Allocate a copy of \p A using the DebugInfoNames allocator 516 /// and return a reference to it. If multiple arguments are given the strings 517 /// are concatenated. 518 StringRef internString(StringRef A, StringRef B = StringRef()) { 519 char *Data = DebugInfoNames.Allocate<char>(A.size() + B.size()); 520 if (!A.empty()) 521 std::memcpy(Data, A.data(), A.size()); 522 if (!B.empty()) 523 std::memcpy(Data + A.size(), B.data(), B.size()); 524 return StringRef(Data, A.size() + B.size()); 525 } 526 }; 527 528 /// A scoped helper to set the current debug location to the specified 529 /// location or preferred location of the specified Expr. 530 class ApplyDebugLocation { 531 private: 532 void init(SourceLocation TemporaryLocation, bool DefaultToEmpty = false); 533 ApplyDebugLocation(CodeGenFunction &CGF, bool DefaultToEmpty, 534 SourceLocation TemporaryLocation); 535 536 llvm::DebugLoc OriginalLocation; 537 CodeGenFunction *CGF; 538 539 public: 540 /// Set the location to the (valid) TemporaryLocation. 541 ApplyDebugLocation(CodeGenFunction &CGF, SourceLocation TemporaryLocation); 542 ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E); 543 ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc); ApplyDebugLocation(ApplyDebugLocation && Other)544 ApplyDebugLocation(ApplyDebugLocation &&Other) : CGF(Other.CGF) { 545 Other.CGF = nullptr; 546 } 547 548 ~ApplyDebugLocation(); 549 550 /// \brief Apply TemporaryLocation if it is valid. Otherwise switch 551 /// to an artificial debug location that has a valid scope, but no 552 /// line information. 553 /// 554 /// Artificial locations are useful when emitting compiler-generated 555 /// helper functions that have no source location associated with 556 /// them. The DWARF specification allows the compiler to use the 557 /// special line number 0 to indicate code that can not be 558 /// attributed to any source location. Note that passing an empty 559 /// SourceLocation to CGDebugInfo::setLocation() will result in the 560 /// last valid location being reused. CreateArtificial(CodeGenFunction & CGF)561 static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF) { 562 return ApplyDebugLocation(CGF, false, SourceLocation()); 563 } 564 /// \brief Apply TemporaryLocation if it is valid. Otherwise switch 565 /// to an artificial debug location that has a valid scope, but no 566 /// line information. 567 static ApplyDebugLocation CreateDefaultArtificial(CodeGenFunction & CGF,SourceLocation TemporaryLocation)568 CreateDefaultArtificial(CodeGenFunction &CGF, 569 SourceLocation TemporaryLocation) { 570 return ApplyDebugLocation(CGF, false, TemporaryLocation); 571 } 572 573 /// Set the IRBuilder to not attach debug locations. Note that 574 /// passing an empty SourceLocation to \a CGDebugInfo::setLocation() 575 /// will result in the last valid location being reused. Note that 576 /// all instructions that do not have a location at the beginning of 577 /// a function are counted towards to function prologue. CreateEmpty(CodeGenFunction & CGF)578 static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF) { 579 return ApplyDebugLocation(CGF, true, SourceLocation()); 580 } 581 582 }; 583 584 } // namespace CodeGen 585 } // namespace clang 586 587 #endif // LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H 588