DWARFLinkerDeclContext.cpp 8.5 KB

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