Skip to content

Commit 9edb6d4

Browse files
committed
dibuilder: use debugPrefixMap to remap source locs
This is required to have reproducible builds without remapping the source code directory to a fixed location (for example in Bazel, where the source code for every build action will end up in a sanbox directory with some hash in the path name). Try to keep the implementation as close as possible to what clang does.
1 parent aad25b3 commit 9edb6d4

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

gen/dibuilder.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ void DIBuilder::SetValue(Loc loc, llvm::Value *value,
218218
IR->scopebb());
219219
}
220220

221+
std::string DIBuilder::remapDIPath(llvm::StringRef path) {
222+
for (const auto &[from, to] : opts::debugPrefixMap) {
223+
if (path.starts_with(from)) {
224+
return to + path.substr(from.size()).str();
225+
}
226+
}
227+
return std::string(path);
228+
}
229+
221230
DIFile DIBuilder::CreateFile(const char *filename) {
222231
if (!filename)
223232
filename = IR->dmodule->srcfile.toChars();
@@ -231,14 +240,14 @@ DIFile DIBuilder::CreateFile(const char *filename) {
231240
// ...)
232241

233242
if (llvm::sys::path::is_absolute(filename)) {
234-
return DBuilder.createFile(llvm::sys::path::relative_path(filename),
235-
llvm::sys::path::root_path(filename));
243+
return DBuilder.createFile(remapDIPath(llvm::sys::path::relative_path(filename)),
244+
remapDIPath(llvm::sys::path::root_path(filename)));
236245
}
237246

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

241-
return DBuilder.createFile(filename, cwd);
250+
return DBuilder.createFile(remapDIPath(filename), remapDIPath(cwd));
242251
}
243252

244253
DIFile DIBuilder::CreateFile(Loc loc) {

gen/dibuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class DIBuilder {
163163
llvm::SmallVector<llvm::Metadata *, 16> &elems);
164164
void AddStaticMembers(AggregateDeclaration *sd, ldc::DIFile file,
165165
llvm::SmallVector<llvm::Metadata *, 16> &elems);
166+
std::string remapDIPath(llvm::StringRef path);
166167
DIFile CreateFile(const char *filename = nullptr);
167168
DIFile CreateFile(Loc loc);
168169
DIFile CreateFile(Dsymbol *decl);

0 commit comments

Comments
 (0)