Skip to content

Commit 376dc8c

Browse files
mstorsjoweliveindetail
authored andcommitted
[clang] Don't add DWARF debug info when assembling .s with clang-cl /Z7 (llvm#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, llvm#101710.
1 parent 9f4b3a3 commit 376dc8c

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

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

8427+
// If a -gdwarf argument appeared, remember it.
8428+
bool EmitDwarf = false;
8429+
if (const Arg *A = getDwarfNArg(Args))
8430+
EmitDwarf = checkDebugInfoOption(A, Args, D, getToolChain());
8431+
8432+
bool EmitCodeView = false;
8433+
if (const Arg *A = Args.getLastArg(options::OPT_gcodeview))
8434+
EmitCodeView = checkDebugInfoOption(A, Args, D, getToolChain());
8435+
8436+
// If the user asked for debug info but did not explicitly specify -gcodeview
8437+
// or -gdwarf, ask the toolchain for the default format.
8438+
if (!EmitCodeView && !EmitDwarf && WantDebug) {
8439+
switch (getToolChain().getDefaultDebugFormat()) {
8440+
case llvm::codegenoptions::DIF_CodeView:
8441+
EmitCodeView = true;
8442+
break;
8443+
case llvm::codegenoptions::DIF_DWARF:
8444+
EmitDwarf = true;
8445+
break;
8446+
}
8447+
}
8448+
8449+
// If the arguments don't imply DWARF, don't emit any debug info here.
8450+
if (!EmitDwarf)
8451+
WantDebug = false;
8452+
84278453
llvm::codegenoptions::DebugInfoKind DebugInfoKind =
84288454
llvm::codegenoptions::NoDebugInfo;
84298455

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,27 @@
2020
// GGDB0-NOT: -debug-info-kind=
2121

2222
// Check to make sure clang with -g on a .s file gets passed.
23-
// rdar://9275556
24-
// RUN: %clang -### -c -integrated-as -g -x assembler %s 2>&1 \
23+
// This requires a target that defaults to DWARF.
24+
// RUN: %clang -### --target=x86_64-linux-gnu -c -integrated-as -g -x assembler %s 2>&1 \
2525
// RUN: | FileCheck %s
2626
//
2727
// CHECK: "-cc1as"
2828
// CHECK: "-debug-info-kind=constructor"
2929

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

0 commit comments

Comments
 (0)