DWARFLinkerDeclContext.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFLinkerDeclContext.h ---------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DWARFLINKER_DWARFLINKERDECLCONTEXT_H
  14. #define LLVM_DWARFLINKER_DWARFLINKERDECLCONTEXT_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/DenseMapInfo.h"
  17. #include "llvm/ADT/DenseSet.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/CodeGen/NonRelocatableStringpool.h"
  20. #include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
  21. #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
  22. #include "llvm/DebugInfo/DWARF/DWARFDie.h"
  23. #include "llvm/Support/FileSystem.h"
  24. #include "llvm/Support/Path.h"
  25. namespace llvm {
  26. struct DeclMapInfo;
  27. /// Small helper that resolves and caches file paths. This helps reduce the
  28. /// number of calls to realpath which is expensive. We assume the input are
  29. /// files, and cache the realpath of their parent. This way we can quickly
  30. /// resolve different files under the same path.
  31. class CachedPathResolver {
  32. public:
  33. /// Resolve a path by calling realpath and cache its result. The returned
  34. /// StringRef is interned in the given \p StringPool.
  35. StringRef resolve(const std::string &Path,
  36. NonRelocatableStringpool &StringPool) {
  37. StringRef FileName = sys::path::filename(Path);
  38. StringRef ParentPath = sys::path::parent_path(Path);
  39. // If the ParentPath has not yet been resolved, resolve and cache it for
  40. // future look-ups.
  41. if (!ResolvedPaths.count(ParentPath)) {
  42. SmallString<256> RealPath;
  43. sys::fs::real_path(ParentPath, RealPath);
  44. ResolvedPaths.insert(
  45. {ParentPath, std::string(RealPath.c_str(), RealPath.size())});
  46. }
  47. // Join the file name again with the resolved path.
  48. SmallString<256> ResolvedPath(ResolvedPaths[ParentPath]);
  49. sys::path::append(ResolvedPath, FileName);
  50. return StringPool.internString(ResolvedPath);
  51. }
  52. private:
  53. StringMap<std::string> ResolvedPaths;
  54. };
  55. /// A DeclContext is a named program scope that is used for ODR uniquing of
  56. /// types.
  57. ///
  58. /// The set of DeclContext for the ODR-subject parts of a Dwarf link is
  59. /// expanded (and uniqued) with each new object file processed. We need to
  60. /// determine the context of each DIE in an linked object file to see if the
  61. /// corresponding type has already been emitted.
  62. ///
  63. /// The contexts are conceptually organized as a tree (eg. a function scope is
  64. /// contained in a namespace scope that contains other scopes), but
  65. /// storing/accessing them in an actual tree is too inefficient: we need to be
  66. /// able to very quickly query a context for a given child context by name.
  67. /// Storing a StringMap in each DeclContext would be too space inefficient.
  68. ///
  69. /// The solution here is to give each DeclContext a link to its parent (this
  70. /// allows to walk up the tree), but to query the existence of a specific
  71. /// DeclContext using a separate DenseMap keyed on the hash of the fully
  72. /// qualified name of the context.
  73. class DeclContext {
  74. public:
  75. using Map = DenseSet<DeclContext *, DeclMapInfo>;
  76. DeclContext() : DefinedInClangModule(0), Parent(*this) {}
  77. DeclContext(unsigned Hash, uint32_t Line, uint32_t ByteSize, uint16_t Tag,
  78. StringRef Name, StringRef File, const DeclContext &Parent,
  79. DWARFDie LastSeenDIE = DWARFDie(), unsigned CUId = 0)
  80. : QualifiedNameHash(Hash), Line(Line), ByteSize(ByteSize), Tag(Tag),
  81. DefinedInClangModule(0), Name(Name), File(File), Parent(Parent),
  82. LastSeenDIE(LastSeenDIE), LastSeenCompileUnitID(CUId) {}
  83. uint32_t getQualifiedNameHash() const { return QualifiedNameHash; }
  84. bool setLastSeenDIE(CompileUnit &U, const DWARFDie &Die);
  85. uint32_t getCanonicalDIEOffset() const { return CanonicalDIEOffset; }
  86. void setCanonicalDIEOffset(uint32_t Offset) { CanonicalDIEOffset = Offset; }
  87. bool isDefinedInClangModule() const { return DefinedInClangModule; }
  88. void setDefinedInClangModule(bool Val) { DefinedInClangModule = Val; }
  89. uint16_t getTag() const { return Tag; }
  90. private:
  91. friend DeclMapInfo;
  92. unsigned QualifiedNameHash = 0;
  93. uint32_t Line = 0;
  94. uint32_t ByteSize = 0;
  95. uint16_t Tag = dwarf::DW_TAG_compile_unit;
  96. unsigned DefinedInClangModule : 1;
  97. StringRef Name;
  98. StringRef File;
  99. const DeclContext &Parent;
  100. DWARFDie LastSeenDIE;
  101. uint32_t LastSeenCompileUnitID = 0;
  102. uint32_t CanonicalDIEOffset = 0;
  103. };
  104. /// This class gives a tree-like API to the DenseMap that stores the
  105. /// DeclContext objects. It holds the BumpPtrAllocator where these objects will
  106. /// be allocated.
  107. class DeclContextTree {
  108. public:
  109. /// Get the child of \a Context described by \a DIE in \a Unit. The
  110. /// required strings will be interned in \a StringPool.
  111. /// \returns The child DeclContext along with one bit that is set if
  112. /// this context is invalid.
  113. ///
  114. /// An invalid context means it shouldn't be considered for uniquing, but its
  115. /// not returning null, because some children of that context might be
  116. /// uniquing candidates.
  117. ///
  118. /// FIXME: The invalid bit along the return value is to emulate some
  119. /// dsymutil-classic functionality.
  120. PointerIntPair<DeclContext *, 1> getChildDeclContext(DeclContext &Context,
  121. const DWARFDie &DIE,
  122. CompileUnit &Unit,
  123. bool InClangModule);
  124. DeclContext &getRoot() { return Root; }
  125. private:
  126. BumpPtrAllocator Allocator;
  127. DeclContext Root;
  128. DeclContext::Map Contexts;
  129. /// Cached resolved paths from the line table.
  130. /// The key is <UniqueUnitID, FileIdx>.
  131. using ResolvedPathsMap = DenseMap<std::pair<unsigned, unsigned>, StringRef>;
  132. ResolvedPathsMap ResolvedPaths;
  133. /// Helper that resolves and caches fragments of file paths.
  134. CachedPathResolver PathResolver;
  135. /// String pool keeping real path bodies.
  136. NonRelocatableStringpool StringPool;
  137. StringRef getResolvedPath(CompileUnit &CU, unsigned FileNum,
  138. const DWARFDebugLine::LineTable &LineTable);
  139. };
  140. /// Info type for the DenseMap storing the DeclContext pointers.
  141. struct DeclMapInfo : private DenseMapInfo<DeclContext *> {
  142. using DenseMapInfo<DeclContext *>::getEmptyKey;
  143. using DenseMapInfo<DeclContext *>::getTombstoneKey;
  144. static unsigned getHashValue(const DeclContext *Ctxt) {
  145. return Ctxt->QualifiedNameHash;
  146. }
  147. static bool isEqual(const DeclContext *LHS, const DeclContext *RHS) {
  148. if (RHS == getEmptyKey() || RHS == getTombstoneKey())
  149. return RHS == LHS;
  150. return LHS->QualifiedNameHash == RHS->QualifiedNameHash &&
  151. LHS->Line == RHS->Line && LHS->ByteSize == RHS->ByteSize &&
  152. LHS->Name.data() == RHS->Name.data() &&
  153. LHS->File.data() == RHS->File.data() &&
  154. LHS->Parent.QualifiedNameHash == RHS->Parent.QualifiedNameHash;
  155. }
  156. };
  157. } // end namespace llvm
  158. #endif // LLVM_DWARFLINKER_DWARFLINKERDECLCONTEXT_H
  159. #ifdef __GNUC__
  160. #pragma GCC diagnostic pop
  161. #endif