[clang] 9d9265d - [clang-cl] Omit command line and workdir in CIFF CV debug info for reproducible builds. (#205736)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 19:11:08 PST 2026
Author: Vladimir Vereschaka
Date: 2026-11-05T19:11:03-08:00
New Revision: 9d9265d87edb03da3a6867cb5d53a7bf24f675f1
URL: https://github.com/llvm/llvm-project/commit/9d9265d87edb03da3a6867cb5d53a7bf24f675f1
DIFF: https://github.com/llvm/llvm-project/commit/9d9265d87edb03da3a6867cb5d53a7bf24f675f1.diff
LOG: [clang-cl] Omit command line and workdir in CIFF CV debug info for reproducible builds. (#205736)
The command line can contain the arguments, such as the pathes or /pathmap options,
which could break a reproducible output for the object files. In addition to
`-gcodeview-commandline` option that drops the command line from the build info,
this patch adds a new `-gcodeview-cwd` option to exclude the current working
directory from the build info of the CodeView debug info sections.
Also it links `-gno-codeview-cwd` and `-gno-codeview-commandline` options with
CL's `/Brepro` option to match (partly) compiler behaviour with MSVC.
Added:
Modified:
clang/include/clang/Basic/DebugOptions.def
clang/include/clang/Options/Options.td
clang/lib/CodeGen/CGDebugInfo.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/test/DebugInfo/Generic/codeview-buildinfo.c
clang/test/Driver/cl-options.c
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DebugOptions.def b/clang/include/clang/Basic/DebugOptions.def
index 480f1f41fad08..87d9d611b6a39 100644
--- a/clang/include/clang/Basic/DebugOptions.def
+++ b/clang/include/clang/Basic/DebugOptions.def
@@ -125,6 +125,9 @@ DEBUGOPT(CodeViewGHash, 1, 0, Compatible)
/// Whether to emit the compiler path and command line into the CodeView debug information.
DEBUGOPT(CodeViewCommandLine, 1, 0, Compatible)
+/// Whether to emit the current working directory into the CodeView debug information.
+DEBUGOPT(CodeViewEmitCurrentWorkdir, 1, 0, Compatible)
+
/// Whether emit extra debug info for sample pgo profile collection.
DEBUGOPT(DebugInfoForProfiling, 1, 0, Compatible)
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index d9abc9b6e37d0..a529d4ec34dc1 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -5353,6 +5353,12 @@ defm codeview_command_line : BoolOption<"g", "codeview-command-line",
NegFlag<SetFalse, [], [ClangOption], "Don't emit compiler path and command line into CodeView debug information">,
BothFlags<[], [ClangOption, CLOption, DXCOption, CC1Option]>>,
Group<g_flags_Group>;
+defm codeview_cwd : BoolOption<"g", "codeview-cwd",
+ CodeGenOpts<"CodeViewEmitCurrentWorkdir">, DefaultTrue,
+ PosFlag<SetTrue, [], [ClangOption], "Emit current working directory into CodeView debug information">,
+ NegFlag<SetFalse, [], [ClangOption], "Don't emit current working directory into CodeView debug information">,
+ BothFlags<[], [ClangOption, CLOption, DXCOption, CC1Option]>>,
+ Group<g_flags_Group>;
defm inline_line_tables : BoolGOption<"inline-line-tables",
CodeGenOpts<"NoInlineLineTables">, DefaultFalse,
NegFlag<SetTrue, [], [ClangOption, CC1Option],
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 436a949891793..63cf4110d9d0a 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -858,9 +858,14 @@ void CGDebugInfo::CreateCompileUnit() {
// file was specified with an absolute path.
if (CSKind)
CSInfo.emplace(*CSKind, Checksum);
- llvm::DIFile *CUFile = DBuilder.createFile(
- remapDIPath(MainFileName), remapDIPath(getCurrentDirname()), CSInfo,
- getSource(SM, SM.getMainFileID()));
+ const llvm::StringRef cwd =
+ !CGOpts.EmitCodeView ||
+ (CGOpts.EmitCodeView && CGOpts.CodeViewEmitCurrentWorkdir)
+ ? getCurrentDirname()
+ : llvm::StringRef("");
+ llvm::DIFile *CUFile =
+ DBuilder.createFile(remapDIPath(MainFileName), remapDIPath(cwd), CSInfo,
+ getSource(SM, SM.getMainFileID()));
StringRef Sysroot, SDK;
if (CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::LLDB) {
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index d8819c952e5ad..df8c9ccb1027b 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9234,6 +9234,9 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
// This option does not allow the user redefinitions for these macros.
if (!HasNoDateTime)
CmdArgs.push_back("-init-datetime-macros=literalone");
+ // Omit tool, current workdir and command line in COFF CV debug section.
+ CmdArgs.push_back("-gno-codeview-command-line");
+ CmdArgs.push_back("-gno-codeview-cwd");
}
}
diff --git a/clang/test/DebugInfo/Generic/codeview-buildinfo.c b/clang/test/DebugInfo/Generic/codeview-buildinfo.c
index 36e8663b799f7..162db800acd7b 100644
--- a/clang/test/DebugInfo/Generic/codeview-buildinfo.c
+++ b/clang/test/DebugInfo/Generic/codeview-buildinfo.c
@@ -31,6 +31,9 @@
// RUN: /pathmap:%t.pathmap=x:/path-to /Fo:%{s:basename}.obj -- %{s:basename}
// RUN: llvm-pdbutil dump --symbols %{s:basename}.obj | FileCheck %s --check-prefix OBJNAME-PATHMAP
+// RUN: %clang_cl --target=i686-windows-msvc /Brepro /c /Z7 /Fo%t.obj -- %s
+// RUN: llvm-pdbutil dump --types %t.obj | FileCheck %s --check-prefix BREPRO
+
int main(void) { return 42; }
// CHECK: Types (.debug$T)
@@ -87,3 +90,10 @@ int main(void) { return 42; }
// RELPATH-SAME: `
// OBJNAME-PATHMAP: {{^.+}} | S_OBJNAME [size = {{.+}}] sig=0, `x:/path-to{{[\\/]}}codeview-buildinfo.c.obj`
+
+// BREPRO: 0x{{.+}} | LF_BUILDINFO [size = {{.+}}]
+// BREPRO-NEXT: 0x{{.*}}: ``
+// BREPRO-NEXT: 0x{{.*}}: ``
+// BREPRO-NEXT: 0x{{.*}}: `{{.+[\\/]codeview-buildinfo\.c}}`
+// BREPRO-NEXT: 0x{{.*}}: ``
+// BREPRO-NEXT: 0x{{.*}}: ``
diff --git a/clang/test/Driver/cl-options.c b/clang/test/Driver/cl-options.c
index 536b93890ef71..a412ecd76ab41 100644
--- a/clang/test/Driver/cl-options.c
+++ b/clang/test/Driver/cl-options.c
@@ -594,6 +594,8 @@
// RUN: %clang_cl /Brepro- /Brepro /c '-###' -- %s 2>&1 | FileCheck -check-prefix=Brepro %s
// Brepro: "-mno-incremental-linker-compatible"
+// Brepro: "-gno-codeview-command-line"
+// Brepro: "-gno-codeview-cwd"
// RUN: %clang_cl /Brepro /Brepro- /c '-###' -- %s 2>&1 | FileCheck -check-prefix=Brepro_ %s
// Brepro_: "-mincremental-linker-compatible"
More information about the cfe-commits
mailing list