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