Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions driver/cl_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,32 @@ cl::opt<bool> emitDwarfDebugInfo(
"gdwarf", cl::ZeroOrMore,
cl::desc("Emit DWARF debuginfo (instead of CodeView) for MSVC targets"));

// Prefix map for filenames in DWARF debuginfo
std::map<std::string, std::string> debugPrefixMap;

struct DwarfPrefixParser : public cl::parser<std::string> {
explicit DwarfPrefixParser(cl::Option &O) : cl::parser<std::string>(O) {}

bool parse(cl::Option &O, llvm::StringRef /*ArgName*/, llvm::StringRef Arg,
std::string & /*Val*/) {
auto [from, to] = Arg.split('=');
if (from.empty() || to.empty()) {
return O.error("invalid debug prefix map: " + Arg);
}
auto fromStr = std::string(from);
if (debugPrefixMap.find(fromStr) != debugPrefixMap.end()) {
return O.error("debug prefix map already contains: " + fromStr);
}
debugPrefixMap[fromStr] = std::string(to);
return false;
}
};

static cl::opt<std::string, false, DwarfPrefixParser> fdebugPrefixMap(
"fdebug-prefix-map", cl::ZeroOrMore,
cl::desc("Prefix map for filenames in DWARF debuginfo"),
cl::value_desc("<old>=<new>"));

cl::opt<bool> noAsm("noasm", cl::desc("Disallow use of inline assembler"),
cl::ZeroOrMore);

Expand Down
3 changes: 3 additions & 0 deletions driver/cl_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/CommandLine.h"
#include <map>
#include <deque>
#include <vector>

Expand Down Expand Up @@ -149,4 +150,6 @@ extern cl::opt<bool> dynamicCompileTlsWorkaround;
#else
constexpr bool enableDynamicCompile = false;
#endif
// Prefix map for filenames in DWARF debuginfo
extern std::map<std::string, std::string> debugPrefixMap;
}
15 changes: 12 additions & 3 deletions gen/dibuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ void DIBuilder::SetValue(Loc loc, llvm::Value *value,
IR->scopebb());
}

std::string DIBuilder::remapDIPath(llvm::StringRef path) {
for (const auto &[from, to] : opts::debugPrefixMap) {
if (path.starts_with(from)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

starts_with looks dangerous, what if I map "some/path="other/thing", does that mean that "some/path_more" also matches and is remapped?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(could be checked in a test case)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but this is what clang does. I guess in the most (only?) use cases this is going to be some "root" path, so we don't expect sources from root_some to be passed to the compiler. Cautious people can do some/path/=other/thing/. It's easy to enforce this in the code, but I don't want to diverge from clang...

return to + path.substr(from.size()).str();
}
}
return std::string(path);
}

DIFile DIBuilder::CreateFile(const char *filename) {
if (!filename)
filename = IR->dmodule->srcfile.toChars();
Expand All @@ -231,14 +240,14 @@ DIFile DIBuilder::CreateFile(const char *filename) {
// ...)

if (llvm::sys::path::is_absolute(filename)) {
return DBuilder.createFile(llvm::sys::path::relative_path(filename),
llvm::sys::path::root_path(filename));
return DBuilder.createFile(remapDIPath(llvm::sys::path::relative_path(filename)),
remapDIPath(llvm::sys::path::root_path(filename)));
}

llvm::SmallString<128> cwd;
llvm::sys::fs::current_path(cwd);

return DBuilder.createFile(filename, cwd);
return DBuilder.createFile(remapDIPath(filename), remapDIPath(cwd));
}

DIFile DIBuilder::CreateFile(Loc loc) {
Expand Down
1 change: 1 addition & 0 deletions gen/dibuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class DIBuilder {
llvm::SmallVector<llvm::Metadata *, 16> &elems);
void AddStaticMembers(AggregateDeclaration *sd, ldc::DIFile file,
llvm::SmallVector<llvm::Metadata *, 16> &elems);
std::string remapDIPath(llvm::StringRef path);
DIFile CreateFile(const char *filename = nullptr);
DIFile CreateFile(Loc loc);
DIFile CreateFile(Dsymbol *decl);
Expand Down
Loading