CGDebugInfo.h 37 KB

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