Skip to content

Commit 2859d33

Browse files
mstorsjollvmbot
authored andcommitted
[clang] Don't add DWARF debug info when assembling .s with clang-cl /Z7 (#106686)
This fixes a regression from f58330c. 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, #101710. (cherry picked from commit fcb7b39)
1 parent 0c64156 commit 2859d33

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8561,6 +8561,32 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
85618561
WantDebug = !A->getOption().matches(options::OPT_g0) &&
85628562
!A->getOption().matches(options::OPT_ggdb0);
85638563

8564+
// If a -gdwarf argument appeared, remember it.
8565+
bool EmitDwarf = false;
8566+
if (const Arg *A = getDwarfNArg(Args))
8567+
EmitDwarf = checkDebugInfoOption(A, Args, D, getToolChain());
8568+
8569+
bool EmitCodeView = false;
8570+
if (const Arg *A = Args.getLastArg(options::OPT_gcodeview))
8571+
EmitCodeView = checkDebugInfoOption(A, Args, D, getToolChain());
8572+
8573+
// If the user asked for debug info but did not explicitly specify -gcodeview
8574+
// or -gdwarf, ask the toolchain for the default format.
8575+
if (!EmitCodeView && !EmitDwarf && WantDebug) {
8576+
switch (getToolChain().getDefaultDebugFormat()) {
8577+
case llvm::codegenoptions::DIF_CodeView:
8578+
EmitCodeView = true;
8579+
break;
8580+
case llvm::codegenoptions::DIF_DWARF:
8581+
EmitDwarf = true;
8582+
break;
8583+
}
8584+
}
8585+
8586+
// If the arguments don't imply DWARF, don't emit any debug info here.
8587+
if (!EmitDwarf)
8588+
WantDebug = false;
8589+
85648590
llvm::codegenoptions::DebugInfoKind DebugInfoKind =
85658591
llvm::codegenoptions::NoDebugInfo;
85668592

clang/test/Driver/debug-options-as.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,27 @@
1919
// GGDB0-NOT: -debug-info-kind=
2020

2121
// Check to make sure clang with -g on a .s file gets passed.
22-
// RUN: %clang -### -c -integrated-as -g -x assembler %s 2>&1 \
22+
// This requires a target that defaults to DWARF.
23+
// RUN: %clang -### --target=x86_64-linux-gnu -c -integrated-as -g -x assembler %s 2>&1 \
2324
// RUN: | FileCheck %s
2425
//
2526
// CHECK: "-cc1as"
2627
// CHECK: "-debug-info-kind=constructor"
2728

29+
// Check that a plain -g, without any -gdwarf, for a MSVC target, doesn't
30+
// trigger producing DWARF output.
31+
// RUN: %clang -### --target=x86_64-windows-msvc -c -integrated-as -g -x assembler %s 2>&1 \
32+
// RUN: | FileCheck -check-prefix=MSVC %s
33+
//
34+
// MSVC: "-cc1as"
35+
// MSVC-NOT: "-debug-info-kind=constructor"
36+
37+
// Check that clang-cl with the -Z7 option works the same, not triggering
38+
// any DWARF output.
39+
//
40+
// RUN: %clang_cl -### -c -Z7 -x assembler %s 2>&1 \
41+
// RUN: | FileCheck -check-prefix=MSVC %s
42+
2843
// Check to make sure clang with -g on a .s file gets passed -dwarf-debug-producer.
2944
// RUN: %clang -### -c -integrated-as -g -x assembler %s 2>&1 \
3045
// RUN: | FileCheck -check-prefix=P %s

0 commit comments

Comments
 (0)