[PATCH] D96865: [Driver] Honor "-gdwarf-N" at any position for assembler sources

Igor Kudrin via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 17 06:53:33 PST 2021


ikudrin created this revision.
ikudrin added reviewers: dblaikie, probinson, aprantl, MaskRay, mehdi_amini.
ikudrin added projects: LLVM, clang, debug-info.
ikudrin requested review of this revision.

This fixes an issue when `-gdwarf-N` switch was ignored if it was given before another debug option.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96865

Files:
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/Driver/debug-options-as.c


Index: clang/test/Driver/debug-options-as.c
===================================================================
--- clang/test/Driver/debug-options-as.c
+++ clang/test/Driver/debug-options-as.c
@@ -60,3 +60,18 @@
 // GDWARF64_VER:  error: invalid argument '-gdwarf64' only allowed with 'DWARFv3 or greater'
 // GDWARF64_32ARCH: error: invalid argument '-gdwarf64' only allowed with '64 bit architecture'
 // GDWARF64_ELF: error: invalid argument '-gdwarf64' only allowed with 'ELF platforms'
+
+// Check that -gdwarf-N can be placed before other options of the "-g" group.
+// RUN: %clang -### -c -g -gdwarf-3 -integrated-as -x assembler %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=DWARF3 %s
+// RUN: %clang -### -c -gdwarf-3 -g -integrated-as -x assembler %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=DWARF3 %s
+// RUN: %clang -### -c -g -gdwarf-5 -integrated-as -x assembler %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=DWARF5 %s
+// RUN: %clang -### -c -gdwarf-5 -g -integrated-as -x assembler %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=DWARF5 %s
+
+// DWARF3: "-cc1as"
+// DWARF3: "-dwarf-version=3"
+// DWARF5: "-cc1as"
+// DWARF5: "-dwarf-version=5"
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -983,6 +983,14 @@
       .Default(0);
 }
 
+// Find a DWARF format version option.
+// This function is a complementary for DwarfVersionNum().
+static const Arg *getDwarfNArg(const ArgList &Args) {
+  return Args.getLastArg(options::OPT_gdwarf_2, options::OPT_gdwarf_3,
+                         options::OPT_gdwarf_4, options::OPT_gdwarf_5,
+                         options::OPT_gdwarf);
+}
+
 static void RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs,
                                     codegenoptions::DebugInfoKind DebugInfoKind,
                                     unsigned DwarfVersion,
@@ -3850,9 +3858,7 @@
   }
 
   // If a -gdwarf argument appeared, remember it.
-  const Arg *GDwarfN = Args.getLastArg(
-      options::OPT_gdwarf_2, options::OPT_gdwarf_3, options::OPT_gdwarf_4,
-      options::OPT_gdwarf_5, options::OPT_gdwarf);
+  const Arg *GDwarfN = getDwarfNArg(Args);
   bool EmitDwarf = false;
   if (GDwarfN) {
     if (checkDebugInfoOption(GDwarfN, Args, D, TC))
@@ -7175,18 +7181,14 @@
   // Forward -g and handle debug info related flags, assuming we are dealing
   // with an actual assembly file.
   bool WantDebug = false;
-  unsigned DwarfVersion = 0;
   Args.ClaimAllArgs(options::OPT_g_Group);
-  if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
+  if (Arg *A = Args.getLastArg(options::OPT_g_Group))
     WantDebug = !A->getOption().matches(options::OPT_g0) &&
                 !A->getOption().matches(options::OPT_ggdb0);
-    if (WantDebug)
-      DwarfVersion = DwarfVersionNum(A->getSpelling());
-  }
 
-  unsigned DefaultDwarfVersion = ParseDebugDefaultVersion(getToolChain(), Args);
-  if (DwarfVersion == 0)
-    DwarfVersion = DefaultDwarfVersion;
+  unsigned DwarfVersion = ParseDebugDefaultVersion(getToolChain(), Args);
+  if (const Arg *GDwarfN = getDwarfNArg(Args))
+    DwarfVersion = DwarfVersionNum(GDwarfN->getSpelling());
 
   if (DwarfVersion == 0)
     DwarfVersion = getToolChain().GetDefaultDwarfVersion();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96865.324291.patch
Type: text/x-patch
Size: 3357 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210217/473738f8/attachment-0001.bin>


More information about the cfe-commits mailing list