diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -972,19 +972,27 @@ CmdArgs.push_back("-o"); CmdArgs.push_back(Output.getFilename()); - for (const auto &II : Inputs) - CmdArgs.push_back(II.getFilename()); - - if (Arg *A = Args.getLastArg(options::OPT_g_Flag, options::OPT_gN_Group, - options::OPT_gdwarf_2, options::OPT_gdwarf_3, - options::OPT_gdwarf_4, options::OPT_gdwarf_5, - options::OPT_gdwarf)) - if (!A->getOption().matches(options::OPT_g0)) { - Args.AddLastArg(CmdArgs, options::OPT_g_Flag); - unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args); - CmdArgs.push_back(Args.MakeArgString("-gdwarf-" + Twine(DwarfVersion))); - } + bool IsInputTyAsm = false; + for (const auto &II : Inputs) { + CmdArgs.push_back(II.getFilename()); + StringRef BaseInput = StringRef(II.getBaseInput()); + types::ID InputType = types::lookupTypeForExtension( + llvm::sys::path::extension(BaseInput).drop_front()); + if (InputType == types::TY_Asm || InputType == types::TY_PP_Asm) + IsInputTyAsm = true; + } + + if (IsInputTyAsm) + if (Arg *A = Args.getLastArg(options::OPT_g_Flag, options::OPT_gN_Group, + options::OPT_gdwarf_2, options::OPT_gdwarf_3, + options::OPT_gdwarf_4, options::OPT_gdwarf_5, + options::OPT_gdwarf)) + if (!A->getOption().matches(options::OPT_g0)) { + Args.AddLastArg(CmdArgs, options::OPT_g_Flag); + unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args); + CmdArgs.push_back(Args.MakeArgString("-gdwarf-" + Twine(DwarfVersion))); + } const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath(DefaultAssembler)); diff --git a/clang/test/Driver/as-options.cpp b/clang/test/Driver/as-options.cpp new file mode 100755 --- /dev/null +++ b/clang/test/Driver/as-options.cpp @@ -0,0 +1,11 @@ +// Test that -g and -gdwarf-* are not passed through to GAS. +// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -g -c %s -### 2>&1 | \ +// RUN: FileCheck --check-prefix=DEBUG %s +// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -gdwarf-4 -c %s -### 2>&1 | \ +// RUN: FileCheck --check-prefix=DEBUG %s +// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -gdwarf-4 -g -c %s -### 2>&1 | \ +// RUN: FileCheck --check-prefix=DEBUG %s +// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -g \ +// RUN: -fdebug-default-version=4 -c %s -### 2>&1 | FileCheck --check-prefix=DEBUG %s +// DEBUG-NOT: "-g" +// DEBUG-NOT: "-gdwarf-4" \ No newline at end of file diff --git a/clang/test/Driver/gcc_forward.c b/clang/test/Driver/gcc_forward.c --- a/clang/test/Driver/gcc_forward.c +++ b/clang/test/Driver/gcc_forward.c @@ -34,3 +34,15 @@ // CHECK-NOT: "-Wall" // CHECK-NOT: "-Wdocumentation" // CHECK: "-o" "a.out" +// +// Test that -g and -gdwarf-* are not passed through to GAS. +// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -g -c %s -### 2>&1 | \ +// RUN: FileCheck --check-prefix=DEBUG %s +// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -gdwarf-4 -c %s -### 2>&1 | \ +// RUN: FileCheck --check-prefix=DEBUG %s +// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -gdwarf-4 -g -c %s -### 2>&1 | \ +// RUN: FileCheck --check-prefix=DEBUG %s +// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -g \ +// RUN: -fdebug-default-version=4 -c %s -### 2>&1 | FileCheck --check-prefix=DEBUG %s +// DEBUG-NOT: "-g" +// DEBUG-NOT: "-gdwarf-4" \ No newline at end of file