#include "llvm/DWARFLinker/DWARFLinkerDeclContext.h"
#include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/DebugInfo/DWARF/DWARFDie.h"
#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
namespace llvm {
bool DeclContext::setLastSeenDIE(CompileUnit &U, const DWARFDie &Die) {
if (LastSeenCompileUnitID == U.getUniqueID()) {
DWARFUnit &OrigUnit = U.getOrigUnit();
uint32_t FirstIdx = OrigUnit.getDIEIndex(LastSeenDIE);
U.getInfo(FirstIdx).Ctxt = nullptr;
return false;
}
LastSeenCompileUnitID = U.getUniqueID();
LastSeenDIE = Die;
return true;
}
PointerIntPair<DeclContext *, 1>
DeclContextTree::getChildDeclContext(DeclContext &Context, const DWARFDie &DIE,
CompileUnit &U, bool InClangModule) {
unsigned Tag = DIE.getTag();
switch (Tag) {
default:
return PointerIntPair<DeclContext *, 1>(nullptr);
case dwarf::DW_TAG_module:
break;
case dwarf::DW_TAG_compile_unit:
return PointerIntPair<DeclContext *, 1>(&Context);
case dwarf::DW_TAG_subprogram:
if ((Context.getTag() == dwarf::DW_TAG_namespace ||
Context.getTag() == dwarf::DW_TAG_compile_unit) &&
!dwarf::toUnsigned(DIE.find(dwarf::DW_AT_external), 0))
return PointerIntPair<DeclContext *, 1>(nullptr);
LLVM_FALLTHROUGH;
case dwarf::DW_TAG_member:
case dwarf::DW_TAG_namespace:
case dwarf::DW_TAG_structure_type:
case dwarf::DW_TAG_class_type:
case dwarf::DW_TAG_union_type:
case dwarf::DW_TAG_enumeration_type:
case dwarf::DW_TAG_typedef:
if (dwarf::toUnsigned(DIE.find(dwarf::DW_AT_artificial), 0))
return PointerIntPair<DeclContext *, 1>(nullptr);
break;
}
StringRef NameRef;
StringRef FileRef;
if (const char *LinkageName = DIE.getLinkageName())
NameRef = StringPool.internString(LinkageName);
else if (const char *ShortName = DIE.getShortName())
NameRef = StringPool.internString(ShortName);
bool IsAnonymousNamespace = NameRef.empty() && Tag == dwarf::DW_TAG_namespace;
if (IsAnonymousNamespace) {
NameRef = "(anonymous namespace)";
}
if (Tag != dwarf::DW_TAG_class_type && Tag != dwarf::DW_TAG_structure_type &&
Tag != dwarf::DW_TAG_union_type &&
Tag != dwarf::DW_TAG_enumeration_type && NameRef.empty())
return PointerIntPair<DeclContext *, 1>(nullptr);
unsigned Line = 0;
unsigned ByteSize = std::numeric_limits<uint32_t>::max();
if (!InClangModule) {
ByteSize = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_byte_size),
std::numeric_limits<uint64_t>::max());
if (Tag != dwarf::DW_TAG_namespace || IsAnonymousNamespace) {
if (unsigned FileNum =
dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_file), 0)) {
if (const auto *LT = U.getOrigUnit().getContext().getLineTableForUnit(
&U.getOrigUnit())) {
if (IsAnonymousNamespace)
FileNum = 1;
if (LT->hasFileAtIndex(FileNum)) {
Line = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_line), 0);
FileRef = getResolvedPath(U, FileNum, *LT);
}
}
}
}
}
if (!Line && NameRef.empty())
return PointerIntPair<DeclContext *, 1>(nullptr);
unsigned Hash = hash_combine(Context.getQualifiedNameHash(), Tag, NameRef);
if (IsAnonymousNamespace)
Hash = hash_combine(Hash, FileRef);
DeclContext Key(Hash, Line, ByteSize, Tag, NameRef, FileRef, Context);
auto ContextIter = Contexts.find(&Key);
if (ContextIter == Contexts.end()) {
bool Inserted;
DeclContext *NewContext =
new (Allocator) DeclContext(Hash, Line, ByteSize, Tag, NameRef, FileRef,
Context, DIE, U.getUniqueID());
std::tie(ContextIter, Inserted) = Contexts.insert(NewContext);
assert(Inserted && "Failed to insert DeclContext");
(void)Inserted;
} else if (Tag != dwarf::DW_TAG_namespace &&
!(*ContextIter)->setLastSeenDIE(U, DIE)) {
return PointerIntPair<DeclContext *, 1>(*ContextIter, 1);
}
assert(ContextIter != Contexts.end());
if ((Tag == dwarf::DW_TAG_subprogram &&
Context.getTag() != dwarf::DW_TAG_structure_type &&
Context.getTag() != dwarf::DW_TAG_class_type) ||
(Tag == dwarf::DW_TAG_union_type))
return PointerIntPair<DeclContext *, 1>(*ContextIter, 1);
return PointerIntPair<DeclContext *, 1>(*ContextIter);
}
StringRef
DeclContextTree::getResolvedPath(CompileUnit &CU, unsigned FileNum,
const DWARFDebugLine::LineTable &LineTable) {
std::pair<unsigned, unsigned> Key = {CU.getUniqueID(), FileNum};
ResolvedPathsMap::const_iterator It = ResolvedPaths.find(Key);
if (It == ResolvedPaths.end()) {
std::string FileName;
bool FoundFileName = LineTable.getFileNameByIndex(
FileNum, CU.getOrigUnit().getCompilationDir(),
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName);
(void)FoundFileName;
assert(FoundFileName && "Must get file name from line table");
StringRef ResolvedPath = PathResolver.resolve(FileName, StringPool);
It = ResolvedPaths.insert(std::make_pair(Key, ResolvedPath)).first;
}
return It->second;
}
}