Skip to content

Commit a0c9ec1

Browse files
committed
[Driver] Honor "-gdwarf-N" at any position for assembler sources
This fixes an issue when "-gdwarf-N" switch was ignored if it was given before another debug option. Differential Revision: https://reviews.llvm.org/D96865
1 parent 9dcfb95 commit a0c9ec1

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,14 @@ static unsigned DwarfVersionNum(StringRef ArgValue) {
981981
.Default(0);
982982
}
983983

984+
// Find a DWARF format version option.
985+
// This function is a complementary for DwarfVersionNum().
986+
static const Arg *getDwarfNArg(const ArgList &Args) {
987+
return Args.getLastArg(options::OPT_gdwarf_2, options::OPT_gdwarf_3,
988+
options::OPT_gdwarf_4, options::OPT_gdwarf_5,
989+
options::OPT_gdwarf);
990+
}
991+
984992
static void RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs,
985993
codegenoptions::DebugInfoKind DebugInfoKind,
986994
unsigned DwarfVersion,
@@ -3848,9 +3856,7 @@ static void renderDebugOptions(const ToolChain &TC, const Driver &D,
38483856
}
38493857

38503858
// If a -gdwarf argument appeared, remember it.
3851-
const Arg *GDwarfN = Args.getLastArg(
3852-
options::OPT_gdwarf_2, options::OPT_gdwarf_3, options::OPT_gdwarf_4,
3853-
options::OPT_gdwarf_5, options::OPT_gdwarf);
3859+
const Arg *GDwarfN = getDwarfNArg(Args);
38543860
bool EmitDwarf = false;
38553861
if (GDwarfN) {
38563862
if (checkDebugInfoOption(GDwarfN, Args, D, TC))
@@ -7168,18 +7174,14 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
71687174
// Forward -g and handle debug info related flags, assuming we are dealing
71697175
// with an actual assembly file.
71707176
bool WantDebug = false;
7171-
unsigned DwarfVersion = 0;
71727177
Args.ClaimAllArgs(options::OPT_g_Group);
7173-
if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
7178+
if (Arg *A = Args.getLastArg(options::OPT_g_Group))
71747179
WantDebug = !A->getOption().matches(options::OPT_g0) &&
71757180
!A->getOption().matches(options::OPT_ggdb0);
7176-
if (WantDebug)
7177-
DwarfVersion = DwarfVersionNum(A->getSpelling());
7178-
}
71797181

7180-
unsigned DefaultDwarfVersion = ParseDebugDefaultVersion(getToolChain(), Args);
7181-
if (DwarfVersion == 0)
7182-
DwarfVersion = DefaultDwarfVersion;
7182+
unsigned DwarfVersion = ParseDebugDefaultVersion(getToolChain(), Args);
7183+
if (const Arg *GDwarfN = getDwarfNArg(Args))
7184+
DwarfVersion = DwarfVersionNum(GDwarfN->getSpelling());
71837185

71847186
if (DwarfVersion == 0)
71857187
DwarfVersion = getToolChain().GetDefaultDwarfVersion();

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,18 @@
6060
// GDWARF64_VER: error: invalid argument '-gdwarf64' only allowed with 'DWARFv3 or greater'
6161
// GDWARF64_32ARCH: error: invalid argument '-gdwarf64' only allowed with '64 bit architecture'
6262
// GDWARF64_ELF: error: invalid argument '-gdwarf64' only allowed with 'ELF platforms'
63+
64+
// Check that -gdwarf-N can be placed before other options of the "-g" group.
65+
// RUN: %clang -### -c -g -gdwarf-3 -target %itanium_abi_triple -fintegrated-as -x assembler %s 2>&1 \
66+
// RUN: | FileCheck -check-prefix=DWARF3 %s
67+
// RUN: %clang -### -c -gdwarf-3 -g -target %itanium_abi_triple -fintegrated-as -x assembler %s 2>&1 \
68+
// RUN: | FileCheck -check-prefix=DWARF3 %s
69+
// RUN: %clang -### -c -g -gdwarf-5 -target %itanium_abi_triple -fintegrated-as -x assembler %s 2>&1 \
70+
// RUN: | FileCheck -check-prefix=DWARF5 %s
71+
// RUN: %clang -### -c -gdwarf-5 -g -target %itanium_abi_triple -fintegrated-as -x assembler %s 2>&1 \
72+
// RUN: | FileCheck -check-prefix=DWARF5 %s
73+
74+
// DWARF3: "-cc1as"
75+
// DWARF3: "-dwarf-version=3"
76+
// DWARF5: "-cc1as"
77+
// DWARF5: "-dwarf-version=5"

0 commit comments

Comments
 (0)