xref: /minix3/external/bsd/llvm/dist/clang/lib/Driver/Driver.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- Driver.cpp - Clang GCC Compatible Driver -------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "clang/Driver/Driver.h"
11f4a2713aSLionel Sambuc #include "InputInfo.h"
12f4a2713aSLionel Sambuc #include "ToolChains.h"
13f4a2713aSLionel Sambuc #include "clang/Basic/Version.h"
14*0a6a1f1dSLionel Sambuc #include "clang/Config/config.h"
15f4a2713aSLionel Sambuc #include "clang/Driver/Action.h"
16f4a2713aSLionel Sambuc #include "clang/Driver/Compilation.h"
17f4a2713aSLionel Sambuc #include "clang/Driver/DriverDiagnostic.h"
18f4a2713aSLionel Sambuc #include "clang/Driver/Job.h"
19f4a2713aSLionel Sambuc #include "clang/Driver/Options.h"
20f4a2713aSLionel Sambuc #include "clang/Driver/Tool.h"
21f4a2713aSLionel Sambuc #include "clang/Driver/ToolChain.h"
22f4a2713aSLionel Sambuc #include "llvm/ADT/ArrayRef.h"
23f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
24*0a6a1f1dSLionel Sambuc #include "llvm/ADT/StringExtras.h"
25f4a2713aSLionel Sambuc #include "llvm/ADT/StringSet.h"
26f4a2713aSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
27f4a2713aSLionel Sambuc #include "llvm/Option/Arg.h"
28f4a2713aSLionel Sambuc #include "llvm/Option/ArgList.h"
29*0a6a1f1dSLionel Sambuc #include "llvm/Option/OptSpecifier.h"
30f4a2713aSLionel Sambuc #include "llvm/Option/OptTable.h"
31f4a2713aSLionel Sambuc #include "llvm/Option/Option.h"
32f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
33f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
34f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
35f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
36f4a2713aSLionel Sambuc #include "llvm/Support/PrettyStackTrace.h"
37*0a6a1f1dSLionel Sambuc #include "llvm/Support/Process.h"
38f4a2713aSLionel Sambuc #include "llvm/Support/Program.h"
39f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
40f4a2713aSLionel Sambuc #include <map>
41*0a6a1f1dSLionel Sambuc #include <memory>
42f4a2713aSLionel Sambuc 
43f4a2713aSLionel Sambuc using namespace clang::driver;
44f4a2713aSLionel Sambuc using namespace clang;
45f4a2713aSLionel Sambuc using namespace llvm::opt;
46f4a2713aSLionel Sambuc 
Driver(StringRef ClangExecutable,StringRef DefaultTargetTriple,DiagnosticsEngine & Diags)47f4a2713aSLionel Sambuc Driver::Driver(StringRef ClangExecutable,
48f4a2713aSLionel Sambuc                StringRef DefaultTargetTriple,
49f4a2713aSLionel Sambuc                DiagnosticsEngine &Diags)
50f4a2713aSLionel Sambuc   : Opts(createDriverOptTable()), Diags(Diags), Mode(GCCMode),
51f4a2713aSLionel Sambuc     ClangExecutable(ClangExecutable), SysRoot(DEFAULT_SYSROOT),
52f4a2713aSLionel Sambuc     UseStdLib(true), DefaultTargetTriple(DefaultTargetTriple),
53f4a2713aSLionel Sambuc     DriverTitle("clang LLVM compiler"),
54*0a6a1f1dSLionel Sambuc     CCPrintOptionsFilename(nullptr), CCPrintHeadersFilename(nullptr),
55*0a6a1f1dSLionel Sambuc     CCLogDiagnosticsFilename(nullptr),
56f4a2713aSLionel Sambuc     CCCPrintBindings(false),
57f4a2713aSLionel Sambuc     CCPrintHeaders(false), CCLogDiagnostics(false),
58f4a2713aSLionel Sambuc     CCGenDiagnostics(false), CCCGenericGCCName(""), CheckInputsExist(true),
59f4a2713aSLionel Sambuc     CCCUsePCH(true), SuppressMissingInputWarning(false) {
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc   Name = llvm::sys::path::stem(ClangExecutable);
62f4a2713aSLionel Sambuc   Dir  = llvm::sys::path::parent_path(ClangExecutable);
63f4a2713aSLionel Sambuc 
64f4a2713aSLionel Sambuc   // Compute the path to the resource directory.
65f4a2713aSLionel Sambuc   StringRef ClangResourceDir(CLANG_RESOURCE_DIR);
66f4a2713aSLionel Sambuc   SmallString<128> P(Dir);
67*0a6a1f1dSLionel Sambuc   if (ClangResourceDir != "") {
68f4a2713aSLionel Sambuc     llvm::sys::path::append(P, ClangResourceDir);
69*0a6a1f1dSLionel Sambuc   } else {
70*0a6a1f1dSLionel Sambuc     StringRef ClangLibdirSuffix(CLANG_LIBDIR_SUFFIX);
71*0a6a1f1dSLionel Sambuc     llvm::sys::path::append(P, "..", Twine("lib") + ClangLibdirSuffix, "clang",
72*0a6a1f1dSLionel Sambuc                             CLANG_VERSION_STRING);
73*0a6a1f1dSLionel Sambuc   }
74f4a2713aSLionel Sambuc   ResourceDir = P.str();
75f4a2713aSLionel Sambuc }
76f4a2713aSLionel Sambuc 
~Driver()77f4a2713aSLionel Sambuc Driver::~Driver() {
78f4a2713aSLionel Sambuc   delete Opts;
79f4a2713aSLionel Sambuc 
80*0a6a1f1dSLionel Sambuc   llvm::DeleteContainerSeconds(ToolChains);
81f4a2713aSLionel Sambuc }
82f4a2713aSLionel Sambuc 
ParseDriverMode(ArrayRef<const char * > Args)83f4a2713aSLionel Sambuc void Driver::ParseDriverMode(ArrayRef<const char *> Args) {
84f4a2713aSLionel Sambuc   const std::string OptName =
85f4a2713aSLionel Sambuc     getOpts().getOption(options::OPT_driver_mode).getPrefixedName();
86f4a2713aSLionel Sambuc 
87f4a2713aSLionel Sambuc   for (size_t I = 0, E = Args.size(); I != E; ++I) {
88*0a6a1f1dSLionel Sambuc     // Ingore nullptrs, they are response file's EOL markers
89*0a6a1f1dSLionel Sambuc     if (Args[I] == nullptr)
90*0a6a1f1dSLionel Sambuc       continue;
91f4a2713aSLionel Sambuc     const StringRef Arg = Args[I];
92f4a2713aSLionel Sambuc     if (!Arg.startswith(OptName))
93f4a2713aSLionel Sambuc       continue;
94f4a2713aSLionel Sambuc 
95f4a2713aSLionel Sambuc     const StringRef Value = Arg.drop_front(OptName.size());
96f4a2713aSLionel Sambuc     const unsigned M = llvm::StringSwitch<unsigned>(Value)
97f4a2713aSLionel Sambuc         .Case("gcc", GCCMode)
98f4a2713aSLionel Sambuc         .Case("g++", GXXMode)
99f4a2713aSLionel Sambuc         .Case("cpp", CPPMode)
100f4a2713aSLionel Sambuc         .Case("cl",  CLMode)
101f4a2713aSLionel Sambuc         .Default(~0U);
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc     if (M != ~0U)
104f4a2713aSLionel Sambuc       Mode = static_cast<DriverMode>(M);
105f4a2713aSLionel Sambuc     else
106f4a2713aSLionel Sambuc       Diag(diag::err_drv_unsupported_option_argument) << OptName << Value;
107f4a2713aSLionel Sambuc   }
108f4a2713aSLionel Sambuc }
109f4a2713aSLionel Sambuc 
ParseArgStrings(ArrayRef<const char * > ArgStrings)110*0a6a1f1dSLionel Sambuc InputArgList *Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings) {
111f4a2713aSLionel Sambuc   llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
112f4a2713aSLionel Sambuc 
113f4a2713aSLionel Sambuc   unsigned IncludedFlagsBitmask;
114f4a2713aSLionel Sambuc   unsigned ExcludedFlagsBitmask;
115*0a6a1f1dSLionel Sambuc   std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) =
116f4a2713aSLionel Sambuc     getIncludeExcludeOptionFlagMasks();
117f4a2713aSLionel Sambuc 
118f4a2713aSLionel Sambuc   unsigned MissingArgIndex, MissingArgCount;
119*0a6a1f1dSLionel Sambuc   InputArgList *Args = getOpts().ParseArgs(ArgStrings.begin(), ArgStrings.end(),
120f4a2713aSLionel Sambuc                                            MissingArgIndex, MissingArgCount,
121f4a2713aSLionel Sambuc                                            IncludedFlagsBitmask,
122f4a2713aSLionel Sambuc                                            ExcludedFlagsBitmask);
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc   // Check for missing argument error.
125f4a2713aSLionel Sambuc   if (MissingArgCount)
126f4a2713aSLionel Sambuc     Diag(clang::diag::err_drv_missing_argument)
127f4a2713aSLionel Sambuc       << Args->getArgString(MissingArgIndex) << MissingArgCount;
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc   // Check for unsupported options.
130*0a6a1f1dSLionel Sambuc   for (const Arg *A : *Args) {
131f4a2713aSLionel Sambuc     if (A->getOption().hasFlag(options::Unsupported)) {
132f4a2713aSLionel Sambuc       Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
133f4a2713aSLionel Sambuc       continue;
134f4a2713aSLionel Sambuc     }
135f4a2713aSLionel Sambuc 
136f4a2713aSLionel Sambuc     // Warn about -mcpu= without an argument.
137f4a2713aSLionel Sambuc     if (A->getOption().matches(options::OPT_mcpu_EQ) &&
138f4a2713aSLionel Sambuc         A->containsValue("")) {
139f4a2713aSLionel Sambuc       Diag(clang::diag::warn_drv_empty_joined_argument) <<
140f4a2713aSLionel Sambuc         A->getAsString(*Args);
141f4a2713aSLionel Sambuc     }
142f4a2713aSLionel Sambuc   }
143f4a2713aSLionel Sambuc 
144f4a2713aSLionel Sambuc   for (arg_iterator it = Args->filtered_begin(options::OPT_UNKNOWN),
145f4a2713aSLionel Sambuc          ie = Args->filtered_end(); it != ie; ++it) {
146f4a2713aSLionel Sambuc     Diags.Report(diag::err_drv_unknown_argument) << (*it) ->getAsString(*Args);
147f4a2713aSLionel Sambuc   }
148f4a2713aSLionel Sambuc 
149f4a2713aSLionel Sambuc   return Args;
150f4a2713aSLionel Sambuc }
151f4a2713aSLionel Sambuc 
152f4a2713aSLionel Sambuc // Determine which compilation mode we are in. We look for options which
153f4a2713aSLionel Sambuc // affect the phase, starting with the earliest phases, and record which
154f4a2713aSLionel Sambuc // option we used to determine the final phase.
getFinalPhase(const DerivedArgList & DAL,Arg ** FinalPhaseArg) const155f4a2713aSLionel Sambuc phases::ID Driver::getFinalPhase(const DerivedArgList &DAL, Arg **FinalPhaseArg)
156f4a2713aSLionel Sambuc const {
157*0a6a1f1dSLionel Sambuc   Arg *PhaseArg = nullptr;
158f4a2713aSLionel Sambuc   phases::ID FinalPhase;
159f4a2713aSLionel Sambuc 
160*0a6a1f1dSLionel Sambuc   // -{E,EP,P,M,MM} only run the preprocessor.
161f4a2713aSLionel Sambuc   if (CCCIsCPP() ||
162f4a2713aSLionel Sambuc       (PhaseArg = DAL.getLastArg(options::OPT_E)) ||
163*0a6a1f1dSLionel Sambuc       (PhaseArg = DAL.getLastArg(options::OPT__SLASH_EP)) ||
164*0a6a1f1dSLionel Sambuc       (PhaseArg = DAL.getLastArg(options::OPT_M, options::OPT_MM)) ||
165*0a6a1f1dSLionel Sambuc       (PhaseArg = DAL.getLastArg(options::OPT__SLASH_P))) {
166f4a2713aSLionel Sambuc     FinalPhase = phases::Preprocess;
167f4a2713aSLionel Sambuc 
168*0a6a1f1dSLionel Sambuc     // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
169f4a2713aSLionel Sambuc   } else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) ||
170f4a2713aSLionel Sambuc              (PhaseArg = DAL.getLastArg(options::OPT_module_file_info)) ||
171*0a6a1f1dSLionel Sambuc              (PhaseArg = DAL.getLastArg(options::OPT_verify_pch)) ||
172f4a2713aSLionel Sambuc              (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) ||
173f4a2713aSLionel Sambuc              (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) ||
174f4a2713aSLionel Sambuc              (PhaseArg = DAL.getLastArg(options::OPT__migrate)) ||
175f4a2713aSLionel Sambuc              (PhaseArg = DAL.getLastArg(options::OPT__analyze,
176f4a2713aSLionel Sambuc                                         options::OPT__analyze_auto)) ||
177*0a6a1f1dSLionel Sambuc              (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) {
178f4a2713aSLionel Sambuc     FinalPhase = phases::Compile;
179f4a2713aSLionel Sambuc 
180*0a6a1f1dSLionel Sambuc     // -S only runs up to the backend.
181*0a6a1f1dSLionel Sambuc   } else if ((PhaseArg = DAL.getLastArg(options::OPT_S))) {
182*0a6a1f1dSLionel Sambuc     FinalPhase = phases::Backend;
183*0a6a1f1dSLionel Sambuc 
184f4a2713aSLionel Sambuc     // -c only runs up to the assembler.
185f4a2713aSLionel Sambuc   } else if ((PhaseArg = DAL.getLastArg(options::OPT_c))) {
186f4a2713aSLionel Sambuc     FinalPhase = phases::Assemble;
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc     // Otherwise do everything.
189f4a2713aSLionel Sambuc   } else
190f4a2713aSLionel Sambuc     FinalPhase = phases::Link;
191f4a2713aSLionel Sambuc 
192f4a2713aSLionel Sambuc   if (FinalPhaseArg)
193f4a2713aSLionel Sambuc     *FinalPhaseArg = PhaseArg;
194f4a2713aSLionel Sambuc 
195f4a2713aSLionel Sambuc   return FinalPhase;
196f4a2713aSLionel Sambuc }
197f4a2713aSLionel Sambuc 
MakeInputArg(DerivedArgList & Args,OptTable * Opts,StringRef Value)198*0a6a1f1dSLionel Sambuc static Arg* MakeInputArg(DerivedArgList &Args, OptTable *Opts,
199f4a2713aSLionel Sambuc                          StringRef Value) {
200f4a2713aSLionel Sambuc   Arg *A = new Arg(Opts->getOption(options::OPT_INPUT), Value,
201f4a2713aSLionel Sambuc                    Args.getBaseArgs().MakeIndex(Value), Value.data());
202*0a6a1f1dSLionel Sambuc   Args.AddSynthesizedArg(A);
203f4a2713aSLionel Sambuc   A->claim();
204f4a2713aSLionel Sambuc   return A;
205f4a2713aSLionel Sambuc }
206f4a2713aSLionel Sambuc 
TranslateInputArgs(const InputArgList & Args) const207f4a2713aSLionel Sambuc DerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const {
208f4a2713aSLionel Sambuc   DerivedArgList *DAL = new DerivedArgList(Args);
209f4a2713aSLionel Sambuc 
210f4a2713aSLionel Sambuc   bool HasNostdlib = Args.hasArg(options::OPT_nostdlib);
211*0a6a1f1dSLionel Sambuc   for (Arg *A : Args) {
212f4a2713aSLionel Sambuc     // Unfortunately, we have to parse some forwarding options (-Xassembler,
213f4a2713aSLionel Sambuc     // -Xlinker, -Xpreprocessor) because we either integrate their functionality
214f4a2713aSLionel Sambuc     // (assembler and preprocessor), or bypass a previous driver ('collect2').
215f4a2713aSLionel Sambuc 
216f4a2713aSLionel Sambuc     // Rewrite linker options, to replace --no-demangle with a custom internal
217f4a2713aSLionel Sambuc     // option.
218f4a2713aSLionel Sambuc     if ((A->getOption().matches(options::OPT_Wl_COMMA) ||
219f4a2713aSLionel Sambuc          A->getOption().matches(options::OPT_Xlinker)) &&
220f4a2713aSLionel Sambuc         A->containsValue("--no-demangle")) {
221f4a2713aSLionel Sambuc       // Add the rewritten no-demangle argument.
222f4a2713aSLionel Sambuc       DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_Xlinker__no_demangle));
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc       // Add the remaining values as Xlinker arguments.
225f4a2713aSLionel Sambuc       for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
226f4a2713aSLionel Sambuc         if (StringRef(A->getValue(i)) != "--no-demangle")
227f4a2713aSLionel Sambuc           DAL->AddSeparateArg(A, Opts->getOption(options::OPT_Xlinker),
228f4a2713aSLionel Sambuc                               A->getValue(i));
229f4a2713aSLionel Sambuc 
230f4a2713aSLionel Sambuc       continue;
231f4a2713aSLionel Sambuc     }
232f4a2713aSLionel Sambuc 
233f4a2713aSLionel Sambuc     // Rewrite preprocessor options, to replace -Wp,-MD,FOO which is used by
234f4a2713aSLionel Sambuc     // some build systems. We don't try to be complete here because we don't
235f4a2713aSLionel Sambuc     // care to encourage this usage model.
236f4a2713aSLionel Sambuc     if (A->getOption().matches(options::OPT_Wp_COMMA) &&
237f4a2713aSLionel Sambuc         (A->getValue(0) == StringRef("-MD") ||
238f4a2713aSLionel Sambuc          A->getValue(0) == StringRef("-MMD"))) {
239f4a2713aSLionel Sambuc       // Rewrite to -MD/-MMD along with -MF.
240f4a2713aSLionel Sambuc       if (A->getValue(0) == StringRef("-MD"))
241f4a2713aSLionel Sambuc         DAL->AddFlagArg(A, Opts->getOption(options::OPT_MD));
242f4a2713aSLionel Sambuc       else
243f4a2713aSLionel Sambuc         DAL->AddFlagArg(A, Opts->getOption(options::OPT_MMD));
244f4a2713aSLionel Sambuc       if (A->getNumValues() == 2)
245f4a2713aSLionel Sambuc         DAL->AddSeparateArg(A, Opts->getOption(options::OPT_MF),
246f4a2713aSLionel Sambuc                             A->getValue(1));
247f4a2713aSLionel Sambuc       continue;
248f4a2713aSLionel Sambuc     }
249f4a2713aSLionel Sambuc 
250f4a2713aSLionel Sambuc     // Rewrite reserved library names.
251f4a2713aSLionel Sambuc     if (A->getOption().matches(options::OPT_l)) {
252f4a2713aSLionel Sambuc       StringRef Value = A->getValue();
253f4a2713aSLionel Sambuc 
254f4a2713aSLionel Sambuc       // Rewrite unless -nostdlib is present.
255f4a2713aSLionel Sambuc       if (!HasNostdlib && Value == "stdc++") {
256f4a2713aSLionel Sambuc         DAL->AddFlagArg(A, Opts->getOption(
257f4a2713aSLionel Sambuc                               options::OPT_Z_reserved_lib_stdcxx));
258f4a2713aSLionel Sambuc         continue;
259f4a2713aSLionel Sambuc       }
260f4a2713aSLionel Sambuc 
261f4a2713aSLionel Sambuc       // Rewrite unconditionally.
262f4a2713aSLionel Sambuc       if (Value == "cc_kext") {
263f4a2713aSLionel Sambuc         DAL->AddFlagArg(A, Opts->getOption(
264f4a2713aSLionel Sambuc                               options::OPT_Z_reserved_lib_cckext));
265f4a2713aSLionel Sambuc         continue;
266f4a2713aSLionel Sambuc       }
267f4a2713aSLionel Sambuc     }
268f4a2713aSLionel Sambuc 
269f4a2713aSLionel Sambuc     // Pick up inputs via the -- option.
270f4a2713aSLionel Sambuc     if (A->getOption().matches(options::OPT__DASH_DASH)) {
271f4a2713aSLionel Sambuc       A->claim();
272f4a2713aSLionel Sambuc       for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
273f4a2713aSLionel Sambuc         DAL->append(MakeInputArg(*DAL, Opts, A->getValue(i)));
274f4a2713aSLionel Sambuc       continue;
275f4a2713aSLionel Sambuc     }
276f4a2713aSLionel Sambuc 
277*0a6a1f1dSLionel Sambuc     DAL->append(A);
278f4a2713aSLionel Sambuc   }
279f4a2713aSLionel Sambuc 
280f4a2713aSLionel Sambuc   // Add a default value of -mlinker-version=, if one was given and the user
281f4a2713aSLionel Sambuc   // didn't specify one.
282f4a2713aSLionel Sambuc #if defined(HOST_LINK_VERSION)
283f4a2713aSLionel Sambuc   if (!Args.hasArg(options::OPT_mlinker_version_EQ)) {
284f4a2713aSLionel Sambuc     DAL->AddJoinedArg(0, Opts->getOption(options::OPT_mlinker_version_EQ),
285f4a2713aSLionel Sambuc                       HOST_LINK_VERSION);
286f4a2713aSLionel Sambuc     DAL->getLastArg(options::OPT_mlinker_version_EQ)->claim();
287f4a2713aSLionel Sambuc   }
288f4a2713aSLionel Sambuc #endif
289f4a2713aSLionel Sambuc 
290f4a2713aSLionel Sambuc   return DAL;
291f4a2713aSLionel Sambuc }
292f4a2713aSLionel Sambuc 
BuildCompilation(ArrayRef<const char * > ArgList)293f4a2713aSLionel Sambuc Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
294f4a2713aSLionel Sambuc   llvm::PrettyStackTraceString CrashInfo("Compilation construction");
295f4a2713aSLionel Sambuc 
296f4a2713aSLionel Sambuc   // FIXME: Handle environment options which affect driver behavior, somewhere
297f4a2713aSLionel Sambuc   // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS.
298f4a2713aSLionel Sambuc 
299f4a2713aSLionel Sambuc   if (char *env = ::getenv("COMPILER_PATH")) {
300f4a2713aSLionel Sambuc     StringRef CompilerPath = env;
301f4a2713aSLionel Sambuc     while (!CompilerPath.empty()) {
302f4a2713aSLionel Sambuc       std::pair<StringRef, StringRef> Split
303f4a2713aSLionel Sambuc         = CompilerPath.split(llvm::sys::EnvPathSeparator);
304f4a2713aSLionel Sambuc       PrefixDirs.push_back(Split.first);
305f4a2713aSLionel Sambuc       CompilerPath = Split.second;
306f4a2713aSLionel Sambuc     }
307f4a2713aSLionel Sambuc   }
308f4a2713aSLionel Sambuc 
309f4a2713aSLionel Sambuc   // We look for the driver mode option early, because the mode can affect
310f4a2713aSLionel Sambuc   // how other options are parsed.
311f4a2713aSLionel Sambuc   ParseDriverMode(ArgList.slice(1));
312f4a2713aSLionel Sambuc 
313f4a2713aSLionel Sambuc   // FIXME: What are we going to do with -V and -b?
314f4a2713aSLionel Sambuc 
315f4a2713aSLionel Sambuc   // FIXME: This stuff needs to go into the Compilation, not the driver.
316f4a2713aSLionel Sambuc   bool CCCPrintActions;
317f4a2713aSLionel Sambuc 
318f4a2713aSLionel Sambuc   InputArgList *Args = ParseArgStrings(ArgList.slice(1));
319f4a2713aSLionel Sambuc 
320f4a2713aSLionel Sambuc   // -no-canonical-prefixes is used very early in main.
321f4a2713aSLionel Sambuc   Args->ClaimAllArgs(options::OPT_no_canonical_prefixes);
322f4a2713aSLionel Sambuc 
323f4a2713aSLionel Sambuc   // Ignore -pipe.
324f4a2713aSLionel Sambuc   Args->ClaimAllArgs(options::OPT_pipe);
325f4a2713aSLionel Sambuc 
326f4a2713aSLionel Sambuc   // Extract -ccc args.
327f4a2713aSLionel Sambuc   //
328f4a2713aSLionel Sambuc   // FIXME: We need to figure out where this behavior should live. Most of it
329f4a2713aSLionel Sambuc   // should be outside in the client; the parts that aren't should have proper
330f4a2713aSLionel Sambuc   // options, either by introducing new ones or by overloading gcc ones like -V
331f4a2713aSLionel Sambuc   // or -b.
332f4a2713aSLionel Sambuc   CCCPrintActions = Args->hasArg(options::OPT_ccc_print_phases);
333f4a2713aSLionel Sambuc   CCCPrintBindings = Args->hasArg(options::OPT_ccc_print_bindings);
334f4a2713aSLionel Sambuc   if (const Arg *A = Args->getLastArg(options::OPT_ccc_gcc_name))
335f4a2713aSLionel Sambuc     CCCGenericGCCName = A->getValue();
336f4a2713aSLionel Sambuc   CCCUsePCH = Args->hasFlag(options::OPT_ccc_pch_is_pch,
337f4a2713aSLionel Sambuc                             options::OPT_ccc_pch_is_pth);
338f4a2713aSLionel Sambuc   // FIXME: DefaultTargetTriple is used by the target-prefixed calls to as/ld
339f4a2713aSLionel Sambuc   // and getToolChain is const.
340f4a2713aSLionel Sambuc   if (IsCLMode()) {
341*0a6a1f1dSLionel Sambuc     // clang-cl targets MSVC-style Win32.
342f4a2713aSLionel Sambuc     llvm::Triple T(DefaultTargetTriple);
343*0a6a1f1dSLionel Sambuc     T.setOS(llvm::Triple::Win32);
344*0a6a1f1dSLionel Sambuc     T.setEnvironment(llvm::Triple::MSVC);
345f4a2713aSLionel Sambuc     DefaultTargetTriple = T.str();
346f4a2713aSLionel Sambuc   }
347f4a2713aSLionel Sambuc   if (const Arg *A = Args->getLastArg(options::OPT_target))
348f4a2713aSLionel Sambuc     DefaultTargetTriple = A->getValue();
349f4a2713aSLionel Sambuc   if (const Arg *A = Args->getLastArg(options::OPT_ccc_install_dir))
350f4a2713aSLionel Sambuc     Dir = InstalledDir = A->getValue();
351f4a2713aSLionel Sambuc   for (arg_iterator it = Args->filtered_begin(options::OPT_B),
352f4a2713aSLionel Sambuc          ie = Args->filtered_end(); it != ie; ++it) {
353f4a2713aSLionel Sambuc     const Arg *A = *it;
354f4a2713aSLionel Sambuc     A->claim();
355f4a2713aSLionel Sambuc     PrefixDirs.push_back(A->getValue(0));
356f4a2713aSLionel Sambuc   }
357f4a2713aSLionel Sambuc   if (const Arg *A = Args->getLastArg(options::OPT__sysroot_EQ))
358f4a2713aSLionel Sambuc     SysRoot = A->getValue();
359f4a2713aSLionel Sambuc   if (const Arg *A = Args->getLastArg(options::OPT__dyld_prefix_EQ))
360f4a2713aSLionel Sambuc     DyldPrefix = A->getValue();
361f4a2713aSLionel Sambuc   if (Args->hasArg(options::OPT_nostdlib))
362f4a2713aSLionel Sambuc     UseStdLib = false;
363f4a2713aSLionel Sambuc 
364f4a2713aSLionel Sambuc   if (const Arg *A = Args->getLastArg(options::OPT_resource_dir))
365f4a2713aSLionel Sambuc     ResourceDir = A->getValue();
366f4a2713aSLionel Sambuc 
367f4a2713aSLionel Sambuc   // Perform the default argument translations.
368f4a2713aSLionel Sambuc   DerivedArgList *TranslatedArgs = TranslateInputArgs(*Args);
369f4a2713aSLionel Sambuc 
370f4a2713aSLionel Sambuc   // Owned by the host.
371f4a2713aSLionel Sambuc   const ToolChain &TC = getToolChain(*Args);
372f4a2713aSLionel Sambuc 
373f4a2713aSLionel Sambuc   // The compilation takes ownership of Args.
374f4a2713aSLionel Sambuc   Compilation *C = new Compilation(*this, TC, Args, TranslatedArgs);
375f4a2713aSLionel Sambuc 
376f4a2713aSLionel Sambuc   if (!HandleImmediateArgs(*C))
377f4a2713aSLionel Sambuc     return C;
378f4a2713aSLionel Sambuc 
379f4a2713aSLionel Sambuc   // Construct the list of inputs.
380f4a2713aSLionel Sambuc   InputList Inputs;
381f4a2713aSLionel Sambuc   BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs);
382f4a2713aSLionel Sambuc 
383f4a2713aSLionel Sambuc   // Construct the list of abstract actions to perform for this compilation. On
384*0a6a1f1dSLionel Sambuc   // MachO targets this uses the driver-driver and universal actions.
385*0a6a1f1dSLionel Sambuc   if (TC.getTriple().isOSBinFormatMachO())
386f4a2713aSLionel Sambuc     BuildUniversalActions(C->getDefaultToolChain(), C->getArgs(),
387f4a2713aSLionel Sambuc                           Inputs, C->getActions());
388f4a2713aSLionel Sambuc   else
389f4a2713aSLionel Sambuc     BuildActions(C->getDefaultToolChain(), C->getArgs(), Inputs,
390f4a2713aSLionel Sambuc                  C->getActions());
391f4a2713aSLionel Sambuc 
392f4a2713aSLionel Sambuc   if (CCCPrintActions) {
393f4a2713aSLionel Sambuc     PrintActions(*C);
394f4a2713aSLionel Sambuc     return C;
395f4a2713aSLionel Sambuc   }
396f4a2713aSLionel Sambuc 
397f4a2713aSLionel Sambuc   BuildJobs(*C);
398f4a2713aSLionel Sambuc 
399f4a2713aSLionel Sambuc   return C;
400f4a2713aSLionel Sambuc }
401f4a2713aSLionel Sambuc 
402f4a2713aSLionel Sambuc // When clang crashes, produce diagnostic information including the fully
403f4a2713aSLionel Sambuc // preprocessed source file(s).  Request that the developer attach the
404f4a2713aSLionel Sambuc // diagnostic information to a bug report.
generateCompilationDiagnostics(Compilation & C,const Command & FailingCommand)405f4a2713aSLionel Sambuc void Driver::generateCompilationDiagnostics(Compilation &C,
406*0a6a1f1dSLionel Sambuc                                             const Command &FailingCommand) {
407f4a2713aSLionel Sambuc   if (C.getArgs().hasArg(options::OPT_fno_crash_diagnostics))
408f4a2713aSLionel Sambuc     return;
409f4a2713aSLionel Sambuc 
410f4a2713aSLionel Sambuc   // Don't try to generate diagnostics for link or dsymutil jobs.
411*0a6a1f1dSLionel Sambuc   if (FailingCommand.getCreator().isLinkJob() ||
412*0a6a1f1dSLionel Sambuc       FailingCommand.getCreator().isDsymutilJob())
413f4a2713aSLionel Sambuc     return;
414f4a2713aSLionel Sambuc 
415f4a2713aSLionel Sambuc   // Print the version of the compiler.
416f4a2713aSLionel Sambuc   PrintVersion(C, llvm::errs());
417f4a2713aSLionel Sambuc 
418f4a2713aSLionel Sambuc   Diag(clang::diag::note_drv_command_failed_diag_msg)
419f4a2713aSLionel Sambuc     << "PLEASE submit a bug report to " BUG_REPORT_URL " and include the "
420f4a2713aSLionel Sambuc     "crash backtrace, preprocessed source, and associated run script.";
421f4a2713aSLionel Sambuc 
422f4a2713aSLionel Sambuc   // Suppress driver output and emit preprocessor output to temp file.
423f4a2713aSLionel Sambuc   Mode = CPPMode;
424f4a2713aSLionel Sambuc   CCGenDiagnostics = true;
425f4a2713aSLionel Sambuc 
426f4a2713aSLionel Sambuc   // Save the original job command(s).
427*0a6a1f1dSLionel Sambuc   Command Cmd = FailingCommand;
428f4a2713aSLionel Sambuc 
429f4a2713aSLionel Sambuc   // Keep track of whether we produce any errors while trying to produce
430f4a2713aSLionel Sambuc   // preprocessed sources.
431f4a2713aSLionel Sambuc   DiagnosticErrorTrap Trap(Diags);
432f4a2713aSLionel Sambuc 
433f4a2713aSLionel Sambuc   // Suppress tool output.
434f4a2713aSLionel Sambuc   C.initCompilationForDiagnostics();
435f4a2713aSLionel Sambuc 
436f4a2713aSLionel Sambuc   // Construct the list of inputs.
437f4a2713aSLionel Sambuc   InputList Inputs;
438f4a2713aSLionel Sambuc   BuildInputs(C.getDefaultToolChain(), C.getArgs(), Inputs);
439f4a2713aSLionel Sambuc 
440f4a2713aSLionel Sambuc   for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) {
441f4a2713aSLionel Sambuc     bool IgnoreInput = false;
442f4a2713aSLionel Sambuc 
443f4a2713aSLionel Sambuc     // Ignore input from stdin or any inputs that cannot be preprocessed.
444*0a6a1f1dSLionel Sambuc     // Check type first as not all linker inputs have a value.
445*0a6a1f1dSLionel Sambuc    if (types::getPreprocessedType(it->first) == types::TY_INVALID) {
446*0a6a1f1dSLionel Sambuc       IgnoreInput = true;
447*0a6a1f1dSLionel Sambuc     } else if (!strcmp(it->second->getValue(), "-")) {
448f4a2713aSLionel Sambuc       Diag(clang::diag::note_drv_command_failed_diag_msg)
449f4a2713aSLionel Sambuc         << "Error generating preprocessed source(s) - ignoring input from stdin"
450f4a2713aSLionel Sambuc         ".";
451f4a2713aSLionel Sambuc       IgnoreInput = true;
452f4a2713aSLionel Sambuc     }
453f4a2713aSLionel Sambuc 
454f4a2713aSLionel Sambuc     if (IgnoreInput) {
455f4a2713aSLionel Sambuc       it = Inputs.erase(it);
456f4a2713aSLionel Sambuc       ie = Inputs.end();
457f4a2713aSLionel Sambuc     } else {
458f4a2713aSLionel Sambuc       ++it;
459f4a2713aSLionel Sambuc     }
460f4a2713aSLionel Sambuc   }
461f4a2713aSLionel Sambuc 
462f4a2713aSLionel Sambuc   if (Inputs.empty()) {
463f4a2713aSLionel Sambuc     Diag(clang::diag::note_drv_command_failed_diag_msg)
464f4a2713aSLionel Sambuc       << "Error generating preprocessed source(s) - no preprocessable inputs.";
465f4a2713aSLionel Sambuc     return;
466f4a2713aSLionel Sambuc   }
467f4a2713aSLionel Sambuc 
468f4a2713aSLionel Sambuc   // Don't attempt to generate preprocessed files if multiple -arch options are
469f4a2713aSLionel Sambuc   // used, unless they're all duplicates.
470f4a2713aSLionel Sambuc   llvm::StringSet<> ArchNames;
471*0a6a1f1dSLionel Sambuc   for (const Arg *A : C.getArgs()) {
472f4a2713aSLionel Sambuc     if (A->getOption().matches(options::OPT_arch)) {
473f4a2713aSLionel Sambuc       StringRef ArchName = A->getValue();
474f4a2713aSLionel Sambuc       ArchNames.insert(ArchName);
475f4a2713aSLionel Sambuc     }
476f4a2713aSLionel Sambuc   }
477f4a2713aSLionel Sambuc   if (ArchNames.size() > 1) {
478f4a2713aSLionel Sambuc     Diag(clang::diag::note_drv_command_failed_diag_msg)
479f4a2713aSLionel Sambuc       << "Error generating preprocessed source(s) - cannot generate "
480f4a2713aSLionel Sambuc       "preprocessed source with multiple -arch options.";
481f4a2713aSLionel Sambuc     return;
482f4a2713aSLionel Sambuc   }
483f4a2713aSLionel Sambuc 
484f4a2713aSLionel Sambuc   // Construct the list of abstract actions to perform for this compilation. On
485f4a2713aSLionel Sambuc   // Darwin OSes this uses the driver-driver and builds universal actions.
486f4a2713aSLionel Sambuc   const ToolChain &TC = C.getDefaultToolChain();
487*0a6a1f1dSLionel Sambuc   if (TC.getTriple().isOSBinFormatMachO())
488f4a2713aSLionel Sambuc     BuildUniversalActions(TC, C.getArgs(), Inputs, C.getActions());
489f4a2713aSLionel Sambuc   else
490f4a2713aSLionel Sambuc     BuildActions(TC, C.getArgs(), Inputs, C.getActions());
491f4a2713aSLionel Sambuc 
492f4a2713aSLionel Sambuc   BuildJobs(C);
493f4a2713aSLionel Sambuc 
494f4a2713aSLionel Sambuc   // If there were errors building the compilation, quit now.
495f4a2713aSLionel Sambuc   if (Trap.hasErrorOccurred()) {
496f4a2713aSLionel Sambuc     Diag(clang::diag::note_drv_command_failed_diag_msg)
497f4a2713aSLionel Sambuc       << "Error generating preprocessed source(s).";
498f4a2713aSLionel Sambuc     return;
499f4a2713aSLionel Sambuc   }
500f4a2713aSLionel Sambuc 
501f4a2713aSLionel Sambuc   // Generate preprocessed output.
502f4a2713aSLionel Sambuc   SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
503f4a2713aSLionel Sambuc   C.ExecuteJob(C.getJobs(), FailingCommands);
504f4a2713aSLionel Sambuc 
505*0a6a1f1dSLionel Sambuc   // If any of the preprocessing commands failed, clean up and exit.
506*0a6a1f1dSLionel Sambuc   if (!FailingCommands.empty()) {
507f4a2713aSLionel Sambuc     if (!C.getArgs().hasArg(options::OPT_save_temps))
508f4a2713aSLionel Sambuc       C.CleanupFileList(C.getTempFiles(), true);
509f4a2713aSLionel Sambuc 
510f4a2713aSLionel Sambuc     Diag(clang::diag::note_drv_command_failed_diag_msg)
511f4a2713aSLionel Sambuc       << "Error generating preprocessed source(s).";
512*0a6a1f1dSLionel Sambuc     return;
513*0a6a1f1dSLionel Sambuc   }
514*0a6a1f1dSLionel Sambuc 
515*0a6a1f1dSLionel Sambuc   const ArgStringList &TempFiles = C.getTempFiles();
516*0a6a1f1dSLionel Sambuc   if (TempFiles.empty()) {
517*0a6a1f1dSLionel Sambuc     Diag(clang::diag::note_drv_command_failed_diag_msg)
518*0a6a1f1dSLionel Sambuc       << "Error generating preprocessed source(s).";
519*0a6a1f1dSLionel Sambuc     return;
520*0a6a1f1dSLionel Sambuc   }
521*0a6a1f1dSLionel Sambuc 
522*0a6a1f1dSLionel Sambuc   Diag(clang::diag::note_drv_command_failed_diag_msg)
523*0a6a1f1dSLionel Sambuc       << "\n********************\n\n"
524*0a6a1f1dSLionel Sambuc          "PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:\n"
525*0a6a1f1dSLionel Sambuc          "Preprocessed source(s) and associated run script(s) are located at:";
526*0a6a1f1dSLionel Sambuc 
527*0a6a1f1dSLionel Sambuc   SmallString<128> VFS;
528*0a6a1f1dSLionel Sambuc   for (const char *TempFile : TempFiles) {
529*0a6a1f1dSLionel Sambuc     Diag(clang::diag::note_drv_command_failed_diag_msg) << TempFile;
530*0a6a1f1dSLionel Sambuc     if (StringRef(TempFile).endswith(".cache")) {
531*0a6a1f1dSLionel Sambuc       // In some cases (modules) we'll dump extra data to help with reproducing
532*0a6a1f1dSLionel Sambuc       // the crash into a directory next to the output.
533*0a6a1f1dSLionel Sambuc       VFS = llvm::sys::path::filename(TempFile);
534*0a6a1f1dSLionel Sambuc       llvm::sys::path::append(VFS, "vfs", "vfs.yaml");
535f4a2713aSLionel Sambuc     }
536f4a2713aSLionel Sambuc   }
537f4a2713aSLionel Sambuc 
538*0a6a1f1dSLionel Sambuc   // Assume associated files are based off of the first temporary file.
539*0a6a1f1dSLionel Sambuc   CrashReportInfo CrashInfo(TempFiles[0], VFS);
540*0a6a1f1dSLionel Sambuc 
541*0a6a1f1dSLionel Sambuc   std::string Script = CrashInfo.Filename.rsplit('.').first.str() + ".sh";
542*0a6a1f1dSLionel Sambuc   std::error_code EC;
543*0a6a1f1dSLionel Sambuc   llvm::raw_fd_ostream ScriptOS(Script, EC, llvm::sys::fs::F_Excl);
544*0a6a1f1dSLionel Sambuc   if (EC) {
545*0a6a1f1dSLionel Sambuc     Diag(clang::diag::note_drv_command_failed_diag_msg)
546*0a6a1f1dSLionel Sambuc         << "Error generating run script: " + Script + " " + EC.message();
547*0a6a1f1dSLionel Sambuc   } else {
548*0a6a1f1dSLionel Sambuc     Cmd.Print(ScriptOS, "\n", /*Quote=*/true, &CrashInfo);
549*0a6a1f1dSLionel Sambuc     Diag(clang::diag::note_drv_command_failed_diag_msg) << Script;
550*0a6a1f1dSLionel Sambuc   }
551*0a6a1f1dSLionel Sambuc 
552*0a6a1f1dSLionel Sambuc   for (const auto &A : C.getArgs().filtered(options::OPT_frewrite_map_file,
553*0a6a1f1dSLionel Sambuc                                             options::OPT_frewrite_map_file_EQ))
554*0a6a1f1dSLionel Sambuc     Diag(clang::diag::note_drv_command_failed_diag_msg) << A->getValue();
555*0a6a1f1dSLionel Sambuc 
556*0a6a1f1dSLionel Sambuc   Diag(clang::diag::note_drv_command_failed_diag_msg)
557*0a6a1f1dSLionel Sambuc       << "\n\n********************";
558*0a6a1f1dSLionel Sambuc }
559*0a6a1f1dSLionel Sambuc 
setUpResponseFiles(Compilation & C,Job & J)560*0a6a1f1dSLionel Sambuc void Driver::setUpResponseFiles(Compilation &C, Job &J) {
561*0a6a1f1dSLionel Sambuc   if (JobList *Jobs = dyn_cast<JobList>(&J)) {
562*0a6a1f1dSLionel Sambuc     for (auto &Job : *Jobs)
563*0a6a1f1dSLionel Sambuc       setUpResponseFiles(C, Job);
564*0a6a1f1dSLionel Sambuc     return;
565*0a6a1f1dSLionel Sambuc   }
566*0a6a1f1dSLionel Sambuc 
567*0a6a1f1dSLionel Sambuc   Command *CurCommand = dyn_cast<Command>(&J);
568*0a6a1f1dSLionel Sambuc   if (!CurCommand)
569*0a6a1f1dSLionel Sambuc     return;
570*0a6a1f1dSLionel Sambuc 
571*0a6a1f1dSLionel Sambuc   // Since argumentsFitWithinSystemLimits() may underestimate system's capacity
572*0a6a1f1dSLionel Sambuc   // if the tool does not support response files, there is a chance/ that things
573*0a6a1f1dSLionel Sambuc   // will just work without a response file, so we silently just skip it.
574*0a6a1f1dSLionel Sambuc   if (CurCommand->getCreator().getResponseFilesSupport() == Tool::RF_None ||
575*0a6a1f1dSLionel Sambuc       llvm::sys::argumentsFitWithinSystemLimits(CurCommand->getArguments()))
576*0a6a1f1dSLionel Sambuc     return;
577*0a6a1f1dSLionel Sambuc 
578*0a6a1f1dSLionel Sambuc   std::string TmpName = GetTemporaryPath("response", "txt");
579*0a6a1f1dSLionel Sambuc   CurCommand->setResponseFile(C.addTempFile(C.getArgs().MakeArgString(
580*0a6a1f1dSLionel Sambuc       TmpName.c_str())));
581*0a6a1f1dSLionel Sambuc }
582*0a6a1f1dSLionel Sambuc 
ExecuteCompilation(Compilation & C,SmallVectorImpl<std::pair<int,const Command * >> & FailingCommands)583*0a6a1f1dSLionel Sambuc int Driver::ExecuteCompilation(Compilation &C,
584*0a6a1f1dSLionel Sambuc     SmallVectorImpl< std::pair<int, const Command *> > &FailingCommands) {
585f4a2713aSLionel Sambuc   // Just print if -### was present.
586f4a2713aSLionel Sambuc   if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
587f4a2713aSLionel Sambuc     C.getJobs().Print(llvm::errs(), "\n", true);
588f4a2713aSLionel Sambuc     return 0;
589f4a2713aSLionel Sambuc   }
590f4a2713aSLionel Sambuc 
591f4a2713aSLionel Sambuc   // If there were errors building the compilation, quit now.
592f4a2713aSLionel Sambuc   if (Diags.hasErrorOccurred())
593f4a2713aSLionel Sambuc     return 1;
594f4a2713aSLionel Sambuc 
595*0a6a1f1dSLionel Sambuc   // Set up response file names for each command, if necessary
596*0a6a1f1dSLionel Sambuc   setUpResponseFiles(C, C.getJobs());
597*0a6a1f1dSLionel Sambuc 
598f4a2713aSLionel Sambuc   C.ExecuteJob(C.getJobs(), FailingCommands);
599f4a2713aSLionel Sambuc 
600f4a2713aSLionel Sambuc   // Remove temp files.
601f4a2713aSLionel Sambuc   C.CleanupFileList(C.getTempFiles());
602f4a2713aSLionel Sambuc 
603f4a2713aSLionel Sambuc   // If the command succeeded, we are done.
604f4a2713aSLionel Sambuc   if (FailingCommands.empty())
605f4a2713aSLionel Sambuc     return 0;
606f4a2713aSLionel Sambuc 
607f4a2713aSLionel Sambuc   // Otherwise, remove result files and print extra information about abnormal
608f4a2713aSLionel Sambuc   // failures.
609f4a2713aSLionel Sambuc   for (SmallVectorImpl< std::pair<int, const Command *> >::iterator it =
610f4a2713aSLionel Sambuc          FailingCommands.begin(), ie = FailingCommands.end(); it != ie; ++it) {
611f4a2713aSLionel Sambuc     int Res = it->first;
612f4a2713aSLionel Sambuc     const Command *FailingCommand = it->second;
613f4a2713aSLionel Sambuc 
614f4a2713aSLionel Sambuc     // Remove result files if we're not saving temps.
615f4a2713aSLionel Sambuc     if (!C.getArgs().hasArg(options::OPT_save_temps)) {
616f4a2713aSLionel Sambuc       const JobAction *JA = cast<JobAction>(&FailingCommand->getSource());
617f4a2713aSLionel Sambuc       C.CleanupFileMap(C.getResultFiles(), JA, true);
618f4a2713aSLionel Sambuc 
619f4a2713aSLionel Sambuc       // Failure result files are valid unless we crashed.
620f4a2713aSLionel Sambuc       if (Res < 0)
621f4a2713aSLionel Sambuc         C.CleanupFileMap(C.getFailureResultFiles(), JA, true);
622f4a2713aSLionel Sambuc     }
623f4a2713aSLionel Sambuc 
624f4a2713aSLionel Sambuc     // Print extra information about abnormal failures, if possible.
625f4a2713aSLionel Sambuc     //
626f4a2713aSLionel Sambuc     // This is ad-hoc, but we don't want to be excessively noisy. If the result
627f4a2713aSLionel Sambuc     // status was 1, assume the command failed normally. In particular, if it
628f4a2713aSLionel Sambuc     // was the compiler then assume it gave a reasonable error code. Failures
629f4a2713aSLionel Sambuc     // in other tools are less common, and they generally have worse
630f4a2713aSLionel Sambuc     // diagnostics, so always print the diagnostic there.
631f4a2713aSLionel Sambuc     const Tool &FailingTool = FailingCommand->getCreator();
632f4a2713aSLionel Sambuc 
633f4a2713aSLionel Sambuc     if (!FailingCommand->getCreator().hasGoodDiagnostics() || Res != 1) {
634f4a2713aSLionel Sambuc       // FIXME: See FIXME above regarding result code interpretation.
635f4a2713aSLionel Sambuc       if (Res < 0)
636f4a2713aSLionel Sambuc         Diag(clang::diag::err_drv_command_signalled)
637f4a2713aSLionel Sambuc           << FailingTool.getShortName();
638f4a2713aSLionel Sambuc       else
639f4a2713aSLionel Sambuc         Diag(clang::diag::err_drv_command_failed)
640f4a2713aSLionel Sambuc           << FailingTool.getShortName() << Res;
641f4a2713aSLionel Sambuc     }
642f4a2713aSLionel Sambuc   }
643f4a2713aSLionel Sambuc   return 0;
644f4a2713aSLionel Sambuc }
645f4a2713aSLionel Sambuc 
PrintHelp(bool ShowHidden) const646f4a2713aSLionel Sambuc void Driver::PrintHelp(bool ShowHidden) const {
647f4a2713aSLionel Sambuc   unsigned IncludedFlagsBitmask;
648f4a2713aSLionel Sambuc   unsigned ExcludedFlagsBitmask;
649*0a6a1f1dSLionel Sambuc   std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) =
650f4a2713aSLionel Sambuc     getIncludeExcludeOptionFlagMasks();
651f4a2713aSLionel Sambuc 
652f4a2713aSLionel Sambuc   ExcludedFlagsBitmask |= options::NoDriverOption;
653f4a2713aSLionel Sambuc   if (!ShowHidden)
654f4a2713aSLionel Sambuc     ExcludedFlagsBitmask |= HelpHidden;
655f4a2713aSLionel Sambuc 
656f4a2713aSLionel Sambuc   getOpts().PrintHelp(llvm::outs(), Name.c_str(), DriverTitle.c_str(),
657f4a2713aSLionel Sambuc                       IncludedFlagsBitmask, ExcludedFlagsBitmask);
658f4a2713aSLionel Sambuc }
659f4a2713aSLionel Sambuc 
PrintVersion(const Compilation & C,raw_ostream & OS) const660f4a2713aSLionel Sambuc void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const {
661f4a2713aSLionel Sambuc   // FIXME: The following handlers should use a callback mechanism, we don't
662f4a2713aSLionel Sambuc   // know what the client would like to do.
663f4a2713aSLionel Sambuc   OS << getClangFullVersion() << '\n';
664f4a2713aSLionel Sambuc   const ToolChain &TC = C.getDefaultToolChain();
665f4a2713aSLionel Sambuc   OS << "Target: " << TC.getTripleString() << '\n';
666f4a2713aSLionel Sambuc 
667f4a2713aSLionel Sambuc   // Print the threading model.
668*0a6a1f1dSLionel Sambuc   if (Arg *A = C.getArgs().getLastArg(options::OPT_mthread_model)) {
669*0a6a1f1dSLionel Sambuc     // Don't print if the ToolChain would have barfed on it already
670*0a6a1f1dSLionel Sambuc     if (TC.isThreadModelSupported(A->getValue()))
671*0a6a1f1dSLionel Sambuc       OS << "Thread model: " << A->getValue();
672*0a6a1f1dSLionel Sambuc   } else
673*0a6a1f1dSLionel Sambuc     OS << "Thread model: " << TC.getThreadModel();
674*0a6a1f1dSLionel Sambuc   OS << '\n';
675f4a2713aSLionel Sambuc }
676f4a2713aSLionel Sambuc 
677f4a2713aSLionel Sambuc /// PrintDiagnosticCategories - Implement the --print-diagnostic-categories
678f4a2713aSLionel Sambuc /// option.
PrintDiagnosticCategories(raw_ostream & OS)679f4a2713aSLionel Sambuc static void PrintDiagnosticCategories(raw_ostream &OS) {
680f4a2713aSLionel Sambuc   // Skip the empty category.
681f4a2713aSLionel Sambuc   for (unsigned i = 1, max = DiagnosticIDs::getNumberOfCategories();
682f4a2713aSLionel Sambuc        i != max; ++i)
683f4a2713aSLionel Sambuc     OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(i) << '\n';
684f4a2713aSLionel Sambuc }
685f4a2713aSLionel Sambuc 
HandleImmediateArgs(const Compilation & C)686f4a2713aSLionel Sambuc bool Driver::HandleImmediateArgs(const Compilation &C) {
687f4a2713aSLionel Sambuc   // The order these options are handled in gcc is all over the place, but we
688f4a2713aSLionel Sambuc   // don't expect inconsistencies w.r.t. that to matter in practice.
689f4a2713aSLionel Sambuc 
690f4a2713aSLionel Sambuc   if (C.getArgs().hasArg(options::OPT_dumpmachine)) {
691f4a2713aSLionel Sambuc     llvm::outs() << C.getDefaultToolChain().getTripleString() << '\n';
692f4a2713aSLionel Sambuc     return false;
693f4a2713aSLionel Sambuc   }
694f4a2713aSLionel Sambuc 
695f4a2713aSLionel Sambuc   if (C.getArgs().hasArg(options::OPT_dumpversion)) {
696f4a2713aSLionel Sambuc     // Since -dumpversion is only implemented for pedantic GCC compatibility, we
697f4a2713aSLionel Sambuc     // return an answer which matches our definition of __VERSION__.
698f4a2713aSLionel Sambuc     //
699f4a2713aSLionel Sambuc     // If we want to return a more correct answer some day, then we should
700f4a2713aSLionel Sambuc     // introduce a non-pedantically GCC compatible mode to Clang in which we
701f4a2713aSLionel Sambuc     // provide sensible definitions for -dumpversion, __VERSION__, etc.
702f4a2713aSLionel Sambuc     llvm::outs() << "4.2.1\n";
703f4a2713aSLionel Sambuc     return false;
704f4a2713aSLionel Sambuc   }
705f4a2713aSLionel Sambuc 
706f4a2713aSLionel Sambuc   if (C.getArgs().hasArg(options::OPT__print_diagnostic_categories)) {
707f4a2713aSLionel Sambuc     PrintDiagnosticCategories(llvm::outs());
708f4a2713aSLionel Sambuc     return false;
709f4a2713aSLionel Sambuc   }
710f4a2713aSLionel Sambuc 
711f4a2713aSLionel Sambuc   if (C.getArgs().hasArg(options::OPT_help) ||
712f4a2713aSLionel Sambuc       C.getArgs().hasArg(options::OPT__help_hidden)) {
713f4a2713aSLionel Sambuc     PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
714f4a2713aSLionel Sambuc     return false;
715f4a2713aSLionel Sambuc   }
716f4a2713aSLionel Sambuc 
717f4a2713aSLionel Sambuc   if (C.getArgs().hasArg(options::OPT__version)) {
718f4a2713aSLionel Sambuc     // Follow gcc behavior and use stdout for --version and stderr for -v.
719f4a2713aSLionel Sambuc     PrintVersion(C, llvm::outs());
720f4a2713aSLionel Sambuc     return false;
721f4a2713aSLionel Sambuc   }
722f4a2713aSLionel Sambuc 
723f4a2713aSLionel Sambuc   if (C.getArgs().hasArg(options::OPT_v) ||
724f4a2713aSLionel Sambuc       C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
725f4a2713aSLionel Sambuc     PrintVersion(C, llvm::errs());
726f4a2713aSLionel Sambuc     SuppressMissingInputWarning = true;
727f4a2713aSLionel Sambuc   }
728f4a2713aSLionel Sambuc 
729f4a2713aSLionel Sambuc   const ToolChain &TC = C.getDefaultToolChain();
730f4a2713aSLionel Sambuc 
731f4a2713aSLionel Sambuc   if (C.getArgs().hasArg(options::OPT_v))
732f4a2713aSLionel Sambuc     TC.printVerboseInfo(llvm::errs());
733f4a2713aSLionel Sambuc 
734f4a2713aSLionel Sambuc   if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
735f4a2713aSLionel Sambuc     llvm::outs() << "programs: =";
736f4a2713aSLionel Sambuc     for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
737f4a2713aSLionel Sambuc            ie = TC.getProgramPaths().end(); it != ie; ++it) {
738f4a2713aSLionel Sambuc       if (it != TC.getProgramPaths().begin())
739f4a2713aSLionel Sambuc         llvm::outs() << ':';
740f4a2713aSLionel Sambuc       llvm::outs() << *it;
741f4a2713aSLionel Sambuc     }
742f4a2713aSLionel Sambuc     llvm::outs() << "\n";
743f4a2713aSLionel Sambuc     llvm::outs() << "libraries: =" << ResourceDir;
744f4a2713aSLionel Sambuc 
745f4a2713aSLionel Sambuc     StringRef sysroot = C.getSysRoot();
746f4a2713aSLionel Sambuc 
747f4a2713aSLionel Sambuc     for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
748f4a2713aSLionel Sambuc            ie = TC.getFilePaths().end(); it != ie; ++it) {
749f4a2713aSLionel Sambuc       llvm::outs() << ':';
750f4a2713aSLionel Sambuc       const char *path = it->c_str();
751f4a2713aSLionel Sambuc       if (path[0] == '=')
752f4a2713aSLionel Sambuc         llvm::outs() << sysroot << path + 1;
753f4a2713aSLionel Sambuc       else
754f4a2713aSLionel Sambuc         llvm::outs() << path;
755f4a2713aSLionel Sambuc     }
756f4a2713aSLionel Sambuc     llvm::outs() << "\n";
757f4a2713aSLionel Sambuc     return false;
758f4a2713aSLionel Sambuc   }
759f4a2713aSLionel Sambuc 
760f4a2713aSLionel Sambuc   // FIXME: The following handlers should use a callback mechanism, we don't
761f4a2713aSLionel Sambuc   // know what the client would like to do.
762f4a2713aSLionel Sambuc   if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
763f4a2713aSLionel Sambuc     llvm::outs() << GetFilePath(A->getValue(), TC) << "\n";
764f4a2713aSLionel Sambuc     return false;
765f4a2713aSLionel Sambuc   }
766f4a2713aSLionel Sambuc 
767f4a2713aSLionel Sambuc   if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
768f4a2713aSLionel Sambuc     llvm::outs() << GetProgramPath(A->getValue(), TC) << "\n";
769f4a2713aSLionel Sambuc     return false;
770f4a2713aSLionel Sambuc   }
771f4a2713aSLionel Sambuc 
772f4a2713aSLionel Sambuc   if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
773f4a2713aSLionel Sambuc     llvm::outs() << GetFilePath("libgcc.a", TC) << "\n";
774f4a2713aSLionel Sambuc     return false;
775f4a2713aSLionel Sambuc   }
776f4a2713aSLionel Sambuc 
777f4a2713aSLionel Sambuc   if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
778*0a6a1f1dSLionel Sambuc     const MultilibSet &Multilibs = TC.getMultilibs();
779f4a2713aSLionel Sambuc 
780*0a6a1f1dSLionel Sambuc     for (MultilibSet::const_iterator I = Multilibs.begin(), E = Multilibs.end();
781*0a6a1f1dSLionel Sambuc          I != E; ++I) {
782*0a6a1f1dSLionel Sambuc       llvm::outs() << *I << "\n";
783f4a2713aSLionel Sambuc     }
784f4a2713aSLionel Sambuc     return false;
785f4a2713aSLionel Sambuc   }
786f4a2713aSLionel Sambuc 
787*0a6a1f1dSLionel Sambuc   if (C.getArgs().hasArg(options::OPT_print_multi_directory)) {
788*0a6a1f1dSLionel Sambuc     const MultilibSet &Multilibs = TC.getMultilibs();
789*0a6a1f1dSLionel Sambuc     for (MultilibSet::const_iterator I = Multilibs.begin(), E = Multilibs.end();
790*0a6a1f1dSLionel Sambuc          I != E; ++I) {
791*0a6a1f1dSLionel Sambuc       if (I->gccSuffix().empty())
792*0a6a1f1dSLionel Sambuc         llvm::outs() << ".\n";
793*0a6a1f1dSLionel Sambuc       else {
794*0a6a1f1dSLionel Sambuc         StringRef Suffix(I->gccSuffix());
795*0a6a1f1dSLionel Sambuc         assert(Suffix.front() == '/');
796*0a6a1f1dSLionel Sambuc         llvm::outs() << Suffix.substr(1) << "\n";
797f4a2713aSLionel Sambuc       }
798*0a6a1f1dSLionel Sambuc     }
799*0a6a1f1dSLionel Sambuc     return false;
800*0a6a1f1dSLionel Sambuc   }
801*0a6a1f1dSLionel Sambuc 
802*0a6a1f1dSLionel Sambuc   if (C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
803*0a6a1f1dSLionel Sambuc     // FIXME: This should print out "lib/../lib", "lib/../lib64", or
804*0a6a1f1dSLionel Sambuc     // "lib/../lib32" as appropriate for the toolchain. For now, print
805*0a6a1f1dSLionel Sambuc     // nothing because it's not supported yet.
806f4a2713aSLionel Sambuc     return false;
807f4a2713aSLionel Sambuc   }
808f4a2713aSLionel Sambuc 
809f4a2713aSLionel Sambuc   return true;
810f4a2713aSLionel Sambuc }
811f4a2713aSLionel Sambuc 
PrintActions1(const Compilation & C,Action * A,std::map<Action *,unsigned> & Ids)812f4a2713aSLionel Sambuc static unsigned PrintActions1(const Compilation &C, Action *A,
813f4a2713aSLionel Sambuc                               std::map<Action*, unsigned> &Ids) {
814f4a2713aSLionel Sambuc   if (Ids.count(A))
815f4a2713aSLionel Sambuc     return Ids[A];
816f4a2713aSLionel Sambuc 
817f4a2713aSLionel Sambuc   std::string str;
818f4a2713aSLionel Sambuc   llvm::raw_string_ostream os(str);
819f4a2713aSLionel Sambuc 
820f4a2713aSLionel Sambuc   os << Action::getClassName(A->getKind()) << ", ";
821f4a2713aSLionel Sambuc   if (InputAction *IA = dyn_cast<InputAction>(A)) {
822f4a2713aSLionel Sambuc     os << "\"" << IA->getInputArg().getValue() << "\"";
823f4a2713aSLionel Sambuc   } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
824f4a2713aSLionel Sambuc     os << '"' << BIA->getArchName() << '"'
825f4a2713aSLionel Sambuc        << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
826f4a2713aSLionel Sambuc   } else {
827f4a2713aSLionel Sambuc     os << "{";
828f4a2713aSLionel Sambuc     for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
829f4a2713aSLionel Sambuc       os << PrintActions1(C, *it, Ids);
830f4a2713aSLionel Sambuc       ++it;
831f4a2713aSLionel Sambuc       if (it != ie)
832f4a2713aSLionel Sambuc         os << ", ";
833f4a2713aSLionel Sambuc     }
834f4a2713aSLionel Sambuc     os << "}";
835f4a2713aSLionel Sambuc   }
836f4a2713aSLionel Sambuc 
837f4a2713aSLionel Sambuc   unsigned Id = Ids.size();
838f4a2713aSLionel Sambuc   Ids[A] = Id;
839f4a2713aSLionel Sambuc   llvm::errs() << Id << ": " << os.str() << ", "
840f4a2713aSLionel Sambuc                << types::getTypeName(A->getType()) << "\n";
841f4a2713aSLionel Sambuc 
842f4a2713aSLionel Sambuc   return Id;
843f4a2713aSLionel Sambuc }
844f4a2713aSLionel Sambuc 
PrintActions(const Compilation & C) const845f4a2713aSLionel Sambuc void Driver::PrintActions(const Compilation &C) const {
846f4a2713aSLionel Sambuc   std::map<Action*, unsigned> Ids;
847f4a2713aSLionel Sambuc   for (ActionList::const_iterator it = C.getActions().begin(),
848f4a2713aSLionel Sambuc          ie = C.getActions().end(); it != ie; ++it)
849f4a2713aSLionel Sambuc     PrintActions1(C, *it, Ids);
850f4a2713aSLionel Sambuc }
851f4a2713aSLionel Sambuc 
852f4a2713aSLionel Sambuc /// \brief Check whether the given input tree contains any compilation or
853f4a2713aSLionel Sambuc /// assembly actions.
ContainsCompileOrAssembleAction(const Action * A)854f4a2713aSLionel Sambuc static bool ContainsCompileOrAssembleAction(const Action *A) {
855*0a6a1f1dSLionel Sambuc   if (isa<CompileJobAction>(A) ||
856*0a6a1f1dSLionel Sambuc       isa<BackendJobAction>(A) ||
857*0a6a1f1dSLionel Sambuc       isa<AssembleJobAction>(A))
858f4a2713aSLionel Sambuc     return true;
859f4a2713aSLionel Sambuc 
860f4a2713aSLionel Sambuc   for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
861f4a2713aSLionel Sambuc     if (ContainsCompileOrAssembleAction(*it))
862f4a2713aSLionel Sambuc       return true;
863f4a2713aSLionel Sambuc 
864f4a2713aSLionel Sambuc   return false;
865f4a2713aSLionel Sambuc }
866f4a2713aSLionel Sambuc 
BuildUniversalActions(const ToolChain & TC,DerivedArgList & Args,const InputList & BAInputs,ActionList & Actions) const867f4a2713aSLionel Sambuc void Driver::BuildUniversalActions(const ToolChain &TC,
868f4a2713aSLionel Sambuc                                    DerivedArgList &Args,
869f4a2713aSLionel Sambuc                                    const InputList &BAInputs,
870f4a2713aSLionel Sambuc                                    ActionList &Actions) const {
871f4a2713aSLionel Sambuc   llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
872f4a2713aSLionel Sambuc   // Collect the list of architectures. Duplicates are allowed, but should only
873f4a2713aSLionel Sambuc   // be handled once (in the order seen).
874f4a2713aSLionel Sambuc   llvm::StringSet<> ArchNames;
875f4a2713aSLionel Sambuc   SmallVector<const char *, 4> Archs;
876*0a6a1f1dSLionel Sambuc   for (Arg *A : Args) {
877f4a2713aSLionel Sambuc     if (A->getOption().matches(options::OPT_arch)) {
878f4a2713aSLionel Sambuc       // Validate the option here; we don't save the type here because its
879f4a2713aSLionel Sambuc       // particular spelling may participate in other driver choices.
880f4a2713aSLionel Sambuc       llvm::Triple::ArchType Arch =
881*0a6a1f1dSLionel Sambuc         tools::darwin::getArchTypeForMachOArchName(A->getValue());
882f4a2713aSLionel Sambuc       if (Arch == llvm::Triple::UnknownArch) {
883f4a2713aSLionel Sambuc         Diag(clang::diag::err_drv_invalid_arch_name)
884f4a2713aSLionel Sambuc           << A->getAsString(Args);
885f4a2713aSLionel Sambuc         continue;
886f4a2713aSLionel Sambuc       }
887f4a2713aSLionel Sambuc 
888f4a2713aSLionel Sambuc       A->claim();
889*0a6a1f1dSLionel Sambuc       if (ArchNames.insert(A->getValue()).second)
890f4a2713aSLionel Sambuc         Archs.push_back(A->getValue());
891f4a2713aSLionel Sambuc     }
892f4a2713aSLionel Sambuc   }
893f4a2713aSLionel Sambuc 
894f4a2713aSLionel Sambuc   // When there is no explicit arch for this platform, make sure we still bind
895f4a2713aSLionel Sambuc   // the architecture (to the default) so that -Xarch_ is handled correctly.
896f4a2713aSLionel Sambuc   if (!Archs.size())
897f4a2713aSLionel Sambuc     Archs.push_back(Args.MakeArgString(TC.getDefaultUniversalArchName()));
898f4a2713aSLionel Sambuc 
899f4a2713aSLionel Sambuc   ActionList SingleActions;
900f4a2713aSLionel Sambuc   BuildActions(TC, Args, BAInputs, SingleActions);
901f4a2713aSLionel Sambuc 
902f4a2713aSLionel Sambuc   // Add in arch bindings for every top level action, as well as lipo and
903f4a2713aSLionel Sambuc   // dsymutil steps if needed.
904f4a2713aSLionel Sambuc   for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
905f4a2713aSLionel Sambuc     Action *Act = SingleActions[i];
906f4a2713aSLionel Sambuc 
907f4a2713aSLionel Sambuc     // Make sure we can lipo this kind of output. If not (and it is an actual
908f4a2713aSLionel Sambuc     // output) then we disallow, since we can't create an output file with the
909f4a2713aSLionel Sambuc     // right name without overwriting it. We could remove this oddity by just
910f4a2713aSLionel Sambuc     // changing the output names to include the arch, which would also fix
911f4a2713aSLionel Sambuc     // -save-temps. Compatibility wins for now.
912f4a2713aSLionel Sambuc 
913f4a2713aSLionel Sambuc     if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
914f4a2713aSLionel Sambuc       Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
915f4a2713aSLionel Sambuc         << types::getTypeName(Act->getType());
916f4a2713aSLionel Sambuc 
917f4a2713aSLionel Sambuc     ActionList Inputs;
918f4a2713aSLionel Sambuc     for (unsigned i = 0, e = Archs.size(); i != e; ++i) {
919*0a6a1f1dSLionel Sambuc       Inputs.push_back(
920*0a6a1f1dSLionel Sambuc           new BindArchAction(std::unique_ptr<Action>(Act), Archs[i]));
921f4a2713aSLionel Sambuc       if (i != 0)
922f4a2713aSLionel Sambuc         Inputs.back()->setOwnsInputs(false);
923f4a2713aSLionel Sambuc     }
924f4a2713aSLionel Sambuc 
925f4a2713aSLionel Sambuc     // Lipo if necessary, we do it this way because we need to set the arch flag
926f4a2713aSLionel Sambuc     // so that -Xarch_ gets overwritten.
927f4a2713aSLionel Sambuc     if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
928f4a2713aSLionel Sambuc       Actions.append(Inputs.begin(), Inputs.end());
929f4a2713aSLionel Sambuc     else
930f4a2713aSLionel Sambuc       Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
931f4a2713aSLionel Sambuc 
932f4a2713aSLionel Sambuc     // Handle debug info queries.
933f4a2713aSLionel Sambuc     Arg *A = Args.getLastArg(options::OPT_g_Group);
934f4a2713aSLionel Sambuc     if (A && !A->getOption().matches(options::OPT_g0) &&
935f4a2713aSLionel Sambuc         !A->getOption().matches(options::OPT_gstabs) &&
936f4a2713aSLionel Sambuc         ContainsCompileOrAssembleAction(Actions.back())) {
937f4a2713aSLionel Sambuc 
938f4a2713aSLionel Sambuc       // Add a 'dsymutil' step if necessary, when debug info is enabled and we
939f4a2713aSLionel Sambuc       // have a compile input. We need to run 'dsymutil' ourselves in such cases
940f4a2713aSLionel Sambuc       // because the debug info will refer to a temporary object file which
941f4a2713aSLionel Sambuc       // will be removed at the end of the compilation process.
942f4a2713aSLionel Sambuc       if (Act->getType() == types::TY_Image) {
943f4a2713aSLionel Sambuc         ActionList Inputs;
944f4a2713aSLionel Sambuc         Inputs.push_back(Actions.back());
945f4a2713aSLionel Sambuc         Actions.pop_back();
946f4a2713aSLionel Sambuc         Actions.push_back(new DsymutilJobAction(Inputs, types::TY_dSYM));
947f4a2713aSLionel Sambuc       }
948f4a2713aSLionel Sambuc 
949*0a6a1f1dSLionel Sambuc       // Verify the debug info output.
950*0a6a1f1dSLionel Sambuc       if (Args.hasArg(options::OPT_verify_debug_info)) {
951*0a6a1f1dSLionel Sambuc         std::unique_ptr<Action> VerifyInput(Actions.back());
952f4a2713aSLionel Sambuc         Actions.pop_back();
953*0a6a1f1dSLionel Sambuc         Actions.push_back(new VerifyDebugInfoJobAction(std::move(VerifyInput),
954f4a2713aSLionel Sambuc                                                        types::TY_Nothing));
955f4a2713aSLionel Sambuc       }
956f4a2713aSLionel Sambuc     }
957f4a2713aSLionel Sambuc   }
958f4a2713aSLionel Sambuc }
959f4a2713aSLionel Sambuc 
960f4a2713aSLionel Sambuc /// \brief Check that the file referenced by Value exists. If it doesn't,
961f4a2713aSLionel Sambuc /// issue a diagnostic and return false.
DiagnoseInputExistence(const Driver & D,const DerivedArgList & Args,StringRef Value)962*0a6a1f1dSLionel Sambuc static bool DiagnoseInputExistence(const Driver &D, const DerivedArgList &Args,
963f4a2713aSLionel Sambuc                                    StringRef Value) {
964f4a2713aSLionel Sambuc   if (!D.getCheckInputsExist())
965f4a2713aSLionel Sambuc     return true;
966f4a2713aSLionel Sambuc 
967f4a2713aSLionel Sambuc   // stdin always exists.
968f4a2713aSLionel Sambuc   if (Value == "-")
969f4a2713aSLionel Sambuc     return true;
970f4a2713aSLionel Sambuc 
971f4a2713aSLionel Sambuc   SmallString<64> Path(Value);
972f4a2713aSLionel Sambuc   if (Arg *WorkDir = Args.getLastArg(options::OPT_working_directory)) {
973f4a2713aSLionel Sambuc     if (!llvm::sys::path::is_absolute(Path.str())) {
974f4a2713aSLionel Sambuc       SmallString<64> Directory(WorkDir->getValue());
975f4a2713aSLionel Sambuc       llvm::sys::path::append(Directory, Value);
976f4a2713aSLionel Sambuc       Path.assign(Directory);
977f4a2713aSLionel Sambuc     }
978f4a2713aSLionel Sambuc   }
979f4a2713aSLionel Sambuc 
980f4a2713aSLionel Sambuc   if (llvm::sys::fs::exists(Twine(Path)))
981f4a2713aSLionel Sambuc     return true;
982f4a2713aSLionel Sambuc 
983*0a6a1f1dSLionel Sambuc   if (D.IsCLMode() && llvm::sys::Process::FindInEnvPath("LIB", Value))
984*0a6a1f1dSLionel Sambuc     return true;
985*0a6a1f1dSLionel Sambuc 
986f4a2713aSLionel Sambuc   D.Diag(clang::diag::err_drv_no_such_file) << Path.str();
987f4a2713aSLionel Sambuc   return false;
988f4a2713aSLionel Sambuc }
989f4a2713aSLionel Sambuc 
990f4a2713aSLionel Sambuc // Construct a the list of inputs and their types.
BuildInputs(const ToolChain & TC,DerivedArgList & Args,InputList & Inputs) const991*0a6a1f1dSLionel Sambuc void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
992f4a2713aSLionel Sambuc                          InputList &Inputs) const {
993f4a2713aSLionel Sambuc   // Track the current user specified (-x) input. We also explicitly track the
994f4a2713aSLionel Sambuc   // argument used to set the type; we only want to claim the type when we
995f4a2713aSLionel Sambuc   // actually use it, so we warn about unused -x arguments.
996f4a2713aSLionel Sambuc   types::ID InputType = types::TY_Nothing;
997*0a6a1f1dSLionel Sambuc   Arg *InputTypeArg = nullptr;
998f4a2713aSLionel Sambuc 
999f4a2713aSLionel Sambuc   // The last /TC or /TP option sets the input type to C or C++ globally.
1000*0a6a1f1dSLionel Sambuc   if (Arg *TCTP = Args.getLastArgNoClaim(options::OPT__SLASH_TC,
1001f4a2713aSLionel Sambuc                                          options::OPT__SLASH_TP)) {
1002f4a2713aSLionel Sambuc     InputTypeArg = TCTP;
1003f4a2713aSLionel Sambuc     InputType = TCTP->getOption().matches(options::OPT__SLASH_TC)
1004f4a2713aSLionel Sambuc         ? types::TY_C : types::TY_CXX;
1005f4a2713aSLionel Sambuc 
1006f4a2713aSLionel Sambuc     arg_iterator it = Args.filtered_begin(options::OPT__SLASH_TC,
1007f4a2713aSLionel Sambuc                                           options::OPT__SLASH_TP);
1008f4a2713aSLionel Sambuc     const arg_iterator ie = Args.filtered_end();
1009f4a2713aSLionel Sambuc     Arg *Previous = *it++;
1010f4a2713aSLionel Sambuc     bool ShowNote = false;
1011f4a2713aSLionel Sambuc     while (it != ie) {
1012f4a2713aSLionel Sambuc       Diag(clang::diag::warn_drv_overriding_flag_option)
1013f4a2713aSLionel Sambuc           << Previous->getSpelling() << (*it)->getSpelling();
1014f4a2713aSLionel Sambuc       Previous = *it++;
1015f4a2713aSLionel Sambuc       ShowNote = true;
1016f4a2713aSLionel Sambuc     }
1017f4a2713aSLionel Sambuc     if (ShowNote)
1018f4a2713aSLionel Sambuc       Diag(clang::diag::note_drv_t_option_is_global);
1019f4a2713aSLionel Sambuc 
1020f4a2713aSLionel Sambuc     // No driver mode exposes -x and /TC or /TP; we don't support mixing them.
1021f4a2713aSLionel Sambuc     assert(!Args.hasArg(options::OPT_x) && "-x and /TC or /TP is not allowed");
1022f4a2713aSLionel Sambuc   }
1023f4a2713aSLionel Sambuc 
1024*0a6a1f1dSLionel Sambuc   for (Arg *A : Args) {
1025f4a2713aSLionel Sambuc     if (A->getOption().getKind() == Option::InputClass) {
1026f4a2713aSLionel Sambuc       const char *Value = A->getValue();
1027f4a2713aSLionel Sambuc       types::ID Ty = types::TY_INVALID;
1028f4a2713aSLionel Sambuc 
1029f4a2713aSLionel Sambuc       // Infer the input type if necessary.
1030f4a2713aSLionel Sambuc       if (InputType == types::TY_Nothing) {
1031f4a2713aSLionel Sambuc         // If there was an explicit arg for this, claim it.
1032f4a2713aSLionel Sambuc         if (InputTypeArg)
1033f4a2713aSLionel Sambuc           InputTypeArg->claim();
1034f4a2713aSLionel Sambuc 
1035f4a2713aSLionel Sambuc         // stdin must be handled specially.
1036f4a2713aSLionel Sambuc         if (memcmp(Value, "-", 2) == 0) {
1037f4a2713aSLionel Sambuc           // If running with -E, treat as a C input (this changes the builtin
1038f4a2713aSLionel Sambuc           // macros, for example). This may be overridden by -ObjC below.
1039f4a2713aSLionel Sambuc           //
1040f4a2713aSLionel Sambuc           // Otherwise emit an error but still use a valid type to avoid
1041f4a2713aSLionel Sambuc           // spurious errors (e.g., no inputs).
1042f4a2713aSLionel Sambuc           if (!Args.hasArgNoClaim(options::OPT_E) && !CCCIsCPP())
1043*0a6a1f1dSLionel Sambuc             Diag(IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl
1044*0a6a1f1dSLionel Sambuc                             : clang::diag::err_drv_unknown_stdin_type);
1045f4a2713aSLionel Sambuc           Ty = types::TY_C;
1046f4a2713aSLionel Sambuc         } else {
1047f4a2713aSLionel Sambuc           // Otherwise lookup by extension.
1048f4a2713aSLionel Sambuc           // Fallback is C if invoked as C preprocessor or Object otherwise.
1049f4a2713aSLionel Sambuc           // We use a host hook here because Darwin at least has its own
1050f4a2713aSLionel Sambuc           // idea of what .s is.
1051f4a2713aSLionel Sambuc           if (const char *Ext = strrchr(Value, '.'))
1052f4a2713aSLionel Sambuc             Ty = TC.LookupTypeForExtension(Ext + 1);
1053f4a2713aSLionel Sambuc 
1054f4a2713aSLionel Sambuc           if (Ty == types::TY_INVALID) {
1055f4a2713aSLionel Sambuc             if (CCCIsCPP())
1056f4a2713aSLionel Sambuc               Ty = types::TY_C;
1057f4a2713aSLionel Sambuc             else
1058f4a2713aSLionel Sambuc               Ty = types::TY_Object;
1059f4a2713aSLionel Sambuc           }
1060f4a2713aSLionel Sambuc 
1061f4a2713aSLionel Sambuc           // If the driver is invoked as C++ compiler (like clang++ or c++) it
1062f4a2713aSLionel Sambuc           // should autodetect some input files as C++ for g++ compatibility.
1063f4a2713aSLionel Sambuc           if (CCCIsCXX()) {
1064f4a2713aSLionel Sambuc             types::ID OldTy = Ty;
1065f4a2713aSLionel Sambuc             Ty = types::lookupCXXTypeForCType(Ty);
1066f4a2713aSLionel Sambuc 
1067f4a2713aSLionel Sambuc             if (Ty != OldTy)
1068f4a2713aSLionel Sambuc               Diag(clang::diag::warn_drv_treating_input_as_cxx)
1069f4a2713aSLionel Sambuc                 << getTypeName(OldTy) << getTypeName(Ty);
1070f4a2713aSLionel Sambuc           }
1071f4a2713aSLionel Sambuc         }
1072f4a2713aSLionel Sambuc 
1073f4a2713aSLionel Sambuc         // -ObjC and -ObjC++ override the default language, but only for "source
1074f4a2713aSLionel Sambuc         // files". We just treat everything that isn't a linker input as a
1075f4a2713aSLionel Sambuc         // source file.
1076f4a2713aSLionel Sambuc         //
1077f4a2713aSLionel Sambuc         // FIXME: Clean this up if we move the phase sequence into the type.
1078f4a2713aSLionel Sambuc         if (Ty != types::TY_Object) {
1079f4a2713aSLionel Sambuc           if (Args.hasArg(options::OPT_ObjC))
1080f4a2713aSLionel Sambuc             Ty = types::TY_ObjC;
1081f4a2713aSLionel Sambuc           else if (Args.hasArg(options::OPT_ObjCXX))
1082f4a2713aSLionel Sambuc             Ty = types::TY_ObjCXX;
1083f4a2713aSLionel Sambuc         }
1084f4a2713aSLionel Sambuc       } else {
1085f4a2713aSLionel Sambuc         assert(InputTypeArg && "InputType set w/o InputTypeArg");
1086*0a6a1f1dSLionel Sambuc         if (!InputTypeArg->getOption().matches(options::OPT_x)) {
1087*0a6a1f1dSLionel Sambuc           // If emulating cl.exe, make sure that /TC and /TP don't affect input
1088*0a6a1f1dSLionel Sambuc           // object files.
1089*0a6a1f1dSLionel Sambuc           const char *Ext = strrchr(Value, '.');
1090*0a6a1f1dSLionel Sambuc           if (Ext && TC.LookupTypeForExtension(Ext + 1) == types::TY_Object)
1091*0a6a1f1dSLionel Sambuc             Ty = types::TY_Object;
1092*0a6a1f1dSLionel Sambuc         }
1093*0a6a1f1dSLionel Sambuc         if (Ty == types::TY_INVALID) {
1094f4a2713aSLionel Sambuc           Ty = InputType;
1095*0a6a1f1dSLionel Sambuc           InputTypeArg->claim();
1096*0a6a1f1dSLionel Sambuc         }
1097f4a2713aSLionel Sambuc       }
1098f4a2713aSLionel Sambuc 
1099*0a6a1f1dSLionel Sambuc       if (DiagnoseInputExistence(*this, Args, Value))
1100f4a2713aSLionel Sambuc         Inputs.push_back(std::make_pair(Ty, A));
1101f4a2713aSLionel Sambuc 
1102f4a2713aSLionel Sambuc     } else if (A->getOption().matches(options::OPT__SLASH_Tc)) {
1103f4a2713aSLionel Sambuc       StringRef Value = A->getValue();
1104*0a6a1f1dSLionel Sambuc       if (DiagnoseInputExistence(*this, Args, Value)) {
1105f4a2713aSLionel Sambuc         Arg *InputArg = MakeInputArg(Args, Opts, A->getValue());
1106f4a2713aSLionel Sambuc         Inputs.push_back(std::make_pair(types::TY_C, InputArg));
1107f4a2713aSLionel Sambuc       }
1108f4a2713aSLionel Sambuc       A->claim();
1109f4a2713aSLionel Sambuc     } else if (A->getOption().matches(options::OPT__SLASH_Tp)) {
1110f4a2713aSLionel Sambuc       StringRef Value = A->getValue();
1111*0a6a1f1dSLionel Sambuc       if (DiagnoseInputExistence(*this, Args, Value)) {
1112f4a2713aSLionel Sambuc         Arg *InputArg = MakeInputArg(Args, Opts, A->getValue());
1113f4a2713aSLionel Sambuc         Inputs.push_back(std::make_pair(types::TY_CXX, InputArg));
1114f4a2713aSLionel Sambuc       }
1115f4a2713aSLionel Sambuc       A->claim();
1116f4a2713aSLionel Sambuc     } else if (A->getOption().hasFlag(options::LinkerInput)) {
1117f4a2713aSLionel Sambuc       // Just treat as object type, we could make a special type for this if
1118f4a2713aSLionel Sambuc       // necessary.
1119f4a2713aSLionel Sambuc       Inputs.push_back(std::make_pair(types::TY_Object, A));
1120f4a2713aSLionel Sambuc 
1121f4a2713aSLionel Sambuc     } else if (A->getOption().matches(options::OPT_x)) {
1122f4a2713aSLionel Sambuc       InputTypeArg = A;
1123f4a2713aSLionel Sambuc       InputType = types::lookupTypeForTypeSpecifier(A->getValue());
1124f4a2713aSLionel Sambuc       A->claim();
1125f4a2713aSLionel Sambuc 
1126f4a2713aSLionel Sambuc       // Follow gcc behavior and treat as linker input for invalid -x
1127f4a2713aSLionel Sambuc       // options. Its not clear why we shouldn't just revert to unknown; but
1128f4a2713aSLionel Sambuc       // this isn't very important, we might as well be bug compatible.
1129f4a2713aSLionel Sambuc       if (!InputType) {
1130f4a2713aSLionel Sambuc         Diag(clang::diag::err_drv_unknown_language) << A->getValue();
1131f4a2713aSLionel Sambuc         InputType = types::TY_Object;
1132f4a2713aSLionel Sambuc       }
1133f4a2713aSLionel Sambuc     }
1134f4a2713aSLionel Sambuc   }
1135f4a2713aSLionel Sambuc   if (CCCIsCPP() && Inputs.empty()) {
1136f4a2713aSLionel Sambuc     // If called as standalone preprocessor, stdin is processed
1137f4a2713aSLionel Sambuc     // if no other input is present.
1138f4a2713aSLionel Sambuc     Arg *A = MakeInputArg(Args, Opts, "-");
1139f4a2713aSLionel Sambuc     Inputs.push_back(std::make_pair(types::TY_C, A));
1140f4a2713aSLionel Sambuc   }
1141f4a2713aSLionel Sambuc }
1142f4a2713aSLionel Sambuc 
BuildActions(const ToolChain & TC,DerivedArgList & Args,const InputList & Inputs,ActionList & Actions) const1143f4a2713aSLionel Sambuc void Driver::BuildActions(const ToolChain &TC, DerivedArgList &Args,
1144f4a2713aSLionel Sambuc                           const InputList &Inputs, ActionList &Actions) const {
1145f4a2713aSLionel Sambuc   llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
1146f4a2713aSLionel Sambuc 
1147f4a2713aSLionel Sambuc   if (!SuppressMissingInputWarning && Inputs.empty()) {
1148f4a2713aSLionel Sambuc     Diag(clang::diag::err_drv_no_input_files);
1149f4a2713aSLionel Sambuc     return;
1150f4a2713aSLionel Sambuc   }
1151f4a2713aSLionel Sambuc 
1152f4a2713aSLionel Sambuc   Arg *FinalPhaseArg;
1153f4a2713aSLionel Sambuc   phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg);
1154f4a2713aSLionel Sambuc 
1155f4a2713aSLionel Sambuc   if (FinalPhase == phases::Link && Args.hasArg(options::OPT_emit_llvm)) {
1156f4a2713aSLionel Sambuc     Diag(clang::diag::err_drv_emit_llvm_link);
1157f4a2713aSLionel Sambuc   }
1158f4a2713aSLionel Sambuc 
1159f4a2713aSLionel Sambuc   // Reject -Z* at the top level, these options should never have been exposed
1160f4a2713aSLionel Sambuc   // by gcc.
1161f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
1162f4a2713aSLionel Sambuc     Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
1163f4a2713aSLionel Sambuc 
1164f4a2713aSLionel Sambuc   // Diagnose misuse of /Fo.
1165f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fo)) {
1166f4a2713aSLionel Sambuc     StringRef V = A->getValue();
1167*0a6a1f1dSLionel Sambuc     if (Inputs.size() > 1 && !V.empty() &&
1168*0a6a1f1dSLionel Sambuc         !llvm::sys::path::is_separator(V.back())) {
1169f4a2713aSLionel Sambuc       // Check whether /Fo tries to name an output file for multiple inputs.
1170f4a2713aSLionel Sambuc       Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources)
1171f4a2713aSLionel Sambuc         << A->getSpelling() << V;
1172f4a2713aSLionel Sambuc       Args.eraseArg(options::OPT__SLASH_Fo);
1173f4a2713aSLionel Sambuc     }
1174f4a2713aSLionel Sambuc   }
1175f4a2713aSLionel Sambuc 
1176f4a2713aSLionel Sambuc   // Diagnose misuse of /Fa.
1177f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fa)) {
1178f4a2713aSLionel Sambuc     StringRef V = A->getValue();
1179*0a6a1f1dSLionel Sambuc     if (Inputs.size() > 1 && !V.empty() &&
1180*0a6a1f1dSLionel Sambuc         !llvm::sys::path::is_separator(V.back())) {
1181f4a2713aSLionel Sambuc       // Check whether /Fa tries to name an asm file for multiple inputs.
1182f4a2713aSLionel Sambuc       Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources)
1183f4a2713aSLionel Sambuc         << A->getSpelling() << V;
1184f4a2713aSLionel Sambuc       Args.eraseArg(options::OPT__SLASH_Fa);
1185f4a2713aSLionel Sambuc     }
1186f4a2713aSLionel Sambuc   }
1187f4a2713aSLionel Sambuc 
1188*0a6a1f1dSLionel Sambuc   // Diagnose misuse of /o.
1189*0a6a1f1dSLionel Sambuc   if (Arg *A = Args.getLastArg(options::OPT__SLASH_o)) {
1190f4a2713aSLionel Sambuc     if (A->getValue()[0] == '\0') {
1191f4a2713aSLionel Sambuc       // It has to have a value.
1192f4a2713aSLionel Sambuc       Diag(clang::diag::err_drv_missing_argument) << A->getSpelling() << 1;
1193*0a6a1f1dSLionel Sambuc       Args.eraseArg(options::OPT__SLASH_o);
1194f4a2713aSLionel Sambuc     }
1195f4a2713aSLionel Sambuc   }
1196f4a2713aSLionel Sambuc 
1197f4a2713aSLionel Sambuc   // Construct the actions to perform.
1198f4a2713aSLionel Sambuc   ActionList LinkerInputs;
1199*0a6a1f1dSLionel Sambuc 
1200f4a2713aSLionel Sambuc   llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PL;
1201f4a2713aSLionel Sambuc   for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
1202f4a2713aSLionel Sambuc     types::ID InputType = Inputs[i].first;
1203f4a2713aSLionel Sambuc     const Arg *InputArg = Inputs[i].second;
1204f4a2713aSLionel Sambuc 
1205f4a2713aSLionel Sambuc     PL.clear();
1206f4a2713aSLionel Sambuc     types::getCompilationPhases(InputType, PL);
1207f4a2713aSLionel Sambuc 
1208f4a2713aSLionel Sambuc     // If the first step comes after the final phase we are doing as part of
1209f4a2713aSLionel Sambuc     // this compilation, warn the user about it.
1210f4a2713aSLionel Sambuc     phases::ID InitialPhase = PL[0];
1211f4a2713aSLionel Sambuc     if (InitialPhase > FinalPhase) {
1212f4a2713aSLionel Sambuc       // Claim here to avoid the more general unused warning.
1213f4a2713aSLionel Sambuc       InputArg->claim();
1214f4a2713aSLionel Sambuc 
1215f4a2713aSLionel Sambuc       // Suppress all unused style warnings with -Qunused-arguments
1216f4a2713aSLionel Sambuc       if (Args.hasArg(options::OPT_Qunused_arguments))
1217f4a2713aSLionel Sambuc         continue;
1218f4a2713aSLionel Sambuc 
1219f4a2713aSLionel Sambuc       // Special case when final phase determined by binary name, rather than
1220f4a2713aSLionel Sambuc       // by a command-line argument with a corresponding Arg.
1221f4a2713aSLionel Sambuc       if (CCCIsCPP())
1222f4a2713aSLionel Sambuc         Diag(clang::diag::warn_drv_input_file_unused_by_cpp)
1223f4a2713aSLionel Sambuc           << InputArg->getAsString(Args)
1224f4a2713aSLionel Sambuc           << getPhaseName(InitialPhase);
1225f4a2713aSLionel Sambuc       // Special case '-E' warning on a previously preprocessed file to make
1226f4a2713aSLionel Sambuc       // more sense.
1227f4a2713aSLionel Sambuc       else if (InitialPhase == phases::Compile &&
1228f4a2713aSLionel Sambuc                FinalPhase == phases::Preprocess &&
1229f4a2713aSLionel Sambuc                getPreprocessedType(InputType) == types::TY_INVALID)
1230f4a2713aSLionel Sambuc         Diag(clang::diag::warn_drv_preprocessed_input_file_unused)
1231f4a2713aSLionel Sambuc           << InputArg->getAsString(Args)
1232f4a2713aSLionel Sambuc           << !!FinalPhaseArg
1233*0a6a1f1dSLionel Sambuc           << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "");
1234f4a2713aSLionel Sambuc       else
1235f4a2713aSLionel Sambuc         Diag(clang::diag::warn_drv_input_file_unused)
1236f4a2713aSLionel Sambuc           << InputArg->getAsString(Args)
1237f4a2713aSLionel Sambuc           << getPhaseName(InitialPhase)
1238f4a2713aSLionel Sambuc           << !!FinalPhaseArg
1239*0a6a1f1dSLionel Sambuc           << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "");
1240f4a2713aSLionel Sambuc       continue;
1241f4a2713aSLionel Sambuc     }
1242f4a2713aSLionel Sambuc 
1243f4a2713aSLionel Sambuc     // Build the pipeline for this file.
1244*0a6a1f1dSLionel Sambuc     std::unique_ptr<Action> Current(new InputAction(*InputArg, InputType));
1245f4a2713aSLionel Sambuc     for (SmallVectorImpl<phases::ID>::iterator
1246f4a2713aSLionel Sambuc            i = PL.begin(), e = PL.end(); i != e; ++i) {
1247f4a2713aSLionel Sambuc       phases::ID Phase = *i;
1248f4a2713aSLionel Sambuc 
1249f4a2713aSLionel Sambuc       // We are done if this step is past what the user requested.
1250f4a2713aSLionel Sambuc       if (Phase > FinalPhase)
1251f4a2713aSLionel Sambuc         break;
1252f4a2713aSLionel Sambuc 
1253f4a2713aSLionel Sambuc       // Queue linker inputs.
1254f4a2713aSLionel Sambuc       if (Phase == phases::Link) {
1255f4a2713aSLionel Sambuc         assert((i + 1) == e && "linking must be final compilation step.");
1256*0a6a1f1dSLionel Sambuc         LinkerInputs.push_back(Current.release());
1257f4a2713aSLionel Sambuc         break;
1258f4a2713aSLionel Sambuc       }
1259f4a2713aSLionel Sambuc 
1260f4a2713aSLionel Sambuc       // Some types skip the assembler phase (e.g., llvm-bc), but we can't
1261f4a2713aSLionel Sambuc       // encode this in the steps because the intermediate type depends on
1262f4a2713aSLionel Sambuc       // arguments. Just special case here.
1263f4a2713aSLionel Sambuc       if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
1264f4a2713aSLionel Sambuc         continue;
1265f4a2713aSLionel Sambuc 
1266f4a2713aSLionel Sambuc       // Otherwise construct the appropriate action.
1267*0a6a1f1dSLionel Sambuc       Current = ConstructPhaseAction(Args, Phase, std::move(Current));
1268f4a2713aSLionel Sambuc       if (Current->getType() == types::TY_Nothing)
1269f4a2713aSLionel Sambuc         break;
1270f4a2713aSLionel Sambuc     }
1271f4a2713aSLionel Sambuc 
1272f4a2713aSLionel Sambuc     // If we ended with something, add to the output list.
1273f4a2713aSLionel Sambuc     if (Current)
1274*0a6a1f1dSLionel Sambuc       Actions.push_back(Current.release());
1275f4a2713aSLionel Sambuc   }
1276f4a2713aSLionel Sambuc 
1277f4a2713aSLionel Sambuc   // Add a link action if necessary.
1278f4a2713aSLionel Sambuc   if (!LinkerInputs.empty())
1279f4a2713aSLionel Sambuc     Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
1280f4a2713aSLionel Sambuc 
1281f4a2713aSLionel Sambuc   // If we are linking, claim any options which are obviously only used for
1282f4a2713aSLionel Sambuc   // compilation.
1283f4a2713aSLionel Sambuc   if (FinalPhase == phases::Link && PL.size() == 1) {
1284f4a2713aSLionel Sambuc     Args.ClaimAllArgs(options::OPT_CompileOnly_Group);
1285f4a2713aSLionel Sambuc     Args.ClaimAllArgs(options::OPT_cl_compile_Group);
1286f4a2713aSLionel Sambuc   }
1287f4a2713aSLionel Sambuc 
1288f4a2713aSLionel Sambuc   // Claim ignored clang-cl options.
1289f4a2713aSLionel Sambuc   Args.ClaimAllArgs(options::OPT_cl_ignored_Group);
1290f4a2713aSLionel Sambuc }
1291f4a2713aSLionel Sambuc 
1292*0a6a1f1dSLionel Sambuc std::unique_ptr<Action>
ConstructPhaseAction(const ArgList & Args,phases::ID Phase,std::unique_ptr<Action> Input) const1293*0a6a1f1dSLionel Sambuc Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
1294*0a6a1f1dSLionel Sambuc                              std::unique_ptr<Action> Input) const {
1295f4a2713aSLionel Sambuc   llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
1296f4a2713aSLionel Sambuc   // Build the appropriate action.
1297f4a2713aSLionel Sambuc   switch (Phase) {
1298f4a2713aSLionel Sambuc   case phases::Link: llvm_unreachable("link action invalid here.");
1299f4a2713aSLionel Sambuc   case phases::Preprocess: {
1300f4a2713aSLionel Sambuc     types::ID OutputTy;
1301f4a2713aSLionel Sambuc     // -{M, MM} alter the output type.
1302f4a2713aSLionel Sambuc     if (Args.hasArg(options::OPT_M, options::OPT_MM)) {
1303f4a2713aSLionel Sambuc       OutputTy = types::TY_Dependencies;
1304f4a2713aSLionel Sambuc     } else {
1305f4a2713aSLionel Sambuc       OutputTy = Input->getType();
1306f4a2713aSLionel Sambuc       if (!Args.hasFlag(options::OPT_frewrite_includes,
1307*0a6a1f1dSLionel Sambuc                         options::OPT_fno_rewrite_includes, false) &&
1308*0a6a1f1dSLionel Sambuc           !CCGenDiagnostics)
1309f4a2713aSLionel Sambuc         OutputTy = types::getPreprocessedType(OutputTy);
1310f4a2713aSLionel Sambuc       assert(OutputTy != types::TY_INVALID &&
1311f4a2713aSLionel Sambuc              "Cannot preprocess this input type!");
1312f4a2713aSLionel Sambuc     }
1313*0a6a1f1dSLionel Sambuc     return llvm::make_unique<PreprocessJobAction>(std::move(Input), OutputTy);
1314f4a2713aSLionel Sambuc   }
1315f4a2713aSLionel Sambuc   case phases::Precompile: {
1316f4a2713aSLionel Sambuc     types::ID OutputTy = types::TY_PCH;
1317f4a2713aSLionel Sambuc     if (Args.hasArg(options::OPT_fsyntax_only)) {
1318f4a2713aSLionel Sambuc       // Syntax checks should not emit a PCH file
1319f4a2713aSLionel Sambuc       OutputTy = types::TY_Nothing;
1320f4a2713aSLionel Sambuc     }
1321*0a6a1f1dSLionel Sambuc     return llvm::make_unique<PrecompileJobAction>(std::move(Input), OutputTy);
1322f4a2713aSLionel Sambuc   }
1323f4a2713aSLionel Sambuc   case phases::Compile: {
1324*0a6a1f1dSLionel Sambuc     if (Args.hasArg(options::OPT_fsyntax_only))
1325*0a6a1f1dSLionel Sambuc       return llvm::make_unique<CompileJobAction>(std::move(Input),
1326*0a6a1f1dSLionel Sambuc                                                  types::TY_Nothing);
1327*0a6a1f1dSLionel Sambuc     if (Args.hasArg(options::OPT_rewrite_objc))
1328*0a6a1f1dSLionel Sambuc       return llvm::make_unique<CompileJobAction>(std::move(Input),
1329*0a6a1f1dSLionel Sambuc                                                  types::TY_RewrittenObjC);
1330*0a6a1f1dSLionel Sambuc     if (Args.hasArg(options::OPT_rewrite_legacy_objc))
1331*0a6a1f1dSLionel Sambuc       return llvm::make_unique<CompileJobAction>(std::move(Input),
1332*0a6a1f1dSLionel Sambuc                                                  types::TY_RewrittenLegacyObjC);
1333*0a6a1f1dSLionel Sambuc     if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto))
1334*0a6a1f1dSLionel Sambuc       return llvm::make_unique<AnalyzeJobAction>(std::move(Input),
1335*0a6a1f1dSLionel Sambuc                                                  types::TY_Plist);
1336*0a6a1f1dSLionel Sambuc     if (Args.hasArg(options::OPT__migrate))
1337*0a6a1f1dSLionel Sambuc       return llvm::make_unique<MigrateJobAction>(std::move(Input),
1338*0a6a1f1dSLionel Sambuc                                                  types::TY_Remap);
1339*0a6a1f1dSLionel Sambuc     if (Args.hasArg(options::OPT_emit_ast))
1340*0a6a1f1dSLionel Sambuc       return llvm::make_unique<CompileJobAction>(std::move(Input),
1341*0a6a1f1dSLionel Sambuc                                                  types::TY_AST);
1342*0a6a1f1dSLionel Sambuc     if (Args.hasArg(options::OPT_module_file_info))
1343*0a6a1f1dSLionel Sambuc       return llvm::make_unique<CompileJobAction>(std::move(Input),
1344*0a6a1f1dSLionel Sambuc                                                  types::TY_ModuleFile);
1345*0a6a1f1dSLionel Sambuc     if (Args.hasArg(options::OPT_verify_pch))
1346*0a6a1f1dSLionel Sambuc       return llvm::make_unique<VerifyPCHJobAction>(std::move(Input),
1347*0a6a1f1dSLionel Sambuc                                                    types::TY_Nothing);
1348*0a6a1f1dSLionel Sambuc     return llvm::make_unique<CompileJobAction>(std::move(Input),
1349*0a6a1f1dSLionel Sambuc                                                types::TY_LLVM_BC);
1350*0a6a1f1dSLionel Sambuc   }
1351*0a6a1f1dSLionel Sambuc   case phases::Backend: {
1352*0a6a1f1dSLionel Sambuc     if (IsUsingLTO(Args)) {
1353f4a2713aSLionel Sambuc       types::ID Output =
1354f4a2713aSLionel Sambuc         Args.hasArg(options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC;
1355*0a6a1f1dSLionel Sambuc       return llvm::make_unique<BackendJobAction>(std::move(Input), Output);
1356*0a6a1f1dSLionel Sambuc     }
1357*0a6a1f1dSLionel Sambuc     if (Args.hasArg(options::OPT_emit_llvm)) {
1358f4a2713aSLionel Sambuc       types::ID Output =
1359f4a2713aSLionel Sambuc         Args.hasArg(options::OPT_S) ? types::TY_LLVM_IR : types::TY_LLVM_BC;
1360*0a6a1f1dSLionel Sambuc       return llvm::make_unique<BackendJobAction>(std::move(Input), Output);
1361f4a2713aSLionel Sambuc     }
1362*0a6a1f1dSLionel Sambuc     return llvm::make_unique<BackendJobAction>(std::move(Input),
1363*0a6a1f1dSLionel Sambuc                                                types::TY_PP_Asm);
1364f4a2713aSLionel Sambuc   }
1365f4a2713aSLionel Sambuc   case phases::Assemble:
1366*0a6a1f1dSLionel Sambuc     return llvm::make_unique<AssembleJobAction>(std::move(Input),
1367*0a6a1f1dSLionel Sambuc                                                 types::TY_Object);
1368f4a2713aSLionel Sambuc   }
1369f4a2713aSLionel Sambuc 
1370f4a2713aSLionel Sambuc   llvm_unreachable("invalid phase in ConstructPhaseAction");
1371f4a2713aSLionel Sambuc }
1372f4a2713aSLionel Sambuc 
IsUsingLTO(const ArgList & Args) const1373f4a2713aSLionel Sambuc bool Driver::IsUsingLTO(const ArgList &Args) const {
1374f4a2713aSLionel Sambuc   if (Args.hasFlag(options::OPT_flto, options::OPT_fno_lto, false))
1375f4a2713aSLionel Sambuc     return true;
1376f4a2713aSLionel Sambuc 
1377f4a2713aSLionel Sambuc   return false;
1378f4a2713aSLionel Sambuc }
1379f4a2713aSLionel Sambuc 
BuildJobs(Compilation & C) const1380f4a2713aSLionel Sambuc void Driver::BuildJobs(Compilation &C) const {
1381f4a2713aSLionel Sambuc   llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
1382f4a2713aSLionel Sambuc 
1383f4a2713aSLionel Sambuc   Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
1384f4a2713aSLionel Sambuc 
1385f4a2713aSLionel Sambuc   // It is an error to provide a -o option if we are making multiple output
1386f4a2713aSLionel Sambuc   // files.
1387f4a2713aSLionel Sambuc   if (FinalOutput) {
1388f4a2713aSLionel Sambuc     unsigned NumOutputs = 0;
1389*0a6a1f1dSLionel Sambuc     for (const Action *A : C.getActions())
1390*0a6a1f1dSLionel Sambuc       if (A->getType() != types::TY_Nothing)
1391f4a2713aSLionel Sambuc         ++NumOutputs;
1392f4a2713aSLionel Sambuc 
1393f4a2713aSLionel Sambuc     if (NumOutputs > 1) {
1394f4a2713aSLionel Sambuc       Diag(clang::diag::err_drv_output_argument_with_multiple_files);
1395*0a6a1f1dSLionel Sambuc       FinalOutput = nullptr;
1396f4a2713aSLionel Sambuc     }
1397f4a2713aSLionel Sambuc   }
1398f4a2713aSLionel Sambuc 
1399f4a2713aSLionel Sambuc   // Collect the list of architectures.
1400f4a2713aSLionel Sambuc   llvm::StringSet<> ArchNames;
1401*0a6a1f1dSLionel Sambuc   if (C.getDefaultToolChain().getTriple().isOSBinFormatMachO())
1402*0a6a1f1dSLionel Sambuc     for (const Arg *A : C.getArgs())
1403f4a2713aSLionel Sambuc       if (A->getOption().matches(options::OPT_arch))
1404f4a2713aSLionel Sambuc         ArchNames.insert(A->getValue());
1405f4a2713aSLionel Sambuc 
1406*0a6a1f1dSLionel Sambuc   for (Action *A : C.getActions()) {
1407f4a2713aSLionel Sambuc     // If we are linking an image for multiple archs then the linker wants
1408f4a2713aSLionel Sambuc     // -arch_multiple and -final_output <final image name>. Unfortunately, this
1409f4a2713aSLionel Sambuc     // doesn't fit in cleanly because we have to pass this information down.
1410f4a2713aSLionel Sambuc     //
1411f4a2713aSLionel Sambuc     // FIXME: This is a hack; find a cleaner way to integrate this into the
1412f4a2713aSLionel Sambuc     // process.
1413*0a6a1f1dSLionel Sambuc     const char *LinkingOutput = nullptr;
1414f4a2713aSLionel Sambuc     if (isa<LipoJobAction>(A)) {
1415f4a2713aSLionel Sambuc       if (FinalOutput)
1416f4a2713aSLionel Sambuc         LinkingOutput = FinalOutput->getValue();
1417f4a2713aSLionel Sambuc       else
1418*0a6a1f1dSLionel Sambuc         LinkingOutput = getDefaultImageName();
1419f4a2713aSLionel Sambuc     }
1420f4a2713aSLionel Sambuc 
1421f4a2713aSLionel Sambuc     InputInfo II;
1422f4a2713aSLionel Sambuc     BuildJobsForAction(C, A, &C.getDefaultToolChain(),
1423*0a6a1f1dSLionel Sambuc                        /*BoundArch*/nullptr,
1424f4a2713aSLionel Sambuc                        /*AtTopLevel*/ true,
1425f4a2713aSLionel Sambuc                        /*MultipleArchs*/ ArchNames.size() > 1,
1426f4a2713aSLionel Sambuc                        /*LinkingOutput*/ LinkingOutput,
1427f4a2713aSLionel Sambuc                        II);
1428f4a2713aSLionel Sambuc   }
1429f4a2713aSLionel Sambuc 
1430f4a2713aSLionel Sambuc   // If the user passed -Qunused-arguments or there were errors, don't warn
1431f4a2713aSLionel Sambuc   // about any unused arguments.
1432f4a2713aSLionel Sambuc   if (Diags.hasErrorOccurred() ||
1433f4a2713aSLionel Sambuc       C.getArgs().hasArg(options::OPT_Qunused_arguments))
1434f4a2713aSLionel Sambuc     return;
1435f4a2713aSLionel Sambuc 
1436f4a2713aSLionel Sambuc   // Claim -### here.
1437f4a2713aSLionel Sambuc   (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
1438f4a2713aSLionel Sambuc 
1439f4a2713aSLionel Sambuc   // Claim --driver-mode, it was handled earlier.
1440f4a2713aSLionel Sambuc   (void) C.getArgs().hasArg(options::OPT_driver_mode);
1441f4a2713aSLionel Sambuc 
1442*0a6a1f1dSLionel Sambuc   for (Arg *A : C.getArgs()) {
1443f4a2713aSLionel Sambuc     // FIXME: It would be nice to be able to send the argument to the
1444f4a2713aSLionel Sambuc     // DiagnosticsEngine, so that extra values, position, and so on could be
1445f4a2713aSLionel Sambuc     // printed.
1446f4a2713aSLionel Sambuc     if (!A->isClaimed()) {
1447f4a2713aSLionel Sambuc       if (A->getOption().hasFlag(options::NoArgumentUnused))
1448f4a2713aSLionel Sambuc         continue;
1449f4a2713aSLionel Sambuc 
1450f4a2713aSLionel Sambuc       // Suppress the warning automatically if this is just a flag, and it is an
1451f4a2713aSLionel Sambuc       // instance of an argument we already claimed.
1452f4a2713aSLionel Sambuc       const Option &Opt = A->getOption();
1453f4a2713aSLionel Sambuc       if (Opt.getKind() == Option::FlagClass) {
1454f4a2713aSLionel Sambuc         bool DuplicateClaimed = false;
1455f4a2713aSLionel Sambuc 
1456f4a2713aSLionel Sambuc         for (arg_iterator it = C.getArgs().filtered_begin(&Opt),
1457f4a2713aSLionel Sambuc                ie = C.getArgs().filtered_end(); it != ie; ++it) {
1458f4a2713aSLionel Sambuc           if ((*it)->isClaimed()) {
1459f4a2713aSLionel Sambuc             DuplicateClaimed = true;
1460f4a2713aSLionel Sambuc             break;
1461f4a2713aSLionel Sambuc           }
1462f4a2713aSLionel Sambuc         }
1463f4a2713aSLionel Sambuc 
1464f4a2713aSLionel Sambuc         if (DuplicateClaimed)
1465f4a2713aSLionel Sambuc           continue;
1466f4a2713aSLionel Sambuc       }
1467f4a2713aSLionel Sambuc 
1468f4a2713aSLionel Sambuc       Diag(clang::diag::warn_drv_unused_argument)
1469f4a2713aSLionel Sambuc         << A->getAsString(C.getArgs());
1470f4a2713aSLionel Sambuc     }
1471f4a2713aSLionel Sambuc   }
1472f4a2713aSLionel Sambuc }
1473f4a2713aSLionel Sambuc 
SelectToolForJob(Compilation & C,const ToolChain * TC,const JobAction * JA,const ActionList * & Inputs)1474f4a2713aSLionel Sambuc static const Tool *SelectToolForJob(Compilation &C, const ToolChain *TC,
1475f4a2713aSLionel Sambuc                                     const JobAction *JA,
1476f4a2713aSLionel Sambuc                                     const ActionList *&Inputs) {
1477*0a6a1f1dSLionel Sambuc   const Tool *ToolForJob = nullptr;
1478f4a2713aSLionel Sambuc 
1479f4a2713aSLionel Sambuc   // See if we should look for a compiler with an integrated assembler. We match
1480f4a2713aSLionel Sambuc   // bottom up, so what we are actually looking for is an assembler job with a
1481f4a2713aSLionel Sambuc   // compiler input.
1482f4a2713aSLionel Sambuc 
1483f4a2713aSLionel Sambuc   if (TC->useIntegratedAs() &&
1484f4a2713aSLionel Sambuc       !C.getArgs().hasArg(options::OPT_save_temps) &&
1485*0a6a1f1dSLionel Sambuc       !C.getArgs().hasArg(options::OPT_via_file_asm) &&
1486f4a2713aSLionel Sambuc       !C.getArgs().hasArg(options::OPT__SLASH_FA) &&
1487f4a2713aSLionel Sambuc       !C.getArgs().hasArg(options::OPT__SLASH_Fa) &&
1488f4a2713aSLionel Sambuc       isa<AssembleJobAction>(JA) &&
1489*0a6a1f1dSLionel Sambuc       Inputs->size() == 1 && isa<BackendJobAction>(*Inputs->begin())) {
1490*0a6a1f1dSLionel Sambuc     // A BackendJob is always preceded by a CompileJob, and without
1491*0a6a1f1dSLionel Sambuc     // -save-temps they will always get combined together, so instead of
1492*0a6a1f1dSLionel Sambuc     // checking the backend tool, check if the tool for the CompileJob
1493*0a6a1f1dSLionel Sambuc     // has an integrated assembler.
1494*0a6a1f1dSLionel Sambuc     const ActionList *BackendInputs = &(*Inputs)[0]->getInputs();
1495*0a6a1f1dSLionel Sambuc     JobAction *CompileJA = cast<CompileJobAction>(*BackendInputs->begin());
1496*0a6a1f1dSLionel Sambuc     const Tool *Compiler = TC->SelectTool(*CompileJA);
1497f4a2713aSLionel Sambuc     if (!Compiler)
1498*0a6a1f1dSLionel Sambuc       return nullptr;
1499f4a2713aSLionel Sambuc     if (Compiler->hasIntegratedAssembler()) {
1500*0a6a1f1dSLionel Sambuc       Inputs = &(*BackendInputs)[0]->getInputs();
1501*0a6a1f1dSLionel Sambuc       ToolForJob = Compiler;
1502*0a6a1f1dSLionel Sambuc     }
1503*0a6a1f1dSLionel Sambuc   }
1504*0a6a1f1dSLionel Sambuc 
1505*0a6a1f1dSLionel Sambuc   // A backend job should always be combined with the preceding compile job
1506*0a6a1f1dSLionel Sambuc   // unless OPT_save_temps is enabled and the compiler is capable of emitting
1507*0a6a1f1dSLionel Sambuc   // LLVM IR as an intermediate output.
1508*0a6a1f1dSLionel Sambuc   if (isa<BackendJobAction>(JA)) {
1509*0a6a1f1dSLionel Sambuc     // Check if the compiler supports emitting LLVM IR.
1510*0a6a1f1dSLionel Sambuc     assert(Inputs->size() == 1);
1511*0a6a1f1dSLionel Sambuc     JobAction *CompileJA = cast<CompileJobAction>(*Inputs->begin());
1512*0a6a1f1dSLionel Sambuc     const Tool *Compiler = TC->SelectTool(*CompileJA);
1513*0a6a1f1dSLionel Sambuc     if (!Compiler)
1514*0a6a1f1dSLionel Sambuc       return nullptr;
1515*0a6a1f1dSLionel Sambuc     if (!Compiler->canEmitIR() ||
1516*0a6a1f1dSLionel Sambuc         !C.getArgs().hasArg(options::OPT_save_temps)) {
1517f4a2713aSLionel Sambuc       Inputs = &(*Inputs)[0]->getInputs();
1518f4a2713aSLionel Sambuc       ToolForJob = Compiler;
1519f4a2713aSLionel Sambuc     }
1520f4a2713aSLionel Sambuc   }
1521f4a2713aSLionel Sambuc 
1522f4a2713aSLionel Sambuc   // Otherwise use the tool for the current job.
1523f4a2713aSLionel Sambuc   if (!ToolForJob)
1524f4a2713aSLionel Sambuc     ToolForJob = TC->SelectTool(*JA);
1525f4a2713aSLionel Sambuc 
1526f4a2713aSLionel Sambuc   // See if we should use an integrated preprocessor. We do so when we have
1527f4a2713aSLionel Sambuc   // exactly one input, since this is the only use case we care about
1528f4a2713aSLionel Sambuc   // (irrelevant since we don't support combine yet).
1529f4a2713aSLionel Sambuc   if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin()) &&
1530f4a2713aSLionel Sambuc       !C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
1531f4a2713aSLionel Sambuc       !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
1532f4a2713aSLionel Sambuc       !C.getArgs().hasArg(options::OPT_save_temps) &&
1533f4a2713aSLionel Sambuc       !C.getArgs().hasArg(options::OPT_rewrite_objc) &&
1534f4a2713aSLionel Sambuc       ToolForJob->hasIntegratedCPP())
1535f4a2713aSLionel Sambuc     Inputs = &(*Inputs)[0]->getInputs();
1536f4a2713aSLionel Sambuc 
1537f4a2713aSLionel Sambuc   return ToolForJob;
1538f4a2713aSLionel Sambuc }
1539f4a2713aSLionel Sambuc 
BuildJobsForAction(Compilation & C,const Action * A,const ToolChain * TC,const char * BoundArch,bool AtTopLevel,bool MultipleArchs,const char * LinkingOutput,InputInfo & Result) const1540f4a2713aSLionel Sambuc void Driver::BuildJobsForAction(Compilation &C,
1541f4a2713aSLionel Sambuc                                 const Action *A,
1542f4a2713aSLionel Sambuc                                 const ToolChain *TC,
1543f4a2713aSLionel Sambuc                                 const char *BoundArch,
1544f4a2713aSLionel Sambuc                                 bool AtTopLevel,
1545f4a2713aSLionel Sambuc                                 bool MultipleArchs,
1546f4a2713aSLionel Sambuc                                 const char *LinkingOutput,
1547f4a2713aSLionel Sambuc                                 InputInfo &Result) const {
1548f4a2713aSLionel Sambuc   llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
1549f4a2713aSLionel Sambuc 
1550f4a2713aSLionel Sambuc   if (const InputAction *IA = dyn_cast<InputAction>(A)) {
1551f4a2713aSLionel Sambuc     // FIXME: It would be nice to not claim this here; maybe the old scheme of
1552f4a2713aSLionel Sambuc     // just using Args was better?
1553f4a2713aSLionel Sambuc     const Arg &Input = IA->getInputArg();
1554f4a2713aSLionel Sambuc     Input.claim();
1555f4a2713aSLionel Sambuc     if (Input.getOption().matches(options::OPT_INPUT)) {
1556f4a2713aSLionel Sambuc       const char *Name = Input.getValue();
1557f4a2713aSLionel Sambuc       Result = InputInfo(Name, A->getType(), Name);
1558f4a2713aSLionel Sambuc     } else
1559f4a2713aSLionel Sambuc       Result = InputInfo(&Input, A->getType(), "");
1560f4a2713aSLionel Sambuc     return;
1561f4a2713aSLionel Sambuc   }
1562f4a2713aSLionel Sambuc 
1563f4a2713aSLionel Sambuc   if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
1564f4a2713aSLionel Sambuc     const ToolChain *TC;
1565f4a2713aSLionel Sambuc     const char *ArchName = BAA->getArchName();
1566f4a2713aSLionel Sambuc 
1567f4a2713aSLionel Sambuc     if (ArchName)
1568f4a2713aSLionel Sambuc       TC = &getToolChain(C.getArgs(), ArchName);
1569f4a2713aSLionel Sambuc     else
1570f4a2713aSLionel Sambuc       TC = &C.getDefaultToolChain();
1571f4a2713aSLionel Sambuc 
1572f4a2713aSLionel Sambuc     BuildJobsForAction(C, *BAA->begin(), TC, BAA->getArchName(),
1573f4a2713aSLionel Sambuc                        AtTopLevel, MultipleArchs, LinkingOutput, Result);
1574f4a2713aSLionel Sambuc     return;
1575f4a2713aSLionel Sambuc   }
1576f4a2713aSLionel Sambuc 
1577f4a2713aSLionel Sambuc   const ActionList *Inputs = &A->getInputs();
1578f4a2713aSLionel Sambuc 
1579f4a2713aSLionel Sambuc   const JobAction *JA = cast<JobAction>(A);
1580f4a2713aSLionel Sambuc   const Tool *T = SelectToolForJob(C, TC, JA, Inputs);
1581f4a2713aSLionel Sambuc   if (!T)
1582f4a2713aSLionel Sambuc     return;
1583f4a2713aSLionel Sambuc 
1584f4a2713aSLionel Sambuc   // Only use pipes when there is exactly one input.
1585f4a2713aSLionel Sambuc   InputInfoList InputInfos;
1586*0a6a1f1dSLionel Sambuc   for (const Action *Input : *Inputs) {
1587f4a2713aSLionel Sambuc     // Treat dsymutil and verify sub-jobs as being at the top-level too, they
1588f4a2713aSLionel Sambuc     // shouldn't get temporary output names.
1589f4a2713aSLionel Sambuc     // FIXME: Clean this up.
1590f4a2713aSLionel Sambuc     bool SubJobAtTopLevel = false;
1591f4a2713aSLionel Sambuc     if (AtTopLevel && (isa<DsymutilJobAction>(A) || isa<VerifyJobAction>(A)))
1592f4a2713aSLionel Sambuc       SubJobAtTopLevel = true;
1593f4a2713aSLionel Sambuc 
1594f4a2713aSLionel Sambuc     InputInfo II;
1595*0a6a1f1dSLionel Sambuc     BuildJobsForAction(C, Input, TC, BoundArch, SubJobAtTopLevel, MultipleArchs,
1596f4a2713aSLionel Sambuc                        LinkingOutput, II);
1597f4a2713aSLionel Sambuc     InputInfos.push_back(II);
1598f4a2713aSLionel Sambuc   }
1599f4a2713aSLionel Sambuc 
1600f4a2713aSLionel Sambuc   // Always use the first input as the base input.
1601f4a2713aSLionel Sambuc   const char *BaseInput = InputInfos[0].getBaseInput();
1602f4a2713aSLionel Sambuc 
1603f4a2713aSLionel Sambuc   // ... except dsymutil actions, which use their actual input as the base
1604f4a2713aSLionel Sambuc   // input.
1605f4a2713aSLionel Sambuc   if (JA->getType() == types::TY_dSYM)
1606f4a2713aSLionel Sambuc     BaseInput = InputInfos[0].getFilename();
1607f4a2713aSLionel Sambuc 
1608f4a2713aSLionel Sambuc   // Determine the place to write output to, if any.
1609f4a2713aSLionel Sambuc   if (JA->getType() == types::TY_Nothing)
1610f4a2713aSLionel Sambuc     Result = InputInfo(A->getType(), BaseInput);
1611f4a2713aSLionel Sambuc   else
1612f4a2713aSLionel Sambuc     Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, BoundArch,
1613f4a2713aSLionel Sambuc                                           AtTopLevel, MultipleArchs),
1614f4a2713aSLionel Sambuc                        A->getType(), BaseInput);
1615f4a2713aSLionel Sambuc 
1616f4a2713aSLionel Sambuc   if (CCCPrintBindings && !CCGenDiagnostics) {
1617f4a2713aSLionel Sambuc     llvm::errs() << "# \"" << T->getToolChain().getTripleString() << '"'
1618f4a2713aSLionel Sambuc                  << " - \"" << T->getName() << "\", inputs: [";
1619f4a2713aSLionel Sambuc     for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
1620f4a2713aSLionel Sambuc       llvm::errs() << InputInfos[i].getAsString();
1621f4a2713aSLionel Sambuc       if (i + 1 != e)
1622f4a2713aSLionel Sambuc         llvm::errs() << ", ";
1623f4a2713aSLionel Sambuc     }
1624f4a2713aSLionel Sambuc     llvm::errs() << "], output: " << Result.getAsString() << "\n";
1625f4a2713aSLionel Sambuc   } else {
1626f4a2713aSLionel Sambuc     T->ConstructJob(C, *JA, Result, InputInfos,
1627f4a2713aSLionel Sambuc                     C.getArgsForToolChain(TC, BoundArch), LinkingOutput);
1628f4a2713aSLionel Sambuc   }
1629f4a2713aSLionel Sambuc }
1630f4a2713aSLionel Sambuc 
getDefaultImageName() const1631*0a6a1f1dSLionel Sambuc const char *Driver::getDefaultImageName() const {
1632*0a6a1f1dSLionel Sambuc   llvm::Triple Target(llvm::Triple::normalize(DefaultTargetTriple));
1633*0a6a1f1dSLionel Sambuc   return Target.isOSWindows() ? "a.exe" : "a.out";
1634*0a6a1f1dSLionel Sambuc }
1635*0a6a1f1dSLionel Sambuc 
1636f4a2713aSLionel Sambuc /// \brief Create output filename based on ArgValue, which could either be a
1637f4a2713aSLionel Sambuc /// full filename, filename without extension, or a directory. If ArgValue
1638f4a2713aSLionel Sambuc /// does not provide a filename, then use BaseName, and use the extension
1639f4a2713aSLionel Sambuc /// suitable for FileType.
MakeCLOutputFilename(const ArgList & Args,StringRef ArgValue,StringRef BaseName,types::ID FileType)1640f4a2713aSLionel Sambuc static const char *MakeCLOutputFilename(const ArgList &Args, StringRef ArgValue,
1641f4a2713aSLionel Sambuc                                         StringRef BaseName, types::ID FileType) {
1642f4a2713aSLionel Sambuc   SmallString<128> Filename = ArgValue;
1643f4a2713aSLionel Sambuc 
1644f4a2713aSLionel Sambuc   if (ArgValue.empty()) {
1645f4a2713aSLionel Sambuc     // If the argument is empty, output to BaseName in the current dir.
1646f4a2713aSLionel Sambuc     Filename = BaseName;
1647f4a2713aSLionel Sambuc   } else if (llvm::sys::path::is_separator(Filename.back())) {
1648f4a2713aSLionel Sambuc     // If the argument is a directory, output to BaseName in that dir.
1649f4a2713aSLionel Sambuc     llvm::sys::path::append(Filename, BaseName);
1650f4a2713aSLionel Sambuc   }
1651f4a2713aSLionel Sambuc 
1652f4a2713aSLionel Sambuc   if (!llvm::sys::path::has_extension(ArgValue)) {
1653f4a2713aSLionel Sambuc     // If the argument didn't provide an extension, then set it.
1654f4a2713aSLionel Sambuc     const char *Extension = types::getTypeTempSuffix(FileType, true);
1655f4a2713aSLionel Sambuc 
1656f4a2713aSLionel Sambuc     if (FileType == types::TY_Image &&
1657f4a2713aSLionel Sambuc         Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd)) {
1658f4a2713aSLionel Sambuc       // The output file is a dll.
1659f4a2713aSLionel Sambuc       Extension = "dll";
1660f4a2713aSLionel Sambuc     }
1661f4a2713aSLionel Sambuc 
1662f4a2713aSLionel Sambuc     llvm::sys::path::replace_extension(Filename, Extension);
1663f4a2713aSLionel Sambuc   }
1664f4a2713aSLionel Sambuc 
1665f4a2713aSLionel Sambuc   return Args.MakeArgString(Filename.c_str());
1666f4a2713aSLionel Sambuc }
1667f4a2713aSLionel Sambuc 
GetNamedOutputPath(Compilation & C,const JobAction & JA,const char * BaseInput,const char * BoundArch,bool AtTopLevel,bool MultipleArchs) const1668f4a2713aSLionel Sambuc const char *Driver::GetNamedOutputPath(Compilation &C,
1669f4a2713aSLionel Sambuc                                        const JobAction &JA,
1670f4a2713aSLionel Sambuc                                        const char *BaseInput,
1671f4a2713aSLionel Sambuc                                        const char *BoundArch,
1672f4a2713aSLionel Sambuc                                        bool AtTopLevel,
1673f4a2713aSLionel Sambuc                                        bool MultipleArchs) const {
1674f4a2713aSLionel Sambuc   llvm::PrettyStackTraceString CrashInfo("Computing output path");
1675f4a2713aSLionel Sambuc   // Output to a user requested destination?
1676f4a2713aSLionel Sambuc   if (AtTopLevel && !isa<DsymutilJobAction>(JA) &&
1677f4a2713aSLionel Sambuc       !isa<VerifyJobAction>(JA)) {
1678f4a2713aSLionel Sambuc     if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
1679f4a2713aSLionel Sambuc       return C.addResultFile(FinalOutput->getValue(), &JA);
1680f4a2713aSLionel Sambuc   }
1681f4a2713aSLionel Sambuc 
1682*0a6a1f1dSLionel Sambuc   // For /P, preprocess to file named after BaseInput.
1683*0a6a1f1dSLionel Sambuc   if (C.getArgs().hasArg(options::OPT__SLASH_P)) {
1684*0a6a1f1dSLionel Sambuc     assert(AtTopLevel && isa<PreprocessJobAction>(JA));
1685*0a6a1f1dSLionel Sambuc     StringRef BaseName = llvm::sys::path::filename(BaseInput);
1686*0a6a1f1dSLionel Sambuc     StringRef NameArg;
1687*0a6a1f1dSLionel Sambuc     if (Arg *A = C.getArgs().getLastArg(options::OPT__SLASH_Fi,
1688*0a6a1f1dSLionel Sambuc                                         options::OPT__SLASH_o))
1689*0a6a1f1dSLionel Sambuc       NameArg = A->getValue();
1690*0a6a1f1dSLionel Sambuc     return C.addResultFile(MakeCLOutputFilename(C.getArgs(), NameArg, BaseName,
1691*0a6a1f1dSLionel Sambuc                                                 types::TY_PP_C), &JA);
1692*0a6a1f1dSLionel Sambuc   }
1693*0a6a1f1dSLionel Sambuc 
1694f4a2713aSLionel Sambuc   // Default to writing to stdout?
1695f4a2713aSLionel Sambuc   if (AtTopLevel && !CCGenDiagnostics &&
1696f4a2713aSLionel Sambuc       (isa<PreprocessJobAction>(JA) || JA.getType() == types::TY_ModuleFile))
1697f4a2713aSLionel Sambuc     return "-";
1698f4a2713aSLionel Sambuc 
1699f4a2713aSLionel Sambuc   // Is this the assembly listing for /FA?
1700f4a2713aSLionel Sambuc   if (JA.getType() == types::TY_PP_Asm &&
1701f4a2713aSLionel Sambuc       (C.getArgs().hasArg(options::OPT__SLASH_FA) ||
1702f4a2713aSLionel Sambuc        C.getArgs().hasArg(options::OPT__SLASH_Fa))) {
1703f4a2713aSLionel Sambuc     // Use /Fa and the input filename to determine the asm file name.
1704f4a2713aSLionel Sambuc     StringRef BaseName = llvm::sys::path::filename(BaseInput);
1705f4a2713aSLionel Sambuc     StringRef FaValue = C.getArgs().getLastArgValue(options::OPT__SLASH_Fa);
1706f4a2713aSLionel Sambuc     return C.addResultFile(MakeCLOutputFilename(C.getArgs(), FaValue, BaseName,
1707f4a2713aSLionel Sambuc                                                 JA.getType()), &JA);
1708f4a2713aSLionel Sambuc   }
1709f4a2713aSLionel Sambuc 
1710f4a2713aSLionel Sambuc   // Output to a temporary file?
1711f4a2713aSLionel Sambuc   if ((!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps) &&
1712f4a2713aSLionel Sambuc         !C.getArgs().hasArg(options::OPT__SLASH_Fo)) ||
1713f4a2713aSLionel Sambuc       CCGenDiagnostics) {
1714f4a2713aSLionel Sambuc     StringRef Name = llvm::sys::path::filename(BaseInput);
1715f4a2713aSLionel Sambuc     std::pair<StringRef, StringRef> Split = Name.split('.');
1716f4a2713aSLionel Sambuc     std::string TmpName =
1717f4a2713aSLionel Sambuc       GetTemporaryPath(Split.first,
1718f4a2713aSLionel Sambuc           types::getTypeTempSuffix(JA.getType(), IsCLMode()));
1719f4a2713aSLionel Sambuc     return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
1720f4a2713aSLionel Sambuc   }
1721f4a2713aSLionel Sambuc 
1722f4a2713aSLionel Sambuc   SmallString<128> BasePath(BaseInput);
1723f4a2713aSLionel Sambuc   StringRef BaseName;
1724f4a2713aSLionel Sambuc 
1725f4a2713aSLionel Sambuc   // Dsymutil actions should use the full path.
1726f4a2713aSLionel Sambuc   if (isa<DsymutilJobAction>(JA) || isa<VerifyJobAction>(JA))
1727f4a2713aSLionel Sambuc     BaseName = BasePath;
1728f4a2713aSLionel Sambuc   else
1729f4a2713aSLionel Sambuc     BaseName = llvm::sys::path::filename(BasePath);
1730f4a2713aSLionel Sambuc 
1731f4a2713aSLionel Sambuc   // Determine what the derived output name should be.
1732f4a2713aSLionel Sambuc   const char *NamedOutput;
1733f4a2713aSLionel Sambuc 
1734f4a2713aSLionel Sambuc   if (JA.getType() == types::TY_Object &&
1735*0a6a1f1dSLionel Sambuc       C.getArgs().hasArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)) {
1736*0a6a1f1dSLionel Sambuc     // The /Fo or /o flag decides the object filename.
1737*0a6a1f1dSLionel Sambuc     StringRef Val = C.getArgs().getLastArg(options::OPT__SLASH_Fo,
1738*0a6a1f1dSLionel Sambuc                                            options::OPT__SLASH_o)->getValue();
1739f4a2713aSLionel Sambuc     NamedOutput = MakeCLOutputFilename(C.getArgs(), Val, BaseName,
1740f4a2713aSLionel Sambuc                                        types::TY_Object);
1741f4a2713aSLionel Sambuc   } else if (JA.getType() == types::TY_Image &&
1742*0a6a1f1dSLionel Sambuc              C.getArgs().hasArg(options::OPT__SLASH_Fe, options::OPT__SLASH_o)) {
1743*0a6a1f1dSLionel Sambuc     // The /Fe or /o flag names the linked file.
1744*0a6a1f1dSLionel Sambuc     StringRef Val = C.getArgs().getLastArg(options::OPT__SLASH_Fe,
1745*0a6a1f1dSLionel Sambuc                                            options::OPT__SLASH_o)->getValue();
1746f4a2713aSLionel Sambuc     NamedOutput = MakeCLOutputFilename(C.getArgs(), Val, BaseName,
1747f4a2713aSLionel Sambuc                                        types::TY_Image);
1748f4a2713aSLionel Sambuc   } else if (JA.getType() == types::TY_Image) {
1749f4a2713aSLionel Sambuc     if (IsCLMode()) {
1750f4a2713aSLionel Sambuc       // clang-cl uses BaseName for the executable name.
1751f4a2713aSLionel Sambuc       NamedOutput = MakeCLOutputFilename(C.getArgs(), "", BaseName,
1752f4a2713aSLionel Sambuc                                          types::TY_Image);
1753f4a2713aSLionel Sambuc     } else if (MultipleArchs && BoundArch) {
1754*0a6a1f1dSLionel Sambuc       SmallString<128> Output(getDefaultImageName());
1755f4a2713aSLionel Sambuc       Output += "-";
1756f4a2713aSLionel Sambuc       Output.append(BoundArch);
1757f4a2713aSLionel Sambuc       NamedOutput = C.getArgs().MakeArgString(Output.c_str());
1758f4a2713aSLionel Sambuc     } else
1759*0a6a1f1dSLionel Sambuc       NamedOutput = getDefaultImageName();
1760f4a2713aSLionel Sambuc   } else {
1761f4a2713aSLionel Sambuc     const char *Suffix = types::getTypeTempSuffix(JA.getType(), IsCLMode());
1762f4a2713aSLionel Sambuc     assert(Suffix && "All types used for output should have a suffix.");
1763f4a2713aSLionel Sambuc 
1764f4a2713aSLionel Sambuc     std::string::size_type End = std::string::npos;
1765f4a2713aSLionel Sambuc     if (!types::appendSuffixForType(JA.getType()))
1766f4a2713aSLionel Sambuc       End = BaseName.rfind('.');
1767f4a2713aSLionel Sambuc     SmallString<128> Suffixed(BaseName.substr(0, End));
1768f4a2713aSLionel Sambuc     if (MultipleArchs && BoundArch) {
1769f4a2713aSLionel Sambuc       Suffixed += "-";
1770f4a2713aSLionel Sambuc       Suffixed.append(BoundArch);
1771f4a2713aSLionel Sambuc     }
1772*0a6a1f1dSLionel Sambuc     // When using both -save-temps and -emit-llvm, use a ".tmp.bc" suffix for
1773*0a6a1f1dSLionel Sambuc     // the unoptimized bitcode so that it does not get overwritten by the ".bc"
1774*0a6a1f1dSLionel Sambuc     // optimized bitcode output.
1775*0a6a1f1dSLionel Sambuc     if (!AtTopLevel && C.getArgs().hasArg(options::OPT_emit_llvm) &&
1776*0a6a1f1dSLionel Sambuc         JA.getType() == types::TY_LLVM_BC)
1777*0a6a1f1dSLionel Sambuc       Suffixed += ".tmp";
1778f4a2713aSLionel Sambuc     Suffixed += '.';
1779f4a2713aSLionel Sambuc     Suffixed += Suffix;
1780f4a2713aSLionel Sambuc     NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1781f4a2713aSLionel Sambuc   }
1782f4a2713aSLionel Sambuc 
1783f4a2713aSLionel Sambuc   // If we're saving temps and the temp file conflicts with the input file,
1784f4a2713aSLionel Sambuc   // then avoid overwriting input file.
1785f4a2713aSLionel Sambuc   if (!AtTopLevel && C.getArgs().hasArg(options::OPT_save_temps) &&
1786f4a2713aSLionel Sambuc       NamedOutput == BaseName) {
1787f4a2713aSLionel Sambuc 
1788f4a2713aSLionel Sambuc     bool SameFile = false;
1789f4a2713aSLionel Sambuc     SmallString<256> Result;
1790f4a2713aSLionel Sambuc     llvm::sys::fs::current_path(Result);
1791f4a2713aSLionel Sambuc     llvm::sys::path::append(Result, BaseName);
1792f4a2713aSLionel Sambuc     llvm::sys::fs::equivalent(BaseInput, Result.c_str(), SameFile);
1793f4a2713aSLionel Sambuc     // Must share the same path to conflict.
1794f4a2713aSLionel Sambuc     if (SameFile) {
1795f4a2713aSLionel Sambuc       StringRef Name = llvm::sys::path::filename(BaseInput);
1796f4a2713aSLionel Sambuc       std::pair<StringRef, StringRef> Split = Name.split('.');
1797f4a2713aSLionel Sambuc       std::string TmpName =
1798f4a2713aSLionel Sambuc         GetTemporaryPath(Split.first,
1799f4a2713aSLionel Sambuc             types::getTypeTempSuffix(JA.getType(), IsCLMode()));
1800f4a2713aSLionel Sambuc       return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
1801f4a2713aSLionel Sambuc     }
1802f4a2713aSLionel Sambuc   }
1803f4a2713aSLionel Sambuc 
1804f4a2713aSLionel Sambuc   // As an annoying special case, PCH generation doesn't strip the pathname.
1805f4a2713aSLionel Sambuc   if (JA.getType() == types::TY_PCH) {
1806f4a2713aSLionel Sambuc     llvm::sys::path::remove_filename(BasePath);
1807f4a2713aSLionel Sambuc     if (BasePath.empty())
1808f4a2713aSLionel Sambuc       BasePath = NamedOutput;
1809f4a2713aSLionel Sambuc     else
1810f4a2713aSLionel Sambuc       llvm::sys::path::append(BasePath, NamedOutput);
1811f4a2713aSLionel Sambuc     return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()), &JA);
1812f4a2713aSLionel Sambuc   } else {
1813f4a2713aSLionel Sambuc     return C.addResultFile(NamedOutput, &JA);
1814f4a2713aSLionel Sambuc   }
1815f4a2713aSLionel Sambuc }
1816f4a2713aSLionel Sambuc 
GetFilePath(const char * Name,const ToolChain & TC) const1817f4a2713aSLionel Sambuc std::string Driver::GetFilePath(const char *Name, const ToolChain &TC) const {
1818f4a2713aSLionel Sambuc   // Respect a limited subset of the '-Bprefix' functionality in GCC by
1819f4a2713aSLionel Sambuc   // attempting to use this prefix when looking for file paths.
1820f4a2713aSLionel Sambuc   for (Driver::prefix_list::const_iterator it = PrefixDirs.begin(),
1821f4a2713aSLionel Sambuc        ie = PrefixDirs.end(); it != ie; ++it) {
1822f4a2713aSLionel Sambuc     std::string Dir(*it);
1823f4a2713aSLionel Sambuc     if (Dir.empty())
1824f4a2713aSLionel Sambuc       continue;
1825f4a2713aSLionel Sambuc     if (Dir[0] == '=')
1826f4a2713aSLionel Sambuc       Dir = SysRoot + Dir.substr(1);
1827f4a2713aSLionel Sambuc     SmallString<128> P(Dir);
1828f4a2713aSLionel Sambuc     llvm::sys::path::append(P, Name);
1829f4a2713aSLionel Sambuc     if (llvm::sys::fs::exists(Twine(P)))
1830f4a2713aSLionel Sambuc       return P.str();
1831f4a2713aSLionel Sambuc   }
1832f4a2713aSLionel Sambuc 
1833f4a2713aSLionel Sambuc   SmallString<128> P(ResourceDir);
1834f4a2713aSLionel Sambuc   llvm::sys::path::append(P, Name);
1835f4a2713aSLionel Sambuc   if (llvm::sys::fs::exists(Twine(P)))
1836f4a2713aSLionel Sambuc     return P.str();
1837f4a2713aSLionel Sambuc 
1838f4a2713aSLionel Sambuc   const ToolChain::path_list &List = TC.getFilePaths();
1839f4a2713aSLionel Sambuc   for (ToolChain::path_list::const_iterator
1840f4a2713aSLionel Sambuc          it = List.begin(), ie = List.end(); it != ie; ++it) {
1841f4a2713aSLionel Sambuc     std::string Dir(*it);
1842f4a2713aSLionel Sambuc     if (Dir.empty())
1843f4a2713aSLionel Sambuc       continue;
1844f4a2713aSLionel Sambuc     if (Dir[0] == '=')
1845f4a2713aSLionel Sambuc       Dir = SysRoot + Dir.substr(1);
1846f4a2713aSLionel Sambuc     SmallString<128> P(Dir);
1847f4a2713aSLionel Sambuc     llvm::sys::path::append(P, Name);
1848f4a2713aSLionel Sambuc     if (llvm::sys::fs::exists(Twine(P)))
1849f4a2713aSLionel Sambuc       return P.str();
1850f4a2713aSLionel Sambuc   }
1851f4a2713aSLionel Sambuc 
1852f4a2713aSLionel Sambuc   return Name;
1853f4a2713aSLionel Sambuc }
1854f4a2713aSLionel Sambuc 
1855*0a6a1f1dSLionel Sambuc void
generatePrefixedToolNames(const char * Tool,const ToolChain & TC,SmallVectorImpl<std::string> & Names) const1856*0a6a1f1dSLionel Sambuc Driver::generatePrefixedToolNames(const char *Tool, const ToolChain &TC,
1857*0a6a1f1dSLionel Sambuc                                   SmallVectorImpl<std::string> &Names) const {
1858*0a6a1f1dSLionel Sambuc   // FIXME: Needs a better variable than DefaultTargetTriple
1859*0a6a1f1dSLionel Sambuc   Names.push_back(DefaultTargetTriple + "-" + Tool);
1860*0a6a1f1dSLionel Sambuc   Names.push_back(Tool);
1861*0a6a1f1dSLionel Sambuc }
1862*0a6a1f1dSLionel Sambuc 
ScanDirForExecutable(SmallString<128> & Dir,ArrayRef<std::string> Names)1863*0a6a1f1dSLionel Sambuc static bool ScanDirForExecutable(SmallString<128> &Dir,
1864*0a6a1f1dSLionel Sambuc                                  ArrayRef<std::string> Names) {
1865*0a6a1f1dSLionel Sambuc   for (const auto &Name : Names) {
1866*0a6a1f1dSLionel Sambuc     llvm::sys::path::append(Dir, Name);
1867*0a6a1f1dSLionel Sambuc     if (llvm::sys::fs::can_execute(Twine(Dir)))
1868*0a6a1f1dSLionel Sambuc       return true;
1869*0a6a1f1dSLionel Sambuc     llvm::sys::path::remove_filename(Dir);
1870*0a6a1f1dSLionel Sambuc   }
1871*0a6a1f1dSLionel Sambuc   return false;
1872*0a6a1f1dSLionel Sambuc }
1873*0a6a1f1dSLionel Sambuc 
GetProgramPath(const char * Name,const ToolChain & TC) const1874f4a2713aSLionel Sambuc std::string Driver::GetProgramPath(const char *Name,
1875f4a2713aSLionel Sambuc                                    const ToolChain &TC) const {
1876*0a6a1f1dSLionel Sambuc   SmallVector<std::string, 2> TargetSpecificExecutables;
1877*0a6a1f1dSLionel Sambuc   generatePrefixedToolNames(Name, TC, TargetSpecificExecutables);
1878*0a6a1f1dSLionel Sambuc 
1879f4a2713aSLionel Sambuc   // Respect a limited subset of the '-Bprefix' functionality in GCC by
1880f4a2713aSLionel Sambuc   // attempting to use this prefix when looking for program paths.
1881*0a6a1f1dSLionel Sambuc   for (const auto &PrefixDir : PrefixDirs) {
1882*0a6a1f1dSLionel Sambuc     if (llvm::sys::fs::is_directory(PrefixDir)) {
1883*0a6a1f1dSLionel Sambuc       SmallString<128> P(PrefixDir);
1884*0a6a1f1dSLionel Sambuc       if (ScanDirForExecutable(P, TargetSpecificExecutables))
1885f4a2713aSLionel Sambuc         return P.str();
1886f4a2713aSLionel Sambuc     } else {
1887*0a6a1f1dSLionel Sambuc       SmallString<128> P(PrefixDir + Name);
1888f4a2713aSLionel Sambuc       if (llvm::sys::fs::can_execute(Twine(P)))
1889f4a2713aSLionel Sambuc         return P.str();
1890f4a2713aSLionel Sambuc     }
1891f4a2713aSLionel Sambuc   }
1892f4a2713aSLionel Sambuc 
1893f4a2713aSLionel Sambuc   const ToolChain::path_list &List = TC.getProgramPaths();
1894*0a6a1f1dSLionel Sambuc   for (const auto &Path : List) {
1895*0a6a1f1dSLionel Sambuc     SmallString<128> P(Path);
1896*0a6a1f1dSLionel Sambuc     if (ScanDirForExecutable(P, TargetSpecificExecutables))
1897f4a2713aSLionel Sambuc       return P.str();
1898f4a2713aSLionel Sambuc   }
1899f4a2713aSLionel Sambuc 
1900f4a2713aSLionel Sambuc   // If all else failed, search the path.
1901*0a6a1f1dSLionel Sambuc   for (const auto &TargetSpecificExecutable : TargetSpecificExecutables)
1902*0a6a1f1dSLionel Sambuc     if (llvm::ErrorOr<std::string> P =
1903*0a6a1f1dSLionel Sambuc             llvm::sys::findProgramByName(TargetSpecificExecutable))
1904*0a6a1f1dSLionel Sambuc       return *P;
1905f4a2713aSLionel Sambuc 
1906f4a2713aSLionel Sambuc   return Name;
1907f4a2713aSLionel Sambuc }
1908f4a2713aSLionel Sambuc 
GetTemporaryPath(StringRef Prefix,const char * Suffix) const1909f4a2713aSLionel Sambuc std::string Driver::GetTemporaryPath(StringRef Prefix, const char *Suffix)
1910f4a2713aSLionel Sambuc   const {
1911f4a2713aSLionel Sambuc   SmallString<128> Path;
1912*0a6a1f1dSLionel Sambuc   std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, Path);
1913f4a2713aSLionel Sambuc   if (EC) {
1914f4a2713aSLionel Sambuc     Diag(clang::diag::err_unable_to_make_temp) << EC.message();
1915f4a2713aSLionel Sambuc     return "";
1916f4a2713aSLionel Sambuc   }
1917f4a2713aSLionel Sambuc 
1918f4a2713aSLionel Sambuc   return Path.str();
1919f4a2713aSLionel Sambuc }
1920f4a2713aSLionel Sambuc 
1921f4a2713aSLionel Sambuc /// \brief Compute target triple from args.
1922f4a2713aSLionel Sambuc ///
1923f4a2713aSLionel Sambuc /// This routine provides the logic to compute a target triple from various
1924f4a2713aSLionel Sambuc /// args passed to the driver and the default triple string.
computeTargetTriple(StringRef DefaultTargetTriple,const ArgList & Args,StringRef DarwinArchName)1925f4a2713aSLionel Sambuc static llvm::Triple computeTargetTriple(StringRef DefaultTargetTriple,
1926f4a2713aSLionel Sambuc                                         const ArgList &Args,
1927f4a2713aSLionel Sambuc                                         StringRef DarwinArchName) {
1928f4a2713aSLionel Sambuc   // FIXME: Already done in Compilation *Driver::BuildCompilation
1929f4a2713aSLionel Sambuc   if (const Arg *A = Args.getLastArg(options::OPT_target))
1930f4a2713aSLionel Sambuc     DefaultTargetTriple = A->getValue();
1931f4a2713aSLionel Sambuc 
1932f4a2713aSLionel Sambuc   llvm::Triple Target(llvm::Triple::normalize(DefaultTargetTriple));
1933f4a2713aSLionel Sambuc 
1934*0a6a1f1dSLionel Sambuc   // Handle Apple-specific options available here.
1935*0a6a1f1dSLionel Sambuc   if (Target.isOSBinFormatMachO()) {
1936f4a2713aSLionel Sambuc     // If an explict Darwin arch name is given, that trumps all.
1937f4a2713aSLionel Sambuc     if (!DarwinArchName.empty()) {
1938*0a6a1f1dSLionel Sambuc       tools::darwin::setTripleTypeForMachOArchName(Target, DarwinArchName);
1939f4a2713aSLionel Sambuc       return Target;
1940f4a2713aSLionel Sambuc     }
1941f4a2713aSLionel Sambuc 
1942f4a2713aSLionel Sambuc     // Handle the Darwin '-arch' flag.
1943f4a2713aSLionel Sambuc     if (Arg *A = Args.getLastArg(options::OPT_arch)) {
1944*0a6a1f1dSLionel Sambuc       StringRef ArchName = A->getValue();
1945*0a6a1f1dSLionel Sambuc       tools::darwin::setTripleTypeForMachOArchName(Target, ArchName);
1946f4a2713aSLionel Sambuc     }
1947f4a2713aSLionel Sambuc   }
1948f4a2713aSLionel Sambuc 
1949*0a6a1f1dSLionel Sambuc   // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
1950*0a6a1f1dSLionel Sambuc   // '-mbig-endian'/'-EB'.
1951*0a6a1f1dSLionel Sambuc   if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
1952*0a6a1f1dSLionel Sambuc                                options::OPT_mbig_endian)) {
1953*0a6a1f1dSLionel Sambuc     if (A->getOption().matches(options::OPT_mlittle_endian)) {
1954f4a2713aSLionel Sambuc       if (Target.getArch() == llvm::Triple::mips)
1955f4a2713aSLionel Sambuc         Target.setArch(llvm::Triple::mipsel);
1956f4a2713aSLionel Sambuc       else if (Target.getArch() == llvm::Triple::mips64)
1957f4a2713aSLionel Sambuc         Target.setArch(llvm::Triple::mips64el);
1958*0a6a1f1dSLionel Sambuc       else if (Target.getArch() == llvm::Triple::aarch64_be)
1959*0a6a1f1dSLionel Sambuc         Target.setArch(llvm::Triple::aarch64);
1960f4a2713aSLionel Sambuc     } else {
1961f4a2713aSLionel Sambuc       if (Target.getArch() == llvm::Triple::mipsel)
1962f4a2713aSLionel Sambuc         Target.setArch(llvm::Triple::mips);
1963f4a2713aSLionel Sambuc       else if (Target.getArch() == llvm::Triple::mips64el)
1964f4a2713aSLionel Sambuc         Target.setArch(llvm::Triple::mips64);
1965*0a6a1f1dSLionel Sambuc       else if (Target.getArch() == llvm::Triple::aarch64)
1966*0a6a1f1dSLionel Sambuc         Target.setArch(llvm::Triple::aarch64_be);
1967f4a2713aSLionel Sambuc     }
1968f4a2713aSLionel Sambuc   }
1969f4a2713aSLionel Sambuc 
1970f4a2713aSLionel Sambuc   // Skip further flag support on OSes which don't support '-m32' or '-m64'.
1971*0a6a1f1dSLionel Sambuc   if (Target.getArchName() == "tce" || Target.getOS() == llvm::Triple::Minix)
1972f4a2713aSLionel Sambuc     return Target;
1973f4a2713aSLionel Sambuc 
1974*0a6a1f1dSLionel Sambuc   // Handle pseudo-target flags '-m64', '-mx32', '-m32' and '-m16'.
1975*0a6a1f1dSLionel Sambuc   if (Arg *A = Args.getLastArg(options::OPT_m64, options::OPT_mx32,
1976*0a6a1f1dSLionel Sambuc                                options::OPT_m32, options::OPT_m16)) {
1977*0a6a1f1dSLionel Sambuc     llvm::Triple::ArchType AT = llvm::Triple::UnknownArch;
1978*0a6a1f1dSLionel Sambuc 
1979*0a6a1f1dSLionel Sambuc     if (A->getOption().matches(options::OPT_m64)) {
1980*0a6a1f1dSLionel Sambuc       AT = Target.get64BitArchVariant().getArch();
1981*0a6a1f1dSLionel Sambuc       if (Target.getEnvironment() == llvm::Triple::GNUX32)
1982*0a6a1f1dSLionel Sambuc         Target.setEnvironment(llvm::Triple::GNU);
1983*0a6a1f1dSLionel Sambuc     } else if (A->getOption().matches(options::OPT_mx32) &&
1984*0a6a1f1dSLionel Sambuc              Target.get64BitArchVariant().getArch() == llvm::Triple::x86_64) {
1985*0a6a1f1dSLionel Sambuc       AT = llvm::Triple::x86_64;
1986*0a6a1f1dSLionel Sambuc       Target.setEnvironment(llvm::Triple::GNUX32);
1987*0a6a1f1dSLionel Sambuc     } else if (A->getOption().matches(options::OPT_m32)) {
1988*0a6a1f1dSLionel Sambuc       AT = Target.get32BitArchVariant().getArch();
1989*0a6a1f1dSLionel Sambuc       if (Target.getEnvironment() == llvm::Triple::GNUX32)
1990*0a6a1f1dSLionel Sambuc         Target.setEnvironment(llvm::Triple::GNU);
1991*0a6a1f1dSLionel Sambuc     } else if (A->getOption().matches(options::OPT_m16) &&
1992*0a6a1f1dSLionel Sambuc              Target.get32BitArchVariant().getArch() == llvm::Triple::x86) {
1993*0a6a1f1dSLionel Sambuc       AT = llvm::Triple::x86;
1994*0a6a1f1dSLionel Sambuc       Target.setEnvironment(llvm::Triple::CODE16);
1995f4a2713aSLionel Sambuc     }
1996*0a6a1f1dSLionel Sambuc 
1997*0a6a1f1dSLionel Sambuc     if (AT != llvm::Triple::UnknownArch && AT != Target.getArch())
1998*0a6a1f1dSLionel Sambuc       Target.setArch(AT);
1999f4a2713aSLionel Sambuc   }
2000f4a2713aSLionel Sambuc 
2001f4a2713aSLionel Sambuc   return Target;
2002f4a2713aSLionel Sambuc }
2003f4a2713aSLionel Sambuc 
getToolChain(const ArgList & Args,StringRef DarwinArchName) const2004f4a2713aSLionel Sambuc const ToolChain &Driver::getToolChain(const ArgList &Args,
2005f4a2713aSLionel Sambuc                                       StringRef DarwinArchName) const {
2006f4a2713aSLionel Sambuc   llvm::Triple Target = computeTargetTriple(DefaultTargetTriple, Args,
2007f4a2713aSLionel Sambuc                                             DarwinArchName);
2008f4a2713aSLionel Sambuc 
2009f4a2713aSLionel Sambuc   ToolChain *&TC = ToolChains[Target.str()];
2010f4a2713aSLionel Sambuc   if (!TC) {
2011f4a2713aSLionel Sambuc     switch (Target.getOS()) {
2012f4a2713aSLionel Sambuc     case llvm::Triple::Darwin:
2013f4a2713aSLionel Sambuc     case llvm::Triple::MacOSX:
2014f4a2713aSLionel Sambuc     case llvm::Triple::IOS:
2015f4a2713aSLionel Sambuc       TC = new toolchains::DarwinClang(*this, Target, Args);
2016f4a2713aSLionel Sambuc       break;
2017f4a2713aSLionel Sambuc     case llvm::Triple::DragonFly:
2018f4a2713aSLionel Sambuc       TC = new toolchains::DragonFly(*this, Target, Args);
2019f4a2713aSLionel Sambuc       break;
2020f4a2713aSLionel Sambuc     case llvm::Triple::OpenBSD:
2021f4a2713aSLionel Sambuc       TC = new toolchains::OpenBSD(*this, Target, Args);
2022f4a2713aSLionel Sambuc       break;
2023f4a2713aSLionel Sambuc     case llvm::Triple::Bitrig:
2024f4a2713aSLionel Sambuc       TC = new toolchains::Bitrig(*this, Target, Args);
2025f4a2713aSLionel Sambuc       break;
2026f4a2713aSLionel Sambuc     case llvm::Triple::NetBSD:
2027f4a2713aSLionel Sambuc       TC = new toolchains::NetBSD(*this, Target, Args);
2028f4a2713aSLionel Sambuc       break;
2029f4a2713aSLionel Sambuc     case llvm::Triple::FreeBSD:
2030f4a2713aSLionel Sambuc       TC = new toolchains::FreeBSD(*this, Target, Args);
2031f4a2713aSLionel Sambuc       break;
2032f4a2713aSLionel Sambuc     case llvm::Triple::Minix:
2033f4a2713aSLionel Sambuc       TC = new toolchains::Minix(*this, Target, Args);
2034f4a2713aSLionel Sambuc       break;
2035f4a2713aSLionel Sambuc     case llvm::Triple::Linux:
2036f4a2713aSLionel Sambuc       if (Target.getArch() == llvm::Triple::hexagon)
2037f4a2713aSLionel Sambuc         TC = new toolchains::Hexagon_TC(*this, Target, Args);
2038f4a2713aSLionel Sambuc       else
2039f4a2713aSLionel Sambuc         TC = new toolchains::Linux(*this, Target, Args);
2040f4a2713aSLionel Sambuc       break;
2041f4a2713aSLionel Sambuc     case llvm::Triple::Solaris:
2042f4a2713aSLionel Sambuc       TC = new toolchains::Solaris(*this, Target, Args);
2043f4a2713aSLionel Sambuc       break;
2044f4a2713aSLionel Sambuc     case llvm::Triple::Win32:
2045*0a6a1f1dSLionel Sambuc       switch (Target.getEnvironment()) {
2046*0a6a1f1dSLionel Sambuc       default:
2047*0a6a1f1dSLionel Sambuc         if (Target.isOSBinFormatELF())
2048*0a6a1f1dSLionel Sambuc           TC = new toolchains::Generic_ELF(*this, Target, Args);
2049*0a6a1f1dSLionel Sambuc         else if (Target.isOSBinFormatMachO())
2050*0a6a1f1dSLionel Sambuc           TC = new toolchains::MachO(*this, Target, Args);
2051*0a6a1f1dSLionel Sambuc         else
2052*0a6a1f1dSLionel Sambuc           TC = new toolchains::Generic_GCC(*this, Target, Args);
2053f4a2713aSLionel Sambuc         break;
2054*0a6a1f1dSLionel Sambuc       case llvm::Triple::GNU:
2055*0a6a1f1dSLionel Sambuc         // FIXME: We need a MinGW toolchain.  Use the default Generic_GCC
2056*0a6a1f1dSLionel Sambuc         // toolchain for now as the default case would below otherwise.
2057*0a6a1f1dSLionel Sambuc         if (Target.isOSBinFormatELF())
2058*0a6a1f1dSLionel Sambuc           TC = new toolchains::Generic_ELF(*this, Target, Args);
2059*0a6a1f1dSLionel Sambuc         else
2060*0a6a1f1dSLionel Sambuc           TC = new toolchains::Generic_GCC(*this, Target, Args);
2061*0a6a1f1dSLionel Sambuc         break;
2062*0a6a1f1dSLionel Sambuc       case llvm::Triple::Itanium:
2063*0a6a1f1dSLionel Sambuc         TC = new toolchains::CrossWindowsToolChain(*this, Target, Args);
2064*0a6a1f1dSLionel Sambuc         break;
2065*0a6a1f1dSLionel Sambuc       case llvm::Triple::MSVC:
2066*0a6a1f1dSLionel Sambuc       case llvm::Triple::UnknownEnvironment:
2067*0a6a1f1dSLionel Sambuc         TC = new toolchains::MSVCToolChain(*this, Target, Args);
2068*0a6a1f1dSLionel Sambuc         break;
2069*0a6a1f1dSLionel Sambuc       }
2070*0a6a1f1dSLionel Sambuc       break;
2071f4a2713aSLionel Sambuc     default:
2072f4a2713aSLionel Sambuc       // TCE is an OSless target
2073f4a2713aSLionel Sambuc       if (Target.getArchName() == "tce") {
2074f4a2713aSLionel Sambuc         TC = new toolchains::TCEToolChain(*this, Target, Args);
2075f4a2713aSLionel Sambuc         break;
2076f4a2713aSLionel Sambuc       }
2077f4a2713aSLionel Sambuc       // If Hexagon is configured as an OSless target
2078f4a2713aSLionel Sambuc       if (Target.getArch() == llvm::Triple::hexagon) {
2079f4a2713aSLionel Sambuc         TC = new toolchains::Hexagon_TC(*this, Target, Args);
2080f4a2713aSLionel Sambuc         break;
2081f4a2713aSLionel Sambuc       }
2082f4a2713aSLionel Sambuc       if (Target.getArch() == llvm::Triple::xcore) {
2083f4a2713aSLionel Sambuc         TC = new toolchains::XCore(*this, Target, Args);
2084f4a2713aSLionel Sambuc         break;
2085f4a2713aSLionel Sambuc       }
2086*0a6a1f1dSLionel Sambuc       if (Target.isOSBinFormatELF()) {
2087*0a6a1f1dSLionel Sambuc         TC = new toolchains::Generic_ELF(*this, Target, Args);
2088*0a6a1f1dSLionel Sambuc         break;
2089*0a6a1f1dSLionel Sambuc       }
2090*0a6a1f1dSLionel Sambuc       if (Target.isOSBinFormatMachO()) {
2091*0a6a1f1dSLionel Sambuc         TC = new toolchains::MachO(*this, Target, Args);
2092*0a6a1f1dSLionel Sambuc         break;
2093*0a6a1f1dSLionel Sambuc       }
2094f4a2713aSLionel Sambuc       TC = new toolchains::Generic_GCC(*this, Target, Args);
2095f4a2713aSLionel Sambuc       break;
2096f4a2713aSLionel Sambuc     }
2097f4a2713aSLionel Sambuc   }
2098f4a2713aSLionel Sambuc   return *TC;
2099f4a2713aSLionel Sambuc }
2100f4a2713aSLionel Sambuc 
ShouldUseClangCompiler(const JobAction & JA) const2101f4a2713aSLionel Sambuc bool Driver::ShouldUseClangCompiler(const JobAction &JA) const {
2102f4a2713aSLionel Sambuc   // Check if user requested no clang, or clang doesn't understand this type (we
2103f4a2713aSLionel Sambuc   // only handle single inputs for now).
2104f4a2713aSLionel Sambuc   if (JA.size() != 1 ||
2105f4a2713aSLionel Sambuc       !types::isAcceptedByClang((*JA.begin())->getType()))
2106f4a2713aSLionel Sambuc     return false;
2107f4a2713aSLionel Sambuc 
2108f4a2713aSLionel Sambuc   // Otherwise make sure this is an action clang understands.
2109f4a2713aSLionel Sambuc   if (!isa<PreprocessJobAction>(JA) && !isa<PrecompileJobAction>(JA) &&
2110*0a6a1f1dSLionel Sambuc       !isa<CompileJobAction>(JA) && !isa<BackendJobAction>(JA))
2111f4a2713aSLionel Sambuc     return false;
2112f4a2713aSLionel Sambuc 
2113f4a2713aSLionel Sambuc   return true;
2114f4a2713aSLionel Sambuc }
2115f4a2713aSLionel Sambuc 
2116f4a2713aSLionel Sambuc /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
2117f4a2713aSLionel Sambuc /// grouped values as integers. Numbers which are not provided are set to 0.
2118f4a2713aSLionel Sambuc ///
2119f4a2713aSLionel Sambuc /// \return True if the entire string was parsed (9.2), or all groups were
2120f4a2713aSLionel Sambuc /// parsed (10.3.5extrastuff).
GetReleaseVersion(const char * Str,unsigned & Major,unsigned & Minor,unsigned & Micro,bool & HadExtra)2121f4a2713aSLionel Sambuc bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
2122f4a2713aSLionel Sambuc                                unsigned &Minor, unsigned &Micro,
2123f4a2713aSLionel Sambuc                                bool &HadExtra) {
2124f4a2713aSLionel Sambuc   HadExtra = false;
2125f4a2713aSLionel Sambuc 
2126f4a2713aSLionel Sambuc   Major = Minor = Micro = 0;
2127f4a2713aSLionel Sambuc   if (*Str == '\0')
2128f4a2713aSLionel Sambuc     return true;
2129f4a2713aSLionel Sambuc 
2130f4a2713aSLionel Sambuc   char *End;
2131f4a2713aSLionel Sambuc   Major = (unsigned) strtol(Str, &End, 10);
2132f4a2713aSLionel Sambuc   if (*Str != '\0' && *End == '\0')
2133f4a2713aSLionel Sambuc     return true;
2134f4a2713aSLionel Sambuc   if (*End != '.')
2135f4a2713aSLionel Sambuc     return false;
2136f4a2713aSLionel Sambuc 
2137f4a2713aSLionel Sambuc   Str = End+1;
2138f4a2713aSLionel Sambuc   Minor = (unsigned) strtol(Str, &End, 10);
2139f4a2713aSLionel Sambuc   if (*Str != '\0' && *End == '\0')
2140f4a2713aSLionel Sambuc     return true;
2141f4a2713aSLionel Sambuc   if (*End != '.')
2142f4a2713aSLionel Sambuc     return false;
2143f4a2713aSLionel Sambuc 
2144f4a2713aSLionel Sambuc   Str = End+1;
2145f4a2713aSLionel Sambuc   Micro = (unsigned) strtol(Str, &End, 10);
2146f4a2713aSLionel Sambuc   if (*Str != '\0' && *End == '\0')
2147f4a2713aSLionel Sambuc     return true;
2148f4a2713aSLionel Sambuc   if (Str == End)
2149f4a2713aSLionel Sambuc     return false;
2150f4a2713aSLionel Sambuc   HadExtra = true;
2151f4a2713aSLionel Sambuc   return true;
2152f4a2713aSLionel Sambuc }
2153f4a2713aSLionel Sambuc 
getIncludeExcludeOptionFlagMasks() const2154f4a2713aSLionel Sambuc std::pair<unsigned, unsigned> Driver::getIncludeExcludeOptionFlagMasks() const {
2155f4a2713aSLionel Sambuc   unsigned IncludedFlagsBitmask = 0;
2156f4a2713aSLionel Sambuc   unsigned ExcludedFlagsBitmask = options::NoDriverOption;
2157f4a2713aSLionel Sambuc 
2158f4a2713aSLionel Sambuc   if (Mode == CLMode) {
2159f4a2713aSLionel Sambuc     // Include CL and Core options.
2160f4a2713aSLionel Sambuc     IncludedFlagsBitmask |= options::CLOption;
2161f4a2713aSLionel Sambuc     IncludedFlagsBitmask |= options::CoreOption;
2162f4a2713aSLionel Sambuc   } else {
2163f4a2713aSLionel Sambuc     ExcludedFlagsBitmask |= options::CLOption;
2164f4a2713aSLionel Sambuc   }
2165f4a2713aSLionel Sambuc 
2166f4a2713aSLionel Sambuc   return std::make_pair(IncludedFlagsBitmask, ExcludedFlagsBitmask);
2167f4a2713aSLionel Sambuc }
2168*0a6a1f1dSLionel Sambuc 
isOptimizationLevelFast(const llvm::opt::ArgList & Args)2169*0a6a1f1dSLionel Sambuc bool clang::driver::isOptimizationLevelFast(const llvm::opt::ArgList &Args) {
2170*0a6a1f1dSLionel Sambuc   return Args.hasFlag(options::OPT_Ofast, options::OPT_O_Group, false);
2171*0a6a1f1dSLionel Sambuc }
2172