DWARFLinkerDeclContext.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //===- DWARFLinkerDeclContext.cpp -----------------------------------------===//
  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. #include "llvm/DWARFLinker/DWARFLinkerDeclContext.h"
  9. #include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
  10. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  11. #include "llvm/DebugInfo/DWARF/DWARFDie.h"
  12. #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
  13. namespace llvm {
  14. /// Set the last DIE/CU a context was seen in and, possibly invalidate the
  15. /// context if it is ambiguous.
  16. ///
  17. /// In the current implementation, we don't handle overloaded functions well,
  18. /// because the argument types are not taken into account when computing the
  19. /// DeclContext tree.
  20. ///
  21. /// Some of this is mitigated byt using mangled names that do contain the
  22. /// arguments types, but sometimes (e.g. with function templates) we don't have
  23. /// that. In that case, just do not unique anything that refers to the contexts
  24. /// we are not able to distinguish.
  25. ///
  26. /// If a context that is not a namespace appears twice in the same CU, we know
  27. /// it is ambiguous. Make it invalid.
  28. bool DeclContext::setLastSeenDIE(CompileUnit &U, const DWARFDie &Die) {
  29. if (LastSeenCompileUnitID == U.getUniqueID()) {
  30. DWARFUnit &OrigUnit = U.getOrigUnit();
  31. uint32_t FirstIdx = OrigUnit.getDIEIndex(LastSeenDIE);
  32. U.getInfo(FirstIdx).Ctxt = nullptr;
  33. return false;
  34. }
  35. LastSeenCompileUnitID = U.getUniqueID();
  36. LastSeenDIE = Die;
  37. return true;
  38. }
  39. PointerIntPair<DeclContext *, 1>
  40. DeclContextTree::getChildDeclContext(DeclContext &Context, const DWARFDie &DIE,
  41. CompileUnit &U, bool InClangModule) {
  42. unsigned Tag = DIE.getTag();
  43. // FIXME: dsymutil-classic compat: We should bail out here if we
  44. // have a specification or an abstract_origin. We will get the
  45. // parent context wrong here.
  46. switch (Tag) {
  47. default:
  48. // By default stop gathering child contexts.
  49. return PointerIntPair<DeclContext *, 1>(nullptr);
  50. case dwarf::DW_TAG_module:
  51. break;
  52. case dwarf::DW_TAG_compile_unit:
  53. return PointerIntPair<DeclContext *, 1>(&Context);
  54. case dwarf::DW_TAG_subprogram:
  55. // Do not unique anything inside CU local functions.
  56. if ((Context.getTag() == dwarf::DW_TAG_namespace ||
  57. Context.getTag() == dwarf::DW_TAG_compile_unit) &&
  58. !dwarf::toUnsigned(DIE.find(dwarf::DW_AT_external), 0))
  59. return PointerIntPair<DeclContext *, 1>(nullptr);
  60. [[fallthrough]];
  61. case dwarf::DW_TAG_member:
  62. case dwarf::DW_TAG_namespace:
  63. case dwarf::DW_TAG_structure_type:
  64. case dwarf::DW_TAG_class_type:
  65. case dwarf::DW_TAG_union_type:
  66. case dwarf::DW_TAG_enumeration_type:
  67. case dwarf::DW_TAG_typedef:
  68. // Artificial things might be ambiguous, because they might be created on
  69. // demand. For example implicitly defined constructors are ambiguous
  70. // because of the way we identify contexts, and they won't be generated
  71. // every time everywhere.
  72. if (dwarf::toUnsigned(DIE.find(dwarf::DW_AT_artificial), 0))
  73. return PointerIntPair<DeclContext *, 1>(nullptr);
  74. break;
  75. }
  76. StringRef NameRef;
  77. StringRef FileRef;
  78. if (const char *LinkageName = DIE.getLinkageName())
  79. NameRef = StringPool.internString(LinkageName);
  80. else if (const char *ShortName = DIE.getShortName())
  81. NameRef = StringPool.internString(ShortName);
  82. bool IsAnonymousNamespace = NameRef.empty() && Tag == dwarf::DW_TAG_namespace;
  83. if (IsAnonymousNamespace) {
  84. // FIXME: For dsymutil-classic compatibility. I think uniquing within
  85. // anonymous namespaces is wrong. There is no ODR guarantee there.
  86. NameRef = "(anonymous namespace)";
  87. }
  88. if (Tag != dwarf::DW_TAG_class_type && Tag != dwarf::DW_TAG_structure_type &&
  89. Tag != dwarf::DW_TAG_union_type &&
  90. Tag != dwarf::DW_TAG_enumeration_type && NameRef.empty())
  91. return PointerIntPair<DeclContext *, 1>(nullptr);
  92. unsigned Line = 0;
  93. unsigned ByteSize = std::numeric_limits<uint32_t>::max();
  94. if (!InClangModule) {
  95. // Gather some discriminating data about the DeclContext we will be
  96. // creating: File, line number and byte size. This shouldn't be necessary,
  97. // because the ODR is just about names, but given that we do some
  98. // approximations with overloaded functions and anonymous namespaces, use
  99. // these additional data points to make the process safer.
  100. //
  101. // This is disabled for clang modules, because forward declarations of
  102. // module-defined types do not have a file and line.
  103. ByteSize = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_byte_size),
  104. std::numeric_limits<uint64_t>::max());
  105. if (Tag != dwarf::DW_TAG_namespace || IsAnonymousNamespace) {
  106. if (unsigned FileNum =
  107. dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_file), 0)) {
  108. if (const auto *LT = U.getOrigUnit().getContext().getLineTableForUnit(
  109. &U.getOrigUnit())) {
  110. // FIXME: dsymutil-classic compatibility. I'd rather not
  111. // unique anything in anonymous namespaces, but if we do, then
  112. // verify that the file and line correspond.
  113. if (IsAnonymousNamespace)
  114. FileNum = 1;
  115. if (LT->hasFileAtIndex(FileNum)) {
  116. Line = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_line), 0);
  117. // Cache the resolved paths based on the index in the line table,
  118. // because calling realpath is expensive.
  119. FileRef = getResolvedPath(U, FileNum, *LT);
  120. }
  121. }
  122. }
  123. }
  124. }
  125. if (!Line && NameRef.empty())
  126. return PointerIntPair<DeclContext *, 1>(nullptr);
  127. // We hash NameRef, which is the mangled name, in order to get most
  128. // overloaded functions resolve correctly.
  129. //
  130. // Strictly speaking, hashing the Tag is only necessary for a
  131. // DW_TAG_module, to prevent uniquing of a module and a namespace
  132. // with the same name.
  133. //
  134. // FIXME: dsymutil-classic won't unique the same type presented
  135. // once as a struct and once as a class. Using the Tag in the fully
  136. // qualified name hash to get the same effect.
  137. unsigned Hash = hash_combine(Context.getQualifiedNameHash(), Tag, NameRef);
  138. // FIXME: dsymutil-classic compatibility: when we don't have a name,
  139. // use the filename.
  140. if (IsAnonymousNamespace)
  141. Hash = hash_combine(Hash, FileRef);
  142. // Now look if this context already exists.
  143. DeclContext Key(Hash, Line, ByteSize, Tag, NameRef, FileRef, Context);
  144. auto ContextIter = Contexts.find(&Key);
  145. if (ContextIter == Contexts.end()) {
  146. // The context wasn't found.
  147. bool Inserted;
  148. DeclContext *NewContext =
  149. new (Allocator) DeclContext(Hash, Line, ByteSize, Tag, NameRef, FileRef,
  150. Context, DIE, U.getUniqueID());
  151. std::tie(ContextIter, Inserted) = Contexts.insert(NewContext);
  152. assert(Inserted && "Failed to insert DeclContext");
  153. (void)Inserted;
  154. } else if (Tag != dwarf::DW_TAG_namespace &&
  155. !(*ContextIter)->setLastSeenDIE(U, DIE)) {
  156. // The context was found, but it is ambiguous with another context
  157. // in the same file. Mark it invalid.
  158. return PointerIntPair<DeclContext *, 1>(*ContextIter, /* IntVal= */ 1);
  159. }
  160. assert(ContextIter != Contexts.end());
  161. // FIXME: dsymutil-classic compatibility. Union types aren't
  162. // uniques, but their children might be.
  163. if ((Tag == dwarf::DW_TAG_subprogram &&
  164. Context.getTag() != dwarf::DW_TAG_structure_type &&
  165. Context.getTag() != dwarf::DW_TAG_class_type) ||
  166. (Tag == dwarf::DW_TAG_union_type))
  167. return PointerIntPair<DeclContext *, 1>(*ContextIter, /* IntVal= */ 1);
  168. return PointerIntPair<DeclContext *, 1>(*ContextIter);
  169. }
  170. StringRef
  171. DeclContextTree::getResolvedPath(CompileUnit &CU, unsigned FileNum,
  172. const DWARFDebugLine::LineTable &LineTable) {
  173. std::pair<unsigned, unsigned> Key = {CU.getUniqueID(), FileNum};
  174. ResolvedPathsMap::const_iterator It = ResolvedPaths.find(Key);
  175. if (It == ResolvedPaths.end()) {
  176. std::string FileName;
  177. bool FoundFileName = LineTable.getFileNameByIndex(
  178. FileNum, CU.getOrigUnit().getCompilationDir(),
  179. DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName);
  180. (void)FoundFileName;
  181. assert(FoundFileName && "Must get file name from line table");
  182. // Second level of caching, this time based on the file's parent
  183. // path.
  184. StringRef ResolvedPath = PathResolver.resolve(FileName, StringPool);
  185. It = ResolvedPaths.insert(std::make_pair(Key, ResolvedPath)).first;
  186. }
  187. return It->second;
  188. }
  189. } // namespace llvm