diff --git a/clang/include/clang/Driver/Driver.h b/clang/include/clang/Driver/Driver.h index 8198913bd04f..b43fa8f6f734 100644 --- a/clang/include/clang/Driver/Driver.h +++ b/clang/include/clang/Driver/Driver.h @@ -40,6 +40,7 @@ class Driver { Diagnostic &Diags; +public: // Diag - Forwarding function for diagnostics. DiagnosticBuilder Diag(unsigned DiagID) const { return Diags.Report(FullSourceLoc(), DiagID); diff --git a/clang/lib/Driver/Tools.cpp b/clang/lib/Driver/Tools.cpp index f7480ea9745f..b84a3e40d045 100644 --- a/clang/lib/Driver/Tools.cpp +++ b/clang/lib/Driver/Tools.cpp @@ -9,8 +9,11 @@ #include "Tools.h" +#include "clang/Driver/Action.h" #include "clang/Driver/Arg.h" #include "clang/Driver/ArgList.h" +#include "clang/Driver/Driver.h" // FIXME: Remove? +#include "clang/Driver/DriverDiagnostic.h" // FIXME: Remove? #include "clang/Driver/Compilation.h" #include "clang/Driver/Job.h" #include "clang/Driver/HostInfo.h" @@ -29,19 +32,286 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Job &Dest, const InputInfo &Output, const InputInfoList &Inputs, - const ArgList &TCArgs, + const ArgList &Args, const char *LinkingOutput) const { ArgStringList CmdArgs; - for (InputInfoList::const_iterator - it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { - const InputInfo &II = *it; - if (II.isPipe()) - CmdArgs.push_back("-"); + if (isa(JA)) { + assert(JA.getType() == types::TY_Plist && "Invalid output type."); + CmdArgs.push_back("-analyze"); + } else if (isa(JA)) { + CmdArgs.push_back("-E"); + } else if (isa(JA)) { + // No special option needed, driven by -x. + // + // FIXME: Don't drive this by -x, that is gross. + + // FIXME: This is a total hack. Copy the input header file + // to the output, so that it can be -include'd by clang. + assert(Inputs.size() == 1 && "Cannot make PCH with multiple inputs."); + assert(!Output.isPipe() && "Unexpected pipe"); + assert(!Inputs[0].isPipe() && "Unexpected pipe"); + const char *InputPath = Inputs[0].getInputFilename(); + llvm::sys::Path OutputPath(Output.getInputFilename()); + OutputPath.eraseComponent(); + if (OutputPath.empty()) + OutputPath = llvm::sys::Path(InputPath).getLast(); else - CmdArgs.push_back(II.getInputFilename()); + OutputPath.appendComponent(llvm::sys::Path(InputPath).getLast()); + if (!OutputPath.exists()) { + ArgStringList CpArgs; + CpArgs.push_back(InputPath); + CpArgs.push_back(Args.MakeArgString(OutputPath.c_str())); + C.getJobs().addJob(new Command("cp", CpArgs)); + } + } else { + assert(isa(JA) && "Invalid action for clang tool."); + + if (JA.getType() == types::TY_Nothing) { + CmdArgs.push_back("-fsyntax-only"); + } else if (JA.getType() == types::TY_LLVMAsm) { + CmdArgs.push_back("-emit-llvm"); + } else if (JA.getType() == types::TY_LLVMBC) { + CmdArgs.push_back("-emit-llvm-bc"); + } else if (JA.getType() == types::TY_PP_Asm) { + CmdArgs.push_back("-S"); + } } + // The make clang go fast button. + CmdArgs.push_back("-disable-free"); + + if (isa(JA)) { + // Add default argument set. + // + // FIXME: Move into clang? + CmdArgs.push_back("-warn-dead-stores"); + CmdArgs.push_back("-checker-cfref"); + CmdArgs.push_back("-warn-objc-methodsigs"); + // Do not enable the missing -dealloc check. + // '-warn-objc-missing-dealloc', + CmdArgs.push_back("-warn-objc-unused-ivars"); + + CmdArgs.push_back("-analyzer-output=plist"); + + // Add -Xanalyzer arguments when running as analyzer. + Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer); + } else { + // Perform argument translation for LLVM backend. This + // takes some care in reconciling with llvm-gcc. The + // issue is that llvm-gcc translates these options based on + // the values in cc1, whereas we are processing based on + // the driver arguments. + // + // FIXME: This is currently broken for -f flags when -fno + // variants are present. + + // This comes from the default translation the driver + cc1 + // would do to enable flag_pic. + // + // FIXME: Centralize this code. + bool PICEnabled = (Args.hasArg(options::OPT_fPIC) || + Args.hasArg(options::OPT_fpic) || + Args.hasArg(options::OPT_fPIE) || + Args.hasArg(options::OPT_fpie)); + bool PICDisabled = (Args.hasArg(options::OPT_mkernel) || + Args.hasArg(options::OPT_static)); + const char *Model = getToolChain().GetForcedPicModel(); + if (!Model) { + if (Args.hasArg(options::OPT_mdynamic_no_pic)) + Model = "dynamic-no-pic"; + else if (PICDisabled) + Model = "static"; + else if (PICEnabled) + Model = "pic"; + else + Model = getToolChain().GetDefaultRelocationModel(); + } + CmdArgs.push_back("--relocation-model"); + CmdArgs.push_back(Model); + + if (Args.hasArg(options::OPT_ftime_report)) + CmdArgs.push_back("--time-passes"); + // FIXME: Set --enable-unsafe-fp-math. + if (!Args.hasArg(options::OPT_fomit_frame_pointer)) + CmdArgs.push_back("--disable-fp-elim"); + if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss, + options::OPT_fno_zero_initialized_in_bss, + true)) + CmdArgs.push_back("--nozero-initialized-in-bss"); + if (Args.hasArg(options::OPT_dA)) + CmdArgs.push_back("--asm-verbose"); + if (Args.hasArg(options::OPT_fdebug_pass_structure)) + CmdArgs.push_back("--debug-pass=Structure"); + if (Args.hasArg(options::OPT_fdebug_pass_arguments)) + CmdArgs.push_back("--debug-pass=Arguments"); + // FIXME: set --inline-threshhold=50 if (optimize_size || optimize + // < 3) + if (Args.hasFlag(options::OPT_funwind_tables, + options::OPT_fno_unwind_tables, + getToolChain().IsUnwindTablesDefault())) + CmdArgs.push_back("--unwind-tables=1"); + else + CmdArgs.push_back("--unwind-tables=0"); + if (!Args.hasFlag(options::OPT_mred_zone, + options::OPT_mno_red_zone, + true)) + CmdArgs.push_back("--disable-red-zone"); + if (Args.hasFlag(options::OPT_msoft_float, + options::OPT_mno_soft_float, + false)) + CmdArgs.push_back("--soft-float"); + + // FIXME: Need target hooks. + if (memcmp(getToolChain().getPlatform().c_str(), "darwin", 6) == 0) { + if (getToolChain().getArchName() == "x86_64") + CmdArgs.push_back("--mcpu=core2"); + else if (getToolChain().getArchName() == "i386") + CmdArgs.push_back("--mcpu=yonah"); + } + + // FIXME: Ignores ordering. Also, we need to find a realistic + // solution for this. + static const struct { + options::ID Pos, Neg; + const char *Name; + } FeatureOptions[] = { + { options::OPT_mmmx, options::OPT_mno_mmx, "mmx" }, + { options::OPT_msse, options::OPT_mno_sse, "sse" }, + { options::OPT_msse2, options::OPT_mno_sse2, "sse2" }, + { options::OPT_msse3, options::OPT_mno_sse3, "sse3" }, + { options::OPT_mssse3, options::OPT_mno_ssse3, "ssse3" }, + { options::OPT_msse41, options::OPT_mno_sse41, "sse41" }, + { options::OPT_msse42, options::OPT_mno_sse42, "sse42" }, + { options::OPT_msse4a, options::OPT_mno_sse4a, "sse4a" }, + { options::OPT_m3dnow, options::OPT_mno_3dnow, "3dnow" }, + { options::OPT_m3dnowa, options::OPT_mno_3dnowa, "3dnowa" } + }; + const unsigned NumFeatureOptions = + sizeof(FeatureOptions)/sizeof(FeatureOptions[0]); + + // FIXME: Avoid std::string + std::string Attrs; + for (unsigned i=0; i < NumFeatureOptions; ++i) { + if (Args.hasArg(FeatureOptions[i].Pos)) { + Attrs += '+'; + Attrs += FeatureOptions[i].Name; + } else if (Args.hasArg(FeatureOptions[i].Neg)) { + Attrs += '-'; + Attrs += FeatureOptions[i].Name; + } + } + if (!Attrs.empty()) { + CmdArgs.push_back("--mattr"); + CmdArgs.push_back(Args.MakeArgString(Attrs.c_str())); + } + + if (Args.hasFlag(options::OPT_fmath_errno, + options::OPT_fno_math_errno, + getToolChain().IsMathErrnoDefault())) + CmdArgs.push_back("--fmath-errno=1"); + else + CmdArgs.push_back("--fmath-errno=0"); + + if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) { + CmdArgs.push_back("--limit-float-precision"); + CmdArgs.push_back(A->getValue(Args)); + } + + // FIXME: Add --stack-protector-buffer-size= on + // -fstack-protect. + + Args.AddLastArg(CmdArgs, options::OPT_MD); + Args.AddLastArg(CmdArgs, options::OPT_MM); + Args.AddAllArgs(CmdArgs, options::OPT_MF); + Args.AddLastArg(CmdArgs, options::OPT_MP); + Args.AddAllArgs(CmdArgs, options::OPT_MT); + + Arg *Unsupported = Args.getLastArg(options::OPT_M); + if (!Unsupported) + Unsupported = Args.getLastArg(options::OPT_MM); + if (!Unsupported) + Unsupported = Args.getLastArg(options::OPT_MG); + if (!Unsupported) + Unsupported = Args.getLastArg(options::OPT_MQ); + if (Unsupported) { + const Driver &D = getToolChain().getHost().getDriver(); + D.Diag(clang::diag::err_drv_unsupported_opt) + << Unsupported->getOption().getName(); + } + } + + Args.AddAllArgs(CmdArgs, options::OPT_v); + Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U); + Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F); + Args.AddLastArg(CmdArgs, options::OPT_P); + Args.AddAllArgs(CmdArgs, options::OPT_mmacosx_version_min_EQ); + + // Special case debug options to only pass -g to clang. This is + // wrong. + if (Args.hasArg(options::OPT_g_Group)) + CmdArgs.push_back("-g"); + + Args.AddLastArg(CmdArgs, options::OPT_nostdinc); + + // FIXME: Clang isn't going to accept just anything here. + Args.AddAllArgs(CmdArgs, options::OPT_i_Group); + + // Automatically load .pth or .gch files which match -include + // options. It's wonky, but we include looking for .gch so we can + // support seamless replacement into a build system already set up + // to be generating .gch files. + + // FIXME: Need to use an iterator for this to be efficient. + for (ArgList::const_iterator + it = Args.begin(), ie = Args.end(); it != ie; ++it) { + const Arg *A = *it; + if (A->getOption().matches(options::OPT_include)) { + llvm::sys::Path P(A->getValue(Args)); + P.appendSuffix("pth"); + if (P.exists()) { + CmdArgs.push_back("-token-cache"); + CmdArgs.push_back(Args.MakeArgString(P.c_str())); + } else { + P.eraseSuffix(); + P.appendSuffix("gch"); + if (P.exists()) { + CmdArgs.push_back("-token-cache"); + CmdArgs.push_back(Args.MakeArgString(P.c_str())); + } + } + } + } + + // Manually translate -O to -O1; let clang reject others. + if (Arg *A = Args.getLastArg(options::OPT_O)) { + if (A->getValue(Args)[0] == '\0') + CmdArgs.push_back("-O1"); + else + CmdArgs.push_back(A->getValue(Args)); + } + + Args.AddAllArgs(CmdArgs, options::OPT_clang_W_Group, options::OPT_pedantic_Group); + Args.AddLastArg(CmdArgs, options::OPT_w); + Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi, + options::OPT_trigraphs); + + if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) { + CmdArgs.push_back("-ftemplate-depth"); + CmdArgs.push_back(A->getValue(Args)); + } + + Args.AddAllArgs(CmdArgs, options::OPT_clang_f_Group); + + Args.AddLastArg(CmdArgs, options::OPT_dM); + + Args.AddAllArgValues(CmdArgs, options::OPT_Xclang); + + // FIXME: Always pass the full triple once we aren't concerned with + // ccc compat. + CmdArgs.push_back("-arch"); + CmdArgs.push_back(getToolChain().getArchName().c_str()); + if (Output.isPipe()) { CmdArgs.push_back("-o"); CmdArgs.push_back("-"); @@ -50,22 +320,35 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back(N); } - Dest.addCommand(new Command("clang", CmdArgs)); + for (InputInfoList::const_iterator + it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { + const InputInfo &II = *it; + CmdArgs.push_back("-x"); + CmdArgs.push_back(types::getTypeName(II.getType())); + if (II.isPipe()) + CmdArgs.push_back("-"); + else + CmdArgs.push_back(II.getInputFilename()); + } + + const char *Exec = + Args.MakeArgString(getToolChain().GetProgramPath(C, "clang").c_str()); + Dest.addCommand(new Command(Exec, CmdArgs)); } void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA, Job &Dest, const InputInfo &Output, const InputInfoList &Inputs, - const ArgList &TCArgs, + const ArgList &Args, const char *LinkingOutput) const { ArgStringList CmdArgs; for (ArgList::const_iterator - it = TCArgs.begin(), ie = TCArgs.end(); it != ie; ++it) { + it = Args.begin(), ie = Args.end(); it != ie; ++it) { Arg *A = *it; if (A->getOption().hasForwardToGCC()) - A->render(TCArgs, CmdArgs); + A->render(Args, CmdArgs); } RenderExtraToolArgs(CmdArgs);