[llvm-branch-commits] [clang] release/19.x: [clang] Don't add DWARF debug info when assembling .s with clang-cl /Z7 (#106686) (PR #107146)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Sep 3 13:58:33 PDT 2024


Martin =?utf-8?q?Storsjö?= <martin at martin.st>,
Martin =?utf-8?q?Storsjö?= <martin at martin.st>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/107146 at github.com>


https://github.com/llvmbot updated https://github.com/llvm/llvm-project/pull/107146

>From 416125130acdec945b91f2db08a80459eca58fea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin at martin.st>
Date: Tue, 3 Sep 2024 22:45:54 +0300
Subject: [PATCH 1/3] [clang] Don't add DWARF debug info when assembling .s
 with clang-cl /Z7 (#106686)

This fixes a regression from f58330cbe44598eb2de0cca3b812f67fea0a71ca.

That commit changed the clang-cl options /Zi and /Z7 to be implemented
as aliases of -g rather than having separate handling.

This had the unintended effect, that when assembling .s files with
clang-cl, the /Z7 option (which implies using CodeView debug info) was
treated as a -g option, which causes `ClangAs::ConstructJob` to pick up
the option as part of `Args.getLastArg(options::OPT_g_Group)`, which
sets the `WantDebug` variable.

Within `Clang::ConstructJob`, we check for whether explicit `-gdwarf` or
`-gcodeview` options have been set, and if not, we pick the default
debug format for the current toolchain. However, in `ClangAs`, if debug
info has been enabled, it always adds DWARF debug info.

Add similar logic in `ClangAs` - check if the user has explicitly
requested either DWARF or CodeView, otherwise look up the toolchain
default. If we (either implicitly or explicitly) should be producing
CodeView, don't enable the default `ClangAs` DWARF generation.

This fixes the issue, where assembling a single `.s` file with clang-cl,
with the /Z7 option, causes the file to contain some DWARF sections.
This causes the output executable to contain DWARF, in addition to the
separate intended main PDB file.

By having the output executable contain DWARF sections, LLDB only looks
at the (very little) DWARF info in the executable, rather than looking
for a separate standalone PDB file. This caused an issue with LLDB's
tests, https://github.com/llvm/llvm-project/issues/101710.

(cherry picked from commit fcb7b390ccd5b4cfc71f13b5e16a846f3f400c10)
---
 clang/lib/Driver/ToolChains/Clang.cpp | 26 ++++++++++++++++++++++++++
 clang/test/Driver/debug-options-as.c  | 17 ++++++++++++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 366b147a052bf2..8858c318aba7a1 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -8561,6 +8561,32 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
     WantDebug = !A->getOption().matches(options::OPT_g0) &&
                 !A->getOption().matches(options::OPT_ggdb0);
 
+  // If a -gdwarf argument appeared, remember it.
+  bool EmitDwarf = false;
+  if (const Arg *A = getDwarfNArg(Args))
+    EmitDwarf = checkDebugInfoOption(A, Args, D, getToolChain());
+
+  bool EmitCodeView = false;
+  if (const Arg *A = Args.getLastArg(options::OPT_gcodeview))
+    EmitCodeView = checkDebugInfoOption(A, Args, D, getToolChain());
+
+  // If the user asked for debug info but did not explicitly specify -gcodeview
+  // or -gdwarf, ask the toolchain for the default format.
+  if (!EmitCodeView && !EmitDwarf && WantDebug) {
+    switch (getToolChain().getDefaultDebugFormat()) {
+    case llvm::codegenoptions::DIF_CodeView:
+      EmitCodeView = true;
+      break;
+    case llvm::codegenoptions::DIF_DWARF:
+      EmitDwarf = true;
+      break;
+    }
+  }
+
+  // If the arguments don't imply DWARF, don't emit any debug info here.
+  if (!EmitDwarf)
+    WantDebug = false;
+
   llvm::codegenoptions::DebugInfoKind DebugInfoKind =
       llvm::codegenoptions::NoDebugInfo;
 
diff --git a/clang/test/Driver/debug-options-as.c b/clang/test/Driver/debug-options-as.c
index c83c0cb90431d3..3e1ae109711003 100644
--- a/clang/test/Driver/debug-options-as.c
+++ b/clang/test/Driver/debug-options-as.c
@@ -19,12 +19,27 @@
 // GGDB0-NOT: -debug-info-kind=
 
 // Check to make sure clang with -g on a .s file gets passed.
-// RUN: %clang -### -c -integrated-as -g -x assembler %s 2>&1 \
+// This requires a target that defaults to DWARF.
+// RUN: %clang -### --target=x86_64-linux-gnu -c -integrated-as -g -x assembler %s 2>&1 \
 // RUN:   | FileCheck %s
 //
 // CHECK: "-cc1as"
 // CHECK: "-debug-info-kind=constructor"
 
+// Check that a plain -g, without any -gdwarf, for a MSVC target, doesn't
+// trigger producing DWARF output.
+// RUN: %clang -### --target=x86_64-windows-msvc -c -integrated-as -g -x assembler %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=MSVC %s
+//
+// MSVC: "-cc1as"
+// MSVC-NOT: "-debug-info-kind=constructor"
+
+// Check that clang-cl with the -Z7 option works the same, not triggering
+// any DWARF output.
+//
+// RUN: %clang_cl -### -c -Z7 -x assembler %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=MSVC %s
+
 // Check to make sure clang with -g on a .s file gets passed -dwarf-debug-producer.
 // RUN: %clang -### -c -integrated-as -g -x assembler %s 2>&1 \
 // RUN:   | FileCheck -check-prefix=P %s

>From 8a51aef71485b7b57550369737177b5fcf01e569 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin at martin.st>
Date: Tue, 3 Sep 2024 23:13:25 +0300
Subject: [PATCH 2/3] [clang] [test] Fix the debug-options-as.c test on macOS

Separate the path, which may begin with e.g. /Users, with "--" from
the other options, to make it clear that it is a path, not an
option.

This fixes a test from fcb7b390ccd5b4cfc71f13b5e16a846f3f400c10.

(cherry picked from commit 70f3511adaea4d3a9f8fadb23e84f518cc0654ab)
---
 clang/test/Driver/debug-options-as.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Driver/debug-options-as.c b/clang/test/Driver/debug-options-as.c
index 3e1ae109711003..f24cea330efe45 100644
--- a/clang/test/Driver/debug-options-as.c
+++ b/clang/test/Driver/debug-options-as.c
@@ -37,7 +37,7 @@
 // Check that clang-cl with the -Z7 option works the same, not triggering
 // any DWARF output.
 //
-// RUN: %clang_cl -### -c -Z7 -x assembler %s 2>&1 \
+// RUN: %clang_cl -### -c -Z7 -x assembler -- %s 2>&1 \
 // RUN:   | FileCheck -check-prefix=MSVC %s
 
 // Check to make sure clang with -g on a .s file gets passed -dwarf-debug-producer.

>From f7705a8c2b38b5003e9be48cdb7bce6ed0f42cea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin at martin.st>
Date: Tue, 3 Sep 2024 23:50:09 +0300
Subject: [PATCH 3/3] [clang] [test] Fix the debug-options-as.c test on PowerPC

Use an explicit MSVC triple with an architecture that does
have proper handling for MSVC style targets.

This fixes a test from fcb7b390ccd5b4cfc71f13b5e16a846f3f400c10.

(cherry picked from commit cbb5f03f5042aa6d7c5d17963eba192861c9165c)
---
 clang/test/Driver/debug-options-as.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Driver/debug-options-as.c b/clang/test/Driver/debug-options-as.c
index f24cea330efe45..cb0492177ff47a 100644
--- a/clang/test/Driver/debug-options-as.c
+++ b/clang/test/Driver/debug-options-as.c
@@ -37,7 +37,7 @@
 // Check that clang-cl with the -Z7 option works the same, not triggering
 // any DWARF output.
 //
-// RUN: %clang_cl -### -c -Z7 -x assembler -- %s 2>&1 \
+// RUN: %clang_cl -### --target=x86_64-pc-windows-msvc -c -Z7 -x assembler -- %s 2>&1 \
 // RUN:   | FileCheck -check-prefix=MSVC %s
 
 // Check to make sure clang with -g on a .s file gets passed -dwarf-debug-producer.



More information about the llvm-branch-commits mailing list