CGDebugInfo.h 24 KB

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