#ifndef LLVM_DWARFLINKER_DWARFLINKERDECLCONTEXT_H
#define LLVM_DWARFLINKER_DWARFLINKERDECLCONTEXT_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/CodeGen/NonRelocatableStringpool.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
#include "llvm/DebugInfo/DWARF/DWARFDie.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include <atomic>
namespace llvm {
class CompileUnit;
struct DeclMapInfo;
class CachedPathResolver {
public:
StringRef resolve(const std::string &Path,
NonRelocatableStringpool &StringPool) {
StringRef FileName = sys::path::filename(Path);
StringRef ParentPath = sys::path::parent_path(Path);
if (!ResolvedPaths.count(ParentPath)) {
SmallString<256> RealPath;
sys::fs::real_path(ParentPath, RealPath);
ResolvedPaths.insert(
{ParentPath, std::string(RealPath.c_str(), RealPath.size())});
}
SmallString<256> ResolvedPath(ResolvedPaths[ParentPath]);
sys::path::append(ResolvedPath, FileName);
return StringPool.internString(ResolvedPath);
}
private:
StringMap<std::string> ResolvedPaths;
};
class DeclContext {
public:
using Map = DenseSet<DeclContext *, DeclMapInfo>;
DeclContext() : DefinedInClangModule(0), Parent(*this) {}
DeclContext(unsigned Hash, uint32_t Line, uint32_t ByteSize, uint16_t Tag,
StringRef Name, StringRef File, const DeclContext &Parent,
DWARFDie LastSeenDIE = DWARFDie(), unsigned CUId = 0)
: QualifiedNameHash(Hash), Line(Line), ByteSize(ByteSize), Tag(Tag),
DefinedInClangModule(0), Name(Name), File(File), Parent(Parent),
LastSeenDIE(LastSeenDIE), LastSeenCompileUnitID(CUId) {}
uint32_t getQualifiedNameHash() const { return QualifiedNameHash; }
bool setLastSeenDIE(CompileUnit &U, const DWARFDie &Die);
void setHasCanonicalDIE() { HasCanonicalDIE = true; }
bool hasCanonicalDIE() const { return HasCanonicalDIE; }
uint32_t getCanonicalDIEOffset() const { return CanonicalDIEOffset; }
void setCanonicalDIEOffset(uint32_t Offset) { CanonicalDIEOffset = Offset; }
bool isDefinedInClangModule() const { return DefinedInClangModule; }
void setDefinedInClangModule(bool Val) { DefinedInClangModule = Val; }
uint16_t getTag() const { return Tag; }
private:
friend DeclMapInfo;
unsigned QualifiedNameHash = 0;
uint32_t Line = 0;
uint32_t ByteSize = 0;
uint16_t Tag = dwarf::DW_TAG_compile_unit;
unsigned DefinedInClangModule : 1;
StringRef Name;
StringRef File;
const DeclContext &Parent;
DWARFDie LastSeenDIE;
uint32_t LastSeenCompileUnitID = 0;
std::atomic<uint32_t> CanonicalDIEOffset = {0};
bool HasCanonicalDIE = false;
};
class DeclContextTree {
public:
PointerIntPair<DeclContext *, 1> getChildDeclContext(DeclContext &Context,
const DWARFDie &DIE,
CompileUnit &Unit,
bool InClangModule);
DeclContext &getRoot() { return Root; }
private:
BumpPtrAllocator Allocator;
DeclContext Root;
DeclContext::Map Contexts;
using ResolvedPathsMap = DenseMap<std::pair<unsigned, unsigned>, StringRef>;
ResolvedPathsMap ResolvedPaths;
CachedPathResolver PathResolver;
NonRelocatableStringpool StringPool;
StringRef getResolvedPath(CompileUnit &CU, unsigned FileNum,
const DWARFDebugLine::LineTable &LineTable);
};
struct DeclMapInfo : private DenseMapInfo<DeclContext *> {
using DenseMapInfo<DeclContext *>::getEmptyKey;
using DenseMapInfo<DeclContext *>::getTombstoneKey;
static unsigned getHashValue(const DeclContext *Ctxt) {
return Ctxt->QualifiedNameHash;
}
static bool isEqual(const DeclContext *LHS, const DeclContext *RHS) {
if (RHS == getEmptyKey() || RHS == getTombstoneKey())
return RHS == LHS;
return LHS->QualifiedNameHash == RHS->QualifiedNameHash &&
LHS->Line == RHS->Line && LHS->ByteSize == RHS->ByteSize &&
LHS->Name.data() == RHS->Name.data() &&
LHS->File.data() == RHS->File.data() &&
LHS->Parent.QualifiedNameHash == RHS->Parent.QualifiedNameHash;
}
};
}
#endif