CGDebugInfo.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. //===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This is the source-level debug info generator for llvm translation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H
  13. #define LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H
  14. #include "CGBuilder.h"
  15. #include "clang/AST/DeclCXX.h"
  16. #include "clang/AST/Expr.h"
  17. #include "clang/AST/ExternalASTSource.h"
  18. #include "clang/AST/Type.h"
  19. #include "clang/AST/TypeOrdering.h"
  20. #include "clang/Basic/CodeGenOptions.h"
  21. #include "clang/Basic/SourceLocation.h"
  22. #include "llvm/ADT/DenseMap.h"
  23. #include "llvm/ADT/DenseSet.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. namespace llvm {
  30. class MDNode;
  31. }
  32. namespace clang {
  33. class ClassTemplateSpecializationDecl;
  34. class GlobalDecl;
  35. class ModuleMap;
  36. class ObjCInterfaceDecl;
  37. class ObjCIvarDecl;
  38. class UsingDecl;
  39. class VarDecl;
  40. enum class DynamicInitKind : unsigned;
  41. namespace CodeGen {
  42. class CodeGenModule;
  43. class CodeGenFunction;
  44. class CGBlockInfo;
  45. /// This class gathers all debug information during compilation and is
  46. /// responsible for emitting to llvm globals or pass directly to the
  47. /// backend.
  48. class CGDebugInfo {
  49. friend class ApplyDebugLocation;
  50. friend class SaveAndRestoreLocation;
  51. CodeGenModule &CGM;
  52. const codegenoptions::DebugInfoKind DebugKind;
  53. bool DebugTypeExtRefs;
  54. llvm::DIBuilder DBuilder;
  55. llvm::DICompileUnit *TheCU = nullptr;
  56. ModuleMap *ClangModuleMap = nullptr;
  57. ExternalASTSource::ASTSourceDescriptor PCHDescriptor;
  58. SourceLocation CurLoc;
  59. llvm::MDNode *CurInlinedAt = nullptr;
  60. llvm::DIType *VTablePtrType = nullptr;
  61. llvm::DIType *ClassTy = nullptr;
  62. llvm::DICompositeType *ObjTy = nullptr;
  63. llvm::DIType *SelTy = nullptr;
  64. #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
  65. llvm::DIType *SingletonId = nullptr;
  66. #include "clang/Basic/OpenCLImageTypes.def"
  67. llvm::DIType *OCLSamplerDITy = nullptr;
  68. llvm::DIType *OCLEventDITy = nullptr;
  69. llvm::DIType *OCLClkEventDITy = nullptr;
  70. llvm::DIType *OCLQueueDITy = nullptr;
  71. llvm::DIType *OCLNDRangeDITy = nullptr;
  72. llvm::DIType *OCLReserveIDDITy = nullptr;
  73. #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
  74. llvm::DIType *Id##Ty = nullptr;
  75. #include "clang/Basic/OpenCLExtensionTypes.def"
  76. /// Cache of previously constructed Types.
  77. llvm::DenseMap<const void *, llvm::TrackingMDRef> TypeCache;
  78. llvm::SmallDenseMap<llvm::StringRef, llvm::StringRef> DebugPrefixMap;
  79. /// Cache that maps VLA types to size expressions for that type,
  80. /// represented by instantiated Metadata nodes.
  81. llvm::SmallDenseMap<QualType, llvm::Metadata *> SizeExprCache;
  82. struct ObjCInterfaceCacheEntry {
  83. const ObjCInterfaceType *Type;
  84. llvm::DIType *Decl;
  85. llvm::DIFile *Unit;
  86. ObjCInterfaceCacheEntry(const ObjCInterfaceType *Type, llvm::DIType *Decl,
  87. llvm::DIFile *Unit)
  88. : Type(Type), Decl(Decl), Unit(Unit) {}
  89. };
  90. /// Cache of previously constructed interfaces which may change.
  91. llvm::SmallVector<ObjCInterfaceCacheEntry, 32> ObjCInterfaceCache;
  92. /// Cache of forward declarations for methods belonging to the interface.
  93. llvm::DenseMap<const ObjCInterfaceDecl *, std::vector<llvm::DISubprogram *>>
  94. ObjCMethodCache;
  95. /// Cache of references to clang modules and precompiled headers.
  96. llvm::DenseMap<const Module *, llvm::TrackingMDRef> ModuleCache;
  97. /// List of interfaces we want to keep even if orphaned.
  98. std::vector<void *> RetainedTypes;
  99. /// Cache of forward declared types to RAUW at the end of compilation.
  100. std::vector<std::pair<const TagType *, llvm::TrackingMDRef>> ReplaceMap;
  101. /// Cache of replaceable forward declarations (functions and
  102. /// variables) to RAUW at the end of compilation.
  103. std::vector<std::pair<const DeclaratorDecl *, llvm::TrackingMDRef>>
  104. FwdDeclReplaceMap;
  105. /// Keep track of our current nested lexical block.
  106. std::vector<llvm::TypedTrackingMDRef<llvm::DIScope>> LexicalBlockStack;
  107. llvm::DenseMap<const Decl *, llvm::TrackingMDRef> RegionMap;
  108. /// Keep track of LexicalBlockStack counter at the beginning of a
  109. /// function. This is used to pop unbalanced regions at the end of a
  110. /// function.
  111. std::vector<unsigned> FnBeginRegionCount;
  112. /// This is a storage for names that are constructed on demand. For
  113. /// example, C++ destructors, C++ operators etc..
  114. llvm::BumpPtrAllocator DebugInfoNames;
  115. StringRef CWDName;
  116. llvm::DenseMap<const char *, llvm::TrackingMDRef> DIFileCache;
  117. llvm::DenseMap<const FunctionDecl *, llvm::TrackingMDRef> SPCache;
  118. /// Cache function definitions relevant to use for parameters mutation
  119. /// analysis.
  120. llvm::DenseMap<const FunctionDecl *, llvm::TrackingMDRef> SPDefCache;
  121. llvm::DenseMap<const ParmVarDecl *, llvm::TrackingMDRef> ParamCache;
  122. /// Cache declarations relevant to DW_TAG_imported_declarations (C++
  123. /// using declarations) that aren't covered by other more specific caches.
  124. llvm::DenseMap<const Decl *, llvm::TrackingMDRef> DeclCache;
  125. llvm::DenseMap<const NamespaceDecl *, llvm::TrackingMDRef> NamespaceCache;
  126. llvm::DenseMap<const NamespaceAliasDecl *, llvm::TrackingMDRef>
  127. NamespaceAliasCache;
  128. llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIDerivedType>>
  129. StaticDataMemberCache;
  130. /// Helper functions for getOrCreateType.
  131. /// @{
  132. /// Currently the checksum of an interface includes the number of
  133. /// ivars and property accessors.
  134. llvm::DIType *CreateType(const BuiltinType *Ty);
  135. llvm::DIType *CreateType(const ComplexType *Ty);
  136. llvm::DIType *CreateQualifiedType(QualType Ty, llvm::DIFile *Fg);
  137. llvm::DIType *CreateType(const TypedefType *Ty, llvm::DIFile *Fg);
  138. llvm::DIType *CreateType(const TemplateSpecializationType *Ty,
  139. llvm::DIFile *Fg);
  140. llvm::DIType *CreateType(const ObjCObjectPointerType *Ty, llvm::DIFile *F);
  141. llvm::DIType *CreateType(const PointerType *Ty, llvm::DIFile *F);
  142. llvm::DIType *CreateType(const BlockPointerType *Ty, llvm::DIFile *F);
  143. llvm::DIType *CreateType(const FunctionType *Ty, llvm::DIFile *F);
  144. /// Get structure or union type.
  145. llvm::DIType *CreateType(const RecordType *Tyg);
  146. llvm::DIType *CreateTypeDefinition(const RecordType *Ty);
  147. llvm::DICompositeType *CreateLimitedType(const RecordType *Ty);
  148. void CollectContainingType(const CXXRecordDecl *RD,
  149. llvm::DICompositeType *CT);
  150. /// Get Objective-C interface type.
  151. llvm::DIType *CreateType(const ObjCInterfaceType *Ty, llvm::DIFile *F);
  152. llvm::DIType *CreateTypeDefinition(const ObjCInterfaceType *Ty,
  153. llvm::DIFile *F);
  154. /// Get Objective-C object type.
  155. llvm::DIType *CreateType(const ObjCObjectType *Ty, llvm::DIFile *F);
  156. llvm::DIType *CreateType(const ObjCTypeParamType *Ty, llvm::DIFile *Unit);
  157. llvm::DIType *CreateType(const VectorType *Ty, llvm::DIFile *F);
  158. llvm::DIType *CreateType(const ArrayType *Ty, llvm::DIFile *F);
  159. llvm::DIType *CreateType(const LValueReferenceType *Ty, llvm::DIFile *F);
  160. llvm::DIType *CreateType(const RValueReferenceType *Ty, llvm::DIFile *Unit);
  161. llvm::DIType *CreateType(const MemberPointerType *Ty, llvm::DIFile *F);
  162. llvm::DIType *CreateType(const AtomicType *Ty, llvm::DIFile *F);
  163. llvm::DIType *CreateType(const PipeType *Ty, llvm::DIFile *F);
  164. /// Get enumeration type.
  165. llvm::DIType *CreateEnumType(const EnumType *Ty);
  166. llvm::DIType *CreateTypeDefinition(const EnumType *Ty);
  167. /// Look up the completed type for a self pointer in the TypeCache and
  168. /// create a copy of it with the ObjectPointer and Artificial flags
  169. /// set. If the type is not cached, a new one is created. This should
  170. /// never happen though, since creating a type for the implicit self
  171. /// argument implies that we already parsed the interface definition
  172. /// and the ivar declarations in the implementation.
  173. llvm::DIType *CreateSelfType(const QualType &QualTy, llvm::DIType *Ty);
  174. /// @}
  175. /// Get the type from the cache or return null type if it doesn't
  176. /// exist.
  177. llvm::DIType *getTypeOrNull(const QualType);
  178. /// Return the debug type for a C++ method.
  179. /// \arg CXXMethodDecl is of FunctionType. This function type is
  180. /// not updated to include implicit \c this pointer. Use this routine
  181. /// to get a method type which includes \c this pointer.
  182. llvm::DISubroutineType *getOrCreateMethodType(const CXXMethodDecl *Method,
  183. llvm::DIFile *F);
  184. llvm::DISubroutineType *
  185. getOrCreateInstanceMethodType(QualType ThisPtr, const FunctionProtoType *Func,
  186. llvm::DIFile *Unit);
  187. llvm::DISubroutineType *
  188. getOrCreateFunctionType(const Decl *D, QualType FnType, llvm::DIFile *F);
  189. /// \return debug info descriptor for vtable.
  190. llvm::DIType *getOrCreateVTablePtrType(llvm::DIFile *F);
  191. /// \return namespace descriptor for the given namespace decl.
  192. llvm::DINamespace *getOrCreateNamespace(const NamespaceDecl *N);
  193. llvm::DIType *CreatePointerLikeType(llvm::dwarf::Tag Tag, const Type *Ty,
  194. QualType PointeeTy, llvm::DIFile *F);
  195. llvm::DIType *getOrCreateStructPtrType(StringRef Name, llvm::DIType *&Cache);
  196. /// A helper function to create a subprogram for a single member
  197. /// function GlobalDecl.
  198. llvm::DISubprogram *CreateCXXMemberFunction(const CXXMethodDecl *Method,
  199. llvm::DIFile *F,
  200. llvm::DIType *RecordTy);
  201. /// A helper function to collect debug info for C++ member
  202. /// functions. This is used while creating debug info entry for a
  203. /// Record.
  204. void CollectCXXMemberFunctions(const CXXRecordDecl *Decl, llvm::DIFile *F,
  205. SmallVectorImpl<llvm::Metadata *> &E,
  206. llvm::DIType *T);
  207. /// A helper function to collect debug info for C++ base
  208. /// classes. This is used while creating debug info entry for a
  209. /// Record.
  210. void CollectCXXBases(const CXXRecordDecl *Decl, llvm::DIFile *F,
  211. SmallVectorImpl<llvm::Metadata *> &EltTys,
  212. llvm::DIType *RecordTy);
  213. /// Helper function for CollectCXXBases.
  214. /// Adds debug info entries for types in Bases that are not in SeenTypes.
  215. void CollectCXXBasesAux(
  216. const CXXRecordDecl *RD, llvm::DIFile *Unit,
  217. SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
  218. const CXXRecordDecl::base_class_const_range &Bases,
  219. llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
  220. llvm::DINode::DIFlags StartingFlags);
  221. /// A helper function to collect template parameters.
  222. llvm::DINodeArray CollectTemplateParams(const TemplateParameterList *TPList,
  223. ArrayRef<TemplateArgument> TAList,
  224. llvm::DIFile *Unit);
  225. /// A helper function to collect debug info for function template
  226. /// parameters.
  227. llvm::DINodeArray CollectFunctionTemplateParams(const FunctionDecl *FD,
  228. llvm::DIFile *Unit);
  229. /// A helper function to collect debug info for function template
  230. /// parameters.
  231. llvm::DINodeArray CollectVarTemplateParams(const VarDecl *VD,
  232. llvm::DIFile *Unit);
  233. /// A helper function to collect debug info for template
  234. /// parameters.
  235. llvm::DINodeArray
  236. CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
  237. llvm::DIFile *F);
  238. llvm::DIType *createFieldType(StringRef name, QualType type,
  239. SourceLocation loc, AccessSpecifier AS,
  240. uint64_t offsetInBits, uint32_t AlignInBits,
  241. llvm::DIFile *tunit, llvm::DIScope *scope,
  242. const RecordDecl *RD = nullptr);
  243. llvm::DIType *createFieldType(StringRef name, QualType type,
  244. SourceLocation loc, AccessSpecifier AS,
  245. uint64_t offsetInBits, llvm::DIFile *tunit,
  246. llvm::DIScope *scope,
  247. const RecordDecl *RD = nullptr) {
  248. return createFieldType(name, type, loc, AS, offsetInBits, 0, tunit, scope,
  249. RD);
  250. }
  251. /// Create new bit field member.
  252. llvm::DIType *createBitFieldType(const FieldDecl *BitFieldDecl,
  253. llvm::DIScope *RecordTy,
  254. const RecordDecl *RD);
  255. /// Helpers for collecting fields of a record.
  256. /// @{
  257. void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
  258. SmallVectorImpl<llvm::Metadata *> &E,
  259. llvm::DIType *RecordTy);
  260. llvm::DIDerivedType *CreateRecordStaticField(const VarDecl *Var,
  261. llvm::DIType *RecordTy,
  262. const RecordDecl *RD);
  263. void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits,
  264. llvm::DIFile *F,
  265. SmallVectorImpl<llvm::Metadata *> &E,
  266. llvm::DIType *RecordTy, const RecordDecl *RD);
  267. void CollectRecordNestedType(const TypeDecl *RD,
  268. SmallVectorImpl<llvm::Metadata *> &E);
  269. void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile *F,
  270. SmallVectorImpl<llvm::Metadata *> &E,
  271. llvm::DICompositeType *RecordTy);
  272. /// If the C++ class has vtable info then insert appropriate debug
  273. /// info entry in EltTys vector.
  274. void CollectVTableInfo(const CXXRecordDecl *Decl, llvm::DIFile *F,
  275. SmallVectorImpl<llvm::Metadata *> &EltTys,
  276. llvm::DICompositeType *RecordTy);
  277. /// @}
  278. /// Create a new lexical block node and push it on the stack.
  279. void CreateLexicalBlock(SourceLocation Loc);
  280. /// If target-specific LLVM \p AddressSpace directly maps to target-specific
  281. /// DWARF address space, appends extended dereferencing mechanism to complex
  282. /// expression \p Expr. Otherwise, does nothing.
  283. ///
  284. /// Extended dereferencing mechanism is has the following format:
  285. /// DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef
  286. void AppendAddressSpaceXDeref(unsigned AddressSpace,
  287. SmallVectorImpl<int64_t> &Expr) const;
  288. /// A helper function to collect debug info for the default elements of a
  289. /// block.
  290. ///
  291. /// \returns The next available field offset after the default elements.
  292. uint64_t collectDefaultElementTypesForBlockPointer(
  293. const BlockPointerType *Ty, llvm::DIFile *Unit,
  294. llvm::DIDerivedType *DescTy, unsigned LineNo,
  295. SmallVectorImpl<llvm::Metadata *> &EltTys);
  296. /// A helper function to collect debug info for the default fields of a
  297. /// block.
  298. void collectDefaultFieldsForBlockLiteralDeclare(
  299. const CGBlockInfo &Block, const ASTContext &Context, SourceLocation Loc,
  300. const llvm::StructLayout &BlockLayout, llvm::DIFile *Unit,
  301. SmallVectorImpl<llvm::Metadata *> &Fields);
  302. public:
  303. CGDebugInfo(CodeGenModule &CGM);
  304. ~CGDebugInfo();
  305. void finalize();
  306. /// Remap a given path with the current debug prefix map
  307. std::string remapDIPath(StringRef) const;
  308. /// Register VLA size expression debug node with the qualified type.
  309. void registerVLASizeExpression(QualType Ty, llvm::Metadata *SizeExpr) {
  310. SizeExprCache[Ty] = SizeExpr;
  311. }
  312. /// Module debugging: Support for building PCMs.
  313. /// @{
  314. /// Set the main CU's DwoId field to \p Signature.
  315. void setDwoId(uint64_t Signature);
  316. /// When generating debug information for a clang module or
  317. /// precompiled header, this module map will be used to determine
  318. /// the module of origin of each Decl.
  319. void setModuleMap(ModuleMap &MMap) { ClangModuleMap = &MMap; }
  320. /// When generating debug information for a clang module or
  321. /// precompiled header, this module map will be used to determine
  322. /// the module of origin of each Decl.
  323. void setPCHDescriptor(ExternalASTSource::ASTSourceDescriptor PCH) {
  324. PCHDescriptor = PCH;
  325. }
  326. /// @}
  327. /// Update the current source location. If \arg loc is invalid it is
  328. /// ignored.
  329. void setLocation(SourceLocation Loc);
  330. /// Return the current source location. This does not necessarily correspond
  331. /// to the IRBuilder's current DebugLoc.
  332. SourceLocation getLocation() const { return CurLoc; }
  333. /// Update the current inline scope. All subsequent calls to \p EmitLocation
  334. /// will create a location with this inlinedAt field.
  335. void setInlinedAt(llvm::MDNode *InlinedAt) { CurInlinedAt = InlinedAt; }
  336. /// \return the current inline scope.
  337. llvm::MDNode *getInlinedAt() const { return CurInlinedAt; }
  338. // Converts a SourceLocation to a DebugLoc
  339. llvm::DebugLoc SourceLocToDebugLoc(SourceLocation Loc);
  340. /// Emit metadata to indicate a change in line/column information in
  341. /// the source file. If the location is invalid, the previous
  342. /// location will be reused.
  343. void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
  344. /// Emit a call to llvm.dbg.function.start to indicate
  345. /// start of a new function.
  346. /// \param Loc The location of the function header.
  347. /// \param ScopeLoc The location of the function body.
  348. void EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
  349. SourceLocation ScopeLoc, QualType FnType,
  350. llvm::Function *Fn, bool CurFnIsThunk,
  351. CGBuilderTy &Builder);
  352. /// Start a new scope for an inlined function.
  353. void EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD);
  354. /// End an inlined function scope.
  355. void EmitInlineFunctionEnd(CGBuilderTy &Builder);
  356. /// Emit debug info for a function declaration.
  357. /// \p Fn is set only when a declaration for a debug call site gets created.
  358. void EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
  359. QualType FnType, llvm::Function *Fn = nullptr);
  360. /// Emit debug info for an extern function being called.
  361. /// This is needed for call site debug info.
  362. void EmitFuncDeclForCallSite(llvm::CallBase *CallOrInvoke,
  363. QualType CalleeType,
  364. const FunctionDecl *CalleeDecl);
  365. /// Constructs the debug code for exiting a function.
  366. void EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn);
  367. /// Emit metadata to indicate the beginning of a new lexical block
  368. /// and push the block onto the stack.
  369. void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
  370. /// Emit metadata to indicate the end of a new lexical block and pop
  371. /// the current block.
  372. void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
  373. /// Emit call to \c llvm.dbg.declare for an automatic variable
  374. /// declaration.
  375. /// Returns a pointer to the DILocalVariable associated with the
  376. /// llvm.dbg.declare, or nullptr otherwise.
  377. llvm::DILocalVariable *
  378. EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
  379. CGBuilderTy &Builder,
  380. const bool UsePointerValue = false);
  381. /// Emit call to \c llvm.dbg.label for an label.
  382. void EmitLabel(const LabelDecl *D, CGBuilderTy &Builder);
  383. /// Emit call to \c llvm.dbg.declare for an imported variable
  384. /// declaration in a block.
  385. void EmitDeclareOfBlockDeclRefVariable(
  386. const VarDecl *variable, llvm::Value *storage, CGBuilderTy &Builder,
  387. const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint = nullptr);
  388. /// Emit call to \c llvm.dbg.declare for an argument variable
  389. /// declaration.
  390. void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
  391. unsigned ArgNo, CGBuilderTy &Builder);
  392. /// Emit call to \c llvm.dbg.declare for the block-literal argument
  393. /// to a block invocation function.
  394. void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
  395. StringRef Name, unsigned ArgNo,
  396. llvm::AllocaInst *LocalAddr,
  397. CGBuilderTy &Builder);
  398. /// Emit information about a global variable.
  399. void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
  400. /// Emit a constant global variable's debug info.
  401. void EmitGlobalVariable(const ValueDecl *VD, const APValue &Init);
  402. /// Emit C++ using directive.
  403. void EmitUsingDirective(const UsingDirectiveDecl &UD);
  404. /// Emit the type explicitly casted to.
  405. void EmitExplicitCastType(QualType Ty);
  406. /// Emit C++ using declaration.
  407. void EmitUsingDecl(const UsingDecl &UD);
  408. /// Emit an @import declaration.
  409. void EmitImportDecl(const ImportDecl &ID);
  410. /// Emit C++ namespace alias.
  411. llvm::DIImportedEntity *EmitNamespaceAlias(const NamespaceAliasDecl &NA);
  412. /// Emit record type's standalone debug info.
  413. llvm::DIType *getOrCreateRecordType(QualType Ty, SourceLocation L);
  414. /// Emit an Objective-C interface type standalone debug info.
  415. llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc);
  416. /// Emit standalone debug info for a type.
  417. llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc);
  418. /// Add heapallocsite metadata for MSAllocator calls.
  419. void addHeapAllocSiteMetadata(llvm::Instruction *CallSite, QualType Ty,
  420. SourceLocation Loc);
  421. void completeType(const EnumDecl *ED);
  422. void completeType(const RecordDecl *RD);
  423. void completeRequiredType(const RecordDecl *RD);
  424. void completeClassData(const RecordDecl *RD);
  425. void completeClass(const RecordDecl *RD);
  426. void completeTemplateDefinition(const ClassTemplateSpecializationDecl &SD);
  427. void completeUnusedClass(const CXXRecordDecl &D);
  428. /// Create debug info for a macro defined by a #define directive or a macro
  429. /// undefined by a #undef directive.
  430. llvm::DIMacro *CreateMacro(llvm::DIMacroFile *Parent, unsigned MType,
  431. SourceLocation LineLoc, StringRef Name,
  432. StringRef Value);
  433. /// Create debug info for a file referenced by an #include directive.
  434. llvm::DIMacroFile *CreateTempMacroFile(llvm::DIMacroFile *Parent,
  435. SourceLocation LineLoc,
  436. SourceLocation FileLoc);
  437. private:
  438. /// Emit call to llvm.dbg.declare for a variable declaration.
  439. /// Returns a pointer to the DILocalVariable associated with the
  440. /// llvm.dbg.declare, or nullptr otherwise.
  441. llvm::DILocalVariable *EmitDeclare(const VarDecl *decl, llvm::Value *AI,
  442. llvm::Optional<unsigned> ArgNo,
  443. CGBuilderTy &Builder,
  444. const bool UsePointerValue = false);
  445. struct BlockByRefType {
  446. /// The wrapper struct used inside the __block_literal struct.
  447. llvm::DIType *BlockByRefWrapper;
  448. /// The type as it appears in the source code.
  449. llvm::DIType *WrappedType;
  450. };
  451. /// Build up structure info for the byref. See \a BuildByRefType.
  452. BlockByRefType EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
  453. uint64_t *OffSet);
  454. /// Get context info for the DeclContext of \p Decl.
  455. llvm::DIScope *getDeclContextDescriptor(const Decl *D);
  456. /// Get context info for a given DeclContext \p Decl.
  457. llvm::DIScope *getContextDescriptor(const Decl *Context,
  458. llvm::DIScope *Default);
  459. llvm::DIScope *getCurrentContextDescriptor(const Decl *Decl);
  460. /// Create a forward decl for a RecordType in a given context.
  461. llvm::DICompositeType *getOrCreateRecordFwdDecl(const RecordType *,
  462. llvm::DIScope *);
  463. /// Return current directory name.
  464. StringRef getCurrentDirname();
  465. /// Create new compile unit.
  466. void CreateCompileUnit();
  467. /// Compute the file checksum debug info for input file ID.
  468. Optional<llvm::DIFile::ChecksumKind>
  469. computeChecksum(FileID FID, SmallString<32> &Checksum) const;
  470. /// Get the source of the given file ID.
  471. Optional<StringRef> getSource(const SourceManager &SM, FileID FID);
  472. /// Convenience function to get the file debug info descriptor for the input
  473. /// location.
  474. llvm::DIFile *getOrCreateFile(SourceLocation Loc);
  475. /// Create a file debug info descriptor for a source file.
  476. llvm::DIFile *
  477. createFile(StringRef FileName,
  478. Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo,
  479. Optional<StringRef> Source);
  480. /// Get the type from the cache or create a new type if necessary.
  481. llvm::DIType *getOrCreateType(QualType Ty, llvm::DIFile *Fg);
  482. /// Get a reference to a clang module. If \p CreateSkeletonCU is true,
  483. /// this also creates a split dwarf skeleton compile unit.
  484. llvm::DIModule *
  485. getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
  486. bool CreateSkeletonCU);
  487. /// DebugTypeExtRefs: If \p D originated in a clang module, return it.
  488. llvm::DIModule *getParentModuleOrNull(const Decl *D);
  489. /// Get the type from the cache or create a new partial type if
  490. /// necessary.
  491. llvm::DICompositeType *getOrCreateLimitedType(const RecordType *Ty,
  492. llvm::DIFile *F);
  493. /// Create type metadata for a source language type.
  494. llvm::DIType *CreateTypeNode(QualType Ty, llvm::DIFile *Fg);
  495. /// Create new member and increase Offset by FType's size.
  496. llvm::DIType *CreateMemberType(llvm::DIFile *Unit, QualType FType,
  497. StringRef Name, uint64_t *Offset);
  498. /// Retrieve the DIDescriptor, if any, for the canonical form of this
  499. /// declaration.
  500. llvm::DINode *getDeclarationOrDefinition(const Decl *D);
  501. /// \return debug info descriptor to describe method
  502. /// declaration for the given method definition.
  503. llvm::DISubprogram *getFunctionDeclaration(const Decl *D);
  504. /// \return debug info descriptor to describe in-class static data
  505. /// member declaration for the given out-of-class definition. If D
  506. /// is an out-of-class definition of a static data member of a
  507. /// class, find its corresponding in-class declaration.
  508. llvm::DIDerivedType *
  509. getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D);
  510. /// Helper that either creates a forward declaration or a stub.
  511. llvm::DISubprogram *getFunctionFwdDeclOrStub(GlobalDecl GD, bool Stub);
  512. /// Create a subprogram describing the forward declaration
  513. /// represented in the given FunctionDecl wrapped in a GlobalDecl.
  514. llvm::DISubprogram *getFunctionForwardDeclaration(GlobalDecl GD);
  515. /// Create a DISubprogram describing the function
  516. /// represented in the given FunctionDecl wrapped in a GlobalDecl.
  517. llvm::DISubprogram *getFunctionStub(GlobalDecl GD);
  518. /// Create a global variable describing the forward declaration
  519. /// represented in the given VarDecl.
  520. llvm::DIGlobalVariable *
  521. getGlobalVariableForwardDeclaration(const VarDecl *VD);
  522. /// Return a global variable that represents one of the collection of global
  523. /// variables created for an anonmyous union.
  524. ///
  525. /// Recursively collect all of the member fields of a global
  526. /// anonymous decl and create static variables for them. The first
  527. /// time this is called it needs to be on a union and then from
  528. /// there we can have additional unnamed fields.
  529. llvm::DIGlobalVariableExpression *
  530. CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile *Unit,
  531. unsigned LineNo, StringRef LinkageName,
  532. llvm::GlobalVariable *Var, llvm::DIScope *DContext);
  533. /// Return flags which enable debug info emission for call sites, provided
  534. /// that it is supported and enabled.
  535. llvm::DINode::DIFlags getCallSiteRelatedAttrs() const;
  536. /// Get the printing policy for producing names for debug info.
  537. PrintingPolicy getPrintingPolicy() const;
  538. /// Get function name for the given FunctionDecl. If the name is
  539. /// constructed on demand (e.g., C++ destructor) then the name is
  540. /// stored on the side.
  541. StringRef getFunctionName(const FunctionDecl *FD);
  542. /// Returns the unmangled name of an Objective-C method.
  543. /// This is the display name for the debugging info.
  544. StringRef getObjCMethodName(const ObjCMethodDecl *FD);
  545. /// Return selector name. This is used for debugging
  546. /// info.
  547. StringRef getSelectorName(Selector S);
  548. /// Get class name including template argument list.
  549. StringRef getClassName(const RecordDecl *RD);
  550. /// Get the vtable name for the given class.
  551. StringRef getVTableName(const CXXRecordDecl *Decl);
  552. /// Get the name to use in the debug info for a dynamic initializer or atexit
  553. /// stub function.
  554. StringRef getDynamicInitializerName(const VarDecl *VD,
  555. DynamicInitKind StubKind,
  556. llvm::Function *InitFn);
  557. /// Get line number for the location. If location is invalid
  558. /// then use current location.
  559. unsigned getLineNumber(SourceLocation Loc);
  560. /// Get column number for the location. If location is
  561. /// invalid then use current location.
  562. /// \param Force Assume DebugColumnInfo option is true.
  563. unsigned getColumnNumber(SourceLocation Loc, bool Force = false);
  564. /// Collect various properties of a FunctionDecl.
  565. /// \param GD A GlobalDecl whose getDecl() must return a FunctionDecl.
  566. void collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
  567. StringRef &Name, StringRef &LinkageName,
  568. llvm::DIScope *&FDContext,
  569. llvm::DINodeArray &TParamsArray,
  570. llvm::DINode::DIFlags &Flags);
  571. /// Collect various properties of a VarDecl.
  572. void collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
  573. unsigned &LineNo, QualType &T, StringRef &Name,
  574. StringRef &LinkageName,
  575. llvm::MDTuple *&TemplateParameters,
  576. llvm::DIScope *&VDContext);
  577. /// Allocate a copy of \p A using the DebugInfoNames allocator
  578. /// and return a reference to it. If multiple arguments are given the strings
  579. /// are concatenated.
  580. StringRef internString(StringRef A, StringRef B = StringRef()) {
  581. char *Data = DebugInfoNames.Allocate<char>(A.size() + B.size());
  582. if (!A.empty())
  583. std::memcpy(Data, A.data(), A.size());
  584. if (!B.empty())
  585. std::memcpy(Data + A.size(), B.data(), B.size());
  586. return StringRef(Data, A.size() + B.size());
  587. }
  588. };
  589. /// A scoped helper to set the current debug location to the specified
  590. /// location or preferred location of the specified Expr.
  591. class ApplyDebugLocation {
  592. private:
  593. void init(SourceLocation TemporaryLocation, bool DefaultToEmpty = false);
  594. ApplyDebugLocation(CodeGenFunction &CGF, bool DefaultToEmpty,
  595. SourceLocation TemporaryLocation);
  596. llvm::DebugLoc OriginalLocation;
  597. CodeGenFunction *CGF;
  598. public:
  599. /// Set the location to the (valid) TemporaryLocation.
  600. ApplyDebugLocation(CodeGenFunction &CGF, SourceLocation TemporaryLocation);
  601. ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E);
  602. ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc);
  603. ApplyDebugLocation(ApplyDebugLocation &&Other) : CGF(Other.CGF) {
  604. Other.CGF = nullptr;
  605. }
  606. ~ApplyDebugLocation();
  607. /// Apply TemporaryLocation if it is valid. Otherwise switch
  608. /// to an artificial debug location that has a valid scope, but no
  609. /// line information.
  610. ///
  611. /// Artificial locations are useful when emitting compiler-generated
  612. /// helper functions that have no source location associated with
  613. /// them. The DWARF specification allows the compiler to use the
  614. /// special line number 0 to indicate code that can not be
  615. /// attributed to any source location. Note that passing an empty
  616. /// SourceLocation to CGDebugInfo::setLocation() will result in the
  617. /// last valid location being reused.
  618. static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF) {
  619. return ApplyDebugLocation(CGF, false, SourceLocation());
  620. }
  621. /// Apply TemporaryLocation if it is valid. Otherwise switch
  622. /// to an artificial debug location that has a valid scope, but no
  623. /// line information.
  624. static ApplyDebugLocation
  625. CreateDefaultArtificial(CodeGenFunction &CGF,
  626. SourceLocation TemporaryLocation) {
  627. return ApplyDebugLocation(CGF, false, TemporaryLocation);
  628. }
  629. /// Set the IRBuilder to not attach debug locations. Note that
  630. /// passing an empty SourceLocation to \a CGDebugInfo::setLocation()
  631. /// will result in the last valid location being reused. Note that
  632. /// all instructions that do not have a location at the beginning of
  633. /// a function are counted towards to function prologue.
  634. static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF) {
  635. return ApplyDebugLocation(CGF, true, SourceLocation());
  636. }
  637. };
  638. /// A scoped helper to set the current debug location to an inlined location.
  639. class ApplyInlineDebugLocation {
  640. SourceLocation SavedLocation;
  641. CodeGenFunction *CGF;
  642. public:
  643. /// Set up the CodeGenFunction's DebugInfo to produce inline locations for the
  644. /// function \p InlinedFn. The current debug location becomes the inlined call
  645. /// site of the inlined function.
  646. ApplyInlineDebugLocation(CodeGenFunction &CGF, GlobalDecl InlinedFn);
  647. /// Restore everything back to the original state.
  648. ~ApplyInlineDebugLocation();
  649. };
  650. } // namespace CodeGen
  651. } // namespace clang
  652. #endif // LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H