[clang] [DebugInfo][CodeView] Emit path prefix substitution for COFF object file name. (PR #205729)

Vladimir Vereschaka via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 24 23:22:17 PDT 2026


https://github.com/vvereschaka created https://github.com/llvm/llvm-project/pull/205729

Remap a file path for the object file name in CodeView debug info (S_OBJNAME) with the path prefix specified by `fdebug-prefix-map=` option.

>From 112097dac5f0b7e44f88507738f11a2f90b210e1 Mon Sep 17 00:00:00 2001
From: Vladimir Vereschaka <vvereschaka at accesssoftek.com>
Date: Wed, 24 Jun 2026 22:21:40 -0700
Subject: [PATCH] [DebugInfo][CodeView] Emit path prefix substitution for COFF
 object file name.

Remap a file path for the object file name in CodeView debug info (S_OBJNAME)
with the path prefix specified by `fdebug-prefix-map=` option.
---
 clang/include/clang/Basic/CodeGenOptions.h |  4 ++++
 clang/lib/Basic/CodeGenOptions.cpp         | 10 ++++++++++
 clang/lib/CodeGen/BackendUtil.cpp          |  2 +-
 clang/lib/CodeGen/CGDebugInfo.cpp          |  6 +-----
 4 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h
index 97d68877467fd..027ccac08e879 100644
--- a/clang/include/clang/Basic/CodeGenOptions.h
+++ b/clang/include/clang/Basic/CodeGenOptions.h
@@ -700,6 +700,10 @@ class CodeGenOptions : public CodeGenOptionsBase {
     }
     llvm_unreachable("Unknown BoolFromMem enum");
   }
+
+  /// Remap specified path prefix using provided DebugPrefixMap map.
+  /// Returns updated path or unchanged if no substituion was found.
+  std::string remapDebugPathPrefix(StringRef Path) const;
 };
 
 }  // end namespace clang
diff --git a/clang/lib/Basic/CodeGenOptions.cpp b/clang/lib/Basic/CodeGenOptions.cpp
index db8a77cd93cf6..a16a8a126e709 100644
--- a/clang/lib/Basic/CodeGenOptions.cpp
+++ b/clang/lib/Basic/CodeGenOptions.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Basic/CodeGenOptions.h"
+#include "llvm/Support/Path.h"
 
 namespace clang {
 
@@ -50,4 +51,13 @@ void CodeGenOptions::resetNonModularOptions(StringRef ModuleFormat) {
   RelocationModel = llvm::Reloc::PIC_;
 }
 
+std::string CodeGenOptions::remapDebugPathPrefix(StringRef Path) const {
+  SmallString<0> P = Path;
+
+  for (auto &[From, To] : llvm::reverse(DebugPrefixMap))
+    if (llvm::sys::path::replace_path_prefix(P, From, To))
+      break;
+  return P.str().str();
+}
+
 }  // end namespace clang
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index a46a25c4492f2..a6730d6a0022e 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -470,7 +470,7 @@ static bool initTargetOptions(const CompilerInstance &CI,
   Options.XRayFunctionIndex = CodeGenOpts.XRayFunctionIndex;
   Options.LoopAlignment = CodeGenOpts.LoopAlignment;
   Options.DebugStrictDwarf = CodeGenOpts.DebugStrictDwarf;
-  Options.ObjectFilenameForDebug = CodeGenOpts.ObjectFilenameForDebug;
+  Options.ObjectFilenameForDebug = CodeGenOpts.remapDebugPathPrefix(CodeGenOpts.ObjectFilenameForDebug);
   Options.Hotpatch = CodeGenOpts.HotPatch;
   Options.JMCInstrument = CodeGenOpts.JMCInstrument;
   Options.XCOFFReadOnlyPointers = CodeGenOpts.XCOFFReadOnlyPointers;
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 7421733efcc24..5331d1231d407 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -671,11 +671,7 @@ llvm::DIFile *CGDebugInfo::createFile(
 }
 
 std::string CGDebugInfo::remapDIPath(StringRef Path) const {
-  SmallString<256> P = Path;
-  for (auto &[From, To] : llvm::reverse(CGM.getCodeGenOpts().DebugPrefixMap))
-    if (llvm::sys::path::replace_path_prefix(P, From, To))
-      break;
-  return P.str().str();
+  return CGM.getCodeGenOpts().remapDebugPathPrefix(Path);
 }
 
 unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {



More information about the cfe-commits mailing list