1 //===--- Driver.cpp - Clang GCC Compatible Driver -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Driver/Driver.h"
11 #include "InputInfo.h"
12 #include "ToolChains.h"
13 #include "clang/Basic/Version.h"
14 #include "clang/Basic/VirtualFileSystem.h"
15 #include "clang/Config/config.h"
16 #include "clang/Driver/Action.h"
17 #include "clang/Driver/Compilation.h"
18 #include "clang/Driver/DriverDiagnostic.h"
19 #include "clang/Driver/Job.h"
20 #include "clang/Driver/Options.h"
21 #include "clang/Driver/SanitizerArgs.h"
22 #include "clang/Driver/Tool.h"
23 #include "clang/Driver/ToolChain.h"
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallSet.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/ADT/StringSet.h"
29 #include "llvm/ADT/StringSwitch.h"
30 #include "llvm/Option/Arg.h"
31 #include "llvm/Option/ArgList.h"
32 #include "llvm/Option/OptSpecifier.h"
33 #include "llvm/Option/OptTable.h"
34 #include "llvm/Option/Option.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/FileSystem.h"
37 #include "llvm/Support/Path.h"
38 #include "llvm/Support/PrettyStackTrace.h"
39 #include "llvm/Support/Process.h"
40 #include "llvm/Support/Program.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include <map>
43 #include <memory>
44 #include <utility>
45 
46 using namespace clang::driver;
47 using namespace clang;
48 using namespace llvm::opt;
49 
50 Driver::Driver(StringRef ClangExecutable, StringRef DefaultTargetTriple,
51                DiagnosticsEngine &Diags,
52                IntrusiveRefCntPtr<vfs::FileSystem> VFS)
53     : Opts(createDriverOptTable()), Diags(Diags), VFS(std::move(VFS)),
54       Mode(GCCMode), SaveTemps(SaveTempsNone), BitcodeEmbed(EmbedNone),
55       LTOMode(LTOK_None), ClangExecutable(ClangExecutable),
56       SysRoot(DEFAULT_SYSROOT), UseStdLib(true),
57       DriverTitle("clang LLVM compiler"), CCPrintOptionsFilename(nullptr),
58       CCPrintHeadersFilename(nullptr), CCLogDiagnosticsFilename(nullptr),
59       CCCPrintBindings(false), CCPrintHeaders(false), CCLogDiagnostics(false),
60       CCGenDiagnostics(false), DefaultTargetTriple(DefaultTargetTriple),
61       CCCGenericGCCName(""), CheckInputsExist(true), CCCUsePCH(true),
62       SuppressMissingInputWarning(false) {
63 
64   // Provide a sane fallback if no VFS is specified.
65   if (!this->VFS)
66     this->VFS = vfs::getRealFileSystem();
67 
68   Name = llvm::sys::path::filename(ClangExecutable);
69   Dir = llvm::sys::path::parent_path(ClangExecutable);
70   InstalledDir = Dir; // Provide a sensible default installed dir.
71 
72   // Compute the path to the resource directory.
73   StringRef ClangResourceDir(CLANG_RESOURCE_DIR);
74   SmallString<128> P(Dir);
75   if (ClangResourceDir != "") {
76     llvm::sys::path::append(P, ClangResourceDir);
77   } else {
78     StringRef ClangLibdirSuffix(CLANG_LIBDIR_SUFFIX);
79     llvm::sys::path::append(P, "..", Twine("lib") + ClangLibdirSuffix, "clang",
80                             CLANG_VERSION_STRING);
81   }
82   ResourceDir = P.str();
83 }
84 
85 Driver::~Driver() {
86   delete Opts;
87 
88   llvm::DeleteContainerSeconds(ToolChains);
89 }
90 
91 void Driver::ParseDriverMode(StringRef ProgramName,
92                              ArrayRef<const char *> Args) {
93   auto Default = ToolChain::getTargetAndModeFromProgramName(ProgramName);
94   StringRef DefaultMode(Default.second);
95   setDriverModeFromOption(DefaultMode);
96 
97   for (const char *ArgPtr : Args) {
98     // Ingore nullptrs, they are response file's EOL markers
99     if (ArgPtr == nullptr)
100       continue;
101     const StringRef Arg = ArgPtr;
102     setDriverModeFromOption(Arg);
103   }
104 }
105 
106 void Driver::setDriverModeFromOption(StringRef Opt) {
107   const std::string OptName =
108       getOpts().getOption(options::OPT_driver_mode).getPrefixedName();
109   if (!Opt.startswith(OptName))
110     return;
111   StringRef Value = Opt.drop_front(OptName.size());
112 
113   const unsigned M = llvm::StringSwitch<unsigned>(Value)
114                          .Case("gcc", GCCMode)
115                          .Case("g++", GXXMode)
116                          .Case("cpp", CPPMode)
117                          .Case("cl", CLMode)
118                          .Default(~0U);
119 
120   if (M != ~0U)
121     Mode = static_cast<DriverMode>(M);
122   else
123     Diag(diag::err_drv_unsupported_option_argument) << OptName << Value;
124 }
125 
126 InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings) {
127   llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
128 
129   unsigned IncludedFlagsBitmask;
130   unsigned ExcludedFlagsBitmask;
131   std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) =
132       getIncludeExcludeOptionFlagMasks();
133 
134   unsigned MissingArgIndex, MissingArgCount;
135   InputArgList Args =
136       getOpts().ParseArgs(ArgStrings, MissingArgIndex, MissingArgCount,
137                           IncludedFlagsBitmask, ExcludedFlagsBitmask);
138 
139   // Check for missing argument error.
140   if (MissingArgCount)
141     Diag(clang::diag::err_drv_missing_argument)
142         << Args.getArgString(MissingArgIndex) << MissingArgCount;
143 
144   // Check for unsupported options.
145   for (const Arg *A : Args) {
146     if (A->getOption().hasFlag(options::Unsupported)) {
147       Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(Args);
148       continue;
149     }
150 
151     // Warn about -mcpu= without an argument.
152     if (A->getOption().matches(options::OPT_mcpu_EQ) && A->containsValue("")) {
153       Diag(clang::diag::warn_drv_empty_joined_argument) << A->getAsString(Args);
154     }
155   }
156 
157   for (const Arg *A : Args.filtered(options::OPT_UNKNOWN))
158     Diags.Report(IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl :
159                               diag::err_drv_unknown_argument)
160       << A->getAsString(Args);
161 
162   return Args;
163 }
164 
165 // Determine which compilation mode we are in. We look for options which
166 // affect the phase, starting with the earliest phases, and record which
167 // option we used to determine the final phase.
168 phases::ID Driver::getFinalPhase(const DerivedArgList &DAL,
169                                  Arg **FinalPhaseArg) const {
170   Arg *PhaseArg = nullptr;
171   phases::ID FinalPhase;
172 
173   // -{E,EP,P,M,MM} only run the preprocessor.
174   if (CCCIsCPP() || (PhaseArg = DAL.getLastArg(options::OPT_E)) ||
175       (PhaseArg = DAL.getLastArg(options::OPT__SLASH_EP)) ||
176       (PhaseArg = DAL.getLastArg(options::OPT_M, options::OPT_MM)) ||
177       (PhaseArg = DAL.getLastArg(options::OPT__SLASH_P))) {
178     FinalPhase = phases::Preprocess;
179 
180     // --precompile only runs up to precompilation.
181   } else if ((PhaseArg = DAL.getLastArg(options::OPT__precompile))) {
182     FinalPhase = phases::Precompile;
183 
184     // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
185   } else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) ||
186              (PhaseArg = DAL.getLastArg(options::OPT_module_file_info)) ||
187              (PhaseArg = DAL.getLastArg(options::OPT_verify_pch)) ||
188              (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) ||
189              (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) ||
190              (PhaseArg = DAL.getLastArg(options::OPT__migrate)) ||
191              (PhaseArg = DAL.getLastArg(options::OPT__analyze,
192                                         options::OPT__analyze_auto)) ||
193              (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) {
194     FinalPhase = phases::Compile;
195 
196     // -S only runs up to the backend.
197   } else if ((PhaseArg = DAL.getLastArg(options::OPT_S))) {
198     FinalPhase = phases::Backend;
199 
200     // -c compilation only runs up to the assembler.
201   } else if ((PhaseArg = DAL.getLastArg(options::OPT_c))) {
202     FinalPhase = phases::Assemble;
203 
204     // Otherwise do everything.
205   } else
206     FinalPhase = phases::Link;
207 
208   if (FinalPhaseArg)
209     *FinalPhaseArg = PhaseArg;
210 
211   return FinalPhase;
212 }
213 
214 static Arg *MakeInputArg(DerivedArgList &Args, OptTable *Opts,
215                          StringRef Value) {
216   Arg *A = new Arg(Opts->getOption(options::OPT_INPUT), Value,
217                    Args.getBaseArgs().MakeIndex(Value), Value.data());
218   Args.AddSynthesizedArg(A);
219   A->claim();
220   return A;
221 }
222 
223 DerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const {
224   DerivedArgList *DAL = new DerivedArgList(Args);
225 
226   bool HasNostdlib = Args.hasArg(options::OPT_nostdlib);
227   bool HasNodefaultlib = Args.hasArg(options::OPT_nodefaultlibs);
228   for (Arg *A : Args) {
229     // Unfortunately, we have to parse some forwarding options (-Xassembler,
230     // -Xlinker, -Xpreprocessor) because we either integrate their functionality
231     // (assembler and preprocessor), or bypass a previous driver ('collect2').
232 
233     // Rewrite linker options, to replace --no-demangle with a custom internal
234     // option.
235     if ((A->getOption().matches(options::OPT_Wl_COMMA) ||
236          A->getOption().matches(options::OPT_Xlinker)) &&
237         A->containsValue("--no-demangle")) {
238       // Add the rewritten no-demangle argument.
239       DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_Xlinker__no_demangle));
240 
241       // Add the remaining values as Xlinker arguments.
242       for (StringRef Val : A->getValues())
243         if (Val != "--no-demangle")
244           DAL->AddSeparateArg(A, Opts->getOption(options::OPT_Xlinker), Val);
245 
246       continue;
247     }
248 
249     // Rewrite preprocessor options, to replace -Wp,-MD,FOO which is used by
250     // some build systems. We don't try to be complete here because we don't
251     // care to encourage this usage model.
252     if (A->getOption().matches(options::OPT_Wp_COMMA) &&
253         (A->getValue(0) == StringRef("-MD") ||
254          A->getValue(0) == StringRef("-MMD"))) {
255       // Rewrite to -MD/-MMD along with -MF.
256       if (A->getValue(0) == StringRef("-MD"))
257         DAL->AddFlagArg(A, Opts->getOption(options::OPT_MD));
258       else
259         DAL->AddFlagArg(A, Opts->getOption(options::OPT_MMD));
260       if (A->getNumValues() == 2)
261         DAL->AddSeparateArg(A, Opts->getOption(options::OPT_MF),
262                             A->getValue(1));
263       continue;
264     }
265 
266     // Rewrite reserved library names.
267     if (A->getOption().matches(options::OPT_l)) {
268       StringRef Value = A->getValue();
269 
270       // Rewrite unless -nostdlib is present.
271       if (!HasNostdlib && !HasNodefaultlib && Value == "stdc++") {
272         DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_reserved_lib_stdcxx));
273         continue;
274       }
275 
276       // Rewrite unconditionally.
277       if (Value == "cc_kext") {
278         DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_reserved_lib_cckext));
279         continue;
280       }
281     }
282 
283     // Pick up inputs via the -- option.
284     if (A->getOption().matches(options::OPT__DASH_DASH)) {
285       A->claim();
286       for (StringRef Val : A->getValues())
287         DAL->append(MakeInputArg(*DAL, Opts, Val));
288       continue;
289     }
290 
291     DAL->append(A);
292   }
293 
294   // Enforce -static if -miamcu is present.
295   if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false))
296     DAL->AddFlagArg(0, Opts->getOption(options::OPT_static));
297 
298 // Add a default value of -mlinker-version=, if one was given and the user
299 // didn't specify one.
300 #if defined(HOST_LINK_VERSION)
301   if (!Args.hasArg(options::OPT_mlinker_version_EQ) &&
302       strlen(HOST_LINK_VERSION) > 0) {
303     DAL->AddJoinedArg(0, Opts->getOption(options::OPT_mlinker_version_EQ),
304                       HOST_LINK_VERSION);
305     DAL->getLastArg(options::OPT_mlinker_version_EQ)->claim();
306   }
307 #endif
308 
309   return DAL;
310 }
311 
312 /// \brief Compute target triple from args.
313 ///
314 /// This routine provides the logic to compute a target triple from various
315 /// args passed to the driver and the default triple string.
316 static llvm::Triple computeTargetTriple(const Driver &D,
317                                         StringRef DefaultTargetTriple,
318                                         const ArgList &Args,
319                                         StringRef DarwinArchName = "") {
320   // FIXME: Already done in Compilation *Driver::BuildCompilation
321   if (const Arg *A = Args.getLastArg(options::OPT_target))
322     DefaultTargetTriple = A->getValue();
323 
324   llvm::Triple Target(llvm::Triple::normalize(DefaultTargetTriple));
325 
326   // Handle Apple-specific options available here.
327   if (Target.isOSBinFormatMachO()) {
328     // If an explict Darwin arch name is given, that trumps all.
329     if (!DarwinArchName.empty()) {
330       tools::darwin::setTripleTypeForMachOArchName(Target, DarwinArchName);
331       return Target;
332     }
333 
334     // Handle the Darwin '-arch' flag.
335     if (Arg *A = Args.getLastArg(options::OPT_arch)) {
336       StringRef ArchName = A->getValue();
337       tools::darwin::setTripleTypeForMachOArchName(Target, ArchName);
338     }
339   }
340 
341   // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
342   // '-mbig-endian'/'-EB'.
343   if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
344                                options::OPT_mbig_endian)) {
345     if (A->getOption().matches(options::OPT_mlittle_endian)) {
346       llvm::Triple LE = Target.getLittleEndianArchVariant();
347       if (LE.getArch() != llvm::Triple::UnknownArch)
348         Target = std::move(LE);
349     } else {
350       llvm::Triple BE = Target.getBigEndianArchVariant();
351       if (BE.getArch() != llvm::Triple::UnknownArch)
352         Target = std::move(BE);
353     }
354   }
355 
356   // Skip further flag support on OSes which don't support '-m32' or '-m64'.
357   if (Target.getArch() == llvm::Triple::tce ||
358       Target.getOS() == llvm::Triple::Minix)
359     return Target;
360 
361   // Handle pseudo-target flags '-m64', '-mx32', '-m32' and '-m16'.
362   Arg *A = Args.getLastArg(options::OPT_m64, options::OPT_mx32,
363                            options::OPT_m32, options::OPT_m16);
364   if (A) {
365     llvm::Triple::ArchType AT = llvm::Triple::UnknownArch;
366 
367     if (A->getOption().matches(options::OPT_m64)) {
368       AT = Target.get64BitArchVariant().getArch();
369       if (Target.getEnvironment() == llvm::Triple::GNUX32)
370         Target.setEnvironment(llvm::Triple::GNU);
371     } else if (A->getOption().matches(options::OPT_mx32) &&
372                Target.get64BitArchVariant().getArch() == llvm::Triple::x86_64) {
373       AT = llvm::Triple::x86_64;
374       Target.setEnvironment(llvm::Triple::GNUX32);
375     } else if (A->getOption().matches(options::OPT_m32)) {
376       AT = Target.get32BitArchVariant().getArch();
377       if (Target.getEnvironment() == llvm::Triple::GNUX32)
378         Target.setEnvironment(llvm::Triple::GNU);
379     } else if (A->getOption().matches(options::OPT_m16) &&
380                Target.get32BitArchVariant().getArch() == llvm::Triple::x86) {
381       AT = llvm::Triple::x86;
382       Target.setEnvironment(llvm::Triple::CODE16);
383     }
384 
385     if (AT != llvm::Triple::UnknownArch && AT != Target.getArch())
386       Target.setArch(AT);
387   }
388 
389   // Handle -miamcu flag.
390   if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) {
391     if (Target.get32BitArchVariant().getArch() != llvm::Triple::x86)
392       D.Diag(diag::err_drv_unsupported_opt_for_target) << "-miamcu"
393                                                        << Target.str();
394 
395     if (A && !A->getOption().matches(options::OPT_m32))
396       D.Diag(diag::err_drv_argument_not_allowed_with)
397           << "-miamcu" << A->getBaseArg().getAsString(Args);
398 
399     Target.setArch(llvm::Triple::x86);
400     Target.setArchName("i586");
401     Target.setEnvironment(llvm::Triple::UnknownEnvironment);
402     Target.setEnvironmentName("");
403     Target.setOS(llvm::Triple::ELFIAMCU);
404     Target.setVendor(llvm::Triple::UnknownVendor);
405     Target.setVendorName("intel");
406   }
407 
408   return Target;
409 }
410 
411 // \brief Parse the LTO options and record the type of LTO compilation
412 // based on which -f(no-)?lto(=.*)? option occurs last.
413 void Driver::setLTOMode(const llvm::opt::ArgList &Args) {
414   LTOMode = LTOK_None;
415   if (!Args.hasFlag(options::OPT_flto, options::OPT_flto_EQ,
416                     options::OPT_fno_lto, false))
417     return;
418 
419   StringRef LTOName("full");
420 
421   const Arg *A = Args.getLastArg(options::OPT_flto_EQ);
422   if (A)
423     LTOName = A->getValue();
424 
425   LTOMode = llvm::StringSwitch<LTOKind>(LTOName)
426                 .Case("full", LTOK_Full)
427                 .Case("thin", LTOK_Thin)
428                 .Default(LTOK_Unknown);
429 
430   if (LTOMode == LTOK_Unknown) {
431     assert(A);
432     Diag(diag::err_drv_unsupported_option_argument) << A->getOption().getName()
433                                                     << A->getValue();
434   }
435 }
436 
437 /// Compute the desired OpenMP runtime from the flags provided.
438 Driver::OpenMPRuntimeKind Driver::getOpenMPRuntime(const ArgList &Args) const {
439   StringRef RuntimeName(CLANG_DEFAULT_OPENMP_RUNTIME);
440 
441   const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ);
442   if (A)
443     RuntimeName = A->getValue();
444 
445   auto RT = llvm::StringSwitch<OpenMPRuntimeKind>(RuntimeName)
446                 .Case("libomp", OMPRT_OMP)
447                 .Case("libgomp", OMPRT_GOMP)
448                 .Case("libiomp5", OMPRT_IOMP5)
449                 .Default(OMPRT_Unknown);
450 
451   if (RT == OMPRT_Unknown) {
452     if (A)
453       Diag(diag::err_drv_unsupported_option_argument)
454           << A->getOption().getName() << A->getValue();
455     else
456       // FIXME: We could use a nicer diagnostic here.
457       Diag(diag::err_drv_unsupported_opt) << "-fopenmp";
458   }
459 
460   return RT;
461 }
462 
463 void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
464                                               InputList &Inputs) {
465 
466   //
467   // CUDA
468   //
469   // We need to generate a CUDA toolchain if any of the inputs has a CUDA type.
470   if (llvm::any_of(Inputs, [](std::pair<types::ID, const llvm::opt::Arg *> &I) {
471         return types::isCuda(I.first);
472       })) {
473     const ToolChain &TC = getToolChain(
474         C.getInputArgs(),
475         llvm::Triple(C.getSingleOffloadToolChain<Action::OFK_Host>()
476                              ->getTriple()
477                              .isArch64Bit()
478                          ? "nvptx64-nvidia-cuda"
479                          : "nvptx-nvidia-cuda"));
480     C.addOffloadDeviceToolChain(&TC, Action::OFK_Cuda);
481   }
482 
483   //
484   // OpenMP
485   //
486   // We need to generate an OpenMP toolchain if the user specified targets with
487   // the -fopenmp-targets option.
488   if (Arg *OpenMPTargets =
489           C.getInputArgs().getLastArg(options::OPT_fopenmp_targets_EQ)) {
490     if (OpenMPTargets->getNumValues()) {
491       // We expect that -fopenmp-targets is always used in conjunction with the
492       // option -fopenmp specifying a valid runtime with offloading support,
493       // i.e. libomp or libiomp.
494       bool HasValidOpenMPRuntime = C.getInputArgs().hasFlag(
495           options::OPT_fopenmp, options::OPT_fopenmp_EQ,
496           options::OPT_fno_openmp, false);
497       if (HasValidOpenMPRuntime) {
498         OpenMPRuntimeKind OpenMPKind = getOpenMPRuntime(C.getInputArgs());
499         HasValidOpenMPRuntime =
500             OpenMPKind == OMPRT_OMP || OpenMPKind == OMPRT_IOMP5;
501       }
502 
503       if (HasValidOpenMPRuntime) {
504         llvm::StringMap<const char *> FoundNormalizedTriples;
505         for (const char *Val : OpenMPTargets->getValues()) {
506           llvm::Triple TT(Val);
507           std::string NormalizedName = TT.normalize();
508 
509           // Make sure we don't have a duplicate triple.
510           auto Duplicate = FoundNormalizedTriples.find(NormalizedName);
511           if (Duplicate != FoundNormalizedTriples.end()) {
512             Diag(clang::diag::warn_drv_omp_offload_target_duplicate)
513                 << Val << Duplicate->second;
514             continue;
515           }
516 
517           // Store the current triple so that we can check for duplicates in the
518           // following iterations.
519           FoundNormalizedTriples[NormalizedName] = Val;
520 
521           // If the specified target is invalid, emit a diagnostic.
522           if (TT.getArch() == llvm::Triple::UnknownArch)
523             Diag(clang::diag::err_drv_invalid_omp_target) << Val;
524           else {
525             const ToolChain &TC = getToolChain(C.getInputArgs(), TT);
526             C.addOffloadDeviceToolChain(&TC, Action::OFK_OpenMP);
527           }
528         }
529       } else
530         Diag(clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets);
531     } else
532       Diag(clang::diag::warn_drv_empty_joined_argument)
533           << OpenMPTargets->getAsString(C.getInputArgs());
534   }
535 
536   //
537   // TODO: Add support for other offloading programming models here.
538   //
539 
540   return;
541 }
542 
543 Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
544   llvm::PrettyStackTraceString CrashInfo("Compilation construction");
545 
546   // FIXME: Handle environment options which affect driver behavior, somewhere
547   // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS.
548 
549   if (Optional<std::string> CompilerPathValue =
550           llvm::sys::Process::GetEnv("COMPILER_PATH")) {
551     StringRef CompilerPath = *CompilerPathValue;
552     while (!CompilerPath.empty()) {
553       std::pair<StringRef, StringRef> Split =
554           CompilerPath.split(llvm::sys::EnvPathSeparator);
555       PrefixDirs.push_back(Split.first);
556       CompilerPath = Split.second;
557     }
558   }
559 
560   // We look for the driver mode option early, because the mode can affect
561   // how other options are parsed.
562   ParseDriverMode(ClangExecutable, ArgList.slice(1));
563 
564   // FIXME: What are we going to do with -V and -b?
565 
566   // FIXME: This stuff needs to go into the Compilation, not the driver.
567   bool CCCPrintPhases;
568 
569   InputArgList Args = ParseArgStrings(ArgList.slice(1));
570 
571   // Silence driver warnings if requested
572   Diags.setIgnoreAllWarnings(Args.hasArg(options::OPT_w));
573 
574   // -no-canonical-prefixes is used very early in main.
575   Args.ClaimAllArgs(options::OPT_no_canonical_prefixes);
576 
577   // Ignore -pipe.
578   Args.ClaimAllArgs(options::OPT_pipe);
579 
580   // Extract -ccc args.
581   //
582   // FIXME: We need to figure out where this behavior should live. Most of it
583   // should be outside in the client; the parts that aren't should have proper
584   // options, either by introducing new ones or by overloading gcc ones like -V
585   // or -b.
586   CCCPrintPhases = Args.hasArg(options::OPT_ccc_print_phases);
587   CCCPrintBindings = Args.hasArg(options::OPT_ccc_print_bindings);
588   if (const Arg *A = Args.getLastArg(options::OPT_ccc_gcc_name))
589     CCCGenericGCCName = A->getValue();
590   CCCUsePCH =
591       Args.hasFlag(options::OPT_ccc_pch_is_pch, options::OPT_ccc_pch_is_pth);
592   // FIXME: DefaultTargetTriple is used by the target-prefixed calls to as/ld
593   // and getToolChain is const.
594   if (IsCLMode()) {
595     // clang-cl targets MSVC-style Win32.
596     llvm::Triple T(DefaultTargetTriple);
597     T.setOS(llvm::Triple::Win32);
598     T.setVendor(llvm::Triple::PC);
599     T.setEnvironment(llvm::Triple::MSVC);
600     DefaultTargetTriple = T.str();
601   }
602   if (const Arg *A = Args.getLastArg(options::OPT_target))
603     DefaultTargetTriple = A->getValue();
604   if (const Arg *A = Args.getLastArg(options::OPT_ccc_install_dir))
605     Dir = InstalledDir = A->getValue();
606   for (const Arg *A : Args.filtered(options::OPT_B)) {
607     A->claim();
608     PrefixDirs.push_back(A->getValue(0));
609   }
610   if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ))
611     SysRoot = A->getValue();
612   if (const Arg *A = Args.getLastArg(options::OPT__dyld_prefix_EQ))
613     DyldPrefix = A->getValue();
614   if (Args.hasArg(options::OPT_nostdlib))
615     UseStdLib = false;
616 
617   if (const Arg *A = Args.getLastArg(options::OPT_resource_dir))
618     ResourceDir = A->getValue();
619 
620   if (const Arg *A = Args.getLastArg(options::OPT_save_temps_EQ)) {
621     SaveTemps = llvm::StringSwitch<SaveTempsMode>(A->getValue())
622                     .Case("cwd", SaveTempsCwd)
623                     .Case("obj", SaveTempsObj)
624                     .Default(SaveTempsCwd);
625   }
626 
627   setLTOMode(Args);
628 
629   // Ignore -fembed-bitcode options with LTO
630   // since the output will be bitcode anyway.
631   if (getLTOMode() == LTOK_None) {
632     if (Arg *A = Args.getLastArg(options::OPT_fembed_bitcode_EQ)) {
633       StringRef Name = A->getValue();
634       unsigned Model = llvm::StringSwitch<unsigned>(Name)
635           .Case("off", EmbedNone)
636           .Case("all", EmbedBitcode)
637           .Case("bitcode", EmbedBitcode)
638           .Case("marker", EmbedMarker)
639           .Default(~0U);
640       if (Model == ~0U) {
641         Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args)
642                                                   << Name;
643       } else
644         BitcodeEmbed = static_cast<BitcodeEmbedMode>(Model);
645     }
646   } else {
647     // claim the bitcode option under LTO so no warning is issued.
648     Args.ClaimAllArgs(options::OPT_fembed_bitcode_EQ);
649   }
650 
651   std::unique_ptr<llvm::opt::InputArgList> UArgs =
652       llvm::make_unique<InputArgList>(std::move(Args));
653 
654   // Perform the default argument translations.
655   DerivedArgList *TranslatedArgs = TranslateInputArgs(*UArgs);
656 
657   // Owned by the host.
658   const ToolChain &TC = getToolChain(
659       *UArgs, computeTargetTriple(*this, DefaultTargetTriple, *UArgs));
660 
661   // The compilation takes ownership of Args.
662   Compilation *C = new Compilation(*this, TC, UArgs.release(), TranslatedArgs);
663 
664   if (!HandleImmediateArgs(*C))
665     return C;
666 
667   // Construct the list of inputs.
668   InputList Inputs;
669   BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs);
670 
671   // Populate the tool chains for the offloading devices, if any.
672   CreateOffloadingDeviceToolChains(*C, Inputs);
673 
674   // Construct the list of abstract actions to perform for this compilation. On
675   // MachO targets this uses the driver-driver and universal actions.
676   if (TC.getTriple().isOSBinFormatMachO())
677     BuildUniversalActions(*C, C->getDefaultToolChain(), Inputs);
678   else
679     BuildActions(*C, C->getArgs(), Inputs, C->getActions());
680 
681   if (CCCPrintPhases) {
682     PrintActions(*C);
683     return C;
684   }
685 
686   BuildJobs(*C);
687 
688   return C;
689 }
690 
691 static void printArgList(raw_ostream &OS, const llvm::opt::ArgList &Args) {
692   llvm::opt::ArgStringList ASL;
693   for (const auto *A : Args)
694     A->render(Args, ASL);
695 
696   for (auto I = ASL.begin(), E = ASL.end(); I != E; ++I) {
697     if (I != ASL.begin())
698       OS << ' ';
699     Command::printArg(OS, *I, true);
700   }
701   OS << '\n';
702 }
703 
704 // When clang crashes, produce diagnostic information including the fully
705 // preprocessed source file(s).  Request that the developer attach the
706 // diagnostic information to a bug report.
707 void Driver::generateCompilationDiagnostics(Compilation &C,
708                                             const Command &FailingCommand) {
709   if (C.getArgs().hasArg(options::OPT_fno_crash_diagnostics))
710     return;
711 
712   // Don't try to generate diagnostics for link or dsymutil jobs.
713   if (FailingCommand.getCreator().isLinkJob() ||
714       FailingCommand.getCreator().isDsymutilJob())
715     return;
716 
717   // Print the version of the compiler.
718   PrintVersion(C, llvm::errs());
719 
720   Diag(clang::diag::note_drv_command_failed_diag_msg)
721       << "PLEASE submit a bug report to " BUG_REPORT_URL " and include the "
722          "crash backtrace, preprocessed source, and associated run script.";
723 
724   // Suppress driver output and emit preprocessor output to temp file.
725   Mode = CPPMode;
726   CCGenDiagnostics = true;
727 
728   // Save the original job command(s).
729   Command Cmd = FailingCommand;
730 
731   // Keep track of whether we produce any errors while trying to produce
732   // preprocessed sources.
733   DiagnosticErrorTrap Trap(Diags);
734 
735   // Suppress tool output.
736   C.initCompilationForDiagnostics();
737 
738   // Construct the list of inputs.
739   InputList Inputs;
740   BuildInputs(C.getDefaultToolChain(), C.getArgs(), Inputs);
741 
742   for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) {
743     bool IgnoreInput = false;
744 
745     // Ignore input from stdin or any inputs that cannot be preprocessed.
746     // Check type first as not all linker inputs have a value.
747     if (types::getPreprocessedType(it->first) == types::TY_INVALID) {
748       IgnoreInput = true;
749     } else if (!strcmp(it->second->getValue(), "-")) {
750       Diag(clang::diag::note_drv_command_failed_diag_msg)
751           << "Error generating preprocessed source(s) - "
752              "ignoring input from stdin.";
753       IgnoreInput = true;
754     }
755 
756     if (IgnoreInput) {
757       it = Inputs.erase(it);
758       ie = Inputs.end();
759     } else {
760       ++it;
761     }
762   }
763 
764   if (Inputs.empty()) {
765     Diag(clang::diag::note_drv_command_failed_diag_msg)
766         << "Error generating preprocessed source(s) - "
767            "no preprocessable inputs.";
768     return;
769   }
770 
771   // Don't attempt to generate preprocessed files if multiple -arch options are
772   // used, unless they're all duplicates.
773   llvm::StringSet<> ArchNames;
774   for (const Arg *A : C.getArgs()) {
775     if (A->getOption().matches(options::OPT_arch)) {
776       StringRef ArchName = A->getValue();
777       ArchNames.insert(ArchName);
778     }
779   }
780   if (ArchNames.size() > 1) {
781     Diag(clang::diag::note_drv_command_failed_diag_msg)
782         << "Error generating preprocessed source(s) - cannot generate "
783            "preprocessed source with multiple -arch options.";
784     return;
785   }
786 
787   // Construct the list of abstract actions to perform for this compilation. On
788   // Darwin OSes this uses the driver-driver and builds universal actions.
789   const ToolChain &TC = C.getDefaultToolChain();
790   if (TC.getTriple().isOSBinFormatMachO())
791     BuildUniversalActions(C, TC, Inputs);
792   else
793     BuildActions(C, C.getArgs(), Inputs, C.getActions());
794 
795   BuildJobs(C);
796 
797   // If there were errors building the compilation, quit now.
798   if (Trap.hasErrorOccurred()) {
799     Diag(clang::diag::note_drv_command_failed_diag_msg)
800         << "Error generating preprocessed source(s).";
801     return;
802   }
803 
804   // Generate preprocessed output.
805   SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
806   C.ExecuteJobs(C.getJobs(), FailingCommands);
807 
808   // If any of the preprocessing commands failed, clean up and exit.
809   if (!FailingCommands.empty()) {
810     if (!isSaveTempsEnabled())
811       C.CleanupFileList(C.getTempFiles(), true);
812 
813     Diag(clang::diag::note_drv_command_failed_diag_msg)
814         << "Error generating preprocessed source(s).";
815     return;
816   }
817 
818   const ArgStringList &TempFiles = C.getTempFiles();
819   if (TempFiles.empty()) {
820     Diag(clang::diag::note_drv_command_failed_diag_msg)
821         << "Error generating preprocessed source(s).";
822     return;
823   }
824 
825   Diag(clang::diag::note_drv_command_failed_diag_msg)
826       << "\n********************\n\n"
827          "PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:\n"
828          "Preprocessed source(s) and associated run script(s) are located at:";
829 
830   SmallString<128> VFS;
831   for (const char *TempFile : TempFiles) {
832     Diag(clang::diag::note_drv_command_failed_diag_msg) << TempFile;
833     if (StringRef(TempFile).endswith(".cache")) {
834       // In some cases (modules) we'll dump extra data to help with reproducing
835       // the crash into a directory next to the output.
836       VFS = llvm::sys::path::filename(TempFile);
837       llvm::sys::path::append(VFS, "vfs", "vfs.yaml");
838     }
839   }
840 
841   // Assume associated files are based off of the first temporary file.
842   CrashReportInfo CrashInfo(TempFiles[0], VFS);
843 
844   std::string Script = CrashInfo.Filename.rsplit('.').first.str() + ".sh";
845   std::error_code EC;
846   llvm::raw_fd_ostream ScriptOS(Script, EC, llvm::sys::fs::F_Excl);
847   if (EC) {
848     Diag(clang::diag::note_drv_command_failed_diag_msg)
849         << "Error generating run script: " + Script + " " + EC.message();
850   } else {
851     ScriptOS << "# Crash reproducer for " << getClangFullVersion() << "\n"
852              << "# Driver args: ";
853     printArgList(ScriptOS, C.getInputArgs());
854     ScriptOS << "# Original command: ";
855     Cmd.Print(ScriptOS, "\n", /*Quote=*/true);
856     Cmd.Print(ScriptOS, "\n", /*Quote=*/true, &CrashInfo);
857     Diag(clang::diag::note_drv_command_failed_diag_msg) << Script;
858   }
859 
860   for (const auto &A : C.getArgs().filtered(options::OPT_frewrite_map_file,
861                                             options::OPT_frewrite_map_file_EQ))
862     Diag(clang::diag::note_drv_command_failed_diag_msg) << A->getValue();
863 
864   Diag(clang::diag::note_drv_command_failed_diag_msg)
865       << "\n\n********************";
866 }
867 
868 void Driver::setUpResponseFiles(Compilation &C, Command &Cmd) {
869   // Since commandLineFitsWithinSystemLimits() may underestimate system's capacity
870   // if the tool does not support response files, there is a chance/ that things
871   // will just work without a response file, so we silently just skip it.
872   if (Cmd.getCreator().getResponseFilesSupport() == Tool::RF_None ||
873       llvm::sys::commandLineFitsWithinSystemLimits(Cmd.getExecutable(), Cmd.getArguments()))
874     return;
875 
876   std::string TmpName = GetTemporaryPath("response", "txt");
877   Cmd.setResponseFile(
878       C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str())));
879 }
880 
881 int Driver::ExecuteCompilation(
882     Compilation &C,
883     SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) {
884   // Just print if -### was present.
885   if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
886     C.getJobs().Print(llvm::errs(), "\n", true);
887     return 0;
888   }
889 
890   // If there were errors building the compilation, quit now.
891   if (Diags.hasErrorOccurred())
892     return 1;
893 
894   // Set up response file names for each command, if necessary
895   for (auto &Job : C.getJobs())
896     setUpResponseFiles(C, Job);
897 
898   C.ExecuteJobs(C.getJobs(), FailingCommands);
899 
900   // Remove temp files.
901   C.CleanupFileList(C.getTempFiles());
902 
903   // If the command succeeded, we are done.
904   if (FailingCommands.empty())
905     return 0;
906 
907   // Otherwise, remove result files and print extra information about abnormal
908   // failures.
909   for (const auto &CmdPair : FailingCommands) {
910     int Res = CmdPair.first;
911     const Command *FailingCommand = CmdPair.second;
912 
913     // Remove result files if we're not saving temps.
914     if (!isSaveTempsEnabled()) {
915       const JobAction *JA = cast<JobAction>(&FailingCommand->getSource());
916       C.CleanupFileMap(C.getResultFiles(), JA, true);
917 
918       // Failure result files are valid unless we crashed.
919       if (Res < 0)
920         C.CleanupFileMap(C.getFailureResultFiles(), JA, true);
921     }
922 
923     // Print extra information about abnormal failures, if possible.
924     //
925     // This is ad-hoc, but we don't want to be excessively noisy. If the result
926     // status was 1, assume the command failed normally. In particular, if it
927     // was the compiler then assume it gave a reasonable error code. Failures
928     // in other tools are less common, and they generally have worse
929     // diagnostics, so always print the diagnostic there.
930     const Tool &FailingTool = FailingCommand->getCreator();
931 
932     if (!FailingCommand->getCreator().hasGoodDiagnostics() || Res != 1) {
933       // FIXME: See FIXME above regarding result code interpretation.
934       if (Res < 0)
935         Diag(clang::diag::err_drv_command_signalled)
936             << FailingTool.getShortName();
937       else
938         Diag(clang::diag::err_drv_command_failed) << FailingTool.getShortName()
939                                                   << Res;
940     }
941   }
942   return 0;
943 }
944 
945 void Driver::PrintHelp(bool ShowHidden) const {
946   unsigned IncludedFlagsBitmask;
947   unsigned ExcludedFlagsBitmask;
948   std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) =
949       getIncludeExcludeOptionFlagMasks();
950 
951   ExcludedFlagsBitmask |= options::NoDriverOption;
952   if (!ShowHidden)
953     ExcludedFlagsBitmask |= HelpHidden;
954 
955   getOpts().PrintHelp(llvm::outs(), Name.c_str(), DriverTitle.c_str(),
956                       IncludedFlagsBitmask, ExcludedFlagsBitmask);
957 }
958 
959 void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const {
960   // FIXME: The following handlers should use a callback mechanism, we don't
961   // know what the client would like to do.
962   OS << getClangFullVersion() << '\n';
963   const ToolChain &TC = C.getDefaultToolChain();
964   OS << "Target: " << TC.getTripleString() << '\n';
965 
966   // Print the threading model.
967   if (Arg *A = C.getArgs().getLastArg(options::OPT_mthread_model)) {
968     // Don't print if the ToolChain would have barfed on it already
969     if (TC.isThreadModelSupported(A->getValue()))
970       OS << "Thread model: " << A->getValue();
971   } else
972     OS << "Thread model: " << TC.getThreadModel();
973   OS << '\n';
974 
975   // Print out the install directory.
976   OS << "InstalledDir: " << InstalledDir << '\n';
977 }
978 
979 /// PrintDiagnosticCategories - Implement the --print-diagnostic-categories
980 /// option.
981 static void PrintDiagnosticCategories(raw_ostream &OS) {
982   // Skip the empty category.
983   for (unsigned i = 1, max = DiagnosticIDs::getNumberOfCategories(); i != max;
984        ++i)
985     OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(i) << '\n';
986 }
987 
988 bool Driver::HandleImmediateArgs(const Compilation &C) {
989   // The order these options are handled in gcc is all over the place, but we
990   // don't expect inconsistencies w.r.t. that to matter in practice.
991 
992   if (C.getArgs().hasArg(options::OPT_dumpmachine)) {
993     llvm::outs() << C.getDefaultToolChain().getTripleString() << '\n';
994     return false;
995   }
996 
997   if (C.getArgs().hasArg(options::OPT_dumpversion)) {
998     // Since -dumpversion is only implemented for pedantic GCC compatibility, we
999     // return an answer which matches our definition of __VERSION__.
1000     //
1001     // If we want to return a more correct answer some day, then we should
1002     // introduce a non-pedantically GCC compatible mode to Clang in which we
1003     // provide sensible definitions for -dumpversion, __VERSION__, etc.
1004     llvm::outs() << "4.2.1\n";
1005     return false;
1006   }
1007 
1008   if (C.getArgs().hasArg(options::OPT__print_diagnostic_categories)) {
1009     PrintDiagnosticCategories(llvm::outs());
1010     return false;
1011   }
1012 
1013   if (C.getArgs().hasArg(options::OPT_help) ||
1014       C.getArgs().hasArg(options::OPT__help_hidden)) {
1015     PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
1016     return false;
1017   }
1018 
1019   if (C.getArgs().hasArg(options::OPT__version)) {
1020     // Follow gcc behavior and use stdout for --version and stderr for -v.
1021     PrintVersion(C, llvm::outs());
1022     return false;
1023   }
1024 
1025   if (C.getArgs().hasArg(options::OPT_v) ||
1026       C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
1027     PrintVersion(C, llvm::errs());
1028     SuppressMissingInputWarning = true;
1029   }
1030 
1031   const ToolChain &TC = C.getDefaultToolChain();
1032 
1033   if (C.getArgs().hasArg(options::OPT_v))
1034     TC.printVerboseInfo(llvm::errs());
1035 
1036   if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
1037     llvm::outs() << "programs: =";
1038     bool separator = false;
1039     for (const std::string &Path : TC.getProgramPaths()) {
1040       if (separator)
1041         llvm::outs() << ':';
1042       llvm::outs() << Path;
1043       separator = true;
1044     }
1045     llvm::outs() << "\n";
1046     llvm::outs() << "libraries: =" << ResourceDir;
1047 
1048     StringRef sysroot = C.getSysRoot();
1049 
1050     for (const std::string &Path : TC.getFilePaths()) {
1051       // Always print a separator. ResourceDir was the first item shown.
1052       llvm::outs() << ':';
1053       // Interpretation of leading '=' is needed only for NetBSD.
1054       if (Path[0] == '=')
1055         llvm::outs() << sysroot << Path.substr(1);
1056       else
1057         llvm::outs() << Path;
1058     }
1059     llvm::outs() << "\n";
1060     return false;
1061   }
1062 
1063   // FIXME: The following handlers should use a callback mechanism, we don't
1064   // know what the client would like to do.
1065   if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
1066     llvm::outs() << GetFilePath(A->getValue(), TC) << "\n";
1067     return false;
1068   }
1069 
1070   if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
1071     llvm::outs() << GetProgramPath(A->getValue(), TC) << "\n";
1072     return false;
1073   }
1074 
1075   if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
1076     ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs());
1077     switch (RLT) {
1078     case ToolChain::RLT_CompilerRT:
1079       llvm::outs() << TC.getCompilerRT(C.getArgs(), "builtins") << "\n";
1080       break;
1081     case ToolChain::RLT_Libgcc:
1082       llvm::outs() << GetFilePath("libgcc.a", TC) << "\n";
1083       break;
1084     }
1085     return false;
1086   }
1087 
1088   if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
1089     for (const Multilib &Multilib : TC.getMultilibs())
1090       llvm::outs() << Multilib << "\n";
1091     return false;
1092   }
1093 
1094   if (C.getArgs().hasArg(options::OPT_print_multi_directory)) {
1095     for (const Multilib &Multilib : TC.getMultilibs()) {
1096       if (Multilib.gccSuffix().empty())
1097         llvm::outs() << ".\n";
1098       else {
1099         StringRef Suffix(Multilib.gccSuffix());
1100         assert(Suffix.front() == '/');
1101         llvm::outs() << Suffix.substr(1) << "\n";
1102       }
1103     }
1104     return false;
1105   }
1106   return true;
1107 }
1108 
1109 // Display an action graph human-readably.  Action A is the "sink" node
1110 // and latest-occuring action. Traversal is in pre-order, visiting the
1111 // inputs to each action before printing the action itself.
1112 static unsigned PrintActions1(const Compilation &C, Action *A,
1113                               std::map<Action *, unsigned> &Ids) {
1114   if (Ids.count(A)) // A was already visited.
1115     return Ids[A];
1116 
1117   std::string str;
1118   llvm::raw_string_ostream os(str);
1119 
1120   os << Action::getClassName(A->getKind()) << ", ";
1121   if (InputAction *IA = dyn_cast<InputAction>(A)) {
1122     os << "\"" << IA->getInputArg().getValue() << "\"";
1123   } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
1124     os << '"' << BIA->getArchName() << '"' << ", {"
1125        << PrintActions1(C, *BIA->input_begin(), Ids) << "}";
1126   } else if (OffloadAction *OA = dyn_cast<OffloadAction>(A)) {
1127     bool IsFirst = true;
1128     OA->doOnEachDependence(
1129         [&](Action *A, const ToolChain *TC, const char *BoundArch) {
1130           // E.g. for two CUDA device dependences whose bound arch is sm_20 and
1131           // sm_35 this will generate:
1132           // "cuda-device" (nvptx64-nvidia-cuda:sm_20) {#ID}, "cuda-device"
1133           // (nvptx64-nvidia-cuda:sm_35) {#ID}
1134           if (!IsFirst)
1135             os << ", ";
1136           os << '"';
1137           if (TC)
1138             os << A->getOffloadingKindPrefix();
1139           else
1140             os << "host";
1141           os << " (";
1142           os << TC->getTriple().normalize();
1143 
1144           if (BoundArch)
1145             os << ":" << BoundArch;
1146           os << ")";
1147           os << '"';
1148           os << " {" << PrintActions1(C, A, Ids) << "}";
1149           IsFirst = false;
1150         });
1151   } else {
1152     const ActionList *AL = &A->getInputs();
1153 
1154     if (AL->size()) {
1155       const char *Prefix = "{";
1156       for (Action *PreRequisite : *AL) {
1157         os << Prefix << PrintActions1(C, PreRequisite, Ids);
1158         Prefix = ", ";
1159       }
1160       os << "}";
1161     } else
1162       os << "{}";
1163   }
1164 
1165   // Append offload info for all options other than the offloading action
1166   // itself (e.g. (cuda-device, sm_20) or (cuda-host)).
1167   std::string offload_str;
1168   llvm::raw_string_ostream offload_os(offload_str);
1169   if (!isa<OffloadAction>(A)) {
1170     auto S = A->getOffloadingKindPrefix();
1171     if (!S.empty()) {
1172       offload_os << ", (" << S;
1173       if (A->getOffloadingArch())
1174         offload_os << ", " << A->getOffloadingArch();
1175       offload_os << ")";
1176     }
1177   }
1178 
1179   unsigned Id = Ids.size();
1180   Ids[A] = Id;
1181   llvm::errs() << Id << ": " << os.str() << ", "
1182                << types::getTypeName(A->getType()) << offload_os.str() << "\n";
1183 
1184   return Id;
1185 }
1186 
1187 // Print the action graphs in a compilation C.
1188 // For example "clang -c file1.c file2.c" is composed of two subgraphs.
1189 void Driver::PrintActions(const Compilation &C) const {
1190   std::map<Action *, unsigned> Ids;
1191   for (Action *A : C.getActions())
1192     PrintActions1(C, A, Ids);
1193 }
1194 
1195 /// \brief Check whether the given input tree contains any compilation or
1196 /// assembly actions.
1197 static bool ContainsCompileOrAssembleAction(const Action *A) {
1198   if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A) ||
1199       isa<AssembleJobAction>(A))
1200     return true;
1201 
1202   for (const Action *Input : A->inputs())
1203     if (ContainsCompileOrAssembleAction(Input))
1204       return true;
1205 
1206   return false;
1207 }
1208 
1209 void Driver::BuildUniversalActions(Compilation &C, const ToolChain &TC,
1210                                    const InputList &BAInputs) const {
1211   DerivedArgList &Args = C.getArgs();
1212   ActionList &Actions = C.getActions();
1213   llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
1214   // Collect the list of architectures. Duplicates are allowed, but should only
1215   // be handled once (in the order seen).
1216   llvm::StringSet<> ArchNames;
1217   SmallVector<const char *, 4> Archs;
1218   for (Arg *A : Args) {
1219     if (A->getOption().matches(options::OPT_arch)) {
1220       // Validate the option here; we don't save the type here because its
1221       // particular spelling may participate in other driver choices.
1222       llvm::Triple::ArchType Arch =
1223           tools::darwin::getArchTypeForMachOArchName(A->getValue());
1224       if (Arch == llvm::Triple::UnknownArch) {
1225         Diag(clang::diag::err_drv_invalid_arch_name) << A->getAsString(Args);
1226         continue;
1227       }
1228 
1229       A->claim();
1230       if (ArchNames.insert(A->getValue()).second)
1231         Archs.push_back(A->getValue());
1232     }
1233   }
1234 
1235   // When there is no explicit arch for this platform, make sure we still bind
1236   // the architecture (to the default) so that -Xarch_ is handled correctly.
1237   if (!Archs.size())
1238     Archs.push_back(Args.MakeArgString(TC.getDefaultUniversalArchName()));
1239 
1240   ActionList SingleActions;
1241   BuildActions(C, Args, BAInputs, SingleActions);
1242 
1243   // Add in arch bindings for every top level action, as well as lipo and
1244   // dsymutil steps if needed.
1245   for (Action* Act : SingleActions) {
1246     // Make sure we can lipo this kind of output. If not (and it is an actual
1247     // output) then we disallow, since we can't create an output file with the
1248     // right name without overwriting it. We could remove this oddity by just
1249     // changing the output names to include the arch, which would also fix
1250     // -save-temps. Compatibility wins for now.
1251 
1252     if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
1253       Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
1254           << types::getTypeName(Act->getType());
1255 
1256     ActionList Inputs;
1257     for (unsigned i = 0, e = Archs.size(); i != e; ++i)
1258       Inputs.push_back(C.MakeAction<BindArchAction>(Act, Archs[i]));
1259 
1260     // Lipo if necessary, we do it this way because we need to set the arch flag
1261     // so that -Xarch_ gets overwritten.
1262     if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
1263       Actions.append(Inputs.begin(), Inputs.end());
1264     else
1265       Actions.push_back(C.MakeAction<LipoJobAction>(Inputs, Act->getType()));
1266 
1267     // Handle debug info queries.
1268     Arg *A = Args.getLastArg(options::OPT_g_Group);
1269     if (A && !A->getOption().matches(options::OPT_g0) &&
1270         !A->getOption().matches(options::OPT_gstabs) &&
1271         ContainsCompileOrAssembleAction(Actions.back())) {
1272 
1273       // Add a 'dsymutil' step if necessary, when debug info is enabled and we
1274       // have a compile input. We need to run 'dsymutil' ourselves in such cases
1275       // because the debug info will refer to a temporary object file which
1276       // will be removed at the end of the compilation process.
1277       if (Act->getType() == types::TY_Image) {
1278         ActionList Inputs;
1279         Inputs.push_back(Actions.back());
1280         Actions.pop_back();
1281         Actions.push_back(
1282             C.MakeAction<DsymutilJobAction>(Inputs, types::TY_dSYM));
1283       }
1284 
1285       // Verify the debug info output.
1286       if (Args.hasArg(options::OPT_verify_debug_info)) {
1287         Action* LastAction = Actions.back();
1288         Actions.pop_back();
1289         Actions.push_back(C.MakeAction<VerifyDebugInfoJobAction>(
1290             LastAction, types::TY_Nothing));
1291       }
1292     }
1293   }
1294 }
1295 
1296 /// \brief Check that the file referenced by Value exists. If it doesn't,
1297 /// issue a diagnostic and return false.
1298 static bool DiagnoseInputExistence(const Driver &D, const DerivedArgList &Args,
1299                                    StringRef Value, types::ID Ty) {
1300   if (!D.getCheckInputsExist())
1301     return true;
1302 
1303   // stdin always exists.
1304   if (Value == "-")
1305     return true;
1306 
1307   SmallString<64> Path(Value);
1308   if (Arg *WorkDir = Args.getLastArg(options::OPT_working_directory)) {
1309     if (!llvm::sys::path::is_absolute(Path)) {
1310       SmallString<64> Directory(WorkDir->getValue());
1311       llvm::sys::path::append(Directory, Value);
1312       Path.assign(Directory);
1313     }
1314   }
1315 
1316   if (llvm::sys::fs::exists(Twine(Path)))
1317     return true;
1318 
1319   if (D.IsCLMode()) {
1320     if (!llvm::sys::path::is_absolute(Twine(Path)) &&
1321         llvm::sys::Process::FindInEnvPath("LIB", Value))
1322       return true;
1323 
1324     if (Args.hasArg(options::OPT__SLASH_link) && Ty == types::TY_Object) {
1325       // Arguments to the /link flag might cause the linker to search for object
1326       // and library files in paths we don't know about. Don't error in such
1327       // cases.
1328       return true;
1329     }
1330   }
1331 
1332   D.Diag(clang::diag::err_drv_no_such_file) << Path;
1333   return false;
1334 }
1335 
1336 // Construct a the list of inputs and their types.
1337 void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
1338                          InputList &Inputs) const {
1339   // Track the current user specified (-x) input. We also explicitly track the
1340   // argument used to set the type; we only want to claim the type when we
1341   // actually use it, so we warn about unused -x arguments.
1342   types::ID InputType = types::TY_Nothing;
1343   Arg *InputTypeArg = nullptr;
1344 
1345   // The last /TC or /TP option sets the input type to C or C++ globally.
1346   if (Arg *TCTP = Args.getLastArgNoClaim(options::OPT__SLASH_TC,
1347                                          options::OPT__SLASH_TP)) {
1348     InputTypeArg = TCTP;
1349     InputType = TCTP->getOption().matches(options::OPT__SLASH_TC)
1350                     ? types::TY_C
1351                     : types::TY_CXX;
1352 
1353     arg_iterator it =
1354         Args.filtered_begin(options::OPT__SLASH_TC, options::OPT__SLASH_TP);
1355     const arg_iterator ie = Args.filtered_end();
1356     Arg *Previous = *it++;
1357     bool ShowNote = false;
1358     while (it != ie) {
1359       Diag(clang::diag::warn_drv_overriding_flag_option)
1360           << Previous->getSpelling() << (*it)->getSpelling();
1361       Previous = *it++;
1362       ShowNote = true;
1363     }
1364     if (ShowNote)
1365       Diag(clang::diag::note_drv_t_option_is_global);
1366 
1367     // No driver mode exposes -x and /TC or /TP; we don't support mixing them.
1368     assert(!Args.hasArg(options::OPT_x) && "-x and /TC or /TP is not allowed");
1369   }
1370 
1371   for (Arg *A : Args) {
1372     if (A->getOption().getKind() == Option::InputClass) {
1373       const char *Value = A->getValue();
1374       types::ID Ty = types::TY_INVALID;
1375 
1376       // Infer the input type if necessary.
1377       if (InputType == types::TY_Nothing) {
1378         // If there was an explicit arg for this, claim it.
1379         if (InputTypeArg)
1380           InputTypeArg->claim();
1381 
1382         // stdin must be handled specially.
1383         if (memcmp(Value, "-", 2) == 0) {
1384           // If running with -E, treat as a C input (this changes the builtin
1385           // macros, for example). This may be overridden by -ObjC below.
1386           //
1387           // Otherwise emit an error but still use a valid type to avoid
1388           // spurious errors (e.g., no inputs).
1389           if (!Args.hasArgNoClaim(options::OPT_E) && !CCCIsCPP())
1390             Diag(IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl
1391                             : clang::diag::err_drv_unknown_stdin_type);
1392           Ty = types::TY_C;
1393         } else {
1394           // Otherwise lookup by extension.
1395           // Fallback is C if invoked as C preprocessor or Object otherwise.
1396           // We use a host hook here because Darwin at least has its own
1397           // idea of what .s is.
1398           if (const char *Ext = strrchr(Value, '.'))
1399             Ty = TC.LookupTypeForExtension(Ext + 1);
1400 
1401           if (Ty == types::TY_INVALID) {
1402             if (CCCIsCPP())
1403               Ty = types::TY_C;
1404             else
1405               Ty = types::TY_Object;
1406           }
1407 
1408           // If the driver is invoked as C++ compiler (like clang++ or c++) it
1409           // should autodetect some input files as C++ for g++ compatibility.
1410           if (CCCIsCXX()) {
1411             types::ID OldTy = Ty;
1412             Ty = types::lookupCXXTypeForCType(Ty);
1413 
1414             if (Ty != OldTy)
1415               Diag(clang::diag::warn_drv_treating_input_as_cxx)
1416                   << getTypeName(OldTy) << getTypeName(Ty);
1417           }
1418         }
1419 
1420         // -ObjC and -ObjC++ override the default language, but only for "source
1421         // files". We just treat everything that isn't a linker input as a
1422         // source file.
1423         //
1424         // FIXME: Clean this up if we move the phase sequence into the type.
1425         if (Ty != types::TY_Object) {
1426           if (Args.hasArg(options::OPT_ObjC))
1427             Ty = types::TY_ObjC;
1428           else if (Args.hasArg(options::OPT_ObjCXX))
1429             Ty = types::TY_ObjCXX;
1430         }
1431       } else {
1432         assert(InputTypeArg && "InputType set w/o InputTypeArg");
1433         if (!InputTypeArg->getOption().matches(options::OPT_x)) {
1434           // If emulating cl.exe, make sure that /TC and /TP don't affect input
1435           // object files.
1436           const char *Ext = strrchr(Value, '.');
1437           if (Ext && TC.LookupTypeForExtension(Ext + 1) == types::TY_Object)
1438             Ty = types::TY_Object;
1439         }
1440         if (Ty == types::TY_INVALID) {
1441           Ty = InputType;
1442           InputTypeArg->claim();
1443         }
1444       }
1445 
1446       if (DiagnoseInputExistence(*this, Args, Value, Ty))
1447         Inputs.push_back(std::make_pair(Ty, A));
1448 
1449     } else if (A->getOption().matches(options::OPT__SLASH_Tc)) {
1450       StringRef Value = A->getValue();
1451       if (DiagnoseInputExistence(*this, Args, Value, types::TY_C)) {
1452         Arg *InputArg = MakeInputArg(Args, Opts, A->getValue());
1453         Inputs.push_back(std::make_pair(types::TY_C, InputArg));
1454       }
1455       A->claim();
1456     } else if (A->getOption().matches(options::OPT__SLASH_Tp)) {
1457       StringRef Value = A->getValue();
1458       if (DiagnoseInputExistence(*this, Args, Value, types::TY_CXX)) {
1459         Arg *InputArg = MakeInputArg(Args, Opts, A->getValue());
1460         Inputs.push_back(std::make_pair(types::TY_CXX, InputArg));
1461       }
1462       A->claim();
1463     } else if (A->getOption().hasFlag(options::LinkerInput)) {
1464       // Just treat as object type, we could make a special type for this if
1465       // necessary.
1466       Inputs.push_back(std::make_pair(types::TY_Object, A));
1467 
1468     } else if (A->getOption().matches(options::OPT_x)) {
1469       InputTypeArg = A;
1470       InputType = types::lookupTypeForTypeSpecifier(A->getValue());
1471       A->claim();
1472 
1473       // Follow gcc behavior and treat as linker input for invalid -x
1474       // options. Its not clear why we shouldn't just revert to unknown; but
1475       // this isn't very important, we might as well be bug compatible.
1476       if (!InputType) {
1477         Diag(clang::diag::err_drv_unknown_language) << A->getValue();
1478         InputType = types::TY_Object;
1479       }
1480     }
1481   }
1482   if (CCCIsCPP() && Inputs.empty()) {
1483     // If called as standalone preprocessor, stdin is processed
1484     // if no other input is present.
1485     Arg *A = MakeInputArg(Args, Opts, "-");
1486     Inputs.push_back(std::make_pair(types::TY_C, A));
1487   }
1488 }
1489 
1490 namespace {
1491 /// Provides a convenient interface for different programming models to generate
1492 /// the required device actions.
1493 class OffloadingActionBuilder final {
1494   /// Flag used to trace errors in the builder.
1495   bool IsValid = false;
1496 
1497   /// The compilation that is using this builder.
1498   Compilation &C;
1499 
1500   /// Map between an input argument and the offload kinds used to process it.
1501   std::map<const Arg *, unsigned> InputArgToOffloadKindMap;
1502 
1503   /// Builder interface. It doesn't build anything or keep any state.
1504   class DeviceActionBuilder {
1505   public:
1506     typedef llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PhasesTy;
1507 
1508     enum ActionBuilderReturnCode {
1509       // The builder acted successfully on the current action.
1510       ABRT_Success,
1511       // The builder didn't have to act on the current action.
1512       ABRT_Inactive,
1513       // The builder was successful and requested the host action to not be
1514       // generated.
1515       ABRT_Ignore_Host,
1516     };
1517 
1518   protected:
1519     /// Compilation associated with this builder.
1520     Compilation &C;
1521 
1522     /// Tool chains associated with this builder. The same programming
1523     /// model may have associated one or more tool chains.
1524     SmallVector<const ToolChain *, 2> ToolChains;
1525 
1526     /// The derived arguments associated with this builder.
1527     DerivedArgList &Args;
1528 
1529     /// The inputs associated with this builder.
1530     const Driver::InputList &Inputs;
1531 
1532     /// The associated offload kind.
1533     Action::OffloadKind AssociatedOffloadKind = Action::OFK_None;
1534 
1535   public:
1536     DeviceActionBuilder(Compilation &C, DerivedArgList &Args,
1537                         const Driver::InputList &Inputs,
1538                         Action::OffloadKind AssociatedOffloadKind)
1539         : C(C), Args(Args), Inputs(Inputs),
1540           AssociatedOffloadKind(AssociatedOffloadKind) {}
1541     virtual ~DeviceActionBuilder() {}
1542 
1543     /// Fill up the array \a DA with all the device dependences that should be
1544     /// added to the provided host action \a HostAction. By default it is
1545     /// inactive.
1546     virtual ActionBuilderReturnCode
1547     getDeviceDependences(OffloadAction::DeviceDependences &DA,
1548                          phases::ID CurPhase, phases::ID FinalPhase,
1549                          PhasesTy &Phases) {
1550       return ABRT_Inactive;
1551     }
1552 
1553     /// Update the state to include the provided host action \a HostAction as a
1554     /// dependency of the current device action. By default it is inactive.
1555     virtual ActionBuilderReturnCode addDeviceDepences(Action *HostAction) {
1556       return ABRT_Inactive;
1557     }
1558 
1559     /// Append top level actions generated by the builder. Return true if errors
1560     /// were found.
1561     virtual void appendTopLevelActions(ActionList &AL) {}
1562 
1563     /// Append linker actions generated by the builder. Return true if errors
1564     /// were found.
1565     virtual void appendLinkDependences(OffloadAction::DeviceDependences &DA) {}
1566 
1567     /// Initialize the builder. Return true if any initialization errors are
1568     /// found.
1569     virtual bool initialize() { return false; }
1570 
1571     /// Return true if the builder can use bundling/unbundling.
1572     virtual bool canUseBundlerUnbundler() const { return false; }
1573 
1574     /// Return true if this builder is valid. We have a valid builder if we have
1575     /// associated device tool chains.
1576     bool isValid() { return !ToolChains.empty(); }
1577 
1578     /// Return the associated offload kind.
1579     Action::OffloadKind getAssociatedOffloadKind() {
1580       return AssociatedOffloadKind;
1581     }
1582   };
1583 
1584   /// \brief CUDA action builder. It injects device code in the host backend
1585   /// action.
1586   class CudaActionBuilder final : public DeviceActionBuilder {
1587     /// Flags to signal if the user requested host-only or device-only
1588     /// compilation.
1589     bool CompileHostOnly = false;
1590     bool CompileDeviceOnly = false;
1591 
1592     /// List of GPU architectures to use in this compilation.
1593     SmallVector<CudaArch, 4> GpuArchList;
1594 
1595     /// The CUDA actions for the current input.
1596     ActionList CudaDeviceActions;
1597 
1598     /// The CUDA fat binary if it was generated for the current input.
1599     Action *CudaFatBinary = nullptr;
1600 
1601     /// Flag that is set to true if this builder acted on the current input.
1602     bool IsActive = false;
1603 
1604   public:
1605     CudaActionBuilder(Compilation &C, DerivedArgList &Args,
1606                       const Driver::InputList &Inputs)
1607         : DeviceActionBuilder(C, Args, Inputs, Action::OFK_Cuda) {}
1608 
1609     ActionBuilderReturnCode
1610     getDeviceDependences(OffloadAction::DeviceDependences &DA,
1611                          phases::ID CurPhase, phases::ID FinalPhase,
1612                          PhasesTy &Phases) override {
1613       if (!IsActive)
1614         return ABRT_Inactive;
1615 
1616       // If we don't have more CUDA actions, we don't have any dependences to
1617       // create for the host.
1618       if (CudaDeviceActions.empty())
1619         return ABRT_Success;
1620 
1621       assert(CudaDeviceActions.size() == GpuArchList.size() &&
1622              "Expecting one action per GPU architecture.");
1623       assert(!CompileHostOnly &&
1624              "Not expecting CUDA actions in host-only compilation.");
1625 
1626       // If we are generating code for the device or we are in a backend phase,
1627       // we attempt to generate the fat binary. We compile each arch to ptx and
1628       // assemble to cubin, then feed the cubin *and* the ptx into a device
1629       // "link" action, which uses fatbinary to combine these cubins into one
1630       // fatbin.  The fatbin is then an input to the host action if not in
1631       // device-only mode.
1632       if (CompileDeviceOnly || CurPhase == phases::Backend) {
1633         ActionList DeviceActions;
1634         for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
1635           // Produce the device action from the current phase up to the assemble
1636           // phase.
1637           for (auto Ph : Phases) {
1638             // Skip the phases that were already dealt with.
1639             if (Ph < CurPhase)
1640               continue;
1641             // We have to be consistent with the host final phase.
1642             if (Ph > FinalPhase)
1643               break;
1644 
1645             CudaDeviceActions[I] = C.getDriver().ConstructPhaseAction(
1646                 C, Args, Ph, CudaDeviceActions[I]);
1647 
1648             if (Ph == phases::Assemble)
1649               break;
1650           }
1651 
1652           // If we didn't reach the assemble phase, we can't generate the fat
1653           // binary. We don't need to generate the fat binary if we are not in
1654           // device-only mode.
1655           if (!isa<AssembleJobAction>(CudaDeviceActions[I]) ||
1656               CompileDeviceOnly)
1657             continue;
1658 
1659           Action *AssembleAction = CudaDeviceActions[I];
1660           assert(AssembleAction->getType() == types::TY_Object);
1661           assert(AssembleAction->getInputs().size() == 1);
1662 
1663           Action *BackendAction = AssembleAction->getInputs()[0];
1664           assert(BackendAction->getType() == types::TY_PP_Asm);
1665 
1666           for (auto &A : {AssembleAction, BackendAction}) {
1667             OffloadAction::DeviceDependences DDep;
1668             DDep.add(*A, *ToolChains.front(), CudaArchToString(GpuArchList[I]),
1669                      Action::OFK_Cuda);
1670             DeviceActions.push_back(
1671                 C.MakeAction<OffloadAction>(DDep, A->getType()));
1672           }
1673         }
1674 
1675         // We generate the fat binary if we have device input actions.
1676         if (!DeviceActions.empty()) {
1677           CudaFatBinary =
1678               C.MakeAction<LinkJobAction>(DeviceActions, types::TY_CUDA_FATBIN);
1679 
1680           if (!CompileDeviceOnly) {
1681             DA.add(*CudaFatBinary, *ToolChains.front(), /*BoundArch=*/nullptr,
1682                    Action::OFK_Cuda);
1683             // Clear the fat binary, it is already a dependence to an host
1684             // action.
1685             CudaFatBinary = nullptr;
1686           }
1687 
1688           // Remove the CUDA actions as they are already connected to an host
1689           // action or fat binary.
1690           CudaDeviceActions.clear();
1691         }
1692 
1693         // We avoid creating host action in device-only mode.
1694         return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success;
1695       } else if (CurPhase > phases::Backend) {
1696         // If we are past the backend phase and still have a device action, we
1697         // don't have to do anything as this action is already a device
1698         // top-level action.
1699         return ABRT_Success;
1700       }
1701 
1702       assert(CurPhase < phases::Backend && "Generating single CUDA "
1703                                            "instructions should only occur "
1704                                            "before the backend phase!");
1705 
1706       // By default, we produce an action for each device arch.
1707       for (Action *&A : CudaDeviceActions)
1708         A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A);
1709 
1710       return ABRT_Success;
1711     }
1712 
1713     ActionBuilderReturnCode addDeviceDepences(Action *HostAction) override {
1714       // While generating code for CUDA, we only depend on the host input action
1715       // to trigger the creation of all the CUDA device actions.
1716 
1717       // If we are dealing with an input action, replicate it for each GPU
1718       // architecture. If we are in host-only mode we return 'success' so that
1719       // the host uses the CUDA offload kind.
1720       if (auto *IA = dyn_cast<InputAction>(HostAction)) {
1721         assert(!GpuArchList.empty() &&
1722                "We should have at least one GPU architecture.");
1723 
1724         // If the host input is not CUDA, we don't need to bother about this
1725         // input.
1726         if (IA->getType() != types::TY_CUDA) {
1727           // The builder will ignore this input.
1728           IsActive = false;
1729           return ABRT_Inactive;
1730         }
1731 
1732         // Set the flag to true, so that the builder acts on the current input.
1733         IsActive = true;
1734 
1735         if (CompileHostOnly)
1736           return ABRT_Success;
1737 
1738         // Replicate inputs for each GPU architecture.
1739         for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I)
1740           CudaDeviceActions.push_back(C.MakeAction<InputAction>(
1741               IA->getInputArg(), types::TY_CUDA_DEVICE));
1742 
1743         return ABRT_Success;
1744       }
1745 
1746       return IsActive ? ABRT_Success : ABRT_Inactive;
1747     }
1748 
1749     void appendTopLevelActions(ActionList &AL) override {
1750       // Utility to append actions to the top level list.
1751       auto AddTopLevel = [&](Action *A, CudaArch BoundArch) {
1752         OffloadAction::DeviceDependences Dep;
1753         Dep.add(*A, *ToolChains.front(), CudaArchToString(BoundArch),
1754                 Action::OFK_Cuda);
1755         AL.push_back(C.MakeAction<OffloadAction>(Dep, A->getType()));
1756       };
1757 
1758       // If we have a fat binary, add it to the list.
1759       if (CudaFatBinary) {
1760         AddTopLevel(CudaFatBinary, CudaArch::UNKNOWN);
1761         CudaDeviceActions.clear();
1762         CudaFatBinary = nullptr;
1763         return;
1764       }
1765 
1766       if (CudaDeviceActions.empty())
1767         return;
1768 
1769       // If we have CUDA actions at this point, that's because we have a have
1770       // partial compilation, so we should have an action for each GPU
1771       // architecture.
1772       assert(CudaDeviceActions.size() == GpuArchList.size() &&
1773              "Expecting one action per GPU architecture.");
1774       assert(ToolChains.size() == 1 &&
1775              "Expecting to have a sing CUDA toolchain.");
1776       for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I)
1777         AddTopLevel(CudaDeviceActions[I], GpuArchList[I]);
1778 
1779       CudaDeviceActions.clear();
1780     }
1781 
1782     bool initialize() override {
1783       // We don't need to support CUDA.
1784       if (!C.hasOffloadToolChain<Action::OFK_Cuda>())
1785         return false;
1786 
1787       const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>();
1788       assert(HostTC && "No toolchain for host compilation.");
1789       if (HostTC->getTriple().isNVPTX()) {
1790         // We do not support targeting NVPTX for host compilation. Throw
1791         // an error and abort pipeline construction early so we don't trip
1792         // asserts that assume device-side compilation.
1793         C.getDriver().Diag(diag::err_drv_cuda_nvptx_host);
1794         return true;
1795       }
1796 
1797       ToolChains.push_back(C.getSingleOffloadToolChain<Action::OFK_Cuda>());
1798 
1799       Arg *PartialCompilationArg = Args.getLastArg(
1800           options::OPT_cuda_host_only, options::OPT_cuda_device_only,
1801           options::OPT_cuda_compile_host_device);
1802       CompileHostOnly = PartialCompilationArg &&
1803                         PartialCompilationArg->getOption().matches(
1804                             options::OPT_cuda_host_only);
1805       CompileDeviceOnly = PartialCompilationArg &&
1806                           PartialCompilationArg->getOption().matches(
1807                               options::OPT_cuda_device_only);
1808 
1809       // Collect all cuda_gpu_arch parameters, removing duplicates.
1810       llvm::SmallSet<CudaArch, 4> GpuArchs;
1811       bool Error = false;
1812       for (Arg *A : Args) {
1813         if (!A->getOption().matches(options::OPT_cuda_gpu_arch_EQ))
1814           continue;
1815         A->claim();
1816 
1817         const auto &ArchStr = A->getValue();
1818         CudaArch Arch = StringToCudaArch(ArchStr);
1819         if (Arch == CudaArch::UNKNOWN) {
1820           C.getDriver().Diag(clang::diag::err_drv_cuda_bad_gpu_arch) << ArchStr;
1821           Error = true;
1822         } else if (GpuArchs.insert(Arch).second)
1823           GpuArchList.push_back(Arch);
1824       }
1825 
1826       // Default to sm_20 which is the lowest common denominator for supported
1827       // GPUs.
1828       // sm_20 code should work correctly, if suboptimally, on all newer GPUs.
1829       if (GpuArchList.empty())
1830         GpuArchList.push_back(CudaArch::SM_20);
1831 
1832       return Error;
1833     }
1834   };
1835 
1836   /// OpenMP action builder. The host bitcode is passed to the device frontend
1837   /// and all the device linked images are passed to the host link phase.
1838   class OpenMPActionBuilder final : public DeviceActionBuilder {
1839     /// The OpenMP actions for the current input.
1840     ActionList OpenMPDeviceActions;
1841 
1842     /// The linker inputs obtained for each toolchain.
1843     SmallVector<ActionList, 8> DeviceLinkerInputs;
1844 
1845   public:
1846     OpenMPActionBuilder(Compilation &C, DerivedArgList &Args,
1847                         const Driver::InputList &Inputs)
1848         : DeviceActionBuilder(C, Args, Inputs, Action::OFK_OpenMP) {}
1849 
1850     ActionBuilderReturnCode
1851     getDeviceDependences(OffloadAction::DeviceDependences &DA,
1852                          phases::ID CurPhase, phases::ID FinalPhase,
1853                          PhasesTy &Phases) override {
1854 
1855       // We should always have an action for each input.
1856       assert(OpenMPDeviceActions.size() == ToolChains.size() &&
1857              "Number of OpenMP actions and toolchains do not match.");
1858 
1859       // The host only depends on device action in the linking phase, when all
1860       // the device images have to be embedded in the host image.
1861       if (CurPhase == phases::Link) {
1862         assert(ToolChains.size() == DeviceLinkerInputs.size() &&
1863                "Toolchains and linker inputs sizes do not match.");
1864         auto LI = DeviceLinkerInputs.begin();
1865         for (auto *A : OpenMPDeviceActions) {
1866           LI->push_back(A);
1867           ++LI;
1868         }
1869 
1870         // We passed the device action as a host dependence, so we don't need to
1871         // do anything else with them.
1872         OpenMPDeviceActions.clear();
1873         return ABRT_Success;
1874       }
1875 
1876       // By default, we produce an action for each device arch.
1877       for (Action *&A : OpenMPDeviceActions)
1878         A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A);
1879 
1880       return ABRT_Success;
1881     }
1882 
1883     ActionBuilderReturnCode addDeviceDepences(Action *HostAction) override {
1884 
1885       // If this is an input action replicate it for each OpenMP toolchain.
1886       if (auto *IA = dyn_cast<InputAction>(HostAction)) {
1887         OpenMPDeviceActions.clear();
1888         for (unsigned I = 0; I < ToolChains.size(); ++I)
1889           OpenMPDeviceActions.push_back(
1890               C.MakeAction<InputAction>(IA->getInputArg(), IA->getType()));
1891         return ABRT_Success;
1892       }
1893 
1894       // If this is an unbundling action use it as is for each OpenMP toolchain.
1895       if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) {
1896         OpenMPDeviceActions.clear();
1897         for (unsigned I = 0; I < ToolChains.size(); ++I) {
1898           OpenMPDeviceActions.push_back(UA);
1899           UA->registerDependentActionInfo(
1900               ToolChains[I], /*BoundArch=*/StringRef(), Action::OFK_OpenMP);
1901         }
1902         return ABRT_Success;
1903       }
1904 
1905       // When generating code for OpenMP we use the host compile phase result as
1906       // a dependence to the device compile phase so that it can learn what
1907       // declarations should be emitted. However, this is not the only use for
1908       // the host action, so we prevent it from being collapsed.
1909       if (isa<CompileJobAction>(HostAction)) {
1910         HostAction->setCannotBeCollapsedWithNextDependentAction();
1911         assert(ToolChains.size() == OpenMPDeviceActions.size() &&
1912                "Toolchains and device action sizes do not match.");
1913         OffloadAction::HostDependence HDep(
1914             *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
1915             /*BoundArch=*/nullptr, Action::OFK_OpenMP);
1916         auto TC = ToolChains.begin();
1917         for (Action *&A : OpenMPDeviceActions) {
1918           assert(isa<CompileJobAction>(A));
1919           OffloadAction::DeviceDependences DDep;
1920           DDep.add(*A, **TC, /*BoundArch=*/nullptr, Action::OFK_OpenMP);
1921           A = C.MakeAction<OffloadAction>(HDep, DDep);
1922           ++TC;
1923         }
1924       }
1925       return ABRT_Success;
1926     }
1927 
1928     void appendTopLevelActions(ActionList &AL) override {
1929       if (OpenMPDeviceActions.empty())
1930         return;
1931 
1932       // We should always have an action for each input.
1933       assert(OpenMPDeviceActions.size() == ToolChains.size() &&
1934              "Number of OpenMP actions and toolchains do not match.");
1935 
1936       // Append all device actions followed by the proper offload action.
1937       auto TI = ToolChains.begin();
1938       for (auto *A : OpenMPDeviceActions) {
1939         OffloadAction::DeviceDependences Dep;
1940         Dep.add(*A, **TI, /*BoundArch=*/nullptr, Action::OFK_OpenMP);
1941         AL.push_back(C.MakeAction<OffloadAction>(Dep, A->getType()));
1942         ++TI;
1943       }
1944       // We no longer need the action stored in this builder.
1945       OpenMPDeviceActions.clear();
1946     }
1947 
1948     void appendLinkDependences(OffloadAction::DeviceDependences &DA) override {
1949       assert(ToolChains.size() == DeviceLinkerInputs.size() &&
1950              "Toolchains and linker inputs sizes do not match.");
1951 
1952       // Append a new link action for each device.
1953       auto TC = ToolChains.begin();
1954       for (auto &LI : DeviceLinkerInputs) {
1955         auto *DeviceLinkAction =
1956             C.MakeAction<LinkJobAction>(LI, types::TY_Image);
1957         DA.add(*DeviceLinkAction, **TC, /*BoundArch=*/nullptr,
1958                Action::OFK_OpenMP);
1959         ++TC;
1960       }
1961     }
1962 
1963     bool initialize() override {
1964       // Get the OpenMP toolchains. If we don't get any, the action builder will
1965       // know there is nothing to do related to OpenMP offloading.
1966       auto OpenMPTCRange = C.getOffloadToolChains<Action::OFK_OpenMP>();
1967       for (auto TI = OpenMPTCRange.first, TE = OpenMPTCRange.second; TI != TE;
1968            ++TI)
1969         ToolChains.push_back(TI->second);
1970 
1971       DeviceLinkerInputs.resize(ToolChains.size());
1972       return false;
1973     }
1974 
1975     bool canUseBundlerUnbundler() const override {
1976       // OpenMP should use bundled files whenever possible.
1977       return true;
1978     }
1979   };
1980 
1981   ///
1982   /// TODO: Add the implementation for other specialized builders here.
1983   ///
1984 
1985   /// Specialized builders being used by this offloading action builder.
1986   SmallVector<DeviceActionBuilder *, 4> SpecializedBuilders;
1987 
1988   /// Flag set to true if all valid builders allow file bundling/unbundling.
1989   bool CanUseBundler;
1990 
1991 public:
1992   OffloadingActionBuilder(Compilation &C, DerivedArgList &Args,
1993                           const Driver::InputList &Inputs)
1994       : C(C) {
1995     // Create a specialized builder for each device toolchain.
1996 
1997     IsValid = true;
1998 
1999     // Create a specialized builder for CUDA.
2000     SpecializedBuilders.push_back(new CudaActionBuilder(C, Args, Inputs));
2001 
2002     // Create a specialized builder for OpenMP.
2003     SpecializedBuilders.push_back(new OpenMPActionBuilder(C, Args, Inputs));
2004 
2005     //
2006     // TODO: Build other specialized builders here.
2007     //
2008 
2009     // Initialize all the builders, keeping track of errors. If all valid
2010     // builders agree that we can use bundling, set the flag to true.
2011     unsigned ValidBuilders = 0u;
2012     unsigned ValidBuildersSupportingBundling = 0u;
2013     for (auto *SB : SpecializedBuilders) {
2014       IsValid = IsValid && !SB->initialize();
2015 
2016       // Update the counters if the builder is valid.
2017       if (SB->isValid()) {
2018         ++ValidBuilders;
2019         if (SB->canUseBundlerUnbundler())
2020           ++ValidBuildersSupportingBundling;
2021       }
2022     }
2023     CanUseBundler =
2024         ValidBuilders && ValidBuilders == ValidBuildersSupportingBundling;
2025   }
2026 
2027   ~OffloadingActionBuilder() {
2028     for (auto *SB : SpecializedBuilders)
2029       delete SB;
2030   }
2031 
2032   /// Generate an action that adds device dependences (if any) to a host action.
2033   /// If no device dependence actions exist, just return the host action \a
2034   /// HostAction. If an error is found or if no builder requires the host action
2035   /// to be generated, return nullptr.
2036   Action *
2037   addDeviceDependencesToHostAction(Action *HostAction, const Arg *InputArg,
2038                                    phases::ID CurPhase, phases::ID FinalPhase,
2039                                    DeviceActionBuilder::PhasesTy &Phases) {
2040     if (!IsValid)
2041       return nullptr;
2042 
2043     if (SpecializedBuilders.empty())
2044       return HostAction;
2045 
2046     assert(HostAction && "Invalid host action!");
2047 
2048     OffloadAction::DeviceDependences DDeps;
2049     // Check if all the programming models agree we should not emit the host
2050     // action. Also, keep track of the offloading kinds employed.
2051     auto &OffloadKind = InputArgToOffloadKindMap[InputArg];
2052     unsigned InactiveBuilders = 0u;
2053     unsigned IgnoringBuilders = 0u;
2054     for (auto *SB : SpecializedBuilders) {
2055       if (!SB->isValid()) {
2056         ++InactiveBuilders;
2057         continue;
2058       }
2059 
2060       auto RetCode =
2061           SB->getDeviceDependences(DDeps, CurPhase, FinalPhase, Phases);
2062 
2063       // If the builder explicitly says the host action should be ignored,
2064       // we need to increment the variable that tracks the builders that request
2065       // the host object to be ignored.
2066       if (RetCode == DeviceActionBuilder::ABRT_Ignore_Host)
2067         ++IgnoringBuilders;
2068 
2069       // Unless the builder was inactive for this action, we have to record the
2070       // offload kind because the host will have to use it.
2071       if (RetCode != DeviceActionBuilder::ABRT_Inactive)
2072         OffloadKind |= SB->getAssociatedOffloadKind();
2073     }
2074 
2075     // If all builders agree that the host object should be ignored, just return
2076     // nullptr.
2077     if (IgnoringBuilders &&
2078         SpecializedBuilders.size() == (InactiveBuilders + IgnoringBuilders))
2079       return nullptr;
2080 
2081     if (DDeps.getActions().empty())
2082       return HostAction;
2083 
2084     // We have dependences we need to bundle together. We use an offload action
2085     // for that.
2086     OffloadAction::HostDependence HDep(
2087         *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
2088         /*BoundArch=*/nullptr, DDeps);
2089     return C.MakeAction<OffloadAction>(HDep, DDeps);
2090   }
2091 
2092   /// Generate an action that adds a host dependence to a device action. The
2093   /// results will be kept in this action builder. Return true if an error was
2094   /// found.
2095   bool addHostDependenceToDeviceActions(Action *&HostAction,
2096                                         const Arg *InputArg) {
2097     if (!IsValid)
2098       return true;
2099 
2100     // If we are supporting bundling/unbundling and the current action is an
2101     // input action of non-source file, we replace the host action by the
2102     // unbundling action. The bundler tool has the logic to detect if an input
2103     // is a bundle or not and if the input is not a bundle it assumes it is a
2104     // host file. Therefore it is safe to create an unbundling action even if
2105     // the input is not a bundle.
2106     if (CanUseBundler && isa<InputAction>(HostAction) &&
2107         InputArg->getOption().getKind() == llvm::opt::Option::InputClass &&
2108         !types::isSrcFile(HostAction->getType())) {
2109       auto UnbundlingHostAction =
2110           C.MakeAction<OffloadUnbundlingJobAction>(HostAction);
2111       UnbundlingHostAction->registerDependentActionInfo(
2112           C.getSingleOffloadToolChain<Action::OFK_Host>(),
2113           /*BoundArch=*/StringRef(), Action::OFK_Host);
2114       HostAction = UnbundlingHostAction;
2115     }
2116 
2117     assert(HostAction && "Invalid host action!");
2118 
2119     // Register the offload kinds that are used.
2120     auto &OffloadKind = InputArgToOffloadKindMap[InputArg];
2121     for (auto *SB : SpecializedBuilders) {
2122       if (!SB->isValid())
2123         continue;
2124 
2125       auto RetCode = SB->addDeviceDepences(HostAction);
2126 
2127       // Host dependences for device actions are not compatible with that same
2128       // action being ignored.
2129       assert(RetCode != DeviceActionBuilder::ABRT_Ignore_Host &&
2130              "Host dependence not expected to be ignored.!");
2131 
2132       // Unless the builder was inactive for this action, we have to record the
2133       // offload kind because the host will have to use it.
2134       if (RetCode != DeviceActionBuilder::ABRT_Inactive)
2135         OffloadKind |= SB->getAssociatedOffloadKind();
2136     }
2137 
2138     return false;
2139   }
2140 
2141   /// Add the offloading top level actions to the provided action list. This
2142   /// function can replace the host action by a bundling action if the
2143   /// programming models allow it.
2144   bool appendTopLevelActions(ActionList &AL, Action *HostAction,
2145                              const Arg *InputArg) {
2146     // Get the device actions to be appended.
2147     ActionList OffloadAL;
2148     for (auto *SB : SpecializedBuilders) {
2149       if (!SB->isValid())
2150         continue;
2151       SB->appendTopLevelActions(OffloadAL);
2152     }
2153 
2154     // If we can use the bundler, replace the host action by the bundling one in
2155     // the resulting list. Otherwise, just append the device actions.
2156     if (CanUseBundler && !OffloadAL.empty()) {
2157       // Add the host action to the list in order to create the bundling action.
2158       OffloadAL.push_back(HostAction);
2159 
2160       // We expect that the host action was just appended to the action list
2161       // before this method was called.
2162       assert(HostAction == AL.back() && "Host action not in the list??");
2163       HostAction = C.MakeAction<OffloadBundlingJobAction>(OffloadAL);
2164       AL.back() = HostAction;
2165     } else
2166       AL.append(OffloadAL.begin(), OffloadAL.end());
2167 
2168     // Propagate to the current host action (if any) the offload information
2169     // associated with the current input.
2170     if (HostAction)
2171       HostAction->propagateHostOffloadInfo(InputArgToOffloadKindMap[InputArg],
2172                                            /*BoundArch=*/nullptr);
2173     return false;
2174   }
2175 
2176   /// Processes the host linker action. This currently consists of replacing it
2177   /// with an offload action if there are device link objects and propagate to
2178   /// the host action all the offload kinds used in the current compilation. The
2179   /// resulting action is returned.
2180   Action *processHostLinkAction(Action *HostAction) {
2181     // Add all the dependences from the device linking actions.
2182     OffloadAction::DeviceDependences DDeps;
2183     for (auto *SB : SpecializedBuilders) {
2184       if (!SB->isValid())
2185         continue;
2186 
2187       SB->appendLinkDependences(DDeps);
2188     }
2189 
2190     // Calculate all the offload kinds used in the current compilation.
2191     unsigned ActiveOffloadKinds = 0u;
2192     for (auto &I : InputArgToOffloadKindMap)
2193       ActiveOffloadKinds |= I.second;
2194 
2195     // If we don't have device dependencies, we don't have to create an offload
2196     // action.
2197     if (DDeps.getActions().empty()) {
2198       // Propagate all the active kinds to host action. Given that it is a link
2199       // action it is assumed to depend on all actions generated so far.
2200       HostAction->propagateHostOffloadInfo(ActiveOffloadKinds,
2201                                            /*BoundArch=*/nullptr);
2202       return HostAction;
2203     }
2204 
2205     // Create the offload action with all dependences. When an offload action
2206     // is created the kinds are propagated to the host action, so we don't have
2207     // to do that explicitly here.
2208     OffloadAction::HostDependence HDep(
2209         *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
2210         /*BoundArch*/ nullptr, ActiveOffloadKinds);
2211     return C.MakeAction<OffloadAction>(HDep, DDeps);
2212   }
2213 };
2214 } // anonymous namespace.
2215 
2216 void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
2217                           const InputList &Inputs, ActionList &Actions) const {
2218   llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
2219 
2220   if (!SuppressMissingInputWarning && Inputs.empty()) {
2221     Diag(clang::diag::err_drv_no_input_files);
2222     return;
2223   }
2224 
2225   Arg *FinalPhaseArg;
2226   phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg);
2227 
2228   if (FinalPhase == phases::Link && Args.hasArg(options::OPT_emit_llvm)) {
2229     Diag(clang::diag::err_drv_emit_llvm_link);
2230   }
2231 
2232   // Reject -Z* at the top level, these options should never have been exposed
2233   // by gcc.
2234   if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
2235     Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
2236 
2237   // Diagnose misuse of /Fo.
2238   if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fo)) {
2239     StringRef V = A->getValue();
2240     if (Inputs.size() > 1 && !V.empty() &&
2241         !llvm::sys::path::is_separator(V.back())) {
2242       // Check whether /Fo tries to name an output file for multiple inputs.
2243       Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources)
2244           << A->getSpelling() << V;
2245       Args.eraseArg(options::OPT__SLASH_Fo);
2246     }
2247   }
2248 
2249   // Diagnose misuse of /Fa.
2250   if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fa)) {
2251     StringRef V = A->getValue();
2252     if (Inputs.size() > 1 && !V.empty() &&
2253         !llvm::sys::path::is_separator(V.back())) {
2254       // Check whether /Fa tries to name an asm file for multiple inputs.
2255       Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources)
2256           << A->getSpelling() << V;
2257       Args.eraseArg(options::OPT__SLASH_Fa);
2258     }
2259   }
2260 
2261   // Diagnose misuse of /o.
2262   if (Arg *A = Args.getLastArg(options::OPT__SLASH_o)) {
2263     if (A->getValue()[0] == '\0') {
2264       // It has to have a value.
2265       Diag(clang::diag::err_drv_missing_argument) << A->getSpelling() << 1;
2266       Args.eraseArg(options::OPT__SLASH_o);
2267     }
2268   }
2269 
2270   // Diagnose unsupported forms of /Yc /Yu. Ignore /Yc/Yu for now if:
2271   // * no filename after it
2272   // * both /Yc and /Yu passed but with different filenames
2273   // * corresponding file not also passed as /FI
2274   Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc);
2275   Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu);
2276   if (YcArg && YcArg->getValue()[0] == '\0') {
2277     Diag(clang::diag::warn_drv_ycyu_no_arg_clang_cl) << YcArg->getSpelling();
2278     Args.eraseArg(options::OPT__SLASH_Yc);
2279     YcArg = nullptr;
2280   }
2281   if (YuArg && YuArg->getValue()[0] == '\0') {
2282     Diag(clang::diag::warn_drv_ycyu_no_arg_clang_cl) << YuArg->getSpelling();
2283     Args.eraseArg(options::OPT__SLASH_Yu);
2284     YuArg = nullptr;
2285   }
2286   if (YcArg && YuArg && strcmp(YcArg->getValue(), YuArg->getValue()) != 0) {
2287     Diag(clang::diag::warn_drv_ycyu_different_arg_clang_cl);
2288     Args.eraseArg(options::OPT__SLASH_Yc);
2289     Args.eraseArg(options::OPT__SLASH_Yu);
2290     YcArg = YuArg = nullptr;
2291   }
2292   if (YcArg || YuArg) {
2293     StringRef Val = YcArg ? YcArg->getValue() : YuArg->getValue();
2294     bool FoundMatchingInclude = false;
2295     for (const Arg *Inc : Args.filtered(options::OPT_include)) {
2296       // FIXME: Do case-insensitive matching and consider / and \ as equal.
2297       if (Inc->getValue() == Val)
2298         FoundMatchingInclude = true;
2299     }
2300     if (!FoundMatchingInclude) {
2301       Diag(clang::diag::warn_drv_ycyu_no_fi_arg_clang_cl)
2302           << (YcArg ? YcArg : YuArg)->getSpelling();
2303       Args.eraseArg(options::OPT__SLASH_Yc);
2304       Args.eraseArg(options::OPT__SLASH_Yu);
2305       YcArg = YuArg = nullptr;
2306     }
2307   }
2308   if (YcArg && Inputs.size() > 1) {
2309     Diag(clang::diag::warn_drv_yc_multiple_inputs_clang_cl);
2310     Args.eraseArg(options::OPT__SLASH_Yc);
2311     YcArg = nullptr;
2312   }
2313   if (Args.hasArg(options::OPT__SLASH_Y_)) {
2314     // /Y- disables all pch handling.  Rather than check for it everywhere,
2315     // just remove clang-cl pch-related flags here.
2316     Args.eraseArg(options::OPT__SLASH_Fp);
2317     Args.eraseArg(options::OPT__SLASH_Yc);
2318     Args.eraseArg(options::OPT__SLASH_Yu);
2319     YcArg = YuArg = nullptr;
2320   }
2321 
2322   // Builder to be used to build offloading actions.
2323   OffloadingActionBuilder OffloadBuilder(C, Args, Inputs);
2324 
2325   // Construct the actions to perform.
2326   ActionList LinkerInputs;
2327 
2328   llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PL;
2329   for (auto &I : Inputs) {
2330     types::ID InputType = I.first;
2331     const Arg *InputArg = I.second;
2332 
2333     PL.clear();
2334     types::getCompilationPhases(InputType, PL);
2335 
2336     // If the first step comes after the final phase we are doing as part of
2337     // this compilation, warn the user about it.
2338     phases::ID InitialPhase = PL[0];
2339     if (InitialPhase > FinalPhase) {
2340       // Claim here to avoid the more general unused warning.
2341       InputArg->claim();
2342 
2343       // Suppress all unused style warnings with -Qunused-arguments
2344       if (Args.hasArg(options::OPT_Qunused_arguments))
2345         continue;
2346 
2347       // Special case when final phase determined by binary name, rather than
2348       // by a command-line argument with a corresponding Arg.
2349       if (CCCIsCPP())
2350         Diag(clang::diag::warn_drv_input_file_unused_by_cpp)
2351             << InputArg->getAsString(Args) << getPhaseName(InitialPhase);
2352       // Special case '-E' warning on a previously preprocessed file to make
2353       // more sense.
2354       else if (InitialPhase == phases::Compile &&
2355                FinalPhase == phases::Preprocess &&
2356                getPreprocessedType(InputType) == types::TY_INVALID)
2357         Diag(clang::diag::warn_drv_preprocessed_input_file_unused)
2358             << InputArg->getAsString(Args) << !!FinalPhaseArg
2359             << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "");
2360       else
2361         Diag(clang::diag::warn_drv_input_file_unused)
2362             << InputArg->getAsString(Args) << getPhaseName(InitialPhase)
2363             << !!FinalPhaseArg
2364             << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "");
2365       continue;
2366     }
2367 
2368     if (YcArg) {
2369       // Add a separate precompile phase for the compile phase.
2370       if (FinalPhase >= phases::Compile) {
2371         const types::ID HeaderType = lookupHeaderTypeForSourceType(InputType);
2372         llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PCHPL;
2373         types::getCompilationPhases(HeaderType, PCHPL);
2374         Arg *PchInputArg = MakeInputArg(Args, Opts, YcArg->getValue());
2375 
2376         // Build the pipeline for the pch file.
2377         Action *ClangClPch =
2378             C.MakeAction<InputAction>(*PchInputArg, HeaderType);
2379         for (phases::ID Phase : PCHPL)
2380           ClangClPch = ConstructPhaseAction(C, Args, Phase, ClangClPch);
2381         assert(ClangClPch);
2382         Actions.push_back(ClangClPch);
2383         // The driver currently exits after the first failed command.  This
2384         // relies on that behavior, to make sure if the pch generation fails,
2385         // the main compilation won't run.
2386       }
2387     }
2388 
2389     // Build the pipeline for this file.
2390     Action *Current = C.MakeAction<InputAction>(*InputArg, InputType);
2391 
2392     // Use the current host action in any of the offloading actions, if
2393     // required.
2394     if (OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg))
2395       break;
2396 
2397     for (SmallVectorImpl<phases::ID>::iterator i = PL.begin(), e = PL.end();
2398          i != e; ++i) {
2399       phases::ID Phase = *i;
2400 
2401       // We are done if this step is past what the user requested.
2402       if (Phase > FinalPhase)
2403         break;
2404 
2405       // Add any offload action the host action depends on.
2406       Current = OffloadBuilder.addDeviceDependencesToHostAction(
2407           Current, InputArg, Phase, FinalPhase, PL);
2408       if (!Current)
2409         break;
2410 
2411       // Queue linker inputs.
2412       if (Phase == phases::Link) {
2413         assert((i + 1) == e && "linking must be final compilation step.");
2414         LinkerInputs.push_back(Current);
2415         Current = nullptr;
2416         break;
2417       }
2418 
2419       // Otherwise construct the appropriate action.
2420       auto *NewCurrent = ConstructPhaseAction(C, Args, Phase, Current);
2421 
2422       // We didn't create a new action, so we will just move to the next phase.
2423       if (NewCurrent == Current)
2424         continue;
2425 
2426       Current = NewCurrent;
2427 
2428       // Use the current host action in any of the offloading actions, if
2429       // required.
2430       if (OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg))
2431         break;
2432 
2433       if (Current->getType() == types::TY_Nothing)
2434         break;
2435     }
2436 
2437     // If we ended with something, add to the output list.
2438     if (Current)
2439       Actions.push_back(Current);
2440 
2441     // Add any top level actions generated for offloading.
2442     OffloadBuilder.appendTopLevelActions(Actions, Current, InputArg);
2443   }
2444 
2445   // Add a link action if necessary.
2446   if (!LinkerInputs.empty()) {
2447     Action *LA = C.MakeAction<LinkJobAction>(LinkerInputs, types::TY_Image);
2448     LA = OffloadBuilder.processHostLinkAction(LA);
2449     Actions.push_back(LA);
2450   }
2451 
2452   // If we are linking, claim any options which are obviously only used for
2453   // compilation.
2454   if (FinalPhase == phases::Link && PL.size() == 1) {
2455     Args.ClaimAllArgs(options::OPT_CompileOnly_Group);
2456     Args.ClaimAllArgs(options::OPT_cl_compile_Group);
2457   }
2458 
2459   // Claim ignored clang-cl options.
2460   Args.ClaimAllArgs(options::OPT_cl_ignored_Group);
2461 
2462   // Claim --cuda-host-only and --cuda-compile-host-device, which may be passed
2463   // to non-CUDA compilations and should not trigger warnings there.
2464   Args.ClaimAllArgs(options::OPT_cuda_host_only);
2465   Args.ClaimAllArgs(options::OPT_cuda_compile_host_device);
2466 }
2467 
2468 Action *Driver::ConstructPhaseAction(Compilation &C, const ArgList &Args,
2469                                      phases::ID Phase, Action *Input) const {
2470   llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
2471 
2472   // Some types skip the assembler phase (e.g., llvm-bc), but we can't
2473   // encode this in the steps because the intermediate type depends on
2474   // arguments. Just special case here.
2475   if (Phase == phases::Assemble && Input->getType() != types::TY_PP_Asm)
2476     return Input;
2477 
2478   // Build the appropriate action.
2479   switch (Phase) {
2480   case phases::Link:
2481     llvm_unreachable("link action invalid here.");
2482   case phases::Preprocess: {
2483     types::ID OutputTy;
2484     // -{M, MM} alter the output type.
2485     if (Args.hasArg(options::OPT_M, options::OPT_MM)) {
2486       OutputTy = types::TY_Dependencies;
2487     } else {
2488       OutputTy = Input->getType();
2489       if (!Args.hasFlag(options::OPT_frewrite_includes,
2490                         options::OPT_fno_rewrite_includes, false) &&
2491           !CCGenDiagnostics)
2492         OutputTy = types::getPreprocessedType(OutputTy);
2493       assert(OutputTy != types::TY_INVALID &&
2494              "Cannot preprocess this input type!");
2495     }
2496     return C.MakeAction<PreprocessJobAction>(Input, OutputTy);
2497   }
2498   case phases::Precompile: {
2499     types::ID OutputTy = getPrecompiledType(Input->getType());
2500     assert(OutputTy != types::TY_INVALID &&
2501            "Cannot precompile this input type!");
2502     if (Args.hasArg(options::OPT_fsyntax_only)) {
2503       // Syntax checks should not emit a PCH file
2504       OutputTy = types::TY_Nothing;
2505     }
2506     return C.MakeAction<PrecompileJobAction>(Input, OutputTy);
2507   }
2508   case phases::Compile: {
2509     if (Args.hasArg(options::OPT_fsyntax_only))
2510       return C.MakeAction<CompileJobAction>(Input, types::TY_Nothing);
2511     if (Args.hasArg(options::OPT_rewrite_objc))
2512       return C.MakeAction<CompileJobAction>(Input, types::TY_RewrittenObjC);
2513     if (Args.hasArg(options::OPT_rewrite_legacy_objc))
2514       return C.MakeAction<CompileJobAction>(Input,
2515                                             types::TY_RewrittenLegacyObjC);
2516     if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto))
2517       return C.MakeAction<AnalyzeJobAction>(Input, types::TY_Plist);
2518     if (Args.hasArg(options::OPT__migrate))
2519       return C.MakeAction<MigrateJobAction>(Input, types::TY_Remap);
2520     if (Args.hasArg(options::OPT_emit_ast))
2521       return C.MakeAction<CompileJobAction>(Input, types::TY_AST);
2522     if (Args.hasArg(options::OPT_module_file_info))
2523       return C.MakeAction<CompileJobAction>(Input, types::TY_ModuleFile);
2524     if (Args.hasArg(options::OPT_verify_pch))
2525       return C.MakeAction<VerifyPCHJobAction>(Input, types::TY_Nothing);
2526     return C.MakeAction<CompileJobAction>(Input, types::TY_LLVM_BC);
2527   }
2528   case phases::Backend: {
2529     if (isUsingLTO()) {
2530       types::ID Output =
2531           Args.hasArg(options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC;
2532       return C.MakeAction<BackendJobAction>(Input, Output);
2533     }
2534     if (Args.hasArg(options::OPT_emit_llvm)) {
2535       types::ID Output =
2536           Args.hasArg(options::OPT_S) ? types::TY_LLVM_IR : types::TY_LLVM_BC;
2537       return C.MakeAction<BackendJobAction>(Input, Output);
2538     }
2539     return C.MakeAction<BackendJobAction>(Input, types::TY_PP_Asm);
2540   }
2541   case phases::Assemble:
2542     return C.MakeAction<AssembleJobAction>(std::move(Input), types::TY_Object);
2543   }
2544 
2545   llvm_unreachable("invalid phase in ConstructPhaseAction");
2546 }
2547 
2548 void Driver::BuildJobs(Compilation &C) const {
2549   llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
2550 
2551   Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
2552 
2553   // It is an error to provide a -o option if we are making multiple output
2554   // files.
2555   if (FinalOutput) {
2556     unsigned NumOutputs = 0;
2557     for (const Action *A : C.getActions())
2558       if (A->getType() != types::TY_Nothing)
2559         ++NumOutputs;
2560 
2561     if (NumOutputs > 1) {
2562       Diag(clang::diag::err_drv_output_argument_with_multiple_files);
2563       FinalOutput = nullptr;
2564     }
2565   }
2566 
2567   // Collect the list of architectures.
2568   llvm::StringSet<> ArchNames;
2569   if (C.getDefaultToolChain().getTriple().isOSBinFormatMachO())
2570     for (const Arg *A : C.getArgs())
2571       if (A->getOption().matches(options::OPT_arch))
2572         ArchNames.insert(A->getValue());
2573 
2574   // Set of (Action, canonical ToolChain triple) pairs we've built jobs for.
2575   std::map<std::pair<const Action *, std::string>, InputInfo> CachedResults;
2576   for (Action *A : C.getActions()) {
2577     // If we are linking an image for multiple archs then the linker wants
2578     // -arch_multiple and -final_output <final image name>. Unfortunately, this
2579     // doesn't fit in cleanly because we have to pass this information down.
2580     //
2581     // FIXME: This is a hack; find a cleaner way to integrate this into the
2582     // process.
2583     const char *LinkingOutput = nullptr;
2584     if (isa<LipoJobAction>(A)) {
2585       if (FinalOutput)
2586         LinkingOutput = FinalOutput->getValue();
2587       else
2588         LinkingOutput = getDefaultImageName();
2589     }
2590 
2591     BuildJobsForAction(C, A, &C.getDefaultToolChain(),
2592                        /*BoundArch*/ StringRef(),
2593                        /*AtTopLevel*/ true,
2594                        /*MultipleArchs*/ ArchNames.size() > 1,
2595                        /*LinkingOutput*/ LinkingOutput, CachedResults,
2596                        /*TargetDeviceOffloadKind*/ Action::OFK_None);
2597   }
2598 
2599   // If the user passed -Qunused-arguments or there were errors, don't warn
2600   // about any unused arguments.
2601   if (Diags.hasErrorOccurred() ||
2602       C.getArgs().hasArg(options::OPT_Qunused_arguments))
2603     return;
2604 
2605   // Claim -### here.
2606   (void)C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
2607 
2608   // Claim --driver-mode, --rsp-quoting, it was handled earlier.
2609   (void)C.getArgs().hasArg(options::OPT_driver_mode);
2610   (void)C.getArgs().hasArg(options::OPT_rsp_quoting);
2611 
2612   for (Arg *A : C.getArgs()) {
2613     // FIXME: It would be nice to be able to send the argument to the
2614     // DiagnosticsEngine, so that extra values, position, and so on could be
2615     // printed.
2616     if (!A->isClaimed()) {
2617       if (A->getOption().hasFlag(options::NoArgumentUnused))
2618         continue;
2619 
2620       // Suppress the warning automatically if this is just a flag, and it is an
2621       // instance of an argument we already claimed.
2622       const Option &Opt = A->getOption();
2623       if (Opt.getKind() == Option::FlagClass) {
2624         bool DuplicateClaimed = false;
2625 
2626         for (const Arg *AA : C.getArgs().filtered(&Opt)) {
2627           if (AA->isClaimed()) {
2628             DuplicateClaimed = true;
2629             break;
2630           }
2631         }
2632 
2633         if (DuplicateClaimed)
2634           continue;
2635       }
2636 
2637       // In clang-cl, don't mention unknown arguments here since they have
2638       // already been warned about.
2639       if (!IsCLMode() || !A->getOption().matches(options::OPT_UNKNOWN))
2640         Diag(clang::diag::warn_drv_unused_argument)
2641             << A->getAsString(C.getArgs());
2642     }
2643   }
2644 }
2645 
2646 namespace {
2647 /// Utility class to control the collapse of dependent actions and select the
2648 /// tools accordingly.
2649 class ToolSelector final {
2650   /// The tool chain this selector refers to.
2651   const ToolChain &TC;
2652 
2653   /// The compilation this selector refers to.
2654   const Compilation &C;
2655 
2656   /// The base action this selector refers to.
2657   const JobAction *BaseAction;
2658 
2659   /// Set to true if the current toolchain refers to host actions.
2660   bool IsHostSelector;
2661 
2662   /// Set to true if save-temps and embed-bitcode functionalities are active.
2663   bool SaveTemps;
2664   bool EmbedBitcode;
2665 
2666   /// Get previous dependent action or null if that does not exist. If
2667   /// \a CanBeCollapsed is false, that action must be legal to collapse or
2668   /// null will be returned.
2669   const JobAction *getPrevDependentAction(const ActionList &Inputs,
2670                                           ActionList &SavedOffloadAction,
2671                                           bool CanBeCollapsed = true) {
2672     // An option can be collapsed only if it has a single input.
2673     if (Inputs.size() != 1)
2674       return nullptr;
2675 
2676     Action *CurAction = *Inputs.begin();
2677     if (CanBeCollapsed &&
2678         !CurAction->isCollapsingWithNextDependentActionLegal())
2679       return nullptr;
2680 
2681     // If the input action is an offload action. Look through it and save any
2682     // offload action that can be dropped in the event of a collapse.
2683     if (auto *OA = dyn_cast<OffloadAction>(CurAction)) {
2684       // If the dependent action is a device action, we will attempt to collapse
2685       // only with other device actions. Otherwise, we would do the same but
2686       // with host actions only.
2687       if (!IsHostSelector) {
2688         if (OA->hasSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)) {
2689           CurAction =
2690               OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true);
2691           if (CanBeCollapsed &&
2692               !CurAction->isCollapsingWithNextDependentActionLegal())
2693             return nullptr;
2694           SavedOffloadAction.push_back(OA);
2695           return dyn_cast<JobAction>(CurAction);
2696         }
2697       } else if (OA->hasHostDependence()) {
2698         CurAction = OA->getHostDependence();
2699         if (CanBeCollapsed &&
2700             !CurAction->isCollapsingWithNextDependentActionLegal())
2701           return nullptr;
2702         SavedOffloadAction.push_back(OA);
2703         return dyn_cast<JobAction>(CurAction);
2704       }
2705       return nullptr;
2706     }
2707 
2708     return dyn_cast<JobAction>(CurAction);
2709   }
2710 
2711   /// Return true if an assemble action can be collapsed.
2712   bool canCollapseAssembleAction() const {
2713     return TC.useIntegratedAs() && !SaveTemps &&
2714            !C.getArgs().hasArg(options::OPT_via_file_asm) &&
2715            !C.getArgs().hasArg(options::OPT__SLASH_FA) &&
2716            !C.getArgs().hasArg(options::OPT__SLASH_Fa);
2717   }
2718 
2719   /// Return true if a preprocessor action can be collapsed.
2720   bool canCollapsePreprocessorAction() const {
2721     return !C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
2722            !C.getArgs().hasArg(options::OPT_traditional_cpp) && !SaveTemps &&
2723            !C.getArgs().hasArg(options::OPT_rewrite_objc);
2724   }
2725 
2726   /// Struct that relates an action with the offload actions that would be
2727   /// collapsed with it.
2728   struct JobActionInfo final {
2729     /// The action this info refers to.
2730     const JobAction *JA = nullptr;
2731     /// The offload actions we need to take care off if this action is
2732     /// collapsed.
2733     ActionList SavedOffloadAction;
2734   };
2735 
2736   /// Append collapsed offload actions from the give nnumber of elements in the
2737   /// action info array.
2738   static void AppendCollapsedOffloadAction(ActionList &CollapsedOffloadAction,
2739                                            ArrayRef<JobActionInfo> &ActionInfo,
2740                                            unsigned ElementNum) {
2741     assert(ElementNum <= ActionInfo.size() && "Invalid number of elements.");
2742     for (unsigned I = 0; I < ElementNum; ++I)
2743       CollapsedOffloadAction.append(ActionInfo[I].SavedOffloadAction.begin(),
2744                                     ActionInfo[I].SavedOffloadAction.end());
2745   }
2746 
2747   /// Functions that attempt to perform the combining. They detect if that is
2748   /// legal, and if so they update the inputs \a Inputs and the offload action
2749   /// that were collapsed in \a CollapsedOffloadAction. A tool that deals with
2750   /// the combined action is returned. If the combining is not legal or if the
2751   /// tool does not exist, null is returned.
2752   /// Currently three kinds of collapsing are supported:
2753   ///  - Assemble + Backend + Compile;
2754   ///  - Assemble + Backend ;
2755   ///  - Backend + Compile.
2756   const Tool *
2757   combineAssembleBackendCompile(ArrayRef<JobActionInfo> ActionInfo,
2758                                 const ActionList *&Inputs,
2759                                 ActionList &CollapsedOffloadAction) {
2760     if (ActionInfo.size() < 3 || !canCollapseAssembleAction())
2761       return nullptr;
2762     auto *AJ = dyn_cast<AssembleJobAction>(ActionInfo[0].JA);
2763     auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[1].JA);
2764     auto *CJ = dyn_cast<CompileJobAction>(ActionInfo[2].JA);
2765     if (!AJ || !BJ || !CJ)
2766       return nullptr;
2767 
2768     // Get compiler tool.
2769     const Tool *T = TC.SelectTool(*CJ);
2770     if (!T)
2771       return nullptr;
2772 
2773     // When using -fembed-bitcode, it is required to have the same tool (clang)
2774     // for both CompilerJA and BackendJA. Otherwise, combine two stages.
2775     if (EmbedBitcode) {
2776       const Tool *BT = TC.SelectTool(*BJ);
2777       if (BT == T)
2778         return nullptr;
2779     }
2780 
2781     if (!T->hasIntegratedAssembler())
2782       return nullptr;
2783 
2784     Inputs = &CJ->getInputs();
2785     AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
2786                                  /*NumElements=*/3);
2787     return T;
2788   }
2789   const Tool *combineAssembleBackend(ArrayRef<JobActionInfo> ActionInfo,
2790                                      const ActionList *&Inputs,
2791                                      ActionList &CollapsedOffloadAction) {
2792     if (ActionInfo.size() < 2 || !canCollapseAssembleAction())
2793       return nullptr;
2794     auto *AJ = dyn_cast<AssembleJobAction>(ActionInfo[0].JA);
2795     auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[1].JA);
2796     if (!AJ || !BJ)
2797       return nullptr;
2798 
2799     // Retrieve the compile job, backend action must always be preceded by one.
2800     ActionList CompileJobOffloadActions;
2801     auto *CJ = getPrevDependentAction(BJ->getInputs(), CompileJobOffloadActions,
2802                                       /*CanBeCollapsed=*/false);
2803     if (!AJ || !BJ || !CJ)
2804       return nullptr;
2805 
2806     assert(isa<CompileJobAction>(CJ) &&
2807            "Expecting compile job preceding backend job.");
2808 
2809     // Get compiler tool.
2810     const Tool *T = TC.SelectTool(*CJ);
2811     if (!T)
2812       return nullptr;
2813 
2814     if (!T->hasIntegratedAssembler())
2815       return nullptr;
2816 
2817     Inputs = &BJ->getInputs();
2818     AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
2819                                  /*NumElements=*/2);
2820     return T;
2821   }
2822   const Tool *combineBackendCompile(ArrayRef<JobActionInfo> ActionInfo,
2823                                     const ActionList *&Inputs,
2824                                     ActionList &CollapsedOffloadAction) {
2825     if (ActionInfo.size() < 2 || !canCollapsePreprocessorAction())
2826       return nullptr;
2827     auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[0].JA);
2828     auto *CJ = dyn_cast<CompileJobAction>(ActionInfo[1].JA);
2829     if (!BJ || !CJ)
2830       return nullptr;
2831 
2832     // Get compiler tool.
2833     const Tool *T = TC.SelectTool(*CJ);
2834     if (!T)
2835       return nullptr;
2836 
2837     if (T->canEmitIR() && (SaveTemps || EmbedBitcode))
2838       return nullptr;
2839 
2840     Inputs = &CJ->getInputs();
2841     AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
2842                                  /*NumElements=*/2);
2843     return T;
2844   }
2845 
2846   /// Updates the inputs if the obtained tool supports combining with
2847   /// preprocessor action, and the current input is indeed a preprocessor
2848   /// action. If combining results in the collapse of offloading actions, those
2849   /// are appended to \a CollapsedOffloadAction.
2850   void combineWithPreprocessor(const Tool *T, const ActionList *&Inputs,
2851                                ActionList &CollapsedOffloadAction) {
2852     if (!T || !canCollapsePreprocessorAction() || !T->hasIntegratedCPP())
2853       return;
2854 
2855     // Attempt to get a preprocessor action dependence.
2856     ActionList PreprocessJobOffloadActions;
2857     auto *PJ = getPrevDependentAction(*Inputs, PreprocessJobOffloadActions);
2858     if (!PJ || !isa<PreprocessJobAction>(PJ))
2859       return;
2860 
2861     // This is legal to combine. Append any offload action we found and set the
2862     // current inputs to preprocessor inputs.
2863     CollapsedOffloadAction.append(PreprocessJobOffloadActions.begin(),
2864                                   PreprocessJobOffloadActions.end());
2865     Inputs = &PJ->getInputs();
2866   }
2867 
2868 public:
2869   ToolSelector(const JobAction *BaseAction, const ToolChain &TC,
2870                const Compilation &C, bool SaveTemps, bool EmbedBitcode)
2871       : TC(TC), C(C), BaseAction(BaseAction), SaveTemps(SaveTemps),
2872         EmbedBitcode(EmbedBitcode) {
2873     assert(BaseAction && "Invalid base action.");
2874     IsHostSelector = BaseAction->getOffloadingDeviceKind() == Action::OFK_None;
2875   }
2876 
2877   /// Check if a chain of actions can be combined and return the tool that can
2878   /// handle the combination of actions. The pointer to the current inputs \a
2879   /// Inputs and the list of offload actions \a CollapsedOffloadActions
2880   /// connected to collapsed actions are updated accordingly. The latter enables
2881   /// the caller of the selector to process them afterwards instead of just
2882   /// dropping them. If no suitable tool is found, null will be returned.
2883   const Tool *getTool(const ActionList *&Inputs,
2884                       ActionList &CollapsedOffloadAction) {
2885     //
2886     // Get the largest chain of actions that we could combine.
2887     //
2888 
2889     SmallVector<JobActionInfo, 5> ActionChain(1);
2890     ActionChain.back().JA = BaseAction;
2891     while (ActionChain.back().JA) {
2892       const Action *CurAction = ActionChain.back().JA;
2893 
2894       // Grow the chain by one element.
2895       ActionChain.resize(ActionChain.size() + 1);
2896       JobActionInfo &AI = ActionChain.back();
2897 
2898       // Attempt to fill it with the
2899       AI.JA =
2900           getPrevDependentAction(CurAction->getInputs(), AI.SavedOffloadAction);
2901     }
2902 
2903     // Pop the last action info as it could not be filled.
2904     ActionChain.pop_back();
2905 
2906     //
2907     // Attempt to combine actions. If all combining attempts failed, just return
2908     // the tool of the provided action. At the end we attempt to combine the
2909     // action with any preprocessor action it may depend on.
2910     //
2911 
2912     const Tool *T = combineAssembleBackendCompile(ActionChain, Inputs,
2913                                                   CollapsedOffloadAction);
2914     if (!T)
2915       T = combineAssembleBackend(ActionChain, Inputs, CollapsedOffloadAction);
2916     if (!T)
2917       T = combineBackendCompile(ActionChain, Inputs, CollapsedOffloadAction);
2918     if (!T) {
2919       Inputs = &BaseAction->getInputs();
2920       T = TC.SelectTool(*BaseAction);
2921     }
2922 
2923     combineWithPreprocessor(T, Inputs, CollapsedOffloadAction);
2924     return T;
2925   }
2926 };
2927 }
2928 
2929 /// Return a string that uniquely identifies the result of a job. The bound arch
2930 /// is not necessarily represented in the toolchain's triple -- for example,
2931 /// armv7 and armv7s both map to the same triple -- so we need both in our map.
2932 /// Also, we need to add the offloading device kind, as the same tool chain can
2933 /// be used for host and device for some programming models, e.g. OpenMP.
2934 static std::string GetTriplePlusArchString(const ToolChain *TC,
2935                                            StringRef BoundArch,
2936                                            Action::OffloadKind OffloadKind) {
2937   std::string TriplePlusArch = TC->getTriple().normalize();
2938   if (!BoundArch.empty()) {
2939     TriplePlusArch += "-";
2940     TriplePlusArch += BoundArch;
2941   }
2942   TriplePlusArch += "-";
2943   TriplePlusArch += Action::GetOffloadKindName(OffloadKind);
2944   return TriplePlusArch;
2945 }
2946 
2947 InputInfo Driver::BuildJobsForAction(
2948     Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch,
2949     bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput,
2950     std::map<std::pair<const Action *, std::string>, InputInfo> &CachedResults,
2951     Action::OffloadKind TargetDeviceOffloadKind) const {
2952   std::pair<const Action *, std::string> ActionTC = {
2953       A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)};
2954   auto CachedResult = CachedResults.find(ActionTC);
2955   if (CachedResult != CachedResults.end()) {
2956     return CachedResult->second;
2957   }
2958   InputInfo Result = BuildJobsForActionNoCache(
2959       C, A, TC, BoundArch, AtTopLevel, MultipleArchs, LinkingOutput,
2960       CachedResults, TargetDeviceOffloadKind);
2961   CachedResults[ActionTC] = Result;
2962   return Result;
2963 }
2964 
2965 InputInfo Driver::BuildJobsForActionNoCache(
2966     Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch,
2967     bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput,
2968     std::map<std::pair<const Action *, std::string>, InputInfo> &CachedResults,
2969     Action::OffloadKind TargetDeviceOffloadKind) const {
2970   llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
2971 
2972   InputInfoList OffloadDependencesInputInfo;
2973   bool BuildingForOffloadDevice = TargetDeviceOffloadKind != Action::OFK_None;
2974   if (const OffloadAction *OA = dyn_cast<OffloadAction>(A)) {
2975     // The offload action is expected to be used in four different situations.
2976     //
2977     // a) Set a toolchain/architecture/kind for a host action:
2978     //    Host Action 1 -> OffloadAction -> Host Action 2
2979     //
2980     // b) Set a toolchain/architecture/kind for a device action;
2981     //    Device Action 1 -> OffloadAction -> Device Action 2
2982     //
2983     // c) Specify a device dependence to a host action;
2984     //    Device Action 1  _
2985     //                      \
2986     //      Host Action 1  ---> OffloadAction -> Host Action 2
2987     //
2988     // d) Specify a host dependence to a device action.
2989     //      Host Action 1  _
2990     //                      \
2991     //    Device Action 1  ---> OffloadAction -> Device Action 2
2992     //
2993     // For a) and b), we just return the job generated for the dependence. For
2994     // c) and d) we override the current action with the host/device dependence
2995     // if the current toolchain is host/device and set the offload dependences
2996     // info with the jobs obtained from the device/host dependence(s).
2997 
2998     // If there is a single device option, just generate the job for it.
2999     if (OA->hasSingleDeviceDependence()) {
3000       InputInfo DevA;
3001       OA->doOnEachDeviceDependence([&](Action *DepA, const ToolChain *DepTC,
3002                                        const char *DepBoundArch) {
3003         DevA =
3004             BuildJobsForAction(C, DepA, DepTC, DepBoundArch, AtTopLevel,
3005                                /*MultipleArchs*/ !!DepBoundArch, LinkingOutput,
3006                                CachedResults, DepA->getOffloadingDeviceKind());
3007       });
3008       return DevA;
3009     }
3010 
3011     // If 'Action 2' is host, we generate jobs for the device dependences and
3012     // override the current action with the host dependence. Otherwise, we
3013     // generate the host dependences and override the action with the device
3014     // dependence. The dependences can't therefore be a top-level action.
3015     OA->doOnEachDependence(
3016         /*IsHostDependence=*/BuildingForOffloadDevice,
3017         [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) {
3018           OffloadDependencesInputInfo.push_back(BuildJobsForAction(
3019               C, DepA, DepTC, DepBoundArch, /*AtTopLevel=*/false,
3020               /*MultipleArchs*/ !!DepBoundArch, LinkingOutput, CachedResults,
3021               DepA->getOffloadingDeviceKind()));
3022         });
3023 
3024     A = BuildingForOffloadDevice
3025             ? OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)
3026             : OA->getHostDependence();
3027   }
3028 
3029   if (const InputAction *IA = dyn_cast<InputAction>(A)) {
3030     // FIXME: It would be nice to not claim this here; maybe the old scheme of
3031     // just using Args was better?
3032     const Arg &Input = IA->getInputArg();
3033     Input.claim();
3034     if (Input.getOption().matches(options::OPT_INPUT)) {
3035       const char *Name = Input.getValue();
3036       return InputInfo(A, Name, /* BaseInput = */ Name);
3037     }
3038     return InputInfo(A, &Input, /* BaseInput = */ "");
3039   }
3040 
3041   if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
3042     const ToolChain *TC;
3043     StringRef ArchName = BAA->getArchName();
3044 
3045     if (!ArchName.empty())
3046       TC = &getToolChain(C.getArgs(),
3047                          computeTargetTriple(*this, DefaultTargetTriple,
3048                                              C.getArgs(), ArchName));
3049     else
3050       TC = &C.getDefaultToolChain();
3051 
3052     return BuildJobsForAction(C, *BAA->input_begin(), TC, ArchName, AtTopLevel,
3053                               MultipleArchs, LinkingOutput, CachedResults,
3054                               TargetDeviceOffloadKind);
3055   }
3056 
3057 
3058   const ActionList *Inputs = &A->getInputs();
3059 
3060   const JobAction *JA = cast<JobAction>(A);
3061   ActionList CollapsedOffloadActions;
3062 
3063   ToolSelector TS(JA, *TC, C, isSaveTempsEnabled(), embedBitcodeEnabled());
3064   const Tool *T = TS.getTool(Inputs, CollapsedOffloadActions);
3065 
3066   if (!T)
3067     return InputInfo();
3068 
3069   // If we've collapsed action list that contained OffloadAction we
3070   // need to build jobs for host/device-side inputs it may have held.
3071   for (const auto *OA : CollapsedOffloadActions)
3072     cast<OffloadAction>(OA)->doOnEachDependence(
3073         /*IsHostDependence=*/BuildingForOffloadDevice,
3074         [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) {
3075           OffloadDependencesInputInfo.push_back(BuildJobsForAction(
3076               C, DepA, DepTC, DepBoundArch, /* AtTopLevel */ false,
3077               /*MultipleArchs=*/!!DepBoundArch, LinkingOutput, CachedResults,
3078               DepA->getOffloadingDeviceKind()));
3079         });
3080 
3081   // Only use pipes when there is exactly one input.
3082   InputInfoList InputInfos;
3083   for (const Action *Input : *Inputs) {
3084     // Treat dsymutil and verify sub-jobs as being at the top-level too, they
3085     // shouldn't get temporary output names.
3086     // FIXME: Clean this up.
3087     bool SubJobAtTopLevel =
3088         AtTopLevel && (isa<DsymutilJobAction>(A) || isa<VerifyJobAction>(A));
3089     InputInfos.push_back(BuildJobsForAction(
3090         C, Input, TC, BoundArch, SubJobAtTopLevel, MultipleArchs, LinkingOutput,
3091         CachedResults, A->getOffloadingDeviceKind()));
3092   }
3093 
3094   // Always use the first input as the base input.
3095   const char *BaseInput = InputInfos[0].getBaseInput();
3096 
3097   // ... except dsymutil actions, which use their actual input as the base
3098   // input.
3099   if (JA->getType() == types::TY_dSYM)
3100     BaseInput = InputInfos[0].getFilename();
3101 
3102   // Append outputs of offload device jobs to the input list
3103   if (!OffloadDependencesInputInfo.empty())
3104     InputInfos.append(OffloadDependencesInputInfo.begin(),
3105                       OffloadDependencesInputInfo.end());
3106 
3107   // Set the effective triple of the toolchain for the duration of this job.
3108   llvm::Triple EffectiveTriple;
3109   const ToolChain &ToolTC = T->getToolChain();
3110   const ArgList &Args =
3111       C.getArgsForToolChain(TC, BoundArch, A->getOffloadingDeviceKind());
3112   if (InputInfos.size() != 1) {
3113     EffectiveTriple = llvm::Triple(ToolTC.ComputeEffectiveClangTriple(Args));
3114   } else {
3115     // Pass along the input type if it can be unambiguously determined.
3116     EffectiveTriple = llvm::Triple(
3117         ToolTC.ComputeEffectiveClangTriple(Args, InputInfos[0].getType()));
3118   }
3119   RegisterEffectiveTriple TripleRAII(ToolTC, EffectiveTriple);
3120 
3121   // Determine the place to write output to, if any.
3122   InputInfo Result;
3123   InputInfoList UnbundlingResults;
3124   if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(JA)) {
3125     // If we have an unbundling job, we need to create results for all the
3126     // outputs. We also update the results cache so that other actions using
3127     // this unbundling action can get the right results.
3128     for (auto &UI : UA->getDependentActionsInfo()) {
3129       assert(UI.DependentOffloadKind != Action::OFK_None &&
3130              "Unbundling with no offloading??");
3131 
3132       // Unbundling actions are never at the top level. When we generate the
3133       // offloading prefix, we also do that for the host file because the
3134       // unbundling action does not change the type of the output which can
3135       // cause a overwrite.
3136       std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix(
3137           UI.DependentOffloadKind,
3138           UI.DependentToolChain->getTriple().normalize(),
3139           /*CreatePrefixForHost=*/true);
3140       auto CurI = InputInfo(
3141           UA, GetNamedOutputPath(C, *UA, BaseInput, UI.DependentBoundArch,
3142                                  /*AtTopLevel=*/false, MultipleArchs,
3143                                  OffloadingPrefix),
3144           BaseInput);
3145       // Save the unbundling result.
3146       UnbundlingResults.push_back(CurI);
3147 
3148       // Get the unique string identifier for this dependence and cache the
3149       // result.
3150       CachedResults[{A, GetTriplePlusArchString(
3151                             UI.DependentToolChain, UI.DependentBoundArch,
3152                             UI.DependentOffloadKind)}] = CurI;
3153     }
3154 
3155     // Now that we have all the results generated, select the one that should be
3156     // returned for the current depending action.
3157     std::pair<const Action *, std::string> ActionTC = {
3158         A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)};
3159     assert(CachedResults.find(ActionTC) != CachedResults.end() &&
3160            "Result does not exist??");
3161     Result = CachedResults[ActionTC];
3162   } else if (JA->getType() == types::TY_Nothing)
3163     Result = InputInfo(A, BaseInput);
3164   else {
3165     // We only have to generate a prefix for the host if this is not a top-level
3166     // action.
3167     std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix(
3168         A->getOffloadingDeviceKind(), TC->getTriple().normalize(),
3169         /*CreatePrefixForHost=*/!!A->getOffloadingHostActiveKinds() &&
3170             !AtTopLevel);
3171     Result = InputInfo(A, GetNamedOutputPath(C, *JA, BaseInput, BoundArch,
3172                                              AtTopLevel, MultipleArchs,
3173                                              OffloadingPrefix),
3174                        BaseInput);
3175   }
3176 
3177   if (CCCPrintBindings && !CCGenDiagnostics) {
3178     llvm::errs() << "# \"" << T->getToolChain().getTripleString() << '"'
3179                  << " - \"" << T->getName() << "\", inputs: [";
3180     for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
3181       llvm::errs() << InputInfos[i].getAsString();
3182       if (i + 1 != e)
3183         llvm::errs() << ", ";
3184     }
3185     if (UnbundlingResults.empty())
3186       llvm::errs() << "], output: " << Result.getAsString() << "\n";
3187     else {
3188       llvm::errs() << "], outputs: [";
3189       for (unsigned i = 0, e = UnbundlingResults.size(); i != e; ++i) {
3190         llvm::errs() << UnbundlingResults[i].getAsString();
3191         if (i + 1 != e)
3192           llvm::errs() << ", ";
3193       }
3194       llvm::errs() << "] \n";
3195     }
3196   } else {
3197     if (UnbundlingResults.empty())
3198       T->ConstructJob(
3199           C, *JA, Result, InputInfos,
3200           C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()),
3201           LinkingOutput);
3202     else
3203       T->ConstructJob(
3204           C, *JA, UnbundlingResults, InputInfos,
3205           C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()),
3206           LinkingOutput);
3207   }
3208   return Result;
3209 }
3210 
3211 const char *Driver::getDefaultImageName() const {
3212   llvm::Triple Target(llvm::Triple::normalize(DefaultTargetTriple));
3213   return Target.isOSWindows() ? "a.exe" : "a.out";
3214 }
3215 
3216 /// \brief Create output filename based on ArgValue, which could either be a
3217 /// full filename, filename without extension, or a directory. If ArgValue
3218 /// does not provide a filename, then use BaseName, and use the extension
3219 /// suitable for FileType.
3220 static const char *MakeCLOutputFilename(const ArgList &Args, StringRef ArgValue,
3221                                         StringRef BaseName,
3222                                         types::ID FileType) {
3223   SmallString<128> Filename = ArgValue;
3224 
3225   if (ArgValue.empty()) {
3226     // If the argument is empty, output to BaseName in the current dir.
3227     Filename = BaseName;
3228   } else if (llvm::sys::path::is_separator(Filename.back())) {
3229     // If the argument is a directory, output to BaseName in that dir.
3230     llvm::sys::path::append(Filename, BaseName);
3231   }
3232 
3233   if (!llvm::sys::path::has_extension(ArgValue)) {
3234     // If the argument didn't provide an extension, then set it.
3235     const char *Extension = types::getTypeTempSuffix(FileType, true);
3236 
3237     if (FileType == types::TY_Image &&
3238         Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd)) {
3239       // The output file is a dll.
3240       Extension = "dll";
3241     }
3242 
3243     llvm::sys::path::replace_extension(Filename, Extension);
3244   }
3245 
3246   return Args.MakeArgString(Filename.c_str());
3247 }
3248 
3249 const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
3250                                        const char *BaseInput,
3251                                        StringRef BoundArch, bool AtTopLevel,
3252                                        bool MultipleArchs,
3253                                        StringRef OffloadingPrefix) const {
3254   llvm::PrettyStackTraceString CrashInfo("Computing output path");
3255   // Output to a user requested destination?
3256   if (AtTopLevel && !isa<DsymutilJobAction>(JA) && !isa<VerifyJobAction>(JA)) {
3257     if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
3258       return C.addResultFile(FinalOutput->getValue(), &JA);
3259   }
3260 
3261   // For /P, preprocess to file named after BaseInput.
3262   if (C.getArgs().hasArg(options::OPT__SLASH_P)) {
3263     assert(AtTopLevel && isa<PreprocessJobAction>(JA));
3264     StringRef BaseName = llvm::sys::path::filename(BaseInput);
3265     StringRef NameArg;
3266     if (Arg *A = C.getArgs().getLastArg(options::OPT__SLASH_Fi))
3267       NameArg = A->getValue();
3268     return C.addResultFile(
3269         MakeCLOutputFilename(C.getArgs(), NameArg, BaseName, types::TY_PP_C),
3270         &JA);
3271   }
3272 
3273   // Default to writing to stdout?
3274   if (AtTopLevel && !CCGenDiagnostics &&
3275       (isa<PreprocessJobAction>(JA) || JA.getType() == types::TY_ModuleFile))
3276     return "-";
3277 
3278   // Is this the assembly listing for /FA?
3279   if (JA.getType() == types::TY_PP_Asm &&
3280       (C.getArgs().hasArg(options::OPT__SLASH_FA) ||
3281        C.getArgs().hasArg(options::OPT__SLASH_Fa))) {
3282     // Use /Fa and the input filename to determine the asm file name.
3283     StringRef BaseName = llvm::sys::path::filename(BaseInput);
3284     StringRef FaValue = C.getArgs().getLastArgValue(options::OPT__SLASH_Fa);
3285     return C.addResultFile(
3286         MakeCLOutputFilename(C.getArgs(), FaValue, BaseName, JA.getType()),
3287         &JA);
3288   }
3289 
3290   // Output to a temporary file?
3291   if ((!AtTopLevel && !isSaveTempsEnabled() &&
3292        !C.getArgs().hasArg(options::OPT__SLASH_Fo)) ||
3293       CCGenDiagnostics) {
3294     StringRef Name = llvm::sys::path::filename(BaseInput);
3295     std::pair<StringRef, StringRef> Split = Name.split('.');
3296     std::string TmpName = GetTemporaryPath(
3297         Split.first, types::getTypeTempSuffix(JA.getType(), IsCLMode()));
3298     return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
3299   }
3300 
3301   SmallString<128> BasePath(BaseInput);
3302   StringRef BaseName;
3303 
3304   // Dsymutil actions should use the full path.
3305   if (isa<DsymutilJobAction>(JA) || isa<VerifyJobAction>(JA))
3306     BaseName = BasePath;
3307   else
3308     BaseName = llvm::sys::path::filename(BasePath);
3309 
3310   // Determine what the derived output name should be.
3311   const char *NamedOutput;
3312 
3313   if ((JA.getType() == types::TY_Object || JA.getType() == types::TY_LTO_BC) &&
3314       C.getArgs().hasArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)) {
3315     // The /Fo or /o flag decides the object filename.
3316     StringRef Val =
3317         C.getArgs()
3318             .getLastArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)
3319             ->getValue();
3320     NamedOutput =
3321         MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Object);
3322   } else if (JA.getType() == types::TY_Image &&
3323              C.getArgs().hasArg(options::OPT__SLASH_Fe,
3324                                 options::OPT__SLASH_o)) {
3325     // The /Fe or /o flag names the linked file.
3326     StringRef Val =
3327         C.getArgs()
3328             .getLastArg(options::OPT__SLASH_Fe, options::OPT__SLASH_o)
3329             ->getValue();
3330     NamedOutput =
3331         MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Image);
3332   } else if (JA.getType() == types::TY_Image) {
3333     if (IsCLMode()) {
3334       // clang-cl uses BaseName for the executable name.
3335       NamedOutput =
3336           MakeCLOutputFilename(C.getArgs(), "", BaseName, types::TY_Image);
3337     } else {
3338       SmallString<128> Output(getDefaultImageName());
3339       Output += OffloadingPrefix;
3340       if (MultipleArchs && !BoundArch.empty()) {
3341         Output += "-";
3342         Output.append(BoundArch);
3343       }
3344       NamedOutput = C.getArgs().MakeArgString(Output.c_str());
3345     }
3346   } else if (JA.getType() == types::TY_PCH && IsCLMode()) {
3347     NamedOutput = C.getArgs().MakeArgString(GetClPchPath(C, BaseName).c_str());
3348   } else {
3349     const char *Suffix = types::getTypeTempSuffix(JA.getType(), IsCLMode());
3350     assert(Suffix && "All types used for output should have a suffix.");
3351 
3352     std::string::size_type End = std::string::npos;
3353     if (!types::appendSuffixForType(JA.getType()))
3354       End = BaseName.rfind('.');
3355     SmallString<128> Suffixed(BaseName.substr(0, End));
3356     Suffixed += OffloadingPrefix;
3357     if (MultipleArchs && !BoundArch.empty()) {
3358       Suffixed += "-";
3359       Suffixed.append(BoundArch);
3360     }
3361     // When using both -save-temps and -emit-llvm, use a ".tmp.bc" suffix for
3362     // the unoptimized bitcode so that it does not get overwritten by the ".bc"
3363     // optimized bitcode output.
3364     if (!AtTopLevel && C.getArgs().hasArg(options::OPT_emit_llvm) &&
3365         JA.getType() == types::TY_LLVM_BC)
3366       Suffixed += ".tmp";
3367     Suffixed += '.';
3368     Suffixed += Suffix;
3369     NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
3370   }
3371 
3372   // Prepend object file path if -save-temps=obj
3373   if (!AtTopLevel && isSaveTempsObj() && C.getArgs().hasArg(options::OPT_o) &&
3374       JA.getType() != types::TY_PCH) {
3375     Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
3376     SmallString<128> TempPath(FinalOutput->getValue());
3377     llvm::sys::path::remove_filename(TempPath);
3378     StringRef OutputFileName = llvm::sys::path::filename(NamedOutput);
3379     llvm::sys::path::append(TempPath, OutputFileName);
3380     NamedOutput = C.getArgs().MakeArgString(TempPath.c_str());
3381   }
3382 
3383   // If we're saving temps and the temp file conflicts with the input file,
3384   // then avoid overwriting input file.
3385   if (!AtTopLevel && isSaveTempsEnabled() && NamedOutput == BaseName) {
3386     bool SameFile = false;
3387     SmallString<256> Result;
3388     llvm::sys::fs::current_path(Result);
3389     llvm::sys::path::append(Result, BaseName);
3390     llvm::sys::fs::equivalent(BaseInput, Result.c_str(), SameFile);
3391     // Must share the same path to conflict.
3392     if (SameFile) {
3393       StringRef Name = llvm::sys::path::filename(BaseInput);
3394       std::pair<StringRef, StringRef> Split = Name.split('.');
3395       std::string TmpName = GetTemporaryPath(
3396           Split.first, types::getTypeTempSuffix(JA.getType(), IsCLMode()));
3397       return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
3398     }
3399   }
3400 
3401   // As an annoying special case, PCH generation doesn't strip the pathname.
3402   if (JA.getType() == types::TY_PCH && !IsCLMode()) {
3403     llvm::sys::path::remove_filename(BasePath);
3404     if (BasePath.empty())
3405       BasePath = NamedOutput;
3406     else
3407       llvm::sys::path::append(BasePath, NamedOutput);
3408     return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()), &JA);
3409   } else {
3410     return C.addResultFile(NamedOutput, &JA);
3411   }
3412 }
3413 
3414 std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const {
3415   // Respect a limited subset of the '-Bprefix' functionality in GCC by
3416   // attempting to use this prefix when looking for file paths.
3417   for (const std::string &Dir : PrefixDirs) {
3418     if (Dir.empty())
3419       continue;
3420     SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir);
3421     llvm::sys::path::append(P, Name);
3422     if (llvm::sys::fs::exists(Twine(P)))
3423       return P.str();
3424   }
3425 
3426   SmallString<128> P(ResourceDir);
3427   llvm::sys::path::append(P, Name);
3428   if (llvm::sys::fs::exists(Twine(P)))
3429     return P.str();
3430 
3431   for (const std::string &Dir : TC.getFilePaths()) {
3432     if (Dir.empty())
3433       continue;
3434     SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir);
3435     llvm::sys::path::append(P, Name);
3436     if (llvm::sys::fs::exists(Twine(P)))
3437       return P.str();
3438   }
3439 
3440   return Name;
3441 }
3442 
3443 void Driver::generatePrefixedToolNames(
3444     StringRef Tool, const ToolChain &TC,
3445     SmallVectorImpl<std::string> &Names) const {
3446   // FIXME: Needs a better variable than DefaultTargetTriple
3447   Names.emplace_back((DefaultTargetTriple + "-" + Tool).str());
3448   Names.emplace_back(Tool);
3449 
3450   // Allow the discovery of tools prefixed with LLVM's default target triple.
3451   std::string LLVMDefaultTargetTriple = llvm::sys::getDefaultTargetTriple();
3452   if (LLVMDefaultTargetTriple != DefaultTargetTriple)
3453     Names.emplace_back((LLVMDefaultTargetTriple + "-" + Tool).str());
3454 }
3455 
3456 static bool ScanDirForExecutable(SmallString<128> &Dir,
3457                                  ArrayRef<std::string> Names) {
3458   for (const auto &Name : Names) {
3459     llvm::sys::path::append(Dir, Name);
3460     if (llvm::sys::fs::can_execute(Twine(Dir)))
3461       return true;
3462     llvm::sys::path::remove_filename(Dir);
3463   }
3464   return false;
3465 }
3466 
3467 std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
3468   SmallVector<std::string, 2> TargetSpecificExecutables;
3469   generatePrefixedToolNames(Name, TC, TargetSpecificExecutables);
3470 
3471   // Respect a limited subset of the '-Bprefix' functionality in GCC by
3472   // attempting to use this prefix when looking for program paths.
3473   for (const auto &PrefixDir : PrefixDirs) {
3474     if (llvm::sys::fs::is_directory(PrefixDir)) {
3475       SmallString<128> P(PrefixDir);
3476       if (ScanDirForExecutable(P, TargetSpecificExecutables))
3477         return P.str();
3478     } else {
3479       SmallString<128> P((PrefixDir + Name).str());
3480       if (llvm::sys::fs::can_execute(Twine(P)))
3481         return P.str();
3482     }
3483   }
3484 
3485   const ToolChain::path_list &List = TC.getProgramPaths();
3486   for (const auto &Path : List) {
3487     SmallString<128> P(Path);
3488     if (ScanDirForExecutable(P, TargetSpecificExecutables))
3489       return P.str();
3490   }
3491 
3492   // If all else failed, search the path.
3493   for (const auto &TargetSpecificExecutable : TargetSpecificExecutables)
3494     if (llvm::ErrorOr<std::string> P =
3495             llvm::sys::findProgramByName(TargetSpecificExecutable))
3496       return *P;
3497 
3498   return Name;
3499 }
3500 
3501 std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const {
3502   SmallString<128> Path;
3503   std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, Path);
3504   if (EC) {
3505     Diag(clang::diag::err_unable_to_make_temp) << EC.message();
3506     return "";
3507   }
3508 
3509   return Path.str();
3510 }
3511 
3512 std::string Driver::GetClPchPath(Compilation &C, StringRef BaseName) const {
3513   SmallString<128> Output;
3514   if (Arg *FpArg = C.getArgs().getLastArg(options::OPT__SLASH_Fp)) {
3515     // FIXME: If anybody needs it, implement this obscure rule:
3516     // "If you specify a directory without a file name, the default file name
3517     // is VCx0.pch., where x is the major version of Visual C++ in use."
3518     Output = FpArg->getValue();
3519 
3520     // "If you do not specify an extension as part of the path name, an
3521     // extension of .pch is assumed. "
3522     if (!llvm::sys::path::has_extension(Output))
3523       Output += ".pch";
3524   } else {
3525     Output = BaseName;
3526     llvm::sys::path::replace_extension(Output, ".pch");
3527   }
3528   return Output.str();
3529 }
3530 
3531 const ToolChain &Driver::getToolChain(const ArgList &Args,
3532                                       const llvm::Triple &Target) const {
3533 
3534   ToolChain *&TC = ToolChains[Target.str()];
3535   if (!TC) {
3536     switch (Target.getOS()) {
3537     case llvm::Triple::Haiku:
3538       TC = new toolchains::Haiku(*this, Target, Args);
3539       break;
3540     case llvm::Triple::CloudABI:
3541       TC = new toolchains::CloudABI(*this, Target, Args);
3542       break;
3543     case llvm::Triple::Darwin:
3544     case llvm::Triple::MacOSX:
3545     case llvm::Triple::IOS:
3546     case llvm::Triple::TvOS:
3547     case llvm::Triple::WatchOS:
3548       TC = new toolchains::DarwinClang(*this, Target, Args);
3549       break;
3550     case llvm::Triple::DragonFly:
3551       TC = new toolchains::DragonFly(*this, Target, Args);
3552       break;
3553     case llvm::Triple::OpenBSD:
3554       TC = new toolchains::OpenBSD(*this, Target, Args);
3555       break;
3556     case llvm::Triple::Bitrig:
3557       TC = new toolchains::Bitrig(*this, Target, Args);
3558       break;
3559     case llvm::Triple::NetBSD:
3560       TC = new toolchains::NetBSD(*this, Target, Args);
3561       break;
3562     case llvm::Triple::FreeBSD:
3563       TC = new toolchains::FreeBSD(*this, Target, Args);
3564       break;
3565     case llvm::Triple::Minix:
3566       TC = new toolchains::Minix(*this, Target, Args);
3567       break;
3568     case llvm::Triple::Linux:
3569     case llvm::Triple::ELFIAMCU:
3570       if (Target.getArch() == llvm::Triple::hexagon)
3571         TC = new toolchains::HexagonToolChain(*this, Target, Args);
3572       else if ((Target.getVendor() == llvm::Triple::MipsTechnologies) &&
3573                !Target.hasEnvironment())
3574         TC = new toolchains::MipsLLVMToolChain(*this, Target, Args);
3575       else
3576         TC = new toolchains::Linux(*this, Target, Args);
3577       break;
3578     case llvm::Triple::NaCl:
3579       TC = new toolchains::NaClToolChain(*this, Target, Args);
3580       break;
3581     case llvm::Triple::Fuchsia:
3582       TC = new toolchains::Fuchsia(*this, Target, Args);
3583       break;
3584     case llvm::Triple::Solaris:
3585       TC = new toolchains::Solaris(*this, Target, Args);
3586       break;
3587     case llvm::Triple::AMDHSA:
3588       TC = new toolchains::AMDGPUToolChain(*this, Target, Args);
3589       break;
3590     case llvm::Triple::Win32:
3591       switch (Target.getEnvironment()) {
3592       default:
3593         if (Target.isOSBinFormatELF())
3594           TC = new toolchains::Generic_ELF(*this, Target, Args);
3595         else if (Target.isOSBinFormatMachO())
3596           TC = new toolchains::MachO(*this, Target, Args);
3597         else
3598           TC = new toolchains::Generic_GCC(*this, Target, Args);
3599         break;
3600       case llvm::Triple::GNU:
3601         TC = new toolchains::MinGW(*this, Target, Args);
3602         break;
3603       case llvm::Triple::Itanium:
3604         TC = new toolchains::CrossWindowsToolChain(*this, Target, Args);
3605         break;
3606       case llvm::Triple::MSVC:
3607       case llvm::Triple::UnknownEnvironment:
3608         TC = new toolchains::MSVCToolChain(*this, Target, Args);
3609         break;
3610       }
3611       break;
3612     case llvm::Triple::CUDA:
3613       TC = new toolchains::CudaToolChain(*this, Target, Args);
3614       break;
3615     case llvm::Triple::PS4:
3616       TC = new toolchains::PS4CPU(*this, Target, Args);
3617       break;
3618     case llvm::Triple::Contiki:
3619       TC = new toolchains::Contiki(*this, Target, Args);
3620       break;
3621     default:
3622       // Of these targets, Hexagon is the only one that might have
3623       // an OS of Linux, in which case it got handled above already.
3624       switch (Target.getArch()) {
3625       case llvm::Triple::tce:
3626         TC = new toolchains::TCEToolChain(*this, Target, Args);
3627         break;
3628       case llvm::Triple::hexagon:
3629         TC = new toolchains::HexagonToolChain(*this, Target, Args);
3630         break;
3631       case llvm::Triple::lanai:
3632         TC = new toolchains::LanaiToolChain(*this, Target, Args);
3633         break;
3634       case llvm::Triple::xcore:
3635         TC = new toolchains::XCoreToolChain(*this, Target, Args);
3636         break;
3637       case llvm::Triple::wasm32:
3638       case llvm::Triple::wasm64:
3639         TC = new toolchains::WebAssembly(*this, Target, Args);
3640         break;
3641       default:
3642         if (Target.getVendor() == llvm::Triple::Myriad)
3643           TC = new toolchains::MyriadToolChain(*this, Target, Args);
3644         else if (Target.isOSBinFormatELF())
3645           TC = new toolchains::Generic_ELF(*this, Target, Args);
3646         else if (Target.isOSBinFormatMachO())
3647           TC = new toolchains::MachO(*this, Target, Args);
3648         else
3649           TC = new toolchains::Generic_GCC(*this, Target, Args);
3650       }
3651     }
3652   }
3653   return *TC;
3654 }
3655 
3656 bool Driver::ShouldUseClangCompiler(const JobAction &JA) const {
3657   // Say "no" if there is not exactly one input of a type clang understands.
3658   if (JA.size() != 1 ||
3659       !types::isAcceptedByClang((*JA.input_begin())->getType()))
3660     return false;
3661 
3662   // And say "no" if this is not a kind of action clang understands.
3663   if (!isa<PreprocessJobAction>(JA) && !isa<PrecompileJobAction>(JA) &&
3664       !isa<CompileJobAction>(JA) && !isa<BackendJobAction>(JA))
3665     return false;
3666 
3667   return true;
3668 }
3669 
3670 /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
3671 /// grouped values as integers. Numbers which are not provided are set to 0.
3672 ///
3673 /// \return True if the entire string was parsed (9.2), or all groups were
3674 /// parsed (10.3.5extrastuff).
3675 bool Driver::GetReleaseVersion(StringRef Str, unsigned &Major, unsigned &Minor,
3676                                unsigned &Micro, bool &HadExtra) {
3677   HadExtra = false;
3678 
3679   Major = Minor = Micro = 0;
3680   if (Str.empty())
3681     return false;
3682 
3683   if (Str.consumeInteger(10, Major))
3684     return false;
3685   if (Str.empty())
3686     return true;
3687   if (Str[0] != '.')
3688     return false;
3689 
3690   Str = Str.drop_front(1);
3691 
3692   if (Str.consumeInteger(10, Minor))
3693     return false;
3694   if (Str.empty())
3695     return true;
3696   if (Str[0] != '.')
3697     return false;
3698   Str = Str.drop_front(1);
3699 
3700   if (Str.consumeInteger(10, Micro))
3701     return false;
3702   if (!Str.empty())
3703     HadExtra = true;
3704   return true;
3705 }
3706 
3707 /// Parse digits from a string \p Str and fulfill \p Digits with
3708 /// the parsed numbers. This method assumes that the max number of
3709 /// digits to look for is equal to Digits.size().
3710 ///
3711 /// \return True if the entire string was parsed and there are
3712 /// no extra characters remaining at the end.
3713 bool Driver::GetReleaseVersion(StringRef Str,
3714                                MutableArrayRef<unsigned> Digits) {
3715   if (Str.empty())
3716     return false;
3717 
3718   unsigned CurDigit = 0;
3719   while (CurDigit < Digits.size()) {
3720     unsigned Digit;
3721     if (Str.consumeInteger(10, Digit))
3722       return false;
3723     Digits[CurDigit] = Digit;
3724     if (Str.empty())
3725       return true;
3726     if (Str[0] != '.')
3727       return false;
3728     Str = Str.drop_front(1);
3729     CurDigit++;
3730   }
3731 
3732   // More digits than requested, bail out...
3733   return false;
3734 }
3735 
3736 std::pair<unsigned, unsigned> Driver::getIncludeExcludeOptionFlagMasks() const {
3737   unsigned IncludedFlagsBitmask = 0;
3738   unsigned ExcludedFlagsBitmask = options::NoDriverOption;
3739 
3740   if (Mode == CLMode) {
3741     // Include CL and Core options.
3742     IncludedFlagsBitmask |= options::CLOption;
3743     IncludedFlagsBitmask |= options::CoreOption;
3744   } else {
3745     ExcludedFlagsBitmask |= options::CLOption;
3746   }
3747 
3748   return std::make_pair(IncludedFlagsBitmask, ExcludedFlagsBitmask);
3749 }
3750 
3751 bool clang::driver::isOptimizationLevelFast(const ArgList &Args) {
3752   return Args.hasFlag(options::OPT_Ofast, options::OPT_O_Group, false);
3753 }
3754