xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/Clang.cpp (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
10b57cec5SDimitry Andric //===-- Clang.cpp - Clang+LLVM ToolChain Implementations --------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "Clang.h"
100b57cec5SDimitry Andric #include "Arch/AArch64.h"
110b57cec5SDimitry Andric #include "Arch/ARM.h"
120b57cec5SDimitry Andric #include "Arch/Mips.h"
130b57cec5SDimitry Andric #include "Arch/PPC.h"
140b57cec5SDimitry Andric #include "Arch/RISCV.h"
150b57cec5SDimitry Andric #include "Arch/Sparc.h"
160b57cec5SDimitry Andric #include "Arch/SystemZ.h"
170b57cec5SDimitry Andric #include "Arch/X86.h"
180b57cec5SDimitry Andric #include "AMDGPU.h"
190b57cec5SDimitry Andric #include "CommonArgs.h"
200b57cec5SDimitry Andric #include "Hexagon.h"
210b57cec5SDimitry Andric #include "MSP430.h"
220b57cec5SDimitry Andric #include "InputInfo.h"
230b57cec5SDimitry Andric #include "PS4CPU.h"
240b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h"
25a7dea167SDimitry Andric #include "clang/Basic/CodeGenOptions.h"
260b57cec5SDimitry Andric #include "clang/Basic/LangOptions.h"
270b57cec5SDimitry Andric #include "clang/Basic/ObjCRuntime.h"
280b57cec5SDimitry Andric #include "clang/Basic/Version.h"
290b57cec5SDimitry Andric #include "clang/Driver/Distro.h"
300b57cec5SDimitry Andric #include "clang/Driver/DriverDiagnostic.h"
310b57cec5SDimitry Andric #include "clang/Driver/Options.h"
320b57cec5SDimitry Andric #include "clang/Driver/SanitizerArgs.h"
330b57cec5SDimitry Andric #include "clang/Driver/XRayArgs.h"
340b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
350b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
360b57cec5SDimitry Andric #include "llvm/Option/ArgList.h"
370b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
380b57cec5SDimitry Andric #include "llvm/Support/Compression.h"
390b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
400b57cec5SDimitry Andric #include "llvm/Support/Path.h"
410b57cec5SDimitry Andric #include "llvm/Support/Process.h"
420b57cec5SDimitry Andric #include "llvm/Support/TargetParser.h"
430b57cec5SDimitry Andric #include "llvm/Support/YAMLParser.h"
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric #ifdef LLVM_ON_UNIX
460b57cec5SDimitry Andric #include <unistd.h> // For getuid().
470b57cec5SDimitry Andric #endif
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric using namespace clang::driver;
500b57cec5SDimitry Andric using namespace clang::driver::tools;
510b57cec5SDimitry Andric using namespace clang;
520b57cec5SDimitry Andric using namespace llvm::opt;
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
550b57cec5SDimitry Andric   if (Arg *A =
560b57cec5SDimitry Andric           Args.getLastArg(clang::driver::options::OPT_C, options::OPT_CC)) {
570b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_P) &&
580b57cec5SDimitry Andric         !Args.hasArg(options::OPT__SLASH_EP) && !D.CCCIsCPP()) {
590b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_argument_only_allowed_with)
600b57cec5SDimitry Andric           << A->getBaseArg().getAsString(Args)
610b57cec5SDimitry Andric           << (D.IsCLMode() ? "/E, /P or /EP" : "-E");
620b57cec5SDimitry Andric     }
630b57cec5SDimitry Andric   }
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
670b57cec5SDimitry Andric   // In gcc, only ARM checks this, but it seems reasonable to check universally.
680b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_static))
690b57cec5SDimitry Andric     if (const Arg *A =
700b57cec5SDimitry Andric             Args.getLastArg(options::OPT_dynamic, options::OPT_mdynamic_no_pic))
710b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args)
720b57cec5SDimitry Andric                                                       << "-static";
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric // Add backslashes to escape spaces and other backslashes.
760b57cec5SDimitry Andric // This is used for the space-separated argument list specified with
770b57cec5SDimitry Andric // the -dwarf-debug-flags option.
780b57cec5SDimitry Andric static void EscapeSpacesAndBackslashes(const char *Arg,
790b57cec5SDimitry Andric                                        SmallVectorImpl<char> &Res) {
800b57cec5SDimitry Andric   for (; *Arg; ++Arg) {
810b57cec5SDimitry Andric     switch (*Arg) {
820b57cec5SDimitry Andric     default:
830b57cec5SDimitry Andric       break;
840b57cec5SDimitry Andric     case ' ':
850b57cec5SDimitry Andric     case '\\':
860b57cec5SDimitry Andric       Res.push_back('\\');
870b57cec5SDimitry Andric       break;
880b57cec5SDimitry Andric     }
890b57cec5SDimitry Andric     Res.push_back(*Arg);
900b57cec5SDimitry Andric   }
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric // Quote target names for inclusion in GNU Make dependency files.
940b57cec5SDimitry Andric // Only the characters '$', '#', ' ', '\t' are quoted.
950b57cec5SDimitry Andric static void QuoteTarget(StringRef Target, SmallVectorImpl<char> &Res) {
960b57cec5SDimitry Andric   for (unsigned i = 0, e = Target.size(); i != e; ++i) {
970b57cec5SDimitry Andric     switch (Target[i]) {
980b57cec5SDimitry Andric     case ' ':
990b57cec5SDimitry Andric     case '\t':
1000b57cec5SDimitry Andric       // Escape the preceding backslashes
1010b57cec5SDimitry Andric       for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
1020b57cec5SDimitry Andric         Res.push_back('\\');
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric       // Escape the space/tab
1050b57cec5SDimitry Andric       Res.push_back('\\');
1060b57cec5SDimitry Andric       break;
1070b57cec5SDimitry Andric     case '$':
1080b57cec5SDimitry Andric       Res.push_back('$');
1090b57cec5SDimitry Andric       break;
1100b57cec5SDimitry Andric     case '#':
1110b57cec5SDimitry Andric       Res.push_back('\\');
1120b57cec5SDimitry Andric       break;
1130b57cec5SDimitry Andric     default:
1140b57cec5SDimitry Andric       break;
1150b57cec5SDimitry Andric     }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric     Res.push_back(Target[i]);
1180b57cec5SDimitry Andric   }
1190b57cec5SDimitry Andric }
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric /// Apply \a Work on the current tool chain \a RegularToolChain and any other
1220b57cec5SDimitry Andric /// offloading tool chain that is associated with the current action \a JA.
1230b57cec5SDimitry Andric static void
1240b57cec5SDimitry Andric forAllAssociatedToolChains(Compilation &C, const JobAction &JA,
1250b57cec5SDimitry Andric                            const ToolChain &RegularToolChain,
1260b57cec5SDimitry Andric                            llvm::function_ref<void(const ToolChain &)> Work) {
1270b57cec5SDimitry Andric   // Apply Work on the current/regular tool chain.
1280b57cec5SDimitry Andric   Work(RegularToolChain);
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   // Apply Work on all the offloading tool chains associated with the current
1310b57cec5SDimitry Andric   // action.
1320b57cec5SDimitry Andric   if (JA.isHostOffloading(Action::OFK_Cuda))
1330b57cec5SDimitry Andric     Work(*C.getSingleOffloadToolChain<Action::OFK_Cuda>());
1340b57cec5SDimitry Andric   else if (JA.isDeviceOffloading(Action::OFK_Cuda))
1350b57cec5SDimitry Andric     Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
1360b57cec5SDimitry Andric   else if (JA.isHostOffloading(Action::OFK_HIP))
1370b57cec5SDimitry Andric     Work(*C.getSingleOffloadToolChain<Action::OFK_HIP>());
1380b57cec5SDimitry Andric   else if (JA.isDeviceOffloading(Action::OFK_HIP))
1390b57cec5SDimitry Andric     Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   if (JA.isHostOffloading(Action::OFK_OpenMP)) {
1420b57cec5SDimitry Andric     auto TCs = C.getOffloadToolChains<Action::OFK_OpenMP>();
1430b57cec5SDimitry Andric     for (auto II = TCs.first, IE = TCs.second; II != IE; ++II)
1440b57cec5SDimitry Andric       Work(*II->second);
1450b57cec5SDimitry Andric   } else if (JA.isDeviceOffloading(Action::OFK_OpenMP))
1460b57cec5SDimitry Andric     Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   //
1490b57cec5SDimitry Andric   // TODO: Add support for other offloading programming models here.
1500b57cec5SDimitry Andric   //
1510b57cec5SDimitry Andric }
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric /// This is a helper function for validating the optional refinement step
1540b57cec5SDimitry Andric /// parameter in reciprocal argument strings. Return false if there is an error
1550b57cec5SDimitry Andric /// parsing the refinement step. Otherwise, return true and set the Position
1560b57cec5SDimitry Andric /// of the refinement step in the input string.
1570b57cec5SDimitry Andric static bool getRefinementStep(StringRef In, const Driver &D,
1580b57cec5SDimitry Andric                               const Arg &A, size_t &Position) {
1590b57cec5SDimitry Andric   const char RefinementStepToken = ':';
1600b57cec5SDimitry Andric   Position = In.find(RefinementStepToken);
1610b57cec5SDimitry Andric   if (Position != StringRef::npos) {
1620b57cec5SDimitry Andric     StringRef Option = A.getOption().getName();
1630b57cec5SDimitry Andric     StringRef RefStep = In.substr(Position + 1);
1640b57cec5SDimitry Andric     // Allow exactly one numeric character for the additional refinement
1650b57cec5SDimitry Andric     // step parameter. This is reasonable for all currently-supported
1660b57cec5SDimitry Andric     // operations and architectures because we would expect that a larger value
1670b57cec5SDimitry Andric     // of refinement steps would cause the estimate "optimization" to
1680b57cec5SDimitry Andric     // under-perform the native operation. Also, if the estimate does not
1690b57cec5SDimitry Andric     // converge quickly, it probably will not ever converge, so further
1700b57cec5SDimitry Andric     // refinement steps will not produce a better answer.
1710b57cec5SDimitry Andric     if (RefStep.size() != 1) {
1720b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << Option << RefStep;
1730b57cec5SDimitry Andric       return false;
1740b57cec5SDimitry Andric     }
1750b57cec5SDimitry Andric     char RefStepChar = RefStep[0];
1760b57cec5SDimitry Andric     if (RefStepChar < '0' || RefStepChar > '9') {
1770b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << Option << RefStep;
1780b57cec5SDimitry Andric       return false;
1790b57cec5SDimitry Andric     }
1800b57cec5SDimitry Andric   }
1810b57cec5SDimitry Andric   return true;
1820b57cec5SDimitry Andric }
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric /// The -mrecip flag requires processing of many optional parameters.
1850b57cec5SDimitry Andric static void ParseMRecip(const Driver &D, const ArgList &Args,
1860b57cec5SDimitry Andric                         ArgStringList &OutStrings) {
1870b57cec5SDimitry Andric   StringRef DisabledPrefixIn = "!";
1880b57cec5SDimitry Andric   StringRef DisabledPrefixOut = "!";
1890b57cec5SDimitry Andric   StringRef EnabledPrefixOut = "";
1900b57cec5SDimitry Andric   StringRef Out = "-mrecip=";
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   Arg *A = Args.getLastArg(options::OPT_mrecip, options::OPT_mrecip_EQ);
1930b57cec5SDimitry Andric   if (!A)
1940b57cec5SDimitry Andric     return;
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric   unsigned NumOptions = A->getNumValues();
1970b57cec5SDimitry Andric   if (NumOptions == 0) {
1980b57cec5SDimitry Andric     // No option is the same as "all".
1990b57cec5SDimitry Andric     OutStrings.push_back(Args.MakeArgString(Out + "all"));
2000b57cec5SDimitry Andric     return;
2010b57cec5SDimitry Andric   }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric   // Pass through "all", "none", or "default" with an optional refinement step.
2040b57cec5SDimitry Andric   if (NumOptions == 1) {
2050b57cec5SDimitry Andric     StringRef Val = A->getValue(0);
2060b57cec5SDimitry Andric     size_t RefStepLoc;
2070b57cec5SDimitry Andric     if (!getRefinementStep(Val, D, *A, RefStepLoc))
2080b57cec5SDimitry Andric       return;
2090b57cec5SDimitry Andric     StringRef ValBase = Val.slice(0, RefStepLoc);
2100b57cec5SDimitry Andric     if (ValBase == "all" || ValBase == "none" || ValBase == "default") {
2110b57cec5SDimitry Andric       OutStrings.push_back(Args.MakeArgString(Out + Val));
2120b57cec5SDimitry Andric       return;
2130b57cec5SDimitry Andric     }
2140b57cec5SDimitry Andric   }
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric   // Each reciprocal type may be enabled or disabled individually.
2170b57cec5SDimitry Andric   // Check each input value for validity, concatenate them all back together,
2180b57cec5SDimitry Andric   // and pass through.
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   llvm::StringMap<bool> OptionStrings;
2210b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("divd", false));
2220b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("divf", false));
2230b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("vec-divd", false));
2240b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("vec-divf", false));
2250b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("sqrtd", false));
2260b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("sqrtf", false));
2270b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("vec-sqrtd", false));
2280b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("vec-sqrtf", false));
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric   for (unsigned i = 0; i != NumOptions; ++i) {
2310b57cec5SDimitry Andric     StringRef Val = A->getValue(i);
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric     bool IsDisabled = Val.startswith(DisabledPrefixIn);
2340b57cec5SDimitry Andric     // Ignore the disablement token for string matching.
2350b57cec5SDimitry Andric     if (IsDisabled)
2360b57cec5SDimitry Andric       Val = Val.substr(1);
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric     size_t RefStep;
2390b57cec5SDimitry Andric     if (!getRefinementStep(Val, D, *A, RefStep))
2400b57cec5SDimitry Andric       return;
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric     StringRef ValBase = Val.slice(0, RefStep);
2430b57cec5SDimitry Andric     llvm::StringMap<bool>::iterator OptionIter = OptionStrings.find(ValBase);
2440b57cec5SDimitry Andric     if (OptionIter == OptionStrings.end()) {
2450b57cec5SDimitry Andric       // Try again specifying float suffix.
2460b57cec5SDimitry Andric       OptionIter = OptionStrings.find(ValBase.str() + 'f');
2470b57cec5SDimitry Andric       if (OptionIter == OptionStrings.end()) {
2480b57cec5SDimitry Andric         // The input name did not match any known option string.
2490b57cec5SDimitry Andric         D.Diag(diag::err_drv_unknown_argument) << Val;
2500b57cec5SDimitry Andric         return;
2510b57cec5SDimitry Andric       }
2520b57cec5SDimitry Andric       // The option was specified without a float or double suffix.
2530b57cec5SDimitry Andric       // Make sure that the double entry was not already specified.
2540b57cec5SDimitry Andric       // The float entry will be checked below.
2550b57cec5SDimitry Andric       if (OptionStrings[ValBase.str() + 'd']) {
2560b57cec5SDimitry Andric         D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val;
2570b57cec5SDimitry Andric         return;
2580b57cec5SDimitry Andric       }
2590b57cec5SDimitry Andric     }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric     if (OptionIter->second == true) {
2620b57cec5SDimitry Andric       // Duplicate option specified.
2630b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val;
2640b57cec5SDimitry Andric       return;
2650b57cec5SDimitry Andric     }
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric     // Mark the matched option as found. Do not allow duplicate specifiers.
2680b57cec5SDimitry Andric     OptionIter->second = true;
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric     // If the precision was not specified, also mark the double entry as found.
2710b57cec5SDimitry Andric     if (ValBase.back() != 'f' && ValBase.back() != 'd')
2720b57cec5SDimitry Andric       OptionStrings[ValBase.str() + 'd'] = true;
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric     // Build the output string.
2750b57cec5SDimitry Andric     StringRef Prefix = IsDisabled ? DisabledPrefixOut : EnabledPrefixOut;
2760b57cec5SDimitry Andric     Out = Args.MakeArgString(Out + Prefix + Val);
2770b57cec5SDimitry Andric     if (i != NumOptions - 1)
2780b57cec5SDimitry Andric       Out = Args.MakeArgString(Out + ",");
2790b57cec5SDimitry Andric   }
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric   OutStrings.push_back(Args.MakeArgString(Out));
2820b57cec5SDimitry Andric }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric /// The -mprefer-vector-width option accepts either a positive integer
2850b57cec5SDimitry Andric /// or the string "none".
2860b57cec5SDimitry Andric static void ParseMPreferVectorWidth(const Driver &D, const ArgList &Args,
2870b57cec5SDimitry Andric                                     ArgStringList &CmdArgs) {
2880b57cec5SDimitry Andric   Arg *A = Args.getLastArg(options::OPT_mprefer_vector_width_EQ);
2890b57cec5SDimitry Andric   if (!A)
2900b57cec5SDimitry Andric     return;
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric   StringRef Value = A->getValue();
2930b57cec5SDimitry Andric   if (Value == "none") {
2940b57cec5SDimitry Andric     CmdArgs.push_back("-mprefer-vector-width=none");
2950b57cec5SDimitry Andric   } else {
2960b57cec5SDimitry Andric     unsigned Width;
2970b57cec5SDimitry Andric     if (Value.getAsInteger(10, Width)) {
2980b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value;
2990b57cec5SDimitry Andric       return;
3000b57cec5SDimitry Andric     }
3010b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-mprefer-vector-width=" + Value));
3020b57cec5SDimitry Andric   }
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric 
305*480093f4SDimitry Andric static void getWebAssemblyTargetFeatures(const ArgList &Args,
306*480093f4SDimitry Andric                                          std::vector<StringRef> &Features) {
307*480093f4SDimitry Andric   handleTargetFeaturesGroup(Args, Features, options::OPT_m_wasm_Features_Group);
308*480093f4SDimitry Andric }
309*480093f4SDimitry Andric 
310*480093f4SDimitry Andric static void getTargetFeatures(const ToolChain &TC, const llvm::Triple &Triple,
311*480093f4SDimitry Andric                               const ArgList &Args, ArgStringList &CmdArgs,
312*480093f4SDimitry Andric                               bool ForAS) {
313*480093f4SDimitry Andric   const Driver &D = TC.getDriver();
314*480093f4SDimitry Andric   std::vector<StringRef> Features;
315*480093f4SDimitry Andric   switch (Triple.getArch()) {
316*480093f4SDimitry Andric   default:
317*480093f4SDimitry Andric     break;
318*480093f4SDimitry Andric   case llvm::Triple::mips:
319*480093f4SDimitry Andric   case llvm::Triple::mipsel:
320*480093f4SDimitry Andric   case llvm::Triple::mips64:
321*480093f4SDimitry Andric   case llvm::Triple::mips64el:
322*480093f4SDimitry Andric     mips::getMIPSTargetFeatures(D, Triple, Args, Features);
323*480093f4SDimitry Andric     break;
324*480093f4SDimitry Andric 
325*480093f4SDimitry Andric   case llvm::Triple::arm:
326*480093f4SDimitry Andric   case llvm::Triple::armeb:
327*480093f4SDimitry Andric   case llvm::Triple::thumb:
328*480093f4SDimitry Andric   case llvm::Triple::thumbeb:
329*480093f4SDimitry Andric     arm::getARMTargetFeatures(TC, Triple, Args, CmdArgs, Features, ForAS);
330*480093f4SDimitry Andric     break;
331*480093f4SDimitry Andric 
332*480093f4SDimitry Andric   case llvm::Triple::ppc:
333*480093f4SDimitry Andric   case llvm::Triple::ppc64:
334*480093f4SDimitry Andric   case llvm::Triple::ppc64le:
335*480093f4SDimitry Andric     ppc::getPPCTargetFeatures(D, Triple, Args, Features);
336*480093f4SDimitry Andric     break;
337*480093f4SDimitry Andric   case llvm::Triple::riscv32:
338*480093f4SDimitry Andric   case llvm::Triple::riscv64:
339*480093f4SDimitry Andric     riscv::getRISCVTargetFeatures(D, Triple, Args, Features);
340*480093f4SDimitry Andric     break;
341*480093f4SDimitry Andric   case llvm::Triple::systemz:
342*480093f4SDimitry Andric     systemz::getSystemZTargetFeatures(Args, Features);
343*480093f4SDimitry Andric     break;
344*480093f4SDimitry Andric   case llvm::Triple::aarch64:
345*480093f4SDimitry Andric   case llvm::Triple::aarch64_32:
346*480093f4SDimitry Andric   case llvm::Triple::aarch64_be:
347*480093f4SDimitry Andric     aarch64::getAArch64TargetFeatures(D, Triple, Args, Features);
348*480093f4SDimitry Andric     break;
349*480093f4SDimitry Andric   case llvm::Triple::x86:
350*480093f4SDimitry Andric   case llvm::Triple::x86_64:
351*480093f4SDimitry Andric     x86::getX86TargetFeatures(D, Triple, Args, Features);
352*480093f4SDimitry Andric     break;
353*480093f4SDimitry Andric   case llvm::Triple::hexagon:
354*480093f4SDimitry Andric     hexagon::getHexagonTargetFeatures(D, Args, Features);
355*480093f4SDimitry Andric     break;
356*480093f4SDimitry Andric   case llvm::Triple::wasm32:
357*480093f4SDimitry Andric   case llvm::Triple::wasm64:
358*480093f4SDimitry Andric     getWebAssemblyTargetFeatures(Args, Features);
359*480093f4SDimitry Andric     break;
360*480093f4SDimitry Andric   case llvm::Triple::sparc:
361*480093f4SDimitry Andric   case llvm::Triple::sparcel:
362*480093f4SDimitry Andric   case llvm::Triple::sparcv9:
363*480093f4SDimitry Andric     sparc::getSparcTargetFeatures(D, Args, Features);
364*480093f4SDimitry Andric     break;
365*480093f4SDimitry Andric   case llvm::Triple::r600:
366*480093f4SDimitry Andric   case llvm::Triple::amdgcn:
367*480093f4SDimitry Andric     amdgpu::getAMDGPUTargetFeatures(D, Args, Features);
368*480093f4SDimitry Andric     break;
369*480093f4SDimitry Andric   case llvm::Triple::msp430:
370*480093f4SDimitry Andric     msp430::getMSP430TargetFeatures(D, Args, Features);
371*480093f4SDimitry Andric   }
372*480093f4SDimitry Andric 
373*480093f4SDimitry Andric   // Find the last of each feature.
374*480093f4SDimitry Andric   llvm::StringMap<unsigned> LastOpt;
375*480093f4SDimitry Andric   for (unsigned I = 0, N = Features.size(); I < N; ++I) {
376*480093f4SDimitry Andric     StringRef Name = Features[I];
377*480093f4SDimitry Andric     assert(Name[0] == '-' || Name[0] == '+');
378*480093f4SDimitry Andric     LastOpt[Name.drop_front(1)] = I;
379*480093f4SDimitry Andric   }
380*480093f4SDimitry Andric 
381*480093f4SDimitry Andric   for (unsigned I = 0, N = Features.size(); I < N; ++I) {
382*480093f4SDimitry Andric     // If this feature was overridden, ignore it.
383*480093f4SDimitry Andric     StringRef Name = Features[I];
384*480093f4SDimitry Andric     llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name.drop_front(1));
385*480093f4SDimitry Andric     assert(LastI != LastOpt.end());
386*480093f4SDimitry Andric     unsigned Last = LastI->second;
387*480093f4SDimitry Andric     if (Last != I)
388*480093f4SDimitry Andric       continue;
389*480093f4SDimitry Andric 
390*480093f4SDimitry Andric     CmdArgs.push_back("-target-feature");
391*480093f4SDimitry Andric     CmdArgs.push_back(Name.data());
392*480093f4SDimitry Andric   }
393*480093f4SDimitry Andric }
394*480093f4SDimitry Andric 
3950b57cec5SDimitry Andric static bool
3960b57cec5SDimitry Andric shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
3970b57cec5SDimitry Andric                                           const llvm::Triple &Triple) {
3980b57cec5SDimitry Andric   // We use the zero-cost exception tables for Objective-C if the non-fragile
3990b57cec5SDimitry Andric   // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
4000b57cec5SDimitry Andric   // later.
4010b57cec5SDimitry Andric   if (runtime.isNonFragile())
4020b57cec5SDimitry Andric     return true;
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric   if (!Triple.isMacOSX())
4050b57cec5SDimitry Andric     return false;
4060b57cec5SDimitry Andric 
4070b57cec5SDimitry Andric   return (!Triple.isMacOSXVersionLT(10, 5) &&
4080b57cec5SDimitry Andric           (Triple.getArch() == llvm::Triple::x86_64 ||
4090b57cec5SDimitry Andric            Triple.getArch() == llvm::Triple::arm));
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric /// Adds exception related arguments to the driver command arguments. There's a
4130b57cec5SDimitry Andric /// master flag, -fexceptions and also language specific flags to enable/disable
4140b57cec5SDimitry Andric /// C++ and Objective-C exceptions. This makes it possible to for example
4150b57cec5SDimitry Andric /// disable C++ exceptions but enable Objective-C exceptions.
4160b57cec5SDimitry Andric static void addExceptionArgs(const ArgList &Args, types::ID InputType,
4170b57cec5SDimitry Andric                              const ToolChain &TC, bool KernelOrKext,
4180b57cec5SDimitry Andric                              const ObjCRuntime &objcRuntime,
4190b57cec5SDimitry Andric                              ArgStringList &CmdArgs) {
4200b57cec5SDimitry Andric   const llvm::Triple &Triple = TC.getTriple();
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric   if (KernelOrKext) {
4230b57cec5SDimitry Andric     // -mkernel and -fapple-kext imply no exceptions, so claim exception related
4240b57cec5SDimitry Andric     // arguments now to avoid warnings about unused arguments.
4250b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fexceptions);
4260b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fno_exceptions);
4270b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
4280b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
4290b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
4300b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
4310b57cec5SDimitry Andric     return;
4320b57cec5SDimitry Andric   }
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric   // See if the user explicitly enabled exceptions.
4350b57cec5SDimitry Andric   bool EH = Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
4360b57cec5SDimitry Andric                          false);
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric   // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
4390b57cec5SDimitry Andric   // is not necessarily sensible, but follows GCC.
4400b57cec5SDimitry Andric   if (types::isObjC(InputType) &&
4410b57cec5SDimitry Andric       Args.hasFlag(options::OPT_fobjc_exceptions,
4420b57cec5SDimitry Andric                    options::OPT_fno_objc_exceptions, true)) {
4430b57cec5SDimitry Andric     CmdArgs.push_back("-fobjc-exceptions");
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric     EH |= shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
4460b57cec5SDimitry Andric   }
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric   if (types::isCXX(InputType)) {
4490b57cec5SDimitry Andric     // Disable C++ EH by default on XCore and PS4.
4500b57cec5SDimitry Andric     bool CXXExceptionsEnabled =
4510b57cec5SDimitry Andric         Triple.getArch() != llvm::Triple::xcore && !Triple.isPS4CPU();
4520b57cec5SDimitry Andric     Arg *ExceptionArg = Args.getLastArg(
4530b57cec5SDimitry Andric         options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
4540b57cec5SDimitry Andric         options::OPT_fexceptions, options::OPT_fno_exceptions);
4550b57cec5SDimitry Andric     if (ExceptionArg)
4560b57cec5SDimitry Andric       CXXExceptionsEnabled =
4570b57cec5SDimitry Andric           ExceptionArg->getOption().matches(options::OPT_fcxx_exceptions) ||
4580b57cec5SDimitry Andric           ExceptionArg->getOption().matches(options::OPT_fexceptions);
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric     if (CXXExceptionsEnabled) {
4610b57cec5SDimitry Andric       CmdArgs.push_back("-fcxx-exceptions");
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric       EH = true;
4640b57cec5SDimitry Andric     }
4650b57cec5SDimitry Andric   }
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric   if (EH)
4680b57cec5SDimitry Andric     CmdArgs.push_back("-fexceptions");
4690b57cec5SDimitry Andric }
4700b57cec5SDimitry Andric 
471*480093f4SDimitry Andric static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC,
472*480093f4SDimitry Andric                                  const JobAction &JA) {
4730b57cec5SDimitry Andric   bool Default = true;
4740b57cec5SDimitry Andric   if (TC.getTriple().isOSDarwin()) {
4750b57cec5SDimitry Andric     // The native darwin assembler doesn't support the linker_option directives,
4760b57cec5SDimitry Andric     // so we disable them if we think the .s file will be passed to it.
4770b57cec5SDimitry Andric     Default = TC.useIntegratedAs();
4780b57cec5SDimitry Andric   }
479*480093f4SDimitry Andric   // The linker_option directives are intended for host compilation.
480*480093f4SDimitry Andric   if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
481*480093f4SDimitry Andric       JA.isDeviceOffloading(Action::OFK_HIP))
482*480093f4SDimitry Andric     Default = false;
483*480093f4SDimitry Andric   return Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
4840b57cec5SDimitry Andric                       Default);
4850b57cec5SDimitry Andric }
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric static bool ShouldDisableDwarfDirectory(const ArgList &Args,
4880b57cec5SDimitry Andric                                         const ToolChain &TC) {
4890b57cec5SDimitry Andric   bool UseDwarfDirectory =
4900b57cec5SDimitry Andric       Args.hasFlag(options::OPT_fdwarf_directory_asm,
4910b57cec5SDimitry Andric                    options::OPT_fno_dwarf_directory_asm, TC.useIntegratedAs());
4920b57cec5SDimitry Andric   return !UseDwarfDirectory;
4930b57cec5SDimitry Andric }
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric // Convert an arg of the form "-gN" or "-ggdbN" or one of their aliases
4960b57cec5SDimitry Andric // to the corresponding DebugInfoKind.
4970b57cec5SDimitry Andric static codegenoptions::DebugInfoKind DebugLevelToInfoKind(const Arg &A) {
4980b57cec5SDimitry Andric   assert(A.getOption().matches(options::OPT_gN_Group) &&
4990b57cec5SDimitry Andric          "Not a -g option that specifies a debug-info level");
5000b57cec5SDimitry Andric   if (A.getOption().matches(options::OPT_g0) ||
5010b57cec5SDimitry Andric       A.getOption().matches(options::OPT_ggdb0))
5020b57cec5SDimitry Andric     return codegenoptions::NoDebugInfo;
5030b57cec5SDimitry Andric   if (A.getOption().matches(options::OPT_gline_tables_only) ||
5040b57cec5SDimitry Andric       A.getOption().matches(options::OPT_ggdb1))
5050b57cec5SDimitry Andric     return codegenoptions::DebugLineTablesOnly;
5060b57cec5SDimitry Andric   if (A.getOption().matches(options::OPT_gline_directives_only))
5070b57cec5SDimitry Andric     return codegenoptions::DebugDirectivesOnly;
5080b57cec5SDimitry Andric   return codegenoptions::LimitedDebugInfo;
5090b57cec5SDimitry Andric }
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric static bool mustUseNonLeafFramePointerForTarget(const llvm::Triple &Triple) {
5120b57cec5SDimitry Andric   switch (Triple.getArch()){
5130b57cec5SDimitry Andric   default:
5140b57cec5SDimitry Andric     return false;
5150b57cec5SDimitry Andric   case llvm::Triple::arm:
5160b57cec5SDimitry Andric   case llvm::Triple::thumb:
5170b57cec5SDimitry Andric     // ARM Darwin targets require a frame pointer to be always present to aid
5180b57cec5SDimitry Andric     // offline debugging via backtraces.
5190b57cec5SDimitry Andric     return Triple.isOSDarwin();
5200b57cec5SDimitry Andric   }
5210b57cec5SDimitry Andric }
5220b57cec5SDimitry Andric 
5230b57cec5SDimitry Andric static bool useFramePointerForTargetByDefault(const ArgList &Args,
5240b57cec5SDimitry Andric                                               const llvm::Triple &Triple) {
525a7dea167SDimitry Andric   if (Args.hasArg(options::OPT_pg))
526a7dea167SDimitry Andric     return true;
527a7dea167SDimitry Andric 
5280b57cec5SDimitry Andric   switch (Triple.getArch()) {
5290b57cec5SDimitry Andric   case llvm::Triple::xcore:
5300b57cec5SDimitry Andric   case llvm::Triple::wasm32:
5310b57cec5SDimitry Andric   case llvm::Triple::wasm64:
5320b57cec5SDimitry Andric   case llvm::Triple::msp430:
5330b57cec5SDimitry Andric     // XCore never wants frame pointers, regardless of OS.
5340b57cec5SDimitry Andric     // WebAssembly never wants frame pointers.
5350b57cec5SDimitry Andric     return false;
5360b57cec5SDimitry Andric   case llvm::Triple::ppc:
5370b57cec5SDimitry Andric   case llvm::Triple::ppc64:
5380b57cec5SDimitry Andric   case llvm::Triple::ppc64le:
5390b57cec5SDimitry Andric   case llvm::Triple::riscv32:
5400b57cec5SDimitry Andric   case llvm::Triple::riscv64:
541*480093f4SDimitry Andric   case llvm::Triple::amdgcn:
542*480093f4SDimitry Andric   case llvm::Triple::r600:
5430b57cec5SDimitry Andric     return !areOptimizationsEnabled(Args);
5440b57cec5SDimitry Andric   default:
5450b57cec5SDimitry Andric     break;
5460b57cec5SDimitry Andric   }
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric   if (Triple.isOSNetBSD()) {
5490b57cec5SDimitry Andric     return !areOptimizationsEnabled(Args);
5500b57cec5SDimitry Andric   }
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric   if (Triple.isOSLinux() || Triple.getOS() == llvm::Triple::CloudABI ||
5530b57cec5SDimitry Andric       Triple.isOSHurd()) {
5540b57cec5SDimitry Andric     switch (Triple.getArch()) {
5550b57cec5SDimitry Andric     // Don't use a frame pointer on linux if optimizing for certain targets.
5560b57cec5SDimitry Andric     case llvm::Triple::mips64:
5570b57cec5SDimitry Andric     case llvm::Triple::mips64el:
5580b57cec5SDimitry Andric     case llvm::Triple::mips:
5590b57cec5SDimitry Andric     case llvm::Triple::mipsel:
5600b57cec5SDimitry Andric     case llvm::Triple::systemz:
5610b57cec5SDimitry Andric     case llvm::Triple::x86:
5620b57cec5SDimitry Andric     case llvm::Triple::x86_64:
5630b57cec5SDimitry Andric       return !areOptimizationsEnabled(Args);
5640b57cec5SDimitry Andric     default:
5650b57cec5SDimitry Andric       return true;
5660b57cec5SDimitry Andric     }
5670b57cec5SDimitry Andric   }
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric   if (Triple.isOSWindows()) {
5700b57cec5SDimitry Andric     switch (Triple.getArch()) {
5710b57cec5SDimitry Andric     case llvm::Triple::x86:
5720b57cec5SDimitry Andric       return !areOptimizationsEnabled(Args);
5730b57cec5SDimitry Andric     case llvm::Triple::x86_64:
5740b57cec5SDimitry Andric       return Triple.isOSBinFormatMachO();
5750b57cec5SDimitry Andric     case llvm::Triple::arm:
5760b57cec5SDimitry Andric     case llvm::Triple::thumb:
5770b57cec5SDimitry Andric       // Windows on ARM builds with FPO disabled to aid fast stack walking
5780b57cec5SDimitry Andric       return true;
5790b57cec5SDimitry Andric     default:
5800b57cec5SDimitry Andric       // All other supported Windows ISAs use xdata unwind information, so frame
5810b57cec5SDimitry Andric       // pointers are not generally useful.
5820b57cec5SDimitry Andric       return false;
5830b57cec5SDimitry Andric     }
5840b57cec5SDimitry Andric   }
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric   return true;
5870b57cec5SDimitry Andric }
5880b57cec5SDimitry Andric 
589a7dea167SDimitry Andric static CodeGenOptions::FramePointerKind
590a7dea167SDimitry Andric getFramePointerKind(const ArgList &Args, const llvm::Triple &Triple) {
591a7dea167SDimitry Andric   // We have 4 states:
592a7dea167SDimitry Andric   //
593a7dea167SDimitry Andric   //  00) leaf retained, non-leaf retained
594a7dea167SDimitry Andric   //  01) leaf retained, non-leaf omitted (this is invalid)
595a7dea167SDimitry Andric   //  10) leaf omitted, non-leaf retained
596a7dea167SDimitry Andric   //      (what -momit-leaf-frame-pointer was designed for)
597a7dea167SDimitry Andric   //  11) leaf omitted, non-leaf omitted
598a7dea167SDimitry Andric   //
599a7dea167SDimitry Andric   //  "omit" options taking precedence over "no-omit" options is the only way
600a7dea167SDimitry Andric   //  to make 3 valid states representable
601a7dea167SDimitry Andric   Arg *A = Args.getLastArg(options::OPT_fomit_frame_pointer,
602a7dea167SDimitry Andric                            options::OPT_fno_omit_frame_pointer);
603a7dea167SDimitry Andric   bool OmitFP = A && A->getOption().matches(options::OPT_fomit_frame_pointer);
604a7dea167SDimitry Andric   bool NoOmitFP =
605a7dea167SDimitry Andric       A && A->getOption().matches(options::OPT_fno_omit_frame_pointer);
606*480093f4SDimitry Andric   bool KeepLeaf = Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
607*480093f4SDimitry Andric                                options::OPT_mno_omit_leaf_frame_pointer,
608*480093f4SDimitry Andric                                Triple.isAArch64() || Triple.isPS4CPU());
609a7dea167SDimitry Andric   if (NoOmitFP || mustUseNonLeafFramePointerForTarget(Triple) ||
610a7dea167SDimitry Andric       (!OmitFP && useFramePointerForTargetByDefault(Args, Triple))) {
611a7dea167SDimitry Andric     if (KeepLeaf)
612a7dea167SDimitry Andric       return CodeGenOptions::FramePointerKind::NonLeaf;
613a7dea167SDimitry Andric     return CodeGenOptions::FramePointerKind::All;
6140b57cec5SDimitry Andric   }
615a7dea167SDimitry Andric   return CodeGenOptions::FramePointerKind::None;
6160b57cec5SDimitry Andric }
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric /// Add a CC1 option to specify the debug compilation directory.
619a7dea167SDimitry Andric static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs,
620a7dea167SDimitry Andric                                const llvm::vfs::FileSystem &VFS) {
6210b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fdebug_compilation_dir)) {
6220b57cec5SDimitry Andric     CmdArgs.push_back("-fdebug-compilation-dir");
6230b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
624a7dea167SDimitry Andric   } else if (llvm::ErrorOr<std::string> CWD =
625a7dea167SDimitry Andric                  VFS.getCurrentWorkingDirectory()) {
6260b57cec5SDimitry Andric     CmdArgs.push_back("-fdebug-compilation-dir");
627a7dea167SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(*CWD));
6280b57cec5SDimitry Andric   }
6290b57cec5SDimitry Andric }
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric /// Add a CC1 and CC1AS option to specify the debug file path prefix map.
6320b57cec5SDimitry Andric static void addDebugPrefixMapArg(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs) {
633*480093f4SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ,
634*480093f4SDimitry Andric                                     options::OPT_fdebug_prefix_map_EQ)) {
6350b57cec5SDimitry Andric     StringRef Map = A->getValue();
6360b57cec5SDimitry Andric     if (Map.find('=') == StringRef::npos)
637*480093f4SDimitry Andric       D.Diag(diag::err_drv_invalid_argument_to_option)
638*480093f4SDimitry Andric           << Map << A->getOption().getName();
6390b57cec5SDimitry Andric     else
6400b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map));
6410b57cec5SDimitry Andric     A->claim();
6420b57cec5SDimitry Andric   }
6430b57cec5SDimitry Andric }
6440b57cec5SDimitry Andric 
645*480093f4SDimitry Andric /// Add a CC1 and CC1AS option to specify the macro file path prefix map.
646*480093f4SDimitry Andric static void addMacroPrefixMapArg(const Driver &D, const ArgList &Args,
647*480093f4SDimitry Andric                                  ArgStringList &CmdArgs) {
648*480093f4SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ,
649*480093f4SDimitry Andric                                     options::OPT_fmacro_prefix_map_EQ)) {
650*480093f4SDimitry Andric     StringRef Map = A->getValue();
651*480093f4SDimitry Andric     if (Map.find('=') == StringRef::npos)
652*480093f4SDimitry Andric       D.Diag(diag::err_drv_invalid_argument_to_option)
653*480093f4SDimitry Andric           << Map << A->getOption().getName();
654*480093f4SDimitry Andric     else
655*480093f4SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-fmacro-prefix-map=" + Map));
656*480093f4SDimitry Andric     A->claim();
657*480093f4SDimitry Andric   }
658*480093f4SDimitry Andric }
659*480093f4SDimitry Andric 
6600b57cec5SDimitry Andric /// Vectorize at all optimization levels greater than 1 except for -Oz.
6610b57cec5SDimitry Andric /// For -Oz the loop vectorizer is disabled, while the slp vectorizer is
6620b57cec5SDimitry Andric /// enabled.
6630b57cec5SDimitry Andric static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) {
6640b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
6650b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_O4) ||
6660b57cec5SDimitry Andric         A->getOption().matches(options::OPT_Ofast))
6670b57cec5SDimitry Andric       return true;
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_O0))
6700b57cec5SDimitry Andric       return false;
6710b57cec5SDimitry Andric 
6720b57cec5SDimitry Andric     assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
6730b57cec5SDimitry Andric 
6740b57cec5SDimitry Andric     // Vectorize -Os.
6750b57cec5SDimitry Andric     StringRef S(A->getValue());
6760b57cec5SDimitry Andric     if (S == "s")
6770b57cec5SDimitry Andric       return true;
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric     // Don't vectorize -Oz, unless it's the slp vectorizer.
6800b57cec5SDimitry Andric     if (S == "z")
6810b57cec5SDimitry Andric       return isSlpVec;
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric     unsigned OptLevel = 0;
6840b57cec5SDimitry Andric     if (S.getAsInteger(10, OptLevel))
6850b57cec5SDimitry Andric       return false;
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric     return OptLevel > 1;
6880b57cec5SDimitry Andric   }
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric   return false;
6910b57cec5SDimitry Andric }
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric /// Add -x lang to \p CmdArgs for \p Input.
6940b57cec5SDimitry Andric static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
6950b57cec5SDimitry Andric                              ArgStringList &CmdArgs) {
6960b57cec5SDimitry Andric   // When using -verify-pch, we don't want to provide the type
6970b57cec5SDimitry Andric   // 'precompiled-header' if it was inferred from the file extension
6980b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH)
6990b57cec5SDimitry Andric     return;
7000b57cec5SDimitry Andric 
7010b57cec5SDimitry Andric   CmdArgs.push_back("-x");
7020b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_rewrite_objc))
7030b57cec5SDimitry Andric     CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
7040b57cec5SDimitry Andric   else {
7050b57cec5SDimitry Andric     // Map the driver type to the frontend type. This is mostly an identity
7060b57cec5SDimitry Andric     // mapping, except that the distinction between module interface units
7070b57cec5SDimitry Andric     // and other source files does not exist at the frontend layer.
7080b57cec5SDimitry Andric     const char *ClangType;
7090b57cec5SDimitry Andric     switch (Input.getType()) {
7100b57cec5SDimitry Andric     case types::TY_CXXModule:
7110b57cec5SDimitry Andric       ClangType = "c++";
7120b57cec5SDimitry Andric       break;
7130b57cec5SDimitry Andric     case types::TY_PP_CXXModule:
7140b57cec5SDimitry Andric       ClangType = "c++-cpp-output";
7150b57cec5SDimitry Andric       break;
7160b57cec5SDimitry Andric     default:
7170b57cec5SDimitry Andric       ClangType = types::getTypeName(Input.getType());
7180b57cec5SDimitry Andric       break;
7190b57cec5SDimitry Andric     }
7200b57cec5SDimitry Andric     CmdArgs.push_back(ClangType);
7210b57cec5SDimitry Andric   }
7220b57cec5SDimitry Andric }
7230b57cec5SDimitry Andric 
7240b57cec5SDimitry Andric static void appendUserToPath(SmallVectorImpl<char> &Result) {
7250b57cec5SDimitry Andric #ifdef LLVM_ON_UNIX
7260b57cec5SDimitry Andric   const char *Username = getenv("LOGNAME");
7270b57cec5SDimitry Andric #else
7280b57cec5SDimitry Andric   const char *Username = getenv("USERNAME");
7290b57cec5SDimitry Andric #endif
7300b57cec5SDimitry Andric   if (Username) {
7310b57cec5SDimitry Andric     // Validate that LoginName can be used in a path, and get its length.
7320b57cec5SDimitry Andric     size_t Len = 0;
7330b57cec5SDimitry Andric     for (const char *P = Username; *P; ++P, ++Len) {
7340b57cec5SDimitry Andric       if (!clang::isAlphanumeric(*P) && *P != '_') {
7350b57cec5SDimitry Andric         Username = nullptr;
7360b57cec5SDimitry Andric         break;
7370b57cec5SDimitry Andric       }
7380b57cec5SDimitry Andric     }
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric     if (Username && Len > 0) {
7410b57cec5SDimitry Andric       Result.append(Username, Username + Len);
7420b57cec5SDimitry Andric       return;
7430b57cec5SDimitry Andric     }
7440b57cec5SDimitry Andric   }
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric // Fallback to user id.
7470b57cec5SDimitry Andric #ifdef LLVM_ON_UNIX
7480b57cec5SDimitry Andric   std::string UID = llvm::utostr(getuid());
7490b57cec5SDimitry Andric #else
7500b57cec5SDimitry Andric   // FIXME: Windows seems to have an 'SID' that might work.
7510b57cec5SDimitry Andric   std::string UID = "9999";
7520b57cec5SDimitry Andric #endif
7530b57cec5SDimitry Andric   Result.append(UID.begin(), UID.end());
7540b57cec5SDimitry Andric }
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
7570b57cec5SDimitry Andric                                    const Driver &D, const InputInfo &Output,
7580b57cec5SDimitry Andric                                    const ArgList &Args,
7590b57cec5SDimitry Andric                                    ArgStringList &CmdArgs) {
7600b57cec5SDimitry Andric 
7610b57cec5SDimitry Andric   auto *PGOGenerateArg = Args.getLastArg(options::OPT_fprofile_generate,
7620b57cec5SDimitry Andric                                          options::OPT_fprofile_generate_EQ,
7630b57cec5SDimitry Andric                                          options::OPT_fno_profile_generate);
7640b57cec5SDimitry Andric   if (PGOGenerateArg &&
7650b57cec5SDimitry Andric       PGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate))
7660b57cec5SDimitry Andric     PGOGenerateArg = nullptr;
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric   auto *CSPGOGenerateArg = Args.getLastArg(options::OPT_fcs_profile_generate,
7690b57cec5SDimitry Andric                                            options::OPT_fcs_profile_generate_EQ,
7700b57cec5SDimitry Andric                                            options::OPT_fno_profile_generate);
7710b57cec5SDimitry Andric   if (CSPGOGenerateArg &&
7720b57cec5SDimitry Andric       CSPGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate))
7730b57cec5SDimitry Andric     CSPGOGenerateArg = nullptr;
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric   auto *ProfileGenerateArg = Args.getLastArg(
7760b57cec5SDimitry Andric       options::OPT_fprofile_instr_generate,
7770b57cec5SDimitry Andric       options::OPT_fprofile_instr_generate_EQ,
7780b57cec5SDimitry Andric       options::OPT_fno_profile_instr_generate);
7790b57cec5SDimitry Andric   if (ProfileGenerateArg &&
7800b57cec5SDimitry Andric       ProfileGenerateArg->getOption().matches(
7810b57cec5SDimitry Andric           options::OPT_fno_profile_instr_generate))
7820b57cec5SDimitry Andric     ProfileGenerateArg = nullptr;
7830b57cec5SDimitry Andric 
7840b57cec5SDimitry Andric   if (PGOGenerateArg && ProfileGenerateArg)
7850b57cec5SDimitry Andric     D.Diag(diag::err_drv_argument_not_allowed_with)
7860b57cec5SDimitry Andric         << PGOGenerateArg->getSpelling() << ProfileGenerateArg->getSpelling();
7870b57cec5SDimitry Andric 
7880b57cec5SDimitry Andric   auto *ProfileUseArg = getLastProfileUseArg(Args);
7890b57cec5SDimitry Andric 
7900b57cec5SDimitry Andric   if (PGOGenerateArg && ProfileUseArg)
7910b57cec5SDimitry Andric     D.Diag(diag::err_drv_argument_not_allowed_with)
7920b57cec5SDimitry Andric         << ProfileUseArg->getSpelling() << PGOGenerateArg->getSpelling();
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric   if (ProfileGenerateArg && ProfileUseArg)
7950b57cec5SDimitry Andric     D.Diag(diag::err_drv_argument_not_allowed_with)
7960b57cec5SDimitry Andric         << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling();
7970b57cec5SDimitry Andric 
7980b57cec5SDimitry Andric   if (CSPGOGenerateArg && PGOGenerateArg)
7990b57cec5SDimitry Andric     D.Diag(diag::err_drv_argument_not_allowed_with)
8000b57cec5SDimitry Andric         << CSPGOGenerateArg->getSpelling() << PGOGenerateArg->getSpelling();
8010b57cec5SDimitry Andric 
8020b57cec5SDimitry Andric   if (ProfileGenerateArg) {
8030b57cec5SDimitry Andric     if (ProfileGenerateArg->getOption().matches(
8040b57cec5SDimitry Andric             options::OPT_fprofile_instr_generate_EQ))
8050b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-instrument-path=") +
8060b57cec5SDimitry Andric                                            ProfileGenerateArg->getValue()));
8070b57cec5SDimitry Andric     // The default is to use Clang Instrumentation.
8080b57cec5SDimitry Andric     CmdArgs.push_back("-fprofile-instrument=clang");
8090b57cec5SDimitry Andric     if (TC.getTriple().isWindowsMSVCEnvironment()) {
8100b57cec5SDimitry Andric       // Add dependent lib for clang_rt.profile
8110b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
8120b57cec5SDimitry Andric                                            TC.getCompilerRT(Args, "profile")));
8130b57cec5SDimitry Andric     }
8140b57cec5SDimitry Andric   }
8150b57cec5SDimitry Andric 
8160b57cec5SDimitry Andric   Arg *PGOGenArg = nullptr;
8170b57cec5SDimitry Andric   if (PGOGenerateArg) {
8180b57cec5SDimitry Andric     assert(!CSPGOGenerateArg);
8190b57cec5SDimitry Andric     PGOGenArg = PGOGenerateArg;
8200b57cec5SDimitry Andric     CmdArgs.push_back("-fprofile-instrument=llvm");
8210b57cec5SDimitry Andric   }
8220b57cec5SDimitry Andric   if (CSPGOGenerateArg) {
8230b57cec5SDimitry Andric     assert(!PGOGenerateArg);
8240b57cec5SDimitry Andric     PGOGenArg = CSPGOGenerateArg;
8250b57cec5SDimitry Andric     CmdArgs.push_back("-fprofile-instrument=csllvm");
8260b57cec5SDimitry Andric   }
8270b57cec5SDimitry Andric   if (PGOGenArg) {
8280b57cec5SDimitry Andric     if (TC.getTriple().isWindowsMSVCEnvironment()) {
8290b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
8300b57cec5SDimitry Andric                                            TC.getCompilerRT(Args, "profile")));
8310b57cec5SDimitry Andric     }
8320b57cec5SDimitry Andric     if (PGOGenArg->getOption().matches(
8330b57cec5SDimitry Andric             PGOGenerateArg ? options::OPT_fprofile_generate_EQ
8340b57cec5SDimitry Andric                            : options::OPT_fcs_profile_generate_EQ)) {
8350b57cec5SDimitry Andric       SmallString<128> Path(PGOGenArg->getValue());
8360b57cec5SDimitry Andric       llvm::sys::path::append(Path, "default_%m.profraw");
8370b57cec5SDimitry Andric       CmdArgs.push_back(
8380b57cec5SDimitry Andric           Args.MakeArgString(Twine("-fprofile-instrument-path=") + Path));
8390b57cec5SDimitry Andric     }
8400b57cec5SDimitry Andric   }
8410b57cec5SDimitry Andric 
8420b57cec5SDimitry Andric   if (ProfileUseArg) {
8430b57cec5SDimitry Andric     if (ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ))
8440b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(
8450b57cec5SDimitry Andric           Twine("-fprofile-instrument-use-path=") + ProfileUseArg->getValue()));
8460b57cec5SDimitry Andric     else if ((ProfileUseArg->getOption().matches(
8470b57cec5SDimitry Andric                   options::OPT_fprofile_use_EQ) ||
8480b57cec5SDimitry Andric               ProfileUseArg->getOption().matches(
8490b57cec5SDimitry Andric                   options::OPT_fprofile_instr_use))) {
8500b57cec5SDimitry Andric       SmallString<128> Path(
8510b57cec5SDimitry Andric           ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue());
8520b57cec5SDimitry Andric       if (Path.empty() || llvm::sys::fs::is_directory(Path))
8530b57cec5SDimitry Andric         llvm::sys::path::append(Path, "default.profdata");
8540b57cec5SDimitry Andric       CmdArgs.push_back(
8550b57cec5SDimitry Andric           Args.MakeArgString(Twine("-fprofile-instrument-use-path=") + Path));
8560b57cec5SDimitry Andric     }
8570b57cec5SDimitry Andric   }
8580b57cec5SDimitry Andric 
859a7dea167SDimitry Andric   bool EmitCovNotes = Args.hasArg(options::OPT_ftest_coverage) ||
860a7dea167SDimitry Andric                       Args.hasArg(options::OPT_coverage);
861a7dea167SDimitry Andric   bool EmitCovData = Args.hasFlag(options::OPT_fprofile_arcs,
862a7dea167SDimitry Andric                                   options::OPT_fno_profile_arcs, false) ||
863a7dea167SDimitry Andric                      Args.hasArg(options::OPT_coverage);
864a7dea167SDimitry Andric   if (EmitCovNotes)
8650b57cec5SDimitry Andric     CmdArgs.push_back("-femit-coverage-notes");
866a7dea167SDimitry Andric   if (EmitCovData)
8670b57cec5SDimitry Andric     CmdArgs.push_back("-femit-coverage-data");
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fcoverage_mapping,
8700b57cec5SDimitry Andric                    options::OPT_fno_coverage_mapping, false)) {
8710b57cec5SDimitry Andric     if (!ProfileGenerateArg)
8720b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_argument_only_allowed_with)
8730b57cec5SDimitry Andric           << "-fcoverage-mapping"
8740b57cec5SDimitry Andric           << "-fprofile-instr-generate";
8750b57cec5SDimitry Andric 
8760b57cec5SDimitry Andric     CmdArgs.push_back("-fcoverage-mapping");
8770b57cec5SDimitry Andric   }
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) {
8800b57cec5SDimitry Andric     auto *Arg = Args.getLastArg(options::OPT_fprofile_exclude_files_EQ);
8810b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_coverage))
8820b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_argument_only_allowed_with)
8830b57cec5SDimitry Andric           << "-fprofile-exclude-files="
8840b57cec5SDimitry Andric           << "--coverage";
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric     StringRef v = Arg->getValue();
8870b57cec5SDimitry Andric     CmdArgs.push_back(
8880b57cec5SDimitry Andric         Args.MakeArgString(Twine("-fprofile-exclude-files=" + v)));
8890b57cec5SDimitry Andric   }
8900b57cec5SDimitry Andric 
8910b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fprofile_filter_files_EQ)) {
8920b57cec5SDimitry Andric     auto *Arg = Args.getLastArg(options::OPT_fprofile_filter_files_EQ);
8930b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_coverage))
8940b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_argument_only_allowed_with)
8950b57cec5SDimitry Andric           << "-fprofile-filter-files="
8960b57cec5SDimitry Andric           << "--coverage";
8970b57cec5SDimitry Andric 
8980b57cec5SDimitry Andric     StringRef v = Arg->getValue();
8990b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-filter-files=" + v)));
9000b57cec5SDimitry Andric   }
9010b57cec5SDimitry Andric 
902a7dea167SDimitry Andric   // Leave -fprofile-dir= an unused argument unless .gcda emission is
903a7dea167SDimitry Andric   // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider
904a7dea167SDimitry Andric   // the flag used. There is no -fno-profile-dir, so the user has no
905a7dea167SDimitry Andric   // targeted way to suppress the warning.
906a7dea167SDimitry Andric   Arg *FProfileDir = nullptr;
907a7dea167SDimitry Andric   if (Args.hasArg(options::OPT_fprofile_arcs) ||
908a7dea167SDimitry Andric       Args.hasArg(options::OPT_coverage))
909a7dea167SDimitry Andric     FProfileDir = Args.getLastArg(options::OPT_fprofile_dir);
910a7dea167SDimitry Andric 
911a7dea167SDimitry Andric   // Put the .gcno and .gcda files (if needed) next to the object file or
912a7dea167SDimitry Andric   // bitcode file in the case of LTO.
913a7dea167SDimitry Andric   // FIXME: There should be a simpler way to find the object file for this
914a7dea167SDimitry Andric   // input, and this code probably does the wrong thing for commands that
915a7dea167SDimitry Andric   // compile and link all at once.
916a7dea167SDimitry Andric   if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
917a7dea167SDimitry Andric       (EmitCovNotes || EmitCovData) && Output.isFilename()) {
9180b57cec5SDimitry Andric     SmallString<128> OutputFilename;
919*480093f4SDimitry Andric     if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT__SLASH_Fo))
920*480093f4SDimitry Andric       OutputFilename = FinalOutput->getValue();
921*480093f4SDimitry Andric     else if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
9220b57cec5SDimitry Andric       OutputFilename = FinalOutput->getValue();
9230b57cec5SDimitry Andric     else
9240b57cec5SDimitry Andric       OutputFilename = llvm::sys::path::filename(Output.getBaseInput());
9250b57cec5SDimitry Andric     SmallString<128> CoverageFilename = OutputFilename;
926a7dea167SDimitry Andric     if (llvm::sys::path::is_relative(CoverageFilename))
927a7dea167SDimitry Andric       (void)D.getVFS().makeAbsolute(CoverageFilename);
9280b57cec5SDimitry Andric     llvm::sys::path::replace_extension(CoverageFilename, "gcno");
929a7dea167SDimitry Andric 
930a7dea167SDimitry Andric     CmdArgs.push_back("-coverage-notes-file");
9310b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
9320b57cec5SDimitry Andric 
933a7dea167SDimitry Andric     if (EmitCovData) {
934a7dea167SDimitry Andric       if (FProfileDir) {
9350b57cec5SDimitry Andric         CoverageFilename = FProfileDir->getValue();
9360b57cec5SDimitry Andric         llvm::sys::path::append(CoverageFilename, OutputFilename);
9370b57cec5SDimitry Andric       }
9380b57cec5SDimitry Andric       llvm::sys::path::replace_extension(CoverageFilename, "gcda");
939a7dea167SDimitry Andric       CmdArgs.push_back("-coverage-data-file");
9400b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
9410b57cec5SDimitry Andric     }
9420b57cec5SDimitry Andric   }
9430b57cec5SDimitry Andric }
9440b57cec5SDimitry Andric 
9450b57cec5SDimitry Andric /// Check whether the given input tree contains any compilation actions.
9460b57cec5SDimitry Andric static bool ContainsCompileAction(const Action *A) {
9470b57cec5SDimitry Andric   if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A))
9480b57cec5SDimitry Andric     return true;
9490b57cec5SDimitry Andric 
9500b57cec5SDimitry Andric   for (const auto &AI : A->inputs())
9510b57cec5SDimitry Andric     if (ContainsCompileAction(AI))
9520b57cec5SDimitry Andric       return true;
9530b57cec5SDimitry Andric 
9540b57cec5SDimitry Andric   return false;
9550b57cec5SDimitry Andric }
9560b57cec5SDimitry Andric 
9570b57cec5SDimitry Andric /// Check if -relax-all should be passed to the internal assembler.
9580b57cec5SDimitry Andric /// This is done by default when compiling non-assembler source with -O0.
9590b57cec5SDimitry Andric static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
9600b57cec5SDimitry Andric   bool RelaxDefault = true;
9610b57cec5SDimitry Andric 
9620b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_O_Group))
9630b57cec5SDimitry Andric     RelaxDefault = A->getOption().matches(options::OPT_O0);
9640b57cec5SDimitry Andric 
9650b57cec5SDimitry Andric   if (RelaxDefault) {
9660b57cec5SDimitry Andric     RelaxDefault = false;
9670b57cec5SDimitry Andric     for (const auto &Act : C.getActions()) {
9680b57cec5SDimitry Andric       if (ContainsCompileAction(Act)) {
9690b57cec5SDimitry Andric         RelaxDefault = true;
9700b57cec5SDimitry Andric         break;
9710b57cec5SDimitry Andric       }
9720b57cec5SDimitry Andric     }
9730b57cec5SDimitry Andric   }
9740b57cec5SDimitry Andric 
9750b57cec5SDimitry Andric   return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
9760b57cec5SDimitry Andric                       RelaxDefault);
9770b57cec5SDimitry Andric }
9780b57cec5SDimitry Andric 
9790b57cec5SDimitry Andric // Extract the integer N from a string spelled "-dwarf-N", returning 0
9800b57cec5SDimitry Andric // on mismatch. The StringRef input (rather than an Arg) allows
9810b57cec5SDimitry Andric // for use by the "-Xassembler" option parser.
9820b57cec5SDimitry Andric static unsigned DwarfVersionNum(StringRef ArgValue) {
9830b57cec5SDimitry Andric   return llvm::StringSwitch<unsigned>(ArgValue)
9840b57cec5SDimitry Andric       .Case("-gdwarf-2", 2)
9850b57cec5SDimitry Andric       .Case("-gdwarf-3", 3)
9860b57cec5SDimitry Andric       .Case("-gdwarf-4", 4)
9870b57cec5SDimitry Andric       .Case("-gdwarf-5", 5)
9880b57cec5SDimitry Andric       .Default(0);
9890b57cec5SDimitry Andric }
9900b57cec5SDimitry Andric 
9910b57cec5SDimitry Andric static void RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs,
9920b57cec5SDimitry Andric                                     codegenoptions::DebugInfoKind DebugInfoKind,
9930b57cec5SDimitry Andric                                     unsigned DwarfVersion,
9940b57cec5SDimitry Andric                                     llvm::DebuggerKind DebuggerTuning) {
9950b57cec5SDimitry Andric   switch (DebugInfoKind) {
9960b57cec5SDimitry Andric   case codegenoptions::DebugDirectivesOnly:
9970b57cec5SDimitry Andric     CmdArgs.push_back("-debug-info-kind=line-directives-only");
9980b57cec5SDimitry Andric     break;
9990b57cec5SDimitry Andric   case codegenoptions::DebugLineTablesOnly:
10000b57cec5SDimitry Andric     CmdArgs.push_back("-debug-info-kind=line-tables-only");
10010b57cec5SDimitry Andric     break;
1002*480093f4SDimitry Andric   case codegenoptions::DebugInfoConstructor:
1003*480093f4SDimitry Andric     CmdArgs.push_back("-debug-info-kind=constructor");
1004*480093f4SDimitry Andric     break;
10050b57cec5SDimitry Andric   case codegenoptions::LimitedDebugInfo:
10060b57cec5SDimitry Andric     CmdArgs.push_back("-debug-info-kind=limited");
10070b57cec5SDimitry Andric     break;
10080b57cec5SDimitry Andric   case codegenoptions::FullDebugInfo:
10090b57cec5SDimitry Andric     CmdArgs.push_back("-debug-info-kind=standalone");
10100b57cec5SDimitry Andric     break;
10110b57cec5SDimitry Andric   default:
10120b57cec5SDimitry Andric     break;
10130b57cec5SDimitry Andric   }
10140b57cec5SDimitry Andric   if (DwarfVersion > 0)
10150b57cec5SDimitry Andric     CmdArgs.push_back(
10160b57cec5SDimitry Andric         Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion)));
10170b57cec5SDimitry Andric   switch (DebuggerTuning) {
10180b57cec5SDimitry Andric   case llvm::DebuggerKind::GDB:
10190b57cec5SDimitry Andric     CmdArgs.push_back("-debugger-tuning=gdb");
10200b57cec5SDimitry Andric     break;
10210b57cec5SDimitry Andric   case llvm::DebuggerKind::LLDB:
10220b57cec5SDimitry Andric     CmdArgs.push_back("-debugger-tuning=lldb");
10230b57cec5SDimitry Andric     break;
10240b57cec5SDimitry Andric   case llvm::DebuggerKind::SCE:
10250b57cec5SDimitry Andric     CmdArgs.push_back("-debugger-tuning=sce");
10260b57cec5SDimitry Andric     break;
10270b57cec5SDimitry Andric   default:
10280b57cec5SDimitry Andric     break;
10290b57cec5SDimitry Andric   }
10300b57cec5SDimitry Andric }
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric static bool checkDebugInfoOption(const Arg *A, const ArgList &Args,
10330b57cec5SDimitry Andric                                  const Driver &D, const ToolChain &TC) {
10340b57cec5SDimitry Andric   assert(A && "Expected non-nullptr argument.");
10350b57cec5SDimitry Andric   if (TC.supportsDebugInfoOption(A))
10360b57cec5SDimitry Andric     return true;
10370b57cec5SDimitry Andric   D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target)
10380b57cec5SDimitry Andric       << A->getAsString(Args) << TC.getTripleString();
10390b57cec5SDimitry Andric   return false;
10400b57cec5SDimitry Andric }
10410b57cec5SDimitry Andric 
10420b57cec5SDimitry Andric static void RenderDebugInfoCompressionArgs(const ArgList &Args,
10430b57cec5SDimitry Andric                                            ArgStringList &CmdArgs,
10440b57cec5SDimitry Andric                                            const Driver &D,
10450b57cec5SDimitry Andric                                            const ToolChain &TC) {
10460b57cec5SDimitry Andric   const Arg *A = Args.getLastArg(options::OPT_gz, options::OPT_gz_EQ);
10470b57cec5SDimitry Andric   if (!A)
10480b57cec5SDimitry Andric     return;
10490b57cec5SDimitry Andric   if (checkDebugInfoOption(A, Args, D, TC)) {
10500b57cec5SDimitry Andric     if (A->getOption().getID() == options::OPT_gz) {
10510b57cec5SDimitry Andric       if (llvm::zlib::isAvailable())
10520b57cec5SDimitry Andric         CmdArgs.push_back("--compress-debug-sections");
10530b57cec5SDimitry Andric       else
10540b57cec5SDimitry Andric         D.Diag(diag::warn_debug_compression_unavailable);
10550b57cec5SDimitry Andric       return;
10560b57cec5SDimitry Andric     }
10570b57cec5SDimitry Andric 
10580b57cec5SDimitry Andric     StringRef Value = A->getValue();
10590b57cec5SDimitry Andric     if (Value == "none") {
10600b57cec5SDimitry Andric       CmdArgs.push_back("--compress-debug-sections=none");
10610b57cec5SDimitry Andric     } else if (Value == "zlib" || Value == "zlib-gnu") {
10620b57cec5SDimitry Andric       if (llvm::zlib::isAvailable()) {
10630b57cec5SDimitry Andric         CmdArgs.push_back(
10640b57cec5SDimitry Andric             Args.MakeArgString("--compress-debug-sections=" + Twine(Value)));
10650b57cec5SDimitry Andric       } else {
10660b57cec5SDimitry Andric         D.Diag(diag::warn_debug_compression_unavailable);
10670b57cec5SDimitry Andric       }
10680b57cec5SDimitry Andric     } else {
10690b57cec5SDimitry Andric       D.Diag(diag::err_drv_unsupported_option_argument)
10700b57cec5SDimitry Andric           << A->getOption().getName() << Value;
10710b57cec5SDimitry Andric     }
10720b57cec5SDimitry Andric   }
10730b57cec5SDimitry Andric }
10740b57cec5SDimitry Andric 
10750b57cec5SDimitry Andric static const char *RelocationModelName(llvm::Reloc::Model Model) {
10760b57cec5SDimitry Andric   switch (Model) {
10770b57cec5SDimitry Andric   case llvm::Reloc::Static:
10780b57cec5SDimitry Andric     return "static";
10790b57cec5SDimitry Andric   case llvm::Reloc::PIC_:
10800b57cec5SDimitry Andric     return "pic";
10810b57cec5SDimitry Andric   case llvm::Reloc::DynamicNoPIC:
10820b57cec5SDimitry Andric     return "dynamic-no-pic";
10830b57cec5SDimitry Andric   case llvm::Reloc::ROPI:
10840b57cec5SDimitry Andric     return "ropi";
10850b57cec5SDimitry Andric   case llvm::Reloc::RWPI:
10860b57cec5SDimitry Andric     return "rwpi";
10870b57cec5SDimitry Andric   case llvm::Reloc::ROPI_RWPI:
10880b57cec5SDimitry Andric     return "ropi-rwpi";
10890b57cec5SDimitry Andric   }
10900b57cec5SDimitry Andric   llvm_unreachable("Unknown Reloc::Model kind");
10910b57cec5SDimitry Andric }
10920b57cec5SDimitry Andric 
10930b57cec5SDimitry Andric void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
10940b57cec5SDimitry Andric                                     const Driver &D, const ArgList &Args,
10950b57cec5SDimitry Andric                                     ArgStringList &CmdArgs,
10960b57cec5SDimitry Andric                                     const InputInfo &Output,
10970b57cec5SDimitry Andric                                     const InputInfoList &Inputs) const {
10980b57cec5SDimitry Andric   const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU();
10990b57cec5SDimitry Andric 
11000b57cec5SDimitry Andric   CheckPreprocessingOptions(D, Args);
11010b57cec5SDimitry Andric 
11020b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_C);
11030b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_CC);
11040b57cec5SDimitry Andric 
11050b57cec5SDimitry Andric   // Handle dependency file generation.
1106a7dea167SDimitry Andric   Arg *ArgM = Args.getLastArg(options::OPT_MM);
1107a7dea167SDimitry Andric   if (!ArgM)
1108a7dea167SDimitry Andric     ArgM = Args.getLastArg(options::OPT_M);
1109a7dea167SDimitry Andric   Arg *ArgMD = Args.getLastArg(options::OPT_MMD);
1110a7dea167SDimitry Andric   if (!ArgMD)
1111a7dea167SDimitry Andric     ArgMD = Args.getLastArg(options::OPT_MD);
1112a7dea167SDimitry Andric 
1113a7dea167SDimitry Andric   // -M and -MM imply -w.
1114a7dea167SDimitry Andric   if (ArgM)
1115a7dea167SDimitry Andric     CmdArgs.push_back("-w");
1116a7dea167SDimitry Andric   else
1117a7dea167SDimitry Andric     ArgM = ArgMD;
1118a7dea167SDimitry Andric 
1119a7dea167SDimitry Andric   if (ArgM) {
11200b57cec5SDimitry Andric     // Determine the output location.
11210b57cec5SDimitry Andric     const char *DepFile;
11220b57cec5SDimitry Andric     if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
11230b57cec5SDimitry Andric       DepFile = MF->getValue();
11240b57cec5SDimitry Andric       C.addFailureResultFile(DepFile, &JA);
11250b57cec5SDimitry Andric     } else if (Output.getType() == types::TY_Dependencies) {
11260b57cec5SDimitry Andric       DepFile = Output.getFilename();
1127a7dea167SDimitry Andric     } else if (!ArgMD) {
11280b57cec5SDimitry Andric       DepFile = "-";
11290b57cec5SDimitry Andric     } else {
11300b57cec5SDimitry Andric       DepFile = getDependencyFileName(Args, Inputs);
11310b57cec5SDimitry Andric       C.addFailureResultFile(DepFile, &JA);
11320b57cec5SDimitry Andric     }
11330b57cec5SDimitry Andric     CmdArgs.push_back("-dependency-file");
11340b57cec5SDimitry Andric     CmdArgs.push_back(DepFile);
11350b57cec5SDimitry Andric 
1136a7dea167SDimitry Andric     bool HasTarget = false;
1137a7dea167SDimitry Andric     for (const Arg *A : Args.filtered(options::OPT_MT, options::OPT_MQ)) {
1138a7dea167SDimitry Andric       HasTarget = true;
1139a7dea167SDimitry Andric       A->claim();
1140a7dea167SDimitry Andric       if (A->getOption().matches(options::OPT_MT)) {
1141a7dea167SDimitry Andric         A->render(Args, CmdArgs);
1142a7dea167SDimitry Andric       } else {
1143a7dea167SDimitry Andric         CmdArgs.push_back("-MT");
1144a7dea167SDimitry Andric         SmallString<128> Quoted;
1145a7dea167SDimitry Andric         QuoteTarget(A->getValue(), Quoted);
1146a7dea167SDimitry Andric         CmdArgs.push_back(Args.MakeArgString(Quoted));
1147a7dea167SDimitry Andric       }
1148a7dea167SDimitry Andric     }
1149a7dea167SDimitry Andric 
11500b57cec5SDimitry Andric     // Add a default target if one wasn't specified.
1151a7dea167SDimitry Andric     if (!HasTarget) {
11520b57cec5SDimitry Andric       const char *DepTarget;
11530b57cec5SDimitry Andric 
11540b57cec5SDimitry Andric       // If user provided -o, that is the dependency target, except
11550b57cec5SDimitry Andric       // when we are only generating a dependency file.
11560b57cec5SDimitry Andric       Arg *OutputOpt = Args.getLastArg(options::OPT_o);
11570b57cec5SDimitry Andric       if (OutputOpt && Output.getType() != types::TY_Dependencies) {
11580b57cec5SDimitry Andric         DepTarget = OutputOpt->getValue();
11590b57cec5SDimitry Andric       } else {
11600b57cec5SDimitry Andric         // Otherwise derive from the base input.
11610b57cec5SDimitry Andric         //
11620b57cec5SDimitry Andric         // FIXME: This should use the computed output file location.
11630b57cec5SDimitry Andric         SmallString<128> P(Inputs[0].getBaseInput());
11640b57cec5SDimitry Andric         llvm::sys::path::replace_extension(P, "o");
11650b57cec5SDimitry Andric         DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
11660b57cec5SDimitry Andric       }
11670b57cec5SDimitry Andric 
11680b57cec5SDimitry Andric       CmdArgs.push_back("-MT");
11690b57cec5SDimitry Andric       SmallString<128> Quoted;
11700b57cec5SDimitry Andric       QuoteTarget(DepTarget, Quoted);
11710b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(Quoted));
11720b57cec5SDimitry Andric     }
11730b57cec5SDimitry Andric 
1174a7dea167SDimitry Andric     if (ArgM->getOption().matches(options::OPT_M) ||
1175a7dea167SDimitry Andric         ArgM->getOption().matches(options::OPT_MD))
11760b57cec5SDimitry Andric       CmdArgs.push_back("-sys-header-deps");
11770b57cec5SDimitry Andric     if ((isa<PrecompileJobAction>(JA) &&
11780b57cec5SDimitry Andric          !Args.hasArg(options::OPT_fno_module_file_deps)) ||
11790b57cec5SDimitry Andric         Args.hasArg(options::OPT_fmodule_file_deps))
11800b57cec5SDimitry Andric       CmdArgs.push_back("-module-file-deps");
11810b57cec5SDimitry Andric   }
11820b57cec5SDimitry Andric 
11830b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_MG)) {
1184a7dea167SDimitry Andric     if (!ArgM || ArgM->getOption().matches(options::OPT_MD) ||
1185a7dea167SDimitry Andric         ArgM->getOption().matches(options::OPT_MMD))
11860b57cec5SDimitry Andric       D.Diag(diag::err_drv_mg_requires_m_or_mm);
11870b57cec5SDimitry Andric     CmdArgs.push_back("-MG");
11880b57cec5SDimitry Andric   }
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_MP);
11910b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_MV);
11920b57cec5SDimitry Andric 
11930b57cec5SDimitry Andric   // Add offload include arguments specific for CUDA.  This must happen before
11940b57cec5SDimitry Andric   // we -I or -include anything else, because we must pick up the CUDA headers
11950b57cec5SDimitry Andric   // from the particular CUDA installation, rather than from e.g.
11960b57cec5SDimitry Andric   // /usr/local/include.
11970b57cec5SDimitry Andric   if (JA.isOffloading(Action::OFK_Cuda))
11980b57cec5SDimitry Andric     getToolChain().AddCudaIncludeArgs(Args, CmdArgs);
11990b57cec5SDimitry Andric 
12000b57cec5SDimitry Andric   // If we are offloading to a target via OpenMP we need to include the
12010b57cec5SDimitry Andric   // openmp_wrappers folder which contains alternative system headers.
12020b57cec5SDimitry Andric   if (JA.isDeviceOffloading(Action::OFK_OpenMP) &&
12030b57cec5SDimitry Andric       getToolChain().getTriple().isNVPTX()){
12040b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_nobuiltininc)) {
12050b57cec5SDimitry Andric       // Add openmp_wrappers/* to our system include path.  This lets us wrap
12060b57cec5SDimitry Andric       // standard library headers.
12070b57cec5SDimitry Andric       SmallString<128> P(D.ResourceDir);
12080b57cec5SDimitry Andric       llvm::sys::path::append(P, "include");
12090b57cec5SDimitry Andric       llvm::sys::path::append(P, "openmp_wrappers");
12100b57cec5SDimitry Andric       CmdArgs.push_back("-internal-isystem");
12110b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(P));
12120b57cec5SDimitry Andric     }
12130b57cec5SDimitry Andric 
12140b57cec5SDimitry Andric     CmdArgs.push_back("-include");
12150b57cec5SDimitry Andric     CmdArgs.push_back("__clang_openmp_math_declares.h");
12160b57cec5SDimitry Andric   }
12170b57cec5SDimitry Andric 
12180b57cec5SDimitry Andric   // Add -i* options, and automatically translate to
12190b57cec5SDimitry Andric   // -include-pch/-include-pth for transparent PCH support. It's
12200b57cec5SDimitry Andric   // wonky, but we include looking for .gch so we can support seamless
12210b57cec5SDimitry Andric   // replacement into a build system already set up to be generating
12220b57cec5SDimitry Andric   // .gch files.
12230b57cec5SDimitry Andric 
12240b57cec5SDimitry Andric   if (getToolChain().getDriver().IsCLMode()) {
12250b57cec5SDimitry Andric     const Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc);
12260b57cec5SDimitry Andric     const Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu);
12270b57cec5SDimitry Andric     if (YcArg && JA.getKind() >= Action::PrecompileJobClass &&
12280b57cec5SDimitry Andric         JA.getKind() <= Action::AssembleJobClass) {
12290b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-building-pch-with-obj"));
12300b57cec5SDimitry Andric     }
12310b57cec5SDimitry Andric     if (YcArg || YuArg) {
12320b57cec5SDimitry Andric       StringRef ThroughHeader = YcArg ? YcArg->getValue() : YuArg->getValue();
12330b57cec5SDimitry Andric       if (!isa<PrecompileJobAction>(JA)) {
12340b57cec5SDimitry Andric         CmdArgs.push_back("-include-pch");
12350b57cec5SDimitry Andric         CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath(
12360b57cec5SDimitry Andric             C, !ThroughHeader.empty()
12370b57cec5SDimitry Andric                    ? ThroughHeader
12380b57cec5SDimitry Andric                    : llvm::sys::path::filename(Inputs[0].getBaseInput()))));
12390b57cec5SDimitry Andric       }
12400b57cec5SDimitry Andric 
12410b57cec5SDimitry Andric       if (ThroughHeader.empty()) {
12420b57cec5SDimitry Andric         CmdArgs.push_back(Args.MakeArgString(
12430b57cec5SDimitry Andric             Twine("-pch-through-hdrstop-") + (YcArg ? "create" : "use")));
12440b57cec5SDimitry Andric       } else {
12450b57cec5SDimitry Andric         CmdArgs.push_back(
12460b57cec5SDimitry Andric             Args.MakeArgString(Twine("-pch-through-header=") + ThroughHeader));
12470b57cec5SDimitry Andric       }
12480b57cec5SDimitry Andric     }
12490b57cec5SDimitry Andric   }
12500b57cec5SDimitry Andric 
12510b57cec5SDimitry Andric   bool RenderedImplicitInclude = false;
12520b57cec5SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) {
12530b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_include)) {
12540b57cec5SDimitry Andric       // Handling of gcc-style gch precompiled headers.
12550b57cec5SDimitry Andric       bool IsFirstImplicitInclude = !RenderedImplicitInclude;
12560b57cec5SDimitry Andric       RenderedImplicitInclude = true;
12570b57cec5SDimitry Andric 
12580b57cec5SDimitry Andric       bool FoundPCH = false;
12590b57cec5SDimitry Andric       SmallString<128> P(A->getValue());
12600b57cec5SDimitry Andric       // We want the files to have a name like foo.h.pch. Add a dummy extension
12610b57cec5SDimitry Andric       // so that replace_extension does the right thing.
12620b57cec5SDimitry Andric       P += ".dummy";
12630b57cec5SDimitry Andric       llvm::sys::path::replace_extension(P, "pch");
12640b57cec5SDimitry Andric       if (llvm::sys::fs::exists(P))
12650b57cec5SDimitry Andric         FoundPCH = true;
12660b57cec5SDimitry Andric 
12670b57cec5SDimitry Andric       if (!FoundPCH) {
12680b57cec5SDimitry Andric         llvm::sys::path::replace_extension(P, "gch");
12690b57cec5SDimitry Andric         if (llvm::sys::fs::exists(P)) {
12700b57cec5SDimitry Andric           FoundPCH = true;
12710b57cec5SDimitry Andric         }
12720b57cec5SDimitry Andric       }
12730b57cec5SDimitry Andric 
12740b57cec5SDimitry Andric       if (FoundPCH) {
12750b57cec5SDimitry Andric         if (IsFirstImplicitInclude) {
12760b57cec5SDimitry Andric           A->claim();
12770b57cec5SDimitry Andric           CmdArgs.push_back("-include-pch");
12780b57cec5SDimitry Andric           CmdArgs.push_back(Args.MakeArgString(P));
12790b57cec5SDimitry Andric           continue;
12800b57cec5SDimitry Andric         } else {
12810b57cec5SDimitry Andric           // Ignore the PCH if not first on command line and emit warning.
12820b57cec5SDimitry Andric           D.Diag(diag::warn_drv_pch_not_first_include) << P
12830b57cec5SDimitry Andric                                                        << A->getAsString(Args);
12840b57cec5SDimitry Andric         }
12850b57cec5SDimitry Andric       }
12860b57cec5SDimitry Andric     } else if (A->getOption().matches(options::OPT_isystem_after)) {
12870b57cec5SDimitry Andric       // Handling of paths which must come late.  These entries are handled by
12880b57cec5SDimitry Andric       // the toolchain itself after the resource dir is inserted in the right
12890b57cec5SDimitry Andric       // search order.
12900b57cec5SDimitry Andric       // Do not claim the argument so that the use of the argument does not
12910b57cec5SDimitry Andric       // silently go unnoticed on toolchains which do not honour the option.
12920b57cec5SDimitry Andric       continue;
1293a7dea167SDimitry Andric     } else if (A->getOption().matches(options::OPT_stdlibxx_isystem)) {
1294a7dea167SDimitry Andric       // Translated to -internal-isystem by the driver, no need to pass to cc1.
1295a7dea167SDimitry Andric       continue;
12960b57cec5SDimitry Andric     }
12970b57cec5SDimitry Andric 
12980b57cec5SDimitry Andric     // Not translated, render as usual.
12990b57cec5SDimitry Andric     A->claim();
13000b57cec5SDimitry Andric     A->render(Args, CmdArgs);
13010b57cec5SDimitry Andric   }
13020b57cec5SDimitry Andric 
13030b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs,
13040b57cec5SDimitry Andric                   {options::OPT_D, options::OPT_U, options::OPT_I_Group,
13050b57cec5SDimitry Andric                    options::OPT_F, options::OPT_index_header_map});
13060b57cec5SDimitry Andric 
13070b57cec5SDimitry Andric   // Add -Wp, and -Xpreprocessor if using the preprocessor.
13080b57cec5SDimitry Andric 
13090b57cec5SDimitry Andric   // FIXME: There is a very unfortunate problem here, some troubled
13100b57cec5SDimitry Andric   // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
13110b57cec5SDimitry Andric   // really support that we would have to parse and then translate
13120b57cec5SDimitry Andric   // those options. :(
13130b57cec5SDimitry Andric   Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
13140b57cec5SDimitry Andric                        options::OPT_Xpreprocessor);
13150b57cec5SDimitry Andric 
13160b57cec5SDimitry Andric   // -I- is a deprecated GCC feature, reject it.
13170b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_I_))
13180b57cec5SDimitry Andric     D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
13190b57cec5SDimitry Andric 
13200b57cec5SDimitry Andric   // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
13210b57cec5SDimitry Andric   // -isysroot to the CC1 invocation.
13220b57cec5SDimitry Andric   StringRef sysroot = C.getSysRoot();
13230b57cec5SDimitry Andric   if (sysroot != "") {
13240b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_isysroot)) {
13250b57cec5SDimitry Andric       CmdArgs.push_back("-isysroot");
13260b57cec5SDimitry Andric       CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
13270b57cec5SDimitry Andric     }
13280b57cec5SDimitry Andric   }
13290b57cec5SDimitry Andric 
13300b57cec5SDimitry Andric   // Parse additional include paths from environment variables.
13310b57cec5SDimitry Andric   // FIXME: We should probably sink the logic for handling these from the
13320b57cec5SDimitry Andric   // frontend into the driver. It will allow deleting 4 otherwise unused flags.
13330b57cec5SDimitry Andric   // CPATH - included following the user specified includes (but prior to
13340b57cec5SDimitry Andric   // builtin and standard includes).
13350b57cec5SDimitry Andric   addDirectoryList(Args, CmdArgs, "-I", "CPATH");
13360b57cec5SDimitry Andric   // C_INCLUDE_PATH - system includes enabled when compiling C.
13370b57cec5SDimitry Andric   addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
13380b57cec5SDimitry Andric   // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
13390b57cec5SDimitry Andric   addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
13400b57cec5SDimitry Andric   // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
13410b57cec5SDimitry Andric   addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
13420b57cec5SDimitry Andric   // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
13430b57cec5SDimitry Andric   addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
13440b57cec5SDimitry Andric 
13450b57cec5SDimitry Andric   // While adding the include arguments, we also attempt to retrieve the
13460b57cec5SDimitry Andric   // arguments of related offloading toolchains or arguments that are specific
13470b57cec5SDimitry Andric   // of an offloading programming model.
13480b57cec5SDimitry Andric 
13490b57cec5SDimitry Andric   // Add C++ include arguments, if needed.
1350a7dea167SDimitry Andric   if (types::isCXX(Inputs[0].getType())) {
1351a7dea167SDimitry Andric     bool HasStdlibxxIsystem = Args.hasArg(options::OPT_stdlibxx_isystem);
1352a7dea167SDimitry Andric     forAllAssociatedToolChains(
1353a7dea167SDimitry Andric         C, JA, getToolChain(),
1354a7dea167SDimitry Andric         [&Args, &CmdArgs, HasStdlibxxIsystem](const ToolChain &TC) {
1355a7dea167SDimitry Andric           HasStdlibxxIsystem ? TC.AddClangCXXStdlibIsystemArgs(Args, CmdArgs)
1356a7dea167SDimitry Andric                              : TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
13570b57cec5SDimitry Andric         });
1358a7dea167SDimitry Andric   }
13590b57cec5SDimitry Andric 
13600b57cec5SDimitry Andric   // Add system include arguments for all targets but IAMCU.
13610b57cec5SDimitry Andric   if (!IsIAMCU)
13620b57cec5SDimitry Andric     forAllAssociatedToolChains(C, JA, getToolChain(),
13630b57cec5SDimitry Andric                                [&Args, &CmdArgs](const ToolChain &TC) {
13640b57cec5SDimitry Andric                                  TC.AddClangSystemIncludeArgs(Args, CmdArgs);
13650b57cec5SDimitry Andric                                });
13660b57cec5SDimitry Andric   else {
13670b57cec5SDimitry Andric     // For IAMCU add special include arguments.
13680b57cec5SDimitry Andric     getToolChain().AddIAMCUIncludeArgs(Args, CmdArgs);
13690b57cec5SDimitry Andric   }
1370*480093f4SDimitry Andric 
1371*480093f4SDimitry Andric   addMacroPrefixMapArg(D, Args, CmdArgs);
13720b57cec5SDimitry Andric }
13730b57cec5SDimitry Andric 
13740b57cec5SDimitry Andric // FIXME: Move to target hook.
13750b57cec5SDimitry Andric static bool isSignedCharDefault(const llvm::Triple &Triple) {
13760b57cec5SDimitry Andric   switch (Triple.getArch()) {
13770b57cec5SDimitry Andric   default:
13780b57cec5SDimitry Andric     return true;
13790b57cec5SDimitry Andric 
13800b57cec5SDimitry Andric   case llvm::Triple::aarch64:
1381*480093f4SDimitry Andric   case llvm::Triple::aarch64_32:
13820b57cec5SDimitry Andric   case llvm::Triple::aarch64_be:
13830b57cec5SDimitry Andric   case llvm::Triple::arm:
13840b57cec5SDimitry Andric   case llvm::Triple::armeb:
13850b57cec5SDimitry Andric   case llvm::Triple::thumb:
13860b57cec5SDimitry Andric   case llvm::Triple::thumbeb:
13870b57cec5SDimitry Andric     if (Triple.isOSDarwin() || Triple.isOSWindows())
13880b57cec5SDimitry Andric       return true;
13890b57cec5SDimitry Andric     return false;
13900b57cec5SDimitry Andric 
13910b57cec5SDimitry Andric   case llvm::Triple::ppc:
13920b57cec5SDimitry Andric   case llvm::Triple::ppc64:
13930b57cec5SDimitry Andric     if (Triple.isOSDarwin())
13940b57cec5SDimitry Andric       return true;
13950b57cec5SDimitry Andric     return false;
13960b57cec5SDimitry Andric 
13970b57cec5SDimitry Andric   case llvm::Triple::hexagon:
13980b57cec5SDimitry Andric   case llvm::Triple::ppc64le:
13990b57cec5SDimitry Andric   case llvm::Triple::riscv32:
14000b57cec5SDimitry Andric   case llvm::Triple::riscv64:
14010b57cec5SDimitry Andric   case llvm::Triple::systemz:
14020b57cec5SDimitry Andric   case llvm::Triple::xcore:
14030b57cec5SDimitry Andric     return false;
14040b57cec5SDimitry Andric   }
14050b57cec5SDimitry Andric }
14060b57cec5SDimitry Andric 
14070b57cec5SDimitry Andric static bool isNoCommonDefault(const llvm::Triple &Triple) {
14080b57cec5SDimitry Andric   switch (Triple.getArch()) {
14090b57cec5SDimitry Andric   default:
14100b57cec5SDimitry Andric     if (Triple.isOSFuchsia())
14110b57cec5SDimitry Andric       return true;
14120b57cec5SDimitry Andric     return false;
14130b57cec5SDimitry Andric 
14140b57cec5SDimitry Andric   case llvm::Triple::xcore:
14150b57cec5SDimitry Andric   case llvm::Triple::wasm32:
14160b57cec5SDimitry Andric   case llvm::Triple::wasm64:
14170b57cec5SDimitry Andric     return true;
14180b57cec5SDimitry Andric   }
14190b57cec5SDimitry Andric }
14200b57cec5SDimitry Andric 
1421*480093f4SDimitry Andric static bool hasMultipleInvocations(const llvm::Triple &Triple,
1422*480093f4SDimitry Andric                                    const ArgList &Args) {
1423*480093f4SDimitry Andric   // Supported only on Darwin where we invoke the compiler multiple times
1424*480093f4SDimitry Andric   // followed by an invocation to lipo.
1425*480093f4SDimitry Andric   if (!Triple.isOSDarwin())
1426*480093f4SDimitry Andric     return false;
1427*480093f4SDimitry Andric   // If more than one "-arch <arch>" is specified, we're targeting multiple
1428*480093f4SDimitry Andric   // architectures resulting in a fat binary.
1429*480093f4SDimitry Andric   return Args.getAllArgValues(options::OPT_arch).size() > 1;
1430*480093f4SDimitry Andric }
1431*480093f4SDimitry Andric 
1432*480093f4SDimitry Andric static bool checkRemarksOptions(const Driver &D, const ArgList &Args,
1433*480093f4SDimitry Andric                                 const llvm::Triple &Triple) {
1434*480093f4SDimitry Andric   // When enabling remarks, we need to error if:
1435*480093f4SDimitry Andric   // * The remark file is specified but we're targeting multiple architectures,
1436*480093f4SDimitry Andric   // which means more than one remark file is being generated.
1437*480093f4SDimitry Andric   bool hasMultipleInvocations = ::hasMultipleInvocations(Triple, Args);
1438*480093f4SDimitry Andric   bool hasExplicitOutputFile =
1439*480093f4SDimitry Andric       Args.getLastArg(options::OPT_foptimization_record_file_EQ);
1440*480093f4SDimitry Andric   if (hasMultipleInvocations && hasExplicitOutputFile) {
1441*480093f4SDimitry Andric     D.Diag(diag::err_drv_invalid_output_with_multiple_archs)
1442*480093f4SDimitry Andric         << "-foptimization-record-file";
1443*480093f4SDimitry Andric     return false;
1444*480093f4SDimitry Andric   }
1445*480093f4SDimitry Andric   return true;
1446*480093f4SDimitry Andric }
1447*480093f4SDimitry Andric 
1448*480093f4SDimitry Andric static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
1449*480093f4SDimitry Andric                                  const llvm::Triple &Triple,
1450*480093f4SDimitry Andric                                  const InputInfo &Input,
1451*480093f4SDimitry Andric                                  const InputInfo &Output, const JobAction &JA) {
1452*480093f4SDimitry Andric   StringRef Format = "yaml";
1453*480093f4SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fsave_optimization_record_EQ))
1454*480093f4SDimitry Andric     Format = A->getValue();
1455*480093f4SDimitry Andric 
1456*480093f4SDimitry Andric   CmdArgs.push_back("-opt-record-file");
1457*480093f4SDimitry Andric 
1458*480093f4SDimitry Andric   const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ);
1459*480093f4SDimitry Andric   if (A) {
1460*480093f4SDimitry Andric     CmdArgs.push_back(A->getValue());
1461*480093f4SDimitry Andric   } else {
1462*480093f4SDimitry Andric     bool hasMultipleArchs =
1463*480093f4SDimitry Andric         Triple.isOSDarwin() && // Only supported on Darwin platforms.
1464*480093f4SDimitry Andric         Args.getAllArgValues(options::OPT_arch).size() > 1;
1465*480093f4SDimitry Andric 
1466*480093f4SDimitry Andric     SmallString<128> F;
1467*480093f4SDimitry Andric 
1468*480093f4SDimitry Andric     if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) {
1469*480093f4SDimitry Andric       if (Arg *FinalOutput = Args.getLastArg(options::OPT_o))
1470*480093f4SDimitry Andric         F = FinalOutput->getValue();
1471*480093f4SDimitry Andric     } else {
1472*480093f4SDimitry Andric       if (Format != "yaml" && // For YAML, keep the original behavior.
1473*480093f4SDimitry Andric           Triple.isOSDarwin() && // Enable this only on darwin, since it's the only platform supporting .dSYM bundles.
1474*480093f4SDimitry Andric           Output.isFilename())
1475*480093f4SDimitry Andric         F = Output.getFilename();
1476*480093f4SDimitry Andric     }
1477*480093f4SDimitry Andric 
1478*480093f4SDimitry Andric     if (F.empty()) {
1479*480093f4SDimitry Andric       // Use the input filename.
1480*480093f4SDimitry Andric       F = llvm::sys::path::stem(Input.getBaseInput());
1481*480093f4SDimitry Andric 
1482*480093f4SDimitry Andric       // If we're compiling for an offload architecture (i.e. a CUDA device),
1483*480093f4SDimitry Andric       // we need to make the file name for the device compilation different
1484*480093f4SDimitry Andric       // from the host compilation.
1485*480093f4SDimitry Andric       if (!JA.isDeviceOffloading(Action::OFK_None) &&
1486*480093f4SDimitry Andric           !JA.isDeviceOffloading(Action::OFK_Host)) {
1487*480093f4SDimitry Andric         llvm::sys::path::replace_extension(F, "");
1488*480093f4SDimitry Andric         F += Action::GetOffloadingFileNamePrefix(JA.getOffloadingDeviceKind(),
1489*480093f4SDimitry Andric                                                  Triple.normalize());
1490*480093f4SDimitry Andric         F += "-";
1491*480093f4SDimitry Andric         F += JA.getOffloadingArch();
1492*480093f4SDimitry Andric       }
1493*480093f4SDimitry Andric     }
1494*480093f4SDimitry Andric 
1495*480093f4SDimitry Andric     // If we're having more than one "-arch", we should name the files
1496*480093f4SDimitry Andric     // differently so that every cc1 invocation writes to a different file.
1497*480093f4SDimitry Andric     // We're doing that by appending "-<arch>" with "<arch>" being the arch
1498*480093f4SDimitry Andric     // name from the triple.
1499*480093f4SDimitry Andric     if (hasMultipleArchs) {
1500*480093f4SDimitry Andric       // First, remember the extension.
1501*480093f4SDimitry Andric       SmallString<64> OldExtension = llvm::sys::path::extension(F);
1502*480093f4SDimitry Andric       // then, remove it.
1503*480093f4SDimitry Andric       llvm::sys::path::replace_extension(F, "");
1504*480093f4SDimitry Andric       // attach -<arch> to it.
1505*480093f4SDimitry Andric       F += "-";
1506*480093f4SDimitry Andric       F += Triple.getArchName();
1507*480093f4SDimitry Andric       // put back the extension.
1508*480093f4SDimitry Andric       llvm::sys::path::replace_extension(F, OldExtension);
1509*480093f4SDimitry Andric     }
1510*480093f4SDimitry Andric 
1511*480093f4SDimitry Andric     SmallString<32> Extension;
1512*480093f4SDimitry Andric     Extension += "opt.";
1513*480093f4SDimitry Andric     Extension += Format;
1514*480093f4SDimitry Andric 
1515*480093f4SDimitry Andric     llvm::sys::path::replace_extension(F, Extension);
1516*480093f4SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(F));
1517*480093f4SDimitry Andric   }
1518*480093f4SDimitry Andric 
1519*480093f4SDimitry Andric   if (const Arg *A =
1520*480093f4SDimitry Andric           Args.getLastArg(options::OPT_foptimization_record_passes_EQ)) {
1521*480093f4SDimitry Andric     CmdArgs.push_back("-opt-record-passes");
1522*480093f4SDimitry Andric     CmdArgs.push_back(A->getValue());
1523*480093f4SDimitry Andric   }
1524*480093f4SDimitry Andric 
1525*480093f4SDimitry Andric   if (!Format.empty()) {
1526*480093f4SDimitry Andric     CmdArgs.push_back("-opt-record-format");
1527*480093f4SDimitry Andric     CmdArgs.push_back(Format.data());
1528*480093f4SDimitry Andric   }
1529*480093f4SDimitry Andric }
1530*480093f4SDimitry Andric 
15310b57cec5SDimitry Andric namespace {
15320b57cec5SDimitry Andric void RenderARMABI(const llvm::Triple &Triple, const ArgList &Args,
15330b57cec5SDimitry Andric                   ArgStringList &CmdArgs) {
15340b57cec5SDimitry Andric   // Select the ABI to use.
15350b57cec5SDimitry Andric   // FIXME: Support -meabi.
15360b57cec5SDimitry Andric   // FIXME: Parts of this are duplicated in the backend, unify this somehow.
15370b57cec5SDimitry Andric   const char *ABIName = nullptr;
15380b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
15390b57cec5SDimitry Andric     ABIName = A->getValue();
15400b57cec5SDimitry Andric   } else {
15410b57cec5SDimitry Andric     std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false);
15420b57cec5SDimitry Andric     ABIName = llvm::ARM::computeDefaultTargetABI(Triple, CPU).data();
15430b57cec5SDimitry Andric   }
15440b57cec5SDimitry Andric 
15450b57cec5SDimitry Andric   CmdArgs.push_back("-target-abi");
15460b57cec5SDimitry Andric   CmdArgs.push_back(ABIName);
15470b57cec5SDimitry Andric }
15480b57cec5SDimitry Andric }
15490b57cec5SDimitry Andric 
15500b57cec5SDimitry Andric void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args,
15510b57cec5SDimitry Andric                              ArgStringList &CmdArgs, bool KernelOrKext) const {
15520b57cec5SDimitry Andric   RenderARMABI(Triple, Args, CmdArgs);
15530b57cec5SDimitry Andric 
15540b57cec5SDimitry Andric   // Determine floating point ABI from the options & target defaults.
15550b57cec5SDimitry Andric   arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args);
15560b57cec5SDimitry Andric   if (ABI == arm::FloatABI::Soft) {
15570b57cec5SDimitry Andric     // Floating point operations and argument passing are soft.
15580b57cec5SDimitry Andric     // FIXME: This changes CPP defines, we need -target-soft-float.
15590b57cec5SDimitry Andric     CmdArgs.push_back("-msoft-float");
15600b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
15610b57cec5SDimitry Andric     CmdArgs.push_back("soft");
15620b57cec5SDimitry Andric   } else if (ABI == arm::FloatABI::SoftFP) {
15630b57cec5SDimitry Andric     // Floating point operations are hard, but argument passing is soft.
15640b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
15650b57cec5SDimitry Andric     CmdArgs.push_back("soft");
15660b57cec5SDimitry Andric   } else {
15670b57cec5SDimitry Andric     // Floating point operations and argument passing are hard.
15680b57cec5SDimitry Andric     assert(ABI == arm::FloatABI::Hard && "Invalid float abi!");
15690b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
15700b57cec5SDimitry Andric     CmdArgs.push_back("hard");
15710b57cec5SDimitry Andric   }
15720b57cec5SDimitry Andric 
15730b57cec5SDimitry Andric   // Forward the -mglobal-merge option for explicit control over the pass.
15740b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
15750b57cec5SDimitry Andric                                options::OPT_mno_global_merge)) {
15760b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
15770b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mno_global_merge))
15780b57cec5SDimitry Andric       CmdArgs.push_back("-arm-global-merge=false");
15790b57cec5SDimitry Andric     else
15800b57cec5SDimitry Andric       CmdArgs.push_back("-arm-global-merge=true");
15810b57cec5SDimitry Andric   }
15820b57cec5SDimitry Andric 
15830b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_mimplicit_float,
15840b57cec5SDimitry Andric                     options::OPT_mno_implicit_float, true))
15850b57cec5SDimitry Andric     CmdArgs.push_back("-no-implicit-float");
15860b57cec5SDimitry Andric 
15870b57cec5SDimitry Andric   if (Args.getLastArg(options::OPT_mcmse))
15880b57cec5SDimitry Andric     CmdArgs.push_back("-mcmse");
15890b57cec5SDimitry Andric }
15900b57cec5SDimitry Andric 
15910b57cec5SDimitry Andric void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple,
15920b57cec5SDimitry Andric                                 const ArgList &Args, bool KernelOrKext,
15930b57cec5SDimitry Andric                                 ArgStringList &CmdArgs) const {
15940b57cec5SDimitry Andric   const ToolChain &TC = getToolChain();
15950b57cec5SDimitry Andric 
15960b57cec5SDimitry Andric   // Add the target features
15970b57cec5SDimitry Andric   getTargetFeatures(TC, EffectiveTriple, Args, CmdArgs, false);
15980b57cec5SDimitry Andric 
15990b57cec5SDimitry Andric   // Add target specific flags.
16000b57cec5SDimitry Andric   switch (TC.getArch()) {
16010b57cec5SDimitry Andric   default:
16020b57cec5SDimitry Andric     break;
16030b57cec5SDimitry Andric 
16040b57cec5SDimitry Andric   case llvm::Triple::arm:
16050b57cec5SDimitry Andric   case llvm::Triple::armeb:
16060b57cec5SDimitry Andric   case llvm::Triple::thumb:
16070b57cec5SDimitry Andric   case llvm::Triple::thumbeb:
16080b57cec5SDimitry Andric     // Use the effective triple, which takes into account the deployment target.
16090b57cec5SDimitry Andric     AddARMTargetArgs(EffectiveTriple, Args, CmdArgs, KernelOrKext);
16100b57cec5SDimitry Andric     CmdArgs.push_back("-fallow-half-arguments-and-returns");
16110b57cec5SDimitry Andric     break;
16120b57cec5SDimitry Andric 
16130b57cec5SDimitry Andric   case llvm::Triple::aarch64:
1614*480093f4SDimitry Andric   case llvm::Triple::aarch64_32:
16150b57cec5SDimitry Andric   case llvm::Triple::aarch64_be:
16160b57cec5SDimitry Andric     AddAArch64TargetArgs(Args, CmdArgs);
16170b57cec5SDimitry Andric     CmdArgs.push_back("-fallow-half-arguments-and-returns");
16180b57cec5SDimitry Andric     break;
16190b57cec5SDimitry Andric 
16200b57cec5SDimitry Andric   case llvm::Triple::mips:
16210b57cec5SDimitry Andric   case llvm::Triple::mipsel:
16220b57cec5SDimitry Andric   case llvm::Triple::mips64:
16230b57cec5SDimitry Andric   case llvm::Triple::mips64el:
16240b57cec5SDimitry Andric     AddMIPSTargetArgs(Args, CmdArgs);
16250b57cec5SDimitry Andric     break;
16260b57cec5SDimitry Andric 
16270b57cec5SDimitry Andric   case llvm::Triple::ppc:
16280b57cec5SDimitry Andric   case llvm::Triple::ppc64:
16290b57cec5SDimitry Andric   case llvm::Triple::ppc64le:
16300b57cec5SDimitry Andric     AddPPCTargetArgs(Args, CmdArgs);
16310b57cec5SDimitry Andric     break;
16320b57cec5SDimitry Andric 
16330b57cec5SDimitry Andric   case llvm::Triple::riscv32:
16340b57cec5SDimitry Andric   case llvm::Triple::riscv64:
16350b57cec5SDimitry Andric     AddRISCVTargetArgs(Args, CmdArgs);
16360b57cec5SDimitry Andric     break;
16370b57cec5SDimitry Andric 
16380b57cec5SDimitry Andric   case llvm::Triple::sparc:
16390b57cec5SDimitry Andric   case llvm::Triple::sparcel:
16400b57cec5SDimitry Andric   case llvm::Triple::sparcv9:
16410b57cec5SDimitry Andric     AddSparcTargetArgs(Args, CmdArgs);
16420b57cec5SDimitry Andric     break;
16430b57cec5SDimitry Andric 
16440b57cec5SDimitry Andric   case llvm::Triple::systemz:
16450b57cec5SDimitry Andric     AddSystemZTargetArgs(Args, CmdArgs);
16460b57cec5SDimitry Andric     break;
16470b57cec5SDimitry Andric 
16480b57cec5SDimitry Andric   case llvm::Triple::x86:
16490b57cec5SDimitry Andric   case llvm::Triple::x86_64:
16500b57cec5SDimitry Andric     AddX86TargetArgs(Args, CmdArgs);
16510b57cec5SDimitry Andric     break;
16520b57cec5SDimitry Andric 
16530b57cec5SDimitry Andric   case llvm::Triple::lanai:
16540b57cec5SDimitry Andric     AddLanaiTargetArgs(Args, CmdArgs);
16550b57cec5SDimitry Andric     break;
16560b57cec5SDimitry Andric 
16570b57cec5SDimitry Andric   case llvm::Triple::hexagon:
16580b57cec5SDimitry Andric     AddHexagonTargetArgs(Args, CmdArgs);
16590b57cec5SDimitry Andric     break;
16600b57cec5SDimitry Andric 
16610b57cec5SDimitry Andric   case llvm::Triple::wasm32:
16620b57cec5SDimitry Andric   case llvm::Triple::wasm64:
16630b57cec5SDimitry Andric     AddWebAssemblyTargetArgs(Args, CmdArgs);
16640b57cec5SDimitry Andric     break;
16650b57cec5SDimitry Andric   }
16660b57cec5SDimitry Andric }
16670b57cec5SDimitry Andric 
16680b57cec5SDimitry Andric namespace {
16690b57cec5SDimitry Andric void RenderAArch64ABI(const llvm::Triple &Triple, const ArgList &Args,
16700b57cec5SDimitry Andric                       ArgStringList &CmdArgs) {
16710b57cec5SDimitry Andric   const char *ABIName = nullptr;
16720b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
16730b57cec5SDimitry Andric     ABIName = A->getValue();
16740b57cec5SDimitry Andric   else if (Triple.isOSDarwin())
16750b57cec5SDimitry Andric     ABIName = "darwinpcs";
16760b57cec5SDimitry Andric   else
16770b57cec5SDimitry Andric     ABIName = "aapcs";
16780b57cec5SDimitry Andric 
16790b57cec5SDimitry Andric   CmdArgs.push_back("-target-abi");
16800b57cec5SDimitry Andric   CmdArgs.push_back(ABIName);
16810b57cec5SDimitry Andric }
16820b57cec5SDimitry Andric }
16830b57cec5SDimitry Andric 
16840b57cec5SDimitry Andric void Clang::AddAArch64TargetArgs(const ArgList &Args,
16850b57cec5SDimitry Andric                                  ArgStringList &CmdArgs) const {
16860b57cec5SDimitry Andric   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
16870b57cec5SDimitry Andric 
16880b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
16890b57cec5SDimitry Andric       Args.hasArg(options::OPT_mkernel) ||
16900b57cec5SDimitry Andric       Args.hasArg(options::OPT_fapple_kext))
16910b57cec5SDimitry Andric     CmdArgs.push_back("-disable-red-zone");
16920b57cec5SDimitry Andric 
16930b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_mimplicit_float,
16940b57cec5SDimitry Andric                     options::OPT_mno_implicit_float, true))
16950b57cec5SDimitry Andric     CmdArgs.push_back("-no-implicit-float");
16960b57cec5SDimitry Andric 
16970b57cec5SDimitry Andric   RenderAArch64ABI(Triple, Args, CmdArgs);
16980b57cec5SDimitry Andric 
16990b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a53_835769,
17000b57cec5SDimitry Andric                                options::OPT_mno_fix_cortex_a53_835769)) {
17010b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
17020b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mfix_cortex_a53_835769))
17030b57cec5SDimitry Andric       CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
17040b57cec5SDimitry Andric     else
17050b57cec5SDimitry Andric       CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=0");
17060b57cec5SDimitry Andric   } else if (Triple.isAndroid()) {
17070b57cec5SDimitry Andric     // Enabled A53 errata (835769) workaround by default on android
17080b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
17090b57cec5SDimitry Andric     CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
17100b57cec5SDimitry Andric   }
17110b57cec5SDimitry Andric 
17120b57cec5SDimitry Andric   // Forward the -mglobal-merge option for explicit control over the pass.
17130b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
17140b57cec5SDimitry Andric                                options::OPT_mno_global_merge)) {
17150b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
17160b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mno_global_merge))
17170b57cec5SDimitry Andric       CmdArgs.push_back("-aarch64-enable-global-merge=false");
17180b57cec5SDimitry Andric     else
17190b57cec5SDimitry Andric       CmdArgs.push_back("-aarch64-enable-global-merge=true");
17200b57cec5SDimitry Andric   }
17210b57cec5SDimitry Andric 
17220b57cec5SDimitry Andric   // Enable/disable return address signing and indirect branch targets.
17230b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_msign_return_address_EQ,
17240b57cec5SDimitry Andric                                options::OPT_mbranch_protection_EQ)) {
17250b57cec5SDimitry Andric 
17260b57cec5SDimitry Andric     const Driver &D = getToolChain().getDriver();
17270b57cec5SDimitry Andric 
17280b57cec5SDimitry Andric     StringRef Scope, Key;
17290b57cec5SDimitry Andric     bool IndirectBranches;
17300b57cec5SDimitry Andric 
17310b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_msign_return_address_EQ)) {
17320b57cec5SDimitry Andric       Scope = A->getValue();
17330b57cec5SDimitry Andric       if (!Scope.equals("none") && !Scope.equals("non-leaf") &&
17340b57cec5SDimitry Andric           !Scope.equals("all"))
17350b57cec5SDimitry Andric         D.Diag(diag::err_invalid_branch_protection)
17360b57cec5SDimitry Andric             << Scope << A->getAsString(Args);
17370b57cec5SDimitry Andric       Key = "a_key";
17380b57cec5SDimitry Andric       IndirectBranches = false;
1739*480093f4SDimitry Andric     } else {
1740*480093f4SDimitry Andric       StringRef Err;
1741*480093f4SDimitry Andric       llvm::AArch64::ParsedBranchProtection PBP;
1742*480093f4SDimitry Andric       if (!llvm::AArch64::parseBranchProtection(A->getValue(), PBP, Err))
1743*480093f4SDimitry Andric         D.Diag(diag::err_invalid_branch_protection)
1744*480093f4SDimitry Andric             << Err << A->getAsString(Args);
1745*480093f4SDimitry Andric       Scope = PBP.Scope;
1746*480093f4SDimitry Andric       Key = PBP.Key;
1747*480093f4SDimitry Andric       IndirectBranches = PBP.BranchTargetEnforcement;
1748*480093f4SDimitry Andric     }
17490b57cec5SDimitry Andric 
17500b57cec5SDimitry Andric     CmdArgs.push_back(
17510b57cec5SDimitry Andric         Args.MakeArgString(Twine("-msign-return-address=") + Scope));
17520b57cec5SDimitry Andric     CmdArgs.push_back(
17530b57cec5SDimitry Andric         Args.MakeArgString(Twine("-msign-return-address-key=") + Key));
17540b57cec5SDimitry Andric     if (IndirectBranches)
17550b57cec5SDimitry Andric       CmdArgs.push_back("-mbranch-target-enforce");
17560b57cec5SDimitry Andric   }
17570b57cec5SDimitry Andric }
17580b57cec5SDimitry Andric 
17590b57cec5SDimitry Andric void Clang::AddMIPSTargetArgs(const ArgList &Args,
17600b57cec5SDimitry Andric                               ArgStringList &CmdArgs) const {
17610b57cec5SDimitry Andric   const Driver &D = getToolChain().getDriver();
17620b57cec5SDimitry Andric   StringRef CPUName;
17630b57cec5SDimitry Andric   StringRef ABIName;
17640b57cec5SDimitry Andric   const llvm::Triple &Triple = getToolChain().getTriple();
17650b57cec5SDimitry Andric   mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
17660b57cec5SDimitry Andric 
17670b57cec5SDimitry Andric   CmdArgs.push_back("-target-abi");
17680b57cec5SDimitry Andric   CmdArgs.push_back(ABIName.data());
17690b57cec5SDimitry Andric 
17700b57cec5SDimitry Andric   mips::FloatABI ABI = mips::getMipsFloatABI(D, Args, Triple);
17710b57cec5SDimitry Andric   if (ABI == mips::FloatABI::Soft) {
17720b57cec5SDimitry Andric     // Floating point operations and argument passing are soft.
17730b57cec5SDimitry Andric     CmdArgs.push_back("-msoft-float");
17740b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
17750b57cec5SDimitry Andric     CmdArgs.push_back("soft");
17760b57cec5SDimitry Andric   } else {
17770b57cec5SDimitry Andric     // Floating point operations and argument passing are hard.
17780b57cec5SDimitry Andric     assert(ABI == mips::FloatABI::Hard && "Invalid float abi!");
17790b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
17800b57cec5SDimitry Andric     CmdArgs.push_back("hard");
17810b57cec5SDimitry Andric   }
17820b57cec5SDimitry Andric 
17830b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1,
17840b57cec5SDimitry Andric                                options::OPT_mno_ldc1_sdc1)) {
17850b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) {
17860b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
17870b57cec5SDimitry Andric       CmdArgs.push_back("-mno-ldc1-sdc1");
17880b57cec5SDimitry Andric     }
17890b57cec5SDimitry Andric   }
17900b57cec5SDimitry Andric 
17910b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division,
17920b57cec5SDimitry Andric                                options::OPT_mno_check_zero_division)) {
17930b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mno_check_zero_division)) {
17940b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
17950b57cec5SDimitry Andric       CmdArgs.push_back("-mno-check-zero-division");
17960b57cec5SDimitry Andric     }
17970b57cec5SDimitry Andric   }
17980b57cec5SDimitry Andric 
17990b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_G)) {
18000b57cec5SDimitry Andric     StringRef v = A->getValue();
18010b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
18020b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
18030b57cec5SDimitry Andric     A->claim();
18040b57cec5SDimitry Andric   }
18050b57cec5SDimitry Andric 
18060b57cec5SDimitry Andric   Arg *GPOpt = Args.getLastArg(options::OPT_mgpopt, options::OPT_mno_gpopt);
18070b57cec5SDimitry Andric   Arg *ABICalls =
18080b57cec5SDimitry Andric       Args.getLastArg(options::OPT_mabicalls, options::OPT_mno_abicalls);
18090b57cec5SDimitry Andric 
18100b57cec5SDimitry Andric   // -mabicalls is the default for many MIPS environments, even with -fno-pic.
18110b57cec5SDimitry Andric   // -mgpopt is the default for static, -fno-pic environments but these two
18120b57cec5SDimitry Andric   // options conflict. We want to be certain that -mno-abicalls -mgpopt is
18130b57cec5SDimitry Andric   // the only case where -mllvm -mgpopt is passed.
18140b57cec5SDimitry Andric   // NOTE: We need a warning here or in the backend to warn when -mgpopt is
18150b57cec5SDimitry Andric   //       passed explicitly when compiling something with -mabicalls
18160b57cec5SDimitry Andric   //       (implictly) in affect. Currently the warning is in the backend.
18170b57cec5SDimitry Andric   //
18180b57cec5SDimitry Andric   // When the ABI in use is  N64, we also need to determine the PIC mode that
18190b57cec5SDimitry Andric   // is in use, as -fno-pic for N64 implies -mno-abicalls.
18200b57cec5SDimitry Andric   bool NoABICalls =
18210b57cec5SDimitry Andric       ABICalls && ABICalls->getOption().matches(options::OPT_mno_abicalls);
18220b57cec5SDimitry Andric 
18230b57cec5SDimitry Andric   llvm::Reloc::Model RelocationModel;
18240b57cec5SDimitry Andric   unsigned PICLevel;
18250b57cec5SDimitry Andric   bool IsPIE;
18260b57cec5SDimitry Andric   std::tie(RelocationModel, PICLevel, IsPIE) =
18270b57cec5SDimitry Andric       ParsePICArgs(getToolChain(), Args);
18280b57cec5SDimitry Andric 
18290b57cec5SDimitry Andric   NoABICalls = NoABICalls ||
18300b57cec5SDimitry Andric                (RelocationModel == llvm::Reloc::Static && ABIName == "n64");
18310b57cec5SDimitry Andric 
18320b57cec5SDimitry Andric   bool WantGPOpt = GPOpt && GPOpt->getOption().matches(options::OPT_mgpopt);
18330b57cec5SDimitry Andric   // We quietly ignore -mno-gpopt as the backend defaults to -mno-gpopt.
18340b57cec5SDimitry Andric   if (NoABICalls && (!GPOpt || WantGPOpt)) {
18350b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
18360b57cec5SDimitry Andric     CmdArgs.push_back("-mgpopt");
18370b57cec5SDimitry Andric 
18380b57cec5SDimitry Andric     Arg *LocalSData = Args.getLastArg(options::OPT_mlocal_sdata,
18390b57cec5SDimitry Andric                                       options::OPT_mno_local_sdata);
18400b57cec5SDimitry Andric     Arg *ExternSData = Args.getLastArg(options::OPT_mextern_sdata,
18410b57cec5SDimitry Andric                                        options::OPT_mno_extern_sdata);
18420b57cec5SDimitry Andric     Arg *EmbeddedData = Args.getLastArg(options::OPT_membedded_data,
18430b57cec5SDimitry Andric                                         options::OPT_mno_embedded_data);
18440b57cec5SDimitry Andric     if (LocalSData) {
18450b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
18460b57cec5SDimitry Andric       if (LocalSData->getOption().matches(options::OPT_mlocal_sdata)) {
18470b57cec5SDimitry Andric         CmdArgs.push_back("-mlocal-sdata=1");
18480b57cec5SDimitry Andric       } else {
18490b57cec5SDimitry Andric         CmdArgs.push_back("-mlocal-sdata=0");
18500b57cec5SDimitry Andric       }
18510b57cec5SDimitry Andric       LocalSData->claim();
18520b57cec5SDimitry Andric     }
18530b57cec5SDimitry Andric 
18540b57cec5SDimitry Andric     if (ExternSData) {
18550b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
18560b57cec5SDimitry Andric       if (ExternSData->getOption().matches(options::OPT_mextern_sdata)) {
18570b57cec5SDimitry Andric         CmdArgs.push_back("-mextern-sdata=1");
18580b57cec5SDimitry Andric       } else {
18590b57cec5SDimitry Andric         CmdArgs.push_back("-mextern-sdata=0");
18600b57cec5SDimitry Andric       }
18610b57cec5SDimitry Andric       ExternSData->claim();
18620b57cec5SDimitry Andric     }
18630b57cec5SDimitry Andric 
18640b57cec5SDimitry Andric     if (EmbeddedData) {
18650b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
18660b57cec5SDimitry Andric       if (EmbeddedData->getOption().matches(options::OPT_membedded_data)) {
18670b57cec5SDimitry Andric         CmdArgs.push_back("-membedded-data=1");
18680b57cec5SDimitry Andric       } else {
18690b57cec5SDimitry Andric         CmdArgs.push_back("-membedded-data=0");
18700b57cec5SDimitry Andric       }
18710b57cec5SDimitry Andric       EmbeddedData->claim();
18720b57cec5SDimitry Andric     }
18730b57cec5SDimitry Andric 
18740b57cec5SDimitry Andric   } else if ((!ABICalls || (!NoABICalls && ABICalls)) && WantGPOpt)
18750b57cec5SDimitry Andric     D.Diag(diag::warn_drv_unsupported_gpopt) << (ABICalls ? 0 : 1);
18760b57cec5SDimitry Andric 
18770b57cec5SDimitry Andric   if (GPOpt)
18780b57cec5SDimitry Andric     GPOpt->claim();
18790b57cec5SDimitry Andric 
18800b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mcompact_branches_EQ)) {
18810b57cec5SDimitry Andric     StringRef Val = StringRef(A->getValue());
18820b57cec5SDimitry Andric     if (mips::hasCompactBranches(CPUName)) {
18830b57cec5SDimitry Andric       if (Val == "never" || Val == "always" || Val == "optimal") {
18840b57cec5SDimitry Andric         CmdArgs.push_back("-mllvm");
18850b57cec5SDimitry Andric         CmdArgs.push_back(Args.MakeArgString("-mips-compact-branches=" + Val));
18860b57cec5SDimitry Andric       } else
18870b57cec5SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
18880b57cec5SDimitry Andric             << A->getOption().getName() << Val;
18890b57cec5SDimitry Andric     } else
18900b57cec5SDimitry Andric       D.Diag(diag::warn_target_unsupported_compact_branches) << CPUName;
18910b57cec5SDimitry Andric   }
18920b57cec5SDimitry Andric 
18930b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mrelax_pic_calls,
18940b57cec5SDimitry Andric                                options::OPT_mno_relax_pic_calls)) {
18950b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mno_relax_pic_calls)) {
18960b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
18970b57cec5SDimitry Andric       CmdArgs.push_back("-mips-jalr-reloc=0");
18980b57cec5SDimitry Andric     }
18990b57cec5SDimitry Andric   }
19000b57cec5SDimitry Andric }
19010b57cec5SDimitry Andric 
19020b57cec5SDimitry Andric void Clang::AddPPCTargetArgs(const ArgList &Args,
19030b57cec5SDimitry Andric                              ArgStringList &CmdArgs) const {
19040b57cec5SDimitry Andric   // Select the ABI to use.
19050b57cec5SDimitry Andric   const char *ABIName = nullptr;
1906*480093f4SDimitry Andric   const llvm::Triple &T = getToolChain().getTriple();
1907*480093f4SDimitry Andric   if (T.isOSBinFormatELF()) {
19080b57cec5SDimitry Andric     switch (getToolChain().getArch()) {
19090b57cec5SDimitry Andric     case llvm::Triple::ppc64: {
19100b57cec5SDimitry Andric       // When targeting a processor that supports QPX, or if QPX is
19110b57cec5SDimitry Andric       // specifically enabled, default to using the ABI that supports QPX (so
19120b57cec5SDimitry Andric       // long as it is not specifically disabled).
19130b57cec5SDimitry Andric       bool HasQPX = false;
19140b57cec5SDimitry Andric       if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
19150b57cec5SDimitry Andric         HasQPX = A->getValue() == StringRef("a2q");
19160b57cec5SDimitry Andric       HasQPX = Args.hasFlag(options::OPT_mqpx, options::OPT_mno_qpx, HasQPX);
19170b57cec5SDimitry Andric       if (HasQPX) {
19180b57cec5SDimitry Andric         ABIName = "elfv1-qpx";
19190b57cec5SDimitry Andric         break;
19200b57cec5SDimitry Andric       }
19210b57cec5SDimitry Andric 
1922*480093f4SDimitry Andric       if (T.isMusl() || (T.isOSFreeBSD() && T.getOSMajorVersion() >= 13))
1923*480093f4SDimitry Andric         ABIName = "elfv2";
1924*480093f4SDimitry Andric       else
19250b57cec5SDimitry Andric         ABIName = "elfv1";
19260b57cec5SDimitry Andric       break;
19270b57cec5SDimitry Andric     }
19280b57cec5SDimitry Andric     case llvm::Triple::ppc64le:
19290b57cec5SDimitry Andric       ABIName = "elfv2";
19300b57cec5SDimitry Andric       break;
19310b57cec5SDimitry Andric     default:
19320b57cec5SDimitry Andric       break;
19330b57cec5SDimitry Andric     }
1934*480093f4SDimitry Andric   }
19350b57cec5SDimitry Andric 
19360b57cec5SDimitry Andric   bool IEEELongDouble = false;
19370b57cec5SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_mabi_EQ)) {
19380b57cec5SDimitry Andric     StringRef V = A->getValue();
19390b57cec5SDimitry Andric     if (V == "ieeelongdouble")
19400b57cec5SDimitry Andric       IEEELongDouble = true;
19410b57cec5SDimitry Andric     else if (V == "ibmlongdouble")
19420b57cec5SDimitry Andric       IEEELongDouble = false;
19430b57cec5SDimitry Andric     else if (V != "altivec")
19440b57cec5SDimitry Andric       // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore
19450b57cec5SDimitry Andric       // the option if given as we don't have backend support for any targets
19460b57cec5SDimitry Andric       // that don't use the altivec abi.
19470b57cec5SDimitry Andric       ABIName = A->getValue();
19480b57cec5SDimitry Andric   }
19490b57cec5SDimitry Andric   if (IEEELongDouble)
19500b57cec5SDimitry Andric     CmdArgs.push_back("-mabi=ieeelongdouble");
19510b57cec5SDimitry Andric 
19520b57cec5SDimitry Andric   ppc::FloatABI FloatABI =
19530b57cec5SDimitry Andric       ppc::getPPCFloatABI(getToolChain().getDriver(), Args);
19540b57cec5SDimitry Andric 
19550b57cec5SDimitry Andric   if (FloatABI == ppc::FloatABI::Soft) {
19560b57cec5SDimitry Andric     // Floating point operations and argument passing are soft.
19570b57cec5SDimitry Andric     CmdArgs.push_back("-msoft-float");
19580b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
19590b57cec5SDimitry Andric     CmdArgs.push_back("soft");
19600b57cec5SDimitry Andric   } else {
19610b57cec5SDimitry Andric     // Floating point operations and argument passing are hard.
19620b57cec5SDimitry Andric     assert(FloatABI == ppc::FloatABI::Hard && "Invalid float abi!");
19630b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
19640b57cec5SDimitry Andric     CmdArgs.push_back("hard");
19650b57cec5SDimitry Andric   }
19660b57cec5SDimitry Andric 
19670b57cec5SDimitry Andric   if (ABIName) {
19680b57cec5SDimitry Andric     CmdArgs.push_back("-target-abi");
19690b57cec5SDimitry Andric     CmdArgs.push_back(ABIName);
19700b57cec5SDimitry Andric   }
19710b57cec5SDimitry Andric }
19720b57cec5SDimitry Andric 
19730b57cec5SDimitry Andric void Clang::AddRISCVTargetArgs(const ArgList &Args,
19740b57cec5SDimitry Andric                                ArgStringList &CmdArgs) const {
19750b57cec5SDimitry Andric   const llvm::Triple &Triple = getToolChain().getTriple();
1976a7dea167SDimitry Andric   StringRef ABIName = riscv::getRISCVABI(Args, Triple);
19770b57cec5SDimitry Andric 
19780b57cec5SDimitry Andric   CmdArgs.push_back("-target-abi");
1979a7dea167SDimitry Andric   CmdArgs.push_back(ABIName.data());
19800b57cec5SDimitry Andric }
19810b57cec5SDimitry Andric 
19820b57cec5SDimitry Andric void Clang::AddSparcTargetArgs(const ArgList &Args,
19830b57cec5SDimitry Andric                                ArgStringList &CmdArgs) const {
19840b57cec5SDimitry Andric   sparc::FloatABI FloatABI =
19850b57cec5SDimitry Andric       sparc::getSparcFloatABI(getToolChain().getDriver(), Args);
19860b57cec5SDimitry Andric 
19870b57cec5SDimitry Andric   if (FloatABI == sparc::FloatABI::Soft) {
19880b57cec5SDimitry Andric     // Floating point operations and argument passing are soft.
19890b57cec5SDimitry Andric     CmdArgs.push_back("-msoft-float");
19900b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
19910b57cec5SDimitry Andric     CmdArgs.push_back("soft");
19920b57cec5SDimitry Andric   } else {
19930b57cec5SDimitry Andric     // Floating point operations and argument passing are hard.
19940b57cec5SDimitry Andric     assert(FloatABI == sparc::FloatABI::Hard && "Invalid float abi!");
19950b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
19960b57cec5SDimitry Andric     CmdArgs.push_back("hard");
19970b57cec5SDimitry Andric   }
19980b57cec5SDimitry Andric }
19990b57cec5SDimitry Andric 
20000b57cec5SDimitry Andric void Clang::AddSystemZTargetArgs(const ArgList &Args,
20010b57cec5SDimitry Andric                                  ArgStringList &CmdArgs) const {
2002*480093f4SDimitry Andric   bool HasBackchain = Args.hasFlag(options::OPT_mbackchain,
2003*480093f4SDimitry Andric                                    options::OPT_mno_backchain, false);
2004*480093f4SDimitry Andric   bool HasPackedStack = Args.hasFlag(options::OPT_mpacked_stack,
2005*480093f4SDimitry Andric                                      options::OPT_mno_packed_stack, false);
2006*480093f4SDimitry Andric   if (HasBackchain && HasPackedStack) {
2007*480093f4SDimitry Andric     const Driver &D = getToolChain().getDriver();
2008*480093f4SDimitry Andric     D.Diag(diag::err_drv_unsupported_opt)
2009*480093f4SDimitry Andric       << Args.getLastArg(options::OPT_mpacked_stack)->getAsString(Args) +
2010*480093f4SDimitry Andric       " " + Args.getLastArg(options::OPT_mbackchain)->getAsString(Args);
2011*480093f4SDimitry Andric   }
2012*480093f4SDimitry Andric   if (HasBackchain)
20130b57cec5SDimitry Andric     CmdArgs.push_back("-mbackchain");
2014*480093f4SDimitry Andric   if (HasPackedStack)
2015*480093f4SDimitry Andric     CmdArgs.push_back("-mpacked-stack");
2016*480093f4SDimitry Andric }
2017*480093f4SDimitry Andric 
2018*480093f4SDimitry Andric static void addX86AlignBranchArgs(const Driver &D, const ArgList &Args,
2019*480093f4SDimitry Andric                                   ArgStringList &CmdArgs) {
2020*480093f4SDimitry Andric   if (Args.hasArg(options::OPT_mbranches_within_32B_boundaries)) {
2021*480093f4SDimitry Andric     CmdArgs.push_back("-mllvm");
2022*480093f4SDimitry Andric     CmdArgs.push_back("-x86-branches-within-32B-boundaries");
2023*480093f4SDimitry Andric   }
2024*480093f4SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_malign_branch_boundary_EQ)) {
2025*480093f4SDimitry Andric     StringRef Value = A->getValue();
2026*480093f4SDimitry Andric     unsigned Boundary;
2027*480093f4SDimitry Andric     if (Value.getAsInteger(10, Boundary) || Boundary < 16 ||
2028*480093f4SDimitry Andric         !llvm::isPowerOf2_64(Boundary)) {
2029*480093f4SDimitry Andric       D.Diag(diag::err_drv_invalid_argument_to_option)
2030*480093f4SDimitry Andric           << Value << A->getOption().getName();
2031*480093f4SDimitry Andric     } else {
2032*480093f4SDimitry Andric       CmdArgs.push_back("-mllvm");
2033*480093f4SDimitry Andric       CmdArgs.push_back(
2034*480093f4SDimitry Andric           Args.MakeArgString("-x86-align-branch-boundary=" + Twine(Boundary)));
2035*480093f4SDimitry Andric     }
2036*480093f4SDimitry Andric   }
2037*480093f4SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_malign_branch_EQ)) {
2038*480093f4SDimitry Andric     std::string AlignBranch;
2039*480093f4SDimitry Andric     for (StringRef T : A->getValues()) {
2040*480093f4SDimitry Andric       if (T != "fused" && T != "jcc" && T != "jmp" && T != "call" &&
2041*480093f4SDimitry Andric           T != "ret" && T != "indirect")
2042*480093f4SDimitry Andric         D.Diag(diag::err_drv_invalid_malign_branch_EQ)
2043*480093f4SDimitry Andric             << T << "fused, jcc, jmp, call, ret, indirect";
2044*480093f4SDimitry Andric       if (!AlignBranch.empty())
2045*480093f4SDimitry Andric         AlignBranch += '+';
2046*480093f4SDimitry Andric       AlignBranch += T;
2047*480093f4SDimitry Andric     }
2048*480093f4SDimitry Andric     CmdArgs.push_back("-mllvm");
2049*480093f4SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-x86-align-branch=" + AlignBranch));
2050*480093f4SDimitry Andric   }
2051*480093f4SDimitry Andric   if (const Arg *A =
2052*480093f4SDimitry Andric           Args.getLastArg(options::OPT_malign_branch_prefix_size_EQ)) {
2053*480093f4SDimitry Andric     StringRef Value = A->getValue();
2054*480093f4SDimitry Andric     unsigned PrefixSize;
2055*480093f4SDimitry Andric     if (Value.getAsInteger(10, PrefixSize) || PrefixSize > 5) {
2056*480093f4SDimitry Andric       D.Diag(diag::err_drv_invalid_argument_to_option)
2057*480093f4SDimitry Andric           << Value << A->getOption().getName();
2058*480093f4SDimitry Andric     } else {
2059*480093f4SDimitry Andric       CmdArgs.push_back("-mllvm");
2060*480093f4SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-x86-align-branch-prefix-size=" +
2061*480093f4SDimitry Andric                                            Twine(PrefixSize)));
2062*480093f4SDimitry Andric     }
2063*480093f4SDimitry Andric   }
20640b57cec5SDimitry Andric }
20650b57cec5SDimitry Andric 
20660b57cec5SDimitry Andric void Clang::AddX86TargetArgs(const ArgList &Args,
20670b57cec5SDimitry Andric                              ArgStringList &CmdArgs) const {
2068*480093f4SDimitry Andric   const Driver &D = getToolChain().getDriver();
2069*480093f4SDimitry Andric   addX86AlignBranchArgs(D, Args, CmdArgs);
2070*480093f4SDimitry Andric 
20710b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
20720b57cec5SDimitry Andric       Args.hasArg(options::OPT_mkernel) ||
20730b57cec5SDimitry Andric       Args.hasArg(options::OPT_fapple_kext))
20740b57cec5SDimitry Andric     CmdArgs.push_back("-disable-red-zone");
20750b57cec5SDimitry Andric 
20760b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs,
20770b57cec5SDimitry Andric                     options::OPT_mno_tls_direct_seg_refs, true))
20780b57cec5SDimitry Andric     CmdArgs.push_back("-mno-tls-direct-seg-refs");
20790b57cec5SDimitry Andric 
20800b57cec5SDimitry Andric   // Default to avoid implicit floating-point for kernel/kext code, but allow
20810b57cec5SDimitry Andric   // that to be overridden with -mno-soft-float.
20820b57cec5SDimitry Andric   bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
20830b57cec5SDimitry Andric                           Args.hasArg(options::OPT_fapple_kext));
20840b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(
20850b57cec5SDimitry Andric           options::OPT_msoft_float, options::OPT_mno_soft_float,
20860b57cec5SDimitry Andric           options::OPT_mimplicit_float, options::OPT_mno_implicit_float)) {
20870b57cec5SDimitry Andric     const Option &O = A->getOption();
20880b57cec5SDimitry Andric     NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
20890b57cec5SDimitry Andric                        O.matches(options::OPT_msoft_float));
20900b57cec5SDimitry Andric   }
20910b57cec5SDimitry Andric   if (NoImplicitFloat)
20920b57cec5SDimitry Andric     CmdArgs.push_back("-no-implicit-float");
20930b57cec5SDimitry Andric 
20940b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
20950b57cec5SDimitry Andric     StringRef Value = A->getValue();
20960b57cec5SDimitry Andric     if (Value == "intel" || Value == "att") {
20970b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
20980b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
20990b57cec5SDimitry Andric     } else {
2100*480093f4SDimitry Andric       D.Diag(diag::err_drv_unsupported_option_argument)
21010b57cec5SDimitry Andric           << A->getOption().getName() << Value;
21020b57cec5SDimitry Andric     }
2103*480093f4SDimitry Andric   } else if (D.IsCLMode()) {
21040b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
21050b57cec5SDimitry Andric     CmdArgs.push_back("-x86-asm-syntax=intel");
21060b57cec5SDimitry Andric   }
21070b57cec5SDimitry Andric 
21080b57cec5SDimitry Andric   // Set flags to support MCU ABI.
21090b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) {
21100b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
21110b57cec5SDimitry Andric     CmdArgs.push_back("soft");
21120b57cec5SDimitry Andric     CmdArgs.push_back("-mstack-alignment=4");
21130b57cec5SDimitry Andric   }
21140b57cec5SDimitry Andric }
21150b57cec5SDimitry Andric 
21160b57cec5SDimitry Andric void Clang::AddHexagonTargetArgs(const ArgList &Args,
21170b57cec5SDimitry Andric                                  ArgStringList &CmdArgs) const {
21180b57cec5SDimitry Andric   CmdArgs.push_back("-mqdsp6-compat");
21190b57cec5SDimitry Andric   CmdArgs.push_back("-Wreturn-type");
21200b57cec5SDimitry Andric 
21210b57cec5SDimitry Andric   if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) {
21220b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
21230b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-hexagon-small-data-threshold=" +
21240b57cec5SDimitry Andric                                          Twine(G.getValue())));
21250b57cec5SDimitry Andric   }
21260b57cec5SDimitry Andric 
21270b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_fno_short_enums))
21280b57cec5SDimitry Andric     CmdArgs.push_back("-fshort-enums");
21290b57cec5SDimitry Andric   if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
21300b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
21310b57cec5SDimitry Andric     CmdArgs.push_back("-enable-hexagon-ieee-rnd-near");
21320b57cec5SDimitry Andric   }
21330b57cec5SDimitry Andric   CmdArgs.push_back("-mllvm");
21340b57cec5SDimitry Andric   CmdArgs.push_back("-machine-sink-split=0");
21350b57cec5SDimitry Andric }
21360b57cec5SDimitry Andric 
21370b57cec5SDimitry Andric void Clang::AddLanaiTargetArgs(const ArgList &Args,
21380b57cec5SDimitry Andric                                ArgStringList &CmdArgs) const {
21390b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
21400b57cec5SDimitry Andric     StringRef CPUName = A->getValue();
21410b57cec5SDimitry Andric 
21420b57cec5SDimitry Andric     CmdArgs.push_back("-target-cpu");
21430b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(CPUName));
21440b57cec5SDimitry Andric   }
21450b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
21460b57cec5SDimitry Andric     StringRef Value = A->getValue();
21470b57cec5SDimitry Andric     // Only support mregparm=4 to support old usage. Report error for all other
21480b57cec5SDimitry Andric     // cases.
21490b57cec5SDimitry Andric     int Mregparm;
21500b57cec5SDimitry Andric     if (Value.getAsInteger(10, Mregparm)) {
21510b57cec5SDimitry Andric       if (Mregparm != 4) {
21520b57cec5SDimitry Andric         getToolChain().getDriver().Diag(
21530b57cec5SDimitry Andric             diag::err_drv_unsupported_option_argument)
21540b57cec5SDimitry Andric             << A->getOption().getName() << Value;
21550b57cec5SDimitry Andric       }
21560b57cec5SDimitry Andric     }
21570b57cec5SDimitry Andric   }
21580b57cec5SDimitry Andric }
21590b57cec5SDimitry Andric 
21600b57cec5SDimitry Andric void Clang::AddWebAssemblyTargetArgs(const ArgList &Args,
21610b57cec5SDimitry Andric                                      ArgStringList &CmdArgs) const {
21620b57cec5SDimitry Andric   // Default to "hidden" visibility.
21630b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_fvisibility_EQ,
21640b57cec5SDimitry Andric                    options::OPT_fvisibility_ms_compat)) {
21650b57cec5SDimitry Andric     CmdArgs.push_back("-fvisibility");
21660b57cec5SDimitry Andric     CmdArgs.push_back("hidden");
21670b57cec5SDimitry Andric   }
21680b57cec5SDimitry Andric }
21690b57cec5SDimitry Andric 
21700b57cec5SDimitry Andric void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename,
21710b57cec5SDimitry Andric                                     StringRef Target, const InputInfo &Output,
21720b57cec5SDimitry Andric                                     const InputInfo &Input, const ArgList &Args) const {
21730b57cec5SDimitry Andric   // If this is a dry run, do not create the compilation database file.
21740b57cec5SDimitry Andric   if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
21750b57cec5SDimitry Andric     return;
21760b57cec5SDimitry Andric 
21770b57cec5SDimitry Andric   using llvm::yaml::escape;
21780b57cec5SDimitry Andric   const Driver &D = getToolChain().getDriver();
21790b57cec5SDimitry Andric 
21800b57cec5SDimitry Andric   if (!CompilationDatabase) {
21810b57cec5SDimitry Andric     std::error_code EC;
2182a7dea167SDimitry Andric     auto File = std::make_unique<llvm::raw_fd_ostream>(Filename, EC,
2183a7dea167SDimitry Andric                                                         llvm::sys::fs::OF_Text);
21840b57cec5SDimitry Andric     if (EC) {
21850b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_compilationdatabase) << Filename
21860b57cec5SDimitry Andric                                                        << EC.message();
21870b57cec5SDimitry Andric       return;
21880b57cec5SDimitry Andric     }
21890b57cec5SDimitry Andric     CompilationDatabase = std::move(File);
21900b57cec5SDimitry Andric   }
21910b57cec5SDimitry Andric   auto &CDB = *CompilationDatabase;
2192a7dea167SDimitry Andric   auto CWD = D.getVFS().getCurrentWorkingDirectory();
2193a7dea167SDimitry Andric   if (!CWD)
2194a7dea167SDimitry Andric     CWD = ".";
2195a7dea167SDimitry Andric   CDB << "{ \"directory\": \"" << escape(*CWD) << "\"";
21960b57cec5SDimitry Andric   CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\"";
21970b57cec5SDimitry Andric   CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\"";
21980b57cec5SDimitry Andric   CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\"";
2199a7dea167SDimitry Andric   SmallString<128> Buf;
22000b57cec5SDimitry Andric   Buf = "-x";
22010b57cec5SDimitry Andric   Buf += types::getTypeName(Input.getType());
22020b57cec5SDimitry Andric   CDB << ", \"" << escape(Buf) << "\"";
22030b57cec5SDimitry Andric   if (!D.SysRoot.empty() && !Args.hasArg(options::OPT__sysroot_EQ)) {
22040b57cec5SDimitry Andric     Buf = "--sysroot=";
22050b57cec5SDimitry Andric     Buf += D.SysRoot;
22060b57cec5SDimitry Andric     CDB << ", \"" << escape(Buf) << "\"";
22070b57cec5SDimitry Andric   }
22080b57cec5SDimitry Andric   CDB << ", \"" << escape(Input.getFilename()) << "\"";
22090b57cec5SDimitry Andric   for (auto &A: Args) {
22100b57cec5SDimitry Andric     auto &O = A->getOption();
22110b57cec5SDimitry Andric     // Skip language selection, which is positional.
22120b57cec5SDimitry Andric     if (O.getID() == options::OPT_x)
22130b57cec5SDimitry Andric       continue;
22140b57cec5SDimitry Andric     // Skip writing dependency output and the compilation database itself.
22150b57cec5SDimitry Andric     if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group)
22160b57cec5SDimitry Andric       continue;
2217a7dea167SDimitry Andric     if (O.getID() == options::OPT_gen_cdb_fragment_path)
2218a7dea167SDimitry Andric       continue;
22190b57cec5SDimitry Andric     // Skip inputs.
22200b57cec5SDimitry Andric     if (O.getKind() == Option::InputClass)
22210b57cec5SDimitry Andric       continue;
22220b57cec5SDimitry Andric     // All other arguments are quoted and appended.
22230b57cec5SDimitry Andric     ArgStringList ASL;
22240b57cec5SDimitry Andric     A->render(Args, ASL);
22250b57cec5SDimitry Andric     for (auto &it: ASL)
22260b57cec5SDimitry Andric       CDB << ", \"" << escape(it) << "\"";
22270b57cec5SDimitry Andric   }
22280b57cec5SDimitry Andric   Buf = "--target=";
22290b57cec5SDimitry Andric   Buf += Target;
22300b57cec5SDimitry Andric   CDB << ", \"" << escape(Buf) << "\"]},\n";
22310b57cec5SDimitry Andric }
22320b57cec5SDimitry Andric 
2233a7dea167SDimitry Andric void Clang::DumpCompilationDatabaseFragmentToDir(
2234a7dea167SDimitry Andric     StringRef Dir, Compilation &C, StringRef Target, const InputInfo &Output,
2235a7dea167SDimitry Andric     const InputInfo &Input, const llvm::opt::ArgList &Args) const {
2236a7dea167SDimitry Andric   // If this is a dry run, do not create the compilation database file.
2237a7dea167SDimitry Andric   if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
2238a7dea167SDimitry Andric     return;
2239a7dea167SDimitry Andric 
2240a7dea167SDimitry Andric   if (CompilationDatabase)
2241a7dea167SDimitry Andric     DumpCompilationDatabase(C, "", Target, Output, Input, Args);
2242a7dea167SDimitry Andric 
2243a7dea167SDimitry Andric   SmallString<256> Path = Dir;
2244a7dea167SDimitry Andric   const auto &Driver = C.getDriver();
2245a7dea167SDimitry Andric   Driver.getVFS().makeAbsolute(Path);
2246a7dea167SDimitry Andric   auto Err = llvm::sys::fs::create_directory(Path, /*IgnoreExisting=*/true);
2247a7dea167SDimitry Andric   if (Err) {
2248a7dea167SDimitry Andric     Driver.Diag(diag::err_drv_compilationdatabase) << Dir << Err.message();
2249a7dea167SDimitry Andric     return;
2250a7dea167SDimitry Andric   }
2251a7dea167SDimitry Andric 
2252a7dea167SDimitry Andric   llvm::sys::path::append(
2253a7dea167SDimitry Andric       Path,
2254a7dea167SDimitry Andric       Twine(llvm::sys::path::filename(Input.getFilename())) + ".%%%%.json");
2255a7dea167SDimitry Andric   int FD;
2256a7dea167SDimitry Andric   SmallString<256> TempPath;
2257a7dea167SDimitry Andric   Err = llvm::sys::fs::createUniqueFile(Path, FD, TempPath);
2258a7dea167SDimitry Andric   if (Err) {
2259a7dea167SDimitry Andric     Driver.Diag(diag::err_drv_compilationdatabase) << Path << Err.message();
2260a7dea167SDimitry Andric     return;
2261a7dea167SDimitry Andric   }
2262a7dea167SDimitry Andric   CompilationDatabase =
2263a7dea167SDimitry Andric       std::make_unique<llvm::raw_fd_ostream>(FD, /*shouldClose=*/true);
2264a7dea167SDimitry Andric   DumpCompilationDatabase(C, "", Target, Output, Input, Args);
2265a7dea167SDimitry Andric }
2266a7dea167SDimitry Andric 
22670b57cec5SDimitry Andric static void CollectArgsForIntegratedAssembler(Compilation &C,
22680b57cec5SDimitry Andric                                               const ArgList &Args,
22690b57cec5SDimitry Andric                                               ArgStringList &CmdArgs,
22700b57cec5SDimitry Andric                                               const Driver &D) {
22710b57cec5SDimitry Andric   if (UseRelaxAll(C, Args))
22720b57cec5SDimitry Andric     CmdArgs.push_back("-mrelax-all");
22730b57cec5SDimitry Andric 
22740b57cec5SDimitry Andric   // Only default to -mincremental-linker-compatible if we think we are
22750b57cec5SDimitry Andric   // targeting the MSVC linker.
22760b57cec5SDimitry Andric   bool DefaultIncrementalLinkerCompatible =
22770b57cec5SDimitry Andric       C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment();
22780b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_mincremental_linker_compatible,
22790b57cec5SDimitry Andric                    options::OPT_mno_incremental_linker_compatible,
22800b57cec5SDimitry Andric                    DefaultIncrementalLinkerCompatible))
22810b57cec5SDimitry Andric     CmdArgs.push_back("-mincremental-linker-compatible");
22820b57cec5SDimitry Andric 
22830b57cec5SDimitry Andric   switch (C.getDefaultToolChain().getArch()) {
22840b57cec5SDimitry Andric   case llvm::Triple::arm:
22850b57cec5SDimitry Andric   case llvm::Triple::armeb:
22860b57cec5SDimitry Andric   case llvm::Triple::thumb:
22870b57cec5SDimitry Andric   case llvm::Triple::thumbeb:
22880b57cec5SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_mimplicit_it_EQ)) {
22890b57cec5SDimitry Andric       StringRef Value = A->getValue();
22900b57cec5SDimitry Andric       if (Value == "always" || Value == "never" || Value == "arm" ||
22910b57cec5SDimitry Andric           Value == "thumb") {
22920b57cec5SDimitry Andric         CmdArgs.push_back("-mllvm");
22930b57cec5SDimitry Andric         CmdArgs.push_back(Args.MakeArgString("-arm-implicit-it=" + Value));
22940b57cec5SDimitry Andric       } else {
22950b57cec5SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
22960b57cec5SDimitry Andric             << A->getOption().getName() << Value;
22970b57cec5SDimitry Andric       }
22980b57cec5SDimitry Andric     }
22990b57cec5SDimitry Andric     break;
23000b57cec5SDimitry Andric   default:
23010b57cec5SDimitry Andric     break;
23020b57cec5SDimitry Andric   }
23030b57cec5SDimitry Andric 
2304a7dea167SDimitry Andric   // If you add more args here, also add them to the block below that
2305a7dea167SDimitry Andric   // starts with "// If CollectArgsForIntegratedAssembler() isn't called below".
2306a7dea167SDimitry Andric 
23070b57cec5SDimitry Andric   // When passing -I arguments to the assembler we sometimes need to
23080b57cec5SDimitry Andric   // unconditionally take the next argument.  For example, when parsing
23090b57cec5SDimitry Andric   // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the
23100b57cec5SDimitry Andric   // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo'
23110b57cec5SDimitry Andric   // arg after parsing the '-I' arg.
23120b57cec5SDimitry Andric   bool TakeNextArg = false;
23130b57cec5SDimitry Andric 
23140b57cec5SDimitry Andric   bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations();
23150b57cec5SDimitry Andric   bool UseNoExecStack = C.getDefaultToolChain().isNoExecStackDefault();
23160b57cec5SDimitry Andric   const char *MipsTargetFeature = nullptr;
23170b57cec5SDimitry Andric   for (const Arg *A :
23180b57cec5SDimitry Andric        Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
23190b57cec5SDimitry Andric     A->claim();
23200b57cec5SDimitry Andric 
23210b57cec5SDimitry Andric     for (StringRef Value : A->getValues()) {
23220b57cec5SDimitry Andric       if (TakeNextArg) {
23230b57cec5SDimitry Andric         CmdArgs.push_back(Value.data());
23240b57cec5SDimitry Andric         TakeNextArg = false;
23250b57cec5SDimitry Andric         continue;
23260b57cec5SDimitry Andric       }
23270b57cec5SDimitry Andric 
23280b57cec5SDimitry Andric       if (C.getDefaultToolChain().getTriple().isOSBinFormatCOFF() &&
23290b57cec5SDimitry Andric           Value == "-mbig-obj")
23300b57cec5SDimitry Andric         continue; // LLVM handles bigobj automatically
23310b57cec5SDimitry Andric 
23320b57cec5SDimitry Andric       switch (C.getDefaultToolChain().getArch()) {
23330b57cec5SDimitry Andric       default:
23340b57cec5SDimitry Andric         break;
23350b57cec5SDimitry Andric       case llvm::Triple::thumb:
23360b57cec5SDimitry Andric       case llvm::Triple::thumbeb:
23370b57cec5SDimitry Andric       case llvm::Triple::arm:
23380b57cec5SDimitry Andric       case llvm::Triple::armeb:
23390b57cec5SDimitry Andric         if (Value == "-mthumb")
23400b57cec5SDimitry Andric           // -mthumb has already been processed in ComputeLLVMTriple()
23410b57cec5SDimitry Andric           // recognize but skip over here.
23420b57cec5SDimitry Andric           continue;
23430b57cec5SDimitry Andric         break;
23440b57cec5SDimitry Andric       case llvm::Triple::mips:
23450b57cec5SDimitry Andric       case llvm::Triple::mipsel:
23460b57cec5SDimitry Andric       case llvm::Triple::mips64:
23470b57cec5SDimitry Andric       case llvm::Triple::mips64el:
23480b57cec5SDimitry Andric         if (Value == "--trap") {
23490b57cec5SDimitry Andric           CmdArgs.push_back("-target-feature");
23500b57cec5SDimitry Andric           CmdArgs.push_back("+use-tcc-in-div");
23510b57cec5SDimitry Andric           continue;
23520b57cec5SDimitry Andric         }
23530b57cec5SDimitry Andric         if (Value == "--break") {
23540b57cec5SDimitry Andric           CmdArgs.push_back("-target-feature");
23550b57cec5SDimitry Andric           CmdArgs.push_back("-use-tcc-in-div");
23560b57cec5SDimitry Andric           continue;
23570b57cec5SDimitry Andric         }
23580b57cec5SDimitry Andric         if (Value.startswith("-msoft-float")) {
23590b57cec5SDimitry Andric           CmdArgs.push_back("-target-feature");
23600b57cec5SDimitry Andric           CmdArgs.push_back("+soft-float");
23610b57cec5SDimitry Andric           continue;
23620b57cec5SDimitry Andric         }
23630b57cec5SDimitry Andric         if (Value.startswith("-mhard-float")) {
23640b57cec5SDimitry Andric           CmdArgs.push_back("-target-feature");
23650b57cec5SDimitry Andric           CmdArgs.push_back("-soft-float");
23660b57cec5SDimitry Andric           continue;
23670b57cec5SDimitry Andric         }
23680b57cec5SDimitry Andric 
23690b57cec5SDimitry Andric         MipsTargetFeature = llvm::StringSwitch<const char *>(Value)
23700b57cec5SDimitry Andric                                 .Case("-mips1", "+mips1")
23710b57cec5SDimitry Andric                                 .Case("-mips2", "+mips2")
23720b57cec5SDimitry Andric                                 .Case("-mips3", "+mips3")
23730b57cec5SDimitry Andric                                 .Case("-mips4", "+mips4")
23740b57cec5SDimitry Andric                                 .Case("-mips5", "+mips5")
23750b57cec5SDimitry Andric                                 .Case("-mips32", "+mips32")
23760b57cec5SDimitry Andric                                 .Case("-mips32r2", "+mips32r2")
23770b57cec5SDimitry Andric                                 .Case("-mips32r3", "+mips32r3")
23780b57cec5SDimitry Andric                                 .Case("-mips32r5", "+mips32r5")
23790b57cec5SDimitry Andric                                 .Case("-mips32r6", "+mips32r6")
23800b57cec5SDimitry Andric                                 .Case("-mips64", "+mips64")
23810b57cec5SDimitry Andric                                 .Case("-mips64r2", "+mips64r2")
23820b57cec5SDimitry Andric                                 .Case("-mips64r3", "+mips64r3")
23830b57cec5SDimitry Andric                                 .Case("-mips64r5", "+mips64r5")
23840b57cec5SDimitry Andric                                 .Case("-mips64r6", "+mips64r6")
23850b57cec5SDimitry Andric                                 .Default(nullptr);
23860b57cec5SDimitry Andric         if (MipsTargetFeature)
23870b57cec5SDimitry Andric           continue;
23880b57cec5SDimitry Andric       }
23890b57cec5SDimitry Andric 
23900b57cec5SDimitry Andric       if (Value == "-force_cpusubtype_ALL") {
23910b57cec5SDimitry Andric         // Do nothing, this is the default and we don't support anything else.
23920b57cec5SDimitry Andric       } else if (Value == "-L") {
23930b57cec5SDimitry Andric         CmdArgs.push_back("-msave-temp-labels");
23940b57cec5SDimitry Andric       } else if (Value == "--fatal-warnings") {
23950b57cec5SDimitry Andric         CmdArgs.push_back("-massembler-fatal-warnings");
2396a7dea167SDimitry Andric       } else if (Value == "--no-warn" || Value == "-W") {
2397a7dea167SDimitry Andric         CmdArgs.push_back("-massembler-no-warn");
23980b57cec5SDimitry Andric       } else if (Value == "--noexecstack") {
23990b57cec5SDimitry Andric         UseNoExecStack = true;
24000b57cec5SDimitry Andric       } else if (Value.startswith("-compress-debug-sections") ||
24010b57cec5SDimitry Andric                  Value.startswith("--compress-debug-sections") ||
24020b57cec5SDimitry Andric                  Value == "-nocompress-debug-sections" ||
24030b57cec5SDimitry Andric                  Value == "--nocompress-debug-sections") {
24040b57cec5SDimitry Andric         CmdArgs.push_back(Value.data());
24050b57cec5SDimitry Andric       } else if (Value == "-mrelax-relocations=yes" ||
24060b57cec5SDimitry Andric                  Value == "--mrelax-relocations=yes") {
24070b57cec5SDimitry Andric         UseRelaxRelocations = true;
24080b57cec5SDimitry Andric       } else if (Value == "-mrelax-relocations=no" ||
24090b57cec5SDimitry Andric                  Value == "--mrelax-relocations=no") {
24100b57cec5SDimitry Andric         UseRelaxRelocations = false;
24110b57cec5SDimitry Andric       } else if (Value.startswith("-I")) {
24120b57cec5SDimitry Andric         CmdArgs.push_back(Value.data());
24130b57cec5SDimitry Andric         // We need to consume the next argument if the current arg is a plain
24140b57cec5SDimitry Andric         // -I. The next arg will be the include directory.
24150b57cec5SDimitry Andric         if (Value == "-I")
24160b57cec5SDimitry Andric           TakeNextArg = true;
24170b57cec5SDimitry Andric       } else if (Value.startswith("-gdwarf-")) {
24180b57cec5SDimitry Andric         // "-gdwarf-N" options are not cc1as options.
24190b57cec5SDimitry Andric         unsigned DwarfVersion = DwarfVersionNum(Value);
24200b57cec5SDimitry Andric         if (DwarfVersion == 0) { // Send it onward, and let cc1as complain.
24210b57cec5SDimitry Andric           CmdArgs.push_back(Value.data());
24220b57cec5SDimitry Andric         } else {
24230b57cec5SDimitry Andric           RenderDebugEnablingArgs(Args, CmdArgs,
24240b57cec5SDimitry Andric                                   codegenoptions::LimitedDebugInfo,
24250b57cec5SDimitry Andric                                   DwarfVersion, llvm::DebuggerKind::Default);
24260b57cec5SDimitry Andric         }
24270b57cec5SDimitry Andric       } else if (Value.startswith("-mcpu") || Value.startswith("-mfpu") ||
24280b57cec5SDimitry Andric                  Value.startswith("-mhwdiv") || Value.startswith("-march")) {
24290b57cec5SDimitry Andric         // Do nothing, we'll validate it later.
24300b57cec5SDimitry Andric       } else if (Value == "-defsym") {
24310b57cec5SDimitry Andric           if (A->getNumValues() != 2) {
24320b57cec5SDimitry Andric             D.Diag(diag::err_drv_defsym_invalid_format) << Value;
24330b57cec5SDimitry Andric             break;
24340b57cec5SDimitry Andric           }
24350b57cec5SDimitry Andric           const char *S = A->getValue(1);
24360b57cec5SDimitry Andric           auto Pair = StringRef(S).split('=');
24370b57cec5SDimitry Andric           auto Sym = Pair.first;
24380b57cec5SDimitry Andric           auto SVal = Pair.second;
24390b57cec5SDimitry Andric 
24400b57cec5SDimitry Andric           if (Sym.empty() || SVal.empty()) {
24410b57cec5SDimitry Andric             D.Diag(diag::err_drv_defsym_invalid_format) << S;
24420b57cec5SDimitry Andric             break;
24430b57cec5SDimitry Andric           }
24440b57cec5SDimitry Andric           int64_t IVal;
24450b57cec5SDimitry Andric           if (SVal.getAsInteger(0, IVal)) {
24460b57cec5SDimitry Andric             D.Diag(diag::err_drv_defsym_invalid_symval) << SVal;
24470b57cec5SDimitry Andric             break;
24480b57cec5SDimitry Andric           }
24490b57cec5SDimitry Andric           CmdArgs.push_back(Value.data());
24500b57cec5SDimitry Andric           TakeNextArg = true;
24510b57cec5SDimitry Andric       } else if (Value == "-fdebug-compilation-dir") {
24520b57cec5SDimitry Andric         CmdArgs.push_back("-fdebug-compilation-dir");
24530b57cec5SDimitry Andric         TakeNextArg = true;
2454*480093f4SDimitry Andric       } else if (Value.consume_front("-fdebug-compilation-dir=")) {
2455*480093f4SDimitry Andric         // The flag is a -Wa / -Xassembler argument and Options doesn't
2456*480093f4SDimitry Andric         // parse the argument, so this isn't automatically aliased to
2457*480093f4SDimitry Andric         // -fdebug-compilation-dir (without '=') here.
2458*480093f4SDimitry Andric         CmdArgs.push_back("-fdebug-compilation-dir");
2459*480093f4SDimitry Andric         CmdArgs.push_back(Value.data());
24600b57cec5SDimitry Andric       } else {
24610b57cec5SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
24620b57cec5SDimitry Andric             << A->getOption().getName() << Value;
24630b57cec5SDimitry Andric       }
24640b57cec5SDimitry Andric     }
24650b57cec5SDimitry Andric   }
24660b57cec5SDimitry Andric   if (UseRelaxRelocations)
24670b57cec5SDimitry Andric     CmdArgs.push_back("--mrelax-relocations");
24680b57cec5SDimitry Andric   if (UseNoExecStack)
24690b57cec5SDimitry Andric     CmdArgs.push_back("-mnoexecstack");
24700b57cec5SDimitry Andric   if (MipsTargetFeature != nullptr) {
24710b57cec5SDimitry Andric     CmdArgs.push_back("-target-feature");
24720b57cec5SDimitry Andric     CmdArgs.push_back(MipsTargetFeature);
24730b57cec5SDimitry Andric   }
24740b57cec5SDimitry Andric 
24750b57cec5SDimitry Andric   // forward -fembed-bitcode to assmebler
24760b57cec5SDimitry Andric   if (C.getDriver().embedBitcodeEnabled() ||
24770b57cec5SDimitry Andric       C.getDriver().embedBitcodeMarkerOnly())
24780b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ);
24790b57cec5SDimitry Andric }
24800b57cec5SDimitry Andric 
24810b57cec5SDimitry Andric static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
24820b57cec5SDimitry Andric                                        bool OFastEnabled, const ArgList &Args,
24830b57cec5SDimitry Andric                                        ArgStringList &CmdArgs) {
24840b57cec5SDimitry Andric   // Handle various floating point optimization flags, mapping them to the
24850b57cec5SDimitry Andric   // appropriate LLVM code generation flags. This is complicated by several
24860b57cec5SDimitry Andric   // "umbrella" flags, so we do this by stepping through the flags incrementally
24870b57cec5SDimitry Andric   // adjusting what we think is enabled/disabled, then at the end setting the
24880b57cec5SDimitry Andric   // LLVM flags based on the final state.
24890b57cec5SDimitry Andric   bool HonorINFs = true;
24900b57cec5SDimitry Andric   bool HonorNaNs = true;
24910b57cec5SDimitry Andric   // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
24920b57cec5SDimitry Andric   bool MathErrno = TC.IsMathErrnoDefault();
24930b57cec5SDimitry Andric   bool AssociativeMath = false;
24940b57cec5SDimitry Andric   bool ReciprocalMath = false;
24950b57cec5SDimitry Andric   bool SignedZeros = true;
2496*480093f4SDimitry Andric   bool TrappingMath = false; // Implemented via -ffp-exception-behavior
2497*480093f4SDimitry Andric   bool TrappingMathPresent = false; // Is trapping-math in args, and not
2498*480093f4SDimitry Andric                                     // overriden by ffp-exception-behavior?
2499*480093f4SDimitry Andric   bool RoundingFPMath = false;
2500*480093f4SDimitry Andric   bool RoundingMathPresent = false; // Is rounding-math in args?
2501*480093f4SDimitry Andric   // -ffp-model values: strict, fast, precise
2502*480093f4SDimitry Andric   StringRef FPModel = "";
2503*480093f4SDimitry Andric   // -ffp-exception-behavior options: strict, maytrap, ignore
2504*480093f4SDimitry Andric   StringRef FPExceptionBehavior = "";
25050b57cec5SDimitry Andric   StringRef DenormalFPMath = "";
25060b57cec5SDimitry Andric   StringRef FPContract = "";
2507*480093f4SDimitry Andric   bool StrictFPModel = false;
25080b57cec5SDimitry Andric 
25090b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
25100b57cec5SDimitry Andric     CmdArgs.push_back("-mlimit-float-precision");
25110b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
25120b57cec5SDimitry Andric   }
25130b57cec5SDimitry Andric 
25140b57cec5SDimitry Andric   for (const Arg *A : Args) {
2515*480093f4SDimitry Andric     auto optID = A->getOption().getID();
2516*480093f4SDimitry Andric     bool PreciseFPModel = false;
2517*480093f4SDimitry Andric     switch (optID) {
2518*480093f4SDimitry Andric     default:
2519*480093f4SDimitry Andric       break;
2520*480093f4SDimitry Andric     case options::OPT_ffp_model_EQ: {
2521*480093f4SDimitry Andric       // If -ffp-model= is seen, reset to fno-fast-math
2522*480093f4SDimitry Andric       HonorINFs = true;
2523*480093f4SDimitry Andric       HonorNaNs = true;
2524*480093f4SDimitry Andric       // Turning *off* -ffast-math restores the toolchain default.
2525*480093f4SDimitry Andric       MathErrno = TC.IsMathErrnoDefault();
2526*480093f4SDimitry Andric       AssociativeMath = false;
2527*480093f4SDimitry Andric       ReciprocalMath = false;
2528*480093f4SDimitry Andric       SignedZeros = true;
2529*480093f4SDimitry Andric       // -fno_fast_math restores default denormal and fpcontract handling
2530*480093f4SDimitry Andric       DenormalFPMath = "";
2531*480093f4SDimitry Andric       FPContract = "";
2532*480093f4SDimitry Andric       StringRef Val = A->getValue();
2533*480093f4SDimitry Andric       if (OFastEnabled && !Val.equals("fast")) {
2534*480093f4SDimitry Andric           // Only -ffp-model=fast is compatible with OFast, ignore.
2535*480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2536*480093f4SDimitry Andric           << Args.MakeArgString("-ffp-model=" + Val)
2537*480093f4SDimitry Andric           << "-Ofast";
2538*480093f4SDimitry Andric         break;
2539*480093f4SDimitry Andric       }
2540*480093f4SDimitry Andric       StrictFPModel = false;
2541*480093f4SDimitry Andric       PreciseFPModel = true;
2542*480093f4SDimitry Andric       // ffp-model= is a Driver option, it is entirely rewritten into more
2543*480093f4SDimitry Andric       // granular options before being passed into cc1.
2544*480093f4SDimitry Andric       // Use the gcc option in the switch below.
2545*480093f4SDimitry Andric       if (!FPModel.empty() && !FPModel.equals(Val)) {
2546*480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2547*480093f4SDimitry Andric           << Args.MakeArgString("-ffp-model=" + FPModel)
2548*480093f4SDimitry Andric           << Args.MakeArgString("-ffp-model=" + Val);
2549*480093f4SDimitry Andric         FPContract = "";
2550*480093f4SDimitry Andric       }
2551*480093f4SDimitry Andric       if (Val.equals("fast")) {
2552*480093f4SDimitry Andric         optID = options::OPT_ffast_math;
2553*480093f4SDimitry Andric         FPModel = Val;
2554*480093f4SDimitry Andric         FPContract = "fast";
2555*480093f4SDimitry Andric       } else if (Val.equals("precise")) {
2556*480093f4SDimitry Andric         optID = options::OPT_ffp_contract;
2557*480093f4SDimitry Andric         FPModel = Val;
2558*480093f4SDimitry Andric         FPContract = "fast";
2559*480093f4SDimitry Andric         PreciseFPModel = true;
2560*480093f4SDimitry Andric       } else if (Val.equals("strict")) {
2561*480093f4SDimitry Andric         StrictFPModel = true;
2562*480093f4SDimitry Andric         optID = options::OPT_frounding_math;
2563*480093f4SDimitry Andric         FPExceptionBehavior = "strict";
2564*480093f4SDimitry Andric         FPModel = Val;
2565*480093f4SDimitry Andric         TrappingMath = true;
2566*480093f4SDimitry Andric       } else
2567*480093f4SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
2568*480093f4SDimitry Andric             << A->getOption().getName() << Val;
2569*480093f4SDimitry Andric       break;
2570*480093f4SDimitry Andric       }
2571*480093f4SDimitry Andric     }
2572*480093f4SDimitry Andric 
2573*480093f4SDimitry Andric     switch (optID) {
25740b57cec5SDimitry Andric     // If this isn't an FP option skip the claim below
25750b57cec5SDimitry Andric     default: continue;
25760b57cec5SDimitry Andric 
25770b57cec5SDimitry Andric     // Options controlling individual features
25780b57cec5SDimitry Andric     case options::OPT_fhonor_infinities:    HonorINFs = true;         break;
25790b57cec5SDimitry Andric     case options::OPT_fno_honor_infinities: HonorINFs = false;        break;
25800b57cec5SDimitry Andric     case options::OPT_fhonor_nans:          HonorNaNs = true;         break;
25810b57cec5SDimitry Andric     case options::OPT_fno_honor_nans:       HonorNaNs = false;        break;
25820b57cec5SDimitry Andric     case options::OPT_fmath_errno:          MathErrno = true;         break;
25830b57cec5SDimitry Andric     case options::OPT_fno_math_errno:       MathErrno = false;        break;
25840b57cec5SDimitry Andric     case options::OPT_fassociative_math:    AssociativeMath = true;   break;
25850b57cec5SDimitry Andric     case options::OPT_fno_associative_math: AssociativeMath = false;  break;
25860b57cec5SDimitry Andric     case options::OPT_freciprocal_math:     ReciprocalMath = true;    break;
25870b57cec5SDimitry Andric     case options::OPT_fno_reciprocal_math:  ReciprocalMath = false;   break;
25880b57cec5SDimitry Andric     case options::OPT_fsigned_zeros:        SignedZeros = true;       break;
25890b57cec5SDimitry Andric     case options::OPT_fno_signed_zeros:     SignedZeros = false;      break;
2590*480093f4SDimitry Andric     case options::OPT_ftrapping_math:
2591*480093f4SDimitry Andric       if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
2592*480093f4SDimitry Andric           !FPExceptionBehavior.equals("strict"))
2593*480093f4SDimitry Andric         // Warn that previous value of option is overridden.
2594*480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2595*480093f4SDimitry Andric           << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior)
2596*480093f4SDimitry Andric           << "-ftrapping-math";
2597*480093f4SDimitry Andric       TrappingMath = true;
2598*480093f4SDimitry Andric       TrappingMathPresent = true;
2599*480093f4SDimitry Andric       FPExceptionBehavior = "strict";
2600*480093f4SDimitry Andric       break;
2601*480093f4SDimitry Andric     case options::OPT_fno_trapping_math:
2602*480093f4SDimitry Andric       if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
2603*480093f4SDimitry Andric           !FPExceptionBehavior.equals("ignore"))
2604*480093f4SDimitry Andric         // Warn that previous value of option is overridden.
2605*480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2606*480093f4SDimitry Andric           << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior)
2607*480093f4SDimitry Andric           << "-fno-trapping-math";
2608*480093f4SDimitry Andric       TrappingMath = false;
2609*480093f4SDimitry Andric       TrappingMathPresent = true;
2610*480093f4SDimitry Andric       FPExceptionBehavior = "ignore";
2611*480093f4SDimitry Andric       break;
2612*480093f4SDimitry Andric 
2613*480093f4SDimitry Andric     case options::OPT_frounding_math:
2614*480093f4SDimitry Andric       RoundingFPMath = true;
2615*480093f4SDimitry Andric       RoundingMathPresent = true;
2616*480093f4SDimitry Andric       break;
2617*480093f4SDimitry Andric 
2618*480093f4SDimitry Andric     case options::OPT_fno_rounding_math:
2619*480093f4SDimitry Andric       RoundingFPMath = false;
2620*480093f4SDimitry Andric       RoundingMathPresent = false;
2621*480093f4SDimitry Andric       break;
26220b57cec5SDimitry Andric 
26230b57cec5SDimitry Andric     case options::OPT_fdenormal_fp_math_EQ:
26240b57cec5SDimitry Andric       DenormalFPMath = A->getValue();
26250b57cec5SDimitry Andric       break;
26260b57cec5SDimitry Andric 
2627*480093f4SDimitry Andric     // Validate and pass through -ffp-contract option.
26280b57cec5SDimitry Andric     case options::OPT_ffp_contract: {
26290b57cec5SDimitry Andric       StringRef Val = A->getValue();
2630*480093f4SDimitry Andric       if (PreciseFPModel) {
2631*480093f4SDimitry Andric         // -ffp-model=precise enables ffp-contract=fast as a side effect
2632*480093f4SDimitry Andric         // the FPContract value has already been set to a string literal
2633*480093f4SDimitry Andric         // and the Val string isn't a pertinent value.
2634*480093f4SDimitry Andric         ;
2635*480093f4SDimitry Andric       } else if (Val.equals("fast") || Val.equals("on") || Val.equals("off"))
26360b57cec5SDimitry Andric         FPContract = Val;
26370b57cec5SDimitry Andric       else
26380b57cec5SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
26390b57cec5SDimitry Andric            << A->getOption().getName() << Val;
26400b57cec5SDimitry Andric       break;
26410b57cec5SDimitry Andric     }
26420b57cec5SDimitry Andric 
2643*480093f4SDimitry Andric     // Validate and pass through -ffp-model option.
2644*480093f4SDimitry Andric     case options::OPT_ffp_model_EQ:
2645*480093f4SDimitry Andric       // This should only occur in the error case
2646*480093f4SDimitry Andric       // since the optID has been replaced by a more granular
2647*480093f4SDimitry Andric       // floating point option.
2648*480093f4SDimitry Andric       break;
2649*480093f4SDimitry Andric 
2650*480093f4SDimitry Andric     // Validate and pass through -ffp-exception-behavior option.
2651*480093f4SDimitry Andric     case options::OPT_ffp_exception_behavior_EQ: {
2652*480093f4SDimitry Andric       StringRef Val = A->getValue();
2653*480093f4SDimitry Andric       if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
2654*480093f4SDimitry Andric           !FPExceptionBehavior.equals(Val))
2655*480093f4SDimitry Andric         // Warn that previous value of option is overridden.
2656*480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2657*480093f4SDimitry Andric           << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior)
2658*480093f4SDimitry Andric           << Args.MakeArgString("-ffp-exception-behavior=" + Val);
2659*480093f4SDimitry Andric       TrappingMath = TrappingMathPresent = false;
2660*480093f4SDimitry Andric       if (Val.equals("ignore") || Val.equals("maytrap"))
2661*480093f4SDimitry Andric         FPExceptionBehavior = Val;
2662*480093f4SDimitry Andric       else if (Val.equals("strict")) {
2663*480093f4SDimitry Andric         FPExceptionBehavior = Val;
2664*480093f4SDimitry Andric         TrappingMath = TrappingMathPresent = true;
2665*480093f4SDimitry Andric       } else
2666*480093f4SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
2667*480093f4SDimitry Andric             << A->getOption().getName() << Val;
2668*480093f4SDimitry Andric       break;
2669*480093f4SDimitry Andric     }
2670*480093f4SDimitry Andric 
26710b57cec5SDimitry Andric     case options::OPT_ffinite_math_only:
26720b57cec5SDimitry Andric       HonorINFs = false;
26730b57cec5SDimitry Andric       HonorNaNs = false;
26740b57cec5SDimitry Andric       break;
26750b57cec5SDimitry Andric     case options::OPT_fno_finite_math_only:
26760b57cec5SDimitry Andric       HonorINFs = true;
26770b57cec5SDimitry Andric       HonorNaNs = true;
26780b57cec5SDimitry Andric       break;
26790b57cec5SDimitry Andric 
26800b57cec5SDimitry Andric     case options::OPT_funsafe_math_optimizations:
26810b57cec5SDimitry Andric       AssociativeMath = true;
26820b57cec5SDimitry Andric       ReciprocalMath = true;
26830b57cec5SDimitry Andric       SignedZeros = false;
26840b57cec5SDimitry Andric       TrappingMath = false;
2685*480093f4SDimitry Andric       FPExceptionBehavior = "";
26860b57cec5SDimitry Andric       break;
26870b57cec5SDimitry Andric     case options::OPT_fno_unsafe_math_optimizations:
26880b57cec5SDimitry Andric       AssociativeMath = false;
26890b57cec5SDimitry Andric       ReciprocalMath = false;
26900b57cec5SDimitry Andric       SignedZeros = true;
26910b57cec5SDimitry Andric       TrappingMath = true;
2692*480093f4SDimitry Andric       FPExceptionBehavior = "strict";
26930b57cec5SDimitry Andric       // -fno_unsafe_math_optimizations restores default denormal handling
26940b57cec5SDimitry Andric       DenormalFPMath = "";
26950b57cec5SDimitry Andric       break;
26960b57cec5SDimitry Andric 
26970b57cec5SDimitry Andric     case options::OPT_Ofast:
26980b57cec5SDimitry Andric       // If -Ofast is the optimization level, then -ffast-math should be enabled
26990b57cec5SDimitry Andric       if (!OFastEnabled)
27000b57cec5SDimitry Andric         continue;
27010b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
27020b57cec5SDimitry Andric     case options::OPT_ffast_math:
27030b57cec5SDimitry Andric       HonorINFs = false;
27040b57cec5SDimitry Andric       HonorNaNs = false;
27050b57cec5SDimitry Andric       MathErrno = false;
27060b57cec5SDimitry Andric       AssociativeMath = true;
27070b57cec5SDimitry Andric       ReciprocalMath = true;
27080b57cec5SDimitry Andric       SignedZeros = false;
27090b57cec5SDimitry Andric       TrappingMath = false;
2710*480093f4SDimitry Andric       RoundingFPMath = false;
27110b57cec5SDimitry Andric       // If fast-math is set then set the fp-contract mode to fast.
27120b57cec5SDimitry Andric       FPContract = "fast";
27130b57cec5SDimitry Andric       break;
27140b57cec5SDimitry Andric     case options::OPT_fno_fast_math:
27150b57cec5SDimitry Andric       HonorINFs = true;
27160b57cec5SDimitry Andric       HonorNaNs = true;
27170b57cec5SDimitry Andric       // Turning on -ffast-math (with either flag) removes the need for
27180b57cec5SDimitry Andric       // MathErrno. However, turning *off* -ffast-math merely restores the
27190b57cec5SDimitry Andric       // toolchain default (which may be false).
27200b57cec5SDimitry Andric       MathErrno = TC.IsMathErrnoDefault();
27210b57cec5SDimitry Andric       AssociativeMath = false;
27220b57cec5SDimitry Andric       ReciprocalMath = false;
27230b57cec5SDimitry Andric       SignedZeros = true;
2724*480093f4SDimitry Andric       TrappingMath = false;
2725*480093f4SDimitry Andric       RoundingFPMath = false;
27260b57cec5SDimitry Andric       // -fno_fast_math restores default denormal and fpcontract handling
27270b57cec5SDimitry Andric       DenormalFPMath = "";
27280b57cec5SDimitry Andric       FPContract = "";
27290b57cec5SDimitry Andric       break;
27300b57cec5SDimitry Andric     }
2731*480093f4SDimitry Andric     if (StrictFPModel) {
2732*480093f4SDimitry Andric       // If -ffp-model=strict has been specified on command line but
2733*480093f4SDimitry Andric       // subsequent options conflict then emit warning diagnostic.
2734*480093f4SDimitry Andric       if (HonorINFs && HonorNaNs &&
2735*480093f4SDimitry Andric         !AssociativeMath && !ReciprocalMath &&
2736*480093f4SDimitry Andric         SignedZeros && TrappingMath && RoundingFPMath &&
2737*480093f4SDimitry Andric         DenormalFPMath.empty() && FPContract.empty())
2738*480093f4SDimitry Andric         // OK: Current Arg doesn't conflict with -ffp-model=strict
2739*480093f4SDimitry Andric         ;
2740*480093f4SDimitry Andric       else {
2741*480093f4SDimitry Andric         StrictFPModel = false;
2742*480093f4SDimitry Andric         FPModel = "";
2743*480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2744*480093f4SDimitry Andric             << "-ffp-model=strict" <<
2745*480093f4SDimitry Andric             ((A->getNumValues() == 0) ?  A->getSpelling()
2746*480093f4SDimitry Andric             : Args.MakeArgString(A->getSpelling() + A->getValue()));
2747*480093f4SDimitry Andric       }
2748*480093f4SDimitry Andric     }
27490b57cec5SDimitry Andric 
27500b57cec5SDimitry Andric     // If we handled this option claim it
27510b57cec5SDimitry Andric     A->claim();
27520b57cec5SDimitry Andric   }
27530b57cec5SDimitry Andric 
27540b57cec5SDimitry Andric   if (!HonorINFs)
27550b57cec5SDimitry Andric     CmdArgs.push_back("-menable-no-infs");
27560b57cec5SDimitry Andric 
27570b57cec5SDimitry Andric   if (!HonorNaNs)
27580b57cec5SDimitry Andric     CmdArgs.push_back("-menable-no-nans");
27590b57cec5SDimitry Andric 
27600b57cec5SDimitry Andric   if (MathErrno)
27610b57cec5SDimitry Andric     CmdArgs.push_back("-fmath-errno");
27620b57cec5SDimitry Andric 
27630b57cec5SDimitry Andric   if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
27640b57cec5SDimitry Andric       !TrappingMath)
27650b57cec5SDimitry Andric     CmdArgs.push_back("-menable-unsafe-fp-math");
27660b57cec5SDimitry Andric 
27670b57cec5SDimitry Andric   if (!SignedZeros)
27680b57cec5SDimitry Andric     CmdArgs.push_back("-fno-signed-zeros");
27690b57cec5SDimitry Andric 
27700b57cec5SDimitry Andric   if (AssociativeMath && !SignedZeros && !TrappingMath)
27710b57cec5SDimitry Andric     CmdArgs.push_back("-mreassociate");
27720b57cec5SDimitry Andric 
27730b57cec5SDimitry Andric   if (ReciprocalMath)
27740b57cec5SDimitry Andric     CmdArgs.push_back("-freciprocal-math");
27750b57cec5SDimitry Andric 
2776*480093f4SDimitry Andric   if (TrappingMath) {
2777*480093f4SDimitry Andric     // FP Exception Behavior is also set to strict
2778*480093f4SDimitry Andric     assert(FPExceptionBehavior.equals("strict"));
2779*480093f4SDimitry Andric     CmdArgs.push_back("-ftrapping-math");
2780*480093f4SDimitry Andric   } else if (TrappingMathPresent)
27810b57cec5SDimitry Andric     CmdArgs.push_back("-fno-trapping-math");
27820b57cec5SDimitry Andric 
27830b57cec5SDimitry Andric   if (!DenormalFPMath.empty())
27840b57cec5SDimitry Andric     CmdArgs.push_back(
27850b57cec5SDimitry Andric         Args.MakeArgString("-fdenormal-fp-math=" + DenormalFPMath));
27860b57cec5SDimitry Andric 
27870b57cec5SDimitry Andric   if (!FPContract.empty())
27880b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract));
27890b57cec5SDimitry Andric 
2790*480093f4SDimitry Andric   if (!RoundingFPMath)
2791*480093f4SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-fno-rounding-math"));
2792*480093f4SDimitry Andric 
2793*480093f4SDimitry Andric   if (RoundingFPMath && RoundingMathPresent)
2794*480093f4SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-frounding-math"));
2795*480093f4SDimitry Andric 
2796*480093f4SDimitry Andric   if (!FPExceptionBehavior.empty())
2797*480093f4SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-ffp-exception-behavior=" +
2798*480093f4SDimitry Andric                       FPExceptionBehavior));
2799*480093f4SDimitry Andric 
28000b57cec5SDimitry Andric   ParseMRecip(D, Args, CmdArgs);
28010b57cec5SDimitry Andric 
28020b57cec5SDimitry Andric   // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for the
28030b57cec5SDimitry Andric   // individual features enabled by -ffast-math instead of the option itself as
28040b57cec5SDimitry Andric   // that's consistent with gcc's behaviour.
28050b57cec5SDimitry Andric   if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath &&
2806*480093f4SDimitry Andric       ReciprocalMath && !SignedZeros && !TrappingMath && !RoundingFPMath) {
28070b57cec5SDimitry Andric     CmdArgs.push_back("-ffast-math");
2808*480093f4SDimitry Andric     if (FPModel.equals("fast")) {
2809*480093f4SDimitry Andric       if (FPContract.equals("fast"))
2810*480093f4SDimitry Andric         // All set, do nothing.
2811*480093f4SDimitry Andric         ;
2812*480093f4SDimitry Andric       else if (FPContract.empty())
2813*480093f4SDimitry Andric         // Enable -ffp-contract=fast
2814*480093f4SDimitry Andric         CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
2815*480093f4SDimitry Andric       else
2816*480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2817*480093f4SDimitry Andric           << "-ffp-model=fast"
2818*480093f4SDimitry Andric           << Args.MakeArgString("-ffp-contract=" + FPContract);
2819*480093f4SDimitry Andric     }
2820*480093f4SDimitry Andric   }
28210b57cec5SDimitry Andric 
28220b57cec5SDimitry Andric   // Handle __FINITE_MATH_ONLY__ similarly.
28230b57cec5SDimitry Andric   if (!HonorINFs && !HonorNaNs)
28240b57cec5SDimitry Andric     CmdArgs.push_back("-ffinite-math-only");
28250b57cec5SDimitry Andric 
28260b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) {
28270b57cec5SDimitry Andric     CmdArgs.push_back("-mfpmath");
28280b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
28290b57cec5SDimitry Andric   }
28300b57cec5SDimitry Andric 
28310b57cec5SDimitry Andric   // Disable a codegen optimization for floating-point casts.
28320b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fno_strict_float_cast_overflow,
28330b57cec5SDimitry Andric                    options::OPT_fstrict_float_cast_overflow, false))
28340b57cec5SDimitry Andric     CmdArgs.push_back("-fno-strict-float-cast-overflow");
28350b57cec5SDimitry Andric }
28360b57cec5SDimitry Andric 
28370b57cec5SDimitry Andric static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs,
28380b57cec5SDimitry Andric                                   const llvm::Triple &Triple,
28390b57cec5SDimitry Andric                                   const InputInfo &Input) {
28400b57cec5SDimitry Andric   // Enable region store model by default.
28410b57cec5SDimitry Andric   CmdArgs.push_back("-analyzer-store=region");
28420b57cec5SDimitry Andric 
28430b57cec5SDimitry Andric   // Treat blocks as analysis entry points.
28440b57cec5SDimitry Andric   CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
28450b57cec5SDimitry Andric 
28460b57cec5SDimitry Andric   // Add default argument set.
28470b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
28480b57cec5SDimitry Andric     CmdArgs.push_back("-analyzer-checker=core");
28490b57cec5SDimitry Andric     CmdArgs.push_back("-analyzer-checker=apiModeling");
28500b57cec5SDimitry Andric 
28510b57cec5SDimitry Andric     if (!Triple.isWindowsMSVCEnvironment()) {
28520b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix");
28530b57cec5SDimitry Andric     } else {
28540b57cec5SDimitry Andric       // Enable "unix" checkers that also work on Windows.
28550b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix.API");
28560b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix.Malloc");
28570b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix.MallocSizeof");
28580b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix.MismatchedDeallocator");
28590b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix.cstring.BadSizeArg");
28600b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix.cstring.NullArg");
28610b57cec5SDimitry Andric     }
28620b57cec5SDimitry Andric 
28630b57cec5SDimitry Andric     // Disable some unix checkers for PS4.
28640b57cec5SDimitry Andric     if (Triple.isPS4CPU()) {
28650b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-disable-checker=unix.API");
28660b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-disable-checker=unix.Vfork");
28670b57cec5SDimitry Andric     }
28680b57cec5SDimitry Andric 
2869*480093f4SDimitry Andric     if (Triple.isOSDarwin()) {
28700b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=osx");
2871*480093f4SDimitry Andric       CmdArgs.push_back(
2872*480093f4SDimitry Andric           "-analyzer-checker=security.insecureAPI.decodeValueOfObjCType");
2873*480093f4SDimitry Andric     }
2874*480093f4SDimitry Andric     else if (Triple.isOSFuchsia())
2875*480093f4SDimitry Andric       CmdArgs.push_back("-analyzer-checker=fuchsia");
28760b57cec5SDimitry Andric 
28770b57cec5SDimitry Andric     CmdArgs.push_back("-analyzer-checker=deadcode");
28780b57cec5SDimitry Andric 
28790b57cec5SDimitry Andric     if (types::isCXX(Input.getType()))
28800b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=cplusplus");
28810b57cec5SDimitry Andric 
28820b57cec5SDimitry Andric     if (!Triple.isPS4CPU()) {
28830b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn");
28840b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
28850b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
28860b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
28870b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
28880b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
28890b57cec5SDimitry Andric     }
28900b57cec5SDimitry Andric 
28910b57cec5SDimitry Andric     // Default nullability checks.
28920b57cec5SDimitry Andric     CmdArgs.push_back("-analyzer-checker=nullability.NullPassedToNonnull");
28930b57cec5SDimitry Andric     CmdArgs.push_back("-analyzer-checker=nullability.NullReturnedFromNonnull");
28940b57cec5SDimitry Andric   }
28950b57cec5SDimitry Andric 
28960b57cec5SDimitry Andric   // Set the output format. The default is plist, for (lame) historical reasons.
28970b57cec5SDimitry Andric   CmdArgs.push_back("-analyzer-output");
28980b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
28990b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
29000b57cec5SDimitry Andric   else
29010b57cec5SDimitry Andric     CmdArgs.push_back("plist");
29020b57cec5SDimitry Andric 
29030b57cec5SDimitry Andric   // Disable the presentation of standard compiler warnings when using
29040b57cec5SDimitry Andric   // --analyze.  We only want to show static analyzer diagnostics or frontend
29050b57cec5SDimitry Andric   // errors.
29060b57cec5SDimitry Andric   CmdArgs.push_back("-w");
29070b57cec5SDimitry Andric 
29080b57cec5SDimitry Andric   // Add -Xanalyzer arguments when running as analyzer.
29090b57cec5SDimitry Andric   Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
29100b57cec5SDimitry Andric }
29110b57cec5SDimitry Andric 
29120b57cec5SDimitry Andric static void RenderSSPOptions(const ToolChain &TC, const ArgList &Args,
29130b57cec5SDimitry Andric                              ArgStringList &CmdArgs, bool KernelOrKext) {
29140b57cec5SDimitry Andric   const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple();
29150b57cec5SDimitry Andric 
29160b57cec5SDimitry Andric   // NVPTX doesn't support stack protectors; from the compiler's perspective, it
29170b57cec5SDimitry Andric   // doesn't even have a stack!
29180b57cec5SDimitry Andric   if (EffectiveTriple.isNVPTX())
29190b57cec5SDimitry Andric     return;
29200b57cec5SDimitry Andric 
29210b57cec5SDimitry Andric   // -stack-protector=0 is default.
29220b57cec5SDimitry Andric   unsigned StackProtectorLevel = 0;
29230b57cec5SDimitry Andric   unsigned DefaultStackProtectorLevel =
29240b57cec5SDimitry Andric       TC.GetDefaultStackProtectorLevel(KernelOrKext);
29250b57cec5SDimitry Andric 
29260b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
29270b57cec5SDimitry Andric                                options::OPT_fstack_protector_all,
29280b57cec5SDimitry Andric                                options::OPT_fstack_protector_strong,
29290b57cec5SDimitry Andric                                options::OPT_fstack_protector)) {
29300b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fstack_protector))
29310b57cec5SDimitry Andric       StackProtectorLevel =
29320b57cec5SDimitry Andric           std::max<unsigned>(LangOptions::SSPOn, DefaultStackProtectorLevel);
29330b57cec5SDimitry Andric     else if (A->getOption().matches(options::OPT_fstack_protector_strong))
29340b57cec5SDimitry Andric       StackProtectorLevel = LangOptions::SSPStrong;
29350b57cec5SDimitry Andric     else if (A->getOption().matches(options::OPT_fstack_protector_all))
29360b57cec5SDimitry Andric       StackProtectorLevel = LangOptions::SSPReq;
29370b57cec5SDimitry Andric   } else {
29380b57cec5SDimitry Andric     StackProtectorLevel = DefaultStackProtectorLevel;
29390b57cec5SDimitry Andric   }
29400b57cec5SDimitry Andric 
29410b57cec5SDimitry Andric   if (StackProtectorLevel) {
29420b57cec5SDimitry Andric     CmdArgs.push_back("-stack-protector");
29430b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
29440b57cec5SDimitry Andric   }
29450b57cec5SDimitry Andric 
29460b57cec5SDimitry Andric   // --param ssp-buffer-size=
29470b57cec5SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT__param)) {
29480b57cec5SDimitry Andric     StringRef Str(A->getValue());
29490b57cec5SDimitry Andric     if (Str.startswith("ssp-buffer-size=")) {
29500b57cec5SDimitry Andric       if (StackProtectorLevel) {
29510b57cec5SDimitry Andric         CmdArgs.push_back("-stack-protector-buffer-size");
29520b57cec5SDimitry Andric         // FIXME: Verify the argument is a valid integer.
29530b57cec5SDimitry Andric         CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
29540b57cec5SDimitry Andric       }
29550b57cec5SDimitry Andric       A->claim();
29560b57cec5SDimitry Andric     }
29570b57cec5SDimitry Andric   }
29580b57cec5SDimitry Andric }
29590b57cec5SDimitry Andric 
29600b57cec5SDimitry Andric static void RenderTrivialAutoVarInitOptions(const Driver &D,
29610b57cec5SDimitry Andric                                             const ToolChain &TC,
29620b57cec5SDimitry Andric                                             const ArgList &Args,
29630b57cec5SDimitry Andric                                             ArgStringList &CmdArgs) {
29640b57cec5SDimitry Andric   auto DefaultTrivialAutoVarInit = TC.GetDefaultTrivialAutoVarInit();
29650b57cec5SDimitry Andric   StringRef TrivialAutoVarInit = "";
29660b57cec5SDimitry Andric 
29670b57cec5SDimitry Andric   for (const Arg *A : Args) {
29680b57cec5SDimitry Andric     switch (A->getOption().getID()) {
29690b57cec5SDimitry Andric     default:
29700b57cec5SDimitry Andric       continue;
29710b57cec5SDimitry Andric     case options::OPT_ftrivial_auto_var_init: {
29720b57cec5SDimitry Andric       A->claim();
29730b57cec5SDimitry Andric       StringRef Val = A->getValue();
29740b57cec5SDimitry Andric       if (Val == "uninitialized" || Val == "zero" || Val == "pattern")
29750b57cec5SDimitry Andric         TrivialAutoVarInit = Val;
29760b57cec5SDimitry Andric       else
29770b57cec5SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
29780b57cec5SDimitry Andric             << A->getOption().getName() << Val;
29790b57cec5SDimitry Andric       break;
29800b57cec5SDimitry Andric     }
29810b57cec5SDimitry Andric     }
29820b57cec5SDimitry Andric   }
29830b57cec5SDimitry Andric 
29840b57cec5SDimitry Andric   if (TrivialAutoVarInit.empty())
29850b57cec5SDimitry Andric     switch (DefaultTrivialAutoVarInit) {
29860b57cec5SDimitry Andric     case LangOptions::TrivialAutoVarInitKind::Uninitialized:
29870b57cec5SDimitry Andric       break;
29880b57cec5SDimitry Andric     case LangOptions::TrivialAutoVarInitKind::Pattern:
29890b57cec5SDimitry Andric       TrivialAutoVarInit = "pattern";
29900b57cec5SDimitry Andric       break;
29910b57cec5SDimitry Andric     case LangOptions::TrivialAutoVarInitKind::Zero:
29920b57cec5SDimitry Andric       TrivialAutoVarInit = "zero";
29930b57cec5SDimitry Andric       break;
29940b57cec5SDimitry Andric     }
29950b57cec5SDimitry Andric 
29960b57cec5SDimitry Andric   if (!TrivialAutoVarInit.empty()) {
29970b57cec5SDimitry Andric     if (TrivialAutoVarInit == "zero" && !Args.hasArg(options::OPT_enable_trivial_var_init_zero))
29980b57cec5SDimitry Andric       D.Diag(diag::err_drv_trivial_auto_var_init_zero_disabled);
29990b57cec5SDimitry Andric     CmdArgs.push_back(
30000b57cec5SDimitry Andric         Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit));
30010b57cec5SDimitry Andric   }
30020b57cec5SDimitry Andric }
30030b57cec5SDimitry Andric 
30040b57cec5SDimitry Andric static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs) {
30050b57cec5SDimitry Andric   const unsigned ForwardedArguments[] = {
30060b57cec5SDimitry Andric       options::OPT_cl_opt_disable,
30070b57cec5SDimitry Andric       options::OPT_cl_strict_aliasing,
30080b57cec5SDimitry Andric       options::OPT_cl_single_precision_constant,
30090b57cec5SDimitry Andric       options::OPT_cl_finite_math_only,
30100b57cec5SDimitry Andric       options::OPT_cl_kernel_arg_info,
30110b57cec5SDimitry Andric       options::OPT_cl_unsafe_math_optimizations,
30120b57cec5SDimitry Andric       options::OPT_cl_fast_relaxed_math,
30130b57cec5SDimitry Andric       options::OPT_cl_mad_enable,
30140b57cec5SDimitry Andric       options::OPT_cl_no_signed_zeros,
30150b57cec5SDimitry Andric       options::OPT_cl_denorms_are_zero,
30160b57cec5SDimitry Andric       options::OPT_cl_fp32_correctly_rounded_divide_sqrt,
30170b57cec5SDimitry Andric       options::OPT_cl_uniform_work_group_size
30180b57cec5SDimitry Andric   };
30190b57cec5SDimitry Andric 
30200b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_cl_std_EQ)) {
30210b57cec5SDimitry Andric     std::string CLStdStr = std::string("-cl-std=") + A->getValue();
30220b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(CLStdStr));
30230b57cec5SDimitry Andric   }
30240b57cec5SDimitry Andric 
30250b57cec5SDimitry Andric   for (const auto &Arg : ForwardedArguments)
30260b57cec5SDimitry Andric     if (const auto *A = Args.getLastArg(Arg))
30270b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName()));
30280b57cec5SDimitry Andric }
30290b57cec5SDimitry Andric 
30300b57cec5SDimitry Andric static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args,
30310b57cec5SDimitry Andric                                         ArgStringList &CmdArgs) {
30320b57cec5SDimitry Andric   bool ARCMTEnabled = false;
30330b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) {
30340b57cec5SDimitry Andric     if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
30350b57cec5SDimitry Andric                                        options::OPT_ccc_arcmt_modify,
30360b57cec5SDimitry Andric                                        options::OPT_ccc_arcmt_migrate)) {
30370b57cec5SDimitry Andric       ARCMTEnabled = true;
30380b57cec5SDimitry Andric       switch (A->getOption().getID()) {
30390b57cec5SDimitry Andric       default: llvm_unreachable("missed a case");
30400b57cec5SDimitry Andric       case options::OPT_ccc_arcmt_check:
30410b57cec5SDimitry Andric         CmdArgs.push_back("-arcmt-check");
30420b57cec5SDimitry Andric         break;
30430b57cec5SDimitry Andric       case options::OPT_ccc_arcmt_modify:
30440b57cec5SDimitry Andric         CmdArgs.push_back("-arcmt-modify");
30450b57cec5SDimitry Andric         break;
30460b57cec5SDimitry Andric       case options::OPT_ccc_arcmt_migrate:
30470b57cec5SDimitry Andric         CmdArgs.push_back("-arcmt-migrate");
30480b57cec5SDimitry Andric         CmdArgs.push_back("-mt-migrate-directory");
30490b57cec5SDimitry Andric         CmdArgs.push_back(A->getValue());
30500b57cec5SDimitry Andric 
30510b57cec5SDimitry Andric         Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
30520b57cec5SDimitry Andric         Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
30530b57cec5SDimitry Andric         break;
30540b57cec5SDimitry Andric       }
30550b57cec5SDimitry Andric     }
30560b57cec5SDimitry Andric   } else {
30570b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_ccc_arcmt_check);
30580b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify);
30590b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate);
30600b57cec5SDimitry Andric   }
30610b57cec5SDimitry Andric 
30620b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
30630b57cec5SDimitry Andric     if (ARCMTEnabled)
30640b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_not_allowed_with)
30650b57cec5SDimitry Andric           << A->getAsString(Args) << "-ccc-arcmt-migrate";
30660b57cec5SDimitry Andric 
30670b57cec5SDimitry Andric     CmdArgs.push_back("-mt-migrate-directory");
30680b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
30690b57cec5SDimitry Andric 
30700b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
30710b57cec5SDimitry Andric                      options::OPT_objcmt_migrate_subscripting,
30720b57cec5SDimitry Andric                      options::OPT_objcmt_migrate_property)) {
30730b57cec5SDimitry Andric       // None specified, means enable them all.
30740b57cec5SDimitry Andric       CmdArgs.push_back("-objcmt-migrate-literals");
30750b57cec5SDimitry Andric       CmdArgs.push_back("-objcmt-migrate-subscripting");
30760b57cec5SDimitry Andric       CmdArgs.push_back("-objcmt-migrate-property");
30770b57cec5SDimitry Andric     } else {
30780b57cec5SDimitry Andric       Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
30790b57cec5SDimitry Andric       Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
30800b57cec5SDimitry Andric       Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
30810b57cec5SDimitry Andric     }
30820b57cec5SDimitry Andric   } else {
30830b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
30840b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
30850b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
30860b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all);
30870b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property);
30880b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property);
30890b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property_dot_syntax);
30900b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation);
30910b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype);
30920b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros);
30930b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance);
30940b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property);
30950b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property);
30960b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly);
30970b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init);
30980b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_whitelist_dir_path);
30990b57cec5SDimitry Andric   }
31000b57cec5SDimitry Andric }
31010b57cec5SDimitry Andric 
31020b57cec5SDimitry Andric static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T,
31030b57cec5SDimitry Andric                                  const ArgList &Args, ArgStringList &CmdArgs) {
31040b57cec5SDimitry Andric   // -fbuiltin is default unless -mkernel is used.
31050b57cec5SDimitry Andric   bool UseBuiltins =
31060b57cec5SDimitry Andric       Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin,
31070b57cec5SDimitry Andric                    !Args.hasArg(options::OPT_mkernel));
31080b57cec5SDimitry Andric   if (!UseBuiltins)
31090b57cec5SDimitry Andric     CmdArgs.push_back("-fno-builtin");
31100b57cec5SDimitry Andric 
31110b57cec5SDimitry Andric   // -ffreestanding implies -fno-builtin.
31120b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_ffreestanding))
31130b57cec5SDimitry Andric     UseBuiltins = false;
31140b57cec5SDimitry Andric 
31150b57cec5SDimitry Andric   // Process the -fno-builtin-* options.
31160b57cec5SDimitry Andric   for (const auto &Arg : Args) {
31170b57cec5SDimitry Andric     const Option &O = Arg->getOption();
31180b57cec5SDimitry Andric     if (!O.matches(options::OPT_fno_builtin_))
31190b57cec5SDimitry Andric       continue;
31200b57cec5SDimitry Andric 
31210b57cec5SDimitry Andric     Arg->claim();
31220b57cec5SDimitry Andric 
31230b57cec5SDimitry Andric     // If -fno-builtin is specified, then there's no need to pass the option to
31240b57cec5SDimitry Andric     // the frontend.
31250b57cec5SDimitry Andric     if (!UseBuiltins)
31260b57cec5SDimitry Andric       continue;
31270b57cec5SDimitry Andric 
31280b57cec5SDimitry Andric     StringRef FuncName = Arg->getValue();
31290b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-fno-builtin-" + FuncName));
31300b57cec5SDimitry Andric   }
31310b57cec5SDimitry Andric 
31320b57cec5SDimitry Andric   // le32-specific flags:
31330b57cec5SDimitry Andric   //  -fno-math-builtin: clang should not convert math builtins to intrinsics
31340b57cec5SDimitry Andric   //                     by default.
31350b57cec5SDimitry Andric   if (TC.getArch() == llvm::Triple::le32)
31360b57cec5SDimitry Andric     CmdArgs.push_back("-fno-math-builtin");
31370b57cec5SDimitry Andric }
31380b57cec5SDimitry Andric 
31390b57cec5SDimitry Andric void Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) {
31400b57cec5SDimitry Andric   llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false, Result);
31410b57cec5SDimitry Andric   llvm::sys::path::append(Result, "org.llvm.clang.");
31420b57cec5SDimitry Andric   appendUserToPath(Result);
31430b57cec5SDimitry Andric   llvm::sys::path::append(Result, "ModuleCache");
31440b57cec5SDimitry Andric }
31450b57cec5SDimitry Andric 
31460b57cec5SDimitry Andric static void RenderModulesOptions(Compilation &C, const Driver &D,
31470b57cec5SDimitry Andric                                  const ArgList &Args, const InputInfo &Input,
31480b57cec5SDimitry Andric                                  const InputInfo &Output,
31490b57cec5SDimitry Andric                                  ArgStringList &CmdArgs, bool &HaveModules) {
31500b57cec5SDimitry Andric   // -fmodules enables the use of precompiled modules (off by default).
31510b57cec5SDimitry Andric   // Users can pass -fno-cxx-modules to turn off modules support for
31520b57cec5SDimitry Andric   // C++/Objective-C++ programs.
31530b57cec5SDimitry Andric   bool HaveClangModules = false;
31540b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
31550b57cec5SDimitry Andric     bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
31560b57cec5SDimitry Andric                                      options::OPT_fno_cxx_modules, true);
31570b57cec5SDimitry Andric     if (AllowedInCXX || !types::isCXX(Input.getType())) {
31580b57cec5SDimitry Andric       CmdArgs.push_back("-fmodules");
31590b57cec5SDimitry Andric       HaveClangModules = true;
31600b57cec5SDimitry Andric     }
31610b57cec5SDimitry Andric   }
31620b57cec5SDimitry Andric 
31630b57cec5SDimitry Andric   HaveModules |= HaveClangModules;
31640b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fmodules_ts)) {
31650b57cec5SDimitry Andric     CmdArgs.push_back("-fmodules-ts");
31660b57cec5SDimitry Andric     HaveModules = true;
31670b57cec5SDimitry Andric   }
31680b57cec5SDimitry Andric 
31690b57cec5SDimitry Andric   // -fmodule-maps enables implicit reading of module map files. By default,
31700b57cec5SDimitry Andric   // this is enabled if we are using Clang's flavor of precompiled modules.
31710b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fimplicit_module_maps,
31720b57cec5SDimitry Andric                    options::OPT_fno_implicit_module_maps, HaveClangModules))
31730b57cec5SDimitry Andric     CmdArgs.push_back("-fimplicit-module-maps");
31740b57cec5SDimitry Andric 
31750b57cec5SDimitry Andric   // -fmodules-decluse checks that modules used are declared so (off by default)
31760b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fmodules_decluse,
31770b57cec5SDimitry Andric                    options::OPT_fno_modules_decluse, false))
31780b57cec5SDimitry Andric     CmdArgs.push_back("-fmodules-decluse");
31790b57cec5SDimitry Andric 
31800b57cec5SDimitry Andric   // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that
31810b57cec5SDimitry Andric   // all #included headers are part of modules.
31820b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fmodules_strict_decluse,
31830b57cec5SDimitry Andric                    options::OPT_fno_modules_strict_decluse, false))
31840b57cec5SDimitry Andric     CmdArgs.push_back("-fmodules-strict-decluse");
31850b57cec5SDimitry Andric 
31860b57cec5SDimitry Andric   // -fno-implicit-modules turns off implicitly compiling modules on demand.
31870b57cec5SDimitry Andric   bool ImplicitModules = false;
31880b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fimplicit_modules,
31890b57cec5SDimitry Andric                     options::OPT_fno_implicit_modules, HaveClangModules)) {
31900b57cec5SDimitry Andric     if (HaveModules)
31910b57cec5SDimitry Andric       CmdArgs.push_back("-fno-implicit-modules");
31920b57cec5SDimitry Andric   } else if (HaveModules) {
31930b57cec5SDimitry Andric     ImplicitModules = true;
31940b57cec5SDimitry Andric     // -fmodule-cache-path specifies where our implicitly-built module files
31950b57cec5SDimitry Andric     // should be written.
31960b57cec5SDimitry Andric     SmallString<128> Path;
31970b57cec5SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path))
31980b57cec5SDimitry Andric       Path = A->getValue();
31990b57cec5SDimitry Andric 
32000b57cec5SDimitry Andric     if (C.isForDiagnostics()) {
32010b57cec5SDimitry Andric       // When generating crash reports, we want to emit the modules along with
32020b57cec5SDimitry Andric       // the reproduction sources, so we ignore any provided module path.
32030b57cec5SDimitry Andric       Path = Output.getFilename();
32040b57cec5SDimitry Andric       llvm::sys::path::replace_extension(Path, ".cache");
32050b57cec5SDimitry Andric       llvm::sys::path::append(Path, "modules");
32060b57cec5SDimitry Andric     } else if (Path.empty()) {
32070b57cec5SDimitry Andric       // No module path was provided: use the default.
32080b57cec5SDimitry Andric       Driver::getDefaultModuleCachePath(Path);
32090b57cec5SDimitry Andric     }
32100b57cec5SDimitry Andric 
32110b57cec5SDimitry Andric     const char Arg[] = "-fmodules-cache-path=";
32120b57cec5SDimitry Andric     Path.insert(Path.begin(), Arg, Arg + strlen(Arg));
32130b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Path));
32140b57cec5SDimitry Andric   }
32150b57cec5SDimitry Andric 
32160b57cec5SDimitry Andric   if (HaveModules) {
32170b57cec5SDimitry Andric     // -fprebuilt-module-path specifies where to load the prebuilt module files.
32180b57cec5SDimitry Andric     for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) {
32190b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(
32200b57cec5SDimitry Andric           std::string("-fprebuilt-module-path=") + A->getValue()));
32210b57cec5SDimitry Andric       A->claim();
32220b57cec5SDimitry Andric     }
3223a7dea167SDimitry Andric     if (Args.hasFlag(options::OPT_fmodules_validate_input_files_content,
3224a7dea167SDimitry Andric                      options::OPT_fno_modules_validate_input_files_content,
3225a7dea167SDimitry Andric                      false))
3226a7dea167SDimitry Andric       CmdArgs.push_back("-fvalidate-ast-input-files-content");
32270b57cec5SDimitry Andric   }
32280b57cec5SDimitry Andric 
32290b57cec5SDimitry Andric   // -fmodule-name specifies the module that is currently being built (or
32300b57cec5SDimitry Andric   // used for header checking by -fmodule-maps).
32310b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fmodule_name_EQ);
32320b57cec5SDimitry Andric 
32330b57cec5SDimitry Andric   // -fmodule-map-file can be used to specify files containing module
32340b57cec5SDimitry Andric   // definitions.
32350b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file);
32360b57cec5SDimitry Andric 
32370b57cec5SDimitry Andric   // -fbuiltin-module-map can be used to load the clang
32380b57cec5SDimitry Andric   // builtin headers modulemap file.
32390b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fbuiltin_module_map)) {
32400b57cec5SDimitry Andric     SmallString<128> BuiltinModuleMap(D.ResourceDir);
32410b57cec5SDimitry Andric     llvm::sys::path::append(BuiltinModuleMap, "include");
32420b57cec5SDimitry Andric     llvm::sys::path::append(BuiltinModuleMap, "module.modulemap");
32430b57cec5SDimitry Andric     if (llvm::sys::fs::exists(BuiltinModuleMap))
32440b57cec5SDimitry Andric       CmdArgs.push_back(
32450b57cec5SDimitry Andric           Args.MakeArgString("-fmodule-map-file=" + BuiltinModuleMap));
32460b57cec5SDimitry Andric   }
32470b57cec5SDimitry Andric 
32480b57cec5SDimitry Andric   // The -fmodule-file=<name>=<file> form specifies the mapping of module
32490b57cec5SDimitry Andric   // names to precompiled module files (the module is loaded only if used).
32500b57cec5SDimitry Andric   // The -fmodule-file=<file> form can be used to unconditionally load
32510b57cec5SDimitry Andric   // precompiled module files (whether used or not).
32520b57cec5SDimitry Andric   if (HaveModules)
32530b57cec5SDimitry Andric     Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file);
32540b57cec5SDimitry Andric   else
32550b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fmodule_file);
32560b57cec5SDimitry Andric 
32570b57cec5SDimitry Andric   // When building modules and generating crashdumps, we need to dump a module
32580b57cec5SDimitry Andric   // dependency VFS alongside the output.
32590b57cec5SDimitry Andric   if (HaveClangModules && C.isForDiagnostics()) {
32600b57cec5SDimitry Andric     SmallString<128> VFSDir(Output.getFilename());
32610b57cec5SDimitry Andric     llvm::sys::path::replace_extension(VFSDir, ".cache");
32620b57cec5SDimitry Andric     // Add the cache directory as a temp so the crash diagnostics pick it up.
32630b57cec5SDimitry Andric     C.addTempFile(Args.MakeArgString(VFSDir));
32640b57cec5SDimitry Andric 
32650b57cec5SDimitry Andric     llvm::sys::path::append(VFSDir, "vfs");
32660b57cec5SDimitry Andric     CmdArgs.push_back("-module-dependency-dir");
32670b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(VFSDir));
32680b57cec5SDimitry Andric   }
32690b57cec5SDimitry Andric 
32700b57cec5SDimitry Andric   if (HaveClangModules)
32710b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path);
32720b57cec5SDimitry Andric 
32730b57cec5SDimitry Andric   // Pass through all -fmodules-ignore-macro arguments.
32740b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
32750b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
32760b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
32770b57cec5SDimitry Andric 
32780b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp);
32790b57cec5SDimitry Andric 
32800b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) {
32810b57cec5SDimitry Andric     if (Args.hasArg(options::OPT_fbuild_session_timestamp))
32820b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_not_allowed_with)
32830b57cec5SDimitry Andric           << A->getAsString(Args) << "-fbuild-session-timestamp";
32840b57cec5SDimitry Andric 
32850b57cec5SDimitry Andric     llvm::sys::fs::file_status Status;
32860b57cec5SDimitry Andric     if (llvm::sys::fs::status(A->getValue(), Status))
32870b57cec5SDimitry Andric       D.Diag(diag::err_drv_no_such_file) << A->getValue();
32880b57cec5SDimitry Andric     CmdArgs.push_back(
32890b57cec5SDimitry Andric         Args.MakeArgString("-fbuild-session-timestamp=" +
32900b57cec5SDimitry Andric                            Twine((uint64_t)Status.getLastModificationTime()
32910b57cec5SDimitry Andric                                      .time_since_epoch()
32920b57cec5SDimitry Andric                                      .count())));
32930b57cec5SDimitry Andric   }
32940b57cec5SDimitry Andric 
32950b57cec5SDimitry Andric   if (Args.getLastArg(options::OPT_fmodules_validate_once_per_build_session)) {
32960b57cec5SDimitry Andric     if (!Args.getLastArg(options::OPT_fbuild_session_timestamp,
32970b57cec5SDimitry Andric                          options::OPT_fbuild_session_file))
32980b57cec5SDimitry Andric       D.Diag(diag::err_drv_modules_validate_once_requires_timestamp);
32990b57cec5SDimitry Andric 
33000b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs,
33010b57cec5SDimitry Andric                     options::OPT_fmodules_validate_once_per_build_session);
33020b57cec5SDimitry Andric   }
33030b57cec5SDimitry Andric 
33040b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fmodules_validate_system_headers,
33050b57cec5SDimitry Andric                    options::OPT_fno_modules_validate_system_headers,
33060b57cec5SDimitry Andric                    ImplicitModules))
33070b57cec5SDimitry Andric     CmdArgs.push_back("-fmodules-validate-system-headers");
33080b57cec5SDimitry Andric 
33090b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fmodules_disable_diagnostic_validation);
33100b57cec5SDimitry Andric }
33110b57cec5SDimitry Andric 
33120b57cec5SDimitry Andric static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T,
33130b57cec5SDimitry Andric                                    ArgStringList &CmdArgs) {
33140b57cec5SDimitry Andric   // -fsigned-char is default.
33150b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fsigned_char,
33160b57cec5SDimitry Andric                                      options::OPT_fno_signed_char,
33170b57cec5SDimitry Andric                                      options::OPT_funsigned_char,
33180b57cec5SDimitry Andric                                      options::OPT_fno_unsigned_char)) {
33190b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_funsigned_char) ||
33200b57cec5SDimitry Andric         A->getOption().matches(options::OPT_fno_signed_char)) {
33210b57cec5SDimitry Andric       CmdArgs.push_back("-fno-signed-char");
33220b57cec5SDimitry Andric     }
33230b57cec5SDimitry Andric   } else if (!isSignedCharDefault(T)) {
33240b57cec5SDimitry Andric     CmdArgs.push_back("-fno-signed-char");
33250b57cec5SDimitry Andric   }
33260b57cec5SDimitry Andric 
33270b57cec5SDimitry Andric   // The default depends on the language standard.
33280b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fchar8__t, options::OPT_fno_char8__t);
33290b57cec5SDimitry Andric 
33300b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fshort_wchar,
33310b57cec5SDimitry Andric                                      options::OPT_fno_short_wchar)) {
33320b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fshort_wchar)) {
33330b57cec5SDimitry Andric       CmdArgs.push_back("-fwchar-type=short");
33340b57cec5SDimitry Andric       CmdArgs.push_back("-fno-signed-wchar");
33350b57cec5SDimitry Andric     } else {
33360b57cec5SDimitry Andric       bool IsARM = T.isARM() || T.isThumb() || T.isAArch64();
33370b57cec5SDimitry Andric       CmdArgs.push_back("-fwchar-type=int");
33380b57cec5SDimitry Andric       if (IsARM && !(T.isOSWindows() || T.isOSNetBSD() ||
33390b57cec5SDimitry Andric                      T.isOSOpenBSD()))
33400b57cec5SDimitry Andric         CmdArgs.push_back("-fno-signed-wchar");
33410b57cec5SDimitry Andric       else
33420b57cec5SDimitry Andric         CmdArgs.push_back("-fsigned-wchar");
33430b57cec5SDimitry Andric     }
33440b57cec5SDimitry Andric   }
33450b57cec5SDimitry Andric }
33460b57cec5SDimitry Andric 
33470b57cec5SDimitry Andric static void RenderObjCOptions(const ToolChain &TC, const Driver &D,
33480b57cec5SDimitry Andric                               const llvm::Triple &T, const ArgList &Args,
33490b57cec5SDimitry Andric                               ObjCRuntime &Runtime, bool InferCovariantReturns,
33500b57cec5SDimitry Andric                               const InputInfo &Input, ArgStringList &CmdArgs) {
33510b57cec5SDimitry Andric   const llvm::Triple::ArchType Arch = TC.getArch();
33520b57cec5SDimitry Andric 
33530b57cec5SDimitry Andric   // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and legacy
33540b57cec5SDimitry Andric   // is the default. Except for deployment target of 10.5, next runtime is
33550b57cec5SDimitry Andric   // always legacy dispatch and -fno-objc-legacy-dispatch gets ignored silently.
33560b57cec5SDimitry Andric   if (Runtime.isNonFragile()) {
33570b57cec5SDimitry Andric     if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
33580b57cec5SDimitry Andric                       options::OPT_fno_objc_legacy_dispatch,
33590b57cec5SDimitry Andric                       Runtime.isLegacyDispatchDefaultForArch(Arch))) {
33600b57cec5SDimitry Andric       if (TC.UseObjCMixedDispatch())
33610b57cec5SDimitry Andric         CmdArgs.push_back("-fobjc-dispatch-method=mixed");
33620b57cec5SDimitry Andric       else
33630b57cec5SDimitry Andric         CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
33640b57cec5SDimitry Andric     }
33650b57cec5SDimitry Andric   }
33660b57cec5SDimitry Andric 
33670b57cec5SDimitry Andric   // When ObjectiveC legacy runtime is in effect on MacOSX, turn on the option
33680b57cec5SDimitry Andric   // to do Array/Dictionary subscripting by default.
33690b57cec5SDimitry Andric   if (Arch == llvm::Triple::x86 && T.isMacOSX() &&
33700b57cec5SDimitry Andric       Runtime.getKind() == ObjCRuntime::FragileMacOSX && Runtime.isNeXTFamily())
33710b57cec5SDimitry Andric     CmdArgs.push_back("-fobjc-subscripting-legacy-runtime");
33720b57cec5SDimitry Andric 
33730b57cec5SDimitry Andric   // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
33740b57cec5SDimitry Andric   // NOTE: This logic is duplicated in ToolChains.cpp.
33750b57cec5SDimitry Andric   if (isObjCAutoRefCount(Args)) {
33760b57cec5SDimitry Andric     TC.CheckObjCARC();
33770b57cec5SDimitry Andric 
33780b57cec5SDimitry Andric     CmdArgs.push_back("-fobjc-arc");
33790b57cec5SDimitry Andric 
33800b57cec5SDimitry Andric     // FIXME: It seems like this entire block, and several around it should be
33810b57cec5SDimitry Andric     // wrapped in isObjC, but for now we just use it here as this is where it
33820b57cec5SDimitry Andric     // was being used previously.
33830b57cec5SDimitry Andric     if (types::isCXX(Input.getType()) && types::isObjC(Input.getType())) {
33840b57cec5SDimitry Andric       if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
33850b57cec5SDimitry Andric         CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
33860b57cec5SDimitry Andric       else
33870b57cec5SDimitry Andric         CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
33880b57cec5SDimitry Andric     }
33890b57cec5SDimitry Andric 
33900b57cec5SDimitry Andric     // Allow the user to enable full exceptions code emission.
33910b57cec5SDimitry Andric     // We default off for Objective-C, on for Objective-C++.
33920b57cec5SDimitry Andric     if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
33930b57cec5SDimitry Andric                      options::OPT_fno_objc_arc_exceptions,
33940b57cec5SDimitry Andric                      /*Default=*/types::isCXX(Input.getType())))
33950b57cec5SDimitry Andric       CmdArgs.push_back("-fobjc-arc-exceptions");
33960b57cec5SDimitry Andric   }
33970b57cec5SDimitry Andric 
33980b57cec5SDimitry Andric   // Silence warning for full exception code emission options when explicitly
33990b57cec5SDimitry Andric   // set to use no ARC.
34000b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fno_objc_arc)) {
34010b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fobjc_arc_exceptions);
34020b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fno_objc_arc_exceptions);
34030b57cec5SDimitry Andric   }
34040b57cec5SDimitry Andric 
34050b57cec5SDimitry Andric   // Allow the user to control whether messages can be converted to runtime
34060b57cec5SDimitry Andric   // functions.
34070b57cec5SDimitry Andric   if (types::isObjC(Input.getType())) {
34080b57cec5SDimitry Andric     auto *Arg = Args.getLastArg(
34090b57cec5SDimitry Andric         options::OPT_fobjc_convert_messages_to_runtime_calls,
34100b57cec5SDimitry Andric         options::OPT_fno_objc_convert_messages_to_runtime_calls);
34110b57cec5SDimitry Andric     if (Arg &&
34120b57cec5SDimitry Andric         Arg->getOption().matches(
34130b57cec5SDimitry Andric             options::OPT_fno_objc_convert_messages_to_runtime_calls))
34140b57cec5SDimitry Andric       CmdArgs.push_back("-fno-objc-convert-messages-to-runtime-calls");
34150b57cec5SDimitry Andric   }
34160b57cec5SDimitry Andric 
34170b57cec5SDimitry Andric   // -fobjc-infer-related-result-type is the default, except in the Objective-C
34180b57cec5SDimitry Andric   // rewriter.
34190b57cec5SDimitry Andric   if (InferCovariantReturns)
34200b57cec5SDimitry Andric     CmdArgs.push_back("-fno-objc-infer-related-result-type");
34210b57cec5SDimitry Andric 
34220b57cec5SDimitry Andric   // Pass down -fobjc-weak or -fno-objc-weak if present.
34230b57cec5SDimitry Andric   if (types::isObjC(Input.getType())) {
34240b57cec5SDimitry Andric     auto WeakArg =
34250b57cec5SDimitry Andric         Args.getLastArg(options::OPT_fobjc_weak, options::OPT_fno_objc_weak);
34260b57cec5SDimitry Andric     if (!WeakArg) {
34270b57cec5SDimitry Andric       // nothing to do
34280b57cec5SDimitry Andric     } else if (!Runtime.allowsWeak()) {
34290b57cec5SDimitry Andric       if (WeakArg->getOption().matches(options::OPT_fobjc_weak))
34300b57cec5SDimitry Andric         D.Diag(diag::err_objc_weak_unsupported);
34310b57cec5SDimitry Andric     } else {
34320b57cec5SDimitry Andric       WeakArg->render(Args, CmdArgs);
34330b57cec5SDimitry Andric     }
34340b57cec5SDimitry Andric   }
34350b57cec5SDimitry Andric }
34360b57cec5SDimitry Andric 
34370b57cec5SDimitry Andric static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args,
34380b57cec5SDimitry Andric                                      ArgStringList &CmdArgs) {
34390b57cec5SDimitry Andric   bool CaretDefault = true;
34400b57cec5SDimitry Andric   bool ColumnDefault = true;
34410b57cec5SDimitry Andric 
34420b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diagnostics_classic,
34430b57cec5SDimitry Andric                                      options::OPT__SLASH_diagnostics_column,
34440b57cec5SDimitry Andric                                      options::OPT__SLASH_diagnostics_caret)) {
34450b57cec5SDimitry Andric     switch (A->getOption().getID()) {
34460b57cec5SDimitry Andric     case options::OPT__SLASH_diagnostics_caret:
34470b57cec5SDimitry Andric       CaretDefault = true;
34480b57cec5SDimitry Andric       ColumnDefault = true;
34490b57cec5SDimitry Andric       break;
34500b57cec5SDimitry Andric     case options::OPT__SLASH_diagnostics_column:
34510b57cec5SDimitry Andric       CaretDefault = false;
34520b57cec5SDimitry Andric       ColumnDefault = true;
34530b57cec5SDimitry Andric       break;
34540b57cec5SDimitry Andric     case options::OPT__SLASH_diagnostics_classic:
34550b57cec5SDimitry Andric       CaretDefault = false;
34560b57cec5SDimitry Andric       ColumnDefault = false;
34570b57cec5SDimitry Andric       break;
34580b57cec5SDimitry Andric     }
34590b57cec5SDimitry Andric   }
34600b57cec5SDimitry Andric 
34610b57cec5SDimitry Andric   // -fcaret-diagnostics is default.
34620b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
34630b57cec5SDimitry Andric                     options::OPT_fno_caret_diagnostics, CaretDefault))
34640b57cec5SDimitry Andric     CmdArgs.push_back("-fno-caret-diagnostics");
34650b57cec5SDimitry Andric 
34660b57cec5SDimitry Andric   // -fdiagnostics-fixit-info is default, only pass non-default.
34670b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
34680b57cec5SDimitry Andric                     options::OPT_fno_diagnostics_fixit_info))
34690b57cec5SDimitry Andric     CmdArgs.push_back("-fno-diagnostics-fixit-info");
34700b57cec5SDimitry Andric 
34710b57cec5SDimitry Andric   // Enable -fdiagnostics-show-option by default.
34720b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
34730b57cec5SDimitry Andric                    options::OPT_fno_diagnostics_show_option))
34740b57cec5SDimitry Andric     CmdArgs.push_back("-fdiagnostics-show-option");
34750b57cec5SDimitry Andric 
34760b57cec5SDimitry Andric   if (const Arg *A =
34770b57cec5SDimitry Andric           Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
34780b57cec5SDimitry Andric     CmdArgs.push_back("-fdiagnostics-show-category");
34790b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
34800b57cec5SDimitry Andric   }
34810b57cec5SDimitry Andric 
34820b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdiagnostics_show_hotness,
34830b57cec5SDimitry Andric                    options::OPT_fno_diagnostics_show_hotness, false))
34840b57cec5SDimitry Andric     CmdArgs.push_back("-fdiagnostics-show-hotness");
34850b57cec5SDimitry Andric 
34860b57cec5SDimitry Andric   if (const Arg *A =
34870b57cec5SDimitry Andric           Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) {
34880b57cec5SDimitry Andric     std::string Opt =
34890b57cec5SDimitry Andric         std::string("-fdiagnostics-hotness-threshold=") + A->getValue();
34900b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Opt));
34910b57cec5SDimitry Andric   }
34920b57cec5SDimitry Andric 
34930b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
34940b57cec5SDimitry Andric     CmdArgs.push_back("-fdiagnostics-format");
34950b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
34960b57cec5SDimitry Andric   }
34970b57cec5SDimitry Andric 
34980b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(
34990b57cec5SDimitry Andric           options::OPT_fdiagnostics_show_note_include_stack,
35000b57cec5SDimitry Andric           options::OPT_fno_diagnostics_show_note_include_stack)) {
35010b57cec5SDimitry Andric     const Option &O = A->getOption();
35020b57cec5SDimitry Andric     if (O.matches(options::OPT_fdiagnostics_show_note_include_stack))
35030b57cec5SDimitry Andric       CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
35040b57cec5SDimitry Andric     else
35050b57cec5SDimitry Andric       CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
35060b57cec5SDimitry Andric   }
35070b57cec5SDimitry Andric 
35080b57cec5SDimitry Andric   // Color diagnostics are parsed by the driver directly from argv and later
35090b57cec5SDimitry Andric   // re-parsed to construct this job; claim any possible color diagnostic here
35100b57cec5SDimitry Andric   // to avoid warn_drv_unused_argument and diagnose bad
35110b57cec5SDimitry Andric   // OPT_fdiagnostics_color_EQ values.
35120b57cec5SDimitry Andric   for (const Arg *A : Args) {
35130b57cec5SDimitry Andric     const Option &O = A->getOption();
35140b57cec5SDimitry Andric     if (!O.matches(options::OPT_fcolor_diagnostics) &&
35150b57cec5SDimitry Andric         !O.matches(options::OPT_fdiagnostics_color) &&
35160b57cec5SDimitry Andric         !O.matches(options::OPT_fno_color_diagnostics) &&
35170b57cec5SDimitry Andric         !O.matches(options::OPT_fno_diagnostics_color) &&
35180b57cec5SDimitry Andric         !O.matches(options::OPT_fdiagnostics_color_EQ))
35190b57cec5SDimitry Andric       continue;
35200b57cec5SDimitry Andric 
35210b57cec5SDimitry Andric     if (O.matches(options::OPT_fdiagnostics_color_EQ)) {
35220b57cec5SDimitry Andric       StringRef Value(A->getValue());
35230b57cec5SDimitry Andric       if (Value != "always" && Value != "never" && Value != "auto")
35240b57cec5SDimitry Andric         D.Diag(diag::err_drv_clang_unsupported)
35250b57cec5SDimitry Andric             << ("-fdiagnostics-color=" + Value).str();
35260b57cec5SDimitry Andric     }
35270b57cec5SDimitry Andric     A->claim();
35280b57cec5SDimitry Andric   }
35290b57cec5SDimitry Andric 
35300b57cec5SDimitry Andric   if (D.getDiags().getDiagnosticOptions().ShowColors)
35310b57cec5SDimitry Andric     CmdArgs.push_back("-fcolor-diagnostics");
35320b57cec5SDimitry Andric 
35330b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fansi_escape_codes))
35340b57cec5SDimitry Andric     CmdArgs.push_back("-fansi-escape-codes");
35350b57cec5SDimitry Andric 
35360b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fshow_source_location,
35370b57cec5SDimitry Andric                     options::OPT_fno_show_source_location))
35380b57cec5SDimitry Andric     CmdArgs.push_back("-fno-show-source-location");
35390b57cec5SDimitry Andric 
35400b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fdiagnostics_absolute_paths))
35410b57cec5SDimitry Andric     CmdArgs.push_back("-fdiagnostics-absolute-paths");
35420b57cec5SDimitry Andric 
35430b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fshow_column, options::OPT_fno_show_column,
35440b57cec5SDimitry Andric                     ColumnDefault))
35450b57cec5SDimitry Andric     CmdArgs.push_back("-fno-show-column");
35460b57cec5SDimitry Andric 
35470b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fspell_checking,
35480b57cec5SDimitry Andric                     options::OPT_fno_spell_checking))
35490b57cec5SDimitry Andric     CmdArgs.push_back("-fno-spell-checking");
35500b57cec5SDimitry Andric }
35510b57cec5SDimitry Andric 
35520b57cec5SDimitry Andric enum class DwarfFissionKind { None, Split, Single };
35530b57cec5SDimitry Andric 
35540b57cec5SDimitry Andric static DwarfFissionKind getDebugFissionKind(const Driver &D,
35550b57cec5SDimitry Andric                                             const ArgList &Args, Arg *&Arg) {
35560b57cec5SDimitry Andric   Arg =
35570b57cec5SDimitry Andric       Args.getLastArg(options::OPT_gsplit_dwarf, options::OPT_gsplit_dwarf_EQ);
35580b57cec5SDimitry Andric   if (!Arg)
35590b57cec5SDimitry Andric     return DwarfFissionKind::None;
35600b57cec5SDimitry Andric 
35610b57cec5SDimitry Andric   if (Arg->getOption().matches(options::OPT_gsplit_dwarf))
35620b57cec5SDimitry Andric     return DwarfFissionKind::Split;
35630b57cec5SDimitry Andric 
35640b57cec5SDimitry Andric   StringRef Value = Arg->getValue();
35650b57cec5SDimitry Andric   if (Value == "split")
35660b57cec5SDimitry Andric     return DwarfFissionKind::Split;
35670b57cec5SDimitry Andric   if (Value == "single")
35680b57cec5SDimitry Andric     return DwarfFissionKind::Single;
35690b57cec5SDimitry Andric 
35700b57cec5SDimitry Andric   D.Diag(diag::err_drv_unsupported_option_argument)
35710b57cec5SDimitry Andric       << Arg->getOption().getName() << Arg->getValue();
35720b57cec5SDimitry Andric   return DwarfFissionKind::None;
35730b57cec5SDimitry Andric }
35740b57cec5SDimitry Andric 
35750b57cec5SDimitry Andric static void RenderDebugOptions(const ToolChain &TC, const Driver &D,
35760b57cec5SDimitry Andric                                const llvm::Triple &T, const ArgList &Args,
35770b57cec5SDimitry Andric                                bool EmitCodeView, bool IsWindowsMSVC,
35780b57cec5SDimitry Andric                                ArgStringList &CmdArgs,
35790b57cec5SDimitry Andric                                codegenoptions::DebugInfoKind &DebugInfoKind,
35800b57cec5SDimitry Andric                                DwarfFissionKind &DwarfFission) {
35810b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdebug_info_for_profiling,
35820b57cec5SDimitry Andric                    options::OPT_fno_debug_info_for_profiling, false) &&
35830b57cec5SDimitry Andric       checkDebugInfoOption(
35840b57cec5SDimitry Andric           Args.getLastArg(options::OPT_fdebug_info_for_profiling), Args, D, TC))
35850b57cec5SDimitry Andric     CmdArgs.push_back("-fdebug-info-for-profiling");
35860b57cec5SDimitry Andric 
35870b57cec5SDimitry Andric   // The 'g' groups options involve a somewhat intricate sequence of decisions
35880b57cec5SDimitry Andric   // about what to pass from the driver to the frontend, but by the time they
35890b57cec5SDimitry Andric   // reach cc1 they've been factored into three well-defined orthogonal choices:
35900b57cec5SDimitry Andric   //  * what level of debug info to generate
35910b57cec5SDimitry Andric   //  * what dwarf version to write
35920b57cec5SDimitry Andric   //  * what debugger tuning to use
35930b57cec5SDimitry Andric   // This avoids having to monkey around further in cc1 other than to disable
35940b57cec5SDimitry Andric   // codeview if not running in a Windows environment. Perhaps even that
35950b57cec5SDimitry Andric   // decision should be made in the driver as well though.
35960b57cec5SDimitry Andric   llvm::DebuggerKind DebuggerTuning = TC.getDefaultDebuggerTuning();
35970b57cec5SDimitry Andric 
35980b57cec5SDimitry Andric   bool SplitDWARFInlining =
35990b57cec5SDimitry Andric       Args.hasFlag(options::OPT_fsplit_dwarf_inlining,
3600*480093f4SDimitry Andric                    options::OPT_fno_split_dwarf_inlining, false);
36010b57cec5SDimitry Andric 
36020b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_g_Group);
36030b57cec5SDimitry Andric 
36040b57cec5SDimitry Andric   Arg* SplitDWARFArg;
36050b57cec5SDimitry Andric   DwarfFission = getDebugFissionKind(D, Args, SplitDWARFArg);
36060b57cec5SDimitry Andric 
36070b57cec5SDimitry Andric   if (DwarfFission != DwarfFissionKind::None &&
36080b57cec5SDimitry Andric       !checkDebugInfoOption(SplitDWARFArg, Args, D, TC)) {
36090b57cec5SDimitry Andric     DwarfFission = DwarfFissionKind::None;
36100b57cec5SDimitry Andric     SplitDWARFInlining = false;
36110b57cec5SDimitry Andric   }
36120b57cec5SDimitry Andric 
36130b57cec5SDimitry Andric   if (const Arg *A =
36140b57cec5SDimitry Andric           Args.getLastArg(options::OPT_g_Group, options::OPT_gsplit_dwarf,
36150b57cec5SDimitry Andric                           options::OPT_gsplit_dwarf_EQ)) {
36160b57cec5SDimitry Andric     DebugInfoKind = codegenoptions::LimitedDebugInfo;
36170b57cec5SDimitry Andric 
36180b57cec5SDimitry Andric     // If the last option explicitly specified a debug-info level, use it.
36190b57cec5SDimitry Andric     if (checkDebugInfoOption(A, Args, D, TC) &&
36200b57cec5SDimitry Andric         A->getOption().matches(options::OPT_gN_Group)) {
36210b57cec5SDimitry Andric       DebugInfoKind = DebugLevelToInfoKind(*A);
36220b57cec5SDimitry Andric       // For -g0 or -gline-tables-only, drop -gsplit-dwarf. This gets a bit more
36230b57cec5SDimitry Andric       // complicated if you've disabled inline info in the skeleton CUs
36240b57cec5SDimitry Andric       // (SplitDWARFInlining) - then there's value in composing split-dwarf and
36250b57cec5SDimitry Andric       // line-tables-only, so let those compose naturally in that case.
36260b57cec5SDimitry Andric       if (DebugInfoKind == codegenoptions::NoDebugInfo ||
36270b57cec5SDimitry Andric           DebugInfoKind == codegenoptions::DebugDirectivesOnly ||
36280b57cec5SDimitry Andric           (DebugInfoKind == codegenoptions::DebugLineTablesOnly &&
36290b57cec5SDimitry Andric            SplitDWARFInlining))
36300b57cec5SDimitry Andric         DwarfFission = DwarfFissionKind::None;
36310b57cec5SDimitry Andric     }
36320b57cec5SDimitry Andric   }
36330b57cec5SDimitry Andric 
36340b57cec5SDimitry Andric   // If a debugger tuning argument appeared, remember it.
36350b57cec5SDimitry Andric   if (const Arg *A =
36360b57cec5SDimitry Andric           Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) {
36370b57cec5SDimitry Andric     if (checkDebugInfoOption(A, Args, D, TC)) {
36380b57cec5SDimitry Andric       if (A->getOption().matches(options::OPT_glldb))
36390b57cec5SDimitry Andric         DebuggerTuning = llvm::DebuggerKind::LLDB;
36400b57cec5SDimitry Andric       else if (A->getOption().matches(options::OPT_gsce))
36410b57cec5SDimitry Andric         DebuggerTuning = llvm::DebuggerKind::SCE;
36420b57cec5SDimitry Andric       else
36430b57cec5SDimitry Andric         DebuggerTuning = llvm::DebuggerKind::GDB;
36440b57cec5SDimitry Andric     }
36450b57cec5SDimitry Andric   }
36460b57cec5SDimitry Andric 
36470b57cec5SDimitry Andric   // If a -gdwarf argument appeared, remember it.
3648*480093f4SDimitry Andric   const Arg *GDwarfN = Args.getLastArg(
3649*480093f4SDimitry Andric       options::OPT_gdwarf_2, options::OPT_gdwarf_3, options::OPT_gdwarf_4,
3650*480093f4SDimitry Andric       options::OPT_gdwarf_5, options::OPT_gdwarf);
3651*480093f4SDimitry Andric   bool EmitDwarf = false;
3652*480093f4SDimitry Andric   if (GDwarfN) {
3653*480093f4SDimitry Andric     if (checkDebugInfoOption(GDwarfN, Args, D, TC))
3654*480093f4SDimitry Andric       EmitDwarf = true;
3655*480093f4SDimitry Andric     else
3656*480093f4SDimitry Andric       GDwarfN = nullptr;
3657*480093f4SDimitry Andric   }
36580b57cec5SDimitry Andric 
36590b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) {
36600b57cec5SDimitry Andric     if (checkDebugInfoOption(A, Args, D, TC))
36610b57cec5SDimitry Andric       EmitCodeView = true;
36620b57cec5SDimitry Andric   }
36630b57cec5SDimitry Andric 
36640b57cec5SDimitry Andric   // If the user asked for debug info but did not explicitly specify -gcodeview
36650b57cec5SDimitry Andric   // or -gdwarf, ask the toolchain for the default format.
3666*480093f4SDimitry Andric   if (!EmitCodeView && !EmitDwarf &&
36670b57cec5SDimitry Andric       DebugInfoKind != codegenoptions::NoDebugInfo) {
36680b57cec5SDimitry Andric     switch (TC.getDefaultDebugFormat()) {
36690b57cec5SDimitry Andric     case codegenoptions::DIF_CodeView:
36700b57cec5SDimitry Andric       EmitCodeView = true;
36710b57cec5SDimitry Andric       break;
36720b57cec5SDimitry Andric     case codegenoptions::DIF_DWARF:
3673*480093f4SDimitry Andric       EmitDwarf = true;
36740b57cec5SDimitry Andric       break;
36750b57cec5SDimitry Andric     }
36760b57cec5SDimitry Andric   }
36770b57cec5SDimitry Andric 
3678*480093f4SDimitry Andric   unsigned DWARFVersion = 0;
3679*480093f4SDimitry Andric   unsigned DefaultDWARFVersion = ParseDebugDefaultVersion(TC, Args);
3680*480093f4SDimitry Andric   if (EmitDwarf) {
3681*480093f4SDimitry Andric     // Start with the platform default DWARF version
3682*480093f4SDimitry Andric     DWARFVersion = TC.GetDefaultDwarfVersion();
3683*480093f4SDimitry Andric     assert(DWARFVersion && "toolchain default DWARF version must be nonzero");
3684*480093f4SDimitry Andric 
3685*480093f4SDimitry Andric     // If the user specified a default DWARF version, that takes precedence
3686*480093f4SDimitry Andric     // over the platform default.
3687*480093f4SDimitry Andric     if (DefaultDWARFVersion)
3688*480093f4SDimitry Andric       DWARFVersion = DefaultDWARFVersion;
3689*480093f4SDimitry Andric 
3690*480093f4SDimitry Andric     // Override with a user-specified DWARF version
3691*480093f4SDimitry Andric     if (GDwarfN)
3692*480093f4SDimitry Andric       if (auto ExplicitVersion = DwarfVersionNum(GDwarfN->getSpelling()))
3693*480093f4SDimitry Andric         DWARFVersion = ExplicitVersion;
3694*480093f4SDimitry Andric   }
3695*480093f4SDimitry Andric 
36960b57cec5SDimitry Andric   // -gline-directives-only supported only for the DWARF debug info.
36970b57cec5SDimitry Andric   if (DWARFVersion == 0 && DebugInfoKind == codegenoptions::DebugDirectivesOnly)
36980b57cec5SDimitry Andric     DebugInfoKind = codegenoptions::NoDebugInfo;
36990b57cec5SDimitry Andric 
37000b57cec5SDimitry Andric   // We ignore flag -gstrict-dwarf for now.
37010b57cec5SDimitry Andric   // And we handle flag -grecord-gcc-switches later with DWARFDebugFlags.
37020b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_g_flags_Group);
37030b57cec5SDimitry Andric 
37040b57cec5SDimitry Andric   // Column info is included by default for everything except SCE and
37050b57cec5SDimitry Andric   // CodeView. Clang doesn't track end columns, just starting columns, which,
37060b57cec5SDimitry Andric   // in theory, is fine for CodeView (and PDB).  In practice, however, the
37070b57cec5SDimitry Andric   // Microsoft debuggers don't handle missing end columns well, so it's better
37080b57cec5SDimitry Andric   // not to include any column info.
37090b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_gcolumn_info))
37100b57cec5SDimitry Andric     (void)checkDebugInfoOption(A, Args, D, TC);
37110b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info,
37120b57cec5SDimitry Andric                    /*Default=*/!EmitCodeView &&
37130b57cec5SDimitry Andric                        DebuggerTuning != llvm::DebuggerKind::SCE))
37140b57cec5SDimitry Andric     CmdArgs.push_back("-dwarf-column-info");
37150b57cec5SDimitry Andric 
37160b57cec5SDimitry Andric   // FIXME: Move backend command line options to the module.
37170b57cec5SDimitry Andric   // If -gline-tables-only or -gline-directives-only is the last option it wins.
37180b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_gmodules))
37190b57cec5SDimitry Andric     if (checkDebugInfoOption(A, Args, D, TC)) {
37200b57cec5SDimitry Andric       if (DebugInfoKind != codegenoptions::DebugLineTablesOnly &&
37210b57cec5SDimitry Andric           DebugInfoKind != codegenoptions::DebugDirectivesOnly) {
37220b57cec5SDimitry Andric         DebugInfoKind = codegenoptions::LimitedDebugInfo;
37230b57cec5SDimitry Andric         CmdArgs.push_back("-dwarf-ext-refs");
37240b57cec5SDimitry Andric         CmdArgs.push_back("-fmodule-format=obj");
37250b57cec5SDimitry Andric       }
37260b57cec5SDimitry Andric     }
37270b57cec5SDimitry Andric 
37280b57cec5SDimitry Andric   if (T.isOSBinFormatELF() && !SplitDWARFInlining)
37290b57cec5SDimitry Andric     CmdArgs.push_back("-fno-split-dwarf-inlining");
37300b57cec5SDimitry Andric 
37310b57cec5SDimitry Andric   // After we've dealt with all combinations of things that could
37320b57cec5SDimitry Andric   // make DebugInfoKind be other than None or DebugLineTablesOnly,
37330b57cec5SDimitry Andric   // figure out if we need to "upgrade" it to standalone debug info.
37340b57cec5SDimitry Andric   // We parse these two '-f' options whether or not they will be used,
37350b57cec5SDimitry Andric   // to claim them even if you wrote "-fstandalone-debug -gline-tables-only"
37360b57cec5SDimitry Andric   bool NeedFullDebug = Args.hasFlag(
37370b57cec5SDimitry Andric       options::OPT_fstandalone_debug, options::OPT_fno_standalone_debug,
37380b57cec5SDimitry Andric       DebuggerTuning == llvm::DebuggerKind::LLDB ||
37390b57cec5SDimitry Andric           TC.GetDefaultStandaloneDebug());
37400b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fstandalone_debug))
37410b57cec5SDimitry Andric     (void)checkDebugInfoOption(A, Args, D, TC);
37420b57cec5SDimitry Andric   if (DebugInfoKind == codegenoptions::LimitedDebugInfo && NeedFullDebug)
37430b57cec5SDimitry Andric     DebugInfoKind = codegenoptions::FullDebugInfo;
37440b57cec5SDimitry Andric 
37450b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source,
37460b57cec5SDimitry Andric                    false)) {
37470b57cec5SDimitry Andric     // Source embedding is a vendor extension to DWARF v5. By now we have
37480b57cec5SDimitry Andric     // checked if a DWARF version was stated explicitly, and have otherwise
37490b57cec5SDimitry Andric     // fallen back to the target default, so if this is still not at least 5
37500b57cec5SDimitry Andric     // we emit an error.
37510b57cec5SDimitry Andric     const Arg *A = Args.getLastArg(options::OPT_gembed_source);
37520b57cec5SDimitry Andric     if (DWARFVersion < 5)
37530b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_only_allowed_with)
37540b57cec5SDimitry Andric           << A->getAsString(Args) << "-gdwarf-5";
37550b57cec5SDimitry Andric     else if (checkDebugInfoOption(A, Args, D, TC))
37560b57cec5SDimitry Andric       CmdArgs.push_back("-gembed-source");
37570b57cec5SDimitry Andric   }
37580b57cec5SDimitry Andric 
37590b57cec5SDimitry Andric   if (EmitCodeView) {
37600b57cec5SDimitry Andric     CmdArgs.push_back("-gcodeview");
37610b57cec5SDimitry Andric 
37620b57cec5SDimitry Andric     // Emit codeview type hashes if requested.
37630b57cec5SDimitry Andric     if (Args.hasFlag(options::OPT_gcodeview_ghash,
37640b57cec5SDimitry Andric                      options::OPT_gno_codeview_ghash, false)) {
37650b57cec5SDimitry Andric       CmdArgs.push_back("-gcodeview-ghash");
37660b57cec5SDimitry Andric     }
37670b57cec5SDimitry Andric   }
37680b57cec5SDimitry Andric 
3769*480093f4SDimitry Andric   // Omit inline line tables if requested.
3770*480093f4SDimitry Andric   if (Args.hasFlag(options::OPT_gno_inline_line_tables,
3771*480093f4SDimitry Andric                    options::OPT_ginline_line_tables, false)) {
3772*480093f4SDimitry Andric     CmdArgs.push_back("-gno-inline-line-tables");
3773*480093f4SDimitry Andric   }
3774*480093f4SDimitry Andric 
37750b57cec5SDimitry Andric   // Adjust the debug info kind for the given toolchain.
37760b57cec5SDimitry Andric   TC.adjustDebugInfoKind(DebugInfoKind, Args);
37770b57cec5SDimitry Andric 
3778*480093f4SDimitry Andric   // When emitting remarks, we need at least debug lines in the output.
3779*480093f4SDimitry Andric   if (willEmitRemarks(Args) &&
3780*480093f4SDimitry Andric       DebugInfoKind <= codegenoptions::DebugDirectivesOnly)
3781*480093f4SDimitry Andric     DebugInfoKind = codegenoptions::DebugLineTablesOnly;
3782*480093f4SDimitry Andric 
37830b57cec5SDimitry Andric   RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DWARFVersion,
37840b57cec5SDimitry Andric                           DebuggerTuning);
37850b57cec5SDimitry Andric 
37860b57cec5SDimitry Andric   // -fdebug-macro turns on macro debug info generation.
37870b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdebug_macro, options::OPT_fno_debug_macro,
37880b57cec5SDimitry Andric                    false))
37890b57cec5SDimitry Andric     if (checkDebugInfoOption(Args.getLastArg(options::OPT_fdebug_macro), Args,
37900b57cec5SDimitry Andric                              D, TC))
37910b57cec5SDimitry Andric       CmdArgs.push_back("-debug-info-macro");
37920b57cec5SDimitry Andric 
37930b57cec5SDimitry Andric   // -ggnu-pubnames turns on gnu style pubnames in the backend.
37940b57cec5SDimitry Andric   const auto *PubnamesArg =
37950b57cec5SDimitry Andric       Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames,
37960b57cec5SDimitry Andric                       options::OPT_gpubnames, options::OPT_gno_pubnames);
37970b57cec5SDimitry Andric   if (DwarfFission != DwarfFissionKind::None ||
37980b57cec5SDimitry Andric       (PubnamesArg && checkDebugInfoOption(PubnamesArg, Args, D, TC)))
37990b57cec5SDimitry Andric     if (!PubnamesArg ||
38000b57cec5SDimitry Andric         (!PubnamesArg->getOption().matches(options::OPT_gno_gnu_pubnames) &&
38010b57cec5SDimitry Andric          !PubnamesArg->getOption().matches(options::OPT_gno_pubnames)))
38020b57cec5SDimitry Andric       CmdArgs.push_back(PubnamesArg && PubnamesArg->getOption().matches(
38030b57cec5SDimitry Andric                                            options::OPT_gpubnames)
38040b57cec5SDimitry Andric                             ? "-gpubnames"
38050b57cec5SDimitry Andric                             : "-ggnu-pubnames");
38060b57cec5SDimitry Andric 
38070b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdebug_ranges_base_address,
38080b57cec5SDimitry Andric                    options::OPT_fno_debug_ranges_base_address, false)) {
38090b57cec5SDimitry Andric     CmdArgs.push_back("-fdebug-ranges-base-address");
38100b57cec5SDimitry Andric   }
38110b57cec5SDimitry Andric 
38120b57cec5SDimitry Andric   // -gdwarf-aranges turns on the emission of the aranges section in the
38130b57cec5SDimitry Andric   // backend.
38140b57cec5SDimitry Andric   // Always enabled for SCE tuning.
38150b57cec5SDimitry Andric   bool NeedAranges = DebuggerTuning == llvm::DebuggerKind::SCE;
38160b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_gdwarf_aranges))
38170b57cec5SDimitry Andric     NeedAranges = checkDebugInfoOption(A, Args, D, TC) || NeedAranges;
38180b57cec5SDimitry Andric   if (NeedAranges) {
38190b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
38200b57cec5SDimitry Andric     CmdArgs.push_back("-generate-arange-section");
38210b57cec5SDimitry Andric   }
38220b57cec5SDimitry Andric 
3823*480093f4SDimitry Andric   if (Args.hasFlag(options::OPT_fforce_dwarf_frame,
3824*480093f4SDimitry Andric                    options::OPT_fno_force_dwarf_frame, false))
3825*480093f4SDimitry Andric     CmdArgs.push_back("-fforce-dwarf-frame");
3826*480093f4SDimitry Andric 
38270b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdebug_types_section,
38280b57cec5SDimitry Andric                    options::OPT_fno_debug_types_section, false)) {
38290b57cec5SDimitry Andric     if (!T.isOSBinFormatELF()) {
38300b57cec5SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
38310b57cec5SDimitry Andric           << Args.getLastArg(options::OPT_fdebug_types_section)
38320b57cec5SDimitry Andric                  ->getAsString(Args)
38330b57cec5SDimitry Andric           << T.getTriple();
38340b57cec5SDimitry Andric     } else if (checkDebugInfoOption(
38350b57cec5SDimitry Andric                    Args.getLastArg(options::OPT_fdebug_types_section), Args, D,
38360b57cec5SDimitry Andric                    TC)) {
38370b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
38380b57cec5SDimitry Andric       CmdArgs.push_back("-generate-type-units");
38390b57cec5SDimitry Andric     }
38400b57cec5SDimitry Andric   }
38410b57cec5SDimitry Andric 
38420b57cec5SDimitry Andric   // Decide how to render forward declarations of template instantiations.
38430b57cec5SDimitry Andric   // SCE wants full descriptions, others just get them in the name.
38440b57cec5SDimitry Andric   if (DebuggerTuning == llvm::DebuggerKind::SCE)
38450b57cec5SDimitry Andric     CmdArgs.push_back("-debug-forward-template-params");
38460b57cec5SDimitry Andric 
38470b57cec5SDimitry Andric   // Do we need to explicitly import anonymous namespaces into the parent
38480b57cec5SDimitry Andric   // scope?
38490b57cec5SDimitry Andric   if (DebuggerTuning == llvm::DebuggerKind::SCE)
38500b57cec5SDimitry Andric     CmdArgs.push_back("-dwarf-explicit-import");
38510b57cec5SDimitry Andric 
38520b57cec5SDimitry Andric   RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC);
38530b57cec5SDimitry Andric }
38540b57cec5SDimitry Andric 
38550b57cec5SDimitry Andric void Clang::ConstructJob(Compilation &C, const JobAction &JA,
38560b57cec5SDimitry Andric                          const InputInfo &Output, const InputInfoList &Inputs,
38570b57cec5SDimitry Andric                          const ArgList &Args, const char *LinkingOutput) const {
38580b57cec5SDimitry Andric   const auto &TC = getToolChain();
38590b57cec5SDimitry Andric   const llvm::Triple &RawTriple = TC.getTriple();
38600b57cec5SDimitry Andric   const llvm::Triple &Triple = TC.getEffectiveTriple();
38610b57cec5SDimitry Andric   const std::string &TripleStr = Triple.getTriple();
38620b57cec5SDimitry Andric 
38630b57cec5SDimitry Andric   bool KernelOrKext =
38640b57cec5SDimitry Andric       Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
38650b57cec5SDimitry Andric   const Driver &D = TC.getDriver();
38660b57cec5SDimitry Andric   ArgStringList CmdArgs;
38670b57cec5SDimitry Andric 
38680b57cec5SDimitry Andric   // Check number of inputs for sanity. We need at least one input.
38690b57cec5SDimitry Andric   assert(Inputs.size() >= 1 && "Must have at least one input.");
38700b57cec5SDimitry Andric   // CUDA/HIP compilation may have multiple inputs (source file + results of
38710b57cec5SDimitry Andric   // device-side compilations). OpenMP device jobs also take the host IR as a
38720b57cec5SDimitry Andric   // second input. Module precompilation accepts a list of header files to
38730b57cec5SDimitry Andric   // include as part of the module. All other jobs are expected to have exactly
38740b57cec5SDimitry Andric   // one input.
38750b57cec5SDimitry Andric   bool IsCuda = JA.isOffloading(Action::OFK_Cuda);
38760b57cec5SDimitry Andric   bool IsHIP = JA.isOffloading(Action::OFK_HIP);
38770b57cec5SDimitry Andric   bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP);
38780b57cec5SDimitry Andric   bool IsHeaderModulePrecompile = isa<HeaderModulePrecompileJobAction>(JA);
38790b57cec5SDimitry Andric 
38800b57cec5SDimitry Andric   // A header module compilation doesn't have a main input file, so invent a
38810b57cec5SDimitry Andric   // fake one as a placeholder.
38820b57cec5SDimitry Andric   const char *ModuleName = [&]{
38830b57cec5SDimitry Andric     auto *ModuleNameArg = Args.getLastArg(options::OPT_fmodule_name_EQ);
38840b57cec5SDimitry Andric     return ModuleNameArg ? ModuleNameArg->getValue() : "";
38850b57cec5SDimitry Andric   }();
38860b57cec5SDimitry Andric   InputInfo HeaderModuleInput(Inputs[0].getType(), ModuleName, ModuleName);
38870b57cec5SDimitry Andric 
38880b57cec5SDimitry Andric   const InputInfo &Input =
38890b57cec5SDimitry Andric       IsHeaderModulePrecompile ? HeaderModuleInput : Inputs[0];
38900b57cec5SDimitry Andric 
38910b57cec5SDimitry Andric   InputInfoList ModuleHeaderInputs;
38920b57cec5SDimitry Andric   const InputInfo *CudaDeviceInput = nullptr;
38930b57cec5SDimitry Andric   const InputInfo *OpenMPDeviceInput = nullptr;
38940b57cec5SDimitry Andric   for (const InputInfo &I : Inputs) {
38950b57cec5SDimitry Andric     if (&I == &Input) {
38960b57cec5SDimitry Andric       // This is the primary input.
38970b57cec5SDimitry Andric     } else if (IsHeaderModulePrecompile &&
38980b57cec5SDimitry Andric                types::getPrecompiledType(I.getType()) == types::TY_PCH) {
38990b57cec5SDimitry Andric       types::ID Expected = HeaderModuleInput.getType();
39000b57cec5SDimitry Andric       if (I.getType() != Expected) {
39010b57cec5SDimitry Andric         D.Diag(diag::err_drv_module_header_wrong_kind)
39020b57cec5SDimitry Andric             << I.getFilename() << types::getTypeName(I.getType())
39030b57cec5SDimitry Andric             << types::getTypeName(Expected);
39040b57cec5SDimitry Andric       }
39050b57cec5SDimitry Andric       ModuleHeaderInputs.push_back(I);
39060b57cec5SDimitry Andric     } else if ((IsCuda || IsHIP) && !CudaDeviceInput) {
39070b57cec5SDimitry Andric       CudaDeviceInput = &I;
39080b57cec5SDimitry Andric     } else if (IsOpenMPDevice && !OpenMPDeviceInput) {
39090b57cec5SDimitry Andric       OpenMPDeviceInput = &I;
39100b57cec5SDimitry Andric     } else {
39110b57cec5SDimitry Andric       llvm_unreachable("unexpectedly given multiple inputs");
39120b57cec5SDimitry Andric     }
39130b57cec5SDimitry Andric   }
39140b57cec5SDimitry Andric 
39150b57cec5SDimitry Andric   const llvm::Triple *AuxTriple = IsCuda ? TC.getAuxTriple() : nullptr;
39160b57cec5SDimitry Andric   bool IsWindowsMSVC = RawTriple.isWindowsMSVCEnvironment();
39170b57cec5SDimitry Andric   bool IsIAMCU = RawTriple.isOSIAMCU();
39180b57cec5SDimitry Andric 
39190b57cec5SDimitry Andric   // Adjust IsWindowsXYZ for CUDA/HIP compilations.  Even when compiling in
39200b57cec5SDimitry Andric   // device mode (i.e., getToolchain().getTriple() is NVPTX/AMDGCN, not
39210b57cec5SDimitry Andric   // Windows), we need to pass Windows-specific flags to cc1.
39220b57cec5SDimitry Andric   if (IsCuda || IsHIP)
39230b57cec5SDimitry Andric     IsWindowsMSVC |= AuxTriple && AuxTriple->isWindowsMSVCEnvironment();
39240b57cec5SDimitry Andric 
39250b57cec5SDimitry Andric   // C++ is not supported for IAMCU.
39260b57cec5SDimitry Andric   if (IsIAMCU && types::isCXX(Input.getType()))
39270b57cec5SDimitry Andric     D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU";
39280b57cec5SDimitry Andric 
39290b57cec5SDimitry Andric   // Invoke ourselves in -cc1 mode.
39300b57cec5SDimitry Andric   //
39310b57cec5SDimitry Andric   // FIXME: Implement custom jobs for internal actions.
39320b57cec5SDimitry Andric   CmdArgs.push_back("-cc1");
39330b57cec5SDimitry Andric 
39340b57cec5SDimitry Andric   // Add the "effective" target triple.
39350b57cec5SDimitry Andric   CmdArgs.push_back("-triple");
39360b57cec5SDimitry Andric   CmdArgs.push_back(Args.MakeArgString(TripleStr));
39370b57cec5SDimitry Andric 
39380b57cec5SDimitry Andric   if (const Arg *MJ = Args.getLastArg(options::OPT_MJ)) {
39390b57cec5SDimitry Andric     DumpCompilationDatabase(C, MJ->getValue(), TripleStr, Output, Input, Args);
39400b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_MJ);
3941a7dea167SDimitry Andric   } else if (const Arg *GenCDBFragment =
3942a7dea167SDimitry Andric                  Args.getLastArg(options::OPT_gen_cdb_fragment_path)) {
3943a7dea167SDimitry Andric     DumpCompilationDatabaseFragmentToDir(GenCDBFragment->getValue(), C,
3944a7dea167SDimitry Andric                                          TripleStr, Output, Input, Args);
3945a7dea167SDimitry Andric     Args.ClaimAllArgs(options::OPT_gen_cdb_fragment_path);
39460b57cec5SDimitry Andric   }
39470b57cec5SDimitry Andric 
39480b57cec5SDimitry Andric   if (IsCuda || IsHIP) {
39490b57cec5SDimitry Andric     // We have to pass the triple of the host if compiling for a CUDA/HIP device
39500b57cec5SDimitry Andric     // and vice-versa.
39510b57cec5SDimitry Andric     std::string NormalizedTriple;
39520b57cec5SDimitry Andric     if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
39530b57cec5SDimitry Andric         JA.isDeviceOffloading(Action::OFK_HIP))
39540b57cec5SDimitry Andric       NormalizedTriple = C.getSingleOffloadToolChain<Action::OFK_Host>()
39550b57cec5SDimitry Andric                              ->getTriple()
39560b57cec5SDimitry Andric                              .normalize();
39570b57cec5SDimitry Andric     else {
39580b57cec5SDimitry Andric       // Host-side compilation.
39590b57cec5SDimitry Andric       NormalizedTriple =
39600b57cec5SDimitry Andric           (IsCuda ? C.getSingleOffloadToolChain<Action::OFK_Cuda>()
39610b57cec5SDimitry Andric                   : C.getSingleOffloadToolChain<Action::OFK_HIP>())
39620b57cec5SDimitry Andric               ->getTriple()
39630b57cec5SDimitry Andric               .normalize();
39640b57cec5SDimitry Andric       if (IsCuda) {
39650b57cec5SDimitry Andric         // We need to figure out which CUDA version we're compiling for, as that
39660b57cec5SDimitry Andric         // determines how we load and launch GPU kernels.
39670b57cec5SDimitry Andric         auto *CTC = static_cast<const toolchains::CudaToolChain *>(
39680b57cec5SDimitry Andric             C.getSingleOffloadToolChain<Action::OFK_Cuda>());
39690b57cec5SDimitry Andric         assert(CTC && "Expected valid CUDA Toolchain.");
39700b57cec5SDimitry Andric         if (CTC && CTC->CudaInstallation.version() != CudaVersion::UNKNOWN)
39710b57cec5SDimitry Andric           CmdArgs.push_back(Args.MakeArgString(
39720b57cec5SDimitry Andric               Twine("-target-sdk-version=") +
39730b57cec5SDimitry Andric               CudaVersionToString(CTC->CudaInstallation.version())));
39740b57cec5SDimitry Andric       }
39750b57cec5SDimitry Andric     }
39760b57cec5SDimitry Andric     CmdArgs.push_back("-aux-triple");
39770b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
39780b57cec5SDimitry Andric   }
39790b57cec5SDimitry Andric 
39800b57cec5SDimitry Andric   if (IsOpenMPDevice) {
39810b57cec5SDimitry Andric     // We have to pass the triple of the host if compiling for an OpenMP device.
39820b57cec5SDimitry Andric     std::string NormalizedTriple =
39830b57cec5SDimitry Andric         C.getSingleOffloadToolChain<Action::OFK_Host>()
39840b57cec5SDimitry Andric             ->getTriple()
39850b57cec5SDimitry Andric             .normalize();
39860b57cec5SDimitry Andric     CmdArgs.push_back("-aux-triple");
39870b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
39880b57cec5SDimitry Andric   }
39890b57cec5SDimitry Andric 
39900b57cec5SDimitry Andric   if (Triple.isOSWindows() && (Triple.getArch() == llvm::Triple::arm ||
39910b57cec5SDimitry Andric                                Triple.getArch() == llvm::Triple::thumb)) {
39920b57cec5SDimitry Andric     unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 4 : 6;
39930b57cec5SDimitry Andric     unsigned Version;
39940b57cec5SDimitry Andric     Triple.getArchName().substr(Offset).getAsInteger(10, Version);
39950b57cec5SDimitry Andric     if (Version < 7)
39960b57cec5SDimitry Andric       D.Diag(diag::err_target_unsupported_arch) << Triple.getArchName()
39970b57cec5SDimitry Andric                                                 << TripleStr;
39980b57cec5SDimitry Andric   }
39990b57cec5SDimitry Andric 
40000b57cec5SDimitry Andric   // Push all default warning arguments that are specific to
40010b57cec5SDimitry Andric   // the given target.  These come before user provided warning options
40020b57cec5SDimitry Andric   // are provided.
40030b57cec5SDimitry Andric   TC.addClangWarningOptions(CmdArgs);
40040b57cec5SDimitry Andric 
40050b57cec5SDimitry Andric   // Select the appropriate action.
40060b57cec5SDimitry Andric   RewriteKind rewriteKind = RK_None;
40070b57cec5SDimitry Andric 
4008a7dea167SDimitry Andric   // If CollectArgsForIntegratedAssembler() isn't called below, claim the args
4009a7dea167SDimitry Andric   // it claims when not running an assembler. Otherwise, clang would emit
4010a7dea167SDimitry Andric   // "argument unused" warnings for assembler flags when e.g. adding "-E" to
4011a7dea167SDimitry Andric   // flags while debugging something. That'd be somewhat inconvenient, and it's
4012a7dea167SDimitry Andric   // also inconsistent with most other flags -- we don't warn on
4013a7dea167SDimitry Andric   // -ffunction-sections not being used in -E mode either for example, even
4014a7dea167SDimitry Andric   // though it's not really used either.
4015a7dea167SDimitry Andric   if (!isa<AssembleJobAction>(JA)) {
4016a7dea167SDimitry Andric     // The args claimed here should match the args used in
4017a7dea167SDimitry Andric     // CollectArgsForIntegratedAssembler().
4018a7dea167SDimitry Andric     if (TC.useIntegratedAs()) {
4019a7dea167SDimitry Andric       Args.ClaimAllArgs(options::OPT_mrelax_all);
4020a7dea167SDimitry Andric       Args.ClaimAllArgs(options::OPT_mno_relax_all);
4021a7dea167SDimitry Andric       Args.ClaimAllArgs(options::OPT_mincremental_linker_compatible);
4022a7dea167SDimitry Andric       Args.ClaimAllArgs(options::OPT_mno_incremental_linker_compatible);
4023a7dea167SDimitry Andric       switch (C.getDefaultToolChain().getArch()) {
4024a7dea167SDimitry Andric       case llvm::Triple::arm:
4025a7dea167SDimitry Andric       case llvm::Triple::armeb:
4026a7dea167SDimitry Andric       case llvm::Triple::thumb:
4027a7dea167SDimitry Andric       case llvm::Triple::thumbeb:
4028a7dea167SDimitry Andric         Args.ClaimAllArgs(options::OPT_mimplicit_it_EQ);
4029a7dea167SDimitry Andric         break;
4030a7dea167SDimitry Andric       default:
4031a7dea167SDimitry Andric         break;
4032a7dea167SDimitry Andric       }
4033a7dea167SDimitry Andric     }
4034a7dea167SDimitry Andric     Args.ClaimAllArgs(options::OPT_Wa_COMMA);
4035a7dea167SDimitry Andric     Args.ClaimAllArgs(options::OPT_Xassembler);
4036a7dea167SDimitry Andric   }
4037a7dea167SDimitry Andric 
40380b57cec5SDimitry Andric   if (isa<AnalyzeJobAction>(JA)) {
40390b57cec5SDimitry Andric     assert(JA.getType() == types::TY_Plist && "Invalid output type.");
40400b57cec5SDimitry Andric     CmdArgs.push_back("-analyze");
40410b57cec5SDimitry Andric   } else if (isa<MigrateJobAction>(JA)) {
40420b57cec5SDimitry Andric     CmdArgs.push_back("-migrate");
40430b57cec5SDimitry Andric   } else if (isa<PreprocessJobAction>(JA)) {
40440b57cec5SDimitry Andric     if (Output.getType() == types::TY_Dependencies)
40450b57cec5SDimitry Andric       CmdArgs.push_back("-Eonly");
40460b57cec5SDimitry Andric     else {
40470b57cec5SDimitry Andric       CmdArgs.push_back("-E");
40480b57cec5SDimitry Andric       if (Args.hasArg(options::OPT_rewrite_objc) &&
40490b57cec5SDimitry Andric           !Args.hasArg(options::OPT_g_Group))
40500b57cec5SDimitry Andric         CmdArgs.push_back("-P");
40510b57cec5SDimitry Andric     }
40520b57cec5SDimitry Andric   } else if (isa<AssembleJobAction>(JA)) {
40530b57cec5SDimitry Andric     CmdArgs.push_back("-emit-obj");
40540b57cec5SDimitry Andric 
40550b57cec5SDimitry Andric     CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
40560b57cec5SDimitry Andric 
40570b57cec5SDimitry Andric     // Also ignore explicit -force_cpusubtype_ALL option.
40580b57cec5SDimitry Andric     (void)Args.hasArg(options::OPT_force__cpusubtype__ALL);
40590b57cec5SDimitry Andric   } else if (isa<PrecompileJobAction>(JA)) {
40600b57cec5SDimitry Andric     if (JA.getType() == types::TY_Nothing)
40610b57cec5SDimitry Andric       CmdArgs.push_back("-fsyntax-only");
40620b57cec5SDimitry Andric     else if (JA.getType() == types::TY_ModuleFile)
40630b57cec5SDimitry Andric       CmdArgs.push_back(IsHeaderModulePrecompile
40640b57cec5SDimitry Andric                             ? "-emit-header-module"
40650b57cec5SDimitry Andric                             : "-emit-module-interface");
40660b57cec5SDimitry Andric     else
40670b57cec5SDimitry Andric       CmdArgs.push_back("-emit-pch");
40680b57cec5SDimitry Andric   } else if (isa<VerifyPCHJobAction>(JA)) {
40690b57cec5SDimitry Andric     CmdArgs.push_back("-verify-pch");
40700b57cec5SDimitry Andric   } else {
40710b57cec5SDimitry Andric     assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) &&
40720b57cec5SDimitry Andric            "Invalid action for clang tool.");
40730b57cec5SDimitry Andric     if (JA.getType() == types::TY_Nothing) {
40740b57cec5SDimitry Andric       CmdArgs.push_back("-fsyntax-only");
40750b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_LLVM_IR ||
40760b57cec5SDimitry Andric                JA.getType() == types::TY_LTO_IR) {
40770b57cec5SDimitry Andric       CmdArgs.push_back("-emit-llvm");
40780b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_LLVM_BC ||
40790b57cec5SDimitry Andric                JA.getType() == types::TY_LTO_BC) {
40800b57cec5SDimitry Andric       CmdArgs.push_back("-emit-llvm-bc");
4081a7dea167SDimitry Andric     } else if (JA.getType() == types::TY_IFS ||
4082a7dea167SDimitry Andric                JA.getType() == types::TY_IFS_CPP) {
4083a7dea167SDimitry Andric       StringRef ArgStr =
4084a7dea167SDimitry Andric           Args.hasArg(options::OPT_interface_stub_version_EQ)
4085a7dea167SDimitry Andric               ? Args.getLastArgValue(options::OPT_interface_stub_version_EQ)
4086a7dea167SDimitry Andric               : "experimental-ifs-v1";
40870b57cec5SDimitry Andric       CmdArgs.push_back("-emit-interface-stubs");
40880b57cec5SDimitry Andric       CmdArgs.push_back(
4089a7dea167SDimitry Andric           Args.MakeArgString(Twine("-interface-stub-version=") + ArgStr.str()));
40900b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_PP_Asm) {
40910b57cec5SDimitry Andric       CmdArgs.push_back("-S");
40920b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_AST) {
40930b57cec5SDimitry Andric       CmdArgs.push_back("-emit-pch");
40940b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_ModuleFile) {
40950b57cec5SDimitry Andric       CmdArgs.push_back("-module-file-info");
40960b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_RewrittenObjC) {
40970b57cec5SDimitry Andric       CmdArgs.push_back("-rewrite-objc");
40980b57cec5SDimitry Andric       rewriteKind = RK_NonFragile;
40990b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
41000b57cec5SDimitry Andric       CmdArgs.push_back("-rewrite-objc");
41010b57cec5SDimitry Andric       rewriteKind = RK_Fragile;
41020b57cec5SDimitry Andric     } else {
41030b57cec5SDimitry Andric       assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!");
41040b57cec5SDimitry Andric     }
41050b57cec5SDimitry Andric 
41060b57cec5SDimitry Andric     // Preserve use-list order by default when emitting bitcode, so that
41070b57cec5SDimitry Andric     // loading the bitcode up in 'opt' or 'llc' and running passes gives the
41080b57cec5SDimitry Andric     // same result as running passes here.  For LTO, we don't need to preserve
41090b57cec5SDimitry Andric     // the use-list order, since serialization to bitcode is part of the flow.
41100b57cec5SDimitry Andric     if (JA.getType() == types::TY_LLVM_BC)
41110b57cec5SDimitry Andric       CmdArgs.push_back("-emit-llvm-uselists");
41120b57cec5SDimitry Andric 
41130b57cec5SDimitry Andric     // Device-side jobs do not support LTO.
41140b57cec5SDimitry Andric     bool isDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) ||
41150b57cec5SDimitry Andric                                    JA.isDeviceOffloading(Action::OFK_Host));
41160b57cec5SDimitry Andric 
41170b57cec5SDimitry Andric     if (D.isUsingLTO() && !isDeviceOffloadAction) {
41180b57cec5SDimitry Andric       Args.AddLastArg(CmdArgs, options::OPT_flto, options::OPT_flto_EQ);
41190b57cec5SDimitry Andric       CmdArgs.push_back("-flto-unit");
41200b57cec5SDimitry Andric     }
41210b57cec5SDimitry Andric   }
41220b57cec5SDimitry Andric 
41230b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fthinlto_index_EQ)) {
41240b57cec5SDimitry Andric     if (!types::isLLVMIR(Input.getType()))
41250b57cec5SDimitry Andric       D.Diag(diag::err_drv_arg_requires_bitcode_input) << A->getAsString(Args);
41260b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ);
41270b57cec5SDimitry Andric   }
41280b57cec5SDimitry Andric 
4129*480093f4SDimitry Andric   if (Args.getLastArg(options::OPT_fthin_link_bitcode_EQ))
4130*480093f4SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fthin_link_bitcode_EQ);
4131*480093f4SDimitry Andric 
41320b57cec5SDimitry Andric   if (Args.getLastArg(options::OPT_save_temps_EQ))
41330b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_save_temps_EQ);
41340b57cec5SDimitry Andric 
41350b57cec5SDimitry Andric   // Embed-bitcode option.
41360b57cec5SDimitry Andric   // Only white-listed flags below are allowed to be embedded.
41370b57cec5SDimitry Andric   if (C.getDriver().embedBitcodeInObject() && !C.getDriver().isUsingLTO() &&
41380b57cec5SDimitry Andric       (isa<BackendJobAction>(JA) || isa<AssembleJobAction>(JA))) {
41390b57cec5SDimitry Andric     // Add flags implied by -fembed-bitcode.
41400b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ);
41410b57cec5SDimitry Andric     // Disable all llvm IR level optimizations.
41420b57cec5SDimitry Andric     CmdArgs.push_back("-disable-llvm-passes");
41430b57cec5SDimitry Andric 
4144*480093f4SDimitry Andric     // Render target options.
41450b57cec5SDimitry Andric     TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind());
41460b57cec5SDimitry Andric 
41470b57cec5SDimitry Andric     // reject options that shouldn't be supported in bitcode
41480b57cec5SDimitry Andric     // also reject kernel/kext
41490b57cec5SDimitry Andric     static const constexpr unsigned kBitcodeOptionBlacklist[] = {
41500b57cec5SDimitry Andric         options::OPT_mkernel,
41510b57cec5SDimitry Andric         options::OPT_fapple_kext,
41520b57cec5SDimitry Andric         options::OPT_ffunction_sections,
41530b57cec5SDimitry Andric         options::OPT_fno_function_sections,
41540b57cec5SDimitry Andric         options::OPT_fdata_sections,
41550b57cec5SDimitry Andric         options::OPT_fno_data_sections,
41560b57cec5SDimitry Andric         options::OPT_funique_section_names,
41570b57cec5SDimitry Andric         options::OPT_fno_unique_section_names,
41580b57cec5SDimitry Andric         options::OPT_mrestrict_it,
41590b57cec5SDimitry Andric         options::OPT_mno_restrict_it,
41600b57cec5SDimitry Andric         options::OPT_mstackrealign,
41610b57cec5SDimitry Andric         options::OPT_mno_stackrealign,
41620b57cec5SDimitry Andric         options::OPT_mstack_alignment,
41630b57cec5SDimitry Andric         options::OPT_mcmodel_EQ,
41640b57cec5SDimitry Andric         options::OPT_mlong_calls,
41650b57cec5SDimitry Andric         options::OPT_mno_long_calls,
41660b57cec5SDimitry Andric         options::OPT_ggnu_pubnames,
41670b57cec5SDimitry Andric         options::OPT_gdwarf_aranges,
41680b57cec5SDimitry Andric         options::OPT_fdebug_types_section,
41690b57cec5SDimitry Andric         options::OPT_fno_debug_types_section,
41700b57cec5SDimitry Andric         options::OPT_fdwarf_directory_asm,
41710b57cec5SDimitry Andric         options::OPT_fno_dwarf_directory_asm,
41720b57cec5SDimitry Andric         options::OPT_mrelax_all,
41730b57cec5SDimitry Andric         options::OPT_mno_relax_all,
41740b57cec5SDimitry Andric         options::OPT_ftrap_function_EQ,
41750b57cec5SDimitry Andric         options::OPT_ffixed_r9,
41760b57cec5SDimitry Andric         options::OPT_mfix_cortex_a53_835769,
41770b57cec5SDimitry Andric         options::OPT_mno_fix_cortex_a53_835769,
41780b57cec5SDimitry Andric         options::OPT_ffixed_x18,
41790b57cec5SDimitry Andric         options::OPT_mglobal_merge,
41800b57cec5SDimitry Andric         options::OPT_mno_global_merge,
41810b57cec5SDimitry Andric         options::OPT_mred_zone,
41820b57cec5SDimitry Andric         options::OPT_mno_red_zone,
41830b57cec5SDimitry Andric         options::OPT_Wa_COMMA,
41840b57cec5SDimitry Andric         options::OPT_Xassembler,
41850b57cec5SDimitry Andric         options::OPT_mllvm,
41860b57cec5SDimitry Andric     };
41870b57cec5SDimitry Andric     for (const auto &A : Args)
41880b57cec5SDimitry Andric       if (llvm::find(kBitcodeOptionBlacklist, A->getOption().getID()) !=
41890b57cec5SDimitry Andric           std::end(kBitcodeOptionBlacklist))
41900b57cec5SDimitry Andric         D.Diag(diag::err_drv_unsupported_embed_bitcode) << A->getSpelling();
41910b57cec5SDimitry Andric 
41920b57cec5SDimitry Andric     // Render the CodeGen options that need to be passed.
41930b57cec5SDimitry Andric     if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
41940b57cec5SDimitry Andric                       options::OPT_fno_optimize_sibling_calls))
41950b57cec5SDimitry Andric       CmdArgs.push_back("-mdisable-tail-calls");
41960b57cec5SDimitry Andric 
41970b57cec5SDimitry Andric     RenderFloatingPointOptions(TC, D, isOptimizationLevelFast(Args), Args,
41980b57cec5SDimitry Andric                                CmdArgs);
41990b57cec5SDimitry Andric 
42000b57cec5SDimitry Andric     // Render ABI arguments
42010b57cec5SDimitry Andric     switch (TC.getArch()) {
42020b57cec5SDimitry Andric     default: break;
42030b57cec5SDimitry Andric     case llvm::Triple::arm:
42040b57cec5SDimitry Andric     case llvm::Triple::armeb:
42050b57cec5SDimitry Andric     case llvm::Triple::thumbeb:
42060b57cec5SDimitry Andric       RenderARMABI(Triple, Args, CmdArgs);
42070b57cec5SDimitry Andric       break;
42080b57cec5SDimitry Andric     case llvm::Triple::aarch64:
4209*480093f4SDimitry Andric     case llvm::Triple::aarch64_32:
42100b57cec5SDimitry Andric     case llvm::Triple::aarch64_be:
42110b57cec5SDimitry Andric       RenderAArch64ABI(Triple, Args, CmdArgs);
42120b57cec5SDimitry Andric       break;
42130b57cec5SDimitry Andric     }
42140b57cec5SDimitry Andric 
42150b57cec5SDimitry Andric     // Optimization level for CodeGen.
42160b57cec5SDimitry Andric     if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) {
42170b57cec5SDimitry Andric       if (A->getOption().matches(options::OPT_O4)) {
42180b57cec5SDimitry Andric         CmdArgs.push_back("-O3");
42190b57cec5SDimitry Andric         D.Diag(diag::warn_O4_is_O3);
42200b57cec5SDimitry Andric       } else {
42210b57cec5SDimitry Andric         A->render(Args, CmdArgs);
42220b57cec5SDimitry Andric       }
42230b57cec5SDimitry Andric     }
42240b57cec5SDimitry Andric 
42250b57cec5SDimitry Andric     // Input/Output file.
42260b57cec5SDimitry Andric     if (Output.getType() == types::TY_Dependencies) {
42270b57cec5SDimitry Andric       // Handled with other dependency code.
42280b57cec5SDimitry Andric     } else if (Output.isFilename()) {
42290b57cec5SDimitry Andric       CmdArgs.push_back("-o");
42300b57cec5SDimitry Andric       CmdArgs.push_back(Output.getFilename());
42310b57cec5SDimitry Andric     } else {
42320b57cec5SDimitry Andric       assert(Output.isNothing() && "Input output.");
42330b57cec5SDimitry Andric     }
42340b57cec5SDimitry Andric 
42350b57cec5SDimitry Andric     for (const auto &II : Inputs) {
42360b57cec5SDimitry Andric       addDashXForInput(Args, II, CmdArgs);
42370b57cec5SDimitry Andric       if (II.isFilename())
42380b57cec5SDimitry Andric         CmdArgs.push_back(II.getFilename());
42390b57cec5SDimitry Andric       else
42400b57cec5SDimitry Andric         II.getInputArg().renderAsInput(Args, CmdArgs);
42410b57cec5SDimitry Andric     }
42420b57cec5SDimitry Andric 
4243a7dea167SDimitry Andric     C.addCommand(std::make_unique<Command>(JA, *this, D.getClangProgramPath(),
42440b57cec5SDimitry Andric                                             CmdArgs, Inputs));
42450b57cec5SDimitry Andric     return;
42460b57cec5SDimitry Andric   }
42470b57cec5SDimitry Andric 
42480b57cec5SDimitry Andric   if (C.getDriver().embedBitcodeMarkerOnly() && !C.getDriver().isUsingLTO())
42490b57cec5SDimitry Andric     CmdArgs.push_back("-fembed-bitcode=marker");
42500b57cec5SDimitry Andric 
42510b57cec5SDimitry Andric   // We normally speed up the clang process a bit by skipping destructors at
42520b57cec5SDimitry Andric   // exit, but when we're generating diagnostics we can rely on some of the
42530b57cec5SDimitry Andric   // cleanup.
42540b57cec5SDimitry Andric   if (!C.isForDiagnostics())
42550b57cec5SDimitry Andric     CmdArgs.push_back("-disable-free");
42560b57cec5SDimitry Andric 
42570b57cec5SDimitry Andric #ifdef NDEBUG
42580b57cec5SDimitry Andric   const bool IsAssertBuild = false;
42590b57cec5SDimitry Andric #else
42600b57cec5SDimitry Andric   const bool IsAssertBuild = true;
42610b57cec5SDimitry Andric #endif
42620b57cec5SDimitry Andric 
42630b57cec5SDimitry Andric   // Disable the verification pass in -asserts builds.
42640b57cec5SDimitry Andric   if (!IsAssertBuild)
42650b57cec5SDimitry Andric     CmdArgs.push_back("-disable-llvm-verifier");
42660b57cec5SDimitry Andric 
42670b57cec5SDimitry Andric   // Discard value names in assert builds unless otherwise specified.
42680b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdiscard_value_names,
42690b57cec5SDimitry Andric                    options::OPT_fno_discard_value_names, !IsAssertBuild))
42700b57cec5SDimitry Andric     CmdArgs.push_back("-discard-value-names");
42710b57cec5SDimitry Andric 
42720b57cec5SDimitry Andric   // Set the main file name, so that debug info works even with
42730b57cec5SDimitry Andric   // -save-temps.
42740b57cec5SDimitry Andric   CmdArgs.push_back("-main-file-name");
42750b57cec5SDimitry Andric   CmdArgs.push_back(getBaseInputName(Args, Input));
42760b57cec5SDimitry Andric 
42770b57cec5SDimitry Andric   // Some flags which affect the language (via preprocessor
42780b57cec5SDimitry Andric   // defines).
42790b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_static))
42800b57cec5SDimitry Andric     CmdArgs.push_back("-static-define");
42810b57cec5SDimitry Andric 
42820b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_municode))
42830b57cec5SDimitry Andric     CmdArgs.push_back("-DUNICODE");
42840b57cec5SDimitry Andric 
42850b57cec5SDimitry Andric   if (isa<AnalyzeJobAction>(JA))
42860b57cec5SDimitry Andric     RenderAnalyzerOptions(Args, CmdArgs, Triple, Input);
42870b57cec5SDimitry Andric 
4288a7dea167SDimitry Andric   if (isa<AnalyzeJobAction>(JA) ||
4289a7dea167SDimitry Andric       (isa<PreprocessJobAction>(JA) && Args.hasArg(options::OPT__analyze)))
4290a7dea167SDimitry Andric     CmdArgs.push_back("-setup-static-analyzer");
4291a7dea167SDimitry Andric 
42920b57cec5SDimitry Andric   // Enable compatilibily mode to avoid analyzer-config related errors.
42930b57cec5SDimitry Andric   // Since we can't access frontend flags through hasArg, let's manually iterate
42940b57cec5SDimitry Andric   // through them.
42950b57cec5SDimitry Andric   bool FoundAnalyzerConfig = false;
42960b57cec5SDimitry Andric   for (auto Arg : Args.filtered(options::OPT_Xclang))
42970b57cec5SDimitry Andric     if (StringRef(Arg->getValue()) == "-analyzer-config") {
42980b57cec5SDimitry Andric       FoundAnalyzerConfig = true;
42990b57cec5SDimitry Andric       break;
43000b57cec5SDimitry Andric     }
43010b57cec5SDimitry Andric   if (!FoundAnalyzerConfig)
43020b57cec5SDimitry Andric     for (auto Arg : Args.filtered(options::OPT_Xanalyzer))
43030b57cec5SDimitry Andric       if (StringRef(Arg->getValue()) == "-analyzer-config") {
43040b57cec5SDimitry Andric         FoundAnalyzerConfig = true;
43050b57cec5SDimitry Andric         break;
43060b57cec5SDimitry Andric       }
43070b57cec5SDimitry Andric   if (FoundAnalyzerConfig)
43080b57cec5SDimitry Andric     CmdArgs.push_back("-analyzer-config-compatibility-mode=true");
43090b57cec5SDimitry Andric 
43100b57cec5SDimitry Andric   CheckCodeGenerationOptions(D, Args);
43110b57cec5SDimitry Andric 
43120b57cec5SDimitry Andric   unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args);
43130b57cec5SDimitry Andric   assert(FunctionAlignment <= 31 && "function alignment will be truncated!");
43140b57cec5SDimitry Andric   if (FunctionAlignment) {
43150b57cec5SDimitry Andric     CmdArgs.push_back("-function-alignment");
43160b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment)));
43170b57cec5SDimitry Andric   }
43180b57cec5SDimitry Andric 
43190b57cec5SDimitry Andric   llvm::Reloc::Model RelocationModel;
43200b57cec5SDimitry Andric   unsigned PICLevel;
43210b57cec5SDimitry Andric   bool IsPIE;
43220b57cec5SDimitry Andric   std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(TC, Args);
43230b57cec5SDimitry Andric 
43240b57cec5SDimitry Andric   const char *RMName = RelocationModelName(RelocationModel);
43250b57cec5SDimitry Andric 
43260b57cec5SDimitry Andric   if ((RelocationModel == llvm::Reloc::ROPI ||
43270b57cec5SDimitry Andric        RelocationModel == llvm::Reloc::ROPI_RWPI) &&
43280b57cec5SDimitry Andric       types::isCXX(Input.getType()) &&
43290b57cec5SDimitry Andric       !Args.hasArg(options::OPT_fallow_unsupported))
43300b57cec5SDimitry Andric     D.Diag(diag::err_drv_ropi_incompatible_with_cxx);
43310b57cec5SDimitry Andric 
43320b57cec5SDimitry Andric   if (RMName) {
43330b57cec5SDimitry Andric     CmdArgs.push_back("-mrelocation-model");
43340b57cec5SDimitry Andric     CmdArgs.push_back(RMName);
43350b57cec5SDimitry Andric   }
43360b57cec5SDimitry Andric   if (PICLevel > 0) {
43370b57cec5SDimitry Andric     CmdArgs.push_back("-pic-level");
43380b57cec5SDimitry Andric     CmdArgs.push_back(PICLevel == 1 ? "1" : "2");
43390b57cec5SDimitry Andric     if (IsPIE)
43400b57cec5SDimitry Andric       CmdArgs.push_back("-pic-is-pie");
43410b57cec5SDimitry Andric   }
43420b57cec5SDimitry Andric 
43430b57cec5SDimitry Andric   if (RelocationModel == llvm::Reloc::ROPI ||
43440b57cec5SDimitry Andric       RelocationModel == llvm::Reloc::ROPI_RWPI)
43450b57cec5SDimitry Andric     CmdArgs.push_back("-fropi");
43460b57cec5SDimitry Andric   if (RelocationModel == llvm::Reloc::RWPI ||
43470b57cec5SDimitry Andric       RelocationModel == llvm::Reloc::ROPI_RWPI)
43480b57cec5SDimitry Andric     CmdArgs.push_back("-frwpi");
43490b57cec5SDimitry Andric 
43500b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_meabi)) {
43510b57cec5SDimitry Andric     CmdArgs.push_back("-meabi");
43520b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
43530b57cec5SDimitry Andric   }
43540b57cec5SDimitry Andric 
43550b57cec5SDimitry Andric   CmdArgs.push_back("-mthread-model");
43560b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) {
43570b57cec5SDimitry Andric     if (!TC.isThreadModelSupported(A->getValue()))
43580b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_thread_model_for_target)
43590b57cec5SDimitry Andric           << A->getValue() << A->getAsString(Args);
43600b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
43610b57cec5SDimitry Andric   }
43620b57cec5SDimitry Andric   else
43630b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(TC.getThreadModel()));
43640b57cec5SDimitry Andric 
43650b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fveclib);
43660b57cec5SDimitry Andric 
43670b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fmerge_all_constants,
43680b57cec5SDimitry Andric                    options::OPT_fno_merge_all_constants, false))
43690b57cec5SDimitry Andric     CmdArgs.push_back("-fmerge-all-constants");
43700b57cec5SDimitry Andric 
43710b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fno_delete_null_pointer_checks,
43720b57cec5SDimitry Andric                    options::OPT_fdelete_null_pointer_checks, false))
43730b57cec5SDimitry Andric     CmdArgs.push_back("-fno-delete-null-pointer-checks");
43740b57cec5SDimitry Andric 
43750b57cec5SDimitry Andric   // LLVM Code Generator Options.
43760b57cec5SDimitry Andric 
43770b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_frewrite_map_file) ||
43780b57cec5SDimitry Andric       Args.hasArg(options::OPT_frewrite_map_file_EQ)) {
43790b57cec5SDimitry Andric     for (const Arg *A : Args.filtered(options::OPT_frewrite_map_file,
43800b57cec5SDimitry Andric                                       options::OPT_frewrite_map_file_EQ)) {
43810b57cec5SDimitry Andric       StringRef Map = A->getValue();
43820b57cec5SDimitry Andric       if (!llvm::sys::fs::exists(Map)) {
43830b57cec5SDimitry Andric         D.Diag(diag::err_drv_no_such_file) << Map;
43840b57cec5SDimitry Andric       } else {
43850b57cec5SDimitry Andric         CmdArgs.push_back("-frewrite-map-file");
43860b57cec5SDimitry Andric         CmdArgs.push_back(A->getValue());
43870b57cec5SDimitry Andric         A->claim();
43880b57cec5SDimitry Andric       }
43890b57cec5SDimitry Andric     }
43900b57cec5SDimitry Andric   }
43910b57cec5SDimitry Andric 
43920b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) {
43930b57cec5SDimitry Andric     StringRef v = A->getValue();
43940b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
43950b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-warn-stack-size=" + v));
43960b57cec5SDimitry Andric     A->claim();
43970b57cec5SDimitry Andric   }
43980b57cec5SDimitry Andric 
43990b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fjump_tables, options::OPT_fno_jump_tables,
44000b57cec5SDimitry Andric                     true))
44010b57cec5SDimitry Andric     CmdArgs.push_back("-fno-jump-tables");
44020b57cec5SDimitry Andric 
44030b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fprofile_sample_accurate,
44040b57cec5SDimitry Andric                    options::OPT_fno_profile_sample_accurate, false))
44050b57cec5SDimitry Andric     CmdArgs.push_back("-fprofile-sample-accurate");
44060b57cec5SDimitry Andric 
44070b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fpreserve_as_comments,
44080b57cec5SDimitry Andric                     options::OPT_fno_preserve_as_comments, true))
44090b57cec5SDimitry Andric     CmdArgs.push_back("-fno-preserve-as-comments");
44100b57cec5SDimitry Andric 
44110b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
44120b57cec5SDimitry Andric     CmdArgs.push_back("-mregparm");
44130b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
44140b57cec5SDimitry Andric   }
44150b57cec5SDimitry Andric 
44160b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return,
44170b57cec5SDimitry Andric                                options::OPT_freg_struct_return)) {
44180b57cec5SDimitry Andric     if (TC.getArch() != llvm::Triple::x86) {
44190b57cec5SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
44200b57cec5SDimitry Andric           << A->getSpelling() << RawTriple.str();
44210b57cec5SDimitry Andric     } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) {
44220b57cec5SDimitry Andric       CmdArgs.push_back("-fpcc-struct-return");
44230b57cec5SDimitry Andric     } else {
44240b57cec5SDimitry Andric       assert(A->getOption().matches(options::OPT_freg_struct_return));
44250b57cec5SDimitry Andric       CmdArgs.push_back("-freg-struct-return");
44260b57cec5SDimitry Andric     }
44270b57cec5SDimitry Andric   }
44280b57cec5SDimitry Andric 
44290b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
44300b57cec5SDimitry Andric     CmdArgs.push_back("-fdefault-calling-conv=stdcall");
44310b57cec5SDimitry Andric 
4432a7dea167SDimitry Andric   CodeGenOptions::FramePointerKind FPKeepKind =
4433a7dea167SDimitry Andric                   getFramePointerKind(Args, RawTriple);
4434a7dea167SDimitry Andric   const char *FPKeepKindStr = nullptr;
4435a7dea167SDimitry Andric   switch (FPKeepKind) {
4436a7dea167SDimitry Andric   case CodeGenOptions::FramePointerKind::None:
4437a7dea167SDimitry Andric     FPKeepKindStr = "-mframe-pointer=none";
4438a7dea167SDimitry Andric     break;
4439a7dea167SDimitry Andric   case CodeGenOptions::FramePointerKind::NonLeaf:
4440a7dea167SDimitry Andric     FPKeepKindStr = "-mframe-pointer=non-leaf";
4441a7dea167SDimitry Andric     break;
4442a7dea167SDimitry Andric   case CodeGenOptions::FramePointerKind::All:
4443a7dea167SDimitry Andric     FPKeepKindStr = "-mframe-pointer=all";
4444a7dea167SDimitry Andric     break;
4445a7dea167SDimitry Andric   }
4446a7dea167SDimitry Andric   assert(FPKeepKindStr && "unknown FramePointerKind");
4447a7dea167SDimitry Andric   CmdArgs.push_back(FPKeepKindStr);
4448a7dea167SDimitry Andric 
44490b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
44500b57cec5SDimitry Andric                     options::OPT_fno_zero_initialized_in_bss))
44510b57cec5SDimitry Andric     CmdArgs.push_back("-mno-zero-initialized-in-bss");
44520b57cec5SDimitry Andric 
44530b57cec5SDimitry Andric   bool OFastEnabled = isOptimizationLevelFast(Args);
44540b57cec5SDimitry Andric   // If -Ofast is the optimization level, then -fstrict-aliasing should be
44550b57cec5SDimitry Andric   // enabled.  This alias option is being used to simplify the hasFlag logic.
44560b57cec5SDimitry Andric   OptSpecifier StrictAliasingAliasOption =
44570b57cec5SDimitry Andric       OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing;
44580b57cec5SDimitry Andric   // We turn strict aliasing off by default if we're in CL mode, since MSVC
44590b57cec5SDimitry Andric   // doesn't do any TBAA.
44600b57cec5SDimitry Andric   bool TBAAOnByDefault = !D.IsCLMode();
44610b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
44620b57cec5SDimitry Andric                     options::OPT_fno_strict_aliasing, TBAAOnByDefault))
44630b57cec5SDimitry Andric     CmdArgs.push_back("-relaxed-aliasing");
44640b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fstruct_path_tbaa,
44650b57cec5SDimitry Andric                     options::OPT_fno_struct_path_tbaa))
44660b57cec5SDimitry Andric     CmdArgs.push_back("-no-struct-path-tbaa");
44670b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
44680b57cec5SDimitry Andric                    false))
44690b57cec5SDimitry Andric     CmdArgs.push_back("-fstrict-enums");
44700b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fstrict_return, options::OPT_fno_strict_return,
44710b57cec5SDimitry Andric                     true))
44720b57cec5SDimitry Andric     CmdArgs.push_back("-fno-strict-return");
44730b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fallow_editor_placeholders,
44740b57cec5SDimitry Andric                    options::OPT_fno_allow_editor_placeholders, false))
44750b57cec5SDimitry Andric     CmdArgs.push_back("-fallow-editor-placeholders");
44760b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fstrict_vtable_pointers,
44770b57cec5SDimitry Andric                    options::OPT_fno_strict_vtable_pointers,
44780b57cec5SDimitry Andric                    false))
44790b57cec5SDimitry Andric     CmdArgs.push_back("-fstrict-vtable-pointers");
44800b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fforce_emit_vtables,
44810b57cec5SDimitry Andric                    options::OPT_fno_force_emit_vtables,
44820b57cec5SDimitry Andric                    false))
44830b57cec5SDimitry Andric     CmdArgs.push_back("-fforce-emit-vtables");
44840b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
44850b57cec5SDimitry Andric                     options::OPT_fno_optimize_sibling_calls))
44860b57cec5SDimitry Andric     CmdArgs.push_back("-mdisable-tail-calls");
44870b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fno_escaping_block_tail_calls,
44880b57cec5SDimitry Andric                    options::OPT_fescaping_block_tail_calls, false))
44890b57cec5SDimitry Andric     CmdArgs.push_back("-fno-escaping-block-tail-calls");
44900b57cec5SDimitry Andric 
44910b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ffine_grained_bitfield_accesses,
44920b57cec5SDimitry Andric                   options::OPT_fno_fine_grained_bitfield_accesses);
44930b57cec5SDimitry Andric 
44940b57cec5SDimitry Andric   // Handle segmented stacks.
44950b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fsplit_stack))
44960b57cec5SDimitry Andric     CmdArgs.push_back("-split-stacks");
44970b57cec5SDimitry Andric 
44980b57cec5SDimitry Andric   RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs);
44990b57cec5SDimitry Andric 
4500a7dea167SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_LongDouble_Group)) {
4501*480093f4SDimitry Andric     if (TC.getTriple().isX86())
4502a7dea167SDimitry Andric       A->render(Args, CmdArgs);
4503a7dea167SDimitry Andric     else if ((TC.getArch() == llvm::Triple::ppc || TC.getTriple().isPPC64()) &&
4504a7dea167SDimitry Andric              (A->getOption().getID() != options::OPT_mlong_double_80))
45050b57cec5SDimitry Andric       A->render(Args, CmdArgs);
45060b57cec5SDimitry Andric     else
45070b57cec5SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
45080b57cec5SDimitry Andric           << A->getAsString(Args) << TripleStr;
45090b57cec5SDimitry Andric   }
45100b57cec5SDimitry Andric 
45110b57cec5SDimitry Andric   // Decide whether to use verbose asm. Verbose assembly is the default on
45120b57cec5SDimitry Andric   // toolchains which have the integrated assembler on by default.
45130b57cec5SDimitry Andric   bool IsIntegratedAssemblerDefault = TC.IsIntegratedAssemblerDefault();
45140b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
4515a7dea167SDimitry Andric                    IsIntegratedAssemblerDefault))
45160b57cec5SDimitry Andric     CmdArgs.push_back("-masm-verbose");
45170b57cec5SDimitry Andric 
45180b57cec5SDimitry Andric   if (!TC.useIntegratedAs())
45190b57cec5SDimitry Andric     CmdArgs.push_back("-no-integrated-as");
45200b57cec5SDimitry Andric 
45210b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
45220b57cec5SDimitry Andric     CmdArgs.push_back("-mdebug-pass");
45230b57cec5SDimitry Andric     CmdArgs.push_back("Structure");
45240b57cec5SDimitry Andric   }
45250b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
45260b57cec5SDimitry Andric     CmdArgs.push_back("-mdebug-pass");
45270b57cec5SDimitry Andric     CmdArgs.push_back("Arguments");
45280b57cec5SDimitry Andric   }
45290b57cec5SDimitry Andric 
45300b57cec5SDimitry Andric   // Enable -mconstructor-aliases except on darwin, where we have to work around
45310b57cec5SDimitry Andric   // a linker bug (see <rdar://problem/7651567>), and CUDA device code, where
45320b57cec5SDimitry Andric   // aliases aren't supported.
45330b57cec5SDimitry Andric   if (!RawTriple.isOSDarwin() && !RawTriple.isNVPTX())
45340b57cec5SDimitry Andric     CmdArgs.push_back("-mconstructor-aliases");
45350b57cec5SDimitry Andric 
45360b57cec5SDimitry Andric   // Darwin's kernel doesn't support guard variables; just die if we
45370b57cec5SDimitry Andric   // try to use them.
45380b57cec5SDimitry Andric   if (KernelOrKext && RawTriple.isOSDarwin())
45390b57cec5SDimitry Andric     CmdArgs.push_back("-fforbid-guard-variables");
45400b57cec5SDimitry Andric 
45410b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_mms_bitfields, options::OPT_mno_ms_bitfields,
45420b57cec5SDimitry Andric                    false)) {
45430b57cec5SDimitry Andric     CmdArgs.push_back("-mms-bitfields");
45440b57cec5SDimitry Andric   }
45450b57cec5SDimitry Andric 
45460b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_mpie_copy_relocations,
45470b57cec5SDimitry Andric                    options::OPT_mno_pie_copy_relocations,
45480b57cec5SDimitry Andric                    false)) {
45490b57cec5SDimitry Andric     CmdArgs.push_back("-mpie-copy-relocations");
45500b57cec5SDimitry Andric   }
45510b57cec5SDimitry Andric 
45520b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) {
45530b57cec5SDimitry Andric     CmdArgs.push_back("-fno-plt");
45540b57cec5SDimitry Andric   }
45550b57cec5SDimitry Andric 
45560b57cec5SDimitry Andric   // -fhosted is default.
45570b57cec5SDimitry Andric   // TODO: Audit uses of KernelOrKext and see where it'd be more appropriate to
45580b57cec5SDimitry Andric   // use Freestanding.
45590b57cec5SDimitry Andric   bool Freestanding =
45600b57cec5SDimitry Andric       Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
45610b57cec5SDimitry Andric       KernelOrKext;
45620b57cec5SDimitry Andric   if (Freestanding)
45630b57cec5SDimitry Andric     CmdArgs.push_back("-ffreestanding");
45640b57cec5SDimitry Andric 
45650b57cec5SDimitry Andric   // This is a coarse approximation of what llvm-gcc actually does, both
45660b57cec5SDimitry Andric   // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
45670b57cec5SDimitry Andric   // complicated ways.
45680b57cec5SDimitry Andric   bool AsynchronousUnwindTables =
45690b57cec5SDimitry Andric       Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
45700b57cec5SDimitry Andric                    options::OPT_fno_asynchronous_unwind_tables,
45710b57cec5SDimitry Andric                    (TC.IsUnwindTablesDefault(Args) ||
45720b57cec5SDimitry Andric                     TC.getSanitizerArgs().needsUnwindTables()) &&
45730b57cec5SDimitry Andric                        !Freestanding);
45740b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
45750b57cec5SDimitry Andric                    AsynchronousUnwindTables))
45760b57cec5SDimitry Andric     CmdArgs.push_back("-munwind-tables");
45770b57cec5SDimitry Andric 
45780b57cec5SDimitry Andric   TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind());
45790b57cec5SDimitry Andric 
45800b57cec5SDimitry Andric   // FIXME: Handle -mtune=.
45810b57cec5SDimitry Andric   (void)Args.hasArg(options::OPT_mtune_EQ);
45820b57cec5SDimitry Andric 
45830b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
45840b57cec5SDimitry Andric     CmdArgs.push_back("-mcode-model");
45850b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
45860b57cec5SDimitry Andric   }
45870b57cec5SDimitry Andric 
4588*480093f4SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mtls_size_EQ)) {
4589*480093f4SDimitry Andric     StringRef Value = A->getValue();
4590*480093f4SDimitry Andric     unsigned TLSSize = 0;
4591*480093f4SDimitry Andric     Value.getAsInteger(10, TLSSize);
4592*480093f4SDimitry Andric     if (!Triple.isAArch64() || !Triple.isOSBinFormatELF())
4593*480093f4SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
4594*480093f4SDimitry Andric           << A->getOption().getName() << TripleStr;
4595*480093f4SDimitry Andric     if (TLSSize != 12 && TLSSize != 24 && TLSSize != 32 && TLSSize != 48)
4596*480093f4SDimitry Andric       D.Diag(diag::err_drv_invalid_int_value)
4597*480093f4SDimitry Andric           << A->getOption().getName() << Value;
4598*480093f4SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_mtls_size_EQ);
4599*480093f4SDimitry Andric   }
4600*480093f4SDimitry Andric 
46010b57cec5SDimitry Andric   // Add the target cpu
46020b57cec5SDimitry Andric   std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false);
46030b57cec5SDimitry Andric   if (!CPU.empty()) {
46040b57cec5SDimitry Andric     CmdArgs.push_back("-target-cpu");
46050b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(CPU));
46060b57cec5SDimitry Andric   }
46070b57cec5SDimitry Andric 
46080b57cec5SDimitry Andric   RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs);
46090b57cec5SDimitry Andric 
46100b57cec5SDimitry Andric   // These two are potentially updated by AddClangCLArgs.
46110b57cec5SDimitry Andric   codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo;
46120b57cec5SDimitry Andric   bool EmitCodeView = false;
46130b57cec5SDimitry Andric 
46140b57cec5SDimitry Andric   // Add clang-cl arguments.
46150b57cec5SDimitry Andric   types::ID InputType = Input.getType();
46160b57cec5SDimitry Andric   if (D.IsCLMode())
46170b57cec5SDimitry Andric     AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView);
46180b57cec5SDimitry Andric 
46190b57cec5SDimitry Andric   DwarfFissionKind DwarfFission;
46200b57cec5SDimitry Andric   RenderDebugOptions(TC, D, RawTriple, Args, EmitCodeView, IsWindowsMSVC,
46210b57cec5SDimitry Andric                      CmdArgs, DebugInfoKind, DwarfFission);
46220b57cec5SDimitry Andric 
46230b57cec5SDimitry Andric   // Add the split debug info name to the command lines here so we
46240b57cec5SDimitry Andric   // can propagate it to the backend.
46250b57cec5SDimitry Andric   bool SplitDWARF = (DwarfFission != DwarfFissionKind::None) &&
46260b57cec5SDimitry Andric                     TC.getTriple().isOSBinFormatELF() &&
46270b57cec5SDimitry Andric                     (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) ||
46280b57cec5SDimitry Andric                      isa<BackendJobAction>(JA));
46290b57cec5SDimitry Andric   if (SplitDWARF) {
46300b57cec5SDimitry Andric     const char *SplitDWARFOut = SplitDebugName(Args, Input, Output);
46310b57cec5SDimitry Andric     CmdArgs.push_back("-split-dwarf-file");
46320b57cec5SDimitry Andric     CmdArgs.push_back(SplitDWARFOut);
46330b57cec5SDimitry Andric     if (DwarfFission == DwarfFissionKind::Split) {
46340b57cec5SDimitry Andric       CmdArgs.push_back("-split-dwarf-output");
46350b57cec5SDimitry Andric       CmdArgs.push_back(SplitDWARFOut);
46360b57cec5SDimitry Andric     }
46370b57cec5SDimitry Andric   }
46380b57cec5SDimitry Andric 
46390b57cec5SDimitry Andric   // Pass the linker version in use.
46400b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
46410b57cec5SDimitry Andric     CmdArgs.push_back("-target-linker-version");
46420b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
46430b57cec5SDimitry Andric   }
46440b57cec5SDimitry Andric 
46450b57cec5SDimitry Andric   // Explicitly error on some things we know we don't support and can't just
46460b57cec5SDimitry Andric   // ignore.
46470b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_fallow_unsupported)) {
46480b57cec5SDimitry Andric     Arg *Unsupported;
46490b57cec5SDimitry Andric     if (types::isCXX(InputType) && RawTriple.isOSDarwin() &&
46500b57cec5SDimitry Andric         TC.getArch() == llvm::Triple::x86) {
46510b57cec5SDimitry Andric       if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
46520b57cec5SDimitry Andric           (Unsupported = Args.getLastArg(options::OPT_mkernel)))
46530b57cec5SDimitry Andric         D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
46540b57cec5SDimitry Andric             << Unsupported->getOption().getName();
46550b57cec5SDimitry Andric     }
46560b57cec5SDimitry Andric     // The faltivec option has been superseded by the maltivec option.
46570b57cec5SDimitry Andric     if ((Unsupported = Args.getLastArg(options::OPT_faltivec)))
46580b57cec5SDimitry Andric       D.Diag(diag::err_drv_clang_unsupported_opt_faltivec)
46590b57cec5SDimitry Andric           << Unsupported->getOption().getName()
46600b57cec5SDimitry Andric           << "please use -maltivec and include altivec.h explicitly";
46610b57cec5SDimitry Andric     if ((Unsupported = Args.getLastArg(options::OPT_fno_altivec)))
46620b57cec5SDimitry Andric       D.Diag(diag::err_drv_clang_unsupported_opt_faltivec)
46630b57cec5SDimitry Andric           << Unsupported->getOption().getName() << "please use -mno-altivec";
46640b57cec5SDimitry Andric   }
46650b57cec5SDimitry Andric 
46660b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_v);
46670b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_H);
46680b57cec5SDimitry Andric   if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
46690b57cec5SDimitry Andric     CmdArgs.push_back("-header-include-file");
46700b57cec5SDimitry Andric     CmdArgs.push_back(D.CCPrintHeadersFilename ? D.CCPrintHeadersFilename
46710b57cec5SDimitry Andric                                                : "-");
46720b57cec5SDimitry Andric   }
46730b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_P);
46740b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
46750b57cec5SDimitry Andric 
46760b57cec5SDimitry Andric   if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
46770b57cec5SDimitry Andric     CmdArgs.push_back("-diagnostic-log-file");
46780b57cec5SDimitry Andric     CmdArgs.push_back(D.CCLogDiagnosticsFilename ? D.CCLogDiagnosticsFilename
46790b57cec5SDimitry Andric                                                  : "-");
46800b57cec5SDimitry Andric   }
46810b57cec5SDimitry Andric 
46820b57cec5SDimitry Andric   bool UseSeparateSections = isUseSeparateSections(Triple);
46830b57cec5SDimitry Andric 
46840b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_ffunction_sections,
46850b57cec5SDimitry Andric                    options::OPT_fno_function_sections, UseSeparateSections)) {
46860b57cec5SDimitry Andric     CmdArgs.push_back("-ffunction-sections");
46870b57cec5SDimitry Andric   }
46880b57cec5SDimitry Andric 
46890b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections,
46900b57cec5SDimitry Andric                    UseSeparateSections)) {
46910b57cec5SDimitry Andric     CmdArgs.push_back("-fdata-sections");
46920b57cec5SDimitry Andric   }
46930b57cec5SDimitry Andric 
46940b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_funique_section_names,
46950b57cec5SDimitry Andric                     options::OPT_fno_unique_section_names, true))
46960b57cec5SDimitry Andric     CmdArgs.push_back("-fno-unique-section-names");
46970b57cec5SDimitry Andric 
46980b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_finstrument_functions,
46990b57cec5SDimitry Andric                   options::OPT_finstrument_functions_after_inlining,
47000b57cec5SDimitry Andric                   options::OPT_finstrument_function_entry_bare);
47010b57cec5SDimitry Andric 
47020b57cec5SDimitry Andric   // NVPTX doesn't support PGO or coverage. There's no runtime support for
47030b57cec5SDimitry Andric   // sampling, overhead of call arc collection is way too high and there's no
47040b57cec5SDimitry Andric   // way to collect the output.
47050b57cec5SDimitry Andric   if (!Triple.isNVPTX())
47060b57cec5SDimitry Andric     addPGOAndCoverageFlags(TC, C, D, Output, Args, CmdArgs);
47070b57cec5SDimitry Andric 
47080b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fclang_abi_compat_EQ);
47090b57cec5SDimitry Andric 
47100b57cec5SDimitry Andric   // Add runtime flag for PS4 when PGO, coverage, or sanitizers are enabled.
47110b57cec5SDimitry Andric   if (RawTriple.isPS4CPU() &&
47120b57cec5SDimitry Andric       !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
47130b57cec5SDimitry Andric     PS4cpu::addProfileRTArgs(TC, Args, CmdArgs);
47140b57cec5SDimitry Andric     PS4cpu::addSanitizerArgs(TC, CmdArgs);
47150b57cec5SDimitry Andric   }
47160b57cec5SDimitry Andric 
47170b57cec5SDimitry Andric   // Pass options for controlling the default header search paths.
47180b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_nostdinc)) {
47190b57cec5SDimitry Andric     CmdArgs.push_back("-nostdsysteminc");
47200b57cec5SDimitry Andric     CmdArgs.push_back("-nobuiltininc");
47210b57cec5SDimitry Andric   } else {
47220b57cec5SDimitry Andric     if (Args.hasArg(options::OPT_nostdlibinc))
47230b57cec5SDimitry Andric       CmdArgs.push_back("-nostdsysteminc");
47240b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
47250b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
47260b57cec5SDimitry Andric   }
47270b57cec5SDimitry Andric 
47280b57cec5SDimitry Andric   // Pass the path to compiler resource files.
47290b57cec5SDimitry Andric   CmdArgs.push_back("-resource-dir");
47300b57cec5SDimitry Andric   CmdArgs.push_back(D.ResourceDir.c_str());
47310b57cec5SDimitry Andric 
47320b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_working_directory);
47330b57cec5SDimitry Andric 
47340b57cec5SDimitry Andric   RenderARCMigrateToolOptions(D, Args, CmdArgs);
47350b57cec5SDimitry Andric 
47360b57cec5SDimitry Andric   // Add preprocessing options like -I, -D, etc. if we are using the
47370b57cec5SDimitry Andric   // preprocessor.
47380b57cec5SDimitry Andric   //
47390b57cec5SDimitry Andric   // FIXME: Support -fpreprocessed
47400b57cec5SDimitry Andric   if (types::getPreprocessedType(InputType) != types::TY_INVALID)
47410b57cec5SDimitry Andric     AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
47420b57cec5SDimitry Andric 
47430b57cec5SDimitry Andric   // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
47440b57cec5SDimitry Andric   // that "The compiler can only warn and ignore the option if not recognized".
47450b57cec5SDimitry Andric   // When building with ccache, it will pass -D options to clang even on
47460b57cec5SDimitry Andric   // preprocessed inputs and configure concludes that -fPIC is not supported.
47470b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_D);
47480b57cec5SDimitry Andric 
47490b57cec5SDimitry Andric   // Manually translate -O4 to -O3; let clang reject others.
47500b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
47510b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_O4)) {
47520b57cec5SDimitry Andric       CmdArgs.push_back("-O3");
47530b57cec5SDimitry Andric       D.Diag(diag::warn_O4_is_O3);
47540b57cec5SDimitry Andric     } else {
47550b57cec5SDimitry Andric       A->render(Args, CmdArgs);
47560b57cec5SDimitry Andric     }
47570b57cec5SDimitry Andric   }
47580b57cec5SDimitry Andric 
47590b57cec5SDimitry Andric   // Warn about ignored options to clang.
47600b57cec5SDimitry Andric   for (const Arg *A :
47610b57cec5SDimitry Andric        Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) {
47620b57cec5SDimitry Andric     D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args);
47630b57cec5SDimitry Andric     A->claim();
47640b57cec5SDimitry Andric   }
47650b57cec5SDimitry Andric 
47660b57cec5SDimitry Andric   for (const Arg *A :
47670b57cec5SDimitry Andric        Args.filtered(options::OPT_clang_ignored_legacy_options_Group)) {
47680b57cec5SDimitry Andric     D.Diag(diag::warn_ignored_clang_option) << A->getAsString(Args);
47690b57cec5SDimitry Andric     A->claim();
47700b57cec5SDimitry Andric   }
47710b57cec5SDimitry Andric 
47720b57cec5SDimitry Andric   claimNoWarnArgs(Args);
47730b57cec5SDimitry Andric 
47740b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_R_Group);
47750b57cec5SDimitry Andric 
47760b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
47770b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
47780b57cec5SDimitry Andric     CmdArgs.push_back("-pedantic");
47790b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
47800b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_w);
47810b57cec5SDimitry Andric 
47820b57cec5SDimitry Andric   // Fixed point flags
47830b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_ffixed_point, options::OPT_fno_fixed_point,
47840b57cec5SDimitry Andric                    /*Default=*/false))
47850b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_ffixed_point);
47860b57cec5SDimitry Andric 
47870b57cec5SDimitry Andric   // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
47880b57cec5SDimitry Andric   // (-ansi is equivalent to -std=c89 or -std=c++98).
47890b57cec5SDimitry Andric   //
47900b57cec5SDimitry Andric   // If a std is supplied, only add -trigraphs if it follows the
47910b57cec5SDimitry Andric   // option.
47920b57cec5SDimitry Andric   bool ImplyVCPPCXXVer = false;
47930b57cec5SDimitry Andric   const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi);
47940b57cec5SDimitry Andric   if (Std) {
47950b57cec5SDimitry Andric     if (Std->getOption().matches(options::OPT_ansi))
47960b57cec5SDimitry Andric       if (types::isCXX(InputType))
47970b57cec5SDimitry Andric         CmdArgs.push_back("-std=c++98");
47980b57cec5SDimitry Andric       else
47990b57cec5SDimitry Andric         CmdArgs.push_back("-std=c89");
48000b57cec5SDimitry Andric     else
48010b57cec5SDimitry Andric       Std->render(Args, CmdArgs);
48020b57cec5SDimitry Andric 
48030b57cec5SDimitry Andric     // If -f(no-)trigraphs appears after the language standard flag, honor it.
48040b57cec5SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
48050b57cec5SDimitry Andric                                  options::OPT_ftrigraphs,
48060b57cec5SDimitry Andric                                  options::OPT_fno_trigraphs))
48070b57cec5SDimitry Andric       if (A != Std)
48080b57cec5SDimitry Andric         A->render(Args, CmdArgs);
48090b57cec5SDimitry Andric   } else {
48100b57cec5SDimitry Andric     // Honor -std-default.
48110b57cec5SDimitry Andric     //
48120b57cec5SDimitry Andric     // FIXME: Clang doesn't correctly handle -std= when the input language
48130b57cec5SDimitry Andric     // doesn't match. For the time being just ignore this for C++ inputs;
48140b57cec5SDimitry Andric     // eventually we want to do all the standard defaulting here instead of
48150b57cec5SDimitry Andric     // splitting it between the driver and clang -cc1.
48160b57cec5SDimitry Andric     if (!types::isCXX(InputType))
48170b57cec5SDimitry Andric       Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=",
48180b57cec5SDimitry Andric                                 /*Joined=*/true);
48190b57cec5SDimitry Andric     else if (IsWindowsMSVC)
48200b57cec5SDimitry Andric       ImplyVCPPCXXVer = true;
48210b57cec5SDimitry Andric 
48220b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_ftrigraphs,
48230b57cec5SDimitry Andric                     options::OPT_fno_trigraphs);
48240b57cec5SDimitry Andric   }
48250b57cec5SDimitry Andric 
48260b57cec5SDimitry Andric   // GCC's behavior for -Wwrite-strings is a bit strange:
48270b57cec5SDimitry Andric   //  * In C, this "warning flag" changes the types of string literals from
48280b57cec5SDimitry Andric   //    'char[N]' to 'const char[N]', and thus triggers an unrelated warning
48290b57cec5SDimitry Andric   //    for the discarded qualifier.
48300b57cec5SDimitry Andric   //  * In C++, this is just a normal warning flag.
48310b57cec5SDimitry Andric   //
48320b57cec5SDimitry Andric   // Implementing this warning correctly in C is hard, so we follow GCC's
48330b57cec5SDimitry Andric   // behavior for now. FIXME: Directly diagnose uses of a string literal as
48340b57cec5SDimitry Andric   // a non-const char* in C, rather than using this crude hack.
48350b57cec5SDimitry Andric   if (!types::isCXX(InputType)) {
48360b57cec5SDimitry Andric     // FIXME: This should behave just like a warning flag, and thus should also
48370b57cec5SDimitry Andric     // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on.
48380b57cec5SDimitry Andric     Arg *WriteStrings =
48390b57cec5SDimitry Andric         Args.getLastArg(options::OPT_Wwrite_strings,
48400b57cec5SDimitry Andric                         options::OPT_Wno_write_strings, options::OPT_w);
48410b57cec5SDimitry Andric     if (WriteStrings &&
48420b57cec5SDimitry Andric         WriteStrings->getOption().matches(options::OPT_Wwrite_strings))
48430b57cec5SDimitry Andric       CmdArgs.push_back("-fconst-strings");
48440b57cec5SDimitry Andric   }
48450b57cec5SDimitry Andric 
48460b57cec5SDimitry Andric   // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
48470b57cec5SDimitry Andric   // during C++ compilation, which it is by default. GCC keeps this define even
48480b57cec5SDimitry Andric   // in the presence of '-w', match this behavior bug-for-bug.
48490b57cec5SDimitry Andric   if (types::isCXX(InputType) &&
48500b57cec5SDimitry Andric       Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
48510b57cec5SDimitry Andric                    true)) {
48520b57cec5SDimitry Andric     CmdArgs.push_back("-fdeprecated-macro");
48530b57cec5SDimitry Andric   }
48540b57cec5SDimitry Andric 
48550b57cec5SDimitry Andric   // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
48560b57cec5SDimitry Andric   if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
48570b57cec5SDimitry Andric     if (Asm->getOption().matches(options::OPT_fasm))
48580b57cec5SDimitry Andric       CmdArgs.push_back("-fgnu-keywords");
48590b57cec5SDimitry Andric     else
48600b57cec5SDimitry Andric       CmdArgs.push_back("-fno-gnu-keywords");
48610b57cec5SDimitry Andric   }
48620b57cec5SDimitry Andric 
48630b57cec5SDimitry Andric   if (ShouldDisableDwarfDirectory(Args, TC))
48640b57cec5SDimitry Andric     CmdArgs.push_back("-fno-dwarf-directory-asm");
48650b57cec5SDimitry Andric 
4866*480093f4SDimitry Andric   if (!ShouldEnableAutolink(Args, TC, JA))
48670b57cec5SDimitry Andric     CmdArgs.push_back("-fno-autolink");
48680b57cec5SDimitry Andric 
48690b57cec5SDimitry Andric   // Add in -fdebug-compilation-dir if necessary.
4870a7dea167SDimitry Andric   addDebugCompDirArg(Args, CmdArgs, D.getVFS());
48710b57cec5SDimitry Andric 
48720b57cec5SDimitry Andric   addDebugPrefixMapArg(D, Args, CmdArgs);
48730b57cec5SDimitry Andric 
48740b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
48750b57cec5SDimitry Andric                                options::OPT_ftemplate_depth_EQ)) {
48760b57cec5SDimitry Andric     CmdArgs.push_back("-ftemplate-depth");
48770b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
48780b57cec5SDimitry Andric   }
48790b57cec5SDimitry Andric 
48800b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) {
48810b57cec5SDimitry Andric     CmdArgs.push_back("-foperator-arrow-depth");
48820b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
48830b57cec5SDimitry Andric   }
48840b57cec5SDimitry Andric 
48850b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
48860b57cec5SDimitry Andric     CmdArgs.push_back("-fconstexpr-depth");
48870b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
48880b57cec5SDimitry Andric   }
48890b57cec5SDimitry Andric 
48900b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) {
48910b57cec5SDimitry Andric     CmdArgs.push_back("-fconstexpr-steps");
48920b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
48930b57cec5SDimitry Andric   }
48940b57cec5SDimitry Andric 
4895a7dea167SDimitry Andric   if (Args.hasArg(options::OPT_fexperimental_new_constant_interpreter))
4896a7dea167SDimitry Andric     CmdArgs.push_back("-fexperimental-new-constant-interpreter");
4897a7dea167SDimitry Andric 
48980b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
48990b57cec5SDimitry Andric     CmdArgs.push_back("-fbracket-depth");
49000b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
49010b57cec5SDimitry Andric   }
49020b57cec5SDimitry Andric 
49030b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
49040b57cec5SDimitry Andric                                options::OPT_Wlarge_by_value_copy_def)) {
49050b57cec5SDimitry Andric     if (A->getNumValues()) {
49060b57cec5SDimitry Andric       StringRef bytes = A->getValue();
49070b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
49080b57cec5SDimitry Andric     } else
49090b57cec5SDimitry Andric       CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
49100b57cec5SDimitry Andric   }
49110b57cec5SDimitry Andric 
49120b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_relocatable_pch))
49130b57cec5SDimitry Andric     CmdArgs.push_back("-relocatable-pch");
49140b57cec5SDimitry Andric 
49150b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fcf_runtime_abi_EQ)) {
49160b57cec5SDimitry Andric     static const char *kCFABIs[] = {
49170b57cec5SDimitry Andric       "standalone", "objc", "swift", "swift-5.0", "swift-4.2", "swift-4.1",
49180b57cec5SDimitry Andric     };
49190b57cec5SDimitry Andric 
49200b57cec5SDimitry Andric     if (find(kCFABIs, StringRef(A->getValue())) == std::end(kCFABIs))
49210b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_cf_runtime_abi) << A->getValue();
49220b57cec5SDimitry Andric     else
49230b57cec5SDimitry Andric       A->render(Args, CmdArgs);
49240b57cec5SDimitry Andric   }
49250b57cec5SDimitry Andric 
49260b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
49270b57cec5SDimitry Andric     CmdArgs.push_back("-fconstant-string-class");
49280b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
49290b57cec5SDimitry Andric   }
49300b57cec5SDimitry Andric 
49310b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
49320b57cec5SDimitry Andric     CmdArgs.push_back("-ftabstop");
49330b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
49340b57cec5SDimitry Andric   }
49350b57cec5SDimitry Andric 
49360b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fstack_size_section,
49370b57cec5SDimitry Andric                    options::OPT_fno_stack_size_section, RawTriple.isPS4()))
49380b57cec5SDimitry Andric     CmdArgs.push_back("-fstack-size-section");
49390b57cec5SDimitry Andric 
49400b57cec5SDimitry Andric   CmdArgs.push_back("-ferror-limit");
49410b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
49420b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
49430b57cec5SDimitry Andric   else
49440b57cec5SDimitry Andric     CmdArgs.push_back("19");
49450b57cec5SDimitry Andric 
49460b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
49470b57cec5SDimitry Andric     CmdArgs.push_back("-fmacro-backtrace-limit");
49480b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
49490b57cec5SDimitry Andric   }
49500b57cec5SDimitry Andric 
49510b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
49520b57cec5SDimitry Andric     CmdArgs.push_back("-ftemplate-backtrace-limit");
49530b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
49540b57cec5SDimitry Andric   }
49550b57cec5SDimitry Andric 
49560b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
49570b57cec5SDimitry Andric     CmdArgs.push_back("-fconstexpr-backtrace-limit");
49580b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
49590b57cec5SDimitry Andric   }
49600b57cec5SDimitry Andric 
49610b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fspell_checking_limit_EQ)) {
49620b57cec5SDimitry Andric     CmdArgs.push_back("-fspell-checking-limit");
49630b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
49640b57cec5SDimitry Andric   }
49650b57cec5SDimitry Andric 
49660b57cec5SDimitry Andric   // Pass -fmessage-length=.
49670b57cec5SDimitry Andric   CmdArgs.push_back("-fmessage-length");
49680b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
49690b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
49700b57cec5SDimitry Andric   } else {
49710b57cec5SDimitry Andric     // If -fmessage-length=N was not specified, determine whether this is a
49720b57cec5SDimitry Andric     // terminal and, if so, implicitly define -fmessage-length appropriately.
49730b57cec5SDimitry Andric     unsigned N = llvm::sys::Process::StandardErrColumns();
49740b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Twine(N)));
49750b57cec5SDimitry Andric   }
49760b57cec5SDimitry Andric 
49770b57cec5SDimitry Andric   // -fvisibility= and -fvisibility-ms-compat are of a piece.
49780b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
49790b57cec5SDimitry Andric                                      options::OPT_fvisibility_ms_compat)) {
49800b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
49810b57cec5SDimitry Andric       CmdArgs.push_back("-fvisibility");
49820b57cec5SDimitry Andric       CmdArgs.push_back(A->getValue());
49830b57cec5SDimitry Andric     } else {
49840b57cec5SDimitry Andric       assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
49850b57cec5SDimitry Andric       CmdArgs.push_back("-fvisibility");
49860b57cec5SDimitry Andric       CmdArgs.push_back("hidden");
49870b57cec5SDimitry Andric       CmdArgs.push_back("-ftype-visibility");
49880b57cec5SDimitry Andric       CmdArgs.push_back("default");
49890b57cec5SDimitry Andric     }
49900b57cec5SDimitry Andric   }
49910b57cec5SDimitry Andric 
49920b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
49930b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden);
49940b57cec5SDimitry Andric 
49950b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
49960b57cec5SDimitry Andric 
49970b57cec5SDimitry Andric   // Forward -f (flag) options which we can pass directly.
49980b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
49990b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
50000b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs);
50010b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
50020b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_femulated_tls,
50030b57cec5SDimitry Andric                   options::OPT_fno_emulated_tls);
50040b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fkeep_static_consts);
50050b57cec5SDimitry Andric 
50060b57cec5SDimitry Andric   // AltiVec-like language extensions aren't relevant for assembling.
50070b57cec5SDimitry Andric   if (!isa<PreprocessJobAction>(JA) || Output.getType() != types::TY_PP_Asm)
50080b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fzvector);
50090b57cec5SDimitry Andric 
50100b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
50110b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
50120b57cec5SDimitry Andric 
50130b57cec5SDimitry Andric   // Forward flags for OpenMP. We don't do this if the current action is an
50140b57cec5SDimitry Andric   // device offloading action other than OpenMP.
50150b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
50160b57cec5SDimitry Andric                    options::OPT_fno_openmp, false) &&
50170b57cec5SDimitry Andric       (JA.isDeviceOffloading(Action::OFK_None) ||
50180b57cec5SDimitry Andric        JA.isDeviceOffloading(Action::OFK_OpenMP))) {
50190b57cec5SDimitry Andric     switch (D.getOpenMPRuntime(Args)) {
50200b57cec5SDimitry Andric     case Driver::OMPRT_OMP:
50210b57cec5SDimitry Andric     case Driver::OMPRT_IOMP5:
50220b57cec5SDimitry Andric       // Clang can generate useful OpenMP code for these two runtime libraries.
50230b57cec5SDimitry Andric       CmdArgs.push_back("-fopenmp");
50240b57cec5SDimitry Andric 
50250b57cec5SDimitry Andric       // If no option regarding the use of TLS in OpenMP codegeneration is
50260b57cec5SDimitry Andric       // given, decide a default based on the target. Otherwise rely on the
50270b57cec5SDimitry Andric       // options and pass the right information to the frontend.
50280b57cec5SDimitry Andric       if (!Args.hasFlag(options::OPT_fopenmp_use_tls,
50290b57cec5SDimitry Andric                         options::OPT_fnoopenmp_use_tls, /*Default=*/true))
50300b57cec5SDimitry Andric         CmdArgs.push_back("-fnoopenmp-use-tls");
50310b57cec5SDimitry Andric       Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd,
50320b57cec5SDimitry Andric                       options::OPT_fno_openmp_simd);
5033*480093f4SDimitry Andric       Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_enable_irbuilder);
50340b57cec5SDimitry Andric       Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ);
50350b57cec5SDimitry Andric       Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_number_of_sm_EQ);
50360b57cec5SDimitry Andric       Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_blocks_per_sm_EQ);
50370b57cec5SDimitry Andric       Args.AddAllArgs(CmdArgs,
50380b57cec5SDimitry Andric                       options::OPT_fopenmp_cuda_teams_reduction_recs_num_EQ);
50390b57cec5SDimitry Andric       if (Args.hasFlag(options::OPT_fopenmp_optimistic_collapse,
50400b57cec5SDimitry Andric                        options::OPT_fno_openmp_optimistic_collapse,
50410b57cec5SDimitry Andric                        /*Default=*/false))
50420b57cec5SDimitry Andric         CmdArgs.push_back("-fopenmp-optimistic-collapse");
50430b57cec5SDimitry Andric 
50440b57cec5SDimitry Andric       // When in OpenMP offloading mode with NVPTX target, forward
50450b57cec5SDimitry Andric       // cuda-mode flag
50460b57cec5SDimitry Andric       if (Args.hasFlag(options::OPT_fopenmp_cuda_mode,
50470b57cec5SDimitry Andric                        options::OPT_fno_openmp_cuda_mode, /*Default=*/false))
50480b57cec5SDimitry Andric         CmdArgs.push_back("-fopenmp-cuda-mode");
50490b57cec5SDimitry Andric 
50500b57cec5SDimitry Andric       // When in OpenMP offloading mode with NVPTX target, check if full runtime
50510b57cec5SDimitry Andric       // is required.
50520b57cec5SDimitry Andric       if (Args.hasFlag(options::OPT_fopenmp_cuda_force_full_runtime,
50530b57cec5SDimitry Andric                        options::OPT_fno_openmp_cuda_force_full_runtime,
50540b57cec5SDimitry Andric                        /*Default=*/false))
50550b57cec5SDimitry Andric         CmdArgs.push_back("-fopenmp-cuda-force-full-runtime");
50560b57cec5SDimitry Andric       break;
50570b57cec5SDimitry Andric     default:
50580b57cec5SDimitry Andric       // By default, if Clang doesn't know how to generate useful OpenMP code
50590b57cec5SDimitry Andric       // for a specific runtime library, we just don't pass the '-fopenmp' flag
50600b57cec5SDimitry Andric       // down to the actual compilation.
50610b57cec5SDimitry Andric       // FIXME: It would be better to have a mode which *only* omits IR
50620b57cec5SDimitry Andric       // generation based on the OpenMP support so that we get consistent
50630b57cec5SDimitry Andric       // semantic analysis, etc.
50640b57cec5SDimitry Andric       break;
50650b57cec5SDimitry Andric     }
50660b57cec5SDimitry Andric   } else {
50670b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd,
50680b57cec5SDimitry Andric                     options::OPT_fno_openmp_simd);
50690b57cec5SDimitry Andric     Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ);
50700b57cec5SDimitry Andric   }
50710b57cec5SDimitry Andric 
50720b57cec5SDimitry Andric   const SanitizerArgs &Sanitize = TC.getSanitizerArgs();
50730b57cec5SDimitry Andric   Sanitize.addArgs(TC, Args, CmdArgs, InputType);
50740b57cec5SDimitry Andric 
50750b57cec5SDimitry Andric   const XRayArgs &XRay = TC.getXRayArgs();
50760b57cec5SDimitry Andric   XRay.addArgs(TC, Args, CmdArgs, InputType);
50770b57cec5SDimitry Andric 
5078*480093f4SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ)) {
5079*480093f4SDimitry Andric     StringRef S0 = A->getValue(), S = S0;
5080*480093f4SDimitry Andric     unsigned Size, Start = 0;
5081*480093f4SDimitry Andric     if (!Triple.isAArch64() && Triple.getArch() != llvm::Triple::x86 &&
5082*480093f4SDimitry Andric         Triple.getArch() != llvm::Triple::x86_64)
5083*480093f4SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
5084*480093f4SDimitry Andric           << A->getAsString(Args) << TripleStr;
5085*480093f4SDimitry Andric     else if (S.consumeInteger(10, Size) ||
5086*480093f4SDimitry Andric              (!S.empty() && (!S.consume_front(",") ||
5087*480093f4SDimitry Andric                              S.consumeInteger(10, Start) || !S.empty())))
5088*480093f4SDimitry Andric       D.Diag(diag::err_drv_invalid_argument_to_option)
5089*480093f4SDimitry Andric           << S0 << A->getOption().getName();
5090*480093f4SDimitry Andric     else if (Start)
5091*480093f4SDimitry Andric       D.Diag(diag::err_drv_unsupported_fpatchable_function_entry_argument);
5092*480093f4SDimitry Andric     else
5093*480093f4SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(A->getSpelling() + Twine(Size)));
5094*480093f4SDimitry Andric   }
5095*480093f4SDimitry Andric 
5096*480093f4SDimitry Andric   if (TC.SupportsProfiling()) {
50970b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_pg);
50980b57cec5SDimitry Andric 
5099*480093f4SDimitry Andric     llvm::Triple::ArchType Arch = TC.getArch();
5100*480093f4SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_mfentry)) {
5101*480093f4SDimitry Andric       if (Arch == llvm::Triple::systemz || TC.getTriple().isX86())
5102*480093f4SDimitry Andric         A->render(Args, CmdArgs);
5103*480093f4SDimitry Andric       else
5104*480093f4SDimitry Andric         D.Diag(diag::err_drv_unsupported_opt_for_target)
5105*480093f4SDimitry Andric             << A->getAsString(Args) << TripleStr;
5106*480093f4SDimitry Andric     }
5107*480093f4SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_mnop_mcount)) {
5108*480093f4SDimitry Andric       if (Arch == llvm::Triple::systemz)
5109*480093f4SDimitry Andric         A->render(Args, CmdArgs);
5110*480093f4SDimitry Andric       else
5111*480093f4SDimitry Andric         D.Diag(diag::err_drv_unsupported_opt_for_target)
5112*480093f4SDimitry Andric             << A->getAsString(Args) << TripleStr;
5113*480093f4SDimitry Andric     }
5114*480093f4SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_mrecord_mcount)) {
5115*480093f4SDimitry Andric       if (Arch == llvm::Triple::systemz)
5116*480093f4SDimitry Andric         A->render(Args, CmdArgs);
5117*480093f4SDimitry Andric       else
5118*480093f4SDimitry Andric         D.Diag(diag::err_drv_unsupported_opt_for_target)
5119*480093f4SDimitry Andric             << A->getAsString(Args) << TripleStr;
5120*480093f4SDimitry Andric     }
5121*480093f4SDimitry Andric   }
51220b57cec5SDimitry Andric 
51230b57cec5SDimitry Andric   if (Args.getLastArg(options::OPT_fapple_kext) ||
51240b57cec5SDimitry Andric       (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType)))
51250b57cec5SDimitry Andric     CmdArgs.push_back("-fapple-kext");
51260b57cec5SDimitry Andric 
5127a7dea167SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_flax_vector_conversions_EQ);
51280b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
51290b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
51300b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
51310b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
51320b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftime_trace);
5133a7dea167SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_granularity_EQ);
51340b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
51350b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_malign_double);
5136*480093f4SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fno_temp_file);
51370b57cec5SDimitry Andric 
51380b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
51390b57cec5SDimitry Andric     CmdArgs.push_back("-ftrapv-handler");
51400b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
51410b57cec5SDimitry Andric   }
51420b57cec5SDimitry Andric 
51430b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
51440b57cec5SDimitry Andric 
51450b57cec5SDimitry Andric   // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
51460b57cec5SDimitry Andric   // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
51470b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fwrapv, options::OPT_fno_wrapv)) {
51480b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fwrapv))
51490b57cec5SDimitry Andric       CmdArgs.push_back("-fwrapv");
51500b57cec5SDimitry Andric   } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
51510b57cec5SDimitry Andric                                       options::OPT_fno_strict_overflow)) {
51520b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fno_strict_overflow))
51530b57cec5SDimitry Andric       CmdArgs.push_back("-fwrapv");
51540b57cec5SDimitry Andric   }
51550b57cec5SDimitry Andric 
51560b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_freroll_loops,
51570b57cec5SDimitry Andric                                options::OPT_fno_reroll_loops))
51580b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_freroll_loops))
51590b57cec5SDimitry Andric       CmdArgs.push_back("-freroll-loops");
51600b57cec5SDimitry Andric 
51610b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
51620b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_funroll_loops,
51630b57cec5SDimitry Andric                   options::OPT_fno_unroll_loops);
51640b57cec5SDimitry Andric 
51650b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_pthread);
51660b57cec5SDimitry Andric 
51670b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_mspeculative_load_hardening, options::OPT_mno_speculative_load_hardening,
51680b57cec5SDimitry Andric                    false))
51690b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-mspeculative-load-hardening"));
51700b57cec5SDimitry Andric 
51710b57cec5SDimitry Andric   RenderSSPOptions(TC, Args, CmdArgs, KernelOrKext);
51720b57cec5SDimitry Andric   RenderTrivialAutoVarInitOptions(D, TC, Args, CmdArgs);
51730b57cec5SDimitry Andric 
51740b57cec5SDimitry Andric   // Translate -mstackrealign
51750b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
51760b57cec5SDimitry Andric                    false))
51770b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
51780b57cec5SDimitry Andric 
51790b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_mstack_alignment)) {
51800b57cec5SDimitry Andric     StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
51810b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
51820b57cec5SDimitry Andric   }
51830b57cec5SDimitry Andric 
51840b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_mstack_probe_size)) {
51850b57cec5SDimitry Andric     StringRef Size = Args.getLastArgValue(options::OPT_mstack_probe_size);
51860b57cec5SDimitry Andric 
51870b57cec5SDimitry Andric     if (!Size.empty())
51880b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-mstack-probe-size=" + Size));
51890b57cec5SDimitry Andric     else
51900b57cec5SDimitry Andric       CmdArgs.push_back("-mstack-probe-size=0");
51910b57cec5SDimitry Andric   }
51920b57cec5SDimitry Andric 
51930b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_mstack_arg_probe,
51940b57cec5SDimitry Andric                     options::OPT_mno_stack_arg_probe, true))
51950b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-mno-stack-arg-probe"));
51960b57cec5SDimitry Andric 
51970b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it,
51980b57cec5SDimitry Andric                                options::OPT_mno_restrict_it)) {
51990b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mrestrict_it)) {
52000b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
52010b57cec5SDimitry Andric       CmdArgs.push_back("-arm-restrict-it");
52020b57cec5SDimitry Andric     } else {
52030b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
52040b57cec5SDimitry Andric       CmdArgs.push_back("-arm-no-restrict-it");
52050b57cec5SDimitry Andric     }
52060b57cec5SDimitry Andric   } else if (Triple.isOSWindows() &&
52070b57cec5SDimitry Andric              (Triple.getArch() == llvm::Triple::arm ||
52080b57cec5SDimitry Andric               Triple.getArch() == llvm::Triple::thumb)) {
52090b57cec5SDimitry Andric     // Windows on ARM expects restricted IT blocks
52100b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
52110b57cec5SDimitry Andric     CmdArgs.push_back("-arm-restrict-it");
52120b57cec5SDimitry Andric   }
52130b57cec5SDimitry Andric 
52140b57cec5SDimitry Andric   // Forward -cl options to -cc1
52150b57cec5SDimitry Andric   RenderOpenCLOptions(Args, CmdArgs);
52160b57cec5SDimitry Andric 
5217a7dea167SDimitry Andric   if (Args.hasFlag(options::OPT_fhip_new_launch_api,
5218a7dea167SDimitry Andric                    options::OPT_fno_hip_new_launch_api, false))
5219a7dea167SDimitry Andric     CmdArgs.push_back("-fhip-new-launch-api");
5220a7dea167SDimitry Andric 
52210b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) {
52220b57cec5SDimitry Andric     CmdArgs.push_back(
52230b57cec5SDimitry Andric         Args.MakeArgString(Twine("-fcf-protection=") + A->getValue()));
52240b57cec5SDimitry Andric   }
52250b57cec5SDimitry Andric 
52260b57cec5SDimitry Andric   // Forward -f options with positive and negative forms; we translate
52270b57cec5SDimitry Andric   // these by hand.
52280b57cec5SDimitry Andric   if (Arg *A = getLastProfileSampleUseArg(Args)) {
52290b57cec5SDimitry Andric     auto *PGOArg = Args.getLastArg(
52300b57cec5SDimitry Andric         options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ,
52310b57cec5SDimitry Andric         options::OPT_fcs_profile_generate, options::OPT_fcs_profile_generate_EQ,
52320b57cec5SDimitry Andric         options::OPT_fprofile_use, options::OPT_fprofile_use_EQ);
52330b57cec5SDimitry Andric     if (PGOArg)
52340b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_not_allowed_with)
52350b57cec5SDimitry Andric           << "SampleUse with PGO options";
52360b57cec5SDimitry Andric 
52370b57cec5SDimitry Andric     StringRef fname = A->getValue();
52380b57cec5SDimitry Andric     if (!llvm::sys::fs::exists(fname))
52390b57cec5SDimitry Andric       D.Diag(diag::err_drv_no_such_file) << fname;
52400b57cec5SDimitry Andric     else
52410b57cec5SDimitry Andric       A->render(Args, CmdArgs);
52420b57cec5SDimitry Andric   }
52430b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fprofile_remapping_file_EQ);
52440b57cec5SDimitry Andric 
52450b57cec5SDimitry Andric   RenderBuiltinOptions(TC, RawTriple, Args, CmdArgs);
52460b57cec5SDimitry Andric 
52470b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
52480b57cec5SDimitry Andric                     options::OPT_fno_assume_sane_operator_new))
52490b57cec5SDimitry Andric     CmdArgs.push_back("-fno-assume-sane-operator-new");
52500b57cec5SDimitry Andric 
52510b57cec5SDimitry Andric   // -fblocks=0 is default.
52520b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
52530b57cec5SDimitry Andric                    TC.IsBlocksDefault()) ||
52540b57cec5SDimitry Andric       (Args.hasArg(options::OPT_fgnu_runtime) &&
52550b57cec5SDimitry Andric        Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
52560b57cec5SDimitry Andric        !Args.hasArg(options::OPT_fno_blocks))) {
52570b57cec5SDimitry Andric     CmdArgs.push_back("-fblocks");
52580b57cec5SDimitry Andric 
52590b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_fgnu_runtime) && !TC.hasBlocksRuntime())
52600b57cec5SDimitry Andric       CmdArgs.push_back("-fblocks-runtime-optional");
52610b57cec5SDimitry Andric   }
52620b57cec5SDimitry Andric 
52630b57cec5SDimitry Andric   // -fencode-extended-block-signature=1 is default.
52640b57cec5SDimitry Andric   if (TC.IsEncodeExtendedBlockSignatureDefault())
52650b57cec5SDimitry Andric     CmdArgs.push_back("-fencode-extended-block-signature");
52660b57cec5SDimitry Andric 
52670b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fcoroutines_ts, options::OPT_fno_coroutines_ts,
52680b57cec5SDimitry Andric                    false) &&
52690b57cec5SDimitry Andric       types::isCXX(InputType)) {
52700b57cec5SDimitry Andric     CmdArgs.push_back("-fcoroutines-ts");
52710b57cec5SDimitry Andric   }
52720b57cec5SDimitry Andric 
52730b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fdouble_square_bracket_attributes,
52740b57cec5SDimitry Andric                   options::OPT_fno_double_square_bracket_attributes);
52750b57cec5SDimitry Andric 
52760b57cec5SDimitry Andric   // -faccess-control is default.
52770b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fno_access_control,
52780b57cec5SDimitry Andric                    options::OPT_faccess_control, false))
52790b57cec5SDimitry Andric     CmdArgs.push_back("-fno-access-control");
52800b57cec5SDimitry Andric 
52810b57cec5SDimitry Andric   // -felide-constructors is the default.
52820b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fno_elide_constructors,
52830b57cec5SDimitry Andric                    options::OPT_felide_constructors, false))
52840b57cec5SDimitry Andric     CmdArgs.push_back("-fno-elide-constructors");
52850b57cec5SDimitry Andric 
52860b57cec5SDimitry Andric   ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
52870b57cec5SDimitry Andric 
52880b57cec5SDimitry Andric   if (KernelOrKext || (types::isCXX(InputType) &&
52890b57cec5SDimitry Andric                        (RTTIMode == ToolChain::RM_Disabled)))
52900b57cec5SDimitry Andric     CmdArgs.push_back("-fno-rtti");
52910b57cec5SDimitry Andric 
52920b57cec5SDimitry Andric   // -fshort-enums=0 is default for all architectures except Hexagon.
52930b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fshort_enums, options::OPT_fno_short_enums,
52940b57cec5SDimitry Andric                    TC.getArch() == llvm::Triple::hexagon))
52950b57cec5SDimitry Andric     CmdArgs.push_back("-fshort-enums");
52960b57cec5SDimitry Andric 
52970b57cec5SDimitry Andric   RenderCharacterOptions(Args, AuxTriple ? *AuxTriple : RawTriple, CmdArgs);
52980b57cec5SDimitry Andric 
52990b57cec5SDimitry Andric   // -fuse-cxa-atexit is default.
53000b57cec5SDimitry Andric   if (!Args.hasFlag(
53010b57cec5SDimitry Andric           options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
53020b57cec5SDimitry Andric           !RawTriple.isOSWindows() &&
53030b57cec5SDimitry Andric               TC.getArch() != llvm::Triple::xcore &&
53040b57cec5SDimitry Andric               ((RawTriple.getVendor() != llvm::Triple::MipsTechnologies) ||
53050b57cec5SDimitry Andric                RawTriple.hasEnvironment())) ||
53060b57cec5SDimitry Andric       KernelOrKext)
53070b57cec5SDimitry Andric     CmdArgs.push_back("-fno-use-cxa-atexit");
53080b57cec5SDimitry Andric 
53090b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fregister_global_dtors_with_atexit,
53100b57cec5SDimitry Andric                    options::OPT_fno_register_global_dtors_with_atexit,
53110b57cec5SDimitry Andric                    RawTriple.isOSDarwin() && !KernelOrKext))
53120b57cec5SDimitry Andric     CmdArgs.push_back("-fregister-global-dtors-with-atexit");
53130b57cec5SDimitry Andric 
53140b57cec5SDimitry Andric   // -fms-extensions=0 is default.
53150b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
53160b57cec5SDimitry Andric                    IsWindowsMSVC))
53170b57cec5SDimitry Andric     CmdArgs.push_back("-fms-extensions");
53180b57cec5SDimitry Andric 
53190b57cec5SDimitry Andric   // -fno-use-line-directives is default.
53200b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fuse_line_directives,
53210b57cec5SDimitry Andric                    options::OPT_fno_use_line_directives, false))
53220b57cec5SDimitry Andric     CmdArgs.push_back("-fuse-line-directives");
53230b57cec5SDimitry Andric 
53240b57cec5SDimitry Andric   // -fms-compatibility=0 is default.
5325a7dea167SDimitry Andric   bool IsMSVCCompat = Args.hasFlag(
5326a7dea167SDimitry Andric       options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility,
5327a7dea167SDimitry Andric       (IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions,
5328a7dea167SDimitry Andric                                      options::OPT_fno_ms_extensions, true)));
5329a7dea167SDimitry Andric   if (IsMSVCCompat)
53300b57cec5SDimitry Andric     CmdArgs.push_back("-fms-compatibility");
53310b57cec5SDimitry Andric 
5332a7dea167SDimitry Andric   // Handle -fgcc-version, if present.
5333a7dea167SDimitry Andric   VersionTuple GNUCVer;
5334a7dea167SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fgnuc_version_EQ)) {
5335a7dea167SDimitry Andric     // Check that the version has 1 to 3 components and the minor and patch
5336a7dea167SDimitry Andric     // versions fit in two decimal digits.
5337a7dea167SDimitry Andric     StringRef Val = A->getValue();
5338a7dea167SDimitry Andric     Val = Val.empty() ? "0" : Val; // Treat "" as 0 or disable.
5339a7dea167SDimitry Andric     bool Invalid = GNUCVer.tryParse(Val);
5340a7dea167SDimitry Andric     unsigned Minor = GNUCVer.getMinor().getValueOr(0);
5341a7dea167SDimitry Andric     unsigned Patch = GNUCVer.getSubminor().getValueOr(0);
5342a7dea167SDimitry Andric     if (Invalid || GNUCVer.getBuild() || Minor >= 100 || Patch >= 100) {
5343a7dea167SDimitry Andric       D.Diag(diag::err_drv_invalid_value)
5344a7dea167SDimitry Andric           << A->getAsString(Args) << A->getValue();
5345a7dea167SDimitry Andric     }
5346a7dea167SDimitry Andric   } else if (!IsMSVCCompat) {
5347a7dea167SDimitry Andric     // Imitate GCC 4.2.1 by default if -fms-compatibility is not in effect.
5348a7dea167SDimitry Andric     GNUCVer = VersionTuple(4, 2, 1);
5349a7dea167SDimitry Andric   }
5350a7dea167SDimitry Andric   if (!GNUCVer.empty()) {
5351a7dea167SDimitry Andric     CmdArgs.push_back(
5352a7dea167SDimitry Andric         Args.MakeArgString("-fgnuc-version=" + GNUCVer.getAsString()));
5353a7dea167SDimitry Andric   }
5354a7dea167SDimitry Andric 
53550b57cec5SDimitry Andric   VersionTuple MSVT = TC.computeMSVCVersion(&D, Args);
53560b57cec5SDimitry Andric   if (!MSVT.empty())
53570b57cec5SDimitry Andric     CmdArgs.push_back(
53580b57cec5SDimitry Andric         Args.MakeArgString("-fms-compatibility-version=" + MSVT.getAsString()));
53590b57cec5SDimitry Andric 
53600b57cec5SDimitry Andric   bool IsMSVC2015Compatible = MSVT.getMajor() >= 19;
53610b57cec5SDimitry Andric   if (ImplyVCPPCXXVer) {
53620b57cec5SDimitry Andric     StringRef LanguageStandard;
53630b57cec5SDimitry Andric     if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) {
53640b57cec5SDimitry Andric       Std = StdArg;
53650b57cec5SDimitry Andric       LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue())
53660b57cec5SDimitry Andric                              .Case("c++14", "-std=c++14")
53670b57cec5SDimitry Andric                              .Case("c++17", "-std=c++17")
53680b57cec5SDimitry Andric                              .Case("c++latest", "-std=c++2a")
53690b57cec5SDimitry Andric                              .Default("");
53700b57cec5SDimitry Andric       if (LanguageStandard.empty())
53710b57cec5SDimitry Andric         D.Diag(clang::diag::warn_drv_unused_argument)
53720b57cec5SDimitry Andric             << StdArg->getAsString(Args);
53730b57cec5SDimitry Andric     }
53740b57cec5SDimitry Andric 
53750b57cec5SDimitry Andric     if (LanguageStandard.empty()) {
53760b57cec5SDimitry Andric       if (IsMSVC2015Compatible)
53770b57cec5SDimitry Andric         LanguageStandard = "-std=c++14";
53780b57cec5SDimitry Andric       else
53790b57cec5SDimitry Andric         LanguageStandard = "-std=c++11";
53800b57cec5SDimitry Andric     }
53810b57cec5SDimitry Andric 
53820b57cec5SDimitry Andric     CmdArgs.push_back(LanguageStandard.data());
53830b57cec5SDimitry Andric   }
53840b57cec5SDimitry Andric 
53850b57cec5SDimitry Andric   // -fno-borland-extensions is default.
53860b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fborland_extensions,
53870b57cec5SDimitry Andric                    options::OPT_fno_borland_extensions, false))
53880b57cec5SDimitry Andric     CmdArgs.push_back("-fborland-extensions");
53890b57cec5SDimitry Andric 
53900b57cec5SDimitry Andric   // -fno-declspec is default, except for PS4.
53910b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec,
53920b57cec5SDimitry Andric                    RawTriple.isPS4()))
53930b57cec5SDimitry Andric     CmdArgs.push_back("-fdeclspec");
53940b57cec5SDimitry Andric   else if (Args.hasArg(options::OPT_fno_declspec))
53950b57cec5SDimitry Andric     CmdArgs.push_back("-fno-declspec"); // Explicitly disabling __declspec.
53960b57cec5SDimitry Andric 
53970b57cec5SDimitry Andric   // -fthreadsafe-static is default, except for MSVC compatibility versions less
53980b57cec5SDimitry Andric   // than 19.
53990b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
54000b57cec5SDimitry Andric                     options::OPT_fno_threadsafe_statics,
54010b57cec5SDimitry Andric                     !IsWindowsMSVC || IsMSVC2015Compatible))
54020b57cec5SDimitry Andric     CmdArgs.push_back("-fno-threadsafe-statics");
54030b57cec5SDimitry Andric 
54040b57cec5SDimitry Andric   // -fno-delayed-template-parsing is default, except when targeting MSVC.
54050b57cec5SDimitry Andric   // Many old Windows SDK versions require this to parse.
54060b57cec5SDimitry Andric   // FIXME: MSVC introduced /Zc:twoPhase- to disable this behavior in their
54070b57cec5SDimitry Andric   // compiler. We should be able to disable this by default at some point.
54080b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
54090b57cec5SDimitry Andric                    options::OPT_fno_delayed_template_parsing, IsWindowsMSVC))
54100b57cec5SDimitry Andric     CmdArgs.push_back("-fdelayed-template-parsing");
54110b57cec5SDimitry Andric 
54120b57cec5SDimitry Andric   // -fgnu-keywords default varies depending on language; only pass if
54130b57cec5SDimitry Andric   // specified.
54140b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fgnu_keywords,
54150b57cec5SDimitry Andric                   options::OPT_fno_gnu_keywords);
54160b57cec5SDimitry Andric 
54170b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fgnu89_inline, options::OPT_fno_gnu89_inline,
54180b57cec5SDimitry Andric                    false))
54190b57cec5SDimitry Andric     CmdArgs.push_back("-fgnu89-inline");
54200b57cec5SDimitry Andric 
54210b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fno_inline))
54220b57cec5SDimitry Andric     CmdArgs.push_back("-fno-inline");
54230b57cec5SDimitry Andric 
54240b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_finline_functions,
54250b57cec5SDimitry Andric                   options::OPT_finline_hint_functions,
54260b57cec5SDimitry Andric                   options::OPT_fno_inline_functions);
54270b57cec5SDimitry Andric 
54280b57cec5SDimitry Andric   // FIXME: Find a better way to determine whether the language has modules
54290b57cec5SDimitry Andric   // support by default, or just assume that all languages do.
54300b57cec5SDimitry Andric   bool HaveModules =
54310b57cec5SDimitry Andric       Std && (Std->containsValue("c++2a") || Std->containsValue("c++latest"));
54320b57cec5SDimitry Andric   RenderModulesOptions(C, D, Args, Input, Output, CmdArgs, HaveModules);
54330b57cec5SDimitry Andric 
5434a7dea167SDimitry Andric   if (Args.hasFlag(options::OPT_fpch_validate_input_files_content,
5435a7dea167SDimitry Andric                    options::OPT_fno_pch_validate_input_files_content, false))
5436a7dea167SDimitry Andric     CmdArgs.push_back("-fvalidate-ast-input-files-content");
5437a7dea167SDimitry Andric 
54380b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fexperimental_new_pass_manager,
54390b57cec5SDimitry Andric                   options::OPT_fno_experimental_new_pass_manager);
54400b57cec5SDimitry Andric 
54410b57cec5SDimitry Andric   ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
54420b57cec5SDimitry Andric   RenderObjCOptions(TC, D, RawTriple, Args, Runtime, rewriteKind != RK_None,
54430b57cec5SDimitry Andric                     Input, CmdArgs);
54440b57cec5SDimitry Andric 
54450b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fapplication_extension,
54460b57cec5SDimitry Andric                    options::OPT_fno_application_extension, false))
54470b57cec5SDimitry Andric     CmdArgs.push_back("-fapplication-extension");
54480b57cec5SDimitry Andric 
54490b57cec5SDimitry Andric   // Handle GCC-style exception args.
54500b57cec5SDimitry Andric   if (!C.getDriver().IsCLMode())
54510b57cec5SDimitry Andric     addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs);
54520b57cec5SDimitry Andric 
54530b57cec5SDimitry Andric   // Handle exception personalities
5454a7dea167SDimitry Andric   Arg *A = Args.getLastArg(
5455a7dea167SDimitry Andric       options::OPT_fsjlj_exceptions, options::OPT_fseh_exceptions,
5456a7dea167SDimitry Andric       options::OPT_fdwarf_exceptions, options::OPT_fwasm_exceptions);
54570b57cec5SDimitry Andric   if (A) {
54580b57cec5SDimitry Andric     const Option &Opt = A->getOption();
54590b57cec5SDimitry Andric     if (Opt.matches(options::OPT_fsjlj_exceptions))
54600b57cec5SDimitry Andric       CmdArgs.push_back("-fsjlj-exceptions");
54610b57cec5SDimitry Andric     if (Opt.matches(options::OPT_fseh_exceptions))
54620b57cec5SDimitry Andric       CmdArgs.push_back("-fseh-exceptions");
54630b57cec5SDimitry Andric     if (Opt.matches(options::OPT_fdwarf_exceptions))
54640b57cec5SDimitry Andric       CmdArgs.push_back("-fdwarf-exceptions");
5465a7dea167SDimitry Andric     if (Opt.matches(options::OPT_fwasm_exceptions))
5466a7dea167SDimitry Andric       CmdArgs.push_back("-fwasm-exceptions");
54670b57cec5SDimitry Andric   } else {
54680b57cec5SDimitry Andric     switch (TC.GetExceptionModel(Args)) {
54690b57cec5SDimitry Andric     default:
54700b57cec5SDimitry Andric       break;
54710b57cec5SDimitry Andric     case llvm::ExceptionHandling::DwarfCFI:
54720b57cec5SDimitry Andric       CmdArgs.push_back("-fdwarf-exceptions");
54730b57cec5SDimitry Andric       break;
54740b57cec5SDimitry Andric     case llvm::ExceptionHandling::SjLj:
54750b57cec5SDimitry Andric       CmdArgs.push_back("-fsjlj-exceptions");
54760b57cec5SDimitry Andric       break;
54770b57cec5SDimitry Andric     case llvm::ExceptionHandling::WinEH:
54780b57cec5SDimitry Andric       CmdArgs.push_back("-fseh-exceptions");
54790b57cec5SDimitry Andric       break;
54800b57cec5SDimitry Andric     }
54810b57cec5SDimitry Andric   }
54820b57cec5SDimitry Andric 
54830b57cec5SDimitry Andric   // C++ "sane" operator new.
54840b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
54850b57cec5SDimitry Andric                     options::OPT_fno_assume_sane_operator_new))
54860b57cec5SDimitry Andric     CmdArgs.push_back("-fno-assume-sane-operator-new");
54870b57cec5SDimitry Andric 
54880b57cec5SDimitry Andric   // -frelaxed-template-template-args is off by default, as it is a severe
54890b57cec5SDimitry Andric   // breaking change until a corresponding change to template partial ordering
54900b57cec5SDimitry Andric   // is provided.
54910b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_frelaxed_template_template_args,
54920b57cec5SDimitry Andric                    options::OPT_fno_relaxed_template_template_args, false))
54930b57cec5SDimitry Andric     CmdArgs.push_back("-frelaxed-template-template-args");
54940b57cec5SDimitry Andric 
54950b57cec5SDimitry Andric   // -fsized-deallocation is off by default, as it is an ABI-breaking change for
54960b57cec5SDimitry Andric   // most platforms.
54970b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fsized_deallocation,
54980b57cec5SDimitry Andric                    options::OPT_fno_sized_deallocation, false))
54990b57cec5SDimitry Andric     CmdArgs.push_back("-fsized-deallocation");
55000b57cec5SDimitry Andric 
55010b57cec5SDimitry Andric   // -faligned-allocation is on by default in C++17 onwards and otherwise off
55020b57cec5SDimitry Andric   // by default.
55030b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_faligned_allocation,
55040b57cec5SDimitry Andric                                options::OPT_fno_aligned_allocation,
55050b57cec5SDimitry Andric                                options::OPT_faligned_new_EQ)) {
55060b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fno_aligned_allocation))
55070b57cec5SDimitry Andric       CmdArgs.push_back("-fno-aligned-allocation");
55080b57cec5SDimitry Andric     else
55090b57cec5SDimitry Andric       CmdArgs.push_back("-faligned-allocation");
55100b57cec5SDimitry Andric   }
55110b57cec5SDimitry Andric 
55120b57cec5SDimitry Andric   // The default new alignment can be specified using a dedicated option or via
55130b57cec5SDimitry Andric   // a GCC-compatible option that also turns on aligned allocation.
55140b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fnew_alignment_EQ,
55150b57cec5SDimitry Andric                                options::OPT_faligned_new_EQ))
55160b57cec5SDimitry Andric     CmdArgs.push_back(
55170b57cec5SDimitry Andric         Args.MakeArgString(Twine("-fnew-alignment=") + A->getValue()));
55180b57cec5SDimitry Andric 
55190b57cec5SDimitry Andric   // -fconstant-cfstrings is default, and may be subject to argument translation
55200b57cec5SDimitry Andric   // on Darwin.
55210b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
55220b57cec5SDimitry Andric                     options::OPT_fno_constant_cfstrings) ||
55230b57cec5SDimitry Andric       !Args.hasFlag(options::OPT_mconstant_cfstrings,
55240b57cec5SDimitry Andric                     options::OPT_mno_constant_cfstrings))
55250b57cec5SDimitry Andric     CmdArgs.push_back("-fno-constant-cfstrings");
55260b57cec5SDimitry Andric 
55270b57cec5SDimitry Andric   // -fno-pascal-strings is default, only pass non-default.
55280b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fpascal_strings,
55290b57cec5SDimitry Andric                    options::OPT_fno_pascal_strings, false))
55300b57cec5SDimitry Andric     CmdArgs.push_back("-fpascal-strings");
55310b57cec5SDimitry Andric 
55320b57cec5SDimitry Andric   // Honor -fpack-struct= and -fpack-struct, if given. Note that
55330b57cec5SDimitry Andric   // -fno-pack-struct doesn't apply to -fpack-struct=.
55340b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
55350b57cec5SDimitry Andric     std::string PackStructStr = "-fpack-struct=";
55360b57cec5SDimitry Andric     PackStructStr += A->getValue();
55370b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(PackStructStr));
55380b57cec5SDimitry Andric   } else if (Args.hasFlag(options::OPT_fpack_struct,
55390b57cec5SDimitry Andric                           options::OPT_fno_pack_struct, false)) {
55400b57cec5SDimitry Andric     CmdArgs.push_back("-fpack-struct=1");
55410b57cec5SDimitry Andric   }
55420b57cec5SDimitry Andric 
55430b57cec5SDimitry Andric   // Handle -fmax-type-align=N and -fno-type-align
55440b57cec5SDimitry Andric   bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align);
55450b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) {
55460b57cec5SDimitry Andric     if (!SkipMaxTypeAlign) {
55470b57cec5SDimitry Andric       std::string MaxTypeAlignStr = "-fmax-type-align=";
55480b57cec5SDimitry Andric       MaxTypeAlignStr += A->getValue();
55490b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
55500b57cec5SDimitry Andric     }
55510b57cec5SDimitry Andric   } else if (RawTriple.isOSDarwin()) {
55520b57cec5SDimitry Andric     if (!SkipMaxTypeAlign) {
55530b57cec5SDimitry Andric       std::string MaxTypeAlignStr = "-fmax-type-align=16";
55540b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
55550b57cec5SDimitry Andric     }
55560b57cec5SDimitry Andric   }
55570b57cec5SDimitry Andric 
55580b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_Qy, options::OPT_Qn, true))
55590b57cec5SDimitry Andric     CmdArgs.push_back("-Qn");
55600b57cec5SDimitry Andric 
55610b57cec5SDimitry Andric   // -fcommon is the default unless compiling kernel code or the target says so
55620b57cec5SDimitry Andric   bool NoCommonDefault = KernelOrKext || isNoCommonDefault(RawTriple);
55630b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common,
55640b57cec5SDimitry Andric                     !NoCommonDefault))
55650b57cec5SDimitry Andric     CmdArgs.push_back("-fno-common");
55660b57cec5SDimitry Andric 
55670b57cec5SDimitry Andric   // -fsigned-bitfields is default, and clang doesn't yet support
55680b57cec5SDimitry Andric   // -funsigned-bitfields.
55690b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fsigned_bitfields,
55700b57cec5SDimitry Andric                     options::OPT_funsigned_bitfields))
55710b57cec5SDimitry Andric     D.Diag(diag::warn_drv_clang_unsupported)
55720b57cec5SDimitry Andric         << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
55730b57cec5SDimitry Andric 
55740b57cec5SDimitry Andric   // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
55750b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope))
55760b57cec5SDimitry Andric     D.Diag(diag::err_drv_clang_unsupported)
55770b57cec5SDimitry Andric         << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
55780b57cec5SDimitry Andric 
55790b57cec5SDimitry Andric   // -finput_charset=UTF-8 is default. Reject others
55800b57cec5SDimitry Andric   if (Arg *inputCharset = Args.getLastArg(options::OPT_finput_charset_EQ)) {
55810b57cec5SDimitry Andric     StringRef value = inputCharset->getValue();
55820b57cec5SDimitry Andric     if (!value.equals_lower("utf-8"))
55830b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args)
55840b57cec5SDimitry Andric                                           << value;
55850b57cec5SDimitry Andric   }
55860b57cec5SDimitry Andric 
55870b57cec5SDimitry Andric   // -fexec_charset=UTF-8 is default. Reject others
55880b57cec5SDimitry Andric   if (Arg *execCharset = Args.getLastArg(options::OPT_fexec_charset_EQ)) {
55890b57cec5SDimitry Andric     StringRef value = execCharset->getValue();
55900b57cec5SDimitry Andric     if (!value.equals_lower("utf-8"))
55910b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args)
55920b57cec5SDimitry Andric                                           << value;
55930b57cec5SDimitry Andric   }
55940b57cec5SDimitry Andric 
55950b57cec5SDimitry Andric   RenderDiagnosticsOptions(D, Args, CmdArgs);
55960b57cec5SDimitry Andric 
55970b57cec5SDimitry Andric   // -fno-asm-blocks is default.
55980b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
55990b57cec5SDimitry Andric                    false))
56000b57cec5SDimitry Andric     CmdArgs.push_back("-fasm-blocks");
56010b57cec5SDimitry Andric 
56020b57cec5SDimitry Andric   // -fgnu-inline-asm is default.
56030b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fgnu_inline_asm,
56040b57cec5SDimitry Andric                     options::OPT_fno_gnu_inline_asm, true))
56050b57cec5SDimitry Andric     CmdArgs.push_back("-fno-gnu-inline-asm");
56060b57cec5SDimitry Andric 
56070b57cec5SDimitry Andric   // Enable vectorization per default according to the optimization level
56080b57cec5SDimitry Andric   // selected. For optimization levels that want vectorization we use the alias
56090b57cec5SDimitry Andric   // option to simplify the hasFlag logic.
56100b57cec5SDimitry Andric   bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false);
56110b57cec5SDimitry Andric   OptSpecifier VectorizeAliasOption =
56120b57cec5SDimitry Andric       EnableVec ? options::OPT_O_Group : options::OPT_fvectorize;
56130b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
56140b57cec5SDimitry Andric                    options::OPT_fno_vectorize, EnableVec))
56150b57cec5SDimitry Andric     CmdArgs.push_back("-vectorize-loops");
56160b57cec5SDimitry Andric 
56170b57cec5SDimitry Andric   // -fslp-vectorize is enabled based on the optimization level selected.
56180b57cec5SDimitry Andric   bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true);
56190b57cec5SDimitry Andric   OptSpecifier SLPVectAliasOption =
56200b57cec5SDimitry Andric       EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize;
56210b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption,
56220b57cec5SDimitry Andric                    options::OPT_fno_slp_vectorize, EnableSLPVec))
56230b57cec5SDimitry Andric     CmdArgs.push_back("-vectorize-slp");
56240b57cec5SDimitry Andric 
56250b57cec5SDimitry Andric   ParseMPreferVectorWidth(D, Args, CmdArgs);
56260b57cec5SDimitry Andric 
56270b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fshow_overloads_EQ);
56280b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs,
56290b57cec5SDimitry Andric                   options::OPT_fsanitize_undefined_strip_path_components_EQ);
56300b57cec5SDimitry Andric 
56310b57cec5SDimitry Andric   // -fdollars-in-identifiers default varies depending on platform and
56320b57cec5SDimitry Andric   // language; only pass if specified.
56330b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
56340b57cec5SDimitry Andric                                options::OPT_fno_dollars_in_identifiers)) {
56350b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
56360b57cec5SDimitry Andric       CmdArgs.push_back("-fdollars-in-identifiers");
56370b57cec5SDimitry Andric     else
56380b57cec5SDimitry Andric       CmdArgs.push_back("-fno-dollars-in-identifiers");
56390b57cec5SDimitry Andric   }
56400b57cec5SDimitry Andric 
56410b57cec5SDimitry Andric   // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
56420b57cec5SDimitry Andric   // practical purposes.
56430b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
56440b57cec5SDimitry Andric                                options::OPT_fno_unit_at_a_time)) {
56450b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
56460b57cec5SDimitry Andric       D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
56470b57cec5SDimitry Andric   }
56480b57cec5SDimitry Andric 
56490b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fapple_pragma_pack,
56500b57cec5SDimitry Andric                    options::OPT_fno_apple_pragma_pack, false))
56510b57cec5SDimitry Andric     CmdArgs.push_back("-fapple-pragma-pack");
56520b57cec5SDimitry Andric 
56530b57cec5SDimitry Andric   // Remarks can be enabled with any of the `-f.*optimization-record.*` flags.
5654*480093f4SDimitry Andric   if (willEmitRemarks(Args) && checkRemarksOptions(D, Args, Triple))
5655*480093f4SDimitry Andric     renderRemarksOptions(Args, CmdArgs, Triple, Input, Output, JA);
56560b57cec5SDimitry Andric 
56570b57cec5SDimitry Andric   bool RewriteImports = Args.hasFlag(options::OPT_frewrite_imports,
56580b57cec5SDimitry Andric                                      options::OPT_fno_rewrite_imports, false);
56590b57cec5SDimitry Andric   if (RewriteImports)
56600b57cec5SDimitry Andric     CmdArgs.push_back("-frewrite-imports");
56610b57cec5SDimitry Andric 
56620b57cec5SDimitry Andric   // Enable rewrite includes if the user's asked for it or if we're generating
56630b57cec5SDimitry Andric   // diagnostics.
56640b57cec5SDimitry Andric   // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be
56650b57cec5SDimitry Andric   // nice to enable this when doing a crashdump for modules as well.
56660b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_frewrite_includes,
56670b57cec5SDimitry Andric                    options::OPT_fno_rewrite_includes, false) ||
56680b57cec5SDimitry Andric       (C.isForDiagnostics() && !HaveModules))
56690b57cec5SDimitry Andric     CmdArgs.push_back("-frewrite-includes");
56700b57cec5SDimitry Andric 
56710b57cec5SDimitry Andric   // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
56720b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_traditional,
56730b57cec5SDimitry Andric                                options::OPT_traditional_cpp)) {
56740b57cec5SDimitry Andric     if (isa<PreprocessJobAction>(JA))
56750b57cec5SDimitry Andric       CmdArgs.push_back("-traditional-cpp");
56760b57cec5SDimitry Andric     else
56770b57cec5SDimitry Andric       D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
56780b57cec5SDimitry Andric   }
56790b57cec5SDimitry Andric 
56800b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_dM);
56810b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_dD);
56820b57cec5SDimitry Andric 
56830b57cec5SDimitry Andric   // Handle serialized diagnostics.
56840b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
56850b57cec5SDimitry Andric     CmdArgs.push_back("-serialize-diagnostic-file");
56860b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(A->getValue()));
56870b57cec5SDimitry Andric   }
56880b57cec5SDimitry Andric 
56890b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
56900b57cec5SDimitry Andric     CmdArgs.push_back("-fretain-comments-from-system-headers");
56910b57cec5SDimitry Andric 
56920b57cec5SDimitry Andric   // Forward -fcomment-block-commands to -cc1.
56930b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
56940b57cec5SDimitry Andric   // Forward -fparse-all-comments to -cc1.
56950b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments);
56960b57cec5SDimitry Andric 
56970b57cec5SDimitry Andric   // Turn -fplugin=name.so into -load name.so
56980b57cec5SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_fplugin_EQ)) {
56990b57cec5SDimitry Andric     CmdArgs.push_back("-load");
57000b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
57010b57cec5SDimitry Andric     A->claim();
57020b57cec5SDimitry Andric   }
57030b57cec5SDimitry Andric 
57040b57cec5SDimitry Andric   // Forward -fpass-plugin=name.so to -cc1.
57050b57cec5SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_fpass_plugin_EQ)) {
57060b57cec5SDimitry Andric     CmdArgs.push_back(
57070b57cec5SDimitry Andric         Args.MakeArgString(Twine("-fpass-plugin=") + A->getValue()));
57080b57cec5SDimitry Andric     A->claim();
57090b57cec5SDimitry Andric   }
57100b57cec5SDimitry Andric 
57110b57cec5SDimitry Andric   // Setup statistics file output.
57120b57cec5SDimitry Andric   SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D);
57130b57cec5SDimitry Andric   if (!StatsFile.empty())
57140b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Twine("-stats-file=") + StatsFile));
57150b57cec5SDimitry Andric 
57160b57cec5SDimitry Andric   // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
57170b57cec5SDimitry Andric   // parser.
57180b57cec5SDimitry Andric   // -finclude-default-header flag is for preprocessor,
57190b57cec5SDimitry Andric   // do not pass it to other cc1 commands when save-temps is enabled
57200b57cec5SDimitry Andric   if (C.getDriver().isSaveTempsEnabled() &&
57210b57cec5SDimitry Andric       !isa<PreprocessJobAction>(JA)) {
57220b57cec5SDimitry Andric     for (auto Arg : Args.filtered(options::OPT_Xclang)) {
57230b57cec5SDimitry Andric       Arg->claim();
57240b57cec5SDimitry Andric       if (StringRef(Arg->getValue()) != "-finclude-default-header")
57250b57cec5SDimitry Andric         CmdArgs.push_back(Arg->getValue());
57260b57cec5SDimitry Andric     }
57270b57cec5SDimitry Andric   }
57280b57cec5SDimitry Andric   else {
57290b57cec5SDimitry Andric     Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
57300b57cec5SDimitry Andric   }
57310b57cec5SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
57320b57cec5SDimitry Andric     A->claim();
57330b57cec5SDimitry Andric 
57340b57cec5SDimitry Andric     // We translate this by hand to the -cc1 argument, since nightly test uses
57350b57cec5SDimitry Andric     // it and developers have been trained to spell it with -mllvm. Both
57360b57cec5SDimitry Andric     // spellings are now deprecated and should be removed.
57370b57cec5SDimitry Andric     if (StringRef(A->getValue(0)) == "-disable-llvm-optzns") {
57380b57cec5SDimitry Andric       CmdArgs.push_back("-disable-llvm-optzns");
57390b57cec5SDimitry Andric     } else {
57400b57cec5SDimitry Andric       A->render(Args, CmdArgs);
57410b57cec5SDimitry Andric     }
57420b57cec5SDimitry Andric   }
57430b57cec5SDimitry Andric 
57440b57cec5SDimitry Andric   // With -save-temps, we want to save the unoptimized bitcode output from the
57450b57cec5SDimitry Andric   // CompileJobAction, use -disable-llvm-passes to get pristine IR generated
57460b57cec5SDimitry Andric   // by the frontend.
57470b57cec5SDimitry Andric   // When -fembed-bitcode is enabled, optimized bitcode is emitted because it
57480b57cec5SDimitry Andric   // has slightly different breakdown between stages.
57490b57cec5SDimitry Andric   // FIXME: -fembed-bitcode -save-temps will save optimized bitcode instead of
57500b57cec5SDimitry Andric   // pristine IR generated by the frontend. Ideally, a new compile action should
57510b57cec5SDimitry Andric   // be added so both IR can be captured.
57520b57cec5SDimitry Andric   if (C.getDriver().isSaveTempsEnabled() &&
57530b57cec5SDimitry Andric       !(C.getDriver().embedBitcodeInObject() && !C.getDriver().isUsingLTO()) &&
57540b57cec5SDimitry Andric       isa<CompileJobAction>(JA))
57550b57cec5SDimitry Andric     CmdArgs.push_back("-disable-llvm-passes");
57560b57cec5SDimitry Andric 
57570b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_undef);
57580b57cec5SDimitry Andric 
57590b57cec5SDimitry Andric   const char *Exec = D.getClangProgramPath();
57600b57cec5SDimitry Andric 
57610b57cec5SDimitry Andric   // Optionally embed the -cc1 level arguments into the debug info or a
57620b57cec5SDimitry Andric   // section, for build analysis.
57630b57cec5SDimitry Andric   // Also record command line arguments into the debug info if
57640b57cec5SDimitry Andric   // -grecord-gcc-switches options is set on.
57650b57cec5SDimitry Andric   // By default, -gno-record-gcc-switches is set on and no recording.
57660b57cec5SDimitry Andric   auto GRecordSwitches =
57670b57cec5SDimitry Andric       Args.hasFlag(options::OPT_grecord_command_line,
57680b57cec5SDimitry Andric                    options::OPT_gno_record_command_line, false);
57690b57cec5SDimitry Andric   auto FRecordSwitches =
57700b57cec5SDimitry Andric       Args.hasFlag(options::OPT_frecord_command_line,
57710b57cec5SDimitry Andric                    options::OPT_fno_record_command_line, false);
57720b57cec5SDimitry Andric   if (FRecordSwitches && !Triple.isOSBinFormatELF())
57730b57cec5SDimitry Andric     D.Diag(diag::err_drv_unsupported_opt_for_target)
57740b57cec5SDimitry Andric         << Args.getLastArg(options::OPT_frecord_command_line)->getAsString(Args)
57750b57cec5SDimitry Andric         << TripleStr;
57760b57cec5SDimitry Andric   if (TC.UseDwarfDebugFlags() || GRecordSwitches || FRecordSwitches) {
57770b57cec5SDimitry Andric     ArgStringList OriginalArgs;
57780b57cec5SDimitry Andric     for (const auto &Arg : Args)
57790b57cec5SDimitry Andric       Arg->render(Args, OriginalArgs);
57800b57cec5SDimitry Andric 
57810b57cec5SDimitry Andric     SmallString<256> Flags;
57820b57cec5SDimitry Andric     Flags += Exec;
57830b57cec5SDimitry Andric     for (const char *OriginalArg : OriginalArgs) {
57840b57cec5SDimitry Andric       SmallString<128> EscapedArg;
57850b57cec5SDimitry Andric       EscapeSpacesAndBackslashes(OriginalArg, EscapedArg);
57860b57cec5SDimitry Andric       Flags += " ";
57870b57cec5SDimitry Andric       Flags += EscapedArg;
57880b57cec5SDimitry Andric     }
57890b57cec5SDimitry Andric     auto FlagsArgString = Args.MakeArgString(Flags);
57900b57cec5SDimitry Andric     if (TC.UseDwarfDebugFlags() || GRecordSwitches) {
57910b57cec5SDimitry Andric       CmdArgs.push_back("-dwarf-debug-flags");
57920b57cec5SDimitry Andric       CmdArgs.push_back(FlagsArgString);
57930b57cec5SDimitry Andric     }
57940b57cec5SDimitry Andric     if (FRecordSwitches) {
57950b57cec5SDimitry Andric       CmdArgs.push_back("-record-command-line");
57960b57cec5SDimitry Andric       CmdArgs.push_back(FlagsArgString);
57970b57cec5SDimitry Andric     }
57980b57cec5SDimitry Andric   }
57990b57cec5SDimitry Andric 
58000b57cec5SDimitry Andric   // Host-side cuda compilation receives all device-side outputs in a single
58010b57cec5SDimitry Andric   // fatbin as Inputs[1]. Include the binary with -fcuda-include-gpubinary.
58020b57cec5SDimitry Andric   if ((IsCuda || IsHIP) && CudaDeviceInput) {
58030b57cec5SDimitry Andric       CmdArgs.push_back("-fcuda-include-gpubinary");
58040b57cec5SDimitry Andric       CmdArgs.push_back(CudaDeviceInput->getFilename());
58050b57cec5SDimitry Andric       if (Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false))
58060b57cec5SDimitry Andric         CmdArgs.push_back("-fgpu-rdc");
58070b57cec5SDimitry Andric   }
58080b57cec5SDimitry Andric 
58090b57cec5SDimitry Andric   if (IsCuda) {
58100b57cec5SDimitry Andric     if (Args.hasFlag(options::OPT_fcuda_short_ptr,
58110b57cec5SDimitry Andric                      options::OPT_fno_cuda_short_ptr, false))
58120b57cec5SDimitry Andric       CmdArgs.push_back("-fcuda-short-ptr");
58130b57cec5SDimitry Andric   }
58140b57cec5SDimitry Andric 
5815*480093f4SDimitry Andric   if (IsHIP)
5816*480093f4SDimitry Andric     CmdArgs.push_back("-fcuda-allow-variadic-functions");
5817*480093f4SDimitry Andric 
58180b57cec5SDimitry Andric   // OpenMP offloading device jobs take the argument -fopenmp-host-ir-file-path
58190b57cec5SDimitry Andric   // to specify the result of the compile phase on the host, so the meaningful
58200b57cec5SDimitry Andric   // device declarations can be identified. Also, -fopenmp-is-device is passed
58210b57cec5SDimitry Andric   // along to tell the frontend that it is generating code for a device, so that
58220b57cec5SDimitry Andric   // only the relevant declarations are emitted.
58230b57cec5SDimitry Andric   if (IsOpenMPDevice) {
58240b57cec5SDimitry Andric     CmdArgs.push_back("-fopenmp-is-device");
58250b57cec5SDimitry Andric     if (OpenMPDeviceInput) {
58260b57cec5SDimitry Andric       CmdArgs.push_back("-fopenmp-host-ir-file-path");
58270b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(OpenMPDeviceInput->getFilename()));
58280b57cec5SDimitry Andric     }
58290b57cec5SDimitry Andric   }
58300b57cec5SDimitry Andric 
58310b57cec5SDimitry Andric   // For all the host OpenMP offloading compile jobs we need to pass the targets
58320b57cec5SDimitry Andric   // information using -fopenmp-targets= option.
58330b57cec5SDimitry Andric   if (JA.isHostOffloading(Action::OFK_OpenMP)) {
58340b57cec5SDimitry Andric     SmallString<128> TargetInfo("-fopenmp-targets=");
58350b57cec5SDimitry Andric 
58360b57cec5SDimitry Andric     Arg *Tgts = Args.getLastArg(options::OPT_fopenmp_targets_EQ);
58370b57cec5SDimitry Andric     assert(Tgts && Tgts->getNumValues() &&
58380b57cec5SDimitry Andric            "OpenMP offloading has to have targets specified.");
58390b57cec5SDimitry Andric     for (unsigned i = 0; i < Tgts->getNumValues(); ++i) {
58400b57cec5SDimitry Andric       if (i)
58410b57cec5SDimitry Andric         TargetInfo += ',';
58420b57cec5SDimitry Andric       // We need to get the string from the triple because it may be not exactly
58430b57cec5SDimitry Andric       // the same as the one we get directly from the arguments.
58440b57cec5SDimitry Andric       llvm::Triple T(Tgts->getValue(i));
58450b57cec5SDimitry Andric       TargetInfo += T.getTriple();
58460b57cec5SDimitry Andric     }
58470b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(TargetInfo.str()));
58480b57cec5SDimitry Andric   }
58490b57cec5SDimitry Andric 
5850a7dea167SDimitry Andric   bool VirtualFunctionElimination =
5851a7dea167SDimitry Andric       Args.hasFlag(options::OPT_fvirtual_function_elimination,
5852a7dea167SDimitry Andric                    options::OPT_fno_virtual_function_elimination, false);
5853a7dea167SDimitry Andric   if (VirtualFunctionElimination) {
5854a7dea167SDimitry Andric     // VFE requires full LTO (currently, this might be relaxed to allow ThinLTO
5855a7dea167SDimitry Andric     // in the future).
5856a7dea167SDimitry Andric     if (D.getLTOMode() != LTOK_Full)
5857a7dea167SDimitry Andric       D.Diag(diag::err_drv_argument_only_allowed_with)
5858a7dea167SDimitry Andric           << "-fvirtual-function-elimination"
5859a7dea167SDimitry Andric           << "-flto=full";
5860a7dea167SDimitry Andric 
5861a7dea167SDimitry Andric     CmdArgs.push_back("-fvirtual-function-elimination");
5862a7dea167SDimitry Andric   }
5863a7dea167SDimitry Andric 
5864a7dea167SDimitry Andric   // VFE requires whole-program-vtables, and enables it by default.
5865a7dea167SDimitry Andric   bool WholeProgramVTables = Args.hasFlag(
5866a7dea167SDimitry Andric       options::OPT_fwhole_program_vtables,
5867a7dea167SDimitry Andric       options::OPT_fno_whole_program_vtables, VirtualFunctionElimination);
5868a7dea167SDimitry Andric   if (VirtualFunctionElimination && !WholeProgramVTables) {
5869a7dea167SDimitry Andric     D.Diag(diag::err_drv_argument_not_allowed_with)
5870a7dea167SDimitry Andric         << "-fno-whole-program-vtables"
5871a7dea167SDimitry Andric         << "-fvirtual-function-elimination";
5872a7dea167SDimitry Andric   }
5873a7dea167SDimitry Andric 
58740b57cec5SDimitry Andric   if (WholeProgramVTables) {
58750b57cec5SDimitry Andric     if (!D.isUsingLTO())
58760b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_only_allowed_with)
58770b57cec5SDimitry Andric           << "-fwhole-program-vtables"
58780b57cec5SDimitry Andric           << "-flto";
58790b57cec5SDimitry Andric     CmdArgs.push_back("-fwhole-program-vtables");
58800b57cec5SDimitry Andric   }
58810b57cec5SDimitry Andric 
5882*480093f4SDimitry Andric   bool DefaultsSplitLTOUnit =
5883*480093f4SDimitry Andric       (WholeProgramVTables || Sanitize.needsLTO()) &&
5884*480093f4SDimitry Andric       (D.getLTOMode() == LTOK_Full || TC.canSplitThinLTOUnit());
58850b57cec5SDimitry Andric   bool SplitLTOUnit =
58860b57cec5SDimitry Andric       Args.hasFlag(options::OPT_fsplit_lto_unit,
5887a7dea167SDimitry Andric                    options::OPT_fno_split_lto_unit, DefaultsSplitLTOUnit);
5888a7dea167SDimitry Andric   if (Sanitize.needsLTO() && !SplitLTOUnit)
5889a7dea167SDimitry Andric     D.Diag(diag::err_drv_argument_not_allowed_with) << "-fno-split-lto-unit"
5890a7dea167SDimitry Andric                                                     << "-fsanitize=cfi";
58910b57cec5SDimitry Andric   if (SplitLTOUnit)
58920b57cec5SDimitry Andric     CmdArgs.push_back("-fsplit-lto-unit");
58930b57cec5SDimitry Andric 
58940b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fexperimental_isel,
58950b57cec5SDimitry Andric                                options::OPT_fno_experimental_isel)) {
58960b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
58970b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fexperimental_isel)) {
58980b57cec5SDimitry Andric       CmdArgs.push_back("-global-isel=1");
58990b57cec5SDimitry Andric 
59000b57cec5SDimitry Andric       // GISel is on by default on AArch64 -O0, so don't bother adding
59010b57cec5SDimitry Andric       // the fallback remarks for it. Other combinations will add a warning of
59020b57cec5SDimitry Andric       // some kind.
59030b57cec5SDimitry Andric       bool IsArchSupported = Triple.getArch() == llvm::Triple::aarch64;
59040b57cec5SDimitry Andric       bool IsOptLevelSupported = false;
59050b57cec5SDimitry Andric 
59060b57cec5SDimitry Andric       Arg *A = Args.getLastArg(options::OPT_O_Group);
59070b57cec5SDimitry Andric       if (Triple.getArch() == llvm::Triple::aarch64) {
59080b57cec5SDimitry Andric         if (!A || A->getOption().matches(options::OPT_O0))
59090b57cec5SDimitry Andric           IsOptLevelSupported = true;
59100b57cec5SDimitry Andric       }
59110b57cec5SDimitry Andric       if (!IsArchSupported || !IsOptLevelSupported) {
59120b57cec5SDimitry Andric         CmdArgs.push_back("-mllvm");
59130b57cec5SDimitry Andric         CmdArgs.push_back("-global-isel-abort=2");
59140b57cec5SDimitry Andric 
59150b57cec5SDimitry Andric         if (!IsArchSupported)
59160b57cec5SDimitry Andric           D.Diag(diag::warn_drv_experimental_isel_incomplete) << Triple.getArchName();
59170b57cec5SDimitry Andric         else
59180b57cec5SDimitry Andric           D.Diag(diag::warn_drv_experimental_isel_incomplete_opt);
59190b57cec5SDimitry Andric       }
59200b57cec5SDimitry Andric     } else {
59210b57cec5SDimitry Andric       CmdArgs.push_back("-global-isel=0");
59220b57cec5SDimitry Andric     }
59230b57cec5SDimitry Andric   }
59240b57cec5SDimitry Andric 
59250b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_forder_file_instrumentation)) {
59260b57cec5SDimitry Andric      CmdArgs.push_back("-forder-file-instrumentation");
59270b57cec5SDimitry Andric      // Enable order file instrumentation when ThinLTO is not on. When ThinLTO is
59280b57cec5SDimitry Andric      // on, we need to pass these flags as linker flags and that will be handled
59290b57cec5SDimitry Andric      // outside of the compiler.
59300b57cec5SDimitry Andric      if (!D.isUsingLTO()) {
59310b57cec5SDimitry Andric        CmdArgs.push_back("-mllvm");
59320b57cec5SDimitry Andric        CmdArgs.push_back("-enable-order-file-instrumentation");
59330b57cec5SDimitry Andric      }
59340b57cec5SDimitry Andric   }
59350b57cec5SDimitry Andric 
59360b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fforce_enable_int128,
59370b57cec5SDimitry Andric                                options::OPT_fno_force_enable_int128)) {
59380b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fforce_enable_int128))
59390b57cec5SDimitry Andric       CmdArgs.push_back("-fforce-enable-int128");
59400b57cec5SDimitry Andric   }
59410b57cec5SDimitry Andric 
59420b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fcomplete_member_pointers,
59430b57cec5SDimitry Andric                    options::OPT_fno_complete_member_pointers, false))
59440b57cec5SDimitry Andric     CmdArgs.push_back("-fcomplete-member-pointers");
59450b57cec5SDimitry Andric 
59460b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fcxx_static_destructors,
59470b57cec5SDimitry Andric                     options::OPT_fno_cxx_static_destructors, true))
59480b57cec5SDimitry Andric     CmdArgs.push_back("-fno-c++-static-destructors");
59490b57cec5SDimitry Andric 
59500b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_moutline,
59510b57cec5SDimitry Andric                                options::OPT_mno_outline)) {
59520b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_moutline)) {
59530b57cec5SDimitry Andric       // We only support -moutline in AArch64 right now. If we're not compiling
59540b57cec5SDimitry Andric       // for AArch64, emit a warning and ignore the flag. Otherwise, add the
59550b57cec5SDimitry Andric       // proper mllvm flags.
5956*480093f4SDimitry Andric       if (Triple.getArch() != llvm::Triple::aarch64 &&
5957*480093f4SDimitry Andric           Triple.getArch() != llvm::Triple::aarch64_32) {
59580b57cec5SDimitry Andric         D.Diag(diag::warn_drv_moutline_unsupported_opt) << Triple.getArchName();
59590b57cec5SDimitry Andric       } else {
59600b57cec5SDimitry Andric         CmdArgs.push_back("-mllvm");
59610b57cec5SDimitry Andric         CmdArgs.push_back("-enable-machine-outliner");
59620b57cec5SDimitry Andric       }
59630b57cec5SDimitry Andric     } else {
59640b57cec5SDimitry Andric       // Disable all outlining behaviour.
59650b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
59660b57cec5SDimitry Andric       CmdArgs.push_back("-enable-machine-outliner=never");
59670b57cec5SDimitry Andric     }
59680b57cec5SDimitry Andric   }
59690b57cec5SDimitry Andric 
59700b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_faddrsig, options::OPT_fno_addrsig,
59710b57cec5SDimitry Andric                    (TC.getTriple().isOSBinFormatELF() ||
59720b57cec5SDimitry Andric                     TC.getTriple().isOSBinFormatCOFF()) &&
59730b57cec5SDimitry Andric                       !TC.getTriple().isPS4() &&
59740b57cec5SDimitry Andric                       !TC.getTriple().isOSNetBSD() &&
5975*480093f4SDimitry Andric                       !Distro(D.getVFS(), TC.getTriple()).IsGentoo() &&
59760b57cec5SDimitry Andric                       !TC.getTriple().isAndroid() &&
59770b57cec5SDimitry Andric                        TC.useIntegratedAs()))
59780b57cec5SDimitry Andric     CmdArgs.push_back("-faddrsig");
59790b57cec5SDimitry Andric 
59800b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fsymbol_partition_EQ)) {
59810b57cec5SDimitry Andric     std::string Str = A->getAsString(Args);
59820b57cec5SDimitry Andric     if (!TC.getTriple().isOSBinFormatELF())
59830b57cec5SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
59840b57cec5SDimitry Andric           << Str << TC.getTripleString();
59850b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Str));
59860b57cec5SDimitry Andric   }
59870b57cec5SDimitry Andric 
59880b57cec5SDimitry Andric   // Add the "-o out -x type src.c" flags last. This is done primarily to make
59890b57cec5SDimitry Andric   // the -cc1 command easier to edit when reproducing compiler crashes.
59900b57cec5SDimitry Andric   if (Output.getType() == types::TY_Dependencies) {
59910b57cec5SDimitry Andric     // Handled with other dependency code.
59920b57cec5SDimitry Andric   } else if (Output.isFilename()) {
5993*480093f4SDimitry Andric     if (Output.getType() == clang::driver::types::TY_IFS_CPP ||
5994*480093f4SDimitry Andric         Output.getType() == clang::driver::types::TY_IFS) {
5995*480093f4SDimitry Andric       SmallString<128> OutputFilename(Output.getFilename());
5996*480093f4SDimitry Andric       llvm::sys::path::replace_extension(OutputFilename, "ifs");
5997*480093f4SDimitry Andric       CmdArgs.push_back("-o");
5998*480093f4SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(OutputFilename));
5999*480093f4SDimitry Andric     } else {
60000b57cec5SDimitry Andric       CmdArgs.push_back("-o");
60010b57cec5SDimitry Andric       CmdArgs.push_back(Output.getFilename());
6002*480093f4SDimitry Andric     }
60030b57cec5SDimitry Andric   } else {
60040b57cec5SDimitry Andric     assert(Output.isNothing() && "Invalid output.");
60050b57cec5SDimitry Andric   }
60060b57cec5SDimitry Andric 
60070b57cec5SDimitry Andric   addDashXForInput(Args, Input, CmdArgs);
60080b57cec5SDimitry Andric 
60090b57cec5SDimitry Andric   ArrayRef<InputInfo> FrontendInputs = Input;
60100b57cec5SDimitry Andric   if (IsHeaderModulePrecompile)
60110b57cec5SDimitry Andric     FrontendInputs = ModuleHeaderInputs;
60120b57cec5SDimitry Andric   else if (Input.isNothing())
60130b57cec5SDimitry Andric     FrontendInputs = {};
60140b57cec5SDimitry Andric 
60150b57cec5SDimitry Andric   for (const InputInfo &Input : FrontendInputs) {
60160b57cec5SDimitry Andric     if (Input.isFilename())
60170b57cec5SDimitry Andric       CmdArgs.push_back(Input.getFilename());
60180b57cec5SDimitry Andric     else
60190b57cec5SDimitry Andric       Input.getInputArg().renderAsInput(Args, CmdArgs);
60200b57cec5SDimitry Andric   }
60210b57cec5SDimitry Andric 
60220b57cec5SDimitry Andric   // Finally add the compile command to the compilation.
60230b57cec5SDimitry Andric   if (Args.hasArg(options::OPT__SLASH_fallback) &&
60240b57cec5SDimitry Andric       Output.getType() == types::TY_Object &&
60250b57cec5SDimitry Andric       (InputType == types::TY_C || InputType == types::TY_CXX)) {
60260b57cec5SDimitry Andric     auto CLCommand =
60270b57cec5SDimitry Andric         getCLFallback()->GetCommand(C, JA, Output, Inputs, Args, LinkingOutput);
6028a7dea167SDimitry Andric     C.addCommand(std::make_unique<FallbackCommand>(
60290b57cec5SDimitry Andric         JA, *this, Exec, CmdArgs, Inputs, std::move(CLCommand)));
60300b57cec5SDimitry Andric   } else if (Args.hasArg(options::OPT__SLASH_fallback) &&
60310b57cec5SDimitry Andric              isa<PrecompileJobAction>(JA)) {
60320b57cec5SDimitry Andric     // In /fallback builds, run the main compilation even if the pch generation
60330b57cec5SDimitry Andric     // fails, so that the main compilation's fallback to cl.exe runs.
6034a7dea167SDimitry Andric     C.addCommand(std::make_unique<ForceSuccessCommand>(JA, *this, Exec,
60350b57cec5SDimitry Andric                                                         CmdArgs, Inputs));
6036*480093f4SDimitry Andric   } else if (D.CC1Main && !D.CCGenDiagnostics) {
6037*480093f4SDimitry Andric     // Invoke the CC1 directly in this process
6038*480093f4SDimitry Andric     C.addCommand(
6039*480093f4SDimitry Andric         std::make_unique<CC1Command>(JA, *this, Exec, CmdArgs, Inputs));
60400b57cec5SDimitry Andric   } else {
6041a7dea167SDimitry Andric     C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
60420b57cec5SDimitry Andric   }
60430b57cec5SDimitry Andric 
60440b57cec5SDimitry Andric   // Make the compile command echo its inputs for /showFilenames.
60450b57cec5SDimitry Andric   if (Output.getType() == types::TY_Object &&
60460b57cec5SDimitry Andric       Args.hasFlag(options::OPT__SLASH_showFilenames,
60470b57cec5SDimitry Andric                    options::OPT__SLASH_showFilenames_, false)) {
60480b57cec5SDimitry Andric     C.getJobs().getJobs().back()->setPrintInputFilenames(true);
60490b57cec5SDimitry Andric   }
60500b57cec5SDimitry Andric 
60510b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_pg))
6052a7dea167SDimitry Andric     if (FPKeepKind == CodeGenOptions::FramePointerKind::None)
60530b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_not_allowed_with) << "-fomit-frame-pointer"
60540b57cec5SDimitry Andric                                                       << A->getAsString(Args);
60550b57cec5SDimitry Andric 
60560b57cec5SDimitry Andric   // Claim some arguments which clang supports automatically.
60570b57cec5SDimitry Andric 
60580b57cec5SDimitry Andric   // -fpch-preprocess is used with gcc to add a special marker in the output to
60590b57cec5SDimitry Andric   // include the PCH file.
60600b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_fpch_preprocess);
60610b57cec5SDimitry Andric 
60620b57cec5SDimitry Andric   // Claim some arguments which clang doesn't support, but we don't
60630b57cec5SDimitry Andric   // care to warn the user about.
60640b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
60650b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
60660b57cec5SDimitry Andric 
60670b57cec5SDimitry Andric   // Disable warnings for clang -E -emit-llvm foo.c
60680b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_emit_llvm);
60690b57cec5SDimitry Andric }
60700b57cec5SDimitry Andric 
60710b57cec5SDimitry Andric Clang::Clang(const ToolChain &TC)
60720b57cec5SDimitry Andric     // CAUTION! The first constructor argument ("clang") is not arbitrary,
60730b57cec5SDimitry Andric     // as it is for other tools. Some operations on a Tool actually test
60740b57cec5SDimitry Andric     // whether that tool is Clang based on the Tool's Name as a string.
60750b57cec5SDimitry Andric     : Tool("clang", "clang frontend", TC, RF_Full) {}
60760b57cec5SDimitry Andric 
60770b57cec5SDimitry Andric Clang::~Clang() {}
60780b57cec5SDimitry Andric 
60790b57cec5SDimitry Andric /// Add options related to the Objective-C runtime/ABI.
60800b57cec5SDimitry Andric ///
60810b57cec5SDimitry Andric /// Returns true if the runtime is non-fragile.
60820b57cec5SDimitry Andric ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
60830b57cec5SDimitry Andric                                       ArgStringList &cmdArgs,
60840b57cec5SDimitry Andric                                       RewriteKind rewriteKind) const {
60850b57cec5SDimitry Andric   // Look for the controlling runtime option.
60860b57cec5SDimitry Andric   Arg *runtimeArg =
60870b57cec5SDimitry Andric       args.getLastArg(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
60880b57cec5SDimitry Andric                       options::OPT_fobjc_runtime_EQ);
60890b57cec5SDimitry Andric 
60900b57cec5SDimitry Andric   // Just forward -fobjc-runtime= to the frontend.  This supercedes
60910b57cec5SDimitry Andric   // options about fragility.
60920b57cec5SDimitry Andric   if (runtimeArg &&
60930b57cec5SDimitry Andric       runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
60940b57cec5SDimitry Andric     ObjCRuntime runtime;
60950b57cec5SDimitry Andric     StringRef value = runtimeArg->getValue();
60960b57cec5SDimitry Andric     if (runtime.tryParse(value)) {
60970b57cec5SDimitry Andric       getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
60980b57cec5SDimitry Andric           << value;
60990b57cec5SDimitry Andric     }
61000b57cec5SDimitry Andric     if ((runtime.getKind() == ObjCRuntime::GNUstep) &&
61010b57cec5SDimitry Andric         (runtime.getVersion() >= VersionTuple(2, 0)))
61020b57cec5SDimitry Andric       if (!getToolChain().getTriple().isOSBinFormatELF() &&
61030b57cec5SDimitry Andric           !getToolChain().getTriple().isOSBinFormatCOFF()) {
61040b57cec5SDimitry Andric         getToolChain().getDriver().Diag(
61050b57cec5SDimitry Andric             diag::err_drv_gnustep_objc_runtime_incompatible_binary)
61060b57cec5SDimitry Andric           << runtime.getVersion().getMajor();
61070b57cec5SDimitry Andric       }
61080b57cec5SDimitry Andric 
61090b57cec5SDimitry Andric     runtimeArg->render(args, cmdArgs);
61100b57cec5SDimitry Andric     return runtime;
61110b57cec5SDimitry Andric   }
61120b57cec5SDimitry Andric 
61130b57cec5SDimitry Andric   // Otherwise, we'll need the ABI "version".  Version numbers are
61140b57cec5SDimitry Andric   // slightly confusing for historical reasons:
61150b57cec5SDimitry Andric   //   1 - Traditional "fragile" ABI
61160b57cec5SDimitry Andric   //   2 - Non-fragile ABI, version 1
61170b57cec5SDimitry Andric   //   3 - Non-fragile ABI, version 2
61180b57cec5SDimitry Andric   unsigned objcABIVersion = 1;
61190b57cec5SDimitry Andric   // If -fobjc-abi-version= is present, use that to set the version.
61200b57cec5SDimitry Andric   if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
61210b57cec5SDimitry Andric     StringRef value = abiArg->getValue();
61220b57cec5SDimitry Andric     if (value == "1")
61230b57cec5SDimitry Andric       objcABIVersion = 1;
61240b57cec5SDimitry Andric     else if (value == "2")
61250b57cec5SDimitry Andric       objcABIVersion = 2;
61260b57cec5SDimitry Andric     else if (value == "3")
61270b57cec5SDimitry Andric       objcABIVersion = 3;
61280b57cec5SDimitry Andric     else
61290b57cec5SDimitry Andric       getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) << value;
61300b57cec5SDimitry Andric   } else {
61310b57cec5SDimitry Andric     // Otherwise, determine if we are using the non-fragile ABI.
61320b57cec5SDimitry Andric     bool nonFragileABIIsDefault =
61330b57cec5SDimitry Andric         (rewriteKind == RK_NonFragile ||
61340b57cec5SDimitry Andric          (rewriteKind == RK_None &&
61350b57cec5SDimitry Andric           getToolChain().IsObjCNonFragileABIDefault()));
61360b57cec5SDimitry Andric     if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
61370b57cec5SDimitry Andric                      options::OPT_fno_objc_nonfragile_abi,
61380b57cec5SDimitry Andric                      nonFragileABIIsDefault)) {
61390b57cec5SDimitry Andric // Determine the non-fragile ABI version to use.
61400b57cec5SDimitry Andric #ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
61410b57cec5SDimitry Andric       unsigned nonFragileABIVersion = 1;
61420b57cec5SDimitry Andric #else
61430b57cec5SDimitry Andric       unsigned nonFragileABIVersion = 2;
61440b57cec5SDimitry Andric #endif
61450b57cec5SDimitry Andric 
61460b57cec5SDimitry Andric       if (Arg *abiArg =
61470b57cec5SDimitry Andric               args.getLastArg(options::OPT_fobjc_nonfragile_abi_version_EQ)) {
61480b57cec5SDimitry Andric         StringRef value = abiArg->getValue();
61490b57cec5SDimitry Andric         if (value == "1")
61500b57cec5SDimitry Andric           nonFragileABIVersion = 1;
61510b57cec5SDimitry Andric         else if (value == "2")
61520b57cec5SDimitry Andric           nonFragileABIVersion = 2;
61530b57cec5SDimitry Andric         else
61540b57cec5SDimitry Andric           getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
61550b57cec5SDimitry Andric               << value;
61560b57cec5SDimitry Andric       }
61570b57cec5SDimitry Andric 
61580b57cec5SDimitry Andric       objcABIVersion = 1 + nonFragileABIVersion;
61590b57cec5SDimitry Andric     } else {
61600b57cec5SDimitry Andric       objcABIVersion = 1;
61610b57cec5SDimitry Andric     }
61620b57cec5SDimitry Andric   }
61630b57cec5SDimitry Andric 
61640b57cec5SDimitry Andric   // We don't actually care about the ABI version other than whether
61650b57cec5SDimitry Andric   // it's non-fragile.
61660b57cec5SDimitry Andric   bool isNonFragile = objcABIVersion != 1;
61670b57cec5SDimitry Andric 
61680b57cec5SDimitry Andric   // If we have no runtime argument, ask the toolchain for its default runtime.
61690b57cec5SDimitry Andric   // However, the rewriter only really supports the Mac runtime, so assume that.
61700b57cec5SDimitry Andric   ObjCRuntime runtime;
61710b57cec5SDimitry Andric   if (!runtimeArg) {
61720b57cec5SDimitry Andric     switch (rewriteKind) {
61730b57cec5SDimitry Andric     case RK_None:
61740b57cec5SDimitry Andric       runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
61750b57cec5SDimitry Andric       break;
61760b57cec5SDimitry Andric     case RK_Fragile:
61770b57cec5SDimitry Andric       runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
61780b57cec5SDimitry Andric       break;
61790b57cec5SDimitry Andric     case RK_NonFragile:
61800b57cec5SDimitry Andric       runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
61810b57cec5SDimitry Andric       break;
61820b57cec5SDimitry Andric     }
61830b57cec5SDimitry Andric 
61840b57cec5SDimitry Andric     // -fnext-runtime
61850b57cec5SDimitry Andric   } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
61860b57cec5SDimitry Andric     // On Darwin, make this use the default behavior for the toolchain.
61870b57cec5SDimitry Andric     if (getToolChain().getTriple().isOSDarwin()) {
61880b57cec5SDimitry Andric       runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
61890b57cec5SDimitry Andric 
61900b57cec5SDimitry Andric       // Otherwise, build for a generic macosx port.
61910b57cec5SDimitry Andric     } else {
61920b57cec5SDimitry Andric       runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
61930b57cec5SDimitry Andric     }
61940b57cec5SDimitry Andric 
61950b57cec5SDimitry Andric     // -fgnu-runtime
61960b57cec5SDimitry Andric   } else {
61970b57cec5SDimitry Andric     assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
61980b57cec5SDimitry Andric     // Legacy behaviour is to target the gnustep runtime if we are in
61990b57cec5SDimitry Andric     // non-fragile mode or the GCC runtime in fragile mode.
62000b57cec5SDimitry Andric     if (isNonFragile)
62010b57cec5SDimitry Andric       runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(2, 0));
62020b57cec5SDimitry Andric     else
62030b57cec5SDimitry Andric       runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
62040b57cec5SDimitry Andric   }
62050b57cec5SDimitry Andric 
62060b57cec5SDimitry Andric   cmdArgs.push_back(
62070b57cec5SDimitry Andric       args.MakeArgString("-fobjc-runtime=" + runtime.getAsString()));
62080b57cec5SDimitry Andric   return runtime;
62090b57cec5SDimitry Andric }
62100b57cec5SDimitry Andric 
62110b57cec5SDimitry Andric static bool maybeConsumeDash(const std::string &EH, size_t &I) {
62120b57cec5SDimitry Andric   bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-');
62130b57cec5SDimitry Andric   I += HaveDash;
62140b57cec5SDimitry Andric   return !HaveDash;
62150b57cec5SDimitry Andric }
62160b57cec5SDimitry Andric 
62170b57cec5SDimitry Andric namespace {
62180b57cec5SDimitry Andric struct EHFlags {
62190b57cec5SDimitry Andric   bool Synch = false;
62200b57cec5SDimitry Andric   bool Asynch = false;
62210b57cec5SDimitry Andric   bool NoUnwindC = false;
62220b57cec5SDimitry Andric };
62230b57cec5SDimitry Andric } // end anonymous namespace
62240b57cec5SDimitry Andric 
62250b57cec5SDimitry Andric /// /EH controls whether to run destructor cleanups when exceptions are
62260b57cec5SDimitry Andric /// thrown.  There are three modifiers:
62270b57cec5SDimitry Andric /// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions.
62280b57cec5SDimitry Andric /// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions.
62290b57cec5SDimitry Andric ///      The 'a' modifier is unimplemented and fundamentally hard in LLVM IR.
62300b57cec5SDimitry Andric /// - c: Assume that extern "C" functions are implicitly nounwind.
62310b57cec5SDimitry Andric /// The default is /EHs-c-, meaning cleanups are disabled.
62320b57cec5SDimitry Andric static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) {
62330b57cec5SDimitry Andric   EHFlags EH;
62340b57cec5SDimitry Andric 
62350b57cec5SDimitry Andric   std::vector<std::string> EHArgs =
62360b57cec5SDimitry Andric       Args.getAllArgValues(options::OPT__SLASH_EH);
62370b57cec5SDimitry Andric   for (auto EHVal : EHArgs) {
62380b57cec5SDimitry Andric     for (size_t I = 0, E = EHVal.size(); I != E; ++I) {
62390b57cec5SDimitry Andric       switch (EHVal[I]) {
62400b57cec5SDimitry Andric       case 'a':
62410b57cec5SDimitry Andric         EH.Asynch = maybeConsumeDash(EHVal, I);
62420b57cec5SDimitry Andric         if (EH.Asynch)
62430b57cec5SDimitry Andric           EH.Synch = false;
62440b57cec5SDimitry Andric         continue;
62450b57cec5SDimitry Andric       case 'c':
62460b57cec5SDimitry Andric         EH.NoUnwindC = maybeConsumeDash(EHVal, I);
62470b57cec5SDimitry Andric         continue;
62480b57cec5SDimitry Andric       case 's':
62490b57cec5SDimitry Andric         EH.Synch = maybeConsumeDash(EHVal, I);
62500b57cec5SDimitry Andric         if (EH.Synch)
62510b57cec5SDimitry Andric           EH.Asynch = false;
62520b57cec5SDimitry Andric         continue;
62530b57cec5SDimitry Andric       default:
62540b57cec5SDimitry Andric         break;
62550b57cec5SDimitry Andric       }
62560b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_invalid_value) << "/EH" << EHVal;
62570b57cec5SDimitry Andric       break;
62580b57cec5SDimitry Andric     }
62590b57cec5SDimitry Andric   }
62600b57cec5SDimitry Andric   // The /GX, /GX- flags are only processed if there are not /EH flags.
62610b57cec5SDimitry Andric   // The default is that /GX is not specified.
62620b57cec5SDimitry Andric   if (EHArgs.empty() &&
62630b57cec5SDimitry Andric       Args.hasFlag(options::OPT__SLASH_GX, options::OPT__SLASH_GX_,
62640b57cec5SDimitry Andric                    /*Default=*/false)) {
62650b57cec5SDimitry Andric     EH.Synch = true;
62660b57cec5SDimitry Andric     EH.NoUnwindC = true;
62670b57cec5SDimitry Andric   }
62680b57cec5SDimitry Andric 
62690b57cec5SDimitry Andric   return EH;
62700b57cec5SDimitry Andric }
62710b57cec5SDimitry Andric 
62720b57cec5SDimitry Andric void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
62730b57cec5SDimitry Andric                            ArgStringList &CmdArgs,
62740b57cec5SDimitry Andric                            codegenoptions::DebugInfoKind *DebugInfoKind,
62750b57cec5SDimitry Andric                            bool *EmitCodeView) const {
62760b57cec5SDimitry Andric   unsigned RTOptionID = options::OPT__SLASH_MT;
62770b57cec5SDimitry Andric 
62780b57cec5SDimitry Andric   if (Args.hasArg(options::OPT__SLASH_LDd))
62790b57cec5SDimitry Andric     // The /LDd option implies /MTd. The dependent lib part can be overridden,
62800b57cec5SDimitry Andric     // but defining _DEBUG is sticky.
62810b57cec5SDimitry Andric     RTOptionID = options::OPT__SLASH_MTd;
62820b57cec5SDimitry Andric 
62830b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group))
62840b57cec5SDimitry Andric     RTOptionID = A->getOption().getID();
62850b57cec5SDimitry Andric 
62860b57cec5SDimitry Andric   StringRef FlagForCRT;
62870b57cec5SDimitry Andric   switch (RTOptionID) {
62880b57cec5SDimitry Andric   case options::OPT__SLASH_MD:
62890b57cec5SDimitry Andric     if (Args.hasArg(options::OPT__SLASH_LDd))
62900b57cec5SDimitry Andric       CmdArgs.push_back("-D_DEBUG");
62910b57cec5SDimitry Andric     CmdArgs.push_back("-D_MT");
62920b57cec5SDimitry Andric     CmdArgs.push_back("-D_DLL");
62930b57cec5SDimitry Andric     FlagForCRT = "--dependent-lib=msvcrt";
62940b57cec5SDimitry Andric     break;
62950b57cec5SDimitry Andric   case options::OPT__SLASH_MDd:
62960b57cec5SDimitry Andric     CmdArgs.push_back("-D_DEBUG");
62970b57cec5SDimitry Andric     CmdArgs.push_back("-D_MT");
62980b57cec5SDimitry Andric     CmdArgs.push_back("-D_DLL");
62990b57cec5SDimitry Andric     FlagForCRT = "--dependent-lib=msvcrtd";
63000b57cec5SDimitry Andric     break;
63010b57cec5SDimitry Andric   case options::OPT__SLASH_MT:
63020b57cec5SDimitry Andric     if (Args.hasArg(options::OPT__SLASH_LDd))
63030b57cec5SDimitry Andric       CmdArgs.push_back("-D_DEBUG");
63040b57cec5SDimitry Andric     CmdArgs.push_back("-D_MT");
63050b57cec5SDimitry Andric     CmdArgs.push_back("-flto-visibility-public-std");
63060b57cec5SDimitry Andric     FlagForCRT = "--dependent-lib=libcmt";
63070b57cec5SDimitry Andric     break;
63080b57cec5SDimitry Andric   case options::OPT__SLASH_MTd:
63090b57cec5SDimitry Andric     CmdArgs.push_back("-D_DEBUG");
63100b57cec5SDimitry Andric     CmdArgs.push_back("-D_MT");
63110b57cec5SDimitry Andric     CmdArgs.push_back("-flto-visibility-public-std");
63120b57cec5SDimitry Andric     FlagForCRT = "--dependent-lib=libcmtd";
63130b57cec5SDimitry Andric     break;
63140b57cec5SDimitry Andric   default:
63150b57cec5SDimitry Andric     llvm_unreachable("Unexpected option ID.");
63160b57cec5SDimitry Andric   }
63170b57cec5SDimitry Andric 
63180b57cec5SDimitry Andric   if (Args.hasArg(options::OPT__SLASH_Zl)) {
63190b57cec5SDimitry Andric     CmdArgs.push_back("-D_VC_NODEFAULTLIB");
63200b57cec5SDimitry Andric   } else {
63210b57cec5SDimitry Andric     CmdArgs.push_back(FlagForCRT.data());
63220b57cec5SDimitry Andric 
63230b57cec5SDimitry Andric     // This provides POSIX compatibility (maps 'open' to '_open'), which most
63240b57cec5SDimitry Andric     // users want.  The /Za flag to cl.exe turns this off, but it's not
63250b57cec5SDimitry Andric     // implemented in clang.
63260b57cec5SDimitry Andric     CmdArgs.push_back("--dependent-lib=oldnames");
63270b57cec5SDimitry Andric   }
63280b57cec5SDimitry Andric 
63290b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_show_includes);
63300b57cec5SDimitry Andric 
63310b57cec5SDimitry Andric   // This controls whether or not we emit RTTI data for polymorphic types.
63320b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,
63330b57cec5SDimitry Andric                    /*Default=*/false))
63340b57cec5SDimitry Andric     CmdArgs.push_back("-fno-rtti-data");
63350b57cec5SDimitry Andric 
63360b57cec5SDimitry Andric   // This controls whether or not we emit stack-protector instrumentation.
63370b57cec5SDimitry Andric   // In MSVC, Buffer Security Check (/GS) is on by default.
63380b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT__SLASH_GS, options::OPT__SLASH_GS_,
63390b57cec5SDimitry Andric                    /*Default=*/true)) {
63400b57cec5SDimitry Andric     CmdArgs.push_back("-stack-protector");
63410b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Twine(LangOptions::SSPStrong)));
63420b57cec5SDimitry Andric   }
63430b57cec5SDimitry Andric 
63440b57cec5SDimitry Andric   // Emit CodeView if -Z7, -Zd, or -gline-tables-only are present.
63450b57cec5SDimitry Andric   if (Arg *DebugInfoArg =
63460b57cec5SDimitry Andric           Args.getLastArg(options::OPT__SLASH_Z7, options::OPT__SLASH_Zd,
63470b57cec5SDimitry Andric                           options::OPT_gline_tables_only)) {
63480b57cec5SDimitry Andric     *EmitCodeView = true;
63490b57cec5SDimitry Andric     if (DebugInfoArg->getOption().matches(options::OPT__SLASH_Z7))
63500b57cec5SDimitry Andric       *DebugInfoKind = codegenoptions::LimitedDebugInfo;
63510b57cec5SDimitry Andric     else
63520b57cec5SDimitry Andric       *DebugInfoKind = codegenoptions::DebugLineTablesOnly;
63530b57cec5SDimitry Andric   } else {
63540b57cec5SDimitry Andric     *EmitCodeView = false;
63550b57cec5SDimitry Andric   }
63560b57cec5SDimitry Andric 
63570b57cec5SDimitry Andric   const Driver &D = getToolChain().getDriver();
63580b57cec5SDimitry Andric   EHFlags EH = parseClangCLEHFlags(D, Args);
63590b57cec5SDimitry Andric   if (EH.Synch || EH.Asynch) {
63600b57cec5SDimitry Andric     if (types::isCXX(InputType))
63610b57cec5SDimitry Andric       CmdArgs.push_back("-fcxx-exceptions");
63620b57cec5SDimitry Andric     CmdArgs.push_back("-fexceptions");
63630b57cec5SDimitry Andric   }
63640b57cec5SDimitry Andric   if (types::isCXX(InputType) && EH.Synch && EH.NoUnwindC)
63650b57cec5SDimitry Andric     CmdArgs.push_back("-fexternc-nounwind");
63660b57cec5SDimitry Andric 
63670b57cec5SDimitry Andric   // /EP should expand to -E -P.
63680b57cec5SDimitry Andric   if (Args.hasArg(options::OPT__SLASH_EP)) {
63690b57cec5SDimitry Andric     CmdArgs.push_back("-E");
63700b57cec5SDimitry Andric     CmdArgs.push_back("-P");
63710b57cec5SDimitry Andric   }
63720b57cec5SDimitry Andric 
63730b57cec5SDimitry Andric   unsigned VolatileOptionID;
6374*480093f4SDimitry Andric   if (getToolChain().getTriple().isX86())
63750b57cec5SDimitry Andric     VolatileOptionID = options::OPT__SLASH_volatile_ms;
63760b57cec5SDimitry Andric   else
63770b57cec5SDimitry Andric     VolatileOptionID = options::OPT__SLASH_volatile_iso;
63780b57cec5SDimitry Andric 
63790b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT__SLASH_volatile_Group))
63800b57cec5SDimitry Andric     VolatileOptionID = A->getOption().getID();
63810b57cec5SDimitry Andric 
63820b57cec5SDimitry Andric   if (VolatileOptionID == options::OPT__SLASH_volatile_ms)
63830b57cec5SDimitry Andric     CmdArgs.push_back("-fms-volatile");
63840b57cec5SDimitry Andric 
63850b57cec5SDimitry Andric  if (Args.hasFlag(options::OPT__SLASH_Zc_dllexportInlines_,
63860b57cec5SDimitry Andric                   options::OPT__SLASH_Zc_dllexportInlines,
63870b57cec5SDimitry Andric                   false)) {
63880b57cec5SDimitry Andric    if (Args.hasArg(options::OPT__SLASH_fallback)) {
63890b57cec5SDimitry Andric      D.Diag(clang::diag::err_drv_dllexport_inlines_and_fallback);
63900b57cec5SDimitry Andric    } else {
63910b57cec5SDimitry Andric     CmdArgs.push_back("-fno-dllexport-inlines");
63920b57cec5SDimitry Andric    }
63930b57cec5SDimitry Andric  }
63940b57cec5SDimitry Andric 
63950b57cec5SDimitry Andric   Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg);
63960b57cec5SDimitry Andric   Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb);
63970b57cec5SDimitry Andric   if (MostGeneralArg && BestCaseArg)
63980b57cec5SDimitry Andric     D.Diag(clang::diag::err_drv_argument_not_allowed_with)
63990b57cec5SDimitry Andric         << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args);
64000b57cec5SDimitry Andric 
64010b57cec5SDimitry Andric   if (MostGeneralArg) {
64020b57cec5SDimitry Andric     Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms);
64030b57cec5SDimitry Andric     Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm);
64040b57cec5SDimitry Andric     Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv);
64050b57cec5SDimitry Andric 
64060b57cec5SDimitry Andric     Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg;
64070b57cec5SDimitry Andric     Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg;
64080b57cec5SDimitry Andric     if (FirstConflict && SecondConflict && FirstConflict != SecondConflict)
64090b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_argument_not_allowed_with)
64100b57cec5SDimitry Andric           << FirstConflict->getAsString(Args)
64110b57cec5SDimitry Andric           << SecondConflict->getAsString(Args);
64120b57cec5SDimitry Andric 
64130b57cec5SDimitry Andric     if (SingleArg)
64140b57cec5SDimitry Andric       CmdArgs.push_back("-fms-memptr-rep=single");
64150b57cec5SDimitry Andric     else if (MultipleArg)
64160b57cec5SDimitry Andric       CmdArgs.push_back("-fms-memptr-rep=multiple");
64170b57cec5SDimitry Andric     else
64180b57cec5SDimitry Andric       CmdArgs.push_back("-fms-memptr-rep=virtual");
64190b57cec5SDimitry Andric   }
64200b57cec5SDimitry Andric 
64210b57cec5SDimitry Andric   // Parse the default calling convention options.
64220b57cec5SDimitry Andric   if (Arg *CCArg =
64230b57cec5SDimitry Andric           Args.getLastArg(options::OPT__SLASH_Gd, options::OPT__SLASH_Gr,
64240b57cec5SDimitry Andric                           options::OPT__SLASH_Gz, options::OPT__SLASH_Gv,
64250b57cec5SDimitry Andric                           options::OPT__SLASH_Gregcall)) {
64260b57cec5SDimitry Andric     unsigned DCCOptId = CCArg->getOption().getID();
64270b57cec5SDimitry Andric     const char *DCCFlag = nullptr;
64280b57cec5SDimitry Andric     bool ArchSupported = true;
64290b57cec5SDimitry Andric     llvm::Triple::ArchType Arch = getToolChain().getArch();
64300b57cec5SDimitry Andric     switch (DCCOptId) {
64310b57cec5SDimitry Andric     case options::OPT__SLASH_Gd:
64320b57cec5SDimitry Andric       DCCFlag = "-fdefault-calling-conv=cdecl";
64330b57cec5SDimitry Andric       break;
64340b57cec5SDimitry Andric     case options::OPT__SLASH_Gr:
64350b57cec5SDimitry Andric       ArchSupported = Arch == llvm::Triple::x86;
64360b57cec5SDimitry Andric       DCCFlag = "-fdefault-calling-conv=fastcall";
64370b57cec5SDimitry Andric       break;
64380b57cec5SDimitry Andric     case options::OPT__SLASH_Gz:
64390b57cec5SDimitry Andric       ArchSupported = Arch == llvm::Triple::x86;
64400b57cec5SDimitry Andric       DCCFlag = "-fdefault-calling-conv=stdcall";
64410b57cec5SDimitry Andric       break;
64420b57cec5SDimitry Andric     case options::OPT__SLASH_Gv:
64430b57cec5SDimitry Andric       ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64;
64440b57cec5SDimitry Andric       DCCFlag = "-fdefault-calling-conv=vectorcall";
64450b57cec5SDimitry Andric       break;
64460b57cec5SDimitry Andric     case options::OPT__SLASH_Gregcall:
64470b57cec5SDimitry Andric       ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64;
64480b57cec5SDimitry Andric       DCCFlag = "-fdefault-calling-conv=regcall";
64490b57cec5SDimitry Andric       break;
64500b57cec5SDimitry Andric     }
64510b57cec5SDimitry Andric 
64520b57cec5SDimitry Andric     // MSVC doesn't warn if /Gr or /Gz is used on x64, so we don't either.
64530b57cec5SDimitry Andric     if (ArchSupported && DCCFlag)
64540b57cec5SDimitry Andric       CmdArgs.push_back(DCCFlag);
64550b57cec5SDimitry Andric   }
64560b57cec5SDimitry Andric 
64570b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_vtordisp_mode_EQ);
64580b57cec5SDimitry Andric 
64590b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) {
64600b57cec5SDimitry Andric     CmdArgs.push_back("-fdiagnostics-format");
64610b57cec5SDimitry Andric     if (Args.hasArg(options::OPT__SLASH_fallback))
64620b57cec5SDimitry Andric       CmdArgs.push_back("msvc-fallback");
64630b57cec5SDimitry Andric     else
64640b57cec5SDimitry Andric       CmdArgs.push_back("msvc");
64650b57cec5SDimitry Andric   }
64660b57cec5SDimitry Andric 
64670b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT__SLASH_guard)) {
6468*480093f4SDimitry Andric     StringRef GuardArgs = A->getValue();
6469*480093f4SDimitry Andric     // The only valid options are "cf", "cf,nochecks", and "cf-".
6470*480093f4SDimitry Andric     if (GuardArgs.equals_lower("cf")) {
6471*480093f4SDimitry Andric       // Emit CFG instrumentation and the table of address-taken functions.
64720b57cec5SDimitry Andric       CmdArgs.push_back("-cfguard");
6473*480093f4SDimitry Andric     } else if (GuardArgs.equals_lower("cf,nochecks")) {
6474*480093f4SDimitry Andric       // Emit only the table of address-taken functions.
6475*480093f4SDimitry Andric       CmdArgs.push_back("-cfguard-no-checks");
6476*480093f4SDimitry Andric     } else if (GuardArgs.equals_lower("cf-")) {
6477*480093f4SDimitry Andric       // Do nothing, but we might want to emit a security warning in future.
6478*480093f4SDimitry Andric     } else {
6479*480093f4SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << GuardArgs;
6480*480093f4SDimitry Andric     }
64810b57cec5SDimitry Andric   }
64820b57cec5SDimitry Andric }
64830b57cec5SDimitry Andric 
64840b57cec5SDimitry Andric visualstudio::Compiler *Clang::getCLFallback() const {
64850b57cec5SDimitry Andric   if (!CLFallback)
64860b57cec5SDimitry Andric     CLFallback.reset(new visualstudio::Compiler(getToolChain()));
64870b57cec5SDimitry Andric   return CLFallback.get();
64880b57cec5SDimitry Andric }
64890b57cec5SDimitry Andric 
64900b57cec5SDimitry Andric 
64910b57cec5SDimitry Andric const char *Clang::getBaseInputName(const ArgList &Args,
64920b57cec5SDimitry Andric                                     const InputInfo &Input) {
64930b57cec5SDimitry Andric   return Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput()));
64940b57cec5SDimitry Andric }
64950b57cec5SDimitry Andric 
64960b57cec5SDimitry Andric const char *Clang::getBaseInputStem(const ArgList &Args,
64970b57cec5SDimitry Andric                                     const InputInfoList &Inputs) {
64980b57cec5SDimitry Andric   const char *Str = getBaseInputName(Args, Inputs[0]);
64990b57cec5SDimitry Andric 
65000b57cec5SDimitry Andric   if (const char *End = strrchr(Str, '.'))
65010b57cec5SDimitry Andric     return Args.MakeArgString(std::string(Str, End));
65020b57cec5SDimitry Andric 
65030b57cec5SDimitry Andric   return Str;
65040b57cec5SDimitry Andric }
65050b57cec5SDimitry Andric 
65060b57cec5SDimitry Andric const char *Clang::getDependencyFileName(const ArgList &Args,
65070b57cec5SDimitry Andric                                          const InputInfoList &Inputs) {
65080b57cec5SDimitry Andric   // FIXME: Think about this more.
65090b57cec5SDimitry Andric 
65100b57cec5SDimitry Andric   if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
6511a7dea167SDimitry Andric     SmallString<128> OutputFilename(OutputOpt->getValue());
6512a7dea167SDimitry Andric     llvm::sys::path::replace_extension(OutputFilename, llvm::Twine('d'));
6513a7dea167SDimitry Andric     return Args.MakeArgString(OutputFilename);
65140b57cec5SDimitry Andric   }
6515a7dea167SDimitry Andric 
6516a7dea167SDimitry Andric   return Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".d");
65170b57cec5SDimitry Andric }
65180b57cec5SDimitry Andric 
65190b57cec5SDimitry Andric // Begin ClangAs
65200b57cec5SDimitry Andric 
65210b57cec5SDimitry Andric void ClangAs::AddMIPSTargetArgs(const ArgList &Args,
65220b57cec5SDimitry Andric                                 ArgStringList &CmdArgs) const {
65230b57cec5SDimitry Andric   StringRef CPUName;
65240b57cec5SDimitry Andric   StringRef ABIName;
65250b57cec5SDimitry Andric   const llvm::Triple &Triple = getToolChain().getTriple();
65260b57cec5SDimitry Andric   mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
65270b57cec5SDimitry Andric 
65280b57cec5SDimitry Andric   CmdArgs.push_back("-target-abi");
65290b57cec5SDimitry Andric   CmdArgs.push_back(ABIName.data());
65300b57cec5SDimitry Andric }
65310b57cec5SDimitry Andric 
65320b57cec5SDimitry Andric void ClangAs::AddX86TargetArgs(const ArgList &Args,
65330b57cec5SDimitry Andric                                ArgStringList &CmdArgs) const {
6534*480093f4SDimitry Andric   addX86AlignBranchArgs(getToolChain().getDriver(), Args, CmdArgs);
6535*480093f4SDimitry Andric 
65360b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
65370b57cec5SDimitry Andric     StringRef Value = A->getValue();
65380b57cec5SDimitry Andric     if (Value == "intel" || Value == "att") {
65390b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
65400b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
65410b57cec5SDimitry Andric     } else {
65420b57cec5SDimitry Andric       getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
65430b57cec5SDimitry Andric           << A->getOption().getName() << Value;
65440b57cec5SDimitry Andric     }
65450b57cec5SDimitry Andric   }
65460b57cec5SDimitry Andric }
65470b57cec5SDimitry Andric 
65480b57cec5SDimitry Andric void ClangAs::AddRISCVTargetArgs(const ArgList &Args,
65490b57cec5SDimitry Andric                                ArgStringList &CmdArgs) const {
65500b57cec5SDimitry Andric   const llvm::Triple &Triple = getToolChain().getTriple();
65510b57cec5SDimitry Andric   StringRef ABIName = riscv::getRISCVABI(Args, Triple);
65520b57cec5SDimitry Andric 
65530b57cec5SDimitry Andric   CmdArgs.push_back("-target-abi");
65540b57cec5SDimitry Andric   CmdArgs.push_back(ABIName.data());
65550b57cec5SDimitry Andric }
65560b57cec5SDimitry Andric 
65570b57cec5SDimitry Andric void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
65580b57cec5SDimitry Andric                            const InputInfo &Output, const InputInfoList &Inputs,
65590b57cec5SDimitry Andric                            const ArgList &Args,
65600b57cec5SDimitry Andric                            const char *LinkingOutput) const {
65610b57cec5SDimitry Andric   ArgStringList CmdArgs;
65620b57cec5SDimitry Andric 
65630b57cec5SDimitry Andric   assert(Inputs.size() == 1 && "Unexpected number of inputs.");
65640b57cec5SDimitry Andric   const InputInfo &Input = Inputs[0];
65650b57cec5SDimitry Andric 
65660b57cec5SDimitry Andric   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
65670b57cec5SDimitry Andric   const std::string &TripleStr = Triple.getTriple();
65680b57cec5SDimitry Andric   const auto &D = getToolChain().getDriver();
65690b57cec5SDimitry Andric 
65700b57cec5SDimitry Andric   // Don't warn about "clang -w -c foo.s"
65710b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_w);
65720b57cec5SDimitry Andric   // and "clang -emit-llvm -c foo.s"
65730b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_emit_llvm);
65740b57cec5SDimitry Andric 
65750b57cec5SDimitry Andric   claimNoWarnArgs(Args);
65760b57cec5SDimitry Andric 
65770b57cec5SDimitry Andric   // Invoke ourselves in -cc1as mode.
65780b57cec5SDimitry Andric   //
65790b57cec5SDimitry Andric   // FIXME: Implement custom jobs for internal actions.
65800b57cec5SDimitry Andric   CmdArgs.push_back("-cc1as");
65810b57cec5SDimitry Andric 
65820b57cec5SDimitry Andric   // Add the "effective" target triple.
65830b57cec5SDimitry Andric   CmdArgs.push_back("-triple");
65840b57cec5SDimitry Andric   CmdArgs.push_back(Args.MakeArgString(TripleStr));
65850b57cec5SDimitry Andric 
65860b57cec5SDimitry Andric   // Set the output mode, we currently only expect to be used as a real
65870b57cec5SDimitry Andric   // assembler.
65880b57cec5SDimitry Andric   CmdArgs.push_back("-filetype");
65890b57cec5SDimitry Andric   CmdArgs.push_back("obj");
65900b57cec5SDimitry Andric 
65910b57cec5SDimitry Andric   // Set the main file name, so that debug info works even with
65920b57cec5SDimitry Andric   // -save-temps or preprocessed assembly.
65930b57cec5SDimitry Andric   CmdArgs.push_back("-main-file-name");
65940b57cec5SDimitry Andric   CmdArgs.push_back(Clang::getBaseInputName(Args, Input));
65950b57cec5SDimitry Andric 
65960b57cec5SDimitry Andric   // Add the target cpu
65970b57cec5SDimitry Andric   std::string CPU = getCPUName(Args, Triple, /*FromAs*/ true);
65980b57cec5SDimitry Andric   if (!CPU.empty()) {
65990b57cec5SDimitry Andric     CmdArgs.push_back("-target-cpu");
66000b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(CPU));
66010b57cec5SDimitry Andric   }
66020b57cec5SDimitry Andric 
66030b57cec5SDimitry Andric   // Add the target features
66040b57cec5SDimitry Andric   getTargetFeatures(getToolChain(), Triple, Args, CmdArgs, true);
66050b57cec5SDimitry Andric 
66060b57cec5SDimitry Andric   // Ignore explicit -force_cpusubtype_ALL option.
66070b57cec5SDimitry Andric   (void)Args.hasArg(options::OPT_force__cpusubtype__ALL);
66080b57cec5SDimitry Andric 
66090b57cec5SDimitry Andric   // Pass along any -I options so we get proper .include search paths.
66100b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_I_Group);
66110b57cec5SDimitry Andric 
66120b57cec5SDimitry Andric   // Determine the original source input.
66130b57cec5SDimitry Andric   const Action *SourceAction = &JA;
66140b57cec5SDimitry Andric   while (SourceAction->getKind() != Action::InputClass) {
66150b57cec5SDimitry Andric     assert(!SourceAction->getInputs().empty() && "unexpected root action!");
66160b57cec5SDimitry Andric     SourceAction = SourceAction->getInputs()[0];
66170b57cec5SDimitry Andric   }
66180b57cec5SDimitry Andric 
66190b57cec5SDimitry Andric   // Forward -g and handle debug info related flags, assuming we are dealing
66200b57cec5SDimitry Andric   // with an actual assembly file.
66210b57cec5SDimitry Andric   bool WantDebug = false;
66220b57cec5SDimitry Andric   unsigned DwarfVersion = 0;
66230b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_g_Group);
66240b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
66250b57cec5SDimitry Andric     WantDebug = !A->getOption().matches(options::OPT_g0) &&
66260b57cec5SDimitry Andric                 !A->getOption().matches(options::OPT_ggdb0);
66270b57cec5SDimitry Andric     if (WantDebug)
66280b57cec5SDimitry Andric       DwarfVersion = DwarfVersionNum(A->getSpelling());
66290b57cec5SDimitry Andric   }
6630*480093f4SDimitry Andric 
6631*480093f4SDimitry Andric   unsigned DefaultDwarfVersion = ParseDebugDefaultVersion(getToolChain(), Args);
6632*480093f4SDimitry Andric   if (DwarfVersion == 0)
6633*480093f4SDimitry Andric     DwarfVersion = DefaultDwarfVersion;
6634*480093f4SDimitry Andric 
66350b57cec5SDimitry Andric   if (DwarfVersion == 0)
66360b57cec5SDimitry Andric     DwarfVersion = getToolChain().GetDefaultDwarfVersion();
66370b57cec5SDimitry Andric 
66380b57cec5SDimitry Andric   codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo;
66390b57cec5SDimitry Andric 
66400b57cec5SDimitry Andric   if (SourceAction->getType() == types::TY_Asm ||
66410b57cec5SDimitry Andric       SourceAction->getType() == types::TY_PP_Asm) {
66420b57cec5SDimitry Andric     // You might think that it would be ok to set DebugInfoKind outside of
66430b57cec5SDimitry Andric     // the guard for source type, however there is a test which asserts
66440b57cec5SDimitry Andric     // that some assembler invocation receives no -debug-info-kind,
66450b57cec5SDimitry Andric     // and it's not clear whether that test is just overly restrictive.
66460b57cec5SDimitry Andric     DebugInfoKind = (WantDebug ? codegenoptions::LimitedDebugInfo
66470b57cec5SDimitry Andric                                : codegenoptions::NoDebugInfo);
66480b57cec5SDimitry Andric     // Add the -fdebug-compilation-dir flag if needed.
6649a7dea167SDimitry Andric     addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS());
66500b57cec5SDimitry Andric 
66510b57cec5SDimitry Andric     addDebugPrefixMapArg(getToolChain().getDriver(), Args, CmdArgs);
66520b57cec5SDimitry Andric 
66530b57cec5SDimitry Andric     // Set the AT_producer to the clang version when using the integrated
66540b57cec5SDimitry Andric     // assembler on assembly source files.
66550b57cec5SDimitry Andric     CmdArgs.push_back("-dwarf-debug-producer");
66560b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
66570b57cec5SDimitry Andric 
66580b57cec5SDimitry Andric     // And pass along -I options
66590b57cec5SDimitry Andric     Args.AddAllArgs(CmdArgs, options::OPT_I);
66600b57cec5SDimitry Andric   }
66610b57cec5SDimitry Andric   RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion,
66620b57cec5SDimitry Andric                           llvm::DebuggerKind::Default);
66630b57cec5SDimitry Andric   RenderDebugInfoCompressionArgs(Args, CmdArgs, D, getToolChain());
66640b57cec5SDimitry Andric 
66650b57cec5SDimitry Andric 
66660b57cec5SDimitry Andric   // Handle -fPIC et al -- the relocation-model affects the assembler
66670b57cec5SDimitry Andric   // for some targets.
66680b57cec5SDimitry Andric   llvm::Reloc::Model RelocationModel;
66690b57cec5SDimitry Andric   unsigned PICLevel;
66700b57cec5SDimitry Andric   bool IsPIE;
66710b57cec5SDimitry Andric   std::tie(RelocationModel, PICLevel, IsPIE) =
66720b57cec5SDimitry Andric       ParsePICArgs(getToolChain(), Args);
66730b57cec5SDimitry Andric 
66740b57cec5SDimitry Andric   const char *RMName = RelocationModelName(RelocationModel);
66750b57cec5SDimitry Andric   if (RMName) {
66760b57cec5SDimitry Andric     CmdArgs.push_back("-mrelocation-model");
66770b57cec5SDimitry Andric     CmdArgs.push_back(RMName);
66780b57cec5SDimitry Andric   }
66790b57cec5SDimitry Andric 
66800b57cec5SDimitry Andric   // Optionally embed the -cc1as level arguments into the debug info, for build
66810b57cec5SDimitry Andric   // analysis.
66820b57cec5SDimitry Andric   if (getToolChain().UseDwarfDebugFlags()) {
66830b57cec5SDimitry Andric     ArgStringList OriginalArgs;
66840b57cec5SDimitry Andric     for (const auto &Arg : Args)
66850b57cec5SDimitry Andric       Arg->render(Args, OriginalArgs);
66860b57cec5SDimitry Andric 
66870b57cec5SDimitry Andric     SmallString<256> Flags;
66880b57cec5SDimitry Andric     const char *Exec = getToolChain().getDriver().getClangProgramPath();
66890b57cec5SDimitry Andric     Flags += Exec;
66900b57cec5SDimitry Andric     for (const char *OriginalArg : OriginalArgs) {
66910b57cec5SDimitry Andric       SmallString<128> EscapedArg;
66920b57cec5SDimitry Andric       EscapeSpacesAndBackslashes(OriginalArg, EscapedArg);
66930b57cec5SDimitry Andric       Flags += " ";
66940b57cec5SDimitry Andric       Flags += EscapedArg;
66950b57cec5SDimitry Andric     }
66960b57cec5SDimitry Andric     CmdArgs.push_back("-dwarf-debug-flags");
66970b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Flags));
66980b57cec5SDimitry Andric   }
66990b57cec5SDimitry Andric 
67000b57cec5SDimitry Andric   // FIXME: Add -static support, once we have it.
67010b57cec5SDimitry Andric 
67020b57cec5SDimitry Andric   // Add target specific flags.
67030b57cec5SDimitry Andric   switch (getToolChain().getArch()) {
67040b57cec5SDimitry Andric   default:
67050b57cec5SDimitry Andric     break;
67060b57cec5SDimitry Andric 
67070b57cec5SDimitry Andric   case llvm::Triple::mips:
67080b57cec5SDimitry Andric   case llvm::Triple::mipsel:
67090b57cec5SDimitry Andric   case llvm::Triple::mips64:
67100b57cec5SDimitry Andric   case llvm::Triple::mips64el:
67110b57cec5SDimitry Andric     AddMIPSTargetArgs(Args, CmdArgs);
67120b57cec5SDimitry Andric     break;
67130b57cec5SDimitry Andric 
67140b57cec5SDimitry Andric   case llvm::Triple::x86:
67150b57cec5SDimitry Andric   case llvm::Triple::x86_64:
67160b57cec5SDimitry Andric     AddX86TargetArgs(Args, CmdArgs);
67170b57cec5SDimitry Andric     break;
67180b57cec5SDimitry Andric 
67190b57cec5SDimitry Andric   case llvm::Triple::arm:
67200b57cec5SDimitry Andric   case llvm::Triple::armeb:
67210b57cec5SDimitry Andric   case llvm::Triple::thumb:
67220b57cec5SDimitry Andric   case llvm::Triple::thumbeb:
67230b57cec5SDimitry Andric     // This isn't in AddARMTargetArgs because we want to do this for assembly
67240b57cec5SDimitry Andric     // only, not C/C++.
67250b57cec5SDimitry Andric     if (Args.hasFlag(options::OPT_mdefault_build_attributes,
67260b57cec5SDimitry Andric                      options::OPT_mno_default_build_attributes, true)) {
67270b57cec5SDimitry Andric         CmdArgs.push_back("-mllvm");
67280b57cec5SDimitry Andric         CmdArgs.push_back("-arm-add-build-attributes");
67290b57cec5SDimitry Andric     }
67300b57cec5SDimitry Andric     break;
67310b57cec5SDimitry Andric 
67320b57cec5SDimitry Andric   case llvm::Triple::riscv32:
67330b57cec5SDimitry Andric   case llvm::Triple::riscv64:
67340b57cec5SDimitry Andric     AddRISCVTargetArgs(Args, CmdArgs);
67350b57cec5SDimitry Andric     break;
67360b57cec5SDimitry Andric   }
67370b57cec5SDimitry Andric 
67380b57cec5SDimitry Andric   // Consume all the warning flags. Usually this would be handled more
67390b57cec5SDimitry Andric   // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as
67400b57cec5SDimitry Andric   // doesn't handle that so rather than warning about unused flags that are
67410b57cec5SDimitry Andric   // actually used, we'll lie by omission instead.
67420b57cec5SDimitry Andric   // FIXME: Stop lying and consume only the appropriate driver flags
67430b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_W_Group);
67440b57cec5SDimitry Andric 
67450b57cec5SDimitry Andric   CollectArgsForIntegratedAssembler(C, Args, CmdArgs,
67460b57cec5SDimitry Andric                                     getToolChain().getDriver());
67470b57cec5SDimitry Andric 
67480b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
67490b57cec5SDimitry Andric 
67500b57cec5SDimitry Andric   assert(Output.isFilename() && "Unexpected lipo output.");
67510b57cec5SDimitry Andric   CmdArgs.push_back("-o");
67520b57cec5SDimitry Andric   CmdArgs.push_back(Output.getFilename());
67530b57cec5SDimitry Andric 
67540b57cec5SDimitry Andric   const llvm::Triple &T = getToolChain().getTriple();
67550b57cec5SDimitry Andric   Arg *A;
67560b57cec5SDimitry Andric   if (getDebugFissionKind(D, Args, A) == DwarfFissionKind::Split &&
67570b57cec5SDimitry Andric       T.isOSBinFormatELF()) {
67580b57cec5SDimitry Andric     CmdArgs.push_back("-split-dwarf-output");
67590b57cec5SDimitry Andric     CmdArgs.push_back(SplitDebugName(Args, Input, Output));
67600b57cec5SDimitry Andric   }
67610b57cec5SDimitry Andric 
67620b57cec5SDimitry Andric   assert(Input.isFilename() && "Invalid input.");
67630b57cec5SDimitry Andric   CmdArgs.push_back(Input.getFilename());
67640b57cec5SDimitry Andric 
67650b57cec5SDimitry Andric   const char *Exec = getToolChain().getDriver().getClangProgramPath();
6766a7dea167SDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
67670b57cec5SDimitry Andric }
67680b57cec5SDimitry Andric 
67690b57cec5SDimitry Andric // Begin OffloadBundler
67700b57cec5SDimitry Andric 
67710b57cec5SDimitry Andric void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA,
67720b57cec5SDimitry Andric                                   const InputInfo &Output,
67730b57cec5SDimitry Andric                                   const InputInfoList &Inputs,
67740b57cec5SDimitry Andric                                   const llvm::opt::ArgList &TCArgs,
67750b57cec5SDimitry Andric                                   const char *LinkingOutput) const {
67760b57cec5SDimitry Andric   // The version with only one output is expected to refer to a bundling job.
67770b57cec5SDimitry Andric   assert(isa<OffloadBundlingJobAction>(JA) && "Expecting bundling job!");
67780b57cec5SDimitry Andric 
67790b57cec5SDimitry Andric   // The bundling command looks like this:
67800b57cec5SDimitry Andric   // clang-offload-bundler -type=bc
67810b57cec5SDimitry Andric   //   -targets=host-triple,openmp-triple1,openmp-triple2
67820b57cec5SDimitry Andric   //   -outputs=input_file
67830b57cec5SDimitry Andric   //   -inputs=unbundle_file_host,unbundle_file_tgt1,unbundle_file_tgt2"
67840b57cec5SDimitry Andric 
67850b57cec5SDimitry Andric   ArgStringList CmdArgs;
67860b57cec5SDimitry Andric 
67870b57cec5SDimitry Andric   // Get the type.
67880b57cec5SDimitry Andric   CmdArgs.push_back(TCArgs.MakeArgString(
67890b57cec5SDimitry Andric       Twine("-type=") + types::getTypeTempSuffix(Output.getType())));
67900b57cec5SDimitry Andric 
67910b57cec5SDimitry Andric   assert(JA.getInputs().size() == Inputs.size() &&
67920b57cec5SDimitry Andric          "Not have inputs for all dependence actions??");
67930b57cec5SDimitry Andric 
67940b57cec5SDimitry Andric   // Get the targets.
67950b57cec5SDimitry Andric   SmallString<128> Triples;
67960b57cec5SDimitry Andric   Triples += "-targets=";
67970b57cec5SDimitry Andric   for (unsigned I = 0; I < Inputs.size(); ++I) {
67980b57cec5SDimitry Andric     if (I)
67990b57cec5SDimitry Andric       Triples += ',';
68000b57cec5SDimitry Andric 
68010b57cec5SDimitry Andric     // Find ToolChain for this input.
68020b57cec5SDimitry Andric     Action::OffloadKind CurKind = Action::OFK_Host;
68030b57cec5SDimitry Andric     const ToolChain *CurTC = &getToolChain();
68040b57cec5SDimitry Andric     const Action *CurDep = JA.getInputs()[I];
68050b57cec5SDimitry Andric 
68060b57cec5SDimitry Andric     if (const auto *OA = dyn_cast<OffloadAction>(CurDep)) {
68070b57cec5SDimitry Andric       CurTC = nullptr;
68080b57cec5SDimitry Andric       OA->doOnEachDependence([&](Action *A, const ToolChain *TC, const char *) {
68090b57cec5SDimitry Andric         assert(CurTC == nullptr && "Expected one dependence!");
68100b57cec5SDimitry Andric         CurKind = A->getOffloadingDeviceKind();
68110b57cec5SDimitry Andric         CurTC = TC;
68120b57cec5SDimitry Andric       });
68130b57cec5SDimitry Andric     }
68140b57cec5SDimitry Andric     Triples += Action::GetOffloadKindName(CurKind);
68150b57cec5SDimitry Andric     Triples += '-';
68160b57cec5SDimitry Andric     Triples += CurTC->getTriple().normalize();
68170b57cec5SDimitry Andric     if (CurKind == Action::OFK_HIP && CurDep->getOffloadingArch()) {
68180b57cec5SDimitry Andric       Triples += '-';
68190b57cec5SDimitry Andric       Triples += CurDep->getOffloadingArch();
68200b57cec5SDimitry Andric     }
68210b57cec5SDimitry Andric   }
68220b57cec5SDimitry Andric   CmdArgs.push_back(TCArgs.MakeArgString(Triples));
68230b57cec5SDimitry Andric 
68240b57cec5SDimitry Andric   // Get bundled file command.
68250b57cec5SDimitry Andric   CmdArgs.push_back(
68260b57cec5SDimitry Andric       TCArgs.MakeArgString(Twine("-outputs=") + Output.getFilename()));
68270b57cec5SDimitry Andric 
68280b57cec5SDimitry Andric   // Get unbundled files command.
68290b57cec5SDimitry Andric   SmallString<128> UB;
68300b57cec5SDimitry Andric   UB += "-inputs=";
68310b57cec5SDimitry Andric   for (unsigned I = 0; I < Inputs.size(); ++I) {
68320b57cec5SDimitry Andric     if (I)
68330b57cec5SDimitry Andric       UB += ',';
68340b57cec5SDimitry Andric 
68350b57cec5SDimitry Andric     // Find ToolChain for this input.
68360b57cec5SDimitry Andric     const ToolChain *CurTC = &getToolChain();
68370b57cec5SDimitry Andric     if (const auto *OA = dyn_cast<OffloadAction>(JA.getInputs()[I])) {
68380b57cec5SDimitry Andric       CurTC = nullptr;
68390b57cec5SDimitry Andric       OA->doOnEachDependence([&](Action *, const ToolChain *TC, const char *) {
68400b57cec5SDimitry Andric         assert(CurTC == nullptr && "Expected one dependence!");
68410b57cec5SDimitry Andric         CurTC = TC;
68420b57cec5SDimitry Andric       });
68430b57cec5SDimitry Andric     }
68440b57cec5SDimitry Andric     UB += CurTC->getInputFilename(Inputs[I]);
68450b57cec5SDimitry Andric   }
68460b57cec5SDimitry Andric   CmdArgs.push_back(TCArgs.MakeArgString(UB));
68470b57cec5SDimitry Andric 
68480b57cec5SDimitry Andric   // All the inputs are encoded as commands.
6849a7dea167SDimitry Andric   C.addCommand(std::make_unique<Command>(
68500b57cec5SDimitry Andric       JA, *this,
68510b57cec5SDimitry Andric       TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
68520b57cec5SDimitry Andric       CmdArgs, None));
68530b57cec5SDimitry Andric }
68540b57cec5SDimitry Andric 
68550b57cec5SDimitry Andric void OffloadBundler::ConstructJobMultipleOutputs(
68560b57cec5SDimitry Andric     Compilation &C, const JobAction &JA, const InputInfoList &Outputs,
68570b57cec5SDimitry Andric     const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs,
68580b57cec5SDimitry Andric     const char *LinkingOutput) const {
68590b57cec5SDimitry Andric   // The version with multiple outputs is expected to refer to a unbundling job.
68600b57cec5SDimitry Andric   auto &UA = cast<OffloadUnbundlingJobAction>(JA);
68610b57cec5SDimitry Andric 
68620b57cec5SDimitry Andric   // The unbundling command looks like this:
68630b57cec5SDimitry Andric   // clang-offload-bundler -type=bc
68640b57cec5SDimitry Andric   //   -targets=host-triple,openmp-triple1,openmp-triple2
68650b57cec5SDimitry Andric   //   -inputs=input_file
68660b57cec5SDimitry Andric   //   -outputs=unbundle_file_host,unbundle_file_tgt1,unbundle_file_tgt2"
68670b57cec5SDimitry Andric   //   -unbundle
68680b57cec5SDimitry Andric 
68690b57cec5SDimitry Andric   ArgStringList CmdArgs;
68700b57cec5SDimitry Andric 
68710b57cec5SDimitry Andric   assert(Inputs.size() == 1 && "Expecting to unbundle a single file!");
68720b57cec5SDimitry Andric   InputInfo Input = Inputs.front();
68730b57cec5SDimitry Andric 
68740b57cec5SDimitry Andric   // Get the type.
68750b57cec5SDimitry Andric   CmdArgs.push_back(TCArgs.MakeArgString(
68760b57cec5SDimitry Andric       Twine("-type=") + types::getTypeTempSuffix(Input.getType())));
68770b57cec5SDimitry Andric 
68780b57cec5SDimitry Andric   // Get the targets.
68790b57cec5SDimitry Andric   SmallString<128> Triples;
68800b57cec5SDimitry Andric   Triples += "-targets=";
68810b57cec5SDimitry Andric   auto DepInfo = UA.getDependentActionsInfo();
68820b57cec5SDimitry Andric   for (unsigned I = 0; I < DepInfo.size(); ++I) {
68830b57cec5SDimitry Andric     if (I)
68840b57cec5SDimitry Andric       Triples += ',';
68850b57cec5SDimitry Andric 
68860b57cec5SDimitry Andric     auto &Dep = DepInfo[I];
68870b57cec5SDimitry Andric     Triples += Action::GetOffloadKindName(Dep.DependentOffloadKind);
68880b57cec5SDimitry Andric     Triples += '-';
68890b57cec5SDimitry Andric     Triples += Dep.DependentToolChain->getTriple().normalize();
68900b57cec5SDimitry Andric     if (Dep.DependentOffloadKind == Action::OFK_HIP &&
68910b57cec5SDimitry Andric         !Dep.DependentBoundArch.empty()) {
68920b57cec5SDimitry Andric       Triples += '-';
68930b57cec5SDimitry Andric       Triples += Dep.DependentBoundArch;
68940b57cec5SDimitry Andric     }
68950b57cec5SDimitry Andric   }
68960b57cec5SDimitry Andric 
68970b57cec5SDimitry Andric   CmdArgs.push_back(TCArgs.MakeArgString(Triples));
68980b57cec5SDimitry Andric 
68990b57cec5SDimitry Andric   // Get bundled file command.
69000b57cec5SDimitry Andric   CmdArgs.push_back(
69010b57cec5SDimitry Andric       TCArgs.MakeArgString(Twine("-inputs=") + Input.getFilename()));
69020b57cec5SDimitry Andric 
69030b57cec5SDimitry Andric   // Get unbundled files command.
69040b57cec5SDimitry Andric   SmallString<128> UB;
69050b57cec5SDimitry Andric   UB += "-outputs=";
69060b57cec5SDimitry Andric   for (unsigned I = 0; I < Outputs.size(); ++I) {
69070b57cec5SDimitry Andric     if (I)
69080b57cec5SDimitry Andric       UB += ',';
69090b57cec5SDimitry Andric     UB += DepInfo[I].DependentToolChain->getInputFilename(Outputs[I]);
69100b57cec5SDimitry Andric   }
69110b57cec5SDimitry Andric   CmdArgs.push_back(TCArgs.MakeArgString(UB));
69120b57cec5SDimitry Andric   CmdArgs.push_back("-unbundle");
69130b57cec5SDimitry Andric 
69140b57cec5SDimitry Andric   // All the inputs are encoded as commands.
6915a7dea167SDimitry Andric   C.addCommand(std::make_unique<Command>(
69160b57cec5SDimitry Andric       JA, *this,
69170b57cec5SDimitry Andric       TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
69180b57cec5SDimitry Andric       CmdArgs, None));
69190b57cec5SDimitry Andric }
6920a7dea167SDimitry Andric 
6921a7dea167SDimitry Andric void OffloadWrapper::ConstructJob(Compilation &C, const JobAction &JA,
6922a7dea167SDimitry Andric                                   const InputInfo &Output,
6923a7dea167SDimitry Andric                                   const InputInfoList &Inputs,
6924a7dea167SDimitry Andric                                   const ArgList &Args,
6925a7dea167SDimitry Andric                                   const char *LinkingOutput) const {
6926a7dea167SDimitry Andric   ArgStringList CmdArgs;
6927a7dea167SDimitry Andric 
6928a7dea167SDimitry Andric   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
6929a7dea167SDimitry Andric 
6930a7dea167SDimitry Andric   // Add the "effective" target triple.
6931a7dea167SDimitry Andric   CmdArgs.push_back("-target");
6932a7dea167SDimitry Andric   CmdArgs.push_back(Args.MakeArgString(Triple.getTriple()));
6933a7dea167SDimitry Andric 
6934a7dea167SDimitry Andric   // Add the output file name.
6935a7dea167SDimitry Andric   assert(Output.isFilename() && "Invalid output.");
6936a7dea167SDimitry Andric   CmdArgs.push_back("-o");
6937a7dea167SDimitry Andric   CmdArgs.push_back(Output.getFilename());
6938a7dea167SDimitry Andric 
6939a7dea167SDimitry Andric   // Add inputs.
6940a7dea167SDimitry Andric   for (const InputInfo &I : Inputs) {
6941a7dea167SDimitry Andric     assert(I.isFilename() && "Invalid input.");
6942a7dea167SDimitry Andric     CmdArgs.push_back(I.getFilename());
6943a7dea167SDimitry Andric   }
6944a7dea167SDimitry Andric 
6945a7dea167SDimitry Andric   C.addCommand(std::make_unique<Command>(
6946a7dea167SDimitry Andric       JA, *this,
6947a7dea167SDimitry Andric       Args.MakeArgString(getToolChain().GetProgramPath(getShortName())),
6948a7dea167SDimitry Andric       CmdArgs, Inputs));
6949a7dea167SDimitry Andric }
6950