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