[clang][Toolchains][Gnu] pass -g through to assembler
authorNick Desaulniers <ndesaulniers@google.com>
Mon, 24 Oct 2022 19:21:12 +0000 (12:21 -0700)
committerNick Desaulniers <ndesaulniers@google.com>
Mon, 24 Oct 2022 19:30:44 +0000 (12:30 -0700)
We've been working around this for a long time in the Linux kernel; we
bend over backwards to continue to support CC=clang (w/
-fno-integrated-as) for architectures where clang can't yet be used to
assemble the kernel's assembler sources. Supporting debug info for the
combination of CC=clang w/ GNU binutils as "GAS" has been painful.

Fix this in clang so that we can work towards dropping complexity in the
Linux kernel's build system, Kbuild, for supporting this combination of
tools.

GAS added support for -g in 2004 2.16 release via
commit 329e276daf98 ("Add support for a -g switch to GAS")

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D136309

clang/lib/Driver/ToolChains/Gnu.cpp
clang/test/Driver/as-options.s
clang/test/Driver/gcc_forward.c

index f717ca3..9a172db 100644 (file)
@@ -969,6 +969,10 @@ void tools::gnutools::Assembler::ConstructJob(Compilation &C,
   for (const auto &II : Inputs)
     CmdArgs.push_back(II.getFilename());
 
+  if (Arg *A = Args.getLastArg(options::OPT_g_Flag, options::OPT_gN_Group))
+    if (!A->getOption().matches(options::OPT_g0))
+      Args.AddLastArg(CmdArgs, options::OPT_g_Flag);
+
   const char *Exec =
       Args.MakeArgString(getToolChain().GetProgramPath(DefaultAssembler));
   C.addCommand(std::make_unique<Command>(JA, *this,
index 6426968..f76b213 100644 (file)
 // RUN: %clang -mrelax-all -fno-integrated-as -x c++ %s -S -o /dev/null 2>&1 \
 // RUN:   | FileCheck --check-prefix=WARN --allow-empty %s
 // WARN: unused
+
+// Test that -g is passed through to GAS.
+// RUN: %clang --target=aarch64-linux-gnu -fno-integrated-as -g %s -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=DEBUG %s
+// RUN: %clang --target=aarch64-linux-gnu -fno-integrated-as -g0 -g %s -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=DEBUG %s
+// DEBUG: "-g"
+// RUN: %clang --target=aarch64-linux-gnu -fno-integrated-as -g -g0 %s -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=NODEBUG %s
+// NODEBUG-NOT: "-g"
index 9e512d1..491750f 100644 (file)
@@ -34,9 +34,3 @@
 // CHECK-NOT: "-Wall"
 // CHECK-NOT: "-Wdocumentation"
 // CHECK: "-o" "a.out"
-
-// Check that we're not forwarding -g options to the assembler
-// RUN: %clang -g -target x86_64-unknown-linux-gnu -no-integrated-as -c %s -### 2>&1 \
-// RUN:   | FileCheck --check-prefix=CHECK-ASM %s
-// CHECK-ASM: as
-// CHECK-ASM-NOT: "-g"