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