diff --git a/clang/include/clang/Driver/CC1AsOptions.td b/clang/include/clang/Driver/CC1AsOptions.td index 620d7215063f..6a91e0cf8c14 100644 --- a/clang/include/clang/Driver/CC1AsOptions.td +++ b/clang/include/clang/Driver/CC1AsOptions.td @@ -37,6 +37,8 @@ def L : Flag<["-"], "L">, HelpText<"Save temporary labels in the symbol table. " "Note this may change .s semantics, it should almost never be used " "on compiler generated code!">; +def main_file_name : Separate<["-"], "main-file-name">, + HelpText<"Main file name to use for debug info">; //===----------------------------------------------------------------------===// // Frontend Options diff --git a/clang/lib/Driver/Tools.cpp b/clang/lib/Driver/Tools.cpp index bbc3cccef327..eec3974c8dcb 100644 --- a/clang/lib/Driver/Tools.cpp +++ b/clang/lib/Driver/Tools.cpp @@ -1388,12 +1388,12 @@ static bool ShouldDisableCFI(const ArgList &Args, // The native darwin assembler doesn't support cfi directives, so // we disable them if we think the .s file will be passed to it. Default = Args.hasFlag(options::OPT_integrated_as, - options::OPT_no_integrated_as, - TC.IsIntegratedAssemblerDefault()); + options::OPT_no_integrated_as, + TC.IsIntegratedAssemblerDefault()); } return !Args.hasFlag(options::OPT_fdwarf2_cfi_asm, - options::OPT_fno_dwarf2_cfi_asm, - Default); + options::OPT_fno_dwarf2_cfi_asm, + Default); } static bool ShouldDisableDwarfDirectory(const ArgList &Args, @@ -3278,6 +3278,11 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-filetype"); CmdArgs.push_back("obj"); + // Set the main file name, so that debug info works even with + // -save-temps or preprocessed assembly. + CmdArgs.push_back("-main-file-name"); + CmdArgs.push_back(Clang::getBaseInputName(Args, Inputs)); + if (UseRelaxAll(C, Args)) CmdArgs.push_back("-relax-all"); @@ -4367,10 +4372,10 @@ void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA, } void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA, - const InputInfo &Output, - const InputInfoList &Inputs, - const ArgList &Args, - const char *LinkingOutput) const { + const InputInfo &Output, + const InputInfoList &Inputs, + const ArgList &Args, + const char *LinkingOutput) const { ArgStringList CmdArgs; CmdArgs.push_back("--verify"); CmdArgs.push_back("--debug-info"); @@ -5751,7 +5756,7 @@ void minix::Link::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-lCompilerRT-Generic"); CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib"); CmdArgs.push_back( - Args.MakeArgString(getToolChain().GetFilePath("crtend.o"))); + Args.MakeArgString(getToolChain().GetFilePath("crtend.o"))); } const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld")); diff --git a/clang/lib/Driver/Tools.h b/clang/lib/Driver/Tools.h index be38edb034a2..dcfd3114ddbb 100644 --- a/clang/lib/Driver/Tools.h +++ b/clang/lib/Driver/Tools.h @@ -30,6 +30,7 @@ namespace tools { /// \brief Clang compiler tool. class LLVM_LIBRARY_VISIBILITY Clang : public Tool { + public: static const char *getBaseInputName(const ArgList &Args, const InputInfoList &Inputs); static const char *getBaseInputStem(const ArgList &Args, @@ -37,6 +38,7 @@ namespace tools { static const char *getDependencyFileName(const ArgList &Args, const InputInfoList &Inputs); + private: void AddPreprocessingOptions(Compilation &C, const Driver &D, const ArgList &Args, @@ -286,15 +288,15 @@ namespace darwin { class LLVM_LIBRARY_VISIBILITY VerifyDebug : public DarwinTool { public: VerifyDebug(const ToolChain &TC) : DarwinTool("darwin::VerifyDebug", - "dwarfdump", TC) {} + "dwarfdump", TC) {} virtual bool hasIntegratedCPP() const { return false; } virtual void ConstructJob(Compilation &C, const JobAction &JA, - const InputInfo &Output, - const InputInfoList &Inputs, - const ArgList &TCArgs, - const char *LinkingOutput) const; + const InputInfo &Output, + const InputInfoList &Inputs, + const ArgList &TCArgs, + const char *LinkingOutput) const; }; } diff --git a/clang/test/Driver/debug-main-file.S b/clang/test/Driver/debug-main-file.S new file mode 100644 index 000000000000..8c154a32df17 --- /dev/null +++ b/clang/test/Driver/debug-main-file.S @@ -0,0 +1,12 @@ +// REQUIRES: clang-driver +// RUN: %clang -### -c -save-temps -integrated-as -g %s 2>&1 \ +// RUN: | FileCheck %s + +// CHECK: main-file-name + +#ifdef(1) +foo: + nop + nop + nop +#endif diff --git a/clang/tools/driver/cc1as_main.cpp b/clang/tools/driver/cc1as_main.cpp index 73be56c2b4cb..f01d97d1c5b2 100644 --- a/clang/tools/driver/cc1as_main.cpp +++ b/clang/tools/driver/cc1as_main.cpp @@ -84,6 +84,7 @@ struct AssemblerInvocation { unsigned GenDwarfForAssembly : 1; std::string DwarfDebugFlags; std::string DebugCompilationDir; + std::string MainFileName; /// @} /// @name Frontend Options @@ -183,6 +184,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, Opts.GenDwarfForAssembly = Args->hasArg(OPT_g); Opts.DwarfDebugFlags = Args->getLastArgValue(OPT_dwarf_debug_flags); Opts.DebugCompilationDir = Args->getLastArgValue(OPT_fdebug_compilation_dir); + Opts.MainFileName = Args->getLastArgValue(OPT_main_file_name); // Frontend Options if (Args->hasArg(OPT_INPUT)) { @@ -309,6 +311,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags)); if (!Opts.DebugCompilationDir.empty()) Ctx.setCompilationDir(Opts.DebugCompilationDir); + if (!Opts.MainFileName.empty()) + Ctx.setMainFileName(StringRef(Opts.MainFileName)); // Build up the feature string from the target feature list. std::string FS;