Skip to content

Commit ac59e23

Browse files
committed
[clang] Don't add DWARF debug info when assembling .s with clang-cl /Z7
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 31684c6 commit ac59e23

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -8641,6 +8641,32 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
86418641
WantDebug = !A->getOption().matches(options::OPT_g0) &&
86428642
!A->getOption().matches(options::OPT_ggdb0);
86438643

8644+
// If a -gdwarf argument appeared, remember it.
8645+
bool EmitDwarf = false;
8646+
if (const Arg *A = getDwarfNArg(Args))
8647+
EmitDwarf = checkDebugInfoOption(A, Args, D, getToolChain());
8648+
8649+
bool EmitCodeView = false;
8650+
if (const Arg *A = Args.getLastArg(options::OPT_gcodeview))
8651+
EmitCodeView = checkDebugInfoOption(A, Args, D, getToolChain());
8652+
8653+
// If the user asked for debug info but did not explicitly specify -gcodeview
8654+
// or -gdwarf, ask the toolchain for the default format.
8655+
if (!EmitCodeView && !EmitDwarf && WantDebug) {
8656+
switch (getToolChain().getDefaultDebugFormat()) {
8657+
case llvm::codegenoptions::DIF_CodeView:
8658+
EmitCodeView = true;
8659+
break;
8660+
case llvm::codegenoptions::DIF_DWARF:
8661+
EmitDwarf = true;
8662+
break;
8663+
}
8664+
}
8665+
8666+
// If the arguments don't imply DWARF, don't emit any debug info here.
8667+
if (!EmitDwarf)
8668+
WantDebug = false;
8669+
86448670
llvm::codegenoptions::DebugInfoKind DebugInfoKind =
86458671
llvm::codegenoptions::NoDebugInfo;
86468672

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

+16-1
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)