Path: blob/main/contrib/llvm-project/llvm/lib/DWARFLinker/Classic/DWARFLinkerDeclContext.cpp
35292 views
//===- DWARFLinkerDeclContext.cpp -----------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/DWARFLinker/Classic/DWARFLinkerDeclContext.h"9#include "llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h"10#include "llvm/DebugInfo/DWARF/DWARFContext.h"11#include "llvm/DebugInfo/DWARF/DWARFDie.h"12#include "llvm/DebugInfo/DWARF/DWARFUnit.h"1314namespace llvm {1516using namespace dwarf_linker;17using namespace dwarf_linker::classic;1819/// Set the last DIE/CU a context was seen in and, possibly invalidate the20/// context if it is ambiguous.21///22/// In the current implementation, we don't handle overloaded functions well,23/// because the argument types are not taken into account when computing the24/// DeclContext tree.25///26/// Some of this is mitigated byt using mangled names that do contain the27/// arguments types, but sometimes (e.g. with function templates) we don't have28/// that. In that case, just do not unique anything that refers to the contexts29/// we are not able to distinguish.30///31/// If a context that is not a namespace appears twice in the same CU, we know32/// it is ambiguous. Make it invalid.33bool DeclContext::setLastSeenDIE(CompileUnit &U, const DWARFDie &Die) {34if (LastSeenCompileUnitID == U.getUniqueID()) {35DWARFUnit &OrigUnit = U.getOrigUnit();36uint32_t FirstIdx = OrigUnit.getDIEIndex(LastSeenDIE);37U.getInfo(FirstIdx).Ctxt = nullptr;38return false;39}4041LastSeenCompileUnitID = U.getUniqueID();42LastSeenDIE = Die;43return true;44}4546PointerIntPair<DeclContext *, 1>47DeclContextTree::getChildDeclContext(DeclContext &Context, const DWARFDie &DIE,48CompileUnit &U, bool InClangModule) {49unsigned Tag = DIE.getTag();5051// FIXME: dsymutil-classic compat: We should bail out here if we52// have a specification or an abstract_origin. We will get the53// parent context wrong here.5455switch (Tag) {56default:57// By default stop gathering child contexts.58return PointerIntPair<DeclContext *, 1>(nullptr);59case dwarf::DW_TAG_module:60break;61case dwarf::DW_TAG_compile_unit:62return PointerIntPair<DeclContext *, 1>(&Context);63case dwarf::DW_TAG_subprogram:64// Do not unique anything inside CU local functions.65if ((Context.getTag() == dwarf::DW_TAG_namespace ||66Context.getTag() == dwarf::DW_TAG_compile_unit) &&67!dwarf::toUnsigned(DIE.find(dwarf::DW_AT_external), 0))68return PointerIntPair<DeclContext *, 1>(nullptr);69[[fallthrough]];70case dwarf::DW_TAG_member:71case dwarf::DW_TAG_namespace:72case dwarf::DW_TAG_structure_type:73case dwarf::DW_TAG_class_type:74case dwarf::DW_TAG_union_type:75case dwarf::DW_TAG_enumeration_type:76case dwarf::DW_TAG_typedef:77// Artificial things might be ambiguous, because they might be created on78// demand. For example implicitly defined constructors are ambiguous79// because of the way we identify contexts, and they won't be generated80// every time everywhere.81if (dwarf::toUnsigned(DIE.find(dwarf::DW_AT_artificial), 0))82return PointerIntPair<DeclContext *, 1>(nullptr);83break;84}8586StringRef NameRef;87StringRef FileRef;8889if (const char *LinkageName = DIE.getLinkageName())90NameRef = StringPool.internString(LinkageName);91else if (const char *ShortName = DIE.getShortName())92NameRef = StringPool.internString(ShortName);9394bool IsAnonymousNamespace = NameRef.empty() && Tag == dwarf::DW_TAG_namespace;95if (IsAnonymousNamespace) {96// FIXME: For dsymutil-classic compatibility. I think uniquing within97// anonymous namespaces is wrong. There is no ODR guarantee there.98NameRef = "(anonymous namespace)";99}100101if (Tag != dwarf::DW_TAG_class_type && Tag != dwarf::DW_TAG_structure_type &&102Tag != dwarf::DW_TAG_union_type &&103Tag != dwarf::DW_TAG_enumeration_type && NameRef.empty())104return PointerIntPair<DeclContext *, 1>(nullptr);105106unsigned Line = 0;107unsigned ByteSize = std::numeric_limits<uint32_t>::max();108109if (!InClangModule) {110// Gather some discriminating data about the DeclContext we will be111// creating: File, line number and byte size. This shouldn't be necessary,112// because the ODR is just about names, but given that we do some113// approximations with overloaded functions and anonymous namespaces, use114// these additional data points to make the process safer.115//116// This is disabled for clang modules, because forward declarations of117// module-defined types do not have a file and line.118ByteSize = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_byte_size),119std::numeric_limits<uint64_t>::max());120if (Tag != dwarf::DW_TAG_namespace || IsAnonymousNamespace) {121if (unsigned FileNum =122dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_file), 0)) {123if (const auto *LT = U.getOrigUnit().getContext().getLineTableForUnit(124&U.getOrigUnit())) {125// FIXME: dsymutil-classic compatibility. I'd rather not126// unique anything in anonymous namespaces, but if we do, then127// verify that the file and line correspond.128if (IsAnonymousNamespace)129FileNum = 1;130131if (LT->hasFileAtIndex(FileNum)) {132Line = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_line), 0);133// Cache the resolved paths based on the index in the line table,134// because calling realpath is expensive.135FileRef = getResolvedPath(U, FileNum, *LT);136}137}138}139}140}141142if (!Line && NameRef.empty())143return PointerIntPair<DeclContext *, 1>(nullptr);144145// We hash NameRef, which is the mangled name, in order to get most146// overloaded functions resolve correctly.147//148// Strictly speaking, hashing the Tag is only necessary for a149// DW_TAG_module, to prevent uniquing of a module and a namespace150// with the same name.151//152// FIXME: dsymutil-classic won't unique the same type presented153// once as a struct and once as a class. Using the Tag in the fully154// qualified name hash to get the same effect.155unsigned Hash = hash_combine(Context.getQualifiedNameHash(), Tag, NameRef);156157// FIXME: dsymutil-classic compatibility: when we don't have a name,158// use the filename.159if (IsAnonymousNamespace)160Hash = hash_combine(Hash, FileRef);161162// Now look if this context already exists.163DeclContext Key(Hash, Line, ByteSize, Tag, NameRef, FileRef, Context);164auto ContextIter = Contexts.find(&Key);165166if (ContextIter == Contexts.end()) {167// The context wasn't found.168bool Inserted;169DeclContext *NewContext =170new (Allocator) DeclContext(Hash, Line, ByteSize, Tag, NameRef, FileRef,171Context, DIE, U.getUniqueID());172std::tie(ContextIter, Inserted) = Contexts.insert(NewContext);173assert(Inserted && "Failed to insert DeclContext");174(void)Inserted;175} else if (Tag != dwarf::DW_TAG_namespace &&176!(*ContextIter)->setLastSeenDIE(U, DIE)) {177// The context was found, but it is ambiguous with another context178// in the same file. Mark it invalid.179return PointerIntPair<DeclContext *, 1>(*ContextIter, /* IntVal= */ 1);180}181182assert(ContextIter != Contexts.end());183// FIXME: dsymutil-classic compatibility. Union types aren't184// uniques, but their children might be.185if ((Tag == dwarf::DW_TAG_subprogram &&186Context.getTag() != dwarf::DW_TAG_structure_type &&187Context.getTag() != dwarf::DW_TAG_class_type) ||188(Tag == dwarf::DW_TAG_union_type))189return PointerIntPair<DeclContext *, 1>(*ContextIter, /* IntVal= */ 1);190191return PointerIntPair<DeclContext *, 1>(*ContextIter);192}193194StringRef195DeclContextTree::getResolvedPath(CompileUnit &CU, unsigned FileNum,196const DWARFDebugLine::LineTable &LineTable) {197std::pair<unsigned, unsigned> Key = {CU.getUniqueID(), FileNum};198199ResolvedPathsMap::const_iterator It = ResolvedPaths.find(Key);200if (It == ResolvedPaths.end()) {201std::string FileName;202bool FoundFileName = LineTable.getFileNameByIndex(203FileNum, CU.getOrigUnit().getCompilationDir(),204DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName);205(void)FoundFileName;206assert(FoundFileName && "Must get file name from line table");207208// Second level of caching, this time based on the file's parent209// path.210StringRef ResolvedPath = PathResolver.resolve(FileName, StringPool);211212It = ResolvedPaths.insert(std::make_pair(Key, ResolvedPath)).first;213}214215return It->second;216}217218} // namespace llvm219220221