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