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"
105ffd83dbSDimitry Andric #include "AMDGPU.h"
110b57cec5SDimitry Andric #include "Arch/AArch64.h"
120b57cec5SDimitry Andric #include "Arch/ARM.h"
13fe6060f1SDimitry Andric #include "Arch/M68k.h"
140b57cec5SDimitry Andric #include "Arch/Mips.h"
150b57cec5SDimitry Andric #include "Arch/PPC.h"
160b57cec5SDimitry Andric #include "Arch/RISCV.h"
170b57cec5SDimitry Andric #include "Arch/Sparc.h"
180b57cec5SDimitry Andric #include "Arch/SystemZ.h"
195ffd83dbSDimitry Andric #include "Arch/VE.h"
200b57cec5SDimitry Andric #include "Arch/X86.h"
210b57cec5SDimitry Andric #include "CommonArgs.h"
220b57cec5SDimitry Andric #include "Hexagon.h"
235ffd83dbSDimitry Andric #include "MSP430.h"
240b57cec5SDimitry Andric #include "PS4CPU.h"
250b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h"
26a7dea167SDimitry Andric #include "clang/Basic/CodeGenOptions.h"
270b57cec5SDimitry Andric #include "clang/Basic/LangOptions.h"
280b57cec5SDimitry Andric #include "clang/Basic/ObjCRuntime.h"
290b57cec5SDimitry Andric #include "clang/Basic/Version.h"
300b57cec5SDimitry Andric #include "clang/Driver/Distro.h"
310b57cec5SDimitry Andric #include "clang/Driver/DriverDiagnostic.h"
32fe6060f1SDimitry Andric #include "clang/Driver/InputInfo.h"
330b57cec5SDimitry Andric #include "clang/Driver/Options.h"
340b57cec5SDimitry Andric #include "clang/Driver/SanitizerArgs.h"
350b57cec5SDimitry Andric #include "clang/Driver/XRayArgs.h"
360b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
370b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
380b57cec5SDimitry Andric #include "llvm/Option/ArgList.h"
390b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
405ffd83dbSDimitry Andric #include "llvm/Support/Compiler.h"
410b57cec5SDimitry Andric #include "llvm/Support/Compression.h"
420b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
43e8d8bef9SDimitry Andric #include "llvm/Support/Host.h"
440b57cec5SDimitry Andric #include "llvm/Support/Path.h"
450b57cec5SDimitry Andric #include "llvm/Support/Process.h"
460b57cec5SDimitry Andric #include "llvm/Support/TargetParser.h"
470b57cec5SDimitry Andric #include "llvm/Support/YAMLParser.h"
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) {
55fe6060f1SDimitry Andric   if (Arg *A = Args.getLastArg(clang::driver::options::OPT_C, options::OPT_CC,
56fe6060f1SDimitry Andric                                options::OPT_fminimize_whitespace,
57fe6060f1SDimitry Andric                                options::OPT_fno_minimize_whitespace)) {
580b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_P) &&
590b57cec5SDimitry Andric         !Args.hasArg(options::OPT__SLASH_EP) && !D.CCCIsCPP()) {
600b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_argument_only_allowed_with)
610b57cec5SDimitry Andric           << A->getBaseArg().getAsString(Args)
620b57cec5SDimitry Andric           << (D.IsCLMode() ? "/E, /P or /EP" : "-E");
630b57cec5SDimitry Andric     }
640b57cec5SDimitry Andric   }
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
680b57cec5SDimitry Andric   // In gcc, only ARM checks this, but it seems reasonable to check universally.
690b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_static))
700b57cec5SDimitry Andric     if (const Arg *A =
710b57cec5SDimitry Andric             Args.getLastArg(options::OPT_dynamic, options::OPT_mdynamic_no_pic))
720b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args)
730b57cec5SDimitry Andric                                                       << "-static";
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric // Add backslashes to escape spaces and other backslashes.
770b57cec5SDimitry Andric // This is used for the space-separated argument list specified with
780b57cec5SDimitry Andric // the -dwarf-debug-flags option.
790b57cec5SDimitry Andric static void EscapeSpacesAndBackslashes(const char *Arg,
800b57cec5SDimitry Andric                                        SmallVectorImpl<char> &Res) {
810b57cec5SDimitry Andric   for (; *Arg; ++Arg) {
820b57cec5SDimitry Andric     switch (*Arg) {
830b57cec5SDimitry Andric     default:
840b57cec5SDimitry Andric       break;
850b57cec5SDimitry Andric     case ' ':
860b57cec5SDimitry Andric     case '\\':
870b57cec5SDimitry Andric       Res.push_back('\\');
880b57cec5SDimitry Andric       break;
890b57cec5SDimitry Andric     }
900b57cec5SDimitry Andric     Res.push_back(*Arg);
910b57cec5SDimitry Andric   }
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric // Quote target names for inclusion in GNU Make dependency files.
950b57cec5SDimitry Andric // Only the characters '$', '#', ' ', '\t' are quoted.
960b57cec5SDimitry Andric static void QuoteTarget(StringRef Target, SmallVectorImpl<char> &Res) {
970b57cec5SDimitry Andric   for (unsigned i = 0, e = Target.size(); i != e; ++i) {
980b57cec5SDimitry Andric     switch (Target[i]) {
990b57cec5SDimitry Andric     case ' ':
1000b57cec5SDimitry Andric     case '\t':
1010b57cec5SDimitry Andric       // Escape the preceding backslashes
1020b57cec5SDimitry Andric       for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
1030b57cec5SDimitry Andric         Res.push_back('\\');
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric       // Escape the space/tab
1060b57cec5SDimitry Andric       Res.push_back('\\');
1070b57cec5SDimitry Andric       break;
1080b57cec5SDimitry Andric     case '$':
1090b57cec5SDimitry Andric       Res.push_back('$');
1100b57cec5SDimitry Andric       break;
1110b57cec5SDimitry Andric     case '#':
1120b57cec5SDimitry Andric       Res.push_back('\\');
1130b57cec5SDimitry Andric       break;
1140b57cec5SDimitry Andric     default:
1150b57cec5SDimitry Andric       break;
1160b57cec5SDimitry Andric     }
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric     Res.push_back(Target[i]);
1190b57cec5SDimitry Andric   }
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric /// Apply \a Work on the current tool chain \a RegularToolChain and any other
1230b57cec5SDimitry Andric /// offloading tool chain that is associated with the current action \a JA.
1240b57cec5SDimitry Andric static void
1250b57cec5SDimitry Andric forAllAssociatedToolChains(Compilation &C, const JobAction &JA,
1260b57cec5SDimitry Andric                            const ToolChain &RegularToolChain,
1270b57cec5SDimitry Andric                            llvm::function_ref<void(const ToolChain &)> Work) {
1280b57cec5SDimitry Andric   // Apply Work on the current/regular tool chain.
1290b57cec5SDimitry Andric   Work(RegularToolChain);
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   // Apply Work on all the offloading tool chains associated with the current
1320b57cec5SDimitry Andric   // action.
1330b57cec5SDimitry Andric   if (JA.isHostOffloading(Action::OFK_Cuda))
1340b57cec5SDimitry Andric     Work(*C.getSingleOffloadToolChain<Action::OFK_Cuda>());
1350b57cec5SDimitry Andric   else if (JA.isDeviceOffloading(Action::OFK_Cuda))
1360b57cec5SDimitry Andric     Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
1370b57cec5SDimitry Andric   else if (JA.isHostOffloading(Action::OFK_HIP))
1380b57cec5SDimitry Andric     Work(*C.getSingleOffloadToolChain<Action::OFK_HIP>());
1390b57cec5SDimitry Andric   else if (JA.isDeviceOffloading(Action::OFK_HIP))
1400b57cec5SDimitry Andric     Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   if (JA.isHostOffloading(Action::OFK_OpenMP)) {
1430b57cec5SDimitry Andric     auto TCs = C.getOffloadToolChains<Action::OFK_OpenMP>();
1440b57cec5SDimitry Andric     for (auto II = TCs.first, IE = TCs.second; II != IE; ++II)
1450b57cec5SDimitry Andric       Work(*II->second);
1460b57cec5SDimitry Andric   } else if (JA.isDeviceOffloading(Action::OFK_OpenMP))
1470b57cec5SDimitry Andric     Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric   //
1500b57cec5SDimitry Andric   // TODO: Add support for other offloading programming models here.
1510b57cec5SDimitry Andric   //
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric /// This is a helper function for validating the optional refinement step
1550b57cec5SDimitry Andric /// parameter in reciprocal argument strings. Return false if there is an error
1560b57cec5SDimitry Andric /// parsing the refinement step. Otherwise, return true and set the Position
1570b57cec5SDimitry Andric /// of the refinement step in the input string.
1580b57cec5SDimitry Andric static bool getRefinementStep(StringRef In, const Driver &D,
1590b57cec5SDimitry Andric                               const Arg &A, size_t &Position) {
1600b57cec5SDimitry Andric   const char RefinementStepToken = ':';
1610b57cec5SDimitry Andric   Position = In.find(RefinementStepToken);
1620b57cec5SDimitry Andric   if (Position != StringRef::npos) {
1630b57cec5SDimitry Andric     StringRef Option = A.getOption().getName();
1640b57cec5SDimitry Andric     StringRef RefStep = In.substr(Position + 1);
1650b57cec5SDimitry Andric     // Allow exactly one numeric character for the additional refinement
1660b57cec5SDimitry Andric     // step parameter. This is reasonable for all currently-supported
1670b57cec5SDimitry Andric     // operations and architectures because we would expect that a larger value
1680b57cec5SDimitry Andric     // of refinement steps would cause the estimate "optimization" to
1690b57cec5SDimitry Andric     // under-perform the native operation. Also, if the estimate does not
1700b57cec5SDimitry Andric     // converge quickly, it probably will not ever converge, so further
1710b57cec5SDimitry Andric     // refinement steps will not produce a better answer.
1720b57cec5SDimitry Andric     if (RefStep.size() != 1) {
1730b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << Option << RefStep;
1740b57cec5SDimitry Andric       return false;
1750b57cec5SDimitry Andric     }
1760b57cec5SDimitry Andric     char RefStepChar = RefStep[0];
1770b57cec5SDimitry Andric     if (RefStepChar < '0' || RefStepChar > '9') {
1780b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << Option << RefStep;
1790b57cec5SDimitry Andric       return false;
1800b57cec5SDimitry Andric     }
1810b57cec5SDimitry Andric   }
1820b57cec5SDimitry Andric   return true;
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric /// The -mrecip flag requires processing of many optional parameters.
1860b57cec5SDimitry Andric static void ParseMRecip(const Driver &D, const ArgList &Args,
1870b57cec5SDimitry Andric                         ArgStringList &OutStrings) {
1880b57cec5SDimitry Andric   StringRef DisabledPrefixIn = "!";
1890b57cec5SDimitry Andric   StringRef DisabledPrefixOut = "!";
1900b57cec5SDimitry Andric   StringRef EnabledPrefixOut = "";
1910b57cec5SDimitry Andric   StringRef Out = "-mrecip=";
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric   Arg *A = Args.getLastArg(options::OPT_mrecip, options::OPT_mrecip_EQ);
1940b57cec5SDimitry Andric   if (!A)
1950b57cec5SDimitry Andric     return;
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric   unsigned NumOptions = A->getNumValues();
1980b57cec5SDimitry Andric   if (NumOptions == 0) {
1990b57cec5SDimitry Andric     // No option is the same as "all".
2000b57cec5SDimitry Andric     OutStrings.push_back(Args.MakeArgString(Out + "all"));
2010b57cec5SDimitry Andric     return;
2020b57cec5SDimitry Andric   }
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric   // Pass through "all", "none", or "default" with an optional refinement step.
2050b57cec5SDimitry Andric   if (NumOptions == 1) {
2060b57cec5SDimitry Andric     StringRef Val = A->getValue(0);
2070b57cec5SDimitry Andric     size_t RefStepLoc;
2080b57cec5SDimitry Andric     if (!getRefinementStep(Val, D, *A, RefStepLoc))
2090b57cec5SDimitry Andric       return;
2100b57cec5SDimitry Andric     StringRef ValBase = Val.slice(0, RefStepLoc);
2110b57cec5SDimitry Andric     if (ValBase == "all" || ValBase == "none" || ValBase == "default") {
2120b57cec5SDimitry Andric       OutStrings.push_back(Args.MakeArgString(Out + Val));
2130b57cec5SDimitry Andric       return;
2140b57cec5SDimitry Andric     }
2150b57cec5SDimitry Andric   }
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   // Each reciprocal type may be enabled or disabled individually.
2180b57cec5SDimitry Andric   // Check each input value for validity, concatenate them all back together,
2190b57cec5SDimitry Andric   // and pass through.
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric   llvm::StringMap<bool> OptionStrings;
2220b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("divd", false));
2230b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("divf", false));
2240b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("vec-divd", false));
2250b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("vec-divf", false));
2260b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("sqrtd", false));
2270b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("sqrtf", false));
2280b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("vec-sqrtd", false));
2290b57cec5SDimitry Andric   OptionStrings.insert(std::make_pair("vec-sqrtf", false));
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric   for (unsigned i = 0; i != NumOptions; ++i) {
2320b57cec5SDimitry Andric     StringRef Val = A->getValue(i);
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric     bool IsDisabled = Val.startswith(DisabledPrefixIn);
2350b57cec5SDimitry Andric     // Ignore the disablement token for string matching.
2360b57cec5SDimitry Andric     if (IsDisabled)
2370b57cec5SDimitry Andric       Val = Val.substr(1);
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric     size_t RefStep;
2400b57cec5SDimitry Andric     if (!getRefinementStep(Val, D, *A, RefStep))
2410b57cec5SDimitry Andric       return;
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric     StringRef ValBase = Val.slice(0, RefStep);
2440b57cec5SDimitry Andric     llvm::StringMap<bool>::iterator OptionIter = OptionStrings.find(ValBase);
2450b57cec5SDimitry Andric     if (OptionIter == OptionStrings.end()) {
2460b57cec5SDimitry Andric       // Try again specifying float suffix.
2470b57cec5SDimitry Andric       OptionIter = OptionStrings.find(ValBase.str() + 'f');
2480b57cec5SDimitry Andric       if (OptionIter == OptionStrings.end()) {
2490b57cec5SDimitry Andric         // The input name did not match any known option string.
2500b57cec5SDimitry Andric         D.Diag(diag::err_drv_unknown_argument) << Val;
2510b57cec5SDimitry Andric         return;
2520b57cec5SDimitry Andric       }
2530b57cec5SDimitry Andric       // The option was specified without a float or double suffix.
2540b57cec5SDimitry Andric       // Make sure that the double entry was not already specified.
2550b57cec5SDimitry Andric       // The float entry will be checked below.
2560b57cec5SDimitry Andric       if (OptionStrings[ValBase.str() + 'd']) {
2570b57cec5SDimitry Andric         D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val;
2580b57cec5SDimitry Andric         return;
2590b57cec5SDimitry Andric       }
2600b57cec5SDimitry Andric     }
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric     if (OptionIter->second == true) {
2630b57cec5SDimitry Andric       // Duplicate option specified.
2640b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val;
2650b57cec5SDimitry Andric       return;
2660b57cec5SDimitry Andric     }
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric     // Mark the matched option as found. Do not allow duplicate specifiers.
2690b57cec5SDimitry Andric     OptionIter->second = true;
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric     // If the precision was not specified, also mark the double entry as found.
2720b57cec5SDimitry Andric     if (ValBase.back() != 'f' && ValBase.back() != 'd')
2730b57cec5SDimitry Andric       OptionStrings[ValBase.str() + 'd'] = true;
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric     // Build the output string.
2760b57cec5SDimitry Andric     StringRef Prefix = IsDisabled ? DisabledPrefixOut : EnabledPrefixOut;
2770b57cec5SDimitry Andric     Out = Args.MakeArgString(Out + Prefix + Val);
2780b57cec5SDimitry Andric     if (i != NumOptions - 1)
2790b57cec5SDimitry Andric       Out = Args.MakeArgString(Out + ",");
2800b57cec5SDimitry Andric   }
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   OutStrings.push_back(Args.MakeArgString(Out));
2830b57cec5SDimitry Andric }
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric /// The -mprefer-vector-width option accepts either a positive integer
2860b57cec5SDimitry Andric /// or the string "none".
2870b57cec5SDimitry Andric static void ParseMPreferVectorWidth(const Driver &D, const ArgList &Args,
2880b57cec5SDimitry Andric                                     ArgStringList &CmdArgs) {
2890b57cec5SDimitry Andric   Arg *A = Args.getLastArg(options::OPT_mprefer_vector_width_EQ);
2900b57cec5SDimitry Andric   if (!A)
2910b57cec5SDimitry Andric     return;
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric   StringRef Value = A->getValue();
2940b57cec5SDimitry Andric   if (Value == "none") {
2950b57cec5SDimitry Andric     CmdArgs.push_back("-mprefer-vector-width=none");
2960b57cec5SDimitry Andric   } else {
2970b57cec5SDimitry Andric     unsigned Width;
2980b57cec5SDimitry Andric     if (Value.getAsInteger(10, Width)) {
2990b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value;
3000b57cec5SDimitry Andric       return;
3010b57cec5SDimitry Andric     }
3020b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-mprefer-vector-width=" + Value));
3030b57cec5SDimitry Andric   }
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric 
306480093f4SDimitry Andric static void getWebAssemblyTargetFeatures(const ArgList &Args,
307480093f4SDimitry Andric                                          std::vector<StringRef> &Features) {
308480093f4SDimitry Andric   handleTargetFeaturesGroup(Args, Features, options::OPT_m_wasm_Features_Group);
309480093f4SDimitry Andric }
310480093f4SDimitry Andric 
3115ffd83dbSDimitry Andric static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
312480093f4SDimitry Andric                               const ArgList &Args, ArgStringList &CmdArgs,
3135ffd83dbSDimitry Andric                               bool ForAS, bool IsAux = false) {
314480093f4SDimitry Andric   std::vector<StringRef> Features;
315480093f4SDimitry Andric   switch (Triple.getArch()) {
316480093f4SDimitry Andric   default:
317480093f4SDimitry Andric     break;
318480093f4SDimitry Andric   case llvm::Triple::mips:
319480093f4SDimitry Andric   case llvm::Triple::mipsel:
320480093f4SDimitry Andric   case llvm::Triple::mips64:
321480093f4SDimitry Andric   case llvm::Triple::mips64el:
322480093f4SDimitry Andric     mips::getMIPSTargetFeatures(D, Triple, Args, Features);
323480093f4SDimitry Andric     break;
324480093f4SDimitry Andric 
325480093f4SDimitry Andric   case llvm::Triple::arm:
326480093f4SDimitry Andric   case llvm::Triple::armeb:
327480093f4SDimitry Andric   case llvm::Triple::thumb:
328480093f4SDimitry Andric   case llvm::Triple::thumbeb:
3295ffd83dbSDimitry Andric     arm::getARMTargetFeatures(D, Triple, Args, CmdArgs, Features, ForAS);
330480093f4SDimitry Andric     break;
331480093f4SDimitry Andric 
332480093f4SDimitry Andric   case llvm::Triple::ppc:
333e8d8bef9SDimitry Andric   case llvm::Triple::ppcle:
334480093f4SDimitry Andric   case llvm::Triple::ppc64:
335480093f4SDimitry Andric   case llvm::Triple::ppc64le:
336480093f4SDimitry Andric     ppc::getPPCTargetFeatures(D, Triple, Args, Features);
337480093f4SDimitry Andric     break;
338480093f4SDimitry Andric   case llvm::Triple::riscv32:
339480093f4SDimitry Andric   case llvm::Triple::riscv64:
340480093f4SDimitry Andric     riscv::getRISCVTargetFeatures(D, Triple, Args, Features);
341480093f4SDimitry Andric     break;
342480093f4SDimitry Andric   case llvm::Triple::systemz:
3435ffd83dbSDimitry Andric     systemz::getSystemZTargetFeatures(D, Args, Features);
344480093f4SDimitry Andric     break;
345480093f4SDimitry Andric   case llvm::Triple::aarch64:
346480093f4SDimitry Andric   case llvm::Triple::aarch64_32:
347480093f4SDimitry Andric   case llvm::Triple::aarch64_be:
348fe6060f1SDimitry Andric     aarch64::getAArch64TargetFeatures(D, Triple, Args, Features, ForAS);
349480093f4SDimitry Andric     break;
350480093f4SDimitry Andric   case llvm::Triple::x86:
351480093f4SDimitry Andric   case llvm::Triple::x86_64:
352480093f4SDimitry Andric     x86::getX86TargetFeatures(D, Triple, Args, Features);
353480093f4SDimitry Andric     break;
354480093f4SDimitry Andric   case llvm::Triple::hexagon:
355480093f4SDimitry Andric     hexagon::getHexagonTargetFeatures(D, Args, Features);
356480093f4SDimitry Andric     break;
357480093f4SDimitry Andric   case llvm::Triple::wasm32:
358480093f4SDimitry Andric   case llvm::Triple::wasm64:
359480093f4SDimitry Andric     getWebAssemblyTargetFeatures(Args, Features);
360480093f4SDimitry Andric     break;
361480093f4SDimitry Andric   case llvm::Triple::sparc:
362480093f4SDimitry Andric   case llvm::Triple::sparcel:
363480093f4SDimitry Andric   case llvm::Triple::sparcv9:
364480093f4SDimitry Andric     sparc::getSparcTargetFeatures(D, Args, Features);
365480093f4SDimitry Andric     break;
366480093f4SDimitry Andric   case llvm::Triple::r600:
367480093f4SDimitry Andric   case llvm::Triple::amdgcn:
368e8d8bef9SDimitry Andric     amdgpu::getAMDGPUTargetFeatures(D, Triple, Args, Features);
369480093f4SDimitry Andric     break;
370fe6060f1SDimitry Andric   case llvm::Triple::m68k:
371fe6060f1SDimitry Andric     m68k::getM68kTargetFeatures(D, Triple, Args, Features);
372fe6060f1SDimitry Andric     break;
373480093f4SDimitry Andric   case llvm::Triple::msp430:
374480093f4SDimitry Andric     msp430::getMSP430TargetFeatures(D, Args, Features);
3755ffd83dbSDimitry Andric     break;
3765ffd83dbSDimitry Andric   case llvm::Triple::ve:
3775ffd83dbSDimitry Andric     ve::getVETargetFeatures(D, Args, Features);
378fe6060f1SDimitry Andric     break;
379480093f4SDimitry Andric   }
380480093f4SDimitry Andric 
3815ffd83dbSDimitry Andric   for (auto Feature : unifyTargetFeatures(Features)) {
3825ffd83dbSDimitry Andric     CmdArgs.push_back(IsAux ? "-aux-target-feature" : "-target-feature");
3835ffd83dbSDimitry Andric     CmdArgs.push_back(Feature.data());
384480093f4SDimitry Andric   }
385480093f4SDimitry Andric }
386480093f4SDimitry Andric 
3870b57cec5SDimitry Andric static bool
3880b57cec5SDimitry Andric shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
3890b57cec5SDimitry Andric                                           const llvm::Triple &Triple) {
3900b57cec5SDimitry Andric   // We use the zero-cost exception tables for Objective-C if the non-fragile
3910b57cec5SDimitry Andric   // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
3920b57cec5SDimitry Andric   // later.
3930b57cec5SDimitry Andric   if (runtime.isNonFragile())
3940b57cec5SDimitry Andric     return true;
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric   if (!Triple.isMacOSX())
3970b57cec5SDimitry Andric     return false;
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric   return (!Triple.isMacOSXVersionLT(10, 5) &&
4000b57cec5SDimitry Andric           (Triple.getArch() == llvm::Triple::x86_64 ||
4010b57cec5SDimitry Andric            Triple.getArch() == llvm::Triple::arm));
4020b57cec5SDimitry Andric }
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric /// Adds exception related arguments to the driver command arguments. There's a
4050b57cec5SDimitry Andric /// master flag, -fexceptions and also language specific flags to enable/disable
4060b57cec5SDimitry Andric /// C++ and Objective-C exceptions. This makes it possible to for example
4070b57cec5SDimitry Andric /// disable C++ exceptions but enable Objective-C exceptions.
408fe6060f1SDimitry Andric static bool addExceptionArgs(const ArgList &Args, types::ID InputType,
4090b57cec5SDimitry Andric                              const ToolChain &TC, bool KernelOrKext,
4100b57cec5SDimitry Andric                              const ObjCRuntime &objcRuntime,
4110b57cec5SDimitry Andric                              ArgStringList &CmdArgs) {
4120b57cec5SDimitry Andric   const llvm::Triple &Triple = TC.getTriple();
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric   if (KernelOrKext) {
4150b57cec5SDimitry Andric     // -mkernel and -fapple-kext imply no exceptions, so claim exception related
4160b57cec5SDimitry Andric     // arguments now to avoid warnings about unused arguments.
4170b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fexceptions);
4180b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fno_exceptions);
4190b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
4200b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
4210b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
4220b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
423fe6060f1SDimitry Andric     Args.ClaimAllArgs(options::OPT_fasync_exceptions);
424fe6060f1SDimitry Andric     Args.ClaimAllArgs(options::OPT_fno_async_exceptions);
425fe6060f1SDimitry Andric     return false;
4260b57cec5SDimitry Andric   }
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric   // See if the user explicitly enabled exceptions.
4290b57cec5SDimitry Andric   bool EH = Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
4300b57cec5SDimitry Andric                          false);
4310b57cec5SDimitry Andric 
432fe6060f1SDimitry Andric   bool EHa = Args.hasFlag(options::OPT_fasync_exceptions,
433fe6060f1SDimitry Andric                           options::OPT_fno_async_exceptions, false);
434fe6060f1SDimitry Andric   if (EHa) {
435fe6060f1SDimitry Andric     CmdArgs.push_back("-fasync-exceptions");
436fe6060f1SDimitry Andric     EH = true;
437fe6060f1SDimitry Andric   }
438fe6060f1SDimitry Andric 
4390b57cec5SDimitry Andric   // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
4400b57cec5SDimitry Andric   // is not necessarily sensible, but follows GCC.
4410b57cec5SDimitry Andric   if (types::isObjC(InputType) &&
4420b57cec5SDimitry Andric       Args.hasFlag(options::OPT_fobjc_exceptions,
4430b57cec5SDimitry Andric                    options::OPT_fno_objc_exceptions, true)) {
4440b57cec5SDimitry Andric     CmdArgs.push_back("-fobjc-exceptions");
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric     EH |= shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
4470b57cec5SDimitry Andric   }
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric   if (types::isCXX(InputType)) {
4500b57cec5SDimitry Andric     // Disable C++ EH by default on XCore and PS4.
4510b57cec5SDimitry Andric     bool CXXExceptionsEnabled =
4520b57cec5SDimitry Andric         Triple.getArch() != llvm::Triple::xcore && !Triple.isPS4CPU();
4530b57cec5SDimitry Andric     Arg *ExceptionArg = Args.getLastArg(
4540b57cec5SDimitry Andric         options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
4550b57cec5SDimitry Andric         options::OPT_fexceptions, options::OPT_fno_exceptions);
4560b57cec5SDimitry Andric     if (ExceptionArg)
4570b57cec5SDimitry Andric       CXXExceptionsEnabled =
4580b57cec5SDimitry Andric           ExceptionArg->getOption().matches(options::OPT_fcxx_exceptions) ||
4590b57cec5SDimitry Andric           ExceptionArg->getOption().matches(options::OPT_fexceptions);
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric     if (CXXExceptionsEnabled) {
4620b57cec5SDimitry Andric       CmdArgs.push_back("-fcxx-exceptions");
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric       EH = true;
4650b57cec5SDimitry Andric     }
4660b57cec5SDimitry Andric   }
4670b57cec5SDimitry Andric 
4685ffd83dbSDimitry Andric   // OPT_fignore_exceptions means exception could still be thrown,
4695ffd83dbSDimitry Andric   // but no clean up or catch would happen in current module.
4705ffd83dbSDimitry Andric   // So we do not set EH to false.
4715ffd83dbSDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fignore_exceptions);
4725ffd83dbSDimitry Andric 
4730b57cec5SDimitry Andric   if (EH)
4740b57cec5SDimitry Andric     CmdArgs.push_back("-fexceptions");
475fe6060f1SDimitry Andric   return EH;
4760b57cec5SDimitry Andric }
4770b57cec5SDimitry Andric 
478480093f4SDimitry Andric static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC,
479480093f4SDimitry Andric                                  const JobAction &JA) {
4800b57cec5SDimitry Andric   bool Default = true;
4810b57cec5SDimitry Andric   if (TC.getTriple().isOSDarwin()) {
4820b57cec5SDimitry Andric     // The native darwin assembler doesn't support the linker_option directives,
4830b57cec5SDimitry Andric     // so we disable them if we think the .s file will be passed to it.
4840b57cec5SDimitry Andric     Default = TC.useIntegratedAs();
4850b57cec5SDimitry Andric   }
486480093f4SDimitry Andric   // The linker_option directives are intended for host compilation.
487480093f4SDimitry Andric   if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
488480093f4SDimitry Andric       JA.isDeviceOffloading(Action::OFK_HIP))
489480093f4SDimitry Andric     Default = false;
490480093f4SDimitry Andric   return Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
4910b57cec5SDimitry Andric                       Default);
4920b57cec5SDimitry Andric }
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric // Convert an arg of the form "-gN" or "-ggdbN" or one of their aliases
4950b57cec5SDimitry Andric // to the corresponding DebugInfoKind.
4960b57cec5SDimitry Andric static codegenoptions::DebugInfoKind DebugLevelToInfoKind(const Arg &A) {
4970b57cec5SDimitry Andric   assert(A.getOption().matches(options::OPT_gN_Group) &&
4980b57cec5SDimitry Andric          "Not a -g option that specifies a debug-info level");
4990b57cec5SDimitry Andric   if (A.getOption().matches(options::OPT_g0) ||
5000b57cec5SDimitry Andric       A.getOption().matches(options::OPT_ggdb0))
5010b57cec5SDimitry Andric     return codegenoptions::NoDebugInfo;
5020b57cec5SDimitry Andric   if (A.getOption().matches(options::OPT_gline_tables_only) ||
5030b57cec5SDimitry Andric       A.getOption().matches(options::OPT_ggdb1))
5040b57cec5SDimitry Andric     return codegenoptions::DebugLineTablesOnly;
5050b57cec5SDimitry Andric   if (A.getOption().matches(options::OPT_gline_directives_only))
5060b57cec5SDimitry Andric     return codegenoptions::DebugDirectivesOnly;
507fe6060f1SDimitry Andric   return codegenoptions::DebugInfoConstructor;
5080b57cec5SDimitry Andric }
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric static bool mustUseNonLeafFramePointerForTarget(const llvm::Triple &Triple) {
5110b57cec5SDimitry Andric   switch (Triple.getArch()){
5120b57cec5SDimitry Andric   default:
5130b57cec5SDimitry Andric     return false;
5140b57cec5SDimitry Andric   case llvm::Triple::arm:
5150b57cec5SDimitry Andric   case llvm::Triple::thumb:
5160b57cec5SDimitry Andric     // ARM Darwin targets require a frame pointer to be always present to aid
5170b57cec5SDimitry Andric     // offline debugging via backtraces.
5180b57cec5SDimitry Andric     return Triple.isOSDarwin();
5190b57cec5SDimitry Andric   }
5200b57cec5SDimitry Andric }
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric static bool useFramePointerForTargetByDefault(const ArgList &Args,
5230b57cec5SDimitry Andric                                               const llvm::Triple &Triple) {
5245ffd83dbSDimitry Andric   if (Args.hasArg(options::OPT_pg) && !Args.hasArg(options::OPT_mfentry))
525a7dea167SDimitry Andric     return true;
526a7dea167SDimitry Andric 
5270b57cec5SDimitry Andric   switch (Triple.getArch()) {
5280b57cec5SDimitry Andric   case llvm::Triple::xcore:
5290b57cec5SDimitry Andric   case llvm::Triple::wasm32:
5300b57cec5SDimitry Andric   case llvm::Triple::wasm64:
5310b57cec5SDimitry Andric   case llvm::Triple::msp430:
5320b57cec5SDimitry Andric     // XCore never wants frame pointers, regardless of OS.
5330b57cec5SDimitry Andric     // WebAssembly never wants frame pointers.
5340b57cec5SDimitry Andric     return false;
5350b57cec5SDimitry Andric   case llvm::Triple::ppc:
536e8d8bef9SDimitry Andric   case llvm::Triple::ppcle:
5370b57cec5SDimitry Andric   case llvm::Triple::ppc64:
5380b57cec5SDimitry Andric   case llvm::Triple::ppc64le:
5390b57cec5SDimitry Andric   case llvm::Triple::riscv32:
5400b57cec5SDimitry Andric   case llvm::Triple::riscv64:
541480093f4SDimitry Andric   case llvm::Triple::amdgcn:
542480093f4SDimitry 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.
5565ffd83dbSDimitry Andric     case llvm::Triple::arm:
5575ffd83dbSDimitry Andric     case llvm::Triple::armeb:
5585ffd83dbSDimitry Andric     case llvm::Triple::thumb:
5595ffd83dbSDimitry Andric     case llvm::Triple::thumbeb:
5605ffd83dbSDimitry Andric       if (Triple.isAndroid())
5615ffd83dbSDimitry Andric         return true;
5625ffd83dbSDimitry Andric       LLVM_FALLTHROUGH;
5630b57cec5SDimitry Andric     case llvm::Triple::mips64:
5640b57cec5SDimitry Andric     case llvm::Triple::mips64el:
5650b57cec5SDimitry Andric     case llvm::Triple::mips:
5660b57cec5SDimitry Andric     case llvm::Triple::mipsel:
5670b57cec5SDimitry Andric     case llvm::Triple::systemz:
5680b57cec5SDimitry Andric     case llvm::Triple::x86:
5690b57cec5SDimitry Andric     case llvm::Triple::x86_64:
5700b57cec5SDimitry Andric       return !areOptimizationsEnabled(Args);
5710b57cec5SDimitry Andric     default:
5720b57cec5SDimitry Andric       return true;
5730b57cec5SDimitry Andric     }
5740b57cec5SDimitry Andric   }
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric   if (Triple.isOSWindows()) {
5770b57cec5SDimitry Andric     switch (Triple.getArch()) {
5780b57cec5SDimitry Andric     case llvm::Triple::x86:
5790b57cec5SDimitry Andric       return !areOptimizationsEnabled(Args);
5800b57cec5SDimitry Andric     case llvm::Triple::x86_64:
5810b57cec5SDimitry Andric       return Triple.isOSBinFormatMachO();
5820b57cec5SDimitry Andric     case llvm::Triple::arm:
5830b57cec5SDimitry Andric     case llvm::Triple::thumb:
5840b57cec5SDimitry Andric       // Windows on ARM builds with FPO disabled to aid fast stack walking
5850b57cec5SDimitry Andric       return true;
5860b57cec5SDimitry Andric     default:
5870b57cec5SDimitry Andric       // All other supported Windows ISAs use xdata unwind information, so frame
5880b57cec5SDimitry Andric       // pointers are not generally useful.
5890b57cec5SDimitry Andric       return false;
5900b57cec5SDimitry Andric     }
5910b57cec5SDimitry Andric   }
5920b57cec5SDimitry Andric 
5930b57cec5SDimitry Andric   return true;
5940b57cec5SDimitry Andric }
5950b57cec5SDimitry Andric 
596a7dea167SDimitry Andric static CodeGenOptions::FramePointerKind
597a7dea167SDimitry Andric getFramePointerKind(const ArgList &Args, const llvm::Triple &Triple) {
598a7dea167SDimitry Andric   // We have 4 states:
599a7dea167SDimitry Andric   //
600a7dea167SDimitry Andric   //  00) leaf retained, non-leaf retained
601a7dea167SDimitry Andric   //  01) leaf retained, non-leaf omitted (this is invalid)
602a7dea167SDimitry Andric   //  10) leaf omitted, non-leaf retained
603a7dea167SDimitry Andric   //      (what -momit-leaf-frame-pointer was designed for)
604a7dea167SDimitry Andric   //  11) leaf omitted, non-leaf omitted
605a7dea167SDimitry Andric   //
606a7dea167SDimitry Andric   //  "omit" options taking precedence over "no-omit" options is the only way
607a7dea167SDimitry Andric   //  to make 3 valid states representable
608a7dea167SDimitry Andric   Arg *A = Args.getLastArg(options::OPT_fomit_frame_pointer,
609a7dea167SDimitry Andric                            options::OPT_fno_omit_frame_pointer);
610a7dea167SDimitry Andric   bool OmitFP = A && A->getOption().matches(options::OPT_fomit_frame_pointer);
611a7dea167SDimitry Andric   bool NoOmitFP =
612a7dea167SDimitry Andric       A && A->getOption().matches(options::OPT_fno_omit_frame_pointer);
613e8d8bef9SDimitry Andric   bool OmitLeafFP = Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
614480093f4SDimitry Andric                                  options::OPT_mno_omit_leaf_frame_pointer,
615480093f4SDimitry Andric                                  Triple.isAArch64() || Triple.isPS4CPU());
616a7dea167SDimitry Andric   if (NoOmitFP || mustUseNonLeafFramePointerForTarget(Triple) ||
617a7dea167SDimitry Andric       (!OmitFP && useFramePointerForTargetByDefault(Args, Triple))) {
618e8d8bef9SDimitry Andric     if (OmitLeafFP)
619a7dea167SDimitry Andric       return CodeGenOptions::FramePointerKind::NonLeaf;
620a7dea167SDimitry Andric     return CodeGenOptions::FramePointerKind::All;
6210b57cec5SDimitry Andric   }
622a7dea167SDimitry Andric   return CodeGenOptions::FramePointerKind::None;
6230b57cec5SDimitry Andric }
6240b57cec5SDimitry Andric 
6250b57cec5SDimitry Andric /// Add a CC1 option to specify the debug compilation directory.
626a7dea167SDimitry Andric static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs,
627a7dea167SDimitry Andric                                const llvm::vfs::FileSystem &VFS) {
628fe6060f1SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ,
629fe6060f1SDimitry Andric                                options::OPT_fdebug_compilation_dir_EQ)) {
630fe6060f1SDimitry Andric     if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ))
631fe6060f1SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(Twine("-fdebug-compilation-dir=") +
632fe6060f1SDimitry Andric                                            A->getValue()));
633fe6060f1SDimitry Andric     else
634fe6060f1SDimitry Andric       A->render(Args, CmdArgs);
635a7dea167SDimitry Andric   } else if (llvm::ErrorOr<std::string> CWD =
636a7dea167SDimitry Andric                  VFS.getCurrentWorkingDirectory()) {
637fe6060f1SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-fdebug-compilation-dir=" + *CWD));
6380b57cec5SDimitry Andric   }
6390b57cec5SDimitry Andric }
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric /// Add a CC1 and CC1AS option to specify the debug file path prefix map.
6420b57cec5SDimitry Andric static void addDebugPrefixMapArg(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs) {
643480093f4SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ,
644480093f4SDimitry Andric                                     options::OPT_fdebug_prefix_map_EQ)) {
6450b57cec5SDimitry Andric     StringRef Map = A->getValue();
6460b57cec5SDimitry Andric     if (Map.find('=') == StringRef::npos)
647480093f4SDimitry Andric       D.Diag(diag::err_drv_invalid_argument_to_option)
648480093f4SDimitry Andric           << Map << A->getOption().getName();
6490b57cec5SDimitry Andric     else
6500b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map));
6510b57cec5SDimitry Andric     A->claim();
6520b57cec5SDimitry Andric   }
6530b57cec5SDimitry Andric }
6540b57cec5SDimitry Andric 
655480093f4SDimitry Andric /// Add a CC1 and CC1AS option to specify the macro file path prefix map.
656480093f4SDimitry Andric static void addMacroPrefixMapArg(const Driver &D, const ArgList &Args,
657480093f4SDimitry Andric                                  ArgStringList &CmdArgs) {
658480093f4SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ,
659480093f4SDimitry Andric                                     options::OPT_fmacro_prefix_map_EQ)) {
660480093f4SDimitry Andric     StringRef Map = A->getValue();
661480093f4SDimitry Andric     if (Map.find('=') == StringRef::npos)
662480093f4SDimitry Andric       D.Diag(diag::err_drv_invalid_argument_to_option)
663480093f4SDimitry Andric           << Map << A->getOption().getName();
664480093f4SDimitry Andric     else
665480093f4SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-fmacro-prefix-map=" + Map));
666480093f4SDimitry Andric     A->claim();
667480093f4SDimitry Andric   }
668480093f4SDimitry Andric }
669480093f4SDimitry Andric 
670e8d8bef9SDimitry Andric /// Add a CC1 and CC1AS option to specify the coverage file path prefix map.
671fe6060f1SDimitry Andric static void addCoveragePrefixMapArg(const Driver &D, const ArgList &Args,
672e8d8bef9SDimitry Andric                                    ArgStringList &CmdArgs) {
673e8d8bef9SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ,
674fe6060f1SDimitry Andric                                     options::OPT_fcoverage_prefix_map_EQ)) {
675e8d8bef9SDimitry Andric     StringRef Map = A->getValue();
676e8d8bef9SDimitry Andric     if (Map.find('=') == StringRef::npos)
677e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_invalid_argument_to_option)
678e8d8bef9SDimitry Andric           << Map << A->getOption().getName();
679e8d8bef9SDimitry Andric     else
680fe6060f1SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-fcoverage-prefix-map=" + Map));
681e8d8bef9SDimitry Andric     A->claim();
682e8d8bef9SDimitry Andric   }
683e8d8bef9SDimitry Andric }
684e8d8bef9SDimitry Andric 
6850b57cec5SDimitry Andric /// Vectorize at all optimization levels greater than 1 except for -Oz.
6860b57cec5SDimitry Andric /// For -Oz the loop vectorizer is disabled, while the slp vectorizer is
6870b57cec5SDimitry Andric /// enabled.
6880b57cec5SDimitry Andric static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) {
6890b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
6900b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_O4) ||
6910b57cec5SDimitry Andric         A->getOption().matches(options::OPT_Ofast))
6920b57cec5SDimitry Andric       return true;
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_O0))
6950b57cec5SDimitry Andric       return false;
6960b57cec5SDimitry Andric 
6970b57cec5SDimitry Andric     assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
6980b57cec5SDimitry Andric 
6990b57cec5SDimitry Andric     // Vectorize -Os.
7000b57cec5SDimitry Andric     StringRef S(A->getValue());
7010b57cec5SDimitry Andric     if (S == "s")
7020b57cec5SDimitry Andric       return true;
7030b57cec5SDimitry Andric 
7040b57cec5SDimitry Andric     // Don't vectorize -Oz, unless it's the slp vectorizer.
7050b57cec5SDimitry Andric     if (S == "z")
7060b57cec5SDimitry Andric       return isSlpVec;
7070b57cec5SDimitry Andric 
7080b57cec5SDimitry Andric     unsigned OptLevel = 0;
7090b57cec5SDimitry Andric     if (S.getAsInteger(10, OptLevel))
7100b57cec5SDimitry Andric       return false;
7110b57cec5SDimitry Andric 
7120b57cec5SDimitry Andric     return OptLevel > 1;
7130b57cec5SDimitry Andric   }
7140b57cec5SDimitry Andric 
7150b57cec5SDimitry Andric   return false;
7160b57cec5SDimitry Andric }
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric /// Add -x lang to \p CmdArgs for \p Input.
7190b57cec5SDimitry Andric static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
7200b57cec5SDimitry Andric                              ArgStringList &CmdArgs) {
7210b57cec5SDimitry Andric   // When using -verify-pch, we don't want to provide the type
7220b57cec5SDimitry Andric   // 'precompiled-header' if it was inferred from the file extension
7230b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH)
7240b57cec5SDimitry Andric     return;
7250b57cec5SDimitry Andric 
7260b57cec5SDimitry Andric   CmdArgs.push_back("-x");
7270b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_rewrite_objc))
7280b57cec5SDimitry Andric     CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
7290b57cec5SDimitry Andric   else {
7300b57cec5SDimitry Andric     // Map the driver type to the frontend type. This is mostly an identity
7310b57cec5SDimitry Andric     // mapping, except that the distinction between module interface units
7320b57cec5SDimitry Andric     // and other source files does not exist at the frontend layer.
7330b57cec5SDimitry Andric     const char *ClangType;
7340b57cec5SDimitry Andric     switch (Input.getType()) {
7350b57cec5SDimitry Andric     case types::TY_CXXModule:
7360b57cec5SDimitry Andric       ClangType = "c++";
7370b57cec5SDimitry Andric       break;
7380b57cec5SDimitry Andric     case types::TY_PP_CXXModule:
7390b57cec5SDimitry Andric       ClangType = "c++-cpp-output";
7400b57cec5SDimitry Andric       break;
7410b57cec5SDimitry Andric     default:
7420b57cec5SDimitry Andric       ClangType = types::getTypeName(Input.getType());
7430b57cec5SDimitry Andric       break;
7440b57cec5SDimitry Andric     }
7450b57cec5SDimitry Andric     CmdArgs.push_back(ClangType);
7460b57cec5SDimitry Andric   }
7470b57cec5SDimitry Andric }
7480b57cec5SDimitry Andric 
7490b57cec5SDimitry Andric static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
7500b57cec5SDimitry Andric                                    const Driver &D, const InputInfo &Output,
7510b57cec5SDimitry Andric                                    const ArgList &Args,
7520b57cec5SDimitry Andric                                    ArgStringList &CmdArgs) {
7530b57cec5SDimitry Andric 
7540b57cec5SDimitry Andric   auto *PGOGenerateArg = Args.getLastArg(options::OPT_fprofile_generate,
7550b57cec5SDimitry Andric                                          options::OPT_fprofile_generate_EQ,
7560b57cec5SDimitry Andric                                          options::OPT_fno_profile_generate);
7570b57cec5SDimitry Andric   if (PGOGenerateArg &&
7580b57cec5SDimitry Andric       PGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate))
7590b57cec5SDimitry Andric     PGOGenerateArg = nullptr;
7600b57cec5SDimitry Andric 
7610b57cec5SDimitry Andric   auto *CSPGOGenerateArg = Args.getLastArg(options::OPT_fcs_profile_generate,
7620b57cec5SDimitry Andric                                            options::OPT_fcs_profile_generate_EQ,
7630b57cec5SDimitry Andric                                            options::OPT_fno_profile_generate);
7640b57cec5SDimitry Andric   if (CSPGOGenerateArg &&
7650b57cec5SDimitry Andric       CSPGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate))
7660b57cec5SDimitry Andric     CSPGOGenerateArg = nullptr;
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric   auto *ProfileGenerateArg = Args.getLastArg(
7690b57cec5SDimitry Andric       options::OPT_fprofile_instr_generate,
7700b57cec5SDimitry Andric       options::OPT_fprofile_instr_generate_EQ,
7710b57cec5SDimitry Andric       options::OPT_fno_profile_instr_generate);
7720b57cec5SDimitry Andric   if (ProfileGenerateArg &&
7730b57cec5SDimitry Andric       ProfileGenerateArg->getOption().matches(
7740b57cec5SDimitry Andric           options::OPT_fno_profile_instr_generate))
7750b57cec5SDimitry Andric     ProfileGenerateArg = nullptr;
7760b57cec5SDimitry Andric 
7770b57cec5SDimitry Andric   if (PGOGenerateArg && ProfileGenerateArg)
7780b57cec5SDimitry Andric     D.Diag(diag::err_drv_argument_not_allowed_with)
7790b57cec5SDimitry Andric         << PGOGenerateArg->getSpelling() << ProfileGenerateArg->getSpelling();
7800b57cec5SDimitry Andric 
7810b57cec5SDimitry Andric   auto *ProfileUseArg = getLastProfileUseArg(Args);
7820b57cec5SDimitry Andric 
7830b57cec5SDimitry Andric   if (PGOGenerateArg && ProfileUseArg)
7840b57cec5SDimitry Andric     D.Diag(diag::err_drv_argument_not_allowed_with)
7850b57cec5SDimitry Andric         << ProfileUseArg->getSpelling() << PGOGenerateArg->getSpelling();
7860b57cec5SDimitry Andric 
7870b57cec5SDimitry Andric   if (ProfileGenerateArg && ProfileUseArg)
7880b57cec5SDimitry Andric     D.Diag(diag::err_drv_argument_not_allowed_with)
7890b57cec5SDimitry Andric         << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling();
7900b57cec5SDimitry Andric 
791e8d8bef9SDimitry Andric   if (CSPGOGenerateArg && PGOGenerateArg) {
7920b57cec5SDimitry Andric     D.Diag(diag::err_drv_argument_not_allowed_with)
7930b57cec5SDimitry Andric         << CSPGOGenerateArg->getSpelling() << PGOGenerateArg->getSpelling();
794e8d8bef9SDimitry Andric     PGOGenerateArg = nullptr;
795e8d8bef9SDimitry Andric   }
7960b57cec5SDimitry Andric 
797fe6060f1SDimitry Andric   if (TC.getTriple().isOSAIX()) {
798fe6060f1SDimitry Andric     if (PGOGenerateArg)
799fe6060f1SDimitry Andric       if (!D.isUsingLTO(false /*IsDeviceOffloadAction */) ||
800fe6060f1SDimitry Andric           D.getLTOMode() != LTOK_Full)
801fe6060f1SDimitry Andric         D.Diag(clang::diag::err_drv_argument_only_allowed_with)
802fe6060f1SDimitry Andric             << PGOGenerateArg->getSpelling() << "-flto";
803fe6060f1SDimitry Andric     if (ProfileGenerateArg)
804fe6060f1SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
805fe6060f1SDimitry Andric           << ProfileGenerateArg->getSpelling() << TC.getTriple().str();
806fe6060f1SDimitry Andric     if (Arg *ProfileSampleUseArg = getLastProfileSampleUseArg(Args))
807fe6060f1SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
808fe6060f1SDimitry Andric           << ProfileSampleUseArg->getSpelling() << TC.getTriple().str();
809fe6060f1SDimitry Andric   }
810fe6060f1SDimitry Andric 
8110b57cec5SDimitry Andric   if (ProfileGenerateArg) {
8120b57cec5SDimitry Andric     if (ProfileGenerateArg->getOption().matches(
8130b57cec5SDimitry Andric             options::OPT_fprofile_instr_generate_EQ))
8140b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-instrument-path=") +
8150b57cec5SDimitry Andric                                            ProfileGenerateArg->getValue()));
8160b57cec5SDimitry Andric     // The default is to use Clang Instrumentation.
8170b57cec5SDimitry Andric     CmdArgs.push_back("-fprofile-instrument=clang");
8180b57cec5SDimitry Andric     if (TC.getTriple().isWindowsMSVCEnvironment()) {
8190b57cec5SDimitry Andric       // Add dependent lib for clang_rt.profile
8205ffd83dbSDimitry Andric       CmdArgs.push_back(Args.MakeArgString(
8215ffd83dbSDimitry Andric           "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile")));
8220b57cec5SDimitry Andric     }
8230b57cec5SDimitry Andric   }
8240b57cec5SDimitry Andric 
8250b57cec5SDimitry Andric   Arg *PGOGenArg = nullptr;
8260b57cec5SDimitry Andric   if (PGOGenerateArg) {
8270b57cec5SDimitry Andric     assert(!CSPGOGenerateArg);
8280b57cec5SDimitry Andric     PGOGenArg = PGOGenerateArg;
8290b57cec5SDimitry Andric     CmdArgs.push_back("-fprofile-instrument=llvm");
8300b57cec5SDimitry Andric   }
8310b57cec5SDimitry Andric   if (CSPGOGenerateArg) {
8320b57cec5SDimitry Andric     assert(!PGOGenerateArg);
8330b57cec5SDimitry Andric     PGOGenArg = CSPGOGenerateArg;
8340b57cec5SDimitry Andric     CmdArgs.push_back("-fprofile-instrument=csllvm");
8350b57cec5SDimitry Andric   }
8360b57cec5SDimitry Andric   if (PGOGenArg) {
8370b57cec5SDimitry Andric     if (TC.getTriple().isWindowsMSVCEnvironment()) {
8385ffd83dbSDimitry Andric       // Add dependent lib for clang_rt.profile
8395ffd83dbSDimitry Andric       CmdArgs.push_back(Args.MakeArgString(
8405ffd83dbSDimitry Andric           "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile")));
8410b57cec5SDimitry Andric     }
8420b57cec5SDimitry Andric     if (PGOGenArg->getOption().matches(
8430b57cec5SDimitry Andric             PGOGenerateArg ? options::OPT_fprofile_generate_EQ
8440b57cec5SDimitry Andric                            : options::OPT_fcs_profile_generate_EQ)) {
8450b57cec5SDimitry Andric       SmallString<128> Path(PGOGenArg->getValue());
8460b57cec5SDimitry Andric       llvm::sys::path::append(Path, "default_%m.profraw");
8470b57cec5SDimitry Andric       CmdArgs.push_back(
8480b57cec5SDimitry Andric           Args.MakeArgString(Twine("-fprofile-instrument-path=") + Path));
8490b57cec5SDimitry Andric     }
8500b57cec5SDimitry Andric   }
8510b57cec5SDimitry Andric 
8520b57cec5SDimitry Andric   if (ProfileUseArg) {
8530b57cec5SDimitry Andric     if (ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ))
8540b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(
8550b57cec5SDimitry Andric           Twine("-fprofile-instrument-use-path=") + ProfileUseArg->getValue()));
8560b57cec5SDimitry Andric     else if ((ProfileUseArg->getOption().matches(
8570b57cec5SDimitry Andric                   options::OPT_fprofile_use_EQ) ||
8580b57cec5SDimitry Andric               ProfileUseArg->getOption().matches(
8590b57cec5SDimitry Andric                   options::OPT_fprofile_instr_use))) {
8600b57cec5SDimitry Andric       SmallString<128> Path(
8610b57cec5SDimitry Andric           ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue());
8620b57cec5SDimitry Andric       if (Path.empty() || llvm::sys::fs::is_directory(Path))
8630b57cec5SDimitry Andric         llvm::sys::path::append(Path, "default.profdata");
8640b57cec5SDimitry Andric       CmdArgs.push_back(
8650b57cec5SDimitry Andric           Args.MakeArgString(Twine("-fprofile-instrument-use-path=") + Path));
8660b57cec5SDimitry Andric     }
8670b57cec5SDimitry Andric   }
8680b57cec5SDimitry Andric 
8695ffd83dbSDimitry Andric   bool EmitCovNotes = Args.hasFlag(options::OPT_ftest_coverage,
8705ffd83dbSDimitry Andric                                    options::OPT_fno_test_coverage, false) ||
871a7dea167SDimitry Andric                       Args.hasArg(options::OPT_coverage);
8725ffd83dbSDimitry Andric   bool EmitCovData = TC.needsGCovInstrumentation(Args);
873a7dea167SDimitry Andric   if (EmitCovNotes)
874e8d8bef9SDimitry Andric     CmdArgs.push_back("-ftest-coverage");
875a7dea167SDimitry Andric   if (EmitCovData)
876e8d8bef9SDimitry Andric     CmdArgs.push_back("-fprofile-arcs");
8770b57cec5SDimitry Andric 
8780b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fcoverage_mapping,
8790b57cec5SDimitry Andric                    options::OPT_fno_coverage_mapping, false)) {
8800b57cec5SDimitry Andric     if (!ProfileGenerateArg)
8810b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_argument_only_allowed_with)
8820b57cec5SDimitry Andric           << "-fcoverage-mapping"
8830b57cec5SDimitry Andric           << "-fprofile-instr-generate";
8840b57cec5SDimitry Andric 
8850b57cec5SDimitry Andric     CmdArgs.push_back("-fcoverage-mapping");
8860b57cec5SDimitry Andric   }
8870b57cec5SDimitry Andric 
888fe6060f1SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ,
889fe6060f1SDimitry Andric                                options::OPT_fcoverage_compilation_dir_EQ)) {
890fe6060f1SDimitry Andric     if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ))
891fe6060f1SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(
892fe6060f1SDimitry Andric           Twine("-fcoverage-compilation-dir=") + A->getValue()));
893fe6060f1SDimitry Andric     else
894fe6060f1SDimitry Andric       A->render(Args, CmdArgs);
895fe6060f1SDimitry Andric   } else if (llvm::ErrorOr<std::string> CWD =
896fe6060f1SDimitry Andric                  D.getVFS().getCurrentWorkingDirectory()) {
897fe6060f1SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-fcoverage-compilation-dir=" + *CWD));
898fe6060f1SDimitry Andric   }
899fe6060f1SDimitry Andric 
9000b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) {
9010b57cec5SDimitry Andric     auto *Arg = Args.getLastArg(options::OPT_fprofile_exclude_files_EQ);
9020b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_coverage))
9030b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_argument_only_allowed_with)
9040b57cec5SDimitry Andric           << "-fprofile-exclude-files="
9050b57cec5SDimitry Andric           << "--coverage";
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric     StringRef v = Arg->getValue();
9080b57cec5SDimitry Andric     CmdArgs.push_back(
9090b57cec5SDimitry Andric         Args.MakeArgString(Twine("-fprofile-exclude-files=" + v)));
9100b57cec5SDimitry Andric   }
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fprofile_filter_files_EQ)) {
9130b57cec5SDimitry Andric     auto *Arg = Args.getLastArg(options::OPT_fprofile_filter_files_EQ);
9140b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_coverage))
9150b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_argument_only_allowed_with)
9160b57cec5SDimitry Andric           << "-fprofile-filter-files="
9170b57cec5SDimitry Andric           << "--coverage";
9180b57cec5SDimitry Andric 
9190b57cec5SDimitry Andric     StringRef v = Arg->getValue();
9200b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-filter-files=" + v)));
9210b57cec5SDimitry Andric   }
9220b57cec5SDimitry Andric 
923e8d8bef9SDimitry Andric   if (const auto *A = Args.getLastArg(options::OPT_fprofile_update_EQ)) {
924e8d8bef9SDimitry Andric     StringRef Val = A->getValue();
925e8d8bef9SDimitry Andric     if (Val == "atomic" || Val == "prefer-atomic")
926e8d8bef9SDimitry Andric       CmdArgs.push_back("-fprofile-update=atomic");
927e8d8bef9SDimitry Andric     else if (Val != "single")
928e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_unsupported_option_argument)
929e8d8bef9SDimitry Andric           << A->getOption().getName() << Val;
930e8d8bef9SDimitry Andric   } else if (TC.getSanitizerArgs().needsTsanRt()) {
931e8d8bef9SDimitry Andric     CmdArgs.push_back("-fprofile-update=atomic");
932e8d8bef9SDimitry Andric   }
933e8d8bef9SDimitry Andric 
934a7dea167SDimitry Andric   // Leave -fprofile-dir= an unused argument unless .gcda emission is
935a7dea167SDimitry Andric   // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider
936a7dea167SDimitry Andric   // the flag used. There is no -fno-profile-dir, so the user has no
937a7dea167SDimitry Andric   // targeted way to suppress the warning.
938a7dea167SDimitry Andric   Arg *FProfileDir = nullptr;
939a7dea167SDimitry Andric   if (Args.hasArg(options::OPT_fprofile_arcs) ||
940a7dea167SDimitry Andric       Args.hasArg(options::OPT_coverage))
941a7dea167SDimitry Andric     FProfileDir = Args.getLastArg(options::OPT_fprofile_dir);
942a7dea167SDimitry Andric 
943a7dea167SDimitry Andric   // Put the .gcno and .gcda files (if needed) next to the object file or
944a7dea167SDimitry Andric   // bitcode file in the case of LTO.
945a7dea167SDimitry Andric   // FIXME: There should be a simpler way to find the object file for this
946a7dea167SDimitry Andric   // input, and this code probably does the wrong thing for commands that
947a7dea167SDimitry Andric   // compile and link all at once.
948a7dea167SDimitry Andric   if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
949a7dea167SDimitry Andric       (EmitCovNotes || EmitCovData) && Output.isFilename()) {
9500b57cec5SDimitry Andric     SmallString<128> OutputFilename;
951480093f4SDimitry Andric     if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT__SLASH_Fo))
952480093f4SDimitry Andric       OutputFilename = FinalOutput->getValue();
953480093f4SDimitry Andric     else if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
9540b57cec5SDimitry Andric       OutputFilename = FinalOutput->getValue();
9550b57cec5SDimitry Andric     else
9560b57cec5SDimitry Andric       OutputFilename = llvm::sys::path::filename(Output.getBaseInput());
9570b57cec5SDimitry Andric     SmallString<128> CoverageFilename = OutputFilename;
958a7dea167SDimitry Andric     if (llvm::sys::path::is_relative(CoverageFilename))
959a7dea167SDimitry Andric       (void)D.getVFS().makeAbsolute(CoverageFilename);
9600b57cec5SDimitry Andric     llvm::sys::path::replace_extension(CoverageFilename, "gcno");
961a7dea167SDimitry Andric 
962a7dea167SDimitry Andric     CmdArgs.push_back("-coverage-notes-file");
9630b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
9640b57cec5SDimitry Andric 
965a7dea167SDimitry Andric     if (EmitCovData) {
966a7dea167SDimitry Andric       if (FProfileDir) {
9670b57cec5SDimitry Andric         CoverageFilename = FProfileDir->getValue();
9680b57cec5SDimitry Andric         llvm::sys::path::append(CoverageFilename, OutputFilename);
9690b57cec5SDimitry Andric       }
9700b57cec5SDimitry Andric       llvm::sys::path::replace_extension(CoverageFilename, "gcda");
971a7dea167SDimitry Andric       CmdArgs.push_back("-coverage-data-file");
9720b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
9730b57cec5SDimitry Andric     }
9740b57cec5SDimitry Andric   }
9750b57cec5SDimitry Andric }
9760b57cec5SDimitry Andric 
9770b57cec5SDimitry Andric /// Check whether the given input tree contains any compilation actions.
9780b57cec5SDimitry Andric static bool ContainsCompileAction(const Action *A) {
9790b57cec5SDimitry Andric   if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A))
9800b57cec5SDimitry Andric     return true;
9810b57cec5SDimitry Andric 
9820b57cec5SDimitry Andric   for (const auto &AI : A->inputs())
9830b57cec5SDimitry Andric     if (ContainsCompileAction(AI))
9840b57cec5SDimitry Andric       return true;
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric   return false;
9870b57cec5SDimitry Andric }
9880b57cec5SDimitry Andric 
9890b57cec5SDimitry Andric /// Check if -relax-all should be passed to the internal assembler.
9900b57cec5SDimitry Andric /// This is done by default when compiling non-assembler source with -O0.
9910b57cec5SDimitry Andric static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
9920b57cec5SDimitry Andric   bool RelaxDefault = true;
9930b57cec5SDimitry Andric 
9940b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_O_Group))
9950b57cec5SDimitry Andric     RelaxDefault = A->getOption().matches(options::OPT_O0);
9960b57cec5SDimitry Andric 
9970b57cec5SDimitry Andric   if (RelaxDefault) {
9980b57cec5SDimitry Andric     RelaxDefault = false;
9990b57cec5SDimitry Andric     for (const auto &Act : C.getActions()) {
10000b57cec5SDimitry Andric       if (ContainsCompileAction(Act)) {
10010b57cec5SDimitry Andric         RelaxDefault = true;
10020b57cec5SDimitry Andric         break;
10030b57cec5SDimitry Andric       }
10040b57cec5SDimitry Andric     }
10050b57cec5SDimitry Andric   }
10060b57cec5SDimitry Andric 
10070b57cec5SDimitry Andric   return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
10080b57cec5SDimitry Andric                       RelaxDefault);
10090b57cec5SDimitry Andric }
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric // Extract the integer N from a string spelled "-dwarf-N", returning 0
10120b57cec5SDimitry Andric // on mismatch. The StringRef input (rather than an Arg) allows
10130b57cec5SDimitry Andric // for use by the "-Xassembler" option parser.
10140b57cec5SDimitry Andric static unsigned DwarfVersionNum(StringRef ArgValue) {
10150b57cec5SDimitry Andric   return llvm::StringSwitch<unsigned>(ArgValue)
10160b57cec5SDimitry Andric       .Case("-gdwarf-2", 2)
10170b57cec5SDimitry Andric       .Case("-gdwarf-3", 3)
10180b57cec5SDimitry Andric       .Case("-gdwarf-4", 4)
10190b57cec5SDimitry Andric       .Case("-gdwarf-5", 5)
10200b57cec5SDimitry Andric       .Default(0);
10210b57cec5SDimitry Andric }
10220b57cec5SDimitry Andric 
1023fe6060f1SDimitry Andric // Find a DWARF format version option.
1024fe6060f1SDimitry Andric // This function is a complementary for DwarfVersionNum().
1025fe6060f1SDimitry Andric static const Arg *getDwarfNArg(const ArgList &Args) {
1026fe6060f1SDimitry Andric   return Args.getLastArg(options::OPT_gdwarf_2, options::OPT_gdwarf_3,
1027fe6060f1SDimitry Andric                          options::OPT_gdwarf_4, options::OPT_gdwarf_5,
1028fe6060f1SDimitry Andric                          options::OPT_gdwarf);
1029fe6060f1SDimitry Andric }
1030fe6060f1SDimitry Andric 
10310b57cec5SDimitry Andric static void RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs,
10320b57cec5SDimitry Andric                                     codegenoptions::DebugInfoKind DebugInfoKind,
10330b57cec5SDimitry Andric                                     unsigned DwarfVersion,
10340b57cec5SDimitry Andric                                     llvm::DebuggerKind DebuggerTuning) {
10350b57cec5SDimitry Andric   switch (DebugInfoKind) {
10360b57cec5SDimitry Andric   case codegenoptions::DebugDirectivesOnly:
10370b57cec5SDimitry Andric     CmdArgs.push_back("-debug-info-kind=line-directives-only");
10380b57cec5SDimitry Andric     break;
10390b57cec5SDimitry Andric   case codegenoptions::DebugLineTablesOnly:
10400b57cec5SDimitry Andric     CmdArgs.push_back("-debug-info-kind=line-tables-only");
10410b57cec5SDimitry Andric     break;
1042480093f4SDimitry Andric   case codegenoptions::DebugInfoConstructor:
1043480093f4SDimitry Andric     CmdArgs.push_back("-debug-info-kind=constructor");
1044480093f4SDimitry Andric     break;
10450b57cec5SDimitry Andric   case codegenoptions::LimitedDebugInfo:
10460b57cec5SDimitry Andric     CmdArgs.push_back("-debug-info-kind=limited");
10470b57cec5SDimitry Andric     break;
10480b57cec5SDimitry Andric   case codegenoptions::FullDebugInfo:
10490b57cec5SDimitry Andric     CmdArgs.push_back("-debug-info-kind=standalone");
10500b57cec5SDimitry Andric     break;
1051e8d8bef9SDimitry Andric   case codegenoptions::UnusedTypeInfo:
1052e8d8bef9SDimitry Andric     CmdArgs.push_back("-debug-info-kind=unused-types");
1053e8d8bef9SDimitry Andric     break;
10540b57cec5SDimitry Andric   default:
10550b57cec5SDimitry Andric     break;
10560b57cec5SDimitry Andric   }
10570b57cec5SDimitry Andric   if (DwarfVersion > 0)
10580b57cec5SDimitry Andric     CmdArgs.push_back(
10590b57cec5SDimitry Andric         Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion)));
10600b57cec5SDimitry Andric   switch (DebuggerTuning) {
10610b57cec5SDimitry Andric   case llvm::DebuggerKind::GDB:
10620b57cec5SDimitry Andric     CmdArgs.push_back("-debugger-tuning=gdb");
10630b57cec5SDimitry Andric     break;
10640b57cec5SDimitry Andric   case llvm::DebuggerKind::LLDB:
10650b57cec5SDimitry Andric     CmdArgs.push_back("-debugger-tuning=lldb");
10660b57cec5SDimitry Andric     break;
10670b57cec5SDimitry Andric   case llvm::DebuggerKind::SCE:
10680b57cec5SDimitry Andric     CmdArgs.push_back("-debugger-tuning=sce");
10690b57cec5SDimitry Andric     break;
1070fe6060f1SDimitry Andric   case llvm::DebuggerKind::DBX:
1071fe6060f1SDimitry Andric     CmdArgs.push_back("-debugger-tuning=dbx");
1072fe6060f1SDimitry Andric     break;
10730b57cec5SDimitry Andric   default:
10740b57cec5SDimitry Andric     break;
10750b57cec5SDimitry Andric   }
10760b57cec5SDimitry Andric }
10770b57cec5SDimitry Andric 
10780b57cec5SDimitry Andric static bool checkDebugInfoOption(const Arg *A, const ArgList &Args,
10790b57cec5SDimitry Andric                                  const Driver &D, const ToolChain &TC) {
10800b57cec5SDimitry Andric   assert(A && "Expected non-nullptr argument.");
10810b57cec5SDimitry Andric   if (TC.supportsDebugInfoOption(A))
10820b57cec5SDimitry Andric     return true;
10830b57cec5SDimitry Andric   D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target)
10840b57cec5SDimitry Andric       << A->getAsString(Args) << TC.getTripleString();
10850b57cec5SDimitry Andric   return false;
10860b57cec5SDimitry Andric }
10870b57cec5SDimitry Andric 
10880b57cec5SDimitry Andric static void RenderDebugInfoCompressionArgs(const ArgList &Args,
10890b57cec5SDimitry Andric                                            ArgStringList &CmdArgs,
10900b57cec5SDimitry Andric                                            const Driver &D,
10910b57cec5SDimitry Andric                                            const ToolChain &TC) {
1092e8d8bef9SDimitry Andric   const Arg *A = Args.getLastArg(options::OPT_gz_EQ);
10930b57cec5SDimitry Andric   if (!A)
10940b57cec5SDimitry Andric     return;
10950b57cec5SDimitry Andric   if (checkDebugInfoOption(A, Args, D, TC)) {
10960b57cec5SDimitry Andric     StringRef Value = A->getValue();
10970b57cec5SDimitry Andric     if (Value == "none") {
10980b57cec5SDimitry Andric       CmdArgs.push_back("--compress-debug-sections=none");
10990b57cec5SDimitry Andric     } else if (Value == "zlib" || Value == "zlib-gnu") {
11000b57cec5SDimitry Andric       if (llvm::zlib::isAvailable()) {
11010b57cec5SDimitry Andric         CmdArgs.push_back(
11020b57cec5SDimitry Andric             Args.MakeArgString("--compress-debug-sections=" + Twine(Value)));
11030b57cec5SDimitry Andric       } else {
11040b57cec5SDimitry Andric         D.Diag(diag::warn_debug_compression_unavailable);
11050b57cec5SDimitry Andric       }
11060b57cec5SDimitry Andric     } else {
11070b57cec5SDimitry Andric       D.Diag(diag::err_drv_unsupported_option_argument)
11080b57cec5SDimitry Andric           << A->getOption().getName() << Value;
11090b57cec5SDimitry Andric     }
11100b57cec5SDimitry Andric   }
11110b57cec5SDimitry Andric }
11120b57cec5SDimitry Andric 
11130b57cec5SDimitry Andric static const char *RelocationModelName(llvm::Reloc::Model Model) {
11140b57cec5SDimitry Andric   switch (Model) {
11150b57cec5SDimitry Andric   case llvm::Reloc::Static:
11160b57cec5SDimitry Andric     return "static";
11170b57cec5SDimitry Andric   case llvm::Reloc::PIC_:
11180b57cec5SDimitry Andric     return "pic";
11190b57cec5SDimitry Andric   case llvm::Reloc::DynamicNoPIC:
11200b57cec5SDimitry Andric     return "dynamic-no-pic";
11210b57cec5SDimitry Andric   case llvm::Reloc::ROPI:
11220b57cec5SDimitry Andric     return "ropi";
11230b57cec5SDimitry Andric   case llvm::Reloc::RWPI:
11240b57cec5SDimitry Andric     return "rwpi";
11250b57cec5SDimitry Andric   case llvm::Reloc::ROPI_RWPI:
11260b57cec5SDimitry Andric     return "ropi-rwpi";
11270b57cec5SDimitry Andric   }
11280b57cec5SDimitry Andric   llvm_unreachable("Unknown Reloc::Model kind");
11290b57cec5SDimitry Andric }
1130e8d8bef9SDimitry Andric static void handleAMDGPUCodeObjectVersionOptions(const Driver &D,
1131e8d8bef9SDimitry Andric                                                  const ArgList &Args,
1132e8d8bef9SDimitry Andric                                                  ArgStringList &CmdArgs) {
1133fe6060f1SDimitry Andric   // If no version was requested by the user, use the default value from the
1134fe6060f1SDimitry Andric   // back end. This is consistent with the value returned from
1135fe6060f1SDimitry Andric   // getAMDGPUCodeObjectVersion. This lets clang emit IR for amdgpu without
1136fe6060f1SDimitry Andric   // requiring the corresponding llvm to have the AMDGPU target enabled,
1137fe6060f1SDimitry Andric   // provided the user (e.g. front end tests) can use the default.
1138fe6060f1SDimitry Andric   if (haveAMDGPUCodeObjectVersionArgument(D, Args)) {
1139fe6060f1SDimitry Andric     unsigned CodeObjVer = getAMDGPUCodeObjectVersion(D, Args);
1140e8d8bef9SDimitry Andric     CmdArgs.insert(CmdArgs.begin() + 1,
1141e8d8bef9SDimitry Andric                    Args.MakeArgString(Twine("--amdhsa-code-object-version=") +
1142e8d8bef9SDimitry Andric                                       Twine(CodeObjVer)));
1143e8d8bef9SDimitry Andric     CmdArgs.insert(CmdArgs.begin() + 1, "-mllvm");
1144e8d8bef9SDimitry Andric   }
1145fe6060f1SDimitry Andric }
11460b57cec5SDimitry Andric 
11470b57cec5SDimitry Andric void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
11480b57cec5SDimitry Andric                                     const Driver &D, const ArgList &Args,
11490b57cec5SDimitry Andric                                     ArgStringList &CmdArgs,
11500b57cec5SDimitry Andric                                     const InputInfo &Output,
11510b57cec5SDimitry Andric                                     const InputInfoList &Inputs) const {
11520b57cec5SDimitry Andric   const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU();
11530b57cec5SDimitry Andric 
11540b57cec5SDimitry Andric   CheckPreprocessingOptions(D, Args);
11550b57cec5SDimitry Andric 
11560b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_C);
11570b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_CC);
11580b57cec5SDimitry Andric 
11590b57cec5SDimitry Andric   // Handle dependency file generation.
1160a7dea167SDimitry Andric   Arg *ArgM = Args.getLastArg(options::OPT_MM);
1161a7dea167SDimitry Andric   if (!ArgM)
1162a7dea167SDimitry Andric     ArgM = Args.getLastArg(options::OPT_M);
1163a7dea167SDimitry Andric   Arg *ArgMD = Args.getLastArg(options::OPT_MMD);
1164a7dea167SDimitry Andric   if (!ArgMD)
1165a7dea167SDimitry Andric     ArgMD = Args.getLastArg(options::OPT_MD);
1166a7dea167SDimitry Andric 
1167a7dea167SDimitry Andric   // -M and -MM imply -w.
1168a7dea167SDimitry Andric   if (ArgM)
1169a7dea167SDimitry Andric     CmdArgs.push_back("-w");
1170a7dea167SDimitry Andric   else
1171a7dea167SDimitry Andric     ArgM = ArgMD;
1172a7dea167SDimitry Andric 
1173a7dea167SDimitry Andric   if (ArgM) {
11740b57cec5SDimitry Andric     // Determine the output location.
11750b57cec5SDimitry Andric     const char *DepFile;
11760b57cec5SDimitry Andric     if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
11770b57cec5SDimitry Andric       DepFile = MF->getValue();
11780b57cec5SDimitry Andric       C.addFailureResultFile(DepFile, &JA);
11790b57cec5SDimitry Andric     } else if (Output.getType() == types::TY_Dependencies) {
11800b57cec5SDimitry Andric       DepFile = Output.getFilename();
1181a7dea167SDimitry Andric     } else if (!ArgMD) {
11820b57cec5SDimitry Andric       DepFile = "-";
11830b57cec5SDimitry Andric     } else {
11840b57cec5SDimitry Andric       DepFile = getDependencyFileName(Args, Inputs);
11850b57cec5SDimitry Andric       C.addFailureResultFile(DepFile, &JA);
11860b57cec5SDimitry Andric     }
11870b57cec5SDimitry Andric     CmdArgs.push_back("-dependency-file");
11880b57cec5SDimitry Andric     CmdArgs.push_back(DepFile);
11890b57cec5SDimitry Andric 
1190a7dea167SDimitry Andric     bool HasTarget = false;
1191a7dea167SDimitry Andric     for (const Arg *A : Args.filtered(options::OPT_MT, options::OPT_MQ)) {
1192a7dea167SDimitry Andric       HasTarget = true;
1193a7dea167SDimitry Andric       A->claim();
1194a7dea167SDimitry Andric       if (A->getOption().matches(options::OPT_MT)) {
1195a7dea167SDimitry Andric         A->render(Args, CmdArgs);
1196a7dea167SDimitry Andric       } else {
1197a7dea167SDimitry Andric         CmdArgs.push_back("-MT");
1198a7dea167SDimitry Andric         SmallString<128> Quoted;
1199a7dea167SDimitry Andric         QuoteTarget(A->getValue(), Quoted);
1200a7dea167SDimitry Andric         CmdArgs.push_back(Args.MakeArgString(Quoted));
1201a7dea167SDimitry Andric       }
1202a7dea167SDimitry Andric     }
1203a7dea167SDimitry Andric 
12040b57cec5SDimitry Andric     // Add a default target if one wasn't specified.
1205a7dea167SDimitry Andric     if (!HasTarget) {
12060b57cec5SDimitry Andric       const char *DepTarget;
12070b57cec5SDimitry Andric 
12080b57cec5SDimitry Andric       // If user provided -o, that is the dependency target, except
12090b57cec5SDimitry Andric       // when we are only generating a dependency file.
12100b57cec5SDimitry Andric       Arg *OutputOpt = Args.getLastArg(options::OPT_o);
12110b57cec5SDimitry Andric       if (OutputOpt && Output.getType() != types::TY_Dependencies) {
12120b57cec5SDimitry Andric         DepTarget = OutputOpt->getValue();
12130b57cec5SDimitry Andric       } else {
12140b57cec5SDimitry Andric         // Otherwise derive from the base input.
12150b57cec5SDimitry Andric         //
12160b57cec5SDimitry Andric         // FIXME: This should use the computed output file location.
12170b57cec5SDimitry Andric         SmallString<128> P(Inputs[0].getBaseInput());
12180b57cec5SDimitry Andric         llvm::sys::path::replace_extension(P, "o");
12190b57cec5SDimitry Andric         DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
12200b57cec5SDimitry Andric       }
12210b57cec5SDimitry Andric 
12220b57cec5SDimitry Andric       CmdArgs.push_back("-MT");
12230b57cec5SDimitry Andric       SmallString<128> Quoted;
12240b57cec5SDimitry Andric       QuoteTarget(DepTarget, Quoted);
12250b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(Quoted));
12260b57cec5SDimitry Andric     }
12270b57cec5SDimitry Andric 
1228a7dea167SDimitry Andric     if (ArgM->getOption().matches(options::OPT_M) ||
1229a7dea167SDimitry Andric         ArgM->getOption().matches(options::OPT_MD))
12300b57cec5SDimitry Andric       CmdArgs.push_back("-sys-header-deps");
12310b57cec5SDimitry Andric     if ((isa<PrecompileJobAction>(JA) &&
12320b57cec5SDimitry Andric          !Args.hasArg(options::OPT_fno_module_file_deps)) ||
12330b57cec5SDimitry Andric         Args.hasArg(options::OPT_fmodule_file_deps))
12340b57cec5SDimitry Andric       CmdArgs.push_back("-module-file-deps");
12350b57cec5SDimitry Andric   }
12360b57cec5SDimitry Andric 
12370b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_MG)) {
1238a7dea167SDimitry Andric     if (!ArgM || ArgM->getOption().matches(options::OPT_MD) ||
1239a7dea167SDimitry Andric         ArgM->getOption().matches(options::OPT_MMD))
12400b57cec5SDimitry Andric       D.Diag(diag::err_drv_mg_requires_m_or_mm);
12410b57cec5SDimitry Andric     CmdArgs.push_back("-MG");
12420b57cec5SDimitry Andric   }
12430b57cec5SDimitry Andric 
12440b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_MP);
12450b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_MV);
12460b57cec5SDimitry Andric 
12475ffd83dbSDimitry Andric   // Add offload include arguments specific for CUDA/HIP.  This must happen
12485ffd83dbSDimitry Andric   // before we -I or -include anything else, because we must pick up the
12495ffd83dbSDimitry Andric   // CUDA/HIP headers from the particular CUDA/ROCm installation, rather than
12505ffd83dbSDimitry Andric   // from e.g. /usr/local/include.
12510b57cec5SDimitry Andric   if (JA.isOffloading(Action::OFK_Cuda))
12520b57cec5SDimitry Andric     getToolChain().AddCudaIncludeArgs(Args, CmdArgs);
12535ffd83dbSDimitry Andric   if (JA.isOffloading(Action::OFK_HIP))
12545ffd83dbSDimitry Andric     getToolChain().AddHIPIncludeArgs(Args, CmdArgs);
12550b57cec5SDimitry Andric 
12560b57cec5SDimitry Andric   // If we are offloading to a target via OpenMP we need to include the
12570b57cec5SDimitry Andric   // openmp_wrappers folder which contains alternative system headers.
12580b57cec5SDimitry Andric   if (JA.isDeviceOffloading(Action::OFK_OpenMP) &&
12590b57cec5SDimitry Andric       getToolChain().getTriple().isNVPTX()){
12600b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_nobuiltininc)) {
12610b57cec5SDimitry Andric       // Add openmp_wrappers/* to our system include path.  This lets us wrap
12620b57cec5SDimitry Andric       // standard library headers.
12630b57cec5SDimitry Andric       SmallString<128> P(D.ResourceDir);
12640b57cec5SDimitry Andric       llvm::sys::path::append(P, "include");
12650b57cec5SDimitry Andric       llvm::sys::path::append(P, "openmp_wrappers");
12660b57cec5SDimitry Andric       CmdArgs.push_back("-internal-isystem");
12670b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(P));
12680b57cec5SDimitry Andric     }
12690b57cec5SDimitry Andric 
12700b57cec5SDimitry Andric     CmdArgs.push_back("-include");
12715ffd83dbSDimitry Andric     CmdArgs.push_back("__clang_openmp_device_functions.h");
12720b57cec5SDimitry Andric   }
12730b57cec5SDimitry Andric 
12740b57cec5SDimitry Andric   // Add -i* options, and automatically translate to
12750b57cec5SDimitry Andric   // -include-pch/-include-pth for transparent PCH support. It's
12760b57cec5SDimitry Andric   // wonky, but we include looking for .gch so we can support seamless
12770b57cec5SDimitry Andric   // replacement into a build system already set up to be generating
12780b57cec5SDimitry Andric   // .gch files.
12790b57cec5SDimitry Andric 
12800b57cec5SDimitry Andric   if (getToolChain().getDriver().IsCLMode()) {
12810b57cec5SDimitry Andric     const Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc);
12820b57cec5SDimitry Andric     const Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu);
12830b57cec5SDimitry Andric     if (YcArg && JA.getKind() >= Action::PrecompileJobClass &&
12840b57cec5SDimitry Andric         JA.getKind() <= Action::AssembleJobClass) {
12850b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-building-pch-with-obj"));
1286f91b0c1cSDimitry Andric       // -fpch-instantiate-templates is the default when creating
1287f91b0c1cSDimitry Andric       // precomp using /Yc
1288f91b0c1cSDimitry Andric       if (Args.hasFlag(options::OPT_fpch_instantiate_templates,
1289f91b0c1cSDimitry Andric                        options::OPT_fno_pch_instantiate_templates, true))
12905ffd83dbSDimitry Andric         CmdArgs.push_back(Args.MakeArgString("-fpch-instantiate-templates"));
12910b57cec5SDimitry Andric     }
12920b57cec5SDimitry Andric     if (YcArg || YuArg) {
12930b57cec5SDimitry Andric       StringRef ThroughHeader = YcArg ? YcArg->getValue() : YuArg->getValue();
12940b57cec5SDimitry Andric       if (!isa<PrecompileJobAction>(JA)) {
12950b57cec5SDimitry Andric         CmdArgs.push_back("-include-pch");
12960b57cec5SDimitry Andric         CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath(
12970b57cec5SDimitry Andric             C, !ThroughHeader.empty()
12980b57cec5SDimitry Andric                    ? ThroughHeader
12990b57cec5SDimitry Andric                    : llvm::sys::path::filename(Inputs[0].getBaseInput()))));
13000b57cec5SDimitry Andric       }
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric       if (ThroughHeader.empty()) {
13030b57cec5SDimitry Andric         CmdArgs.push_back(Args.MakeArgString(
13040b57cec5SDimitry Andric             Twine("-pch-through-hdrstop-") + (YcArg ? "create" : "use")));
13050b57cec5SDimitry Andric       } else {
13060b57cec5SDimitry Andric         CmdArgs.push_back(
13070b57cec5SDimitry Andric             Args.MakeArgString(Twine("-pch-through-header=") + ThroughHeader));
13080b57cec5SDimitry Andric       }
13090b57cec5SDimitry Andric     }
13100b57cec5SDimitry Andric   }
13110b57cec5SDimitry Andric 
13120b57cec5SDimitry Andric   bool RenderedImplicitInclude = false;
13130b57cec5SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) {
13140b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_include)) {
13150b57cec5SDimitry Andric       // Handling of gcc-style gch precompiled headers.
13160b57cec5SDimitry Andric       bool IsFirstImplicitInclude = !RenderedImplicitInclude;
13170b57cec5SDimitry Andric       RenderedImplicitInclude = true;
13180b57cec5SDimitry Andric 
13190b57cec5SDimitry Andric       bool FoundPCH = false;
13200b57cec5SDimitry Andric       SmallString<128> P(A->getValue());
13210b57cec5SDimitry Andric       // We want the files to have a name like foo.h.pch. Add a dummy extension
13220b57cec5SDimitry Andric       // so that replace_extension does the right thing.
13230b57cec5SDimitry Andric       P += ".dummy";
13240b57cec5SDimitry Andric       llvm::sys::path::replace_extension(P, "pch");
13250b57cec5SDimitry Andric       if (llvm::sys::fs::exists(P))
13260b57cec5SDimitry Andric         FoundPCH = true;
13270b57cec5SDimitry Andric 
13280b57cec5SDimitry Andric       if (!FoundPCH) {
13290b57cec5SDimitry Andric         llvm::sys::path::replace_extension(P, "gch");
13300b57cec5SDimitry Andric         if (llvm::sys::fs::exists(P)) {
13310b57cec5SDimitry Andric           FoundPCH = true;
13320b57cec5SDimitry Andric         }
13330b57cec5SDimitry Andric       }
13340b57cec5SDimitry Andric 
13350b57cec5SDimitry Andric       if (FoundPCH) {
13360b57cec5SDimitry Andric         if (IsFirstImplicitInclude) {
13370b57cec5SDimitry Andric           A->claim();
13380b57cec5SDimitry Andric           CmdArgs.push_back("-include-pch");
13390b57cec5SDimitry Andric           CmdArgs.push_back(Args.MakeArgString(P));
13400b57cec5SDimitry Andric           continue;
13410b57cec5SDimitry Andric         } else {
13420b57cec5SDimitry Andric           // Ignore the PCH if not first on command line and emit warning.
13430b57cec5SDimitry Andric           D.Diag(diag::warn_drv_pch_not_first_include) << P
13440b57cec5SDimitry Andric                                                        << A->getAsString(Args);
13450b57cec5SDimitry Andric         }
13460b57cec5SDimitry Andric       }
13470b57cec5SDimitry Andric     } else if (A->getOption().matches(options::OPT_isystem_after)) {
13480b57cec5SDimitry Andric       // Handling of paths which must come late.  These entries are handled by
13490b57cec5SDimitry Andric       // the toolchain itself after the resource dir is inserted in the right
13500b57cec5SDimitry Andric       // search order.
13510b57cec5SDimitry Andric       // Do not claim the argument so that the use of the argument does not
13520b57cec5SDimitry Andric       // silently go unnoticed on toolchains which do not honour the option.
13530b57cec5SDimitry Andric       continue;
1354a7dea167SDimitry Andric     } else if (A->getOption().matches(options::OPT_stdlibxx_isystem)) {
1355a7dea167SDimitry Andric       // Translated to -internal-isystem by the driver, no need to pass to cc1.
1356a7dea167SDimitry Andric       continue;
13570b57cec5SDimitry Andric     }
13580b57cec5SDimitry Andric 
13590b57cec5SDimitry Andric     // Not translated, render as usual.
13600b57cec5SDimitry Andric     A->claim();
13610b57cec5SDimitry Andric     A->render(Args, CmdArgs);
13620b57cec5SDimitry Andric   }
13630b57cec5SDimitry Andric 
13640b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs,
13650b57cec5SDimitry Andric                   {options::OPT_D, options::OPT_U, options::OPT_I_Group,
13660b57cec5SDimitry Andric                    options::OPT_F, options::OPT_index_header_map});
13670b57cec5SDimitry Andric 
13680b57cec5SDimitry Andric   // Add -Wp, and -Xpreprocessor if using the preprocessor.
13690b57cec5SDimitry Andric 
13700b57cec5SDimitry Andric   // FIXME: There is a very unfortunate problem here, some troubled
13710b57cec5SDimitry Andric   // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
13720b57cec5SDimitry Andric   // really support that we would have to parse and then translate
13730b57cec5SDimitry Andric   // those options. :(
13740b57cec5SDimitry Andric   Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
13750b57cec5SDimitry Andric                        options::OPT_Xpreprocessor);
13760b57cec5SDimitry Andric 
13770b57cec5SDimitry Andric   // -I- is a deprecated GCC feature, reject it.
13780b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_I_))
13790b57cec5SDimitry Andric     D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
13800b57cec5SDimitry Andric 
13810b57cec5SDimitry Andric   // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
13820b57cec5SDimitry Andric   // -isysroot to the CC1 invocation.
13830b57cec5SDimitry Andric   StringRef sysroot = C.getSysRoot();
13840b57cec5SDimitry Andric   if (sysroot != "") {
13850b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_isysroot)) {
13860b57cec5SDimitry Andric       CmdArgs.push_back("-isysroot");
13870b57cec5SDimitry Andric       CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
13880b57cec5SDimitry Andric     }
13890b57cec5SDimitry Andric   }
13900b57cec5SDimitry Andric 
13910b57cec5SDimitry Andric   // Parse additional include paths from environment variables.
13920b57cec5SDimitry Andric   // FIXME: We should probably sink the logic for handling these from the
13930b57cec5SDimitry Andric   // frontend into the driver. It will allow deleting 4 otherwise unused flags.
13940b57cec5SDimitry Andric   // CPATH - included following the user specified includes (but prior to
13950b57cec5SDimitry Andric   // builtin and standard includes).
13960b57cec5SDimitry Andric   addDirectoryList(Args, CmdArgs, "-I", "CPATH");
13970b57cec5SDimitry Andric   // C_INCLUDE_PATH - system includes enabled when compiling C.
13980b57cec5SDimitry Andric   addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
13990b57cec5SDimitry Andric   // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
14000b57cec5SDimitry Andric   addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
14010b57cec5SDimitry Andric   // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
14020b57cec5SDimitry Andric   addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
14030b57cec5SDimitry Andric   // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
14040b57cec5SDimitry Andric   addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
14050b57cec5SDimitry Andric 
14060b57cec5SDimitry Andric   // While adding the include arguments, we also attempt to retrieve the
14070b57cec5SDimitry Andric   // arguments of related offloading toolchains or arguments that are specific
14080b57cec5SDimitry Andric   // of an offloading programming model.
14090b57cec5SDimitry Andric 
14100b57cec5SDimitry Andric   // Add C++ include arguments, if needed.
1411a7dea167SDimitry Andric   if (types::isCXX(Inputs[0].getType())) {
1412a7dea167SDimitry Andric     bool HasStdlibxxIsystem = Args.hasArg(options::OPT_stdlibxx_isystem);
1413a7dea167SDimitry Andric     forAllAssociatedToolChains(
1414a7dea167SDimitry Andric         C, JA, getToolChain(),
1415a7dea167SDimitry Andric         [&Args, &CmdArgs, HasStdlibxxIsystem](const ToolChain &TC) {
1416a7dea167SDimitry Andric           HasStdlibxxIsystem ? TC.AddClangCXXStdlibIsystemArgs(Args, CmdArgs)
1417a7dea167SDimitry Andric                              : TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
14180b57cec5SDimitry Andric         });
1419a7dea167SDimitry Andric   }
14200b57cec5SDimitry Andric 
14210b57cec5SDimitry Andric   // Add system include arguments for all targets but IAMCU.
14220b57cec5SDimitry Andric   if (!IsIAMCU)
14230b57cec5SDimitry Andric     forAllAssociatedToolChains(C, JA, getToolChain(),
14240b57cec5SDimitry Andric                                [&Args, &CmdArgs](const ToolChain &TC) {
14250b57cec5SDimitry Andric                                  TC.AddClangSystemIncludeArgs(Args, CmdArgs);
14260b57cec5SDimitry Andric                                });
14270b57cec5SDimitry Andric   else {
14280b57cec5SDimitry Andric     // For IAMCU add special include arguments.
14290b57cec5SDimitry Andric     getToolChain().AddIAMCUIncludeArgs(Args, CmdArgs);
14300b57cec5SDimitry Andric   }
1431480093f4SDimitry Andric 
1432480093f4SDimitry Andric   addMacroPrefixMapArg(D, Args, CmdArgs);
1433fe6060f1SDimitry Andric   addCoveragePrefixMapArg(D, Args, CmdArgs);
14340b57cec5SDimitry Andric }
14350b57cec5SDimitry Andric 
14360b57cec5SDimitry Andric // FIXME: Move to target hook.
14370b57cec5SDimitry Andric static bool isSignedCharDefault(const llvm::Triple &Triple) {
14380b57cec5SDimitry Andric   switch (Triple.getArch()) {
14390b57cec5SDimitry Andric   default:
14400b57cec5SDimitry Andric     return true;
14410b57cec5SDimitry Andric 
14420b57cec5SDimitry Andric   case llvm::Triple::aarch64:
1443480093f4SDimitry Andric   case llvm::Triple::aarch64_32:
14440b57cec5SDimitry Andric   case llvm::Triple::aarch64_be:
14450b57cec5SDimitry Andric   case llvm::Triple::arm:
14460b57cec5SDimitry Andric   case llvm::Triple::armeb:
14470b57cec5SDimitry Andric   case llvm::Triple::thumb:
14480b57cec5SDimitry Andric   case llvm::Triple::thumbeb:
14490b57cec5SDimitry Andric     if (Triple.isOSDarwin() || Triple.isOSWindows())
14500b57cec5SDimitry Andric       return true;
14510b57cec5SDimitry Andric     return false;
14520b57cec5SDimitry Andric 
14530b57cec5SDimitry Andric   case llvm::Triple::ppc:
14540b57cec5SDimitry Andric   case llvm::Triple::ppc64:
14550b57cec5SDimitry Andric     if (Triple.isOSDarwin())
14560b57cec5SDimitry Andric       return true;
14570b57cec5SDimitry Andric     return false;
14580b57cec5SDimitry Andric 
14590b57cec5SDimitry Andric   case llvm::Triple::hexagon:
1460e8d8bef9SDimitry Andric   case llvm::Triple::ppcle:
14610b57cec5SDimitry Andric   case llvm::Triple::ppc64le:
14620b57cec5SDimitry Andric   case llvm::Triple::riscv32:
14630b57cec5SDimitry Andric   case llvm::Triple::riscv64:
14640b57cec5SDimitry Andric   case llvm::Triple::systemz:
14650b57cec5SDimitry Andric   case llvm::Triple::xcore:
14660b57cec5SDimitry Andric     return false;
14670b57cec5SDimitry Andric   }
14680b57cec5SDimitry Andric }
14690b57cec5SDimitry Andric 
1470480093f4SDimitry Andric static bool hasMultipleInvocations(const llvm::Triple &Triple,
1471480093f4SDimitry Andric                                    const ArgList &Args) {
1472480093f4SDimitry Andric   // Supported only on Darwin where we invoke the compiler multiple times
1473480093f4SDimitry Andric   // followed by an invocation to lipo.
1474480093f4SDimitry Andric   if (!Triple.isOSDarwin())
1475480093f4SDimitry Andric     return false;
1476480093f4SDimitry Andric   // If more than one "-arch <arch>" is specified, we're targeting multiple
1477480093f4SDimitry Andric   // architectures resulting in a fat binary.
1478480093f4SDimitry Andric   return Args.getAllArgValues(options::OPT_arch).size() > 1;
1479480093f4SDimitry Andric }
1480480093f4SDimitry Andric 
1481480093f4SDimitry Andric static bool checkRemarksOptions(const Driver &D, const ArgList &Args,
1482480093f4SDimitry Andric                                 const llvm::Triple &Triple) {
1483480093f4SDimitry Andric   // When enabling remarks, we need to error if:
1484480093f4SDimitry Andric   // * The remark file is specified but we're targeting multiple architectures,
1485480093f4SDimitry Andric   // which means more than one remark file is being generated.
1486480093f4SDimitry Andric   bool hasMultipleInvocations = ::hasMultipleInvocations(Triple, Args);
1487480093f4SDimitry Andric   bool hasExplicitOutputFile =
1488480093f4SDimitry Andric       Args.getLastArg(options::OPT_foptimization_record_file_EQ);
1489480093f4SDimitry Andric   if (hasMultipleInvocations && hasExplicitOutputFile) {
1490480093f4SDimitry Andric     D.Diag(diag::err_drv_invalid_output_with_multiple_archs)
1491480093f4SDimitry Andric         << "-foptimization-record-file";
1492480093f4SDimitry Andric     return false;
1493480093f4SDimitry Andric   }
1494480093f4SDimitry Andric   return true;
1495480093f4SDimitry Andric }
1496480093f4SDimitry Andric 
1497480093f4SDimitry Andric static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
1498480093f4SDimitry Andric                                  const llvm::Triple &Triple,
1499480093f4SDimitry Andric                                  const InputInfo &Input,
1500480093f4SDimitry Andric                                  const InputInfo &Output, const JobAction &JA) {
1501480093f4SDimitry Andric   StringRef Format = "yaml";
1502480093f4SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fsave_optimization_record_EQ))
1503480093f4SDimitry Andric     Format = A->getValue();
1504480093f4SDimitry Andric 
1505480093f4SDimitry Andric   CmdArgs.push_back("-opt-record-file");
1506480093f4SDimitry Andric 
1507480093f4SDimitry Andric   const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ);
1508480093f4SDimitry Andric   if (A) {
1509480093f4SDimitry Andric     CmdArgs.push_back(A->getValue());
1510480093f4SDimitry Andric   } else {
1511480093f4SDimitry Andric     bool hasMultipleArchs =
1512480093f4SDimitry Andric         Triple.isOSDarwin() && // Only supported on Darwin platforms.
1513480093f4SDimitry Andric         Args.getAllArgValues(options::OPT_arch).size() > 1;
1514480093f4SDimitry Andric 
1515480093f4SDimitry Andric     SmallString<128> F;
1516480093f4SDimitry Andric 
1517480093f4SDimitry Andric     if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) {
1518480093f4SDimitry Andric       if (Arg *FinalOutput = Args.getLastArg(options::OPT_o))
1519480093f4SDimitry Andric         F = FinalOutput->getValue();
1520480093f4SDimitry Andric     } else {
1521480093f4SDimitry Andric       if (Format != "yaml" && // For YAML, keep the original behavior.
1522480093f4SDimitry Andric           Triple.isOSDarwin() && // Enable this only on darwin, since it's the only platform supporting .dSYM bundles.
1523480093f4SDimitry Andric           Output.isFilename())
1524480093f4SDimitry Andric         F = Output.getFilename();
1525480093f4SDimitry Andric     }
1526480093f4SDimitry Andric 
1527480093f4SDimitry Andric     if (F.empty()) {
1528480093f4SDimitry Andric       // Use the input filename.
1529480093f4SDimitry Andric       F = llvm::sys::path::stem(Input.getBaseInput());
1530480093f4SDimitry Andric 
1531480093f4SDimitry Andric       // If we're compiling for an offload architecture (i.e. a CUDA device),
1532480093f4SDimitry Andric       // we need to make the file name for the device compilation different
1533480093f4SDimitry Andric       // from the host compilation.
1534480093f4SDimitry Andric       if (!JA.isDeviceOffloading(Action::OFK_None) &&
1535480093f4SDimitry Andric           !JA.isDeviceOffloading(Action::OFK_Host)) {
1536480093f4SDimitry Andric         llvm::sys::path::replace_extension(F, "");
1537480093f4SDimitry Andric         F += Action::GetOffloadingFileNamePrefix(JA.getOffloadingDeviceKind(),
1538480093f4SDimitry Andric                                                  Triple.normalize());
1539480093f4SDimitry Andric         F += "-";
1540480093f4SDimitry Andric         F += JA.getOffloadingArch();
1541480093f4SDimitry Andric       }
1542480093f4SDimitry Andric     }
1543480093f4SDimitry Andric 
1544480093f4SDimitry Andric     // If we're having more than one "-arch", we should name the files
1545480093f4SDimitry Andric     // differently so that every cc1 invocation writes to a different file.
1546480093f4SDimitry Andric     // We're doing that by appending "-<arch>" with "<arch>" being the arch
1547480093f4SDimitry Andric     // name from the triple.
1548480093f4SDimitry Andric     if (hasMultipleArchs) {
1549480093f4SDimitry Andric       // First, remember the extension.
1550480093f4SDimitry Andric       SmallString<64> OldExtension = llvm::sys::path::extension(F);
1551480093f4SDimitry Andric       // then, remove it.
1552480093f4SDimitry Andric       llvm::sys::path::replace_extension(F, "");
1553480093f4SDimitry Andric       // attach -<arch> to it.
1554480093f4SDimitry Andric       F += "-";
1555480093f4SDimitry Andric       F += Triple.getArchName();
1556480093f4SDimitry Andric       // put back the extension.
1557480093f4SDimitry Andric       llvm::sys::path::replace_extension(F, OldExtension);
1558480093f4SDimitry Andric     }
1559480093f4SDimitry Andric 
1560480093f4SDimitry Andric     SmallString<32> Extension;
1561480093f4SDimitry Andric     Extension += "opt.";
1562480093f4SDimitry Andric     Extension += Format;
1563480093f4SDimitry Andric 
1564480093f4SDimitry Andric     llvm::sys::path::replace_extension(F, Extension);
1565480093f4SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(F));
1566480093f4SDimitry Andric   }
1567480093f4SDimitry Andric 
1568480093f4SDimitry Andric   if (const Arg *A =
1569480093f4SDimitry Andric           Args.getLastArg(options::OPT_foptimization_record_passes_EQ)) {
1570480093f4SDimitry Andric     CmdArgs.push_back("-opt-record-passes");
1571480093f4SDimitry Andric     CmdArgs.push_back(A->getValue());
1572480093f4SDimitry Andric   }
1573480093f4SDimitry Andric 
1574480093f4SDimitry Andric   if (!Format.empty()) {
1575480093f4SDimitry Andric     CmdArgs.push_back("-opt-record-format");
1576480093f4SDimitry Andric     CmdArgs.push_back(Format.data());
1577480093f4SDimitry Andric   }
1578480093f4SDimitry Andric }
1579480093f4SDimitry Andric 
1580fe6060f1SDimitry Andric void AddAAPCSVolatileBitfieldArgs(const ArgList &Args, ArgStringList &CmdArgs) {
1581fe6060f1SDimitry Andric   if (!Args.hasFlag(options::OPT_faapcs_bitfield_width,
1582fe6060f1SDimitry Andric                     options::OPT_fno_aapcs_bitfield_width, true))
1583fe6060f1SDimitry Andric     CmdArgs.push_back("-fno-aapcs-bitfield-width");
1584fe6060f1SDimitry Andric 
1585fe6060f1SDimitry Andric   if (Args.getLastArg(options::OPT_ForceAAPCSBitfieldLoad))
1586fe6060f1SDimitry Andric     CmdArgs.push_back("-faapcs-bitfield-load");
1587fe6060f1SDimitry Andric }
1588fe6060f1SDimitry Andric 
15890b57cec5SDimitry Andric namespace {
15900b57cec5SDimitry Andric void RenderARMABI(const llvm::Triple &Triple, const ArgList &Args,
15910b57cec5SDimitry Andric                   ArgStringList &CmdArgs) {
15920b57cec5SDimitry Andric   // Select the ABI to use.
15930b57cec5SDimitry Andric   // FIXME: Support -meabi.
15940b57cec5SDimitry Andric   // FIXME: Parts of this are duplicated in the backend, unify this somehow.
15950b57cec5SDimitry Andric   const char *ABIName = nullptr;
15960b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
15970b57cec5SDimitry Andric     ABIName = A->getValue();
15980b57cec5SDimitry Andric   } else {
15990b57cec5SDimitry Andric     std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false);
16000b57cec5SDimitry Andric     ABIName = llvm::ARM::computeDefaultTargetABI(Triple, CPU).data();
16010b57cec5SDimitry Andric   }
16020b57cec5SDimitry Andric 
16030b57cec5SDimitry Andric   CmdArgs.push_back("-target-abi");
16040b57cec5SDimitry Andric   CmdArgs.push_back(ABIName);
16050b57cec5SDimitry Andric }
16060b57cec5SDimitry Andric }
16070b57cec5SDimitry Andric 
16080b57cec5SDimitry Andric void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args,
16090b57cec5SDimitry Andric                              ArgStringList &CmdArgs, bool KernelOrKext) const {
16100b57cec5SDimitry Andric   RenderARMABI(Triple, Args, CmdArgs);
16110b57cec5SDimitry Andric 
16120b57cec5SDimitry Andric   // Determine floating point ABI from the options & target defaults.
16130b57cec5SDimitry Andric   arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args);
16140b57cec5SDimitry Andric   if (ABI == arm::FloatABI::Soft) {
16150b57cec5SDimitry Andric     // Floating point operations and argument passing are soft.
16160b57cec5SDimitry Andric     // FIXME: This changes CPP defines, we need -target-soft-float.
16170b57cec5SDimitry Andric     CmdArgs.push_back("-msoft-float");
16180b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
16190b57cec5SDimitry Andric     CmdArgs.push_back("soft");
16200b57cec5SDimitry Andric   } else if (ABI == arm::FloatABI::SoftFP) {
16210b57cec5SDimitry Andric     // Floating point operations are hard, but argument passing is soft.
16220b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
16230b57cec5SDimitry Andric     CmdArgs.push_back("soft");
16240b57cec5SDimitry Andric   } else {
16250b57cec5SDimitry Andric     // Floating point operations and argument passing are hard.
16260b57cec5SDimitry Andric     assert(ABI == arm::FloatABI::Hard && "Invalid float abi!");
16270b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
16280b57cec5SDimitry Andric     CmdArgs.push_back("hard");
16290b57cec5SDimitry Andric   }
16300b57cec5SDimitry Andric 
16310b57cec5SDimitry Andric   // Forward the -mglobal-merge option for explicit control over the pass.
16320b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
16330b57cec5SDimitry Andric                                options::OPT_mno_global_merge)) {
16340b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
16350b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mno_global_merge))
16360b57cec5SDimitry Andric       CmdArgs.push_back("-arm-global-merge=false");
16370b57cec5SDimitry Andric     else
16380b57cec5SDimitry Andric       CmdArgs.push_back("-arm-global-merge=true");
16390b57cec5SDimitry Andric   }
16400b57cec5SDimitry Andric 
16410b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_mimplicit_float,
16420b57cec5SDimitry Andric                     options::OPT_mno_implicit_float, true))
16430b57cec5SDimitry Andric     CmdArgs.push_back("-no-implicit-float");
16440b57cec5SDimitry Andric 
16450b57cec5SDimitry Andric   if (Args.getLastArg(options::OPT_mcmse))
16460b57cec5SDimitry Andric     CmdArgs.push_back("-mcmse");
1647fe6060f1SDimitry Andric 
1648fe6060f1SDimitry Andric   AddAAPCSVolatileBitfieldArgs(Args, CmdArgs);
16490b57cec5SDimitry Andric }
16500b57cec5SDimitry Andric 
16510b57cec5SDimitry Andric void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple,
16520b57cec5SDimitry Andric                                 const ArgList &Args, bool KernelOrKext,
16530b57cec5SDimitry Andric                                 ArgStringList &CmdArgs) const {
16540b57cec5SDimitry Andric   const ToolChain &TC = getToolChain();
16550b57cec5SDimitry Andric 
16560b57cec5SDimitry Andric   // Add the target features
16575ffd83dbSDimitry Andric   getTargetFeatures(TC.getDriver(), EffectiveTriple, Args, CmdArgs, false);
16580b57cec5SDimitry Andric 
16590b57cec5SDimitry Andric   // Add target specific flags.
16600b57cec5SDimitry Andric   switch (TC.getArch()) {
16610b57cec5SDimitry Andric   default:
16620b57cec5SDimitry Andric     break;
16630b57cec5SDimitry Andric 
16640b57cec5SDimitry Andric   case llvm::Triple::arm:
16650b57cec5SDimitry Andric   case llvm::Triple::armeb:
16660b57cec5SDimitry Andric   case llvm::Triple::thumb:
16670b57cec5SDimitry Andric   case llvm::Triple::thumbeb:
16680b57cec5SDimitry Andric     // Use the effective triple, which takes into account the deployment target.
16690b57cec5SDimitry Andric     AddARMTargetArgs(EffectiveTriple, Args, CmdArgs, KernelOrKext);
16700b57cec5SDimitry Andric     CmdArgs.push_back("-fallow-half-arguments-and-returns");
16710b57cec5SDimitry Andric     break;
16720b57cec5SDimitry Andric 
16730b57cec5SDimitry Andric   case llvm::Triple::aarch64:
1674480093f4SDimitry Andric   case llvm::Triple::aarch64_32:
16750b57cec5SDimitry Andric   case llvm::Triple::aarch64_be:
16760b57cec5SDimitry Andric     AddAArch64TargetArgs(Args, CmdArgs);
16770b57cec5SDimitry Andric     CmdArgs.push_back("-fallow-half-arguments-and-returns");
16780b57cec5SDimitry Andric     break;
16790b57cec5SDimitry Andric 
16800b57cec5SDimitry Andric   case llvm::Triple::mips:
16810b57cec5SDimitry Andric   case llvm::Triple::mipsel:
16820b57cec5SDimitry Andric   case llvm::Triple::mips64:
16830b57cec5SDimitry Andric   case llvm::Triple::mips64el:
16840b57cec5SDimitry Andric     AddMIPSTargetArgs(Args, CmdArgs);
16850b57cec5SDimitry Andric     break;
16860b57cec5SDimitry Andric 
16870b57cec5SDimitry Andric   case llvm::Triple::ppc:
1688e8d8bef9SDimitry Andric   case llvm::Triple::ppcle:
16890b57cec5SDimitry Andric   case llvm::Triple::ppc64:
16900b57cec5SDimitry Andric   case llvm::Triple::ppc64le:
16910b57cec5SDimitry Andric     AddPPCTargetArgs(Args, CmdArgs);
16920b57cec5SDimitry Andric     break;
16930b57cec5SDimitry Andric 
16940b57cec5SDimitry Andric   case llvm::Triple::riscv32:
16950b57cec5SDimitry Andric   case llvm::Triple::riscv64:
16960b57cec5SDimitry Andric     AddRISCVTargetArgs(Args, CmdArgs);
16970b57cec5SDimitry Andric     break;
16980b57cec5SDimitry Andric 
16990b57cec5SDimitry Andric   case llvm::Triple::sparc:
17000b57cec5SDimitry Andric   case llvm::Triple::sparcel:
17010b57cec5SDimitry Andric   case llvm::Triple::sparcv9:
17020b57cec5SDimitry Andric     AddSparcTargetArgs(Args, CmdArgs);
17030b57cec5SDimitry Andric     break;
17040b57cec5SDimitry Andric 
17050b57cec5SDimitry Andric   case llvm::Triple::systemz:
17060b57cec5SDimitry Andric     AddSystemZTargetArgs(Args, CmdArgs);
17070b57cec5SDimitry Andric     break;
17080b57cec5SDimitry Andric 
17090b57cec5SDimitry Andric   case llvm::Triple::x86:
17100b57cec5SDimitry Andric   case llvm::Triple::x86_64:
17110b57cec5SDimitry Andric     AddX86TargetArgs(Args, CmdArgs);
17120b57cec5SDimitry Andric     break;
17130b57cec5SDimitry Andric 
17140b57cec5SDimitry Andric   case llvm::Triple::lanai:
17150b57cec5SDimitry Andric     AddLanaiTargetArgs(Args, CmdArgs);
17160b57cec5SDimitry Andric     break;
17170b57cec5SDimitry Andric 
17180b57cec5SDimitry Andric   case llvm::Triple::hexagon:
17190b57cec5SDimitry Andric     AddHexagonTargetArgs(Args, CmdArgs);
17200b57cec5SDimitry Andric     break;
17210b57cec5SDimitry Andric 
17220b57cec5SDimitry Andric   case llvm::Triple::wasm32:
17230b57cec5SDimitry Andric   case llvm::Triple::wasm64:
17240b57cec5SDimitry Andric     AddWebAssemblyTargetArgs(Args, CmdArgs);
17250b57cec5SDimitry Andric     break;
17265ffd83dbSDimitry Andric 
17275ffd83dbSDimitry Andric   case llvm::Triple::ve:
17285ffd83dbSDimitry Andric     AddVETargetArgs(Args, CmdArgs);
17295ffd83dbSDimitry Andric     break;
17300b57cec5SDimitry Andric   }
17310b57cec5SDimitry Andric }
17320b57cec5SDimitry Andric 
17330b57cec5SDimitry Andric namespace {
17340b57cec5SDimitry Andric void RenderAArch64ABI(const llvm::Triple &Triple, const ArgList &Args,
17350b57cec5SDimitry Andric                       ArgStringList &CmdArgs) {
17360b57cec5SDimitry Andric   const char *ABIName = nullptr;
17370b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
17380b57cec5SDimitry Andric     ABIName = A->getValue();
17390b57cec5SDimitry Andric   else if (Triple.isOSDarwin())
17400b57cec5SDimitry Andric     ABIName = "darwinpcs";
17410b57cec5SDimitry Andric   else
17420b57cec5SDimitry Andric     ABIName = "aapcs";
17430b57cec5SDimitry Andric 
17440b57cec5SDimitry Andric   CmdArgs.push_back("-target-abi");
17450b57cec5SDimitry Andric   CmdArgs.push_back(ABIName);
17460b57cec5SDimitry Andric }
17470b57cec5SDimitry Andric }
17480b57cec5SDimitry Andric 
17490b57cec5SDimitry Andric void Clang::AddAArch64TargetArgs(const ArgList &Args,
17500b57cec5SDimitry Andric                                  ArgStringList &CmdArgs) const {
17510b57cec5SDimitry Andric   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
17520b57cec5SDimitry Andric 
17530b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
17540b57cec5SDimitry Andric       Args.hasArg(options::OPT_mkernel) ||
17550b57cec5SDimitry Andric       Args.hasArg(options::OPT_fapple_kext))
17560b57cec5SDimitry Andric     CmdArgs.push_back("-disable-red-zone");
17570b57cec5SDimitry Andric 
17580b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_mimplicit_float,
17590b57cec5SDimitry Andric                     options::OPT_mno_implicit_float, true))
17600b57cec5SDimitry Andric     CmdArgs.push_back("-no-implicit-float");
17610b57cec5SDimitry Andric 
17620b57cec5SDimitry Andric   RenderAArch64ABI(Triple, Args, CmdArgs);
17630b57cec5SDimitry Andric 
17640b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a53_835769,
17650b57cec5SDimitry Andric                                options::OPT_mno_fix_cortex_a53_835769)) {
17660b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
17670b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mfix_cortex_a53_835769))
17680b57cec5SDimitry Andric       CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
17690b57cec5SDimitry Andric     else
17700b57cec5SDimitry Andric       CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=0");
17710b57cec5SDimitry Andric   } else if (Triple.isAndroid()) {
17720b57cec5SDimitry Andric     // Enabled A53 errata (835769) workaround by default on android
17730b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
17740b57cec5SDimitry Andric     CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
17750b57cec5SDimitry Andric   }
17760b57cec5SDimitry Andric 
17770b57cec5SDimitry Andric   // Forward the -mglobal-merge option for explicit control over the pass.
17780b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
17790b57cec5SDimitry Andric                                options::OPT_mno_global_merge)) {
17800b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
17810b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mno_global_merge))
17820b57cec5SDimitry Andric       CmdArgs.push_back("-aarch64-enable-global-merge=false");
17830b57cec5SDimitry Andric     else
17840b57cec5SDimitry Andric       CmdArgs.push_back("-aarch64-enable-global-merge=true");
17850b57cec5SDimitry Andric   }
17860b57cec5SDimitry Andric 
17870b57cec5SDimitry Andric   // Enable/disable return address signing and indirect branch targets.
17880b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_msign_return_address_EQ,
17890b57cec5SDimitry Andric                                options::OPT_mbranch_protection_EQ)) {
17900b57cec5SDimitry Andric 
17910b57cec5SDimitry Andric     const Driver &D = getToolChain().getDriver();
17920b57cec5SDimitry Andric 
17930b57cec5SDimitry Andric     StringRef Scope, Key;
17940b57cec5SDimitry Andric     bool IndirectBranches;
17950b57cec5SDimitry Andric 
17960b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_msign_return_address_EQ)) {
17970b57cec5SDimitry Andric       Scope = A->getValue();
17980b57cec5SDimitry Andric       if (!Scope.equals("none") && !Scope.equals("non-leaf") &&
17990b57cec5SDimitry Andric           !Scope.equals("all"))
18000b57cec5SDimitry Andric         D.Diag(diag::err_invalid_branch_protection)
18010b57cec5SDimitry Andric             << Scope << A->getAsString(Args);
18020b57cec5SDimitry Andric       Key = "a_key";
18030b57cec5SDimitry Andric       IndirectBranches = false;
1804480093f4SDimitry Andric     } else {
1805480093f4SDimitry Andric       StringRef Err;
1806480093f4SDimitry Andric       llvm::AArch64::ParsedBranchProtection PBP;
1807480093f4SDimitry Andric       if (!llvm::AArch64::parseBranchProtection(A->getValue(), PBP, Err))
1808480093f4SDimitry Andric         D.Diag(diag::err_invalid_branch_protection)
1809480093f4SDimitry Andric             << Err << A->getAsString(Args);
1810480093f4SDimitry Andric       Scope = PBP.Scope;
1811480093f4SDimitry Andric       Key = PBP.Key;
1812480093f4SDimitry Andric       IndirectBranches = PBP.BranchTargetEnforcement;
1813480093f4SDimitry Andric     }
18140b57cec5SDimitry Andric 
18150b57cec5SDimitry Andric     CmdArgs.push_back(
18160b57cec5SDimitry Andric         Args.MakeArgString(Twine("-msign-return-address=") + Scope));
18170b57cec5SDimitry Andric     CmdArgs.push_back(
18180b57cec5SDimitry Andric         Args.MakeArgString(Twine("-msign-return-address-key=") + Key));
18190b57cec5SDimitry Andric     if (IndirectBranches)
18200b57cec5SDimitry Andric       CmdArgs.push_back("-mbranch-target-enforce");
18210b57cec5SDimitry Andric   }
1822e8d8bef9SDimitry Andric 
1823e8d8bef9SDimitry Andric   // Handle -msve_vector_bits=<bits>
1824e8d8bef9SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) {
1825e8d8bef9SDimitry Andric     StringRef Val = A->getValue();
1826e8d8bef9SDimitry Andric     const Driver &D = getToolChain().getDriver();
1827e8d8bef9SDimitry Andric     if (Val.equals("128") || Val.equals("256") || Val.equals("512") ||
1828e8d8bef9SDimitry Andric         Val.equals("1024") || Val.equals("2048"))
1829e8d8bef9SDimitry Andric       CmdArgs.push_back(
1830e8d8bef9SDimitry Andric           Args.MakeArgString(llvm::Twine("-msve-vector-bits=") + Val));
1831e8d8bef9SDimitry Andric     // Silently drop requests for vector-length agnostic code as it's implied.
1832e8d8bef9SDimitry Andric     else if (!Val.equals("scalable"))
1833e8d8bef9SDimitry Andric       // Handle the unsupported values passed to msve-vector-bits.
1834e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_unsupported_option_argument)
1835e8d8bef9SDimitry Andric           << A->getOption().getName() << Val;
1836e8d8bef9SDimitry Andric   }
1837fe6060f1SDimitry Andric 
1838fe6060f1SDimitry Andric   AddAAPCSVolatileBitfieldArgs(Args, CmdArgs);
18390b57cec5SDimitry Andric }
18400b57cec5SDimitry Andric 
18410b57cec5SDimitry Andric void Clang::AddMIPSTargetArgs(const ArgList &Args,
18420b57cec5SDimitry Andric                               ArgStringList &CmdArgs) const {
18430b57cec5SDimitry Andric   const Driver &D = getToolChain().getDriver();
18440b57cec5SDimitry Andric   StringRef CPUName;
18450b57cec5SDimitry Andric   StringRef ABIName;
18460b57cec5SDimitry Andric   const llvm::Triple &Triple = getToolChain().getTriple();
18470b57cec5SDimitry Andric   mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
18480b57cec5SDimitry Andric 
18490b57cec5SDimitry Andric   CmdArgs.push_back("-target-abi");
18500b57cec5SDimitry Andric   CmdArgs.push_back(ABIName.data());
18510b57cec5SDimitry Andric 
18520b57cec5SDimitry Andric   mips::FloatABI ABI = mips::getMipsFloatABI(D, Args, Triple);
18530b57cec5SDimitry Andric   if (ABI == mips::FloatABI::Soft) {
18540b57cec5SDimitry Andric     // Floating point operations and argument passing are soft.
18550b57cec5SDimitry Andric     CmdArgs.push_back("-msoft-float");
18560b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
18570b57cec5SDimitry Andric     CmdArgs.push_back("soft");
18580b57cec5SDimitry Andric   } else {
18590b57cec5SDimitry Andric     // Floating point operations and argument passing are hard.
18600b57cec5SDimitry Andric     assert(ABI == mips::FloatABI::Hard && "Invalid float abi!");
18610b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
18620b57cec5SDimitry Andric     CmdArgs.push_back("hard");
18630b57cec5SDimitry Andric   }
18640b57cec5SDimitry Andric 
18650b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1,
18660b57cec5SDimitry Andric                                options::OPT_mno_ldc1_sdc1)) {
18670b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) {
18680b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
18690b57cec5SDimitry Andric       CmdArgs.push_back("-mno-ldc1-sdc1");
18700b57cec5SDimitry Andric     }
18710b57cec5SDimitry Andric   }
18720b57cec5SDimitry Andric 
18730b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division,
18740b57cec5SDimitry Andric                                options::OPT_mno_check_zero_division)) {
18750b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mno_check_zero_division)) {
18760b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
18770b57cec5SDimitry Andric       CmdArgs.push_back("-mno-check-zero-division");
18780b57cec5SDimitry Andric     }
18790b57cec5SDimitry Andric   }
18800b57cec5SDimitry Andric 
18810b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_G)) {
18820b57cec5SDimitry Andric     StringRef v = A->getValue();
18830b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
18840b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
18850b57cec5SDimitry Andric     A->claim();
18860b57cec5SDimitry Andric   }
18870b57cec5SDimitry Andric 
18880b57cec5SDimitry Andric   Arg *GPOpt = Args.getLastArg(options::OPT_mgpopt, options::OPT_mno_gpopt);
18890b57cec5SDimitry Andric   Arg *ABICalls =
18900b57cec5SDimitry Andric       Args.getLastArg(options::OPT_mabicalls, options::OPT_mno_abicalls);
18910b57cec5SDimitry Andric 
18920b57cec5SDimitry Andric   // -mabicalls is the default for many MIPS environments, even with -fno-pic.
18930b57cec5SDimitry Andric   // -mgpopt is the default for static, -fno-pic environments but these two
18940b57cec5SDimitry Andric   // options conflict. We want to be certain that -mno-abicalls -mgpopt is
18950b57cec5SDimitry Andric   // the only case where -mllvm -mgpopt is passed.
18960b57cec5SDimitry Andric   // NOTE: We need a warning here or in the backend to warn when -mgpopt is
18970b57cec5SDimitry Andric   //       passed explicitly when compiling something with -mabicalls
18980b57cec5SDimitry Andric   //       (implictly) in affect. Currently the warning is in the backend.
18990b57cec5SDimitry Andric   //
19000b57cec5SDimitry Andric   // When the ABI in use is  N64, we also need to determine the PIC mode that
19010b57cec5SDimitry Andric   // is in use, as -fno-pic for N64 implies -mno-abicalls.
19020b57cec5SDimitry Andric   bool NoABICalls =
19030b57cec5SDimitry Andric       ABICalls && ABICalls->getOption().matches(options::OPT_mno_abicalls);
19040b57cec5SDimitry Andric 
19050b57cec5SDimitry Andric   llvm::Reloc::Model RelocationModel;
19060b57cec5SDimitry Andric   unsigned PICLevel;
19070b57cec5SDimitry Andric   bool IsPIE;
19080b57cec5SDimitry Andric   std::tie(RelocationModel, PICLevel, IsPIE) =
19090b57cec5SDimitry Andric       ParsePICArgs(getToolChain(), Args);
19100b57cec5SDimitry Andric 
19110b57cec5SDimitry Andric   NoABICalls = NoABICalls ||
19120b57cec5SDimitry Andric                (RelocationModel == llvm::Reloc::Static && ABIName == "n64");
19130b57cec5SDimitry Andric 
19140b57cec5SDimitry Andric   bool WantGPOpt = GPOpt && GPOpt->getOption().matches(options::OPT_mgpopt);
19150b57cec5SDimitry Andric   // We quietly ignore -mno-gpopt as the backend defaults to -mno-gpopt.
19160b57cec5SDimitry Andric   if (NoABICalls && (!GPOpt || WantGPOpt)) {
19170b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
19180b57cec5SDimitry Andric     CmdArgs.push_back("-mgpopt");
19190b57cec5SDimitry Andric 
19200b57cec5SDimitry Andric     Arg *LocalSData = Args.getLastArg(options::OPT_mlocal_sdata,
19210b57cec5SDimitry Andric                                       options::OPT_mno_local_sdata);
19220b57cec5SDimitry Andric     Arg *ExternSData = Args.getLastArg(options::OPT_mextern_sdata,
19230b57cec5SDimitry Andric                                        options::OPT_mno_extern_sdata);
19240b57cec5SDimitry Andric     Arg *EmbeddedData = Args.getLastArg(options::OPT_membedded_data,
19250b57cec5SDimitry Andric                                         options::OPT_mno_embedded_data);
19260b57cec5SDimitry Andric     if (LocalSData) {
19270b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
19280b57cec5SDimitry Andric       if (LocalSData->getOption().matches(options::OPT_mlocal_sdata)) {
19290b57cec5SDimitry Andric         CmdArgs.push_back("-mlocal-sdata=1");
19300b57cec5SDimitry Andric       } else {
19310b57cec5SDimitry Andric         CmdArgs.push_back("-mlocal-sdata=0");
19320b57cec5SDimitry Andric       }
19330b57cec5SDimitry Andric       LocalSData->claim();
19340b57cec5SDimitry Andric     }
19350b57cec5SDimitry Andric 
19360b57cec5SDimitry Andric     if (ExternSData) {
19370b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
19380b57cec5SDimitry Andric       if (ExternSData->getOption().matches(options::OPT_mextern_sdata)) {
19390b57cec5SDimitry Andric         CmdArgs.push_back("-mextern-sdata=1");
19400b57cec5SDimitry Andric       } else {
19410b57cec5SDimitry Andric         CmdArgs.push_back("-mextern-sdata=0");
19420b57cec5SDimitry Andric       }
19430b57cec5SDimitry Andric       ExternSData->claim();
19440b57cec5SDimitry Andric     }
19450b57cec5SDimitry Andric 
19460b57cec5SDimitry Andric     if (EmbeddedData) {
19470b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
19480b57cec5SDimitry Andric       if (EmbeddedData->getOption().matches(options::OPT_membedded_data)) {
19490b57cec5SDimitry Andric         CmdArgs.push_back("-membedded-data=1");
19500b57cec5SDimitry Andric       } else {
19510b57cec5SDimitry Andric         CmdArgs.push_back("-membedded-data=0");
19520b57cec5SDimitry Andric       }
19530b57cec5SDimitry Andric       EmbeddedData->claim();
19540b57cec5SDimitry Andric     }
19550b57cec5SDimitry Andric 
19560b57cec5SDimitry Andric   } else if ((!ABICalls || (!NoABICalls && ABICalls)) && WantGPOpt)
19570b57cec5SDimitry Andric     D.Diag(diag::warn_drv_unsupported_gpopt) << (ABICalls ? 0 : 1);
19580b57cec5SDimitry Andric 
19590b57cec5SDimitry Andric   if (GPOpt)
19600b57cec5SDimitry Andric     GPOpt->claim();
19610b57cec5SDimitry Andric 
19620b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mcompact_branches_EQ)) {
19630b57cec5SDimitry Andric     StringRef Val = StringRef(A->getValue());
19640b57cec5SDimitry Andric     if (mips::hasCompactBranches(CPUName)) {
19650b57cec5SDimitry Andric       if (Val == "never" || Val == "always" || Val == "optimal") {
19660b57cec5SDimitry Andric         CmdArgs.push_back("-mllvm");
19670b57cec5SDimitry Andric         CmdArgs.push_back(Args.MakeArgString("-mips-compact-branches=" + Val));
19680b57cec5SDimitry Andric       } else
19690b57cec5SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
19700b57cec5SDimitry Andric             << A->getOption().getName() << Val;
19710b57cec5SDimitry Andric     } else
19720b57cec5SDimitry Andric       D.Diag(diag::warn_target_unsupported_compact_branches) << CPUName;
19730b57cec5SDimitry Andric   }
19740b57cec5SDimitry Andric 
19750b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mrelax_pic_calls,
19760b57cec5SDimitry Andric                                options::OPT_mno_relax_pic_calls)) {
19770b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mno_relax_pic_calls)) {
19780b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
19790b57cec5SDimitry Andric       CmdArgs.push_back("-mips-jalr-reloc=0");
19800b57cec5SDimitry Andric     }
19810b57cec5SDimitry Andric   }
19820b57cec5SDimitry Andric }
19830b57cec5SDimitry Andric 
19840b57cec5SDimitry Andric void Clang::AddPPCTargetArgs(const ArgList &Args,
19850b57cec5SDimitry Andric                              ArgStringList &CmdArgs) const {
19860b57cec5SDimitry Andric   // Select the ABI to use.
19870b57cec5SDimitry Andric   const char *ABIName = nullptr;
1988480093f4SDimitry Andric   const llvm::Triple &T = getToolChain().getTriple();
1989480093f4SDimitry Andric   if (T.isOSBinFormatELF()) {
19900b57cec5SDimitry Andric     switch (getToolChain().getArch()) {
19910b57cec5SDimitry Andric     case llvm::Triple::ppc64: {
199275b4d546SDimitry Andric       if ((T.isOSFreeBSD() && T.getOSMajorVersion() >= 13) ||
199375b4d546SDimitry Andric           T.isOSOpenBSD() || T.isMusl())
1994480093f4SDimitry Andric         ABIName = "elfv2";
1995480093f4SDimitry Andric       else
19960b57cec5SDimitry Andric         ABIName = "elfv1";
19970b57cec5SDimitry Andric       break;
19980b57cec5SDimitry Andric     }
19990b57cec5SDimitry Andric     case llvm::Triple::ppc64le:
20000b57cec5SDimitry Andric       ABIName = "elfv2";
20010b57cec5SDimitry Andric       break;
20020b57cec5SDimitry Andric     default:
20030b57cec5SDimitry Andric       break;
20040b57cec5SDimitry Andric     }
2005480093f4SDimitry Andric   }
20060b57cec5SDimitry Andric 
20070b57cec5SDimitry Andric   bool IEEELongDouble = false;
20080b57cec5SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_mabi_EQ)) {
20090b57cec5SDimitry Andric     StringRef V = A->getValue();
20100b57cec5SDimitry Andric     if (V == "ieeelongdouble")
20110b57cec5SDimitry Andric       IEEELongDouble = true;
20120b57cec5SDimitry Andric     else if (V == "ibmlongdouble")
20130b57cec5SDimitry Andric       IEEELongDouble = false;
20140b57cec5SDimitry Andric     else if (V != "altivec")
20150b57cec5SDimitry Andric       // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore
20160b57cec5SDimitry Andric       // the option if given as we don't have backend support for any targets
20170b57cec5SDimitry Andric       // that don't use the altivec abi.
20180b57cec5SDimitry Andric       ABIName = A->getValue();
20190b57cec5SDimitry Andric   }
20200b57cec5SDimitry Andric   if (IEEELongDouble)
20210b57cec5SDimitry Andric     CmdArgs.push_back("-mabi=ieeelongdouble");
20220b57cec5SDimitry Andric 
20230b57cec5SDimitry Andric   ppc::FloatABI FloatABI =
20240b57cec5SDimitry Andric       ppc::getPPCFloatABI(getToolChain().getDriver(), Args);
20250b57cec5SDimitry Andric 
20260b57cec5SDimitry Andric   if (FloatABI == ppc::FloatABI::Soft) {
20270b57cec5SDimitry Andric     // Floating point operations and argument passing are soft.
20280b57cec5SDimitry Andric     CmdArgs.push_back("-msoft-float");
20290b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
20300b57cec5SDimitry Andric     CmdArgs.push_back("soft");
20310b57cec5SDimitry Andric   } else {
20320b57cec5SDimitry Andric     // Floating point operations and argument passing are hard.
20330b57cec5SDimitry Andric     assert(FloatABI == ppc::FloatABI::Hard && "Invalid float abi!");
20340b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
20350b57cec5SDimitry Andric     CmdArgs.push_back("hard");
20360b57cec5SDimitry Andric   }
20370b57cec5SDimitry Andric 
20380b57cec5SDimitry Andric   if (ABIName) {
20390b57cec5SDimitry Andric     CmdArgs.push_back("-target-abi");
20400b57cec5SDimitry Andric     CmdArgs.push_back(ABIName);
20410b57cec5SDimitry Andric   }
20420b57cec5SDimitry Andric }
20430b57cec5SDimitry Andric 
20445ffd83dbSDimitry Andric static void SetRISCVSmallDataLimit(const ToolChain &TC, const ArgList &Args,
20455ffd83dbSDimitry Andric                                    ArgStringList &CmdArgs) {
20465ffd83dbSDimitry Andric   const Driver &D = TC.getDriver();
20475ffd83dbSDimitry Andric   const llvm::Triple &Triple = TC.getTriple();
20485ffd83dbSDimitry Andric   // Default small data limitation is eight.
20495ffd83dbSDimitry Andric   const char *SmallDataLimit = "8";
20505ffd83dbSDimitry Andric   // Get small data limitation.
20515ffd83dbSDimitry Andric   if (Args.getLastArg(options::OPT_shared, options::OPT_fpic,
20525ffd83dbSDimitry Andric                       options::OPT_fPIC)) {
20535ffd83dbSDimitry Andric     // Not support linker relaxation for PIC.
20545ffd83dbSDimitry Andric     SmallDataLimit = "0";
20555ffd83dbSDimitry Andric     if (Args.hasArg(options::OPT_G)) {
20565ffd83dbSDimitry Andric       D.Diag(diag::warn_drv_unsupported_sdata);
20575ffd83dbSDimitry Andric     }
20585ffd83dbSDimitry Andric   } else if (Args.getLastArgValue(options::OPT_mcmodel_EQ)
2059fe6060f1SDimitry Andric                  .equals_insensitive("large") &&
20605ffd83dbSDimitry Andric              (Triple.getArch() == llvm::Triple::riscv64)) {
20615ffd83dbSDimitry Andric     // Not support linker relaxation for RV64 with large code model.
20625ffd83dbSDimitry Andric     SmallDataLimit = "0";
20635ffd83dbSDimitry Andric     if (Args.hasArg(options::OPT_G)) {
20645ffd83dbSDimitry Andric       D.Diag(diag::warn_drv_unsupported_sdata);
20655ffd83dbSDimitry Andric     }
20665ffd83dbSDimitry Andric   } else if (Arg *A = Args.getLastArg(options::OPT_G)) {
20675ffd83dbSDimitry Andric     SmallDataLimit = A->getValue();
20685ffd83dbSDimitry Andric   }
20695ffd83dbSDimitry Andric   // Forward the -msmall-data-limit= option.
20705ffd83dbSDimitry Andric   CmdArgs.push_back("-msmall-data-limit");
20715ffd83dbSDimitry Andric   CmdArgs.push_back(SmallDataLimit);
20725ffd83dbSDimitry Andric }
20735ffd83dbSDimitry Andric 
20740b57cec5SDimitry Andric void Clang::AddRISCVTargetArgs(const ArgList &Args,
20750b57cec5SDimitry Andric                                ArgStringList &CmdArgs) const {
20760b57cec5SDimitry Andric   const llvm::Triple &Triple = getToolChain().getTriple();
2077a7dea167SDimitry Andric   StringRef ABIName = riscv::getRISCVABI(Args, Triple);
20780b57cec5SDimitry Andric 
20790b57cec5SDimitry Andric   CmdArgs.push_back("-target-abi");
2080a7dea167SDimitry Andric   CmdArgs.push_back(ABIName.data());
20815ffd83dbSDimitry Andric 
20825ffd83dbSDimitry Andric   SetRISCVSmallDataLimit(getToolChain(), Args, CmdArgs);
2083e8d8bef9SDimitry Andric 
2084e8d8bef9SDimitry Andric   std::string TuneCPU;
2085e8d8bef9SDimitry Andric 
2086e8d8bef9SDimitry Andric   if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) {
2087e8d8bef9SDimitry Andric     StringRef Name = A->getValue();
2088e8d8bef9SDimitry Andric 
2089e8d8bef9SDimitry Andric     Name = llvm::RISCV::resolveTuneCPUAlias(Name, Triple.isArch64Bit());
2090e8d8bef9SDimitry Andric     TuneCPU = std::string(Name);
2091e8d8bef9SDimitry Andric   }
2092e8d8bef9SDimitry Andric 
2093e8d8bef9SDimitry Andric   if (!TuneCPU.empty()) {
2094e8d8bef9SDimitry Andric     CmdArgs.push_back("-tune-cpu");
2095e8d8bef9SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(TuneCPU));
2096e8d8bef9SDimitry Andric   }
20970b57cec5SDimitry Andric }
20980b57cec5SDimitry Andric 
20990b57cec5SDimitry Andric void Clang::AddSparcTargetArgs(const ArgList &Args,
21000b57cec5SDimitry Andric                                ArgStringList &CmdArgs) const {
21010b57cec5SDimitry Andric   sparc::FloatABI FloatABI =
21020b57cec5SDimitry Andric       sparc::getSparcFloatABI(getToolChain().getDriver(), Args);
21030b57cec5SDimitry Andric 
21040b57cec5SDimitry Andric   if (FloatABI == sparc::FloatABI::Soft) {
21050b57cec5SDimitry Andric     // Floating point operations and argument passing are soft.
21060b57cec5SDimitry Andric     CmdArgs.push_back("-msoft-float");
21070b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
21080b57cec5SDimitry Andric     CmdArgs.push_back("soft");
21090b57cec5SDimitry Andric   } else {
21100b57cec5SDimitry Andric     // Floating point operations and argument passing are hard.
21110b57cec5SDimitry Andric     assert(FloatABI == sparc::FloatABI::Hard && "Invalid float abi!");
21120b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
21130b57cec5SDimitry Andric     CmdArgs.push_back("hard");
21140b57cec5SDimitry Andric   }
21150b57cec5SDimitry Andric }
21160b57cec5SDimitry Andric 
21170b57cec5SDimitry Andric void Clang::AddSystemZTargetArgs(const ArgList &Args,
21180b57cec5SDimitry Andric                                  ArgStringList &CmdArgs) const {
2119480093f4SDimitry Andric   bool HasBackchain = Args.hasFlag(options::OPT_mbackchain,
2120480093f4SDimitry Andric                                    options::OPT_mno_backchain, false);
2121480093f4SDimitry Andric   bool HasPackedStack = Args.hasFlag(options::OPT_mpacked_stack,
2122480093f4SDimitry Andric                                      options::OPT_mno_packed_stack, false);
21235ffd83dbSDimitry Andric   systemz::FloatABI FloatABI =
21245ffd83dbSDimitry Andric       systemz::getSystemZFloatABI(getToolChain().getDriver(), Args);
21255ffd83dbSDimitry Andric   bool HasSoftFloat = (FloatABI == systemz::FloatABI::Soft);
21265ffd83dbSDimitry Andric   if (HasBackchain && HasPackedStack && !HasSoftFloat) {
2127480093f4SDimitry Andric     const Driver &D = getToolChain().getDriver();
2128480093f4SDimitry Andric     D.Diag(diag::err_drv_unsupported_opt)
21295ffd83dbSDimitry Andric       << "-mpacked-stack -mbackchain -mhard-float";
2130480093f4SDimitry Andric   }
2131480093f4SDimitry Andric   if (HasBackchain)
21320b57cec5SDimitry Andric     CmdArgs.push_back("-mbackchain");
2133480093f4SDimitry Andric   if (HasPackedStack)
2134480093f4SDimitry Andric     CmdArgs.push_back("-mpacked-stack");
21355ffd83dbSDimitry Andric   if (HasSoftFloat) {
21365ffd83dbSDimitry Andric     // Floating point operations and argument passing are soft.
21375ffd83dbSDimitry Andric     CmdArgs.push_back("-msoft-float");
21385ffd83dbSDimitry Andric     CmdArgs.push_back("-mfloat-abi");
21395ffd83dbSDimitry Andric     CmdArgs.push_back("soft");
2140480093f4SDimitry Andric   }
21410b57cec5SDimitry Andric }
21420b57cec5SDimitry Andric 
21430b57cec5SDimitry Andric void Clang::AddX86TargetArgs(const ArgList &Args,
21440b57cec5SDimitry Andric                              ArgStringList &CmdArgs) const {
2145480093f4SDimitry Andric   const Driver &D = getToolChain().getDriver();
21465ffd83dbSDimitry Andric   addX86AlignBranchArgs(D, Args, CmdArgs, /*IsLTO=*/false);
2147480093f4SDimitry Andric 
21480b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
21490b57cec5SDimitry Andric       Args.hasArg(options::OPT_mkernel) ||
21500b57cec5SDimitry Andric       Args.hasArg(options::OPT_fapple_kext))
21510b57cec5SDimitry Andric     CmdArgs.push_back("-disable-red-zone");
21520b57cec5SDimitry Andric 
21530b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs,
21540b57cec5SDimitry Andric                     options::OPT_mno_tls_direct_seg_refs, true))
21550b57cec5SDimitry Andric     CmdArgs.push_back("-mno-tls-direct-seg-refs");
21560b57cec5SDimitry Andric 
21570b57cec5SDimitry Andric   // Default to avoid implicit floating-point for kernel/kext code, but allow
21580b57cec5SDimitry Andric   // that to be overridden with -mno-soft-float.
21590b57cec5SDimitry Andric   bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
21600b57cec5SDimitry Andric                           Args.hasArg(options::OPT_fapple_kext));
21610b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(
21620b57cec5SDimitry Andric           options::OPT_msoft_float, options::OPT_mno_soft_float,
21630b57cec5SDimitry Andric           options::OPT_mimplicit_float, options::OPT_mno_implicit_float)) {
21640b57cec5SDimitry Andric     const Option &O = A->getOption();
21650b57cec5SDimitry Andric     NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
21660b57cec5SDimitry Andric                        O.matches(options::OPT_msoft_float));
21670b57cec5SDimitry Andric   }
21680b57cec5SDimitry Andric   if (NoImplicitFloat)
21690b57cec5SDimitry Andric     CmdArgs.push_back("-no-implicit-float");
21700b57cec5SDimitry Andric 
21710b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
21720b57cec5SDimitry Andric     StringRef Value = A->getValue();
21730b57cec5SDimitry Andric     if (Value == "intel" || Value == "att") {
21740b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
21750b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
21760b57cec5SDimitry Andric     } else {
2177480093f4SDimitry Andric       D.Diag(diag::err_drv_unsupported_option_argument)
21780b57cec5SDimitry Andric           << A->getOption().getName() << Value;
21790b57cec5SDimitry Andric     }
2180480093f4SDimitry Andric   } else if (D.IsCLMode()) {
21810b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
21820b57cec5SDimitry Andric     CmdArgs.push_back("-x86-asm-syntax=intel");
21830b57cec5SDimitry Andric   }
21840b57cec5SDimitry Andric 
21850b57cec5SDimitry Andric   // Set flags to support MCU ABI.
21860b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) {
21870b57cec5SDimitry Andric     CmdArgs.push_back("-mfloat-abi");
21880b57cec5SDimitry Andric     CmdArgs.push_back("soft");
21890b57cec5SDimitry Andric     CmdArgs.push_back("-mstack-alignment=4");
21900b57cec5SDimitry Andric   }
2191e8d8bef9SDimitry Andric 
2192e8d8bef9SDimitry Andric   // Handle -mtune.
2193e8d8bef9SDimitry Andric 
2194e8d8bef9SDimitry Andric   // Default to "generic" unless -march is present or targetting the PS4.
2195e8d8bef9SDimitry Andric   std::string TuneCPU;
2196e8d8bef9SDimitry Andric   if (!Args.hasArg(clang::driver::options::OPT_march_EQ) &&
2197e8d8bef9SDimitry Andric       !getToolChain().getTriple().isPS4CPU())
2198e8d8bef9SDimitry Andric     TuneCPU = "generic";
2199e8d8bef9SDimitry Andric 
2200e8d8bef9SDimitry Andric   // Override based on -mtune.
2201e8d8bef9SDimitry Andric   if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) {
2202e8d8bef9SDimitry Andric     StringRef Name = A->getValue();
2203e8d8bef9SDimitry Andric 
2204e8d8bef9SDimitry Andric     if (Name == "native") {
2205e8d8bef9SDimitry Andric       Name = llvm::sys::getHostCPUName();
2206e8d8bef9SDimitry Andric       if (!Name.empty())
2207e8d8bef9SDimitry Andric         TuneCPU = std::string(Name);
2208e8d8bef9SDimitry Andric     } else
2209e8d8bef9SDimitry Andric       TuneCPU = std::string(Name);
2210e8d8bef9SDimitry Andric   }
2211e8d8bef9SDimitry Andric 
2212e8d8bef9SDimitry Andric   if (!TuneCPU.empty()) {
2213e8d8bef9SDimitry Andric     CmdArgs.push_back("-tune-cpu");
2214e8d8bef9SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(TuneCPU));
2215e8d8bef9SDimitry Andric   }
22160b57cec5SDimitry Andric }
22170b57cec5SDimitry Andric 
22180b57cec5SDimitry Andric void Clang::AddHexagonTargetArgs(const ArgList &Args,
22190b57cec5SDimitry Andric                                  ArgStringList &CmdArgs) const {
22200b57cec5SDimitry Andric   CmdArgs.push_back("-mqdsp6-compat");
22210b57cec5SDimitry Andric   CmdArgs.push_back("-Wreturn-type");
22220b57cec5SDimitry Andric 
22230b57cec5SDimitry Andric   if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) {
22240b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
22250b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-hexagon-small-data-threshold=" +
22260b57cec5SDimitry Andric                                          Twine(G.getValue())));
22270b57cec5SDimitry Andric   }
22280b57cec5SDimitry Andric 
22290b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_fno_short_enums))
22300b57cec5SDimitry Andric     CmdArgs.push_back("-fshort-enums");
22310b57cec5SDimitry Andric   if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
22320b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
22330b57cec5SDimitry Andric     CmdArgs.push_back("-enable-hexagon-ieee-rnd-near");
22340b57cec5SDimitry Andric   }
22350b57cec5SDimitry Andric   CmdArgs.push_back("-mllvm");
22360b57cec5SDimitry Andric   CmdArgs.push_back("-machine-sink-split=0");
22370b57cec5SDimitry Andric }
22380b57cec5SDimitry Andric 
22390b57cec5SDimitry Andric void Clang::AddLanaiTargetArgs(const ArgList &Args,
22400b57cec5SDimitry Andric                                ArgStringList &CmdArgs) const {
22410b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
22420b57cec5SDimitry Andric     StringRef CPUName = A->getValue();
22430b57cec5SDimitry Andric 
22440b57cec5SDimitry Andric     CmdArgs.push_back("-target-cpu");
22450b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(CPUName));
22460b57cec5SDimitry Andric   }
22470b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
22480b57cec5SDimitry Andric     StringRef Value = A->getValue();
22490b57cec5SDimitry Andric     // Only support mregparm=4 to support old usage. Report error for all other
22500b57cec5SDimitry Andric     // cases.
22510b57cec5SDimitry Andric     int Mregparm;
22520b57cec5SDimitry Andric     if (Value.getAsInteger(10, Mregparm)) {
22530b57cec5SDimitry Andric       if (Mregparm != 4) {
22540b57cec5SDimitry Andric         getToolChain().getDriver().Diag(
22550b57cec5SDimitry Andric             diag::err_drv_unsupported_option_argument)
22560b57cec5SDimitry Andric             << A->getOption().getName() << Value;
22570b57cec5SDimitry Andric       }
22580b57cec5SDimitry Andric     }
22590b57cec5SDimitry Andric   }
22600b57cec5SDimitry Andric }
22610b57cec5SDimitry Andric 
22620b57cec5SDimitry Andric void Clang::AddWebAssemblyTargetArgs(const ArgList &Args,
22630b57cec5SDimitry Andric                                      ArgStringList &CmdArgs) const {
22640b57cec5SDimitry Andric   // Default to "hidden" visibility.
22650b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_fvisibility_EQ,
22660b57cec5SDimitry Andric                    options::OPT_fvisibility_ms_compat)) {
22670b57cec5SDimitry Andric     CmdArgs.push_back("-fvisibility");
22680b57cec5SDimitry Andric     CmdArgs.push_back("hidden");
22690b57cec5SDimitry Andric   }
22700b57cec5SDimitry Andric }
22710b57cec5SDimitry Andric 
22725ffd83dbSDimitry Andric void Clang::AddVETargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const {
22735ffd83dbSDimitry Andric   // Floating point operations and argument passing are hard.
22745ffd83dbSDimitry Andric   CmdArgs.push_back("-mfloat-abi");
22755ffd83dbSDimitry Andric   CmdArgs.push_back("hard");
22765ffd83dbSDimitry Andric }
22775ffd83dbSDimitry Andric 
22780b57cec5SDimitry Andric void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename,
22790b57cec5SDimitry Andric                                     StringRef Target, const InputInfo &Output,
22800b57cec5SDimitry Andric                                     const InputInfo &Input, const ArgList &Args) const {
22810b57cec5SDimitry Andric   // If this is a dry run, do not create the compilation database file.
22820b57cec5SDimitry Andric   if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
22830b57cec5SDimitry Andric     return;
22840b57cec5SDimitry Andric 
22850b57cec5SDimitry Andric   using llvm::yaml::escape;
22860b57cec5SDimitry Andric   const Driver &D = getToolChain().getDriver();
22870b57cec5SDimitry Andric 
22880b57cec5SDimitry Andric   if (!CompilationDatabase) {
22890b57cec5SDimitry Andric     std::error_code EC;
2290fe6060f1SDimitry Andric     auto File = std::make_unique<llvm::raw_fd_ostream>(
2291fe6060f1SDimitry Andric         Filename, EC, llvm::sys::fs::OF_TextWithCRLF);
22920b57cec5SDimitry Andric     if (EC) {
22930b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_compilationdatabase) << Filename
22940b57cec5SDimitry Andric                                                        << EC.message();
22950b57cec5SDimitry Andric       return;
22960b57cec5SDimitry Andric     }
22970b57cec5SDimitry Andric     CompilationDatabase = std::move(File);
22980b57cec5SDimitry Andric   }
22990b57cec5SDimitry Andric   auto &CDB = *CompilationDatabase;
2300a7dea167SDimitry Andric   auto CWD = D.getVFS().getCurrentWorkingDirectory();
2301a7dea167SDimitry Andric   if (!CWD)
2302a7dea167SDimitry Andric     CWD = ".";
2303a7dea167SDimitry Andric   CDB << "{ \"directory\": \"" << escape(*CWD) << "\"";
23040b57cec5SDimitry Andric   CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\"";
23050b57cec5SDimitry Andric   CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\"";
23060b57cec5SDimitry Andric   CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\"";
2307a7dea167SDimitry Andric   SmallString<128> Buf;
23080b57cec5SDimitry Andric   Buf = "-x";
23090b57cec5SDimitry Andric   Buf += types::getTypeName(Input.getType());
23100b57cec5SDimitry Andric   CDB << ", \"" << escape(Buf) << "\"";
23110b57cec5SDimitry Andric   if (!D.SysRoot.empty() && !Args.hasArg(options::OPT__sysroot_EQ)) {
23120b57cec5SDimitry Andric     Buf = "--sysroot=";
23130b57cec5SDimitry Andric     Buf += D.SysRoot;
23140b57cec5SDimitry Andric     CDB << ", \"" << escape(Buf) << "\"";
23150b57cec5SDimitry Andric   }
23160b57cec5SDimitry Andric   CDB << ", \"" << escape(Input.getFilename()) << "\"";
23170b57cec5SDimitry Andric   for (auto &A: Args) {
23180b57cec5SDimitry Andric     auto &O = A->getOption();
23190b57cec5SDimitry Andric     // Skip language selection, which is positional.
23200b57cec5SDimitry Andric     if (O.getID() == options::OPT_x)
23210b57cec5SDimitry Andric       continue;
23220b57cec5SDimitry Andric     // Skip writing dependency output and the compilation database itself.
23230b57cec5SDimitry Andric     if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group)
23240b57cec5SDimitry Andric       continue;
2325a7dea167SDimitry Andric     if (O.getID() == options::OPT_gen_cdb_fragment_path)
2326a7dea167SDimitry Andric       continue;
23270b57cec5SDimitry Andric     // Skip inputs.
23280b57cec5SDimitry Andric     if (O.getKind() == Option::InputClass)
23290b57cec5SDimitry Andric       continue;
23300b57cec5SDimitry Andric     // All other arguments are quoted and appended.
23310b57cec5SDimitry Andric     ArgStringList ASL;
23320b57cec5SDimitry Andric     A->render(Args, ASL);
23330b57cec5SDimitry Andric     for (auto &it: ASL)
23340b57cec5SDimitry Andric       CDB << ", \"" << escape(it) << "\"";
23350b57cec5SDimitry Andric   }
23360b57cec5SDimitry Andric   Buf = "--target=";
23370b57cec5SDimitry Andric   Buf += Target;
23380b57cec5SDimitry Andric   CDB << ", \"" << escape(Buf) << "\"]},\n";
23390b57cec5SDimitry Andric }
23400b57cec5SDimitry Andric 
2341a7dea167SDimitry Andric void Clang::DumpCompilationDatabaseFragmentToDir(
2342a7dea167SDimitry Andric     StringRef Dir, Compilation &C, StringRef Target, const InputInfo &Output,
2343a7dea167SDimitry Andric     const InputInfo &Input, const llvm::opt::ArgList &Args) const {
2344a7dea167SDimitry Andric   // If this is a dry run, do not create the compilation database file.
2345a7dea167SDimitry Andric   if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
2346a7dea167SDimitry Andric     return;
2347a7dea167SDimitry Andric 
2348a7dea167SDimitry Andric   if (CompilationDatabase)
2349a7dea167SDimitry Andric     DumpCompilationDatabase(C, "", Target, Output, Input, Args);
2350a7dea167SDimitry Andric 
2351a7dea167SDimitry Andric   SmallString<256> Path = Dir;
2352a7dea167SDimitry Andric   const auto &Driver = C.getDriver();
2353a7dea167SDimitry Andric   Driver.getVFS().makeAbsolute(Path);
2354a7dea167SDimitry Andric   auto Err = llvm::sys::fs::create_directory(Path, /*IgnoreExisting=*/true);
2355a7dea167SDimitry Andric   if (Err) {
2356a7dea167SDimitry Andric     Driver.Diag(diag::err_drv_compilationdatabase) << Dir << Err.message();
2357a7dea167SDimitry Andric     return;
2358a7dea167SDimitry Andric   }
2359a7dea167SDimitry Andric 
2360a7dea167SDimitry Andric   llvm::sys::path::append(
2361a7dea167SDimitry Andric       Path,
2362a7dea167SDimitry Andric       Twine(llvm::sys::path::filename(Input.getFilename())) + ".%%%%.json");
2363a7dea167SDimitry Andric   int FD;
2364a7dea167SDimitry Andric   SmallString<256> TempPath;
2365fe6060f1SDimitry Andric   Err = llvm::sys::fs::createUniqueFile(Path, FD, TempPath,
2366fe6060f1SDimitry Andric                                         llvm::sys::fs::OF_Text);
2367a7dea167SDimitry Andric   if (Err) {
2368a7dea167SDimitry Andric     Driver.Diag(diag::err_drv_compilationdatabase) << Path << Err.message();
2369a7dea167SDimitry Andric     return;
2370a7dea167SDimitry Andric   }
2371a7dea167SDimitry Andric   CompilationDatabase =
2372a7dea167SDimitry Andric       std::make_unique<llvm::raw_fd_ostream>(FD, /*shouldClose=*/true);
2373a7dea167SDimitry Andric   DumpCompilationDatabase(C, "", Target, Output, Input, Args);
2374a7dea167SDimitry Andric }
2375a7dea167SDimitry Andric 
2376fe6060f1SDimitry Andric static bool CheckARMImplicitITArg(StringRef Value) {
2377fe6060f1SDimitry Andric   return Value == "always" || Value == "never" || Value == "arm" ||
2378fe6060f1SDimitry Andric          Value == "thumb";
2379fe6060f1SDimitry Andric }
2380fe6060f1SDimitry Andric 
2381fe6060f1SDimitry Andric static void AddARMImplicitITArgs(const ArgList &Args, ArgStringList &CmdArgs,
2382fe6060f1SDimitry Andric                                  StringRef Value) {
2383fe6060f1SDimitry Andric   CmdArgs.push_back("-mllvm");
2384fe6060f1SDimitry Andric   CmdArgs.push_back(Args.MakeArgString("-arm-implicit-it=" + Value));
2385fe6060f1SDimitry Andric }
2386fe6060f1SDimitry Andric 
23870b57cec5SDimitry Andric static void CollectArgsForIntegratedAssembler(Compilation &C,
23880b57cec5SDimitry Andric                                               const ArgList &Args,
23890b57cec5SDimitry Andric                                               ArgStringList &CmdArgs,
23900b57cec5SDimitry Andric                                               const Driver &D) {
23910b57cec5SDimitry Andric   if (UseRelaxAll(C, Args))
23920b57cec5SDimitry Andric     CmdArgs.push_back("-mrelax-all");
23930b57cec5SDimitry Andric 
23940b57cec5SDimitry Andric   // Only default to -mincremental-linker-compatible if we think we are
23950b57cec5SDimitry Andric   // targeting the MSVC linker.
23960b57cec5SDimitry Andric   bool DefaultIncrementalLinkerCompatible =
23970b57cec5SDimitry Andric       C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment();
23980b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_mincremental_linker_compatible,
23990b57cec5SDimitry Andric                    options::OPT_mno_incremental_linker_compatible,
24000b57cec5SDimitry Andric                    DefaultIncrementalLinkerCompatible))
24010b57cec5SDimitry Andric     CmdArgs.push_back("-mincremental-linker-compatible");
24020b57cec5SDimitry Andric 
2403a7dea167SDimitry Andric   // If you add more args here, also add them to the block below that
2404a7dea167SDimitry Andric   // starts with "// If CollectArgsForIntegratedAssembler() isn't called below".
2405a7dea167SDimitry Andric 
24060b57cec5SDimitry Andric   // When passing -I arguments to the assembler we sometimes need to
24070b57cec5SDimitry Andric   // unconditionally take the next argument.  For example, when parsing
24080b57cec5SDimitry Andric   // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the
24090b57cec5SDimitry Andric   // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo'
24100b57cec5SDimitry Andric   // arg after parsing the '-I' arg.
24110b57cec5SDimitry Andric   bool TakeNextArg = false;
24120b57cec5SDimitry Andric 
24130b57cec5SDimitry Andric   bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations();
24140b57cec5SDimitry Andric   bool UseNoExecStack = C.getDefaultToolChain().isNoExecStackDefault();
24150b57cec5SDimitry Andric   const char *MipsTargetFeature = nullptr;
2416fe6060f1SDimitry Andric   StringRef ImplicitIt;
24170b57cec5SDimitry Andric   for (const Arg *A :
2418fe6060f1SDimitry Andric        Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler,
2419fe6060f1SDimitry Andric                      options::OPT_mimplicit_it_EQ)) {
24200b57cec5SDimitry Andric     A->claim();
24210b57cec5SDimitry Andric 
2422fe6060f1SDimitry Andric     if (A->getOption().getID() == options::OPT_mimplicit_it_EQ) {
2423fe6060f1SDimitry Andric       switch (C.getDefaultToolChain().getArch()) {
2424fe6060f1SDimitry Andric       case llvm::Triple::arm:
2425fe6060f1SDimitry Andric       case llvm::Triple::armeb:
2426fe6060f1SDimitry Andric       case llvm::Triple::thumb:
2427fe6060f1SDimitry Andric       case llvm::Triple::thumbeb:
2428fe6060f1SDimitry Andric         // Only store the value; the last value set takes effect.
2429fe6060f1SDimitry Andric         ImplicitIt = A->getValue();
2430fe6060f1SDimitry Andric         if (!CheckARMImplicitITArg(ImplicitIt))
2431fe6060f1SDimitry Andric           D.Diag(diag::err_drv_unsupported_option_argument)
2432fe6060f1SDimitry Andric               << A->getOption().getName() << ImplicitIt;
2433fe6060f1SDimitry Andric         continue;
2434fe6060f1SDimitry Andric       default:
2435fe6060f1SDimitry Andric         break;
2436fe6060f1SDimitry Andric       }
2437fe6060f1SDimitry Andric     }
2438fe6060f1SDimitry Andric 
24390b57cec5SDimitry Andric     for (StringRef Value : A->getValues()) {
24400b57cec5SDimitry Andric       if (TakeNextArg) {
24410b57cec5SDimitry Andric         CmdArgs.push_back(Value.data());
24420b57cec5SDimitry Andric         TakeNextArg = false;
24430b57cec5SDimitry Andric         continue;
24440b57cec5SDimitry Andric       }
24450b57cec5SDimitry Andric 
24460b57cec5SDimitry Andric       if (C.getDefaultToolChain().getTriple().isOSBinFormatCOFF() &&
24470b57cec5SDimitry Andric           Value == "-mbig-obj")
24480b57cec5SDimitry Andric         continue; // LLVM handles bigobj automatically
24490b57cec5SDimitry Andric 
24500b57cec5SDimitry Andric       switch (C.getDefaultToolChain().getArch()) {
24510b57cec5SDimitry Andric       default:
24520b57cec5SDimitry Andric         break;
24530b57cec5SDimitry Andric       case llvm::Triple::thumb:
24540b57cec5SDimitry Andric       case llvm::Triple::thumbeb:
24550b57cec5SDimitry Andric       case llvm::Triple::arm:
24560b57cec5SDimitry Andric       case llvm::Triple::armeb:
2457fe6060f1SDimitry Andric         if (Value.startswith("-mimplicit-it=")) {
2458fe6060f1SDimitry Andric           // Only store the value; the last value set takes effect.
2459fe6060f1SDimitry Andric           ImplicitIt = Value.split("=").second;
2460fe6060f1SDimitry Andric           if (CheckARMImplicitITArg(ImplicitIt))
2461fe6060f1SDimitry Andric             continue;
2462fe6060f1SDimitry Andric         }
24630b57cec5SDimitry Andric         if (Value == "-mthumb")
24640b57cec5SDimitry Andric           // -mthumb has already been processed in ComputeLLVMTriple()
24650b57cec5SDimitry Andric           // recognize but skip over here.
24660b57cec5SDimitry Andric           continue;
24670b57cec5SDimitry Andric         break;
24680b57cec5SDimitry Andric       case llvm::Triple::mips:
24690b57cec5SDimitry Andric       case llvm::Triple::mipsel:
24700b57cec5SDimitry Andric       case llvm::Triple::mips64:
24710b57cec5SDimitry Andric       case llvm::Triple::mips64el:
24720b57cec5SDimitry Andric         if (Value == "--trap") {
24730b57cec5SDimitry Andric           CmdArgs.push_back("-target-feature");
24740b57cec5SDimitry Andric           CmdArgs.push_back("+use-tcc-in-div");
24750b57cec5SDimitry Andric           continue;
24760b57cec5SDimitry Andric         }
24770b57cec5SDimitry Andric         if (Value == "--break") {
24780b57cec5SDimitry Andric           CmdArgs.push_back("-target-feature");
24790b57cec5SDimitry Andric           CmdArgs.push_back("-use-tcc-in-div");
24800b57cec5SDimitry Andric           continue;
24810b57cec5SDimitry Andric         }
24820b57cec5SDimitry Andric         if (Value.startswith("-msoft-float")) {
24830b57cec5SDimitry Andric           CmdArgs.push_back("-target-feature");
24840b57cec5SDimitry Andric           CmdArgs.push_back("+soft-float");
24850b57cec5SDimitry Andric           continue;
24860b57cec5SDimitry Andric         }
24870b57cec5SDimitry Andric         if (Value.startswith("-mhard-float")) {
24880b57cec5SDimitry Andric           CmdArgs.push_back("-target-feature");
24890b57cec5SDimitry Andric           CmdArgs.push_back("-soft-float");
24900b57cec5SDimitry Andric           continue;
24910b57cec5SDimitry Andric         }
24920b57cec5SDimitry Andric 
24930b57cec5SDimitry Andric         MipsTargetFeature = llvm::StringSwitch<const char *>(Value)
24940b57cec5SDimitry Andric                                 .Case("-mips1", "+mips1")
24950b57cec5SDimitry Andric                                 .Case("-mips2", "+mips2")
24960b57cec5SDimitry Andric                                 .Case("-mips3", "+mips3")
24970b57cec5SDimitry Andric                                 .Case("-mips4", "+mips4")
24980b57cec5SDimitry Andric                                 .Case("-mips5", "+mips5")
24990b57cec5SDimitry Andric                                 .Case("-mips32", "+mips32")
25000b57cec5SDimitry Andric                                 .Case("-mips32r2", "+mips32r2")
25010b57cec5SDimitry Andric                                 .Case("-mips32r3", "+mips32r3")
25020b57cec5SDimitry Andric                                 .Case("-mips32r5", "+mips32r5")
25030b57cec5SDimitry Andric                                 .Case("-mips32r6", "+mips32r6")
25040b57cec5SDimitry Andric                                 .Case("-mips64", "+mips64")
25050b57cec5SDimitry Andric                                 .Case("-mips64r2", "+mips64r2")
25060b57cec5SDimitry Andric                                 .Case("-mips64r3", "+mips64r3")
25070b57cec5SDimitry Andric                                 .Case("-mips64r5", "+mips64r5")
25080b57cec5SDimitry Andric                                 .Case("-mips64r6", "+mips64r6")
25090b57cec5SDimitry Andric                                 .Default(nullptr);
25100b57cec5SDimitry Andric         if (MipsTargetFeature)
25110b57cec5SDimitry Andric           continue;
25120b57cec5SDimitry Andric       }
25130b57cec5SDimitry Andric 
25140b57cec5SDimitry Andric       if (Value == "-force_cpusubtype_ALL") {
25150b57cec5SDimitry Andric         // Do nothing, this is the default and we don't support anything else.
25160b57cec5SDimitry Andric       } else if (Value == "-L") {
25170b57cec5SDimitry Andric         CmdArgs.push_back("-msave-temp-labels");
25180b57cec5SDimitry Andric       } else if (Value == "--fatal-warnings") {
25190b57cec5SDimitry Andric         CmdArgs.push_back("-massembler-fatal-warnings");
2520a7dea167SDimitry Andric       } else if (Value == "--no-warn" || Value == "-W") {
2521a7dea167SDimitry Andric         CmdArgs.push_back("-massembler-no-warn");
25220b57cec5SDimitry Andric       } else if (Value == "--noexecstack") {
25230b57cec5SDimitry Andric         UseNoExecStack = true;
25240b57cec5SDimitry Andric       } else if (Value.startswith("-compress-debug-sections") ||
25250b57cec5SDimitry Andric                  Value.startswith("--compress-debug-sections") ||
25260b57cec5SDimitry Andric                  Value == "-nocompress-debug-sections" ||
25270b57cec5SDimitry Andric                  Value == "--nocompress-debug-sections") {
25280b57cec5SDimitry Andric         CmdArgs.push_back(Value.data());
25290b57cec5SDimitry Andric       } else if (Value == "-mrelax-relocations=yes" ||
25300b57cec5SDimitry Andric                  Value == "--mrelax-relocations=yes") {
25310b57cec5SDimitry Andric         UseRelaxRelocations = true;
25320b57cec5SDimitry Andric       } else if (Value == "-mrelax-relocations=no" ||
25330b57cec5SDimitry Andric                  Value == "--mrelax-relocations=no") {
25340b57cec5SDimitry Andric         UseRelaxRelocations = false;
25350b57cec5SDimitry Andric       } else if (Value.startswith("-I")) {
25360b57cec5SDimitry Andric         CmdArgs.push_back(Value.data());
25370b57cec5SDimitry Andric         // We need to consume the next argument if the current arg is a plain
25380b57cec5SDimitry Andric         // -I. The next arg will be the include directory.
25390b57cec5SDimitry Andric         if (Value == "-I")
25400b57cec5SDimitry Andric           TakeNextArg = true;
25410b57cec5SDimitry Andric       } else if (Value.startswith("-gdwarf-")) {
25420b57cec5SDimitry Andric         // "-gdwarf-N" options are not cc1as options.
25430b57cec5SDimitry Andric         unsigned DwarfVersion = DwarfVersionNum(Value);
25440b57cec5SDimitry Andric         if (DwarfVersion == 0) { // Send it onward, and let cc1as complain.
25450b57cec5SDimitry Andric           CmdArgs.push_back(Value.data());
25460b57cec5SDimitry Andric         } else {
25470b57cec5SDimitry Andric           RenderDebugEnablingArgs(Args, CmdArgs,
2548fe6060f1SDimitry Andric                                   codegenoptions::DebugInfoConstructor,
25490b57cec5SDimitry Andric                                   DwarfVersion, llvm::DebuggerKind::Default);
25500b57cec5SDimitry Andric         }
25510b57cec5SDimitry Andric       } else if (Value.startswith("-mcpu") || Value.startswith("-mfpu") ||
25520b57cec5SDimitry Andric                  Value.startswith("-mhwdiv") || Value.startswith("-march")) {
25530b57cec5SDimitry Andric         // Do nothing, we'll validate it later.
25540b57cec5SDimitry Andric       } else if (Value == "-defsym") {
25550b57cec5SDimitry Andric           if (A->getNumValues() != 2) {
25560b57cec5SDimitry Andric             D.Diag(diag::err_drv_defsym_invalid_format) << Value;
25570b57cec5SDimitry Andric             break;
25580b57cec5SDimitry Andric           }
25590b57cec5SDimitry Andric           const char *S = A->getValue(1);
25600b57cec5SDimitry Andric           auto Pair = StringRef(S).split('=');
25610b57cec5SDimitry Andric           auto Sym = Pair.first;
25620b57cec5SDimitry Andric           auto SVal = Pair.second;
25630b57cec5SDimitry Andric 
25640b57cec5SDimitry Andric           if (Sym.empty() || SVal.empty()) {
25650b57cec5SDimitry Andric             D.Diag(diag::err_drv_defsym_invalid_format) << S;
25660b57cec5SDimitry Andric             break;
25670b57cec5SDimitry Andric           }
25680b57cec5SDimitry Andric           int64_t IVal;
25690b57cec5SDimitry Andric           if (SVal.getAsInteger(0, IVal)) {
25700b57cec5SDimitry Andric             D.Diag(diag::err_drv_defsym_invalid_symval) << SVal;
25710b57cec5SDimitry Andric             break;
25720b57cec5SDimitry Andric           }
25730b57cec5SDimitry Andric           CmdArgs.push_back(Value.data());
25740b57cec5SDimitry Andric           TakeNextArg = true;
25750b57cec5SDimitry Andric       } else if (Value == "-fdebug-compilation-dir") {
25760b57cec5SDimitry Andric         CmdArgs.push_back("-fdebug-compilation-dir");
25770b57cec5SDimitry Andric         TakeNextArg = true;
2578480093f4SDimitry Andric       } else if (Value.consume_front("-fdebug-compilation-dir=")) {
2579480093f4SDimitry Andric         // The flag is a -Wa / -Xassembler argument and Options doesn't
2580480093f4SDimitry Andric         // parse the argument, so this isn't automatically aliased to
2581480093f4SDimitry Andric         // -fdebug-compilation-dir (without '=') here.
2582480093f4SDimitry Andric         CmdArgs.push_back("-fdebug-compilation-dir");
2583480093f4SDimitry Andric         CmdArgs.push_back(Value.data());
2584fe6060f1SDimitry Andric       } else if (Value == "--version") {
2585fe6060f1SDimitry Andric         D.PrintVersion(C, llvm::outs());
25860b57cec5SDimitry Andric       } else {
25870b57cec5SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
25880b57cec5SDimitry Andric             << A->getOption().getName() << Value;
25890b57cec5SDimitry Andric       }
25900b57cec5SDimitry Andric     }
25910b57cec5SDimitry Andric   }
2592fe6060f1SDimitry Andric   if (ImplicitIt.size())
2593fe6060f1SDimitry Andric     AddARMImplicitITArgs(Args, CmdArgs, ImplicitIt);
25940b57cec5SDimitry Andric   if (UseRelaxRelocations)
25950b57cec5SDimitry Andric     CmdArgs.push_back("--mrelax-relocations");
25960b57cec5SDimitry Andric   if (UseNoExecStack)
25970b57cec5SDimitry Andric     CmdArgs.push_back("-mnoexecstack");
25980b57cec5SDimitry Andric   if (MipsTargetFeature != nullptr) {
25990b57cec5SDimitry Andric     CmdArgs.push_back("-target-feature");
26000b57cec5SDimitry Andric     CmdArgs.push_back(MipsTargetFeature);
26010b57cec5SDimitry Andric   }
26020b57cec5SDimitry Andric 
26030b57cec5SDimitry Andric   // forward -fembed-bitcode to assmebler
26040b57cec5SDimitry Andric   if (C.getDriver().embedBitcodeEnabled() ||
26050b57cec5SDimitry Andric       C.getDriver().embedBitcodeMarkerOnly())
26060b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ);
26070b57cec5SDimitry Andric }
26080b57cec5SDimitry Andric 
26090b57cec5SDimitry Andric static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
26100b57cec5SDimitry Andric                                        bool OFastEnabled, const ArgList &Args,
26115ffd83dbSDimitry Andric                                        ArgStringList &CmdArgs,
26125ffd83dbSDimitry Andric                                        const JobAction &JA) {
26130b57cec5SDimitry Andric   // Handle various floating point optimization flags, mapping them to the
26140b57cec5SDimitry Andric   // appropriate LLVM code generation flags. This is complicated by several
26150b57cec5SDimitry Andric   // "umbrella" flags, so we do this by stepping through the flags incrementally
26160b57cec5SDimitry Andric   // adjusting what we think is enabled/disabled, then at the end setting the
26170b57cec5SDimitry Andric   // LLVM flags based on the final state.
26180b57cec5SDimitry Andric   bool HonorINFs = true;
26190b57cec5SDimitry Andric   bool HonorNaNs = true;
26200b57cec5SDimitry Andric   // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
26210b57cec5SDimitry Andric   bool MathErrno = TC.IsMathErrnoDefault();
26220b57cec5SDimitry Andric   bool AssociativeMath = false;
26230b57cec5SDimitry Andric   bool ReciprocalMath = false;
26240b57cec5SDimitry Andric   bool SignedZeros = true;
2625480093f4SDimitry Andric   bool TrappingMath = false; // Implemented via -ffp-exception-behavior
2626480093f4SDimitry Andric   bool TrappingMathPresent = false; // Is trapping-math in args, and not
2627480093f4SDimitry Andric                                     // overriden by ffp-exception-behavior?
2628480093f4SDimitry Andric   bool RoundingFPMath = false;
2629480093f4SDimitry Andric   bool RoundingMathPresent = false; // Is rounding-math in args?
2630480093f4SDimitry Andric   // -ffp-model values: strict, fast, precise
2631480093f4SDimitry Andric   StringRef FPModel = "";
2632480093f4SDimitry Andric   // -ffp-exception-behavior options: strict, maytrap, ignore
2633480093f4SDimitry Andric   StringRef FPExceptionBehavior = "";
26345ffd83dbSDimitry Andric   const llvm::DenormalMode DefaultDenormalFPMath =
26355ffd83dbSDimitry Andric       TC.getDefaultDenormalModeForType(Args, JA);
26365ffd83dbSDimitry Andric   const llvm::DenormalMode DefaultDenormalFP32Math =
26375ffd83dbSDimitry Andric       TC.getDefaultDenormalModeForType(Args, JA, &llvm::APFloat::IEEEsingle());
26385ffd83dbSDimitry Andric 
26395ffd83dbSDimitry Andric   llvm::DenormalMode DenormalFPMath = DefaultDenormalFPMath;
26405ffd83dbSDimitry Andric   llvm::DenormalMode DenormalFP32Math = DefaultDenormalFP32Math;
2641fe6060f1SDimitry Andric   StringRef FPContract = "on";
2642480093f4SDimitry Andric   bool StrictFPModel = false;
26430b57cec5SDimitry Andric 
26445ffd83dbSDimitry Andric 
26450b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
26460b57cec5SDimitry Andric     CmdArgs.push_back("-mlimit-float-precision");
26470b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
26480b57cec5SDimitry Andric   }
26490b57cec5SDimitry Andric 
26500b57cec5SDimitry Andric   for (const Arg *A : Args) {
2651480093f4SDimitry Andric     auto optID = A->getOption().getID();
2652480093f4SDimitry Andric     bool PreciseFPModel = false;
2653480093f4SDimitry Andric     switch (optID) {
2654480093f4SDimitry Andric     default:
2655480093f4SDimitry Andric       break;
2656480093f4SDimitry Andric     case options::OPT_ffp_model_EQ: {
2657480093f4SDimitry Andric       // If -ffp-model= is seen, reset to fno-fast-math
2658480093f4SDimitry Andric       HonorINFs = true;
2659480093f4SDimitry Andric       HonorNaNs = true;
2660480093f4SDimitry Andric       // Turning *off* -ffast-math restores the toolchain default.
2661480093f4SDimitry Andric       MathErrno = TC.IsMathErrnoDefault();
2662480093f4SDimitry Andric       AssociativeMath = false;
2663480093f4SDimitry Andric       ReciprocalMath = false;
2664480093f4SDimitry Andric       SignedZeros = true;
2665480093f4SDimitry Andric       // -fno_fast_math restores default denormal and fpcontract handling
2666fe6060f1SDimitry Andric       FPContract = "on";
26675ffd83dbSDimitry Andric       DenormalFPMath = llvm::DenormalMode::getIEEE();
26685ffd83dbSDimitry Andric 
26695ffd83dbSDimitry Andric       // FIXME: The target may have picked a non-IEEE default mode here based on
26705ffd83dbSDimitry Andric       // -cl-denorms-are-zero. Should the target consider -fp-model interaction?
26715ffd83dbSDimitry Andric       DenormalFP32Math = llvm::DenormalMode::getIEEE();
26725ffd83dbSDimitry Andric 
2673480093f4SDimitry Andric       StringRef Val = A->getValue();
2674480093f4SDimitry Andric       if (OFastEnabled && !Val.equals("fast")) {
2675480093f4SDimitry Andric           // Only -ffp-model=fast is compatible with OFast, ignore.
2676480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2677480093f4SDimitry Andric           << Args.MakeArgString("-ffp-model=" + Val)
2678480093f4SDimitry Andric           << "-Ofast";
2679480093f4SDimitry Andric         break;
2680480093f4SDimitry Andric       }
2681480093f4SDimitry Andric       StrictFPModel = false;
2682480093f4SDimitry Andric       PreciseFPModel = true;
2683480093f4SDimitry Andric       // ffp-model= is a Driver option, it is entirely rewritten into more
2684480093f4SDimitry Andric       // granular options before being passed into cc1.
2685480093f4SDimitry Andric       // Use the gcc option in the switch below.
2686fe6060f1SDimitry Andric       if (!FPModel.empty() && !FPModel.equals(Val))
2687480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2688480093f4SDimitry Andric           << Args.MakeArgString("-ffp-model=" + FPModel)
2689480093f4SDimitry Andric           << Args.MakeArgString("-ffp-model=" + Val);
2690480093f4SDimitry Andric       if (Val.equals("fast")) {
2691480093f4SDimitry Andric         optID = options::OPT_ffast_math;
2692480093f4SDimitry Andric         FPModel = Val;
2693fe6060f1SDimitry Andric         FPContract = Val;
2694480093f4SDimitry Andric       } else if (Val.equals("precise")) {
2695480093f4SDimitry Andric         optID = options::OPT_ffp_contract;
2696480093f4SDimitry Andric         FPModel = Val;
2697fe6060f1SDimitry Andric         FPContract = "on";
2698480093f4SDimitry Andric         PreciseFPModel = true;
2699480093f4SDimitry Andric       } else if (Val.equals("strict")) {
2700480093f4SDimitry Andric         StrictFPModel = true;
2701480093f4SDimitry Andric         optID = options::OPT_frounding_math;
2702480093f4SDimitry Andric         FPExceptionBehavior = "strict";
2703480093f4SDimitry Andric         FPModel = Val;
27045ffd83dbSDimitry Andric         FPContract = "off";
2705480093f4SDimitry Andric         TrappingMath = true;
2706480093f4SDimitry Andric       } else
2707480093f4SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
2708480093f4SDimitry Andric             << A->getOption().getName() << Val;
2709480093f4SDimitry Andric       break;
2710480093f4SDimitry Andric       }
2711480093f4SDimitry Andric     }
2712480093f4SDimitry Andric 
2713480093f4SDimitry Andric     switch (optID) {
27140b57cec5SDimitry Andric     // If this isn't an FP option skip the claim below
27150b57cec5SDimitry Andric     default: continue;
27160b57cec5SDimitry Andric 
27170b57cec5SDimitry Andric     // Options controlling individual features
27180b57cec5SDimitry Andric     case options::OPT_fhonor_infinities:    HonorINFs = true;         break;
27190b57cec5SDimitry Andric     case options::OPT_fno_honor_infinities: HonorINFs = false;        break;
27200b57cec5SDimitry Andric     case options::OPT_fhonor_nans:          HonorNaNs = true;         break;
27210b57cec5SDimitry Andric     case options::OPT_fno_honor_nans:       HonorNaNs = false;        break;
27220b57cec5SDimitry Andric     case options::OPT_fmath_errno:          MathErrno = true;         break;
27230b57cec5SDimitry Andric     case options::OPT_fno_math_errno:       MathErrno = false;        break;
27240b57cec5SDimitry Andric     case options::OPT_fassociative_math:    AssociativeMath = true;   break;
27250b57cec5SDimitry Andric     case options::OPT_fno_associative_math: AssociativeMath = false;  break;
27260b57cec5SDimitry Andric     case options::OPT_freciprocal_math:     ReciprocalMath = true;    break;
27270b57cec5SDimitry Andric     case options::OPT_fno_reciprocal_math:  ReciprocalMath = false;   break;
27280b57cec5SDimitry Andric     case options::OPT_fsigned_zeros:        SignedZeros = true;       break;
27290b57cec5SDimitry Andric     case options::OPT_fno_signed_zeros:     SignedZeros = false;      break;
2730480093f4SDimitry Andric     case options::OPT_ftrapping_math:
2731480093f4SDimitry Andric       if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
2732480093f4SDimitry Andric           !FPExceptionBehavior.equals("strict"))
2733480093f4SDimitry Andric         // Warn that previous value of option is overridden.
2734480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2735480093f4SDimitry Andric           << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior)
2736480093f4SDimitry Andric           << "-ftrapping-math";
2737480093f4SDimitry Andric       TrappingMath = true;
2738480093f4SDimitry Andric       TrappingMathPresent = true;
2739480093f4SDimitry Andric       FPExceptionBehavior = "strict";
2740480093f4SDimitry Andric       break;
2741480093f4SDimitry Andric     case options::OPT_fno_trapping_math:
2742480093f4SDimitry Andric       if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
2743480093f4SDimitry Andric           !FPExceptionBehavior.equals("ignore"))
2744480093f4SDimitry Andric         // Warn that previous value of option is overridden.
2745480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2746480093f4SDimitry Andric           << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior)
2747480093f4SDimitry Andric           << "-fno-trapping-math";
2748480093f4SDimitry Andric       TrappingMath = false;
2749480093f4SDimitry Andric       TrappingMathPresent = true;
2750480093f4SDimitry Andric       FPExceptionBehavior = "ignore";
2751480093f4SDimitry Andric       break;
2752480093f4SDimitry Andric 
2753480093f4SDimitry Andric     case options::OPT_frounding_math:
2754480093f4SDimitry Andric       RoundingFPMath = true;
2755480093f4SDimitry Andric       RoundingMathPresent = true;
2756480093f4SDimitry Andric       break;
2757480093f4SDimitry Andric 
2758480093f4SDimitry Andric     case options::OPT_fno_rounding_math:
2759480093f4SDimitry Andric       RoundingFPMath = false;
2760480093f4SDimitry Andric       RoundingMathPresent = false;
2761480093f4SDimitry Andric       break;
27620b57cec5SDimitry Andric 
27630b57cec5SDimitry Andric     case options::OPT_fdenormal_fp_math_EQ:
27645ffd83dbSDimitry Andric       DenormalFPMath = llvm::parseDenormalFPAttribute(A->getValue());
27655ffd83dbSDimitry Andric       if (!DenormalFPMath.isValid()) {
27665ffd83dbSDimitry Andric         D.Diag(diag::err_drv_invalid_value)
27675ffd83dbSDimitry Andric             << A->getAsString(Args) << A->getValue();
27685ffd83dbSDimitry Andric       }
27695ffd83dbSDimitry Andric       break;
27705ffd83dbSDimitry Andric 
27715ffd83dbSDimitry Andric     case options::OPT_fdenormal_fp_math_f32_EQ:
27725ffd83dbSDimitry Andric       DenormalFP32Math = llvm::parseDenormalFPAttribute(A->getValue());
27735ffd83dbSDimitry Andric       if (!DenormalFP32Math.isValid()) {
27745ffd83dbSDimitry Andric         D.Diag(diag::err_drv_invalid_value)
27755ffd83dbSDimitry Andric             << A->getAsString(Args) << A->getValue();
27765ffd83dbSDimitry Andric       }
27770b57cec5SDimitry Andric       break;
27780b57cec5SDimitry Andric 
2779480093f4SDimitry Andric     // Validate and pass through -ffp-contract option.
27800b57cec5SDimitry Andric     case options::OPT_ffp_contract: {
27810b57cec5SDimitry Andric       StringRef Val = A->getValue();
2782480093f4SDimitry Andric       if (PreciseFPModel) {
2783fe6060f1SDimitry Andric         // When -ffp-model=precise is seen on the command line,
2784fe6060f1SDimitry Andric         // the boolean PreciseFPModel is set to true which indicates
2785fe6060f1SDimitry Andric         // "the current option is actually PreciseFPModel". The optID
2786fe6060f1SDimitry Andric         // is changed to OPT_ffp_contract and FPContract is set to "on".
2787fe6060f1SDimitry Andric         // the argument Val string is "precise": it shouldn't be checked.
2788480093f4SDimitry Andric         ;
2789480093f4SDimitry Andric       } else if (Val.equals("fast") || Val.equals("on") || Val.equals("off"))
27900b57cec5SDimitry Andric         FPContract = Val;
27910b57cec5SDimitry Andric       else
27920b57cec5SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
27930b57cec5SDimitry Andric            << A->getOption().getName() << Val;
27940b57cec5SDimitry Andric       break;
27950b57cec5SDimitry Andric     }
27960b57cec5SDimitry Andric 
2797480093f4SDimitry Andric     // Validate and pass through -ffp-model option.
2798480093f4SDimitry Andric     case options::OPT_ffp_model_EQ:
2799480093f4SDimitry Andric       // This should only occur in the error case
2800480093f4SDimitry Andric       // since the optID has been replaced by a more granular
2801480093f4SDimitry Andric       // floating point option.
2802480093f4SDimitry Andric       break;
2803480093f4SDimitry Andric 
2804480093f4SDimitry Andric     // Validate and pass through -ffp-exception-behavior option.
2805480093f4SDimitry Andric     case options::OPT_ffp_exception_behavior_EQ: {
2806480093f4SDimitry Andric       StringRef Val = A->getValue();
2807480093f4SDimitry Andric       if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
2808480093f4SDimitry Andric           !FPExceptionBehavior.equals(Val))
2809480093f4SDimitry Andric         // Warn that previous value of option is overridden.
2810480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2811480093f4SDimitry Andric           << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior)
2812480093f4SDimitry Andric           << Args.MakeArgString("-ffp-exception-behavior=" + Val);
2813480093f4SDimitry Andric       TrappingMath = TrappingMathPresent = false;
2814480093f4SDimitry Andric       if (Val.equals("ignore") || Val.equals("maytrap"))
2815480093f4SDimitry Andric         FPExceptionBehavior = Val;
2816480093f4SDimitry Andric       else if (Val.equals("strict")) {
2817480093f4SDimitry Andric         FPExceptionBehavior = Val;
2818480093f4SDimitry Andric         TrappingMath = TrappingMathPresent = true;
2819480093f4SDimitry Andric       } else
2820480093f4SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
2821480093f4SDimitry Andric             << A->getOption().getName() << Val;
2822480093f4SDimitry Andric       break;
2823480093f4SDimitry Andric     }
2824480093f4SDimitry Andric 
28250b57cec5SDimitry Andric     case options::OPT_ffinite_math_only:
28260b57cec5SDimitry Andric       HonorINFs = false;
28270b57cec5SDimitry Andric       HonorNaNs = false;
28280b57cec5SDimitry Andric       break;
28290b57cec5SDimitry Andric     case options::OPT_fno_finite_math_only:
28300b57cec5SDimitry Andric       HonorINFs = true;
28310b57cec5SDimitry Andric       HonorNaNs = true;
28320b57cec5SDimitry Andric       break;
28330b57cec5SDimitry Andric 
28340b57cec5SDimitry Andric     case options::OPT_funsafe_math_optimizations:
28350b57cec5SDimitry Andric       AssociativeMath = true;
28360b57cec5SDimitry Andric       ReciprocalMath = true;
28370b57cec5SDimitry Andric       SignedZeros = false;
28380b57cec5SDimitry Andric       TrappingMath = false;
2839480093f4SDimitry Andric       FPExceptionBehavior = "";
28400b57cec5SDimitry Andric       break;
28410b57cec5SDimitry Andric     case options::OPT_fno_unsafe_math_optimizations:
28420b57cec5SDimitry Andric       AssociativeMath = false;
28430b57cec5SDimitry Andric       ReciprocalMath = false;
28440b57cec5SDimitry Andric       SignedZeros = true;
28450b57cec5SDimitry Andric       TrappingMath = true;
2846480093f4SDimitry Andric       FPExceptionBehavior = "strict";
28475ffd83dbSDimitry Andric 
28485ffd83dbSDimitry Andric       // The target may have opted to flush by default, so force IEEE.
28495ffd83dbSDimitry Andric       DenormalFPMath = llvm::DenormalMode::getIEEE();
28505ffd83dbSDimitry Andric       DenormalFP32Math = llvm::DenormalMode::getIEEE();
28510b57cec5SDimitry Andric       break;
28520b57cec5SDimitry Andric 
28530b57cec5SDimitry Andric     case options::OPT_Ofast:
28540b57cec5SDimitry Andric       // If -Ofast is the optimization level, then -ffast-math should be enabled
28550b57cec5SDimitry Andric       if (!OFastEnabled)
28560b57cec5SDimitry Andric         continue;
28570b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
28580b57cec5SDimitry Andric     case options::OPT_ffast_math:
28590b57cec5SDimitry Andric       HonorINFs = false;
28600b57cec5SDimitry Andric       HonorNaNs = false;
28610b57cec5SDimitry Andric       MathErrno = false;
28620b57cec5SDimitry Andric       AssociativeMath = true;
28630b57cec5SDimitry Andric       ReciprocalMath = true;
28640b57cec5SDimitry Andric       SignedZeros = false;
28650b57cec5SDimitry Andric       TrappingMath = false;
2866480093f4SDimitry Andric       RoundingFPMath = false;
28670b57cec5SDimitry Andric       // If fast-math is set then set the fp-contract mode to fast.
28680b57cec5SDimitry Andric       FPContract = "fast";
28690b57cec5SDimitry Andric       break;
28700b57cec5SDimitry Andric     case options::OPT_fno_fast_math:
28710b57cec5SDimitry Andric       HonorINFs = true;
28720b57cec5SDimitry Andric       HonorNaNs = true;
28730b57cec5SDimitry Andric       // Turning on -ffast-math (with either flag) removes the need for
28740b57cec5SDimitry Andric       // MathErrno. However, turning *off* -ffast-math merely restores the
28750b57cec5SDimitry Andric       // toolchain default (which may be false).
28760b57cec5SDimitry Andric       MathErrno = TC.IsMathErrnoDefault();
28770b57cec5SDimitry Andric       AssociativeMath = false;
28780b57cec5SDimitry Andric       ReciprocalMath = false;
28790b57cec5SDimitry Andric       SignedZeros = true;
2880480093f4SDimitry Andric       TrappingMath = false;
2881480093f4SDimitry Andric       RoundingFPMath = false;
28820b57cec5SDimitry Andric       // -fno_fast_math restores default denormal and fpcontract handling
28835ffd83dbSDimitry Andric       DenormalFPMath = DefaultDenormalFPMath;
28845ffd83dbSDimitry Andric       DenormalFP32Math = llvm::DenormalMode::getIEEE();
2885fe6060f1SDimitry Andric       FPContract = "on";
28860b57cec5SDimitry Andric       break;
28870b57cec5SDimitry Andric     }
2888480093f4SDimitry Andric     if (StrictFPModel) {
2889480093f4SDimitry Andric       // If -ffp-model=strict has been specified on command line but
2890480093f4SDimitry Andric       // subsequent options conflict then emit warning diagnostic.
2891fe6060f1SDimitry Andric       if (HonorINFs && HonorNaNs && !AssociativeMath && !ReciprocalMath &&
2892480093f4SDimitry Andric           SignedZeros && TrappingMath && RoundingFPMath &&
28935ffd83dbSDimitry Andric           DenormalFPMath == llvm::DenormalMode::getIEEE() &&
2894fe6060f1SDimitry Andric           DenormalFP32Math == llvm::DenormalMode::getIEEE() &&
2895fe6060f1SDimitry Andric           FPContract.equals("off"))
2896480093f4SDimitry Andric         // OK: Current Arg doesn't conflict with -ffp-model=strict
2897480093f4SDimitry Andric         ;
2898480093f4SDimitry Andric       else {
2899480093f4SDimitry Andric         StrictFPModel = false;
2900480093f4SDimitry Andric         FPModel = "";
2901480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2902480093f4SDimitry Andric             << "-ffp-model=strict" <<
2903480093f4SDimitry Andric             ((A->getNumValues() == 0) ?  A->getSpelling()
2904480093f4SDimitry Andric             : Args.MakeArgString(A->getSpelling() + A->getValue()));
2905480093f4SDimitry Andric       }
2906480093f4SDimitry Andric     }
29070b57cec5SDimitry Andric 
29080b57cec5SDimitry Andric     // If we handled this option claim it
29090b57cec5SDimitry Andric     A->claim();
29100b57cec5SDimitry Andric   }
29110b57cec5SDimitry Andric 
29120b57cec5SDimitry Andric   if (!HonorINFs)
29130b57cec5SDimitry Andric     CmdArgs.push_back("-menable-no-infs");
29140b57cec5SDimitry Andric 
29150b57cec5SDimitry Andric   if (!HonorNaNs)
29160b57cec5SDimitry Andric     CmdArgs.push_back("-menable-no-nans");
29170b57cec5SDimitry Andric 
29180b57cec5SDimitry Andric   if (MathErrno)
29190b57cec5SDimitry Andric     CmdArgs.push_back("-fmath-errno");
29200b57cec5SDimitry Andric 
29210b57cec5SDimitry Andric   if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
29220b57cec5SDimitry Andric       !TrappingMath)
29230b57cec5SDimitry Andric     CmdArgs.push_back("-menable-unsafe-fp-math");
29240b57cec5SDimitry Andric 
29250b57cec5SDimitry Andric   if (!SignedZeros)
29260b57cec5SDimitry Andric     CmdArgs.push_back("-fno-signed-zeros");
29270b57cec5SDimitry Andric 
29280b57cec5SDimitry Andric   if (AssociativeMath && !SignedZeros && !TrappingMath)
29290b57cec5SDimitry Andric     CmdArgs.push_back("-mreassociate");
29300b57cec5SDimitry Andric 
29310b57cec5SDimitry Andric   if (ReciprocalMath)
29320b57cec5SDimitry Andric     CmdArgs.push_back("-freciprocal-math");
29330b57cec5SDimitry Andric 
2934480093f4SDimitry Andric   if (TrappingMath) {
2935480093f4SDimitry Andric     // FP Exception Behavior is also set to strict
2936480093f4SDimitry Andric     assert(FPExceptionBehavior.equals("strict"));
2937e8d8bef9SDimitry Andric   }
29380b57cec5SDimitry Andric 
29395ffd83dbSDimitry Andric   // The default is IEEE.
29405ffd83dbSDimitry Andric   if (DenormalFPMath != llvm::DenormalMode::getIEEE()) {
29415ffd83dbSDimitry Andric     llvm::SmallString<64> DenormFlag;
29425ffd83dbSDimitry Andric     llvm::raw_svector_ostream ArgStr(DenormFlag);
29435ffd83dbSDimitry Andric     ArgStr << "-fdenormal-fp-math=" << DenormalFPMath;
29445ffd83dbSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(ArgStr.str()));
29455ffd83dbSDimitry Andric   }
29465ffd83dbSDimitry Andric 
29475ffd83dbSDimitry Andric   // Add f32 specific denormal mode flag if it's different.
29485ffd83dbSDimitry Andric   if (DenormalFP32Math != DenormalFPMath) {
29495ffd83dbSDimitry Andric     llvm::SmallString<64> DenormFlag;
29505ffd83dbSDimitry Andric     llvm::raw_svector_ostream ArgStr(DenormFlag);
29515ffd83dbSDimitry Andric     ArgStr << "-fdenormal-fp-math-f32=" << DenormalFP32Math;
29525ffd83dbSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(ArgStr.str()));
29535ffd83dbSDimitry Andric   }
29540b57cec5SDimitry Andric 
29550b57cec5SDimitry Andric   if (!FPContract.empty())
29560b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract));
29570b57cec5SDimitry Andric 
2958480093f4SDimitry Andric   if (!RoundingFPMath)
2959480093f4SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-fno-rounding-math"));
2960480093f4SDimitry Andric 
2961480093f4SDimitry Andric   if (RoundingFPMath && RoundingMathPresent)
2962480093f4SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-frounding-math"));
2963480093f4SDimitry Andric 
2964480093f4SDimitry Andric   if (!FPExceptionBehavior.empty())
2965480093f4SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-ffp-exception-behavior=" +
2966480093f4SDimitry Andric                       FPExceptionBehavior));
2967480093f4SDimitry Andric 
29680b57cec5SDimitry Andric   ParseMRecip(D, Args, CmdArgs);
29690b57cec5SDimitry Andric 
29700b57cec5SDimitry Andric   // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for the
29710b57cec5SDimitry Andric   // individual features enabled by -ffast-math instead of the option itself as
29720b57cec5SDimitry Andric   // that's consistent with gcc's behaviour.
29730b57cec5SDimitry Andric   if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath &&
2974480093f4SDimitry Andric       ReciprocalMath && !SignedZeros && !TrappingMath && !RoundingFPMath) {
29750b57cec5SDimitry Andric     CmdArgs.push_back("-ffast-math");
2976480093f4SDimitry Andric     if (FPModel.equals("fast")) {
2977480093f4SDimitry Andric       if (FPContract.equals("fast"))
2978480093f4SDimitry Andric         // All set, do nothing.
2979480093f4SDimitry Andric         ;
2980480093f4SDimitry Andric       else if (FPContract.empty())
2981480093f4SDimitry Andric         // Enable -ffp-contract=fast
2982480093f4SDimitry Andric         CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
2983480093f4SDimitry Andric       else
2984480093f4SDimitry Andric         D.Diag(clang::diag::warn_drv_overriding_flag_option)
2985480093f4SDimitry Andric           << "-ffp-model=fast"
2986480093f4SDimitry Andric           << Args.MakeArgString("-ffp-contract=" + FPContract);
2987480093f4SDimitry Andric     }
2988480093f4SDimitry Andric   }
29890b57cec5SDimitry Andric 
29900b57cec5SDimitry Andric   // Handle __FINITE_MATH_ONLY__ similarly.
29910b57cec5SDimitry Andric   if (!HonorINFs && !HonorNaNs)
29920b57cec5SDimitry Andric     CmdArgs.push_back("-ffinite-math-only");
29930b57cec5SDimitry Andric 
29940b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) {
29950b57cec5SDimitry Andric     CmdArgs.push_back("-mfpmath");
29960b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
29970b57cec5SDimitry Andric   }
29980b57cec5SDimitry Andric 
29990b57cec5SDimitry Andric   // Disable a codegen optimization for floating-point casts.
30000b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fno_strict_float_cast_overflow,
30010b57cec5SDimitry Andric                    options::OPT_fstrict_float_cast_overflow, false))
30020b57cec5SDimitry Andric     CmdArgs.push_back("-fno-strict-float-cast-overflow");
30030b57cec5SDimitry Andric }
30040b57cec5SDimitry Andric 
30050b57cec5SDimitry Andric static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs,
30060b57cec5SDimitry Andric                                   const llvm::Triple &Triple,
30070b57cec5SDimitry Andric                                   const InputInfo &Input) {
30080b57cec5SDimitry Andric   // Enable region store model by default.
30090b57cec5SDimitry Andric   CmdArgs.push_back("-analyzer-store=region");
30100b57cec5SDimitry Andric 
30110b57cec5SDimitry Andric   // Treat blocks as analysis entry points.
30120b57cec5SDimitry Andric   CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
30130b57cec5SDimitry Andric 
30140b57cec5SDimitry Andric   // Add default argument set.
30150b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
30160b57cec5SDimitry Andric     CmdArgs.push_back("-analyzer-checker=core");
30170b57cec5SDimitry Andric     CmdArgs.push_back("-analyzer-checker=apiModeling");
30180b57cec5SDimitry Andric 
30190b57cec5SDimitry Andric     if (!Triple.isWindowsMSVCEnvironment()) {
30200b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix");
30210b57cec5SDimitry Andric     } else {
30220b57cec5SDimitry Andric       // Enable "unix" checkers that also work on Windows.
30230b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix.API");
30240b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix.Malloc");
30250b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix.MallocSizeof");
30260b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix.MismatchedDeallocator");
30270b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix.cstring.BadSizeArg");
30280b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=unix.cstring.NullArg");
30290b57cec5SDimitry Andric     }
30300b57cec5SDimitry Andric 
30310b57cec5SDimitry Andric     // Disable some unix checkers for PS4.
30320b57cec5SDimitry Andric     if (Triple.isPS4CPU()) {
30330b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-disable-checker=unix.API");
30340b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-disable-checker=unix.Vfork");
30350b57cec5SDimitry Andric     }
30360b57cec5SDimitry Andric 
3037480093f4SDimitry Andric     if (Triple.isOSDarwin()) {
30380b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=osx");
3039480093f4SDimitry Andric       CmdArgs.push_back(
3040480093f4SDimitry Andric           "-analyzer-checker=security.insecureAPI.decodeValueOfObjCType");
3041480093f4SDimitry Andric     }
3042480093f4SDimitry Andric     else if (Triple.isOSFuchsia())
3043480093f4SDimitry Andric       CmdArgs.push_back("-analyzer-checker=fuchsia");
30440b57cec5SDimitry Andric 
30450b57cec5SDimitry Andric     CmdArgs.push_back("-analyzer-checker=deadcode");
30460b57cec5SDimitry Andric 
30470b57cec5SDimitry Andric     if (types::isCXX(Input.getType()))
30480b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=cplusplus");
30490b57cec5SDimitry Andric 
30500b57cec5SDimitry Andric     if (!Triple.isPS4CPU()) {
30510b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn");
30520b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
30530b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
30540b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
30550b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
30560b57cec5SDimitry Andric       CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
30570b57cec5SDimitry Andric     }
30580b57cec5SDimitry Andric 
30590b57cec5SDimitry Andric     // Default nullability checks.
30600b57cec5SDimitry Andric     CmdArgs.push_back("-analyzer-checker=nullability.NullPassedToNonnull");
30610b57cec5SDimitry Andric     CmdArgs.push_back("-analyzer-checker=nullability.NullReturnedFromNonnull");
30620b57cec5SDimitry Andric   }
30630b57cec5SDimitry Andric 
30640b57cec5SDimitry Andric   // Set the output format. The default is plist, for (lame) historical reasons.
30650b57cec5SDimitry Andric   CmdArgs.push_back("-analyzer-output");
30660b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
30670b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
30680b57cec5SDimitry Andric   else
30690b57cec5SDimitry Andric     CmdArgs.push_back("plist");
30700b57cec5SDimitry Andric 
30710b57cec5SDimitry Andric   // Disable the presentation of standard compiler warnings when using
30720b57cec5SDimitry Andric   // --analyze.  We only want to show static analyzer diagnostics or frontend
30730b57cec5SDimitry Andric   // errors.
30740b57cec5SDimitry Andric   CmdArgs.push_back("-w");
30750b57cec5SDimitry Andric 
30760b57cec5SDimitry Andric   // Add -Xanalyzer arguments when running as analyzer.
30770b57cec5SDimitry Andric   Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
30780b57cec5SDimitry Andric }
30790b57cec5SDimitry Andric 
3080e8d8bef9SDimitry Andric static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
3081e8d8bef9SDimitry Andric                              const ArgList &Args, ArgStringList &CmdArgs,
3082e8d8bef9SDimitry Andric                              bool KernelOrKext) {
30830b57cec5SDimitry Andric   const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple();
30840b57cec5SDimitry Andric 
30850b57cec5SDimitry Andric   // NVPTX doesn't support stack protectors; from the compiler's perspective, it
30860b57cec5SDimitry Andric   // doesn't even have a stack!
30870b57cec5SDimitry Andric   if (EffectiveTriple.isNVPTX())
30880b57cec5SDimitry Andric     return;
30890b57cec5SDimitry Andric 
30900b57cec5SDimitry Andric   // -stack-protector=0 is default.
3091e8d8bef9SDimitry Andric   LangOptions::StackProtectorMode StackProtectorLevel = LangOptions::SSPOff;
3092e8d8bef9SDimitry Andric   LangOptions::StackProtectorMode DefaultStackProtectorLevel =
30930b57cec5SDimitry Andric       TC.GetDefaultStackProtectorLevel(KernelOrKext);
30940b57cec5SDimitry Andric 
30950b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
30960b57cec5SDimitry Andric                                options::OPT_fstack_protector_all,
30970b57cec5SDimitry Andric                                options::OPT_fstack_protector_strong,
30980b57cec5SDimitry Andric                                options::OPT_fstack_protector)) {
30990b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fstack_protector))
31000b57cec5SDimitry Andric       StackProtectorLevel =
3101e8d8bef9SDimitry Andric           std::max<>(LangOptions::SSPOn, DefaultStackProtectorLevel);
31020b57cec5SDimitry Andric     else if (A->getOption().matches(options::OPT_fstack_protector_strong))
31030b57cec5SDimitry Andric       StackProtectorLevel = LangOptions::SSPStrong;
31040b57cec5SDimitry Andric     else if (A->getOption().matches(options::OPT_fstack_protector_all))
31050b57cec5SDimitry Andric       StackProtectorLevel = LangOptions::SSPReq;
31060b57cec5SDimitry Andric   } else {
31070b57cec5SDimitry Andric     StackProtectorLevel = DefaultStackProtectorLevel;
31080b57cec5SDimitry Andric   }
31090b57cec5SDimitry Andric 
31100b57cec5SDimitry Andric   if (StackProtectorLevel) {
31110b57cec5SDimitry Andric     CmdArgs.push_back("-stack-protector");
31120b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
31130b57cec5SDimitry Andric   }
31140b57cec5SDimitry Andric 
31150b57cec5SDimitry Andric   // --param ssp-buffer-size=
31160b57cec5SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT__param)) {
31170b57cec5SDimitry Andric     StringRef Str(A->getValue());
31180b57cec5SDimitry Andric     if (Str.startswith("ssp-buffer-size=")) {
31190b57cec5SDimitry Andric       if (StackProtectorLevel) {
31200b57cec5SDimitry Andric         CmdArgs.push_back("-stack-protector-buffer-size");
31210b57cec5SDimitry Andric         // FIXME: Verify the argument is a valid integer.
31220b57cec5SDimitry Andric         CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
31230b57cec5SDimitry Andric       }
31240b57cec5SDimitry Andric       A->claim();
31250b57cec5SDimitry Andric     }
31260b57cec5SDimitry Andric   }
3127e8d8bef9SDimitry Andric 
3128e8d8bef9SDimitry Andric   const std::string &TripleStr = EffectiveTriple.getTriple();
3129e8d8bef9SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_EQ)) {
3130e8d8bef9SDimitry Andric     StringRef Value = A->getValue();
3131e8d8bef9SDimitry Andric     if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64())
3132e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
3133e8d8bef9SDimitry Andric           << A->getAsString(Args) << TripleStr;
3134fe6060f1SDimitry Andric     if (EffectiveTriple.isX86() && Value != "tls" && Value != "global") {
3135e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_invalid_value_with_suggestion)
3136fe6060f1SDimitry Andric           << A->getOption().getName() << Value << "tls global";
3137fe6060f1SDimitry Andric       return;
3138fe6060f1SDimitry Andric     }
3139fe6060f1SDimitry Andric     if (EffectiveTriple.isAArch64() && Value != "sysreg" && Value != "global") {
3140fe6060f1SDimitry Andric       D.Diag(diag::err_drv_invalid_value_with_suggestion)
3141fe6060f1SDimitry Andric           << A->getOption().getName() << Value << "sysreg global";
3142e8d8bef9SDimitry Andric       return;
3143e8d8bef9SDimitry Andric     }
3144e8d8bef9SDimitry Andric     A->render(Args, CmdArgs);
3145e8d8bef9SDimitry Andric   }
3146e8d8bef9SDimitry Andric 
3147e8d8bef9SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_offset_EQ)) {
3148e8d8bef9SDimitry Andric     StringRef Value = A->getValue();
3149fe6060f1SDimitry Andric     if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64())
3150e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
3151e8d8bef9SDimitry Andric           << A->getAsString(Args) << TripleStr;
3152fe6060f1SDimitry Andric     int Offset;
3153e8d8bef9SDimitry Andric     if (Value.getAsInteger(10, Offset)) {
3154e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value;
3155e8d8bef9SDimitry Andric       return;
3156e8d8bef9SDimitry Andric     }
3157e8d8bef9SDimitry Andric     A->render(Args, CmdArgs);
3158e8d8bef9SDimitry Andric   }
3159e8d8bef9SDimitry Andric 
3160e8d8bef9SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_reg_EQ)) {
3161e8d8bef9SDimitry Andric     StringRef Value = A->getValue();
3162fe6060f1SDimitry Andric     if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64())
3163e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
3164e8d8bef9SDimitry Andric           << A->getAsString(Args) << TripleStr;
3165e8d8bef9SDimitry Andric     if (EffectiveTriple.isX86() && (Value != "fs" && Value != "gs")) {
3166e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_invalid_value_with_suggestion)
3167fe6060f1SDimitry Andric           << A->getOption().getName() << Value << "fs gs";
3168fe6060f1SDimitry Andric       return;
3169fe6060f1SDimitry Andric     }
3170fe6060f1SDimitry Andric     if (EffectiveTriple.isAArch64() && Value != "sp_el0") {
3171fe6060f1SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value;
3172e8d8bef9SDimitry Andric       return;
3173e8d8bef9SDimitry Andric     }
3174e8d8bef9SDimitry Andric     A->render(Args, CmdArgs);
3175e8d8bef9SDimitry Andric   }
31760b57cec5SDimitry Andric }
31770b57cec5SDimitry Andric 
31785ffd83dbSDimitry Andric static void RenderSCPOptions(const ToolChain &TC, const ArgList &Args,
31795ffd83dbSDimitry Andric                              ArgStringList &CmdArgs) {
31805ffd83dbSDimitry Andric   const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple();
31815ffd83dbSDimitry Andric 
31823063e1e5SEd Maste   if (!EffectiveTriple.isOSFreeBSD() && !EffectiveTriple.isOSLinux())
31835ffd83dbSDimitry Andric     return;
31845ffd83dbSDimitry Andric 
31855ffd83dbSDimitry Andric   if (!EffectiveTriple.isX86() && !EffectiveTriple.isSystemZ() &&
31865ffd83dbSDimitry Andric       !EffectiveTriple.isPPC64())
31875ffd83dbSDimitry Andric     return;
31885ffd83dbSDimitry Andric 
31895ffd83dbSDimitry Andric   if (Args.hasFlag(options::OPT_fstack_clash_protection,
319075b4d546SDimitry Andric                    options::OPT_fno_stack_clash_protection, false))
31915ffd83dbSDimitry Andric     CmdArgs.push_back("-fstack-clash-protection");
31925ffd83dbSDimitry Andric }
31935ffd83dbSDimitry Andric 
31940b57cec5SDimitry Andric static void RenderTrivialAutoVarInitOptions(const Driver &D,
31950b57cec5SDimitry Andric                                             const ToolChain &TC,
31960b57cec5SDimitry Andric                                             const ArgList &Args,
31970b57cec5SDimitry Andric                                             ArgStringList &CmdArgs) {
31980b57cec5SDimitry Andric   auto DefaultTrivialAutoVarInit = TC.GetDefaultTrivialAutoVarInit();
31990b57cec5SDimitry Andric   StringRef TrivialAutoVarInit = "";
32000b57cec5SDimitry Andric 
32010b57cec5SDimitry Andric   for (const Arg *A : Args) {
32020b57cec5SDimitry Andric     switch (A->getOption().getID()) {
32030b57cec5SDimitry Andric     default:
32040b57cec5SDimitry Andric       continue;
32050b57cec5SDimitry Andric     case options::OPT_ftrivial_auto_var_init: {
32060b57cec5SDimitry Andric       A->claim();
32070b57cec5SDimitry Andric       StringRef Val = A->getValue();
32080b57cec5SDimitry Andric       if (Val == "uninitialized" || Val == "zero" || Val == "pattern")
32090b57cec5SDimitry Andric         TrivialAutoVarInit = Val;
32100b57cec5SDimitry Andric       else
32110b57cec5SDimitry Andric         D.Diag(diag::err_drv_unsupported_option_argument)
32120b57cec5SDimitry Andric             << A->getOption().getName() << Val;
32130b57cec5SDimitry Andric       break;
32140b57cec5SDimitry Andric     }
32150b57cec5SDimitry Andric     }
32160b57cec5SDimitry Andric   }
32170b57cec5SDimitry Andric 
32180b57cec5SDimitry Andric   if (TrivialAutoVarInit.empty())
32190b57cec5SDimitry Andric     switch (DefaultTrivialAutoVarInit) {
32200b57cec5SDimitry Andric     case LangOptions::TrivialAutoVarInitKind::Uninitialized:
32210b57cec5SDimitry Andric       break;
32220b57cec5SDimitry Andric     case LangOptions::TrivialAutoVarInitKind::Pattern:
32230b57cec5SDimitry Andric       TrivialAutoVarInit = "pattern";
32240b57cec5SDimitry Andric       break;
32250b57cec5SDimitry Andric     case LangOptions::TrivialAutoVarInitKind::Zero:
32260b57cec5SDimitry Andric       TrivialAutoVarInit = "zero";
32270b57cec5SDimitry Andric       break;
32280b57cec5SDimitry Andric     }
32290b57cec5SDimitry Andric 
32300b57cec5SDimitry Andric   if (!TrivialAutoVarInit.empty()) {
32310b57cec5SDimitry Andric     if (TrivialAutoVarInit == "zero" && !Args.hasArg(options::OPT_enable_trivial_var_init_zero))
32320b57cec5SDimitry Andric       D.Diag(diag::err_drv_trivial_auto_var_init_zero_disabled);
32330b57cec5SDimitry Andric     CmdArgs.push_back(
32340b57cec5SDimitry Andric         Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit));
32350b57cec5SDimitry Andric   }
32365ffd83dbSDimitry Andric 
32375ffd83dbSDimitry Andric   if (Arg *A =
32385ffd83dbSDimitry Andric           Args.getLastArg(options::OPT_ftrivial_auto_var_init_stop_after)) {
32395ffd83dbSDimitry Andric     if (!Args.hasArg(options::OPT_ftrivial_auto_var_init) ||
32405ffd83dbSDimitry Andric         StringRef(
32415ffd83dbSDimitry Andric             Args.getLastArg(options::OPT_ftrivial_auto_var_init)->getValue()) ==
32425ffd83dbSDimitry Andric             "uninitialized")
32435ffd83dbSDimitry Andric       D.Diag(diag::err_drv_trivial_auto_var_init_stop_after_missing_dependency);
32445ffd83dbSDimitry Andric     A->claim();
32455ffd83dbSDimitry Andric     StringRef Val = A->getValue();
32465ffd83dbSDimitry Andric     if (std::stoi(Val.str()) <= 0)
32475ffd83dbSDimitry Andric       D.Diag(diag::err_drv_trivial_auto_var_init_stop_after_invalid_value);
32485ffd83dbSDimitry Andric     CmdArgs.push_back(
32495ffd83dbSDimitry Andric         Args.MakeArgString("-ftrivial-auto-var-init-stop-after=" + Val));
32505ffd83dbSDimitry Andric   }
32510b57cec5SDimitry Andric }
32520b57cec5SDimitry Andric 
3253fe6060f1SDimitry Andric static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs,
3254fe6060f1SDimitry Andric                                 types::ID InputType) {
32555ffd83dbSDimitry Andric   // cl-denorms-are-zero is not forwarded. It is translated into a generic flag
32565ffd83dbSDimitry Andric   // for denormal flushing handling based on the target.
32570b57cec5SDimitry Andric   const unsigned ForwardedArguments[] = {
32580b57cec5SDimitry Andric       options::OPT_cl_opt_disable,
32590b57cec5SDimitry Andric       options::OPT_cl_strict_aliasing,
32600b57cec5SDimitry Andric       options::OPT_cl_single_precision_constant,
32610b57cec5SDimitry Andric       options::OPT_cl_finite_math_only,
32620b57cec5SDimitry Andric       options::OPT_cl_kernel_arg_info,
32630b57cec5SDimitry Andric       options::OPT_cl_unsafe_math_optimizations,
32640b57cec5SDimitry Andric       options::OPT_cl_fast_relaxed_math,
32650b57cec5SDimitry Andric       options::OPT_cl_mad_enable,
32660b57cec5SDimitry Andric       options::OPT_cl_no_signed_zeros,
32670b57cec5SDimitry Andric       options::OPT_cl_fp32_correctly_rounded_divide_sqrt,
32680b57cec5SDimitry Andric       options::OPT_cl_uniform_work_group_size
32690b57cec5SDimitry Andric   };
32700b57cec5SDimitry Andric 
32710b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_cl_std_EQ)) {
32720b57cec5SDimitry Andric     std::string CLStdStr = std::string("-cl-std=") + A->getValue();
32730b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(CLStdStr));
32740b57cec5SDimitry Andric   }
32750b57cec5SDimitry Andric 
32760b57cec5SDimitry Andric   for (const auto &Arg : ForwardedArguments)
32770b57cec5SDimitry Andric     if (const auto *A = Args.getLastArg(Arg))
32780b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName()));
3279fe6060f1SDimitry Andric 
3280fe6060f1SDimitry Andric   // Only add the default headers if we are compiling OpenCL sources.
3281fe6060f1SDimitry Andric   if ((types::isOpenCL(InputType) ||
3282fe6060f1SDimitry Andric        (Args.hasArg(options::OPT_cl_std_EQ) && types::isSrcFile(InputType))) &&
3283fe6060f1SDimitry Andric       !Args.hasArg(options::OPT_cl_no_stdinc)) {
3284fe6060f1SDimitry Andric     CmdArgs.push_back("-finclude-default-header");
3285fe6060f1SDimitry Andric     CmdArgs.push_back("-fdeclare-opencl-builtins");
3286fe6060f1SDimitry Andric   }
32870b57cec5SDimitry Andric }
32880b57cec5SDimitry Andric 
32890b57cec5SDimitry Andric static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args,
32900b57cec5SDimitry Andric                                         ArgStringList &CmdArgs) {
32910b57cec5SDimitry Andric   bool ARCMTEnabled = false;
32920b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) {
32930b57cec5SDimitry Andric     if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
32940b57cec5SDimitry Andric                                        options::OPT_ccc_arcmt_modify,
32950b57cec5SDimitry Andric                                        options::OPT_ccc_arcmt_migrate)) {
32960b57cec5SDimitry Andric       ARCMTEnabled = true;
32970b57cec5SDimitry Andric       switch (A->getOption().getID()) {
32980b57cec5SDimitry Andric       default: llvm_unreachable("missed a case");
32990b57cec5SDimitry Andric       case options::OPT_ccc_arcmt_check:
3300e8d8bef9SDimitry Andric         CmdArgs.push_back("-arcmt-action=check");
33010b57cec5SDimitry Andric         break;
33020b57cec5SDimitry Andric       case options::OPT_ccc_arcmt_modify:
3303e8d8bef9SDimitry Andric         CmdArgs.push_back("-arcmt-action=modify");
33040b57cec5SDimitry Andric         break;
33050b57cec5SDimitry Andric       case options::OPT_ccc_arcmt_migrate:
3306e8d8bef9SDimitry Andric         CmdArgs.push_back("-arcmt-action=migrate");
33070b57cec5SDimitry Andric         CmdArgs.push_back("-mt-migrate-directory");
33080b57cec5SDimitry Andric         CmdArgs.push_back(A->getValue());
33090b57cec5SDimitry Andric 
33100b57cec5SDimitry Andric         Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
33110b57cec5SDimitry Andric         Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
33120b57cec5SDimitry Andric         break;
33130b57cec5SDimitry Andric       }
33140b57cec5SDimitry Andric     }
33150b57cec5SDimitry Andric   } else {
33160b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_ccc_arcmt_check);
33170b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify);
33180b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate);
33190b57cec5SDimitry Andric   }
33200b57cec5SDimitry Andric 
33210b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
33220b57cec5SDimitry Andric     if (ARCMTEnabled)
33230b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_not_allowed_with)
33240b57cec5SDimitry Andric           << A->getAsString(Args) << "-ccc-arcmt-migrate";
33250b57cec5SDimitry Andric 
33260b57cec5SDimitry Andric     CmdArgs.push_back("-mt-migrate-directory");
33270b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
33280b57cec5SDimitry Andric 
33290b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
33300b57cec5SDimitry Andric                      options::OPT_objcmt_migrate_subscripting,
33310b57cec5SDimitry Andric                      options::OPT_objcmt_migrate_property)) {
33320b57cec5SDimitry Andric       // None specified, means enable them all.
33330b57cec5SDimitry Andric       CmdArgs.push_back("-objcmt-migrate-literals");
33340b57cec5SDimitry Andric       CmdArgs.push_back("-objcmt-migrate-subscripting");
33350b57cec5SDimitry Andric       CmdArgs.push_back("-objcmt-migrate-property");
33360b57cec5SDimitry Andric     } else {
33370b57cec5SDimitry Andric       Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
33380b57cec5SDimitry Andric       Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
33390b57cec5SDimitry Andric       Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
33400b57cec5SDimitry Andric     }
33410b57cec5SDimitry Andric   } else {
33420b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
33430b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
33440b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
33450b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all);
33460b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property);
33470b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property);
33480b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property_dot_syntax);
33490b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation);
33500b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype);
33510b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros);
33520b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance);
33530b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property);
33540b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property);
33550b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly);
33560b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init);
33570b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_objcmt_whitelist_dir_path);
33580b57cec5SDimitry Andric   }
33590b57cec5SDimitry Andric }
33600b57cec5SDimitry Andric 
33610b57cec5SDimitry Andric static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T,
33620b57cec5SDimitry Andric                                  const ArgList &Args, ArgStringList &CmdArgs) {
33630b57cec5SDimitry Andric   // -fbuiltin is default unless -mkernel is used.
33640b57cec5SDimitry Andric   bool UseBuiltins =
33650b57cec5SDimitry Andric       Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin,
33660b57cec5SDimitry Andric                    !Args.hasArg(options::OPT_mkernel));
33670b57cec5SDimitry Andric   if (!UseBuiltins)
33680b57cec5SDimitry Andric     CmdArgs.push_back("-fno-builtin");
33690b57cec5SDimitry Andric 
33700b57cec5SDimitry Andric   // -ffreestanding implies -fno-builtin.
33710b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_ffreestanding))
33720b57cec5SDimitry Andric     UseBuiltins = false;
33730b57cec5SDimitry Andric 
33740b57cec5SDimitry Andric   // Process the -fno-builtin-* options.
33750b57cec5SDimitry Andric   for (const auto &Arg : Args) {
33760b57cec5SDimitry Andric     const Option &O = Arg->getOption();
33770b57cec5SDimitry Andric     if (!O.matches(options::OPT_fno_builtin_))
33780b57cec5SDimitry Andric       continue;
33790b57cec5SDimitry Andric 
33800b57cec5SDimitry Andric     Arg->claim();
33810b57cec5SDimitry Andric 
33820b57cec5SDimitry Andric     // If -fno-builtin is specified, then there's no need to pass the option to
33830b57cec5SDimitry Andric     // the frontend.
33840b57cec5SDimitry Andric     if (!UseBuiltins)
33850b57cec5SDimitry Andric       continue;
33860b57cec5SDimitry Andric 
33870b57cec5SDimitry Andric     StringRef FuncName = Arg->getValue();
33880b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-fno-builtin-" + FuncName));
33890b57cec5SDimitry Andric   }
33900b57cec5SDimitry Andric 
33910b57cec5SDimitry Andric   // le32-specific flags:
33920b57cec5SDimitry Andric   //  -fno-math-builtin: clang should not convert math builtins to intrinsics
33930b57cec5SDimitry Andric   //                     by default.
33940b57cec5SDimitry Andric   if (TC.getArch() == llvm::Triple::le32)
33950b57cec5SDimitry Andric     CmdArgs.push_back("-fno-math-builtin");
33960b57cec5SDimitry Andric }
33970b57cec5SDimitry Andric 
33985ffd83dbSDimitry Andric bool Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) {
33995ffd83dbSDimitry Andric   if (llvm::sys::path::cache_directory(Result)) {
34005ffd83dbSDimitry Andric     llvm::sys::path::append(Result, "clang");
34010b57cec5SDimitry Andric     llvm::sys::path::append(Result, "ModuleCache");
34025ffd83dbSDimitry Andric     return true;
34035ffd83dbSDimitry Andric   }
34045ffd83dbSDimitry Andric   return false;
34050b57cec5SDimitry Andric }
34060b57cec5SDimitry Andric 
34070b57cec5SDimitry Andric static void RenderModulesOptions(Compilation &C, const Driver &D,
34080b57cec5SDimitry Andric                                  const ArgList &Args, const InputInfo &Input,
34090b57cec5SDimitry Andric                                  const InputInfo &Output,
34100b57cec5SDimitry Andric                                  ArgStringList &CmdArgs, bool &HaveModules) {
34110b57cec5SDimitry Andric   // -fmodules enables the use of precompiled modules (off by default).
34120b57cec5SDimitry Andric   // Users can pass -fno-cxx-modules to turn off modules support for
34130b57cec5SDimitry Andric   // C++/Objective-C++ programs.
34140b57cec5SDimitry Andric   bool HaveClangModules = false;
34150b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
34160b57cec5SDimitry Andric     bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
34170b57cec5SDimitry Andric                                      options::OPT_fno_cxx_modules, true);
34180b57cec5SDimitry Andric     if (AllowedInCXX || !types::isCXX(Input.getType())) {
34190b57cec5SDimitry Andric       CmdArgs.push_back("-fmodules");
34200b57cec5SDimitry Andric       HaveClangModules = true;
34210b57cec5SDimitry Andric     }
34220b57cec5SDimitry Andric   }
34230b57cec5SDimitry Andric 
34240b57cec5SDimitry Andric   HaveModules |= HaveClangModules;
34250b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fmodules_ts)) {
34260b57cec5SDimitry Andric     CmdArgs.push_back("-fmodules-ts");
34270b57cec5SDimitry Andric     HaveModules = true;
34280b57cec5SDimitry Andric   }
34290b57cec5SDimitry Andric 
34300b57cec5SDimitry Andric   // -fmodule-maps enables implicit reading of module map files. By default,
34310b57cec5SDimitry Andric   // this is enabled if we are using Clang's flavor of precompiled modules.
34320b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fimplicit_module_maps,
34330b57cec5SDimitry Andric                    options::OPT_fno_implicit_module_maps, HaveClangModules))
34340b57cec5SDimitry Andric     CmdArgs.push_back("-fimplicit-module-maps");
34350b57cec5SDimitry Andric 
34360b57cec5SDimitry Andric   // -fmodules-decluse checks that modules used are declared so (off by default)
34370b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fmodules_decluse,
34380b57cec5SDimitry Andric                    options::OPT_fno_modules_decluse, false))
34390b57cec5SDimitry Andric     CmdArgs.push_back("-fmodules-decluse");
34400b57cec5SDimitry Andric 
34410b57cec5SDimitry Andric   // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that
34420b57cec5SDimitry Andric   // all #included headers are part of modules.
34430b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fmodules_strict_decluse,
34440b57cec5SDimitry Andric                    options::OPT_fno_modules_strict_decluse, false))
34450b57cec5SDimitry Andric     CmdArgs.push_back("-fmodules-strict-decluse");
34460b57cec5SDimitry Andric 
34470b57cec5SDimitry Andric   // -fno-implicit-modules turns off implicitly compiling modules on demand.
34480b57cec5SDimitry Andric   bool ImplicitModules = false;
34490b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fimplicit_modules,
34500b57cec5SDimitry Andric                     options::OPT_fno_implicit_modules, HaveClangModules)) {
34510b57cec5SDimitry Andric     if (HaveModules)
34520b57cec5SDimitry Andric       CmdArgs.push_back("-fno-implicit-modules");
34530b57cec5SDimitry Andric   } else if (HaveModules) {
34540b57cec5SDimitry Andric     ImplicitModules = true;
34550b57cec5SDimitry Andric     // -fmodule-cache-path specifies where our implicitly-built module files
34560b57cec5SDimitry Andric     // should be written.
34570b57cec5SDimitry Andric     SmallString<128> Path;
34580b57cec5SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path))
34590b57cec5SDimitry Andric       Path = A->getValue();
34600b57cec5SDimitry Andric 
34615ffd83dbSDimitry Andric     bool HasPath = true;
34620b57cec5SDimitry Andric     if (C.isForDiagnostics()) {
34630b57cec5SDimitry Andric       // When generating crash reports, we want to emit the modules along with
34640b57cec5SDimitry Andric       // the reproduction sources, so we ignore any provided module path.
34650b57cec5SDimitry Andric       Path = Output.getFilename();
34660b57cec5SDimitry Andric       llvm::sys::path::replace_extension(Path, ".cache");
34670b57cec5SDimitry Andric       llvm::sys::path::append(Path, "modules");
34680b57cec5SDimitry Andric     } else if (Path.empty()) {
34690b57cec5SDimitry Andric       // No module path was provided: use the default.
34705ffd83dbSDimitry Andric       HasPath = Driver::getDefaultModuleCachePath(Path);
34710b57cec5SDimitry Andric     }
34720b57cec5SDimitry Andric 
34735ffd83dbSDimitry Andric     // `HasPath` will only be false if getDefaultModuleCachePath() fails.
34745ffd83dbSDimitry Andric     // That being said, that failure is unlikely and not caching is harmless.
34755ffd83dbSDimitry Andric     if (HasPath) {
34760b57cec5SDimitry Andric       const char Arg[] = "-fmodules-cache-path=";
34770b57cec5SDimitry Andric       Path.insert(Path.begin(), Arg, Arg + strlen(Arg));
34780b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(Path));
34790b57cec5SDimitry Andric     }
34805ffd83dbSDimitry Andric   }
34810b57cec5SDimitry Andric 
34820b57cec5SDimitry Andric   if (HaveModules) {
34830b57cec5SDimitry Andric     // -fprebuilt-module-path specifies where to load the prebuilt module files.
34840b57cec5SDimitry Andric     for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) {
34850b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(
34860b57cec5SDimitry Andric           std::string("-fprebuilt-module-path=") + A->getValue()));
34870b57cec5SDimitry Andric       A->claim();
34880b57cec5SDimitry Andric     }
3489e8d8bef9SDimitry Andric     if (Args.hasFlag(options::OPT_fprebuilt_implicit_modules,
3490e8d8bef9SDimitry Andric                      options::OPT_fno_prebuilt_implicit_modules, false))
3491e8d8bef9SDimitry Andric       CmdArgs.push_back("-fprebuilt-implicit-modules");
3492a7dea167SDimitry Andric     if (Args.hasFlag(options::OPT_fmodules_validate_input_files_content,
3493a7dea167SDimitry Andric                      options::OPT_fno_modules_validate_input_files_content,
3494a7dea167SDimitry Andric                      false))
3495a7dea167SDimitry Andric       CmdArgs.push_back("-fvalidate-ast-input-files-content");
34960b57cec5SDimitry Andric   }
34970b57cec5SDimitry Andric 
34980b57cec5SDimitry Andric   // -fmodule-name specifies the module that is currently being built (or
34990b57cec5SDimitry Andric   // used for header checking by -fmodule-maps).
35000b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fmodule_name_EQ);
35010b57cec5SDimitry Andric 
35020b57cec5SDimitry Andric   // -fmodule-map-file can be used to specify files containing module
35030b57cec5SDimitry Andric   // definitions.
35040b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file);
35050b57cec5SDimitry Andric 
35060b57cec5SDimitry Andric   // -fbuiltin-module-map can be used to load the clang
35070b57cec5SDimitry Andric   // builtin headers modulemap file.
35080b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fbuiltin_module_map)) {
35090b57cec5SDimitry Andric     SmallString<128> BuiltinModuleMap(D.ResourceDir);
35100b57cec5SDimitry Andric     llvm::sys::path::append(BuiltinModuleMap, "include");
35110b57cec5SDimitry Andric     llvm::sys::path::append(BuiltinModuleMap, "module.modulemap");
35120b57cec5SDimitry Andric     if (llvm::sys::fs::exists(BuiltinModuleMap))
35130b57cec5SDimitry Andric       CmdArgs.push_back(
35140b57cec5SDimitry Andric           Args.MakeArgString("-fmodule-map-file=" + BuiltinModuleMap));
35150b57cec5SDimitry Andric   }
35160b57cec5SDimitry Andric 
35170b57cec5SDimitry Andric   // The -fmodule-file=<name>=<file> form specifies the mapping of module
35180b57cec5SDimitry Andric   // names to precompiled module files (the module is loaded only if used).
35190b57cec5SDimitry Andric   // The -fmodule-file=<file> form can be used to unconditionally load
35200b57cec5SDimitry Andric   // precompiled module files (whether used or not).
35210b57cec5SDimitry Andric   if (HaveModules)
35220b57cec5SDimitry Andric     Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file);
35230b57cec5SDimitry Andric   else
35240b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fmodule_file);
35250b57cec5SDimitry Andric 
35260b57cec5SDimitry Andric   // When building modules and generating crashdumps, we need to dump a module
35270b57cec5SDimitry Andric   // dependency VFS alongside the output.
35280b57cec5SDimitry Andric   if (HaveClangModules && C.isForDiagnostics()) {
35290b57cec5SDimitry Andric     SmallString<128> VFSDir(Output.getFilename());
35300b57cec5SDimitry Andric     llvm::sys::path::replace_extension(VFSDir, ".cache");
35310b57cec5SDimitry Andric     // Add the cache directory as a temp so the crash diagnostics pick it up.
35320b57cec5SDimitry Andric     C.addTempFile(Args.MakeArgString(VFSDir));
35330b57cec5SDimitry Andric 
35340b57cec5SDimitry Andric     llvm::sys::path::append(VFSDir, "vfs");
35350b57cec5SDimitry Andric     CmdArgs.push_back("-module-dependency-dir");
35360b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(VFSDir));
35370b57cec5SDimitry Andric   }
35380b57cec5SDimitry Andric 
35390b57cec5SDimitry Andric   if (HaveClangModules)
35400b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path);
35410b57cec5SDimitry Andric 
35420b57cec5SDimitry Andric   // Pass through all -fmodules-ignore-macro arguments.
35430b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
35440b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
35450b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
35460b57cec5SDimitry Andric 
35470b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp);
35480b57cec5SDimitry Andric 
35490b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) {
35500b57cec5SDimitry Andric     if (Args.hasArg(options::OPT_fbuild_session_timestamp))
35510b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_not_allowed_with)
35520b57cec5SDimitry Andric           << A->getAsString(Args) << "-fbuild-session-timestamp";
35530b57cec5SDimitry Andric 
35540b57cec5SDimitry Andric     llvm::sys::fs::file_status Status;
35550b57cec5SDimitry Andric     if (llvm::sys::fs::status(A->getValue(), Status))
35560b57cec5SDimitry Andric       D.Diag(diag::err_drv_no_such_file) << A->getValue();
35570b57cec5SDimitry Andric     CmdArgs.push_back(
35580b57cec5SDimitry Andric         Args.MakeArgString("-fbuild-session-timestamp=" +
35590b57cec5SDimitry Andric                            Twine((uint64_t)Status.getLastModificationTime()
35600b57cec5SDimitry Andric                                      .time_since_epoch()
35610b57cec5SDimitry Andric                                      .count())));
35620b57cec5SDimitry Andric   }
35630b57cec5SDimitry Andric 
35640b57cec5SDimitry Andric   if (Args.getLastArg(options::OPT_fmodules_validate_once_per_build_session)) {
35650b57cec5SDimitry Andric     if (!Args.getLastArg(options::OPT_fbuild_session_timestamp,
35660b57cec5SDimitry Andric                          options::OPT_fbuild_session_file))
35670b57cec5SDimitry Andric       D.Diag(diag::err_drv_modules_validate_once_requires_timestamp);
35680b57cec5SDimitry Andric 
35690b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs,
35700b57cec5SDimitry Andric                     options::OPT_fmodules_validate_once_per_build_session);
35710b57cec5SDimitry Andric   }
35720b57cec5SDimitry Andric 
35730b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fmodules_validate_system_headers,
35740b57cec5SDimitry Andric                    options::OPT_fno_modules_validate_system_headers,
35750b57cec5SDimitry Andric                    ImplicitModules))
35760b57cec5SDimitry Andric     CmdArgs.push_back("-fmodules-validate-system-headers");
35770b57cec5SDimitry Andric 
35780b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fmodules_disable_diagnostic_validation);
35790b57cec5SDimitry Andric }
35800b57cec5SDimitry Andric 
35810b57cec5SDimitry Andric static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T,
35820b57cec5SDimitry Andric                                    ArgStringList &CmdArgs) {
35830b57cec5SDimitry Andric   // -fsigned-char is default.
35840b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fsigned_char,
35850b57cec5SDimitry Andric                                      options::OPT_fno_signed_char,
35860b57cec5SDimitry Andric                                      options::OPT_funsigned_char,
35870b57cec5SDimitry Andric                                      options::OPT_fno_unsigned_char)) {
35880b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_funsigned_char) ||
35890b57cec5SDimitry Andric         A->getOption().matches(options::OPT_fno_signed_char)) {
35900b57cec5SDimitry Andric       CmdArgs.push_back("-fno-signed-char");
35910b57cec5SDimitry Andric     }
35920b57cec5SDimitry Andric   } else if (!isSignedCharDefault(T)) {
35930b57cec5SDimitry Andric     CmdArgs.push_back("-fno-signed-char");
35940b57cec5SDimitry Andric   }
35950b57cec5SDimitry Andric 
35960b57cec5SDimitry Andric   // The default depends on the language standard.
35970b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fchar8__t, options::OPT_fno_char8__t);
35980b57cec5SDimitry Andric 
35990b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fshort_wchar,
36000b57cec5SDimitry Andric                                      options::OPT_fno_short_wchar)) {
36010b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fshort_wchar)) {
36020b57cec5SDimitry Andric       CmdArgs.push_back("-fwchar-type=short");
36030b57cec5SDimitry Andric       CmdArgs.push_back("-fno-signed-wchar");
36040b57cec5SDimitry Andric     } else {
36050b57cec5SDimitry Andric       bool IsARM = T.isARM() || T.isThumb() || T.isAArch64();
36060b57cec5SDimitry Andric       CmdArgs.push_back("-fwchar-type=int");
3607e8d8bef9SDimitry Andric       if (T.isOSzOS() ||
3608e8d8bef9SDimitry Andric           (IsARM && !(T.isOSWindows() || T.isOSNetBSD() || T.isOSOpenBSD())))
36090b57cec5SDimitry Andric         CmdArgs.push_back("-fno-signed-wchar");
36100b57cec5SDimitry Andric       else
36110b57cec5SDimitry Andric         CmdArgs.push_back("-fsigned-wchar");
36120b57cec5SDimitry Andric     }
36130b57cec5SDimitry Andric   }
36140b57cec5SDimitry Andric }
36150b57cec5SDimitry Andric 
36160b57cec5SDimitry Andric static void RenderObjCOptions(const ToolChain &TC, const Driver &D,
36170b57cec5SDimitry Andric                               const llvm::Triple &T, const ArgList &Args,
36180b57cec5SDimitry Andric                               ObjCRuntime &Runtime, bool InferCovariantReturns,
36190b57cec5SDimitry Andric                               const InputInfo &Input, ArgStringList &CmdArgs) {
36200b57cec5SDimitry Andric   const llvm::Triple::ArchType Arch = TC.getArch();
36210b57cec5SDimitry Andric 
36220b57cec5SDimitry Andric   // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and legacy
36230b57cec5SDimitry Andric   // is the default. Except for deployment target of 10.5, next runtime is
36240b57cec5SDimitry Andric   // always legacy dispatch and -fno-objc-legacy-dispatch gets ignored silently.
36250b57cec5SDimitry Andric   if (Runtime.isNonFragile()) {
36260b57cec5SDimitry Andric     if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
36270b57cec5SDimitry Andric                       options::OPT_fno_objc_legacy_dispatch,
36280b57cec5SDimitry Andric                       Runtime.isLegacyDispatchDefaultForArch(Arch))) {
36290b57cec5SDimitry Andric       if (TC.UseObjCMixedDispatch())
36300b57cec5SDimitry Andric         CmdArgs.push_back("-fobjc-dispatch-method=mixed");
36310b57cec5SDimitry Andric       else
36320b57cec5SDimitry Andric         CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
36330b57cec5SDimitry Andric     }
36340b57cec5SDimitry Andric   }
36350b57cec5SDimitry Andric 
36360b57cec5SDimitry Andric   // When ObjectiveC legacy runtime is in effect on MacOSX, turn on the option
36370b57cec5SDimitry Andric   // to do Array/Dictionary subscripting by default.
36380b57cec5SDimitry Andric   if (Arch == llvm::Triple::x86 && T.isMacOSX() &&
36390b57cec5SDimitry Andric       Runtime.getKind() == ObjCRuntime::FragileMacOSX && Runtime.isNeXTFamily())
36400b57cec5SDimitry Andric     CmdArgs.push_back("-fobjc-subscripting-legacy-runtime");
36410b57cec5SDimitry Andric 
36420b57cec5SDimitry Andric   // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
36430b57cec5SDimitry Andric   // NOTE: This logic is duplicated in ToolChains.cpp.
36440b57cec5SDimitry Andric   if (isObjCAutoRefCount(Args)) {
36450b57cec5SDimitry Andric     TC.CheckObjCARC();
36460b57cec5SDimitry Andric 
36470b57cec5SDimitry Andric     CmdArgs.push_back("-fobjc-arc");
36480b57cec5SDimitry Andric 
36490b57cec5SDimitry Andric     // FIXME: It seems like this entire block, and several around it should be
36500b57cec5SDimitry Andric     // wrapped in isObjC, but for now we just use it here as this is where it
36510b57cec5SDimitry Andric     // was being used previously.
36520b57cec5SDimitry Andric     if (types::isCXX(Input.getType()) && types::isObjC(Input.getType())) {
36530b57cec5SDimitry Andric       if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
36540b57cec5SDimitry Andric         CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
36550b57cec5SDimitry Andric       else
36560b57cec5SDimitry Andric         CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
36570b57cec5SDimitry Andric     }
36580b57cec5SDimitry Andric 
36590b57cec5SDimitry Andric     // Allow the user to enable full exceptions code emission.
36600b57cec5SDimitry Andric     // We default off for Objective-C, on for Objective-C++.
36610b57cec5SDimitry Andric     if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
36620b57cec5SDimitry Andric                      options::OPT_fno_objc_arc_exceptions,
36630b57cec5SDimitry Andric                      /*Default=*/types::isCXX(Input.getType())))
36640b57cec5SDimitry Andric       CmdArgs.push_back("-fobjc-arc-exceptions");
36650b57cec5SDimitry Andric   }
36660b57cec5SDimitry Andric 
36670b57cec5SDimitry Andric   // Silence warning for full exception code emission options when explicitly
36680b57cec5SDimitry Andric   // set to use no ARC.
36690b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fno_objc_arc)) {
36700b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fobjc_arc_exceptions);
36710b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_fno_objc_arc_exceptions);
36720b57cec5SDimitry Andric   }
36730b57cec5SDimitry Andric 
36740b57cec5SDimitry Andric   // Allow the user to control whether messages can be converted to runtime
36750b57cec5SDimitry Andric   // functions.
36760b57cec5SDimitry Andric   if (types::isObjC(Input.getType())) {
36770b57cec5SDimitry Andric     auto *Arg = Args.getLastArg(
36780b57cec5SDimitry Andric         options::OPT_fobjc_convert_messages_to_runtime_calls,
36790b57cec5SDimitry Andric         options::OPT_fno_objc_convert_messages_to_runtime_calls);
36800b57cec5SDimitry Andric     if (Arg &&
36810b57cec5SDimitry Andric         Arg->getOption().matches(
36820b57cec5SDimitry Andric             options::OPT_fno_objc_convert_messages_to_runtime_calls))
36830b57cec5SDimitry Andric       CmdArgs.push_back("-fno-objc-convert-messages-to-runtime-calls");
36840b57cec5SDimitry Andric   }
36850b57cec5SDimitry Andric 
36860b57cec5SDimitry Andric   // -fobjc-infer-related-result-type is the default, except in the Objective-C
36870b57cec5SDimitry Andric   // rewriter.
36880b57cec5SDimitry Andric   if (InferCovariantReturns)
36890b57cec5SDimitry Andric     CmdArgs.push_back("-fno-objc-infer-related-result-type");
36900b57cec5SDimitry Andric 
36910b57cec5SDimitry Andric   // Pass down -fobjc-weak or -fno-objc-weak if present.
36920b57cec5SDimitry Andric   if (types::isObjC(Input.getType())) {
36930b57cec5SDimitry Andric     auto WeakArg =
36940b57cec5SDimitry Andric         Args.getLastArg(options::OPT_fobjc_weak, options::OPT_fno_objc_weak);
36950b57cec5SDimitry Andric     if (!WeakArg) {
36960b57cec5SDimitry Andric       // nothing to do
36970b57cec5SDimitry Andric     } else if (!Runtime.allowsWeak()) {
36980b57cec5SDimitry Andric       if (WeakArg->getOption().matches(options::OPT_fobjc_weak))
36990b57cec5SDimitry Andric         D.Diag(diag::err_objc_weak_unsupported);
37000b57cec5SDimitry Andric     } else {
37010b57cec5SDimitry Andric       WeakArg->render(Args, CmdArgs);
37020b57cec5SDimitry Andric     }
37030b57cec5SDimitry Andric   }
3704fe6060f1SDimitry Andric 
3705fe6060f1SDimitry Andric   if (Args.hasArg(options::OPT_fobjc_disable_direct_methods_for_testing))
3706fe6060f1SDimitry Andric     CmdArgs.push_back("-fobjc-disable-direct-methods-for-testing");
37070b57cec5SDimitry Andric }
37080b57cec5SDimitry Andric 
37090b57cec5SDimitry Andric static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args,
37100b57cec5SDimitry Andric                                      ArgStringList &CmdArgs) {
37110b57cec5SDimitry Andric   bool CaretDefault = true;
37120b57cec5SDimitry Andric   bool ColumnDefault = true;
37130b57cec5SDimitry Andric 
37140b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diagnostics_classic,
37150b57cec5SDimitry Andric                                      options::OPT__SLASH_diagnostics_column,
37160b57cec5SDimitry Andric                                      options::OPT__SLASH_diagnostics_caret)) {
37170b57cec5SDimitry Andric     switch (A->getOption().getID()) {
37180b57cec5SDimitry Andric     case options::OPT__SLASH_diagnostics_caret:
37190b57cec5SDimitry Andric       CaretDefault = true;
37200b57cec5SDimitry Andric       ColumnDefault = true;
37210b57cec5SDimitry Andric       break;
37220b57cec5SDimitry Andric     case options::OPT__SLASH_diagnostics_column:
37230b57cec5SDimitry Andric       CaretDefault = false;
37240b57cec5SDimitry Andric       ColumnDefault = true;
37250b57cec5SDimitry Andric       break;
37260b57cec5SDimitry Andric     case options::OPT__SLASH_diagnostics_classic:
37270b57cec5SDimitry Andric       CaretDefault = false;
37280b57cec5SDimitry Andric       ColumnDefault = false;
37290b57cec5SDimitry Andric       break;
37300b57cec5SDimitry Andric     }
37310b57cec5SDimitry Andric   }
37320b57cec5SDimitry Andric 
37330b57cec5SDimitry Andric   // -fcaret-diagnostics is default.
37340b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
37350b57cec5SDimitry Andric                     options::OPT_fno_caret_diagnostics, CaretDefault))
37360b57cec5SDimitry Andric     CmdArgs.push_back("-fno-caret-diagnostics");
37370b57cec5SDimitry Andric 
37380b57cec5SDimitry Andric   // -fdiagnostics-fixit-info is default, only pass non-default.
37390b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
37400b57cec5SDimitry Andric                     options::OPT_fno_diagnostics_fixit_info))
37410b57cec5SDimitry Andric     CmdArgs.push_back("-fno-diagnostics-fixit-info");
37420b57cec5SDimitry Andric 
37430b57cec5SDimitry Andric   // Enable -fdiagnostics-show-option by default.
37445ffd83dbSDimitry Andric   if (!Args.hasFlag(options::OPT_fdiagnostics_show_option,
37455ffd83dbSDimitry Andric                     options::OPT_fno_diagnostics_show_option, true))
37465ffd83dbSDimitry Andric     CmdArgs.push_back("-fno-diagnostics-show-option");
37470b57cec5SDimitry Andric 
37480b57cec5SDimitry Andric   if (const Arg *A =
37490b57cec5SDimitry Andric           Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
37500b57cec5SDimitry Andric     CmdArgs.push_back("-fdiagnostics-show-category");
37510b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
37520b57cec5SDimitry Andric   }
37530b57cec5SDimitry Andric 
37540b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdiagnostics_show_hotness,
37550b57cec5SDimitry Andric                    options::OPT_fno_diagnostics_show_hotness, false))
37560b57cec5SDimitry Andric     CmdArgs.push_back("-fdiagnostics-show-hotness");
37570b57cec5SDimitry Andric 
37580b57cec5SDimitry Andric   if (const Arg *A =
37590b57cec5SDimitry Andric           Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) {
37600b57cec5SDimitry Andric     std::string Opt =
37610b57cec5SDimitry Andric         std::string("-fdiagnostics-hotness-threshold=") + A->getValue();
37620b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Opt));
37630b57cec5SDimitry Andric   }
37640b57cec5SDimitry Andric 
37650b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
37660b57cec5SDimitry Andric     CmdArgs.push_back("-fdiagnostics-format");
37670b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
37680b57cec5SDimitry Andric   }
37690b57cec5SDimitry Andric 
37700b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(
37710b57cec5SDimitry Andric           options::OPT_fdiagnostics_show_note_include_stack,
37720b57cec5SDimitry Andric           options::OPT_fno_diagnostics_show_note_include_stack)) {
37730b57cec5SDimitry Andric     const Option &O = A->getOption();
37740b57cec5SDimitry Andric     if (O.matches(options::OPT_fdiagnostics_show_note_include_stack))
37750b57cec5SDimitry Andric       CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
37760b57cec5SDimitry Andric     else
37770b57cec5SDimitry Andric       CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
37780b57cec5SDimitry Andric   }
37790b57cec5SDimitry Andric 
37800b57cec5SDimitry Andric   // Color diagnostics are parsed by the driver directly from argv and later
37810b57cec5SDimitry Andric   // re-parsed to construct this job; claim any possible color diagnostic here
37820b57cec5SDimitry Andric   // to avoid warn_drv_unused_argument and diagnose bad
37830b57cec5SDimitry Andric   // OPT_fdiagnostics_color_EQ values.
37840b57cec5SDimitry Andric   for (const Arg *A : Args) {
37850b57cec5SDimitry Andric     const Option &O = A->getOption();
37860b57cec5SDimitry Andric     if (!O.matches(options::OPT_fcolor_diagnostics) &&
37870b57cec5SDimitry Andric         !O.matches(options::OPT_fdiagnostics_color) &&
37880b57cec5SDimitry Andric         !O.matches(options::OPT_fno_color_diagnostics) &&
37890b57cec5SDimitry Andric         !O.matches(options::OPT_fno_diagnostics_color) &&
37900b57cec5SDimitry Andric         !O.matches(options::OPT_fdiagnostics_color_EQ))
37910b57cec5SDimitry Andric       continue;
37920b57cec5SDimitry Andric 
37930b57cec5SDimitry Andric     if (O.matches(options::OPT_fdiagnostics_color_EQ)) {
37940b57cec5SDimitry Andric       StringRef Value(A->getValue());
37950b57cec5SDimitry Andric       if (Value != "always" && Value != "never" && Value != "auto")
37960b57cec5SDimitry Andric         D.Diag(diag::err_drv_clang_unsupported)
37970b57cec5SDimitry Andric             << ("-fdiagnostics-color=" + Value).str();
37980b57cec5SDimitry Andric     }
37990b57cec5SDimitry Andric     A->claim();
38000b57cec5SDimitry Andric   }
38010b57cec5SDimitry Andric 
38020b57cec5SDimitry Andric   if (D.getDiags().getDiagnosticOptions().ShowColors)
38030b57cec5SDimitry Andric     CmdArgs.push_back("-fcolor-diagnostics");
38040b57cec5SDimitry Andric 
38050b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fansi_escape_codes))
38060b57cec5SDimitry Andric     CmdArgs.push_back("-fansi-escape-codes");
38070b57cec5SDimitry Andric 
38080b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fshow_source_location,
38090b57cec5SDimitry Andric                     options::OPT_fno_show_source_location))
38100b57cec5SDimitry Andric     CmdArgs.push_back("-fno-show-source-location");
38110b57cec5SDimitry Andric 
38120b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fdiagnostics_absolute_paths))
38130b57cec5SDimitry Andric     CmdArgs.push_back("-fdiagnostics-absolute-paths");
38140b57cec5SDimitry Andric 
38150b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fshow_column, options::OPT_fno_show_column,
38160b57cec5SDimitry Andric                     ColumnDefault))
38170b57cec5SDimitry Andric     CmdArgs.push_back("-fno-show-column");
38180b57cec5SDimitry Andric 
38190b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fspell_checking,
38200b57cec5SDimitry Andric                     options::OPT_fno_spell_checking))
38210b57cec5SDimitry Andric     CmdArgs.push_back("-fno-spell-checking");
38220b57cec5SDimitry Andric }
38230b57cec5SDimitry Andric 
38240b57cec5SDimitry Andric enum class DwarfFissionKind { None, Split, Single };
38250b57cec5SDimitry Andric 
38260b57cec5SDimitry Andric static DwarfFissionKind getDebugFissionKind(const Driver &D,
38270b57cec5SDimitry Andric                                             const ArgList &Args, Arg *&Arg) {
3828e8d8bef9SDimitry Andric   Arg = Args.getLastArg(options::OPT_gsplit_dwarf, options::OPT_gsplit_dwarf_EQ,
3829e8d8bef9SDimitry Andric                         options::OPT_gno_split_dwarf);
3830e8d8bef9SDimitry Andric   if (!Arg || Arg->getOption().matches(options::OPT_gno_split_dwarf))
38310b57cec5SDimitry Andric     return DwarfFissionKind::None;
38320b57cec5SDimitry Andric 
38330b57cec5SDimitry Andric   if (Arg->getOption().matches(options::OPT_gsplit_dwarf))
38340b57cec5SDimitry Andric     return DwarfFissionKind::Split;
38350b57cec5SDimitry Andric 
38360b57cec5SDimitry Andric   StringRef Value = Arg->getValue();
38370b57cec5SDimitry Andric   if (Value == "split")
38380b57cec5SDimitry Andric     return DwarfFissionKind::Split;
38390b57cec5SDimitry Andric   if (Value == "single")
38400b57cec5SDimitry Andric     return DwarfFissionKind::Single;
38410b57cec5SDimitry Andric 
38420b57cec5SDimitry Andric   D.Diag(diag::err_drv_unsupported_option_argument)
38430b57cec5SDimitry Andric       << Arg->getOption().getName() << Arg->getValue();
38440b57cec5SDimitry Andric   return DwarfFissionKind::None;
38450b57cec5SDimitry Andric }
38460b57cec5SDimitry Andric 
3847fe6060f1SDimitry Andric static void renderDwarfFormat(const Driver &D, const llvm::Triple &T,
3848fe6060f1SDimitry Andric                               const ArgList &Args, ArgStringList &CmdArgs,
3849fe6060f1SDimitry Andric                               unsigned DwarfVersion) {
3850fe6060f1SDimitry Andric   auto *DwarfFormatArg =
3851fe6060f1SDimitry Andric       Args.getLastArg(options::OPT_gdwarf64, options::OPT_gdwarf32);
3852fe6060f1SDimitry Andric   if (!DwarfFormatArg)
3853fe6060f1SDimitry Andric     return;
3854fe6060f1SDimitry Andric 
3855fe6060f1SDimitry Andric   if (DwarfFormatArg->getOption().matches(options::OPT_gdwarf64)) {
3856fe6060f1SDimitry Andric     if (DwarfVersion < 3)
3857fe6060f1SDimitry Andric       D.Diag(diag::err_drv_argument_only_allowed_with)
3858fe6060f1SDimitry Andric           << DwarfFormatArg->getAsString(Args) << "DWARFv3 or greater";
3859fe6060f1SDimitry Andric     else if (!T.isArch64Bit())
3860fe6060f1SDimitry Andric       D.Diag(diag::err_drv_argument_only_allowed_with)
3861fe6060f1SDimitry Andric           << DwarfFormatArg->getAsString(Args) << "64 bit architecture";
3862fe6060f1SDimitry Andric     else if (!T.isOSBinFormatELF())
3863fe6060f1SDimitry Andric       D.Diag(diag::err_drv_argument_only_allowed_with)
3864fe6060f1SDimitry Andric           << DwarfFormatArg->getAsString(Args) << "ELF platforms";
3865fe6060f1SDimitry Andric   }
3866fe6060f1SDimitry Andric 
3867fe6060f1SDimitry Andric   DwarfFormatArg->render(Args, CmdArgs);
3868fe6060f1SDimitry Andric }
3869fe6060f1SDimitry Andric 
3870e8d8bef9SDimitry Andric static void renderDebugOptions(const ToolChain &TC, const Driver &D,
38710b57cec5SDimitry Andric                                const llvm::Triple &T, const ArgList &Args,
3872e8d8bef9SDimitry Andric                                bool EmitCodeView, bool IRInput,
3873e8d8bef9SDimitry Andric                                ArgStringList &CmdArgs,
38740b57cec5SDimitry Andric                                codegenoptions::DebugInfoKind &DebugInfoKind,
38750b57cec5SDimitry Andric                                DwarfFissionKind &DwarfFission) {
3876fe6060f1SDimitry Andric   // These two forms of profiling info can't be used together.
3877fe6060f1SDimitry Andric   if (const Arg *A1 = Args.getLastArg(options::OPT_fpseudo_probe_for_profiling))
3878fe6060f1SDimitry Andric     if (const Arg *A2 = Args.getLastArg(options::OPT_fdebug_info_for_profiling))
3879fe6060f1SDimitry Andric       D.Diag(diag::err_drv_argument_not_allowed_with)
3880fe6060f1SDimitry Andric           << A1->getAsString(Args) << A2->getAsString(Args);
3881fe6060f1SDimitry Andric 
38820b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdebug_info_for_profiling,
38830b57cec5SDimitry Andric                    options::OPT_fno_debug_info_for_profiling, false) &&
38840b57cec5SDimitry Andric       checkDebugInfoOption(
38850b57cec5SDimitry Andric           Args.getLastArg(options::OPT_fdebug_info_for_profiling), Args, D, TC))
38860b57cec5SDimitry Andric     CmdArgs.push_back("-fdebug-info-for-profiling");
38870b57cec5SDimitry Andric 
38880b57cec5SDimitry Andric   // The 'g' groups options involve a somewhat intricate sequence of decisions
38890b57cec5SDimitry Andric   // about what to pass from the driver to the frontend, but by the time they
38900b57cec5SDimitry Andric   // reach cc1 they've been factored into three well-defined orthogonal choices:
38910b57cec5SDimitry Andric   //  * what level of debug info to generate
38920b57cec5SDimitry Andric   //  * what dwarf version to write
38930b57cec5SDimitry Andric   //  * what debugger tuning to use
38940b57cec5SDimitry Andric   // This avoids having to monkey around further in cc1 other than to disable
38950b57cec5SDimitry Andric   // codeview if not running in a Windows environment. Perhaps even that
38960b57cec5SDimitry Andric   // decision should be made in the driver as well though.
38970b57cec5SDimitry Andric   llvm::DebuggerKind DebuggerTuning = TC.getDefaultDebuggerTuning();
38980b57cec5SDimitry Andric 
38990b57cec5SDimitry Andric   bool SplitDWARFInlining =
39000b57cec5SDimitry Andric       Args.hasFlag(options::OPT_fsplit_dwarf_inlining,
3901480093f4SDimitry Andric                    options::OPT_fno_split_dwarf_inlining, false);
39020b57cec5SDimitry Andric 
3903e8d8bef9SDimitry Andric   // Normally -gsplit-dwarf is only useful with -gN. For IR input, Clang does
3904e8d8bef9SDimitry Andric   // object file generation and no IR generation, -gN should not be needed. So
3905e8d8bef9SDimitry Andric   // allow -gsplit-dwarf with either -gN or IR input.
3906e8d8bef9SDimitry Andric   if (IRInput || Args.hasArg(options::OPT_g_Group)) {
39070b57cec5SDimitry Andric     Arg *SplitDWARFArg;
39080b57cec5SDimitry Andric     DwarfFission = getDebugFissionKind(D, Args, SplitDWARFArg);
39090b57cec5SDimitry Andric     if (DwarfFission != DwarfFissionKind::None &&
39100b57cec5SDimitry Andric         !checkDebugInfoOption(SplitDWARFArg, Args, D, TC)) {
39110b57cec5SDimitry Andric       DwarfFission = DwarfFissionKind::None;
39120b57cec5SDimitry Andric       SplitDWARFInlining = false;
39130b57cec5SDimitry Andric     }
3914e8d8bef9SDimitry Andric   }
3915e8d8bef9SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) {
3916fe6060f1SDimitry Andric     DebugInfoKind = codegenoptions::DebugInfoConstructor;
39170b57cec5SDimitry Andric 
39180b57cec5SDimitry Andric     // If the last option explicitly specified a debug-info level, use it.
39190b57cec5SDimitry Andric     if (checkDebugInfoOption(A, Args, D, TC) &&
39200b57cec5SDimitry Andric         A->getOption().matches(options::OPT_gN_Group)) {
39210b57cec5SDimitry Andric       DebugInfoKind = DebugLevelToInfoKind(*A);
39220b57cec5SDimitry Andric       // For -g0 or -gline-tables-only, drop -gsplit-dwarf. This gets a bit more
39230b57cec5SDimitry Andric       // complicated if you've disabled inline info in the skeleton CUs
39240b57cec5SDimitry Andric       // (SplitDWARFInlining) - then there's value in composing split-dwarf and
39250b57cec5SDimitry Andric       // line-tables-only, so let those compose naturally in that case.
39260b57cec5SDimitry Andric       if (DebugInfoKind == codegenoptions::NoDebugInfo ||
39270b57cec5SDimitry Andric           DebugInfoKind == codegenoptions::DebugDirectivesOnly ||
39280b57cec5SDimitry Andric           (DebugInfoKind == codegenoptions::DebugLineTablesOnly &&
39290b57cec5SDimitry Andric            SplitDWARFInlining))
39300b57cec5SDimitry Andric         DwarfFission = DwarfFissionKind::None;
39310b57cec5SDimitry Andric     }
39320b57cec5SDimitry Andric   }
39330b57cec5SDimitry Andric 
39340b57cec5SDimitry Andric   // If a debugger tuning argument appeared, remember it.
39350b57cec5SDimitry Andric   if (const Arg *A =
39360b57cec5SDimitry Andric           Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) {
39370b57cec5SDimitry Andric     if (checkDebugInfoOption(A, Args, D, TC)) {
39380b57cec5SDimitry Andric       if (A->getOption().matches(options::OPT_glldb))
39390b57cec5SDimitry Andric         DebuggerTuning = llvm::DebuggerKind::LLDB;
39400b57cec5SDimitry Andric       else if (A->getOption().matches(options::OPT_gsce))
39410b57cec5SDimitry Andric         DebuggerTuning = llvm::DebuggerKind::SCE;
3942fe6060f1SDimitry Andric       else if (A->getOption().matches(options::OPT_gdbx))
3943fe6060f1SDimitry Andric         DebuggerTuning = llvm::DebuggerKind::DBX;
39440b57cec5SDimitry Andric       else
39450b57cec5SDimitry Andric         DebuggerTuning = llvm::DebuggerKind::GDB;
39460b57cec5SDimitry Andric     }
39470b57cec5SDimitry Andric   }
39480b57cec5SDimitry Andric 
39490b57cec5SDimitry Andric   // If a -gdwarf argument appeared, remember it.
3950fe6060f1SDimitry Andric   const Arg *GDwarfN = getDwarfNArg(Args);
3951480093f4SDimitry Andric   bool EmitDwarf = false;
3952480093f4SDimitry Andric   if (GDwarfN) {
3953480093f4SDimitry Andric     if (checkDebugInfoOption(GDwarfN, Args, D, TC))
3954480093f4SDimitry Andric       EmitDwarf = true;
3955480093f4SDimitry Andric     else
3956480093f4SDimitry Andric       GDwarfN = nullptr;
3957480093f4SDimitry Andric   }
39580b57cec5SDimitry Andric 
39590b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) {
39600b57cec5SDimitry Andric     if (checkDebugInfoOption(A, Args, D, TC))
39610b57cec5SDimitry Andric       EmitCodeView = true;
39620b57cec5SDimitry Andric   }
39630b57cec5SDimitry Andric 
39640b57cec5SDimitry Andric   // If the user asked for debug info but did not explicitly specify -gcodeview
39650b57cec5SDimitry Andric   // or -gdwarf, ask the toolchain for the default format.
3966480093f4SDimitry Andric   if (!EmitCodeView && !EmitDwarf &&
39670b57cec5SDimitry Andric       DebugInfoKind != codegenoptions::NoDebugInfo) {
39680b57cec5SDimitry Andric     switch (TC.getDefaultDebugFormat()) {
39690b57cec5SDimitry Andric     case codegenoptions::DIF_CodeView:
39700b57cec5SDimitry Andric       EmitCodeView = true;
39710b57cec5SDimitry Andric       break;
39720b57cec5SDimitry Andric     case codegenoptions::DIF_DWARF:
3973480093f4SDimitry Andric       EmitDwarf = true;
39740b57cec5SDimitry Andric       break;
39750b57cec5SDimitry Andric     }
39760b57cec5SDimitry Andric   }
39770b57cec5SDimitry Andric 
3978e8d8bef9SDimitry Andric   unsigned RequestedDWARFVersion = 0; // DWARF version requested by the user
3979e8d8bef9SDimitry Andric   unsigned EffectiveDWARFVersion = 0; // DWARF version TC can generate. It may
3980e8d8bef9SDimitry Andric                                       // be lower than what the user wanted.
3981480093f4SDimitry Andric   unsigned DefaultDWARFVersion = ParseDebugDefaultVersion(TC, Args);
3982480093f4SDimitry Andric   if (EmitDwarf) {
3983480093f4SDimitry Andric     // Start with the platform default DWARF version
3984e8d8bef9SDimitry Andric     RequestedDWARFVersion = TC.GetDefaultDwarfVersion();
3985e8d8bef9SDimitry Andric     assert(RequestedDWARFVersion &&
3986e8d8bef9SDimitry Andric            "toolchain default DWARF version must be nonzero");
3987480093f4SDimitry Andric 
3988480093f4SDimitry Andric     // If the user specified a default DWARF version, that takes precedence
3989480093f4SDimitry Andric     // over the platform default.
3990480093f4SDimitry Andric     if (DefaultDWARFVersion)
3991e8d8bef9SDimitry Andric       RequestedDWARFVersion = DefaultDWARFVersion;
3992480093f4SDimitry Andric 
3993480093f4SDimitry Andric     // Override with a user-specified DWARF version
3994480093f4SDimitry Andric     if (GDwarfN)
3995480093f4SDimitry Andric       if (auto ExplicitVersion = DwarfVersionNum(GDwarfN->getSpelling()))
3996e8d8bef9SDimitry Andric         RequestedDWARFVersion = ExplicitVersion;
3997e8d8bef9SDimitry Andric     // Clamp effective DWARF version to the max supported by the toolchain.
3998e8d8bef9SDimitry Andric     EffectiveDWARFVersion =
3999e8d8bef9SDimitry Andric         std::min(RequestedDWARFVersion, TC.getMaxDwarfVersion());
4000480093f4SDimitry Andric   }
4001480093f4SDimitry Andric 
40020b57cec5SDimitry Andric   // -gline-directives-only supported only for the DWARF debug info.
4003e8d8bef9SDimitry Andric   if (RequestedDWARFVersion == 0 &&
4004e8d8bef9SDimitry Andric       DebugInfoKind == codegenoptions::DebugDirectivesOnly)
40050b57cec5SDimitry Andric     DebugInfoKind = codegenoptions::NoDebugInfo;
40060b57cec5SDimitry Andric 
4007fe6060f1SDimitry Andric   // strict DWARF is set to false by default. But for DBX, we need it to be set
4008fe6060f1SDimitry Andric   // as true by default.
4009fe6060f1SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_gstrict_dwarf))
4010fe6060f1SDimitry Andric     (void)checkDebugInfoOption(A, Args, D, TC);
4011fe6060f1SDimitry Andric   if (Args.hasFlag(options::OPT_gstrict_dwarf, options::OPT_gno_strict_dwarf,
4012fe6060f1SDimitry Andric                    DebuggerTuning == llvm::DebuggerKind::DBX))
4013fe6060f1SDimitry Andric     CmdArgs.push_back("-gstrict-dwarf");
4014fe6060f1SDimitry Andric 
40150b57cec5SDimitry Andric   // And we handle flag -grecord-gcc-switches later with DWARFDebugFlags.
40160b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_g_flags_Group);
40170b57cec5SDimitry Andric 
40180b57cec5SDimitry Andric   // Column info is included by default for everything except SCE and
40190b57cec5SDimitry Andric   // CodeView. Clang doesn't track end columns, just starting columns, which,
40200b57cec5SDimitry Andric   // in theory, is fine for CodeView (and PDB).  In practice, however, the
4021fe6060f1SDimitry Andric   // Microsoft debuggers don't handle missing end columns well, and the AIX
4022fe6060f1SDimitry Andric   // debugger DBX also doesn't handle the columns well, so it's better not to
4023fe6060f1SDimitry Andric   // include any column info.
40240b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_gcolumn_info))
40250b57cec5SDimitry Andric     (void)checkDebugInfoOption(A, Args, D, TC);
40265ffd83dbSDimitry Andric   if (!Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info,
4027fe6060f1SDimitry Andric                     !EmitCodeView &&
4028fe6060f1SDimitry Andric                         (DebuggerTuning != llvm::DebuggerKind::SCE &&
4029fe6060f1SDimitry Andric                          DebuggerTuning != llvm::DebuggerKind::DBX)))
40305ffd83dbSDimitry Andric     CmdArgs.push_back("-gno-column-info");
40310b57cec5SDimitry Andric 
40320b57cec5SDimitry Andric   // FIXME: Move backend command line options to the module.
40330b57cec5SDimitry Andric   // If -gline-tables-only or -gline-directives-only is the last option it wins.
40340b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_gmodules))
40350b57cec5SDimitry Andric     if (checkDebugInfoOption(A, Args, D, TC)) {
40360b57cec5SDimitry Andric       if (DebugInfoKind != codegenoptions::DebugLineTablesOnly &&
40370b57cec5SDimitry Andric           DebugInfoKind != codegenoptions::DebugDirectivesOnly) {
4038fe6060f1SDimitry Andric         DebugInfoKind = codegenoptions::DebugInfoConstructor;
40390b57cec5SDimitry Andric         CmdArgs.push_back("-dwarf-ext-refs");
40400b57cec5SDimitry Andric         CmdArgs.push_back("-fmodule-format=obj");
40410b57cec5SDimitry Andric       }
40420b57cec5SDimitry Andric     }
40430b57cec5SDimitry Andric 
4044fe6060f1SDimitry Andric   if (T.isOSBinFormatELF() && SplitDWARFInlining)
4045fe6060f1SDimitry Andric     CmdArgs.push_back("-fsplit-dwarf-inlining");
40460b57cec5SDimitry Andric 
40470b57cec5SDimitry Andric   // After we've dealt with all combinations of things that could
40480b57cec5SDimitry Andric   // make DebugInfoKind be other than None or DebugLineTablesOnly,
40490b57cec5SDimitry Andric   // figure out if we need to "upgrade" it to standalone debug info.
40500b57cec5SDimitry Andric   // We parse these two '-f' options whether or not they will be used,
40510b57cec5SDimitry Andric   // to claim them even if you wrote "-fstandalone-debug -gline-tables-only"
40520b57cec5SDimitry Andric   bool NeedFullDebug = Args.hasFlag(
40530b57cec5SDimitry Andric       options::OPT_fstandalone_debug, options::OPT_fno_standalone_debug,
40540b57cec5SDimitry Andric       DebuggerTuning == llvm::DebuggerKind::LLDB ||
40550b57cec5SDimitry Andric           TC.GetDefaultStandaloneDebug());
40560b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fstandalone_debug))
40570b57cec5SDimitry Andric     (void)checkDebugInfoOption(A, Args, D, TC);
4058e8d8bef9SDimitry Andric 
4059fe6060f1SDimitry Andric   if (DebugInfoKind == codegenoptions::LimitedDebugInfo ||
4060fe6060f1SDimitry Andric       DebugInfoKind == codegenoptions::DebugInfoConstructor) {
4061e8d8bef9SDimitry Andric     if (Args.hasFlag(options::OPT_fno_eliminate_unused_debug_types,
4062e8d8bef9SDimitry Andric                      options::OPT_feliminate_unused_debug_types, false))
4063e8d8bef9SDimitry Andric       DebugInfoKind = codegenoptions::UnusedTypeInfo;
4064e8d8bef9SDimitry Andric     else if (NeedFullDebug)
40650b57cec5SDimitry Andric       DebugInfoKind = codegenoptions::FullDebugInfo;
4066e8d8bef9SDimitry Andric   }
40670b57cec5SDimitry Andric 
40680b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source,
40690b57cec5SDimitry Andric                    false)) {
40700b57cec5SDimitry Andric     // Source embedding is a vendor extension to DWARF v5. By now we have
40710b57cec5SDimitry Andric     // checked if a DWARF version was stated explicitly, and have otherwise
40720b57cec5SDimitry Andric     // fallen back to the target default, so if this is still not at least 5
40730b57cec5SDimitry Andric     // we emit an error.
40740b57cec5SDimitry Andric     const Arg *A = Args.getLastArg(options::OPT_gembed_source);
4075e8d8bef9SDimitry Andric     if (RequestedDWARFVersion < 5)
40760b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_only_allowed_with)
40770b57cec5SDimitry Andric           << A->getAsString(Args) << "-gdwarf-5";
4078e8d8bef9SDimitry Andric     else if (EffectiveDWARFVersion < 5)
4079e8d8bef9SDimitry Andric       // The toolchain has reduced allowed dwarf version, so we can't enable
4080e8d8bef9SDimitry Andric       // -gembed-source.
4081e8d8bef9SDimitry Andric       D.Diag(diag::warn_drv_dwarf_version_limited_by_target)
4082e8d8bef9SDimitry Andric           << A->getAsString(Args) << TC.getTripleString() << 5
4083e8d8bef9SDimitry Andric           << EffectiveDWARFVersion;
40840b57cec5SDimitry Andric     else if (checkDebugInfoOption(A, Args, D, TC))
40850b57cec5SDimitry Andric       CmdArgs.push_back("-gembed-source");
40860b57cec5SDimitry Andric   }
40870b57cec5SDimitry Andric 
40880b57cec5SDimitry Andric   if (EmitCodeView) {
40890b57cec5SDimitry Andric     CmdArgs.push_back("-gcodeview");
40900b57cec5SDimitry Andric 
40910b57cec5SDimitry Andric     // Emit codeview type hashes if requested.
40920b57cec5SDimitry Andric     if (Args.hasFlag(options::OPT_gcodeview_ghash,
40930b57cec5SDimitry Andric                      options::OPT_gno_codeview_ghash, false)) {
40940b57cec5SDimitry Andric       CmdArgs.push_back("-gcodeview-ghash");
40950b57cec5SDimitry Andric     }
40960b57cec5SDimitry Andric   }
40970b57cec5SDimitry Andric 
4098480093f4SDimitry Andric   // Omit inline line tables if requested.
4099480093f4SDimitry Andric   if (Args.hasFlag(options::OPT_gno_inline_line_tables,
4100480093f4SDimitry Andric                    options::OPT_ginline_line_tables, false)) {
4101480093f4SDimitry Andric     CmdArgs.push_back("-gno-inline-line-tables");
4102480093f4SDimitry Andric   }
4103480093f4SDimitry Andric 
4104480093f4SDimitry Andric   // When emitting remarks, we need at least debug lines in the output.
4105480093f4SDimitry Andric   if (willEmitRemarks(Args) &&
4106480093f4SDimitry Andric       DebugInfoKind <= codegenoptions::DebugDirectivesOnly)
4107480093f4SDimitry Andric     DebugInfoKind = codegenoptions::DebugLineTablesOnly;
4108480093f4SDimitry Andric 
4109e8d8bef9SDimitry Andric   // Adjust the debug info kind for the given toolchain.
4110e8d8bef9SDimitry Andric   TC.adjustDebugInfoKind(DebugInfoKind, Args);
4111e8d8bef9SDimitry Andric 
4112e8d8bef9SDimitry Andric   RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, EffectiveDWARFVersion,
41130b57cec5SDimitry Andric                           DebuggerTuning);
41140b57cec5SDimitry Andric 
41150b57cec5SDimitry Andric   // -fdebug-macro turns on macro debug info generation.
41160b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdebug_macro, options::OPT_fno_debug_macro,
41170b57cec5SDimitry Andric                    false))
41180b57cec5SDimitry Andric     if (checkDebugInfoOption(Args.getLastArg(options::OPT_fdebug_macro), Args,
41190b57cec5SDimitry Andric                              D, TC))
41200b57cec5SDimitry Andric       CmdArgs.push_back("-debug-info-macro");
41210b57cec5SDimitry Andric 
41220b57cec5SDimitry Andric   // -ggnu-pubnames turns on gnu style pubnames in the backend.
41230b57cec5SDimitry Andric   const auto *PubnamesArg =
41240b57cec5SDimitry Andric       Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames,
41250b57cec5SDimitry Andric                       options::OPT_gpubnames, options::OPT_gno_pubnames);
41260b57cec5SDimitry Andric   if (DwarfFission != DwarfFissionKind::None ||
41270b57cec5SDimitry Andric       (PubnamesArg && checkDebugInfoOption(PubnamesArg, Args, D, TC)))
41280b57cec5SDimitry Andric     if (!PubnamesArg ||
41290b57cec5SDimitry Andric         (!PubnamesArg->getOption().matches(options::OPT_gno_gnu_pubnames) &&
41300b57cec5SDimitry Andric          !PubnamesArg->getOption().matches(options::OPT_gno_pubnames)))
41310b57cec5SDimitry Andric       CmdArgs.push_back(PubnamesArg && PubnamesArg->getOption().matches(
41320b57cec5SDimitry Andric                                            options::OPT_gpubnames)
41330b57cec5SDimitry Andric                             ? "-gpubnames"
41340b57cec5SDimitry Andric                             : "-ggnu-pubnames");
41350b57cec5SDimitry Andric 
41360b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdebug_ranges_base_address,
41370b57cec5SDimitry Andric                    options::OPT_fno_debug_ranges_base_address, false)) {
41380b57cec5SDimitry Andric     CmdArgs.push_back("-fdebug-ranges-base-address");
41390b57cec5SDimitry Andric   }
41400b57cec5SDimitry Andric 
41410b57cec5SDimitry Andric   // -gdwarf-aranges turns on the emission of the aranges section in the
41420b57cec5SDimitry Andric   // backend.
41430b57cec5SDimitry Andric   // Always enabled for SCE tuning.
41440b57cec5SDimitry Andric   bool NeedAranges = DebuggerTuning == llvm::DebuggerKind::SCE;
41450b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_gdwarf_aranges))
41460b57cec5SDimitry Andric     NeedAranges = checkDebugInfoOption(A, Args, D, TC) || NeedAranges;
41470b57cec5SDimitry Andric   if (NeedAranges) {
41480b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
41490b57cec5SDimitry Andric     CmdArgs.push_back("-generate-arange-section");
41500b57cec5SDimitry Andric   }
41510b57cec5SDimitry Andric 
4152480093f4SDimitry Andric   if (Args.hasFlag(options::OPT_fforce_dwarf_frame,
4153480093f4SDimitry Andric                    options::OPT_fno_force_dwarf_frame, false))
4154480093f4SDimitry Andric     CmdArgs.push_back("-fforce-dwarf-frame");
4155480093f4SDimitry Andric 
41560b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdebug_types_section,
41570b57cec5SDimitry Andric                    options::OPT_fno_debug_types_section, false)) {
4158e8d8bef9SDimitry Andric     if (!(T.isOSBinFormatELF() || T.isOSBinFormatWasm())) {
41590b57cec5SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
41600b57cec5SDimitry Andric           << Args.getLastArg(options::OPT_fdebug_types_section)
41610b57cec5SDimitry Andric                  ->getAsString(Args)
41620b57cec5SDimitry Andric           << T.getTriple();
41630b57cec5SDimitry Andric     } else if (checkDebugInfoOption(
41640b57cec5SDimitry Andric                    Args.getLastArg(options::OPT_fdebug_types_section), Args, D,
41650b57cec5SDimitry Andric                    TC)) {
41660b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
41670b57cec5SDimitry Andric       CmdArgs.push_back("-generate-type-units");
41680b57cec5SDimitry Andric     }
41690b57cec5SDimitry Andric   }
41700b57cec5SDimitry Andric 
4171fe6060f1SDimitry Andric   // To avoid join/split of directory+filename, the integrated assembler prefers
4172fe6060f1SDimitry Andric   // the directory form of .file on all DWARF versions. GNU as doesn't allow the
4173fe6060f1SDimitry Andric   // form before DWARF v5.
4174fe6060f1SDimitry Andric   if (!Args.hasFlag(options::OPT_fdwarf_directory_asm,
4175fe6060f1SDimitry Andric                     options::OPT_fno_dwarf_directory_asm,
4176fe6060f1SDimitry Andric                     TC.useIntegratedAs() || EffectiveDWARFVersion >= 5))
4177fe6060f1SDimitry Andric     CmdArgs.push_back("-fno-dwarf-directory-asm");
4178fe6060f1SDimitry Andric 
41790b57cec5SDimitry Andric   // Decide how to render forward declarations of template instantiations.
41800b57cec5SDimitry Andric   // SCE wants full descriptions, others just get them in the name.
41810b57cec5SDimitry Andric   if (DebuggerTuning == llvm::DebuggerKind::SCE)
41820b57cec5SDimitry Andric     CmdArgs.push_back("-debug-forward-template-params");
41830b57cec5SDimitry Andric 
41840b57cec5SDimitry Andric   // Do we need to explicitly import anonymous namespaces into the parent
41850b57cec5SDimitry Andric   // scope?
41860b57cec5SDimitry Andric   if (DebuggerTuning == llvm::DebuggerKind::SCE)
41870b57cec5SDimitry Andric     CmdArgs.push_back("-dwarf-explicit-import");
41880b57cec5SDimitry Andric 
4189fe6060f1SDimitry Andric   renderDwarfFormat(D, T, Args, CmdArgs, EffectiveDWARFVersion);
41900b57cec5SDimitry Andric   RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC);
41910b57cec5SDimitry Andric }
41920b57cec5SDimitry Andric 
41930b57cec5SDimitry Andric void Clang::ConstructJob(Compilation &C, const JobAction &JA,
41940b57cec5SDimitry Andric                          const InputInfo &Output, const InputInfoList &Inputs,
41950b57cec5SDimitry Andric                          const ArgList &Args, const char *LinkingOutput) const {
41960b57cec5SDimitry Andric   const auto &TC = getToolChain();
41970b57cec5SDimitry Andric   const llvm::Triple &RawTriple = TC.getTriple();
41980b57cec5SDimitry Andric   const llvm::Triple &Triple = TC.getEffectiveTriple();
41990b57cec5SDimitry Andric   const std::string &TripleStr = Triple.getTriple();
42000b57cec5SDimitry Andric 
42010b57cec5SDimitry Andric   bool KernelOrKext =
42020b57cec5SDimitry Andric       Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
42030b57cec5SDimitry Andric   const Driver &D = TC.getDriver();
42040b57cec5SDimitry Andric   ArgStringList CmdArgs;
42050b57cec5SDimitry Andric 
42060b57cec5SDimitry Andric   // Check number of inputs for sanity. We need at least one input.
42070b57cec5SDimitry Andric   assert(Inputs.size() >= 1 && "Must have at least one input.");
42080b57cec5SDimitry Andric   // CUDA/HIP compilation may have multiple inputs (source file + results of
42090b57cec5SDimitry Andric   // device-side compilations). OpenMP device jobs also take the host IR as a
42100b57cec5SDimitry Andric   // second input. Module precompilation accepts a list of header files to
42110b57cec5SDimitry Andric   // include as part of the module. All other jobs are expected to have exactly
42120b57cec5SDimitry Andric   // one input.
42130b57cec5SDimitry Andric   bool IsCuda = JA.isOffloading(Action::OFK_Cuda);
4214fe6060f1SDimitry Andric   bool IsCudaDevice = JA.isDeviceOffloading(Action::OFK_Cuda);
42150b57cec5SDimitry Andric   bool IsHIP = JA.isOffloading(Action::OFK_HIP);
4216fe6060f1SDimitry Andric   bool IsHIPDevice = JA.isDeviceOffloading(Action::OFK_HIP);
42170b57cec5SDimitry Andric   bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP);
42180b57cec5SDimitry Andric   bool IsHeaderModulePrecompile = isa<HeaderModulePrecompileJobAction>(JA);
4219fe6060f1SDimitry Andric   bool IsDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) ||
4220fe6060f1SDimitry Andric                                  JA.isDeviceOffloading(Action::OFK_Host));
4221fe6060f1SDimitry Andric   bool IsUsingLTO = D.isUsingLTO(IsDeviceOffloadAction);
4222fe6060f1SDimitry Andric   auto LTOMode = D.getLTOMode(IsDeviceOffloadAction);
42230b57cec5SDimitry Andric 
42240b57cec5SDimitry Andric   // A header module compilation doesn't have a main input file, so invent a
42250b57cec5SDimitry Andric   // fake one as a placeholder.
42260b57cec5SDimitry Andric   const char *ModuleName = [&]{
42270b57cec5SDimitry Andric     auto *ModuleNameArg = Args.getLastArg(options::OPT_fmodule_name_EQ);
42280b57cec5SDimitry Andric     return ModuleNameArg ? ModuleNameArg->getValue() : "";
42290b57cec5SDimitry Andric   }();
42300b57cec5SDimitry Andric   InputInfo HeaderModuleInput(Inputs[0].getType(), ModuleName, ModuleName);
42310b57cec5SDimitry Andric 
42320b57cec5SDimitry Andric   const InputInfo &Input =
42330b57cec5SDimitry Andric       IsHeaderModulePrecompile ? HeaderModuleInput : Inputs[0];
42340b57cec5SDimitry Andric 
42350b57cec5SDimitry Andric   InputInfoList ModuleHeaderInputs;
42360b57cec5SDimitry Andric   const InputInfo *CudaDeviceInput = nullptr;
42370b57cec5SDimitry Andric   const InputInfo *OpenMPDeviceInput = nullptr;
42380b57cec5SDimitry Andric   for (const InputInfo &I : Inputs) {
42390b57cec5SDimitry Andric     if (&I == &Input) {
42400b57cec5SDimitry Andric       // This is the primary input.
42410b57cec5SDimitry Andric     } else if (IsHeaderModulePrecompile &&
42420b57cec5SDimitry Andric                types::getPrecompiledType(I.getType()) == types::TY_PCH) {
42430b57cec5SDimitry Andric       types::ID Expected = HeaderModuleInput.getType();
42440b57cec5SDimitry Andric       if (I.getType() != Expected) {
42450b57cec5SDimitry Andric         D.Diag(diag::err_drv_module_header_wrong_kind)
42460b57cec5SDimitry Andric             << I.getFilename() << types::getTypeName(I.getType())
42470b57cec5SDimitry Andric             << types::getTypeName(Expected);
42480b57cec5SDimitry Andric       }
42490b57cec5SDimitry Andric       ModuleHeaderInputs.push_back(I);
42500b57cec5SDimitry Andric     } else if ((IsCuda || IsHIP) && !CudaDeviceInput) {
42510b57cec5SDimitry Andric       CudaDeviceInput = &I;
42520b57cec5SDimitry Andric     } else if (IsOpenMPDevice && !OpenMPDeviceInput) {
42530b57cec5SDimitry Andric       OpenMPDeviceInput = &I;
42540b57cec5SDimitry Andric     } else {
42550b57cec5SDimitry Andric       llvm_unreachable("unexpectedly given multiple inputs");
42560b57cec5SDimitry Andric     }
42570b57cec5SDimitry Andric   }
42580b57cec5SDimitry Andric 
42595ffd83dbSDimitry Andric   const llvm::Triple *AuxTriple =
42605ffd83dbSDimitry Andric       (IsCuda || IsHIP) ? TC.getAuxTriple() : nullptr;
42610b57cec5SDimitry Andric   bool IsWindowsMSVC = RawTriple.isWindowsMSVCEnvironment();
42620b57cec5SDimitry Andric   bool IsIAMCU = RawTriple.isOSIAMCU();
42630b57cec5SDimitry Andric 
42640b57cec5SDimitry Andric   // Adjust IsWindowsXYZ for CUDA/HIP compilations.  Even when compiling in
42650b57cec5SDimitry Andric   // device mode (i.e., getToolchain().getTriple() is NVPTX/AMDGCN, not
42660b57cec5SDimitry Andric   // Windows), we need to pass Windows-specific flags to cc1.
42670b57cec5SDimitry Andric   if (IsCuda || IsHIP)
42680b57cec5SDimitry Andric     IsWindowsMSVC |= AuxTriple && AuxTriple->isWindowsMSVCEnvironment();
42690b57cec5SDimitry Andric 
42700b57cec5SDimitry Andric   // C++ is not supported for IAMCU.
42710b57cec5SDimitry Andric   if (IsIAMCU && types::isCXX(Input.getType()))
42720b57cec5SDimitry Andric     D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU";
42730b57cec5SDimitry Andric 
42740b57cec5SDimitry Andric   // Invoke ourselves in -cc1 mode.
42750b57cec5SDimitry Andric   //
42760b57cec5SDimitry Andric   // FIXME: Implement custom jobs for internal actions.
42770b57cec5SDimitry Andric   CmdArgs.push_back("-cc1");
42780b57cec5SDimitry Andric 
42790b57cec5SDimitry Andric   // Add the "effective" target triple.
42800b57cec5SDimitry Andric   CmdArgs.push_back("-triple");
42810b57cec5SDimitry Andric   CmdArgs.push_back(Args.MakeArgString(TripleStr));
42820b57cec5SDimitry Andric 
42830b57cec5SDimitry Andric   if (const Arg *MJ = Args.getLastArg(options::OPT_MJ)) {
42840b57cec5SDimitry Andric     DumpCompilationDatabase(C, MJ->getValue(), TripleStr, Output, Input, Args);
42850b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_MJ);
4286a7dea167SDimitry Andric   } else if (const Arg *GenCDBFragment =
4287a7dea167SDimitry Andric                  Args.getLastArg(options::OPT_gen_cdb_fragment_path)) {
4288a7dea167SDimitry Andric     DumpCompilationDatabaseFragmentToDir(GenCDBFragment->getValue(), C,
4289a7dea167SDimitry Andric                                          TripleStr, Output, Input, Args);
4290a7dea167SDimitry Andric     Args.ClaimAllArgs(options::OPT_gen_cdb_fragment_path);
42910b57cec5SDimitry Andric   }
42920b57cec5SDimitry Andric 
42930b57cec5SDimitry Andric   if (IsCuda || IsHIP) {
42940b57cec5SDimitry Andric     // We have to pass the triple of the host if compiling for a CUDA/HIP device
42950b57cec5SDimitry Andric     // and vice-versa.
42960b57cec5SDimitry Andric     std::string NormalizedTriple;
42970b57cec5SDimitry Andric     if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
42980b57cec5SDimitry Andric         JA.isDeviceOffloading(Action::OFK_HIP))
42990b57cec5SDimitry Andric       NormalizedTriple = C.getSingleOffloadToolChain<Action::OFK_Host>()
43000b57cec5SDimitry Andric                              ->getTriple()
43010b57cec5SDimitry Andric                              .normalize();
43020b57cec5SDimitry Andric     else {
43030b57cec5SDimitry Andric       // Host-side compilation.
43040b57cec5SDimitry Andric       NormalizedTriple =
43050b57cec5SDimitry Andric           (IsCuda ? C.getSingleOffloadToolChain<Action::OFK_Cuda>()
43060b57cec5SDimitry Andric                   : C.getSingleOffloadToolChain<Action::OFK_HIP>())
43070b57cec5SDimitry Andric               ->getTriple()
43080b57cec5SDimitry Andric               .normalize();
43090b57cec5SDimitry Andric       if (IsCuda) {
43100b57cec5SDimitry Andric         // We need to figure out which CUDA version we're compiling for, as that
43110b57cec5SDimitry Andric         // determines how we load and launch GPU kernels.
43120b57cec5SDimitry Andric         auto *CTC = static_cast<const toolchains::CudaToolChain *>(
43130b57cec5SDimitry Andric             C.getSingleOffloadToolChain<Action::OFK_Cuda>());
43140b57cec5SDimitry Andric         assert(CTC && "Expected valid CUDA Toolchain.");
43150b57cec5SDimitry Andric         if (CTC && CTC->CudaInstallation.version() != CudaVersion::UNKNOWN)
43160b57cec5SDimitry Andric           CmdArgs.push_back(Args.MakeArgString(
43170b57cec5SDimitry Andric               Twine("-target-sdk-version=") +
43180b57cec5SDimitry Andric               CudaVersionToString(CTC->CudaInstallation.version())));
43190b57cec5SDimitry Andric       }
43200b57cec5SDimitry Andric     }
43210b57cec5SDimitry Andric     CmdArgs.push_back("-aux-triple");
43220b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
43230b57cec5SDimitry Andric   }
43240b57cec5SDimitry Andric 
43255ffd83dbSDimitry Andric   if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) {
43265ffd83dbSDimitry Andric     CmdArgs.push_back("-fsycl-is-device");
43275ffd83dbSDimitry Andric 
43285ffd83dbSDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_sycl_std_EQ)) {
43295ffd83dbSDimitry Andric       A->render(Args, CmdArgs);
43305ffd83dbSDimitry Andric     } else {
4331fe6060f1SDimitry Andric       // Ensure the default version in SYCL mode is 2020.
4332fe6060f1SDimitry Andric       CmdArgs.push_back("-sycl-std=2020");
43335ffd83dbSDimitry Andric     }
43345ffd83dbSDimitry Andric   }
43355ffd83dbSDimitry Andric 
43360b57cec5SDimitry Andric   if (IsOpenMPDevice) {
43370b57cec5SDimitry Andric     // We have to pass the triple of the host if compiling for an OpenMP device.
43380b57cec5SDimitry Andric     std::string NormalizedTriple =
43390b57cec5SDimitry Andric         C.getSingleOffloadToolChain<Action::OFK_Host>()
43400b57cec5SDimitry Andric             ->getTriple()
43410b57cec5SDimitry Andric             .normalize();
43420b57cec5SDimitry Andric     CmdArgs.push_back("-aux-triple");
43430b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
43440b57cec5SDimitry Andric   }
43450b57cec5SDimitry Andric 
43460b57cec5SDimitry Andric   if (Triple.isOSWindows() && (Triple.getArch() == llvm::Triple::arm ||
43470b57cec5SDimitry Andric                                Triple.getArch() == llvm::Triple::thumb)) {
43480b57cec5SDimitry Andric     unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 4 : 6;
43495ffd83dbSDimitry Andric     unsigned Version = 0;
43505ffd83dbSDimitry Andric     bool Failure =
43515ffd83dbSDimitry Andric         Triple.getArchName().substr(Offset).consumeInteger(10, Version);
43525ffd83dbSDimitry Andric     if (Failure || Version < 7)
43530b57cec5SDimitry Andric       D.Diag(diag::err_target_unsupported_arch) << Triple.getArchName()
43540b57cec5SDimitry Andric                                                 << TripleStr;
43550b57cec5SDimitry Andric   }
43560b57cec5SDimitry Andric 
43570b57cec5SDimitry Andric   // Push all default warning arguments that are specific to
43580b57cec5SDimitry Andric   // the given target.  These come before user provided warning options
43590b57cec5SDimitry Andric   // are provided.
43600b57cec5SDimitry Andric   TC.addClangWarningOptions(CmdArgs);
43610b57cec5SDimitry Andric 
4362fe6060f1SDimitry Andric   // FIXME: Subclass ToolChain for SPIR and move this to addClangWarningOptions.
4363fe6060f1SDimitry Andric   if (Triple.isSPIR())
4364fe6060f1SDimitry Andric     CmdArgs.push_back("-Wspir-compat");
4365fe6060f1SDimitry Andric 
43660b57cec5SDimitry Andric   // Select the appropriate action.
43670b57cec5SDimitry Andric   RewriteKind rewriteKind = RK_None;
43680b57cec5SDimitry Andric 
4369a7dea167SDimitry Andric   // If CollectArgsForIntegratedAssembler() isn't called below, claim the args
4370a7dea167SDimitry Andric   // it claims when not running an assembler. Otherwise, clang would emit
4371a7dea167SDimitry Andric   // "argument unused" warnings for assembler flags when e.g. adding "-E" to
4372a7dea167SDimitry Andric   // flags while debugging something. That'd be somewhat inconvenient, and it's
4373a7dea167SDimitry Andric   // also inconsistent with most other flags -- we don't warn on
4374a7dea167SDimitry Andric   // -ffunction-sections not being used in -E mode either for example, even
4375a7dea167SDimitry Andric   // though it's not really used either.
4376a7dea167SDimitry Andric   if (!isa<AssembleJobAction>(JA)) {
4377a7dea167SDimitry Andric     // The args claimed here should match the args used in
4378a7dea167SDimitry Andric     // CollectArgsForIntegratedAssembler().
4379a7dea167SDimitry Andric     if (TC.useIntegratedAs()) {
4380a7dea167SDimitry Andric       Args.ClaimAllArgs(options::OPT_mrelax_all);
4381a7dea167SDimitry Andric       Args.ClaimAllArgs(options::OPT_mno_relax_all);
4382a7dea167SDimitry Andric       Args.ClaimAllArgs(options::OPT_mincremental_linker_compatible);
4383a7dea167SDimitry Andric       Args.ClaimAllArgs(options::OPT_mno_incremental_linker_compatible);
4384a7dea167SDimitry Andric       switch (C.getDefaultToolChain().getArch()) {
4385a7dea167SDimitry Andric       case llvm::Triple::arm:
4386a7dea167SDimitry Andric       case llvm::Triple::armeb:
4387a7dea167SDimitry Andric       case llvm::Triple::thumb:
4388a7dea167SDimitry Andric       case llvm::Triple::thumbeb:
4389a7dea167SDimitry Andric         Args.ClaimAllArgs(options::OPT_mimplicit_it_EQ);
4390a7dea167SDimitry Andric         break;
4391a7dea167SDimitry Andric       default:
4392a7dea167SDimitry Andric         break;
4393a7dea167SDimitry Andric       }
4394a7dea167SDimitry Andric     }
4395a7dea167SDimitry Andric     Args.ClaimAllArgs(options::OPT_Wa_COMMA);
4396a7dea167SDimitry Andric     Args.ClaimAllArgs(options::OPT_Xassembler);
4397a7dea167SDimitry Andric   }
4398a7dea167SDimitry Andric 
43990b57cec5SDimitry Andric   if (isa<AnalyzeJobAction>(JA)) {
44000b57cec5SDimitry Andric     assert(JA.getType() == types::TY_Plist && "Invalid output type.");
44010b57cec5SDimitry Andric     CmdArgs.push_back("-analyze");
44020b57cec5SDimitry Andric   } else if (isa<MigrateJobAction>(JA)) {
44030b57cec5SDimitry Andric     CmdArgs.push_back("-migrate");
44040b57cec5SDimitry Andric   } else if (isa<PreprocessJobAction>(JA)) {
44050b57cec5SDimitry Andric     if (Output.getType() == types::TY_Dependencies)
44060b57cec5SDimitry Andric       CmdArgs.push_back("-Eonly");
44070b57cec5SDimitry Andric     else {
44080b57cec5SDimitry Andric       CmdArgs.push_back("-E");
44090b57cec5SDimitry Andric       if (Args.hasArg(options::OPT_rewrite_objc) &&
44100b57cec5SDimitry Andric           !Args.hasArg(options::OPT_g_Group))
44110b57cec5SDimitry Andric         CmdArgs.push_back("-P");
44120b57cec5SDimitry Andric     }
44130b57cec5SDimitry Andric   } else if (isa<AssembleJobAction>(JA)) {
44140b57cec5SDimitry Andric     CmdArgs.push_back("-emit-obj");
44150b57cec5SDimitry Andric 
44160b57cec5SDimitry Andric     CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
44170b57cec5SDimitry Andric 
44180b57cec5SDimitry Andric     // Also ignore explicit -force_cpusubtype_ALL option.
44190b57cec5SDimitry Andric     (void)Args.hasArg(options::OPT_force__cpusubtype__ALL);
44200b57cec5SDimitry Andric   } else if (isa<PrecompileJobAction>(JA)) {
44210b57cec5SDimitry Andric     if (JA.getType() == types::TY_Nothing)
44220b57cec5SDimitry Andric       CmdArgs.push_back("-fsyntax-only");
44230b57cec5SDimitry Andric     else if (JA.getType() == types::TY_ModuleFile)
44240b57cec5SDimitry Andric       CmdArgs.push_back(IsHeaderModulePrecompile
44250b57cec5SDimitry Andric                             ? "-emit-header-module"
44260b57cec5SDimitry Andric                             : "-emit-module-interface");
44270b57cec5SDimitry Andric     else
44280b57cec5SDimitry Andric       CmdArgs.push_back("-emit-pch");
44290b57cec5SDimitry Andric   } else if (isa<VerifyPCHJobAction>(JA)) {
44300b57cec5SDimitry Andric     CmdArgs.push_back("-verify-pch");
44310b57cec5SDimitry Andric   } else {
44320b57cec5SDimitry Andric     assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) &&
44330b57cec5SDimitry Andric            "Invalid action for clang tool.");
44340b57cec5SDimitry Andric     if (JA.getType() == types::TY_Nothing) {
44350b57cec5SDimitry Andric       CmdArgs.push_back("-fsyntax-only");
44360b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_LLVM_IR ||
44370b57cec5SDimitry Andric                JA.getType() == types::TY_LTO_IR) {
44380b57cec5SDimitry Andric       CmdArgs.push_back("-emit-llvm");
44390b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_LLVM_BC ||
44400b57cec5SDimitry Andric                JA.getType() == types::TY_LTO_BC) {
4441fe6060f1SDimitry Andric       // Emit textual llvm IR for AMDGPU offloading for -emit-llvm -S
4442fe6060f1SDimitry Andric       if (Triple.isAMDGCN() && IsOpenMPDevice && Args.hasArg(options::OPT_S) &&
4443fe6060f1SDimitry Andric           Args.hasArg(options::OPT_emit_llvm)) {
4444fe6060f1SDimitry Andric         CmdArgs.push_back("-emit-llvm");
4445fe6060f1SDimitry Andric       } else {
44460b57cec5SDimitry Andric         CmdArgs.push_back("-emit-llvm-bc");
4447fe6060f1SDimitry Andric       }
4448a7dea167SDimitry Andric     } else if (JA.getType() == types::TY_IFS ||
4449a7dea167SDimitry Andric                JA.getType() == types::TY_IFS_CPP) {
4450a7dea167SDimitry Andric       StringRef ArgStr =
4451a7dea167SDimitry Andric           Args.hasArg(options::OPT_interface_stub_version_EQ)
4452a7dea167SDimitry Andric               ? Args.getLastArgValue(options::OPT_interface_stub_version_EQ)
4453fe6060f1SDimitry Andric               : "ifs-v1";
44540b57cec5SDimitry Andric       CmdArgs.push_back("-emit-interface-stubs");
44550b57cec5SDimitry Andric       CmdArgs.push_back(
4456a7dea167SDimitry Andric           Args.MakeArgString(Twine("-interface-stub-version=") + ArgStr.str()));
44570b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_PP_Asm) {
44580b57cec5SDimitry Andric       CmdArgs.push_back("-S");
44590b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_AST) {
44600b57cec5SDimitry Andric       CmdArgs.push_back("-emit-pch");
44610b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_ModuleFile) {
44620b57cec5SDimitry Andric       CmdArgs.push_back("-module-file-info");
44630b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_RewrittenObjC) {
44640b57cec5SDimitry Andric       CmdArgs.push_back("-rewrite-objc");
44650b57cec5SDimitry Andric       rewriteKind = RK_NonFragile;
44660b57cec5SDimitry Andric     } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
44670b57cec5SDimitry Andric       CmdArgs.push_back("-rewrite-objc");
44680b57cec5SDimitry Andric       rewriteKind = RK_Fragile;
44690b57cec5SDimitry Andric     } else {
44700b57cec5SDimitry Andric       assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!");
44710b57cec5SDimitry Andric     }
44720b57cec5SDimitry Andric 
44730b57cec5SDimitry Andric     // Preserve use-list order by default when emitting bitcode, so that
44740b57cec5SDimitry Andric     // loading the bitcode up in 'opt' or 'llc' and running passes gives the
44750b57cec5SDimitry Andric     // same result as running passes here.  For LTO, we don't need to preserve
44760b57cec5SDimitry Andric     // the use-list order, since serialization to bitcode is part of the flow.
44770b57cec5SDimitry Andric     if (JA.getType() == types::TY_LLVM_BC)
44780b57cec5SDimitry Andric       CmdArgs.push_back("-emit-llvm-uselists");
44790b57cec5SDimitry Andric 
4480fe6060f1SDimitry Andric     if (IsUsingLTO) {
4481fe6060f1SDimitry Andric       if (!IsDeviceOffloadAction) {
4482fe6060f1SDimitry Andric         if (Args.hasArg(options::OPT_flto))
4483fe6060f1SDimitry Andric           CmdArgs.push_back("-flto");
4484fe6060f1SDimitry Andric         else {
4485fe6060f1SDimitry Andric           if (D.getLTOMode() == LTOK_Thin)
4486fe6060f1SDimitry Andric             CmdArgs.push_back("-flto=thin");
4487fe6060f1SDimitry Andric           else
4488fe6060f1SDimitry Andric             CmdArgs.push_back("-flto=full");
4489fe6060f1SDimitry Andric         }
44900b57cec5SDimitry Andric         CmdArgs.push_back("-flto-unit");
4491fe6060f1SDimitry Andric       } else if (Triple.isAMDGPU()) {
4492fe6060f1SDimitry Andric         // Only AMDGPU supports device-side LTO
4493fe6060f1SDimitry Andric         assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin);
4494fe6060f1SDimitry Andric         CmdArgs.push_back(Args.MakeArgString(
4495fe6060f1SDimitry Andric             Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full")));
4496fe6060f1SDimitry Andric         CmdArgs.push_back("-flto-unit");
4497fe6060f1SDimitry Andric       } else {
4498fe6060f1SDimitry Andric         D.Diag(diag::err_drv_unsupported_opt_for_target)
4499fe6060f1SDimitry Andric             << Args.getLastArg(options::OPT_foffload_lto,
4500fe6060f1SDimitry Andric                                options::OPT_foffload_lto_EQ)
4501fe6060f1SDimitry Andric                    ->getAsString(Args)
4502fe6060f1SDimitry Andric             << Triple.getTriple();
4503fe6060f1SDimitry Andric       }
45040b57cec5SDimitry Andric     }
45050b57cec5SDimitry Andric   }
45060b57cec5SDimitry Andric 
45070b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fthinlto_index_EQ)) {
45080b57cec5SDimitry Andric     if (!types::isLLVMIR(Input.getType()))
45090b57cec5SDimitry Andric       D.Diag(diag::err_drv_arg_requires_bitcode_input) << A->getAsString(Args);
45100b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ);
45110b57cec5SDimitry Andric   }
45120b57cec5SDimitry Andric 
4513480093f4SDimitry Andric   if (Args.getLastArg(options::OPT_fthin_link_bitcode_EQ))
4514480093f4SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fthin_link_bitcode_EQ);
4515480093f4SDimitry Andric 
45160b57cec5SDimitry Andric   if (Args.getLastArg(options::OPT_save_temps_EQ))
45170b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_save_temps_EQ);
45180b57cec5SDimitry Andric 
4519e8d8bef9SDimitry Andric   auto *MemProfArg = Args.getLastArg(options::OPT_fmemory_profile,
4520e8d8bef9SDimitry Andric                                      options::OPT_fmemory_profile_EQ,
4521e8d8bef9SDimitry Andric                                      options::OPT_fno_memory_profile);
4522e8d8bef9SDimitry Andric   if (MemProfArg &&
4523e8d8bef9SDimitry Andric       !MemProfArg->getOption().matches(options::OPT_fno_memory_profile))
4524e8d8bef9SDimitry Andric     MemProfArg->render(Args, CmdArgs);
4525e8d8bef9SDimitry Andric 
45260b57cec5SDimitry Andric   // Embed-bitcode option.
45270b57cec5SDimitry Andric   // Only white-listed flags below are allowed to be embedded.
4528fe6060f1SDimitry Andric   if (C.getDriver().embedBitcodeInObject() && !IsUsingLTO &&
45290b57cec5SDimitry Andric       (isa<BackendJobAction>(JA) || isa<AssembleJobAction>(JA))) {
45300b57cec5SDimitry Andric     // Add flags implied by -fembed-bitcode.
45310b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ);
45320b57cec5SDimitry Andric     // Disable all llvm IR level optimizations.
45330b57cec5SDimitry Andric     CmdArgs.push_back("-disable-llvm-passes");
45340b57cec5SDimitry Andric 
4535480093f4SDimitry Andric     // Render target options.
45360b57cec5SDimitry Andric     TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind());
45370b57cec5SDimitry Andric 
45380b57cec5SDimitry Andric     // reject options that shouldn't be supported in bitcode
45390b57cec5SDimitry Andric     // also reject kernel/kext
45400b57cec5SDimitry Andric     static const constexpr unsigned kBitcodeOptionBlacklist[] = {
45410b57cec5SDimitry Andric         options::OPT_mkernel,
45420b57cec5SDimitry Andric         options::OPT_fapple_kext,
45430b57cec5SDimitry Andric         options::OPT_ffunction_sections,
45440b57cec5SDimitry Andric         options::OPT_fno_function_sections,
45450b57cec5SDimitry Andric         options::OPT_fdata_sections,
45460b57cec5SDimitry Andric         options::OPT_fno_data_sections,
45475ffd83dbSDimitry Andric         options::OPT_fbasic_block_sections_EQ,
45485ffd83dbSDimitry Andric         options::OPT_funique_internal_linkage_names,
45495ffd83dbSDimitry Andric         options::OPT_fno_unique_internal_linkage_names,
45500b57cec5SDimitry Andric         options::OPT_funique_section_names,
45510b57cec5SDimitry Andric         options::OPT_fno_unique_section_names,
45525ffd83dbSDimitry Andric         options::OPT_funique_basic_block_section_names,
45535ffd83dbSDimitry Andric         options::OPT_fno_unique_basic_block_section_names,
45540b57cec5SDimitry Andric         options::OPT_mrestrict_it,
45550b57cec5SDimitry Andric         options::OPT_mno_restrict_it,
45560b57cec5SDimitry Andric         options::OPT_mstackrealign,
45570b57cec5SDimitry Andric         options::OPT_mno_stackrealign,
45580b57cec5SDimitry Andric         options::OPT_mstack_alignment,
45590b57cec5SDimitry Andric         options::OPT_mcmodel_EQ,
45600b57cec5SDimitry Andric         options::OPT_mlong_calls,
45610b57cec5SDimitry Andric         options::OPT_mno_long_calls,
45620b57cec5SDimitry Andric         options::OPT_ggnu_pubnames,
45630b57cec5SDimitry Andric         options::OPT_gdwarf_aranges,
45640b57cec5SDimitry Andric         options::OPT_fdebug_types_section,
45650b57cec5SDimitry Andric         options::OPT_fno_debug_types_section,
45660b57cec5SDimitry Andric         options::OPT_fdwarf_directory_asm,
45670b57cec5SDimitry Andric         options::OPT_fno_dwarf_directory_asm,
45680b57cec5SDimitry Andric         options::OPT_mrelax_all,
45690b57cec5SDimitry Andric         options::OPT_mno_relax_all,
45700b57cec5SDimitry Andric         options::OPT_ftrap_function_EQ,
45710b57cec5SDimitry Andric         options::OPT_ffixed_r9,
45720b57cec5SDimitry Andric         options::OPT_mfix_cortex_a53_835769,
45730b57cec5SDimitry Andric         options::OPT_mno_fix_cortex_a53_835769,
45740b57cec5SDimitry Andric         options::OPT_ffixed_x18,
45750b57cec5SDimitry Andric         options::OPT_mglobal_merge,
45760b57cec5SDimitry Andric         options::OPT_mno_global_merge,
45770b57cec5SDimitry Andric         options::OPT_mred_zone,
45780b57cec5SDimitry Andric         options::OPT_mno_red_zone,
45790b57cec5SDimitry Andric         options::OPT_Wa_COMMA,
45800b57cec5SDimitry Andric         options::OPT_Xassembler,
45810b57cec5SDimitry Andric         options::OPT_mllvm,
45820b57cec5SDimitry Andric     };
45830b57cec5SDimitry Andric     for (const auto &A : Args)
45840b57cec5SDimitry Andric       if (llvm::find(kBitcodeOptionBlacklist, A->getOption().getID()) !=
45850b57cec5SDimitry Andric           std::end(kBitcodeOptionBlacklist))
45860b57cec5SDimitry Andric         D.Diag(diag::err_drv_unsupported_embed_bitcode) << A->getSpelling();
45870b57cec5SDimitry Andric 
45880b57cec5SDimitry Andric     // Render the CodeGen options that need to be passed.
45890b57cec5SDimitry Andric     if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
45900b57cec5SDimitry Andric                       options::OPT_fno_optimize_sibling_calls))
45910b57cec5SDimitry Andric       CmdArgs.push_back("-mdisable-tail-calls");
45920b57cec5SDimitry Andric 
45930b57cec5SDimitry Andric     RenderFloatingPointOptions(TC, D, isOptimizationLevelFast(Args), Args,
45945ffd83dbSDimitry Andric                                CmdArgs, JA);
45950b57cec5SDimitry Andric 
45960b57cec5SDimitry Andric     // Render ABI arguments
45970b57cec5SDimitry Andric     switch (TC.getArch()) {
45980b57cec5SDimitry Andric     default: break;
45990b57cec5SDimitry Andric     case llvm::Triple::arm:
46000b57cec5SDimitry Andric     case llvm::Triple::armeb:
46010b57cec5SDimitry Andric     case llvm::Triple::thumbeb:
46020b57cec5SDimitry Andric       RenderARMABI(Triple, Args, CmdArgs);
46030b57cec5SDimitry Andric       break;
46040b57cec5SDimitry Andric     case llvm::Triple::aarch64:
4605480093f4SDimitry Andric     case llvm::Triple::aarch64_32:
46060b57cec5SDimitry Andric     case llvm::Triple::aarch64_be:
46070b57cec5SDimitry Andric       RenderAArch64ABI(Triple, Args, CmdArgs);
46080b57cec5SDimitry Andric       break;
46090b57cec5SDimitry Andric     }
46100b57cec5SDimitry Andric 
46110b57cec5SDimitry Andric     // Optimization level for CodeGen.
46120b57cec5SDimitry Andric     if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) {
46130b57cec5SDimitry Andric       if (A->getOption().matches(options::OPT_O4)) {
46140b57cec5SDimitry Andric         CmdArgs.push_back("-O3");
46150b57cec5SDimitry Andric         D.Diag(diag::warn_O4_is_O3);
46160b57cec5SDimitry Andric       } else {
46170b57cec5SDimitry Andric         A->render(Args, CmdArgs);
46180b57cec5SDimitry Andric       }
46190b57cec5SDimitry Andric     }
46200b57cec5SDimitry Andric 
46210b57cec5SDimitry Andric     // Input/Output file.
46220b57cec5SDimitry Andric     if (Output.getType() == types::TY_Dependencies) {
46230b57cec5SDimitry Andric       // Handled with other dependency code.
46240b57cec5SDimitry Andric     } else if (Output.isFilename()) {
46250b57cec5SDimitry Andric       CmdArgs.push_back("-o");
46260b57cec5SDimitry Andric       CmdArgs.push_back(Output.getFilename());
46270b57cec5SDimitry Andric     } else {
46280b57cec5SDimitry Andric       assert(Output.isNothing() && "Input output.");
46290b57cec5SDimitry Andric     }
46300b57cec5SDimitry Andric 
46310b57cec5SDimitry Andric     for (const auto &II : Inputs) {
46320b57cec5SDimitry Andric       addDashXForInput(Args, II, CmdArgs);
46330b57cec5SDimitry Andric       if (II.isFilename())
46340b57cec5SDimitry Andric         CmdArgs.push_back(II.getFilename());
46350b57cec5SDimitry Andric       else
46360b57cec5SDimitry Andric         II.getInputArg().renderAsInput(Args, CmdArgs);
46370b57cec5SDimitry Andric     }
46380b57cec5SDimitry Andric 
4639e8d8bef9SDimitry Andric     C.addCommand(std::make_unique<Command>(
4640e8d8bef9SDimitry Andric         JA, *this, ResponseFileSupport::AtFileUTF8(), D.getClangProgramPath(),
4641e8d8bef9SDimitry Andric         CmdArgs, Inputs, Output));
46420b57cec5SDimitry Andric     return;
46430b57cec5SDimitry Andric   }
46440b57cec5SDimitry Andric 
4645fe6060f1SDimitry Andric   if (C.getDriver().embedBitcodeMarkerOnly() && !IsUsingLTO)
46460b57cec5SDimitry Andric     CmdArgs.push_back("-fembed-bitcode=marker");
46470b57cec5SDimitry Andric 
46480b57cec5SDimitry Andric   // We normally speed up the clang process a bit by skipping destructors at
46490b57cec5SDimitry Andric   // exit, but when we're generating diagnostics we can rely on some of the
46500b57cec5SDimitry Andric   // cleanup.
46510b57cec5SDimitry Andric   if (!C.isForDiagnostics())
46520b57cec5SDimitry Andric     CmdArgs.push_back("-disable-free");
46530b57cec5SDimitry Andric 
46540b57cec5SDimitry Andric #ifdef NDEBUG
46550b57cec5SDimitry Andric   const bool IsAssertBuild = false;
46560b57cec5SDimitry Andric #else
46570b57cec5SDimitry Andric   const bool IsAssertBuild = true;
46580b57cec5SDimitry Andric #endif
46590b57cec5SDimitry Andric 
46600b57cec5SDimitry Andric   // Disable the verification pass in -asserts builds.
46610b57cec5SDimitry Andric   if (!IsAssertBuild)
46620b57cec5SDimitry Andric     CmdArgs.push_back("-disable-llvm-verifier");
46630b57cec5SDimitry Andric 
46640b57cec5SDimitry Andric   // Discard value names in assert builds unless otherwise specified.
46650b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdiscard_value_names,
466647395794SDimitry Andric                    options::OPT_fno_discard_value_names, !IsAssertBuild)) {
466747395794SDimitry Andric     if (Args.hasArg(options::OPT_fdiscard_value_names) &&
466847395794SDimitry Andric         (std::any_of(Inputs.begin(), Inputs.end(),
466947395794SDimitry Andric                      [](const clang::driver::InputInfo &II) {
467047395794SDimitry Andric                        return types::isLLVMIR(II.getType());
467147395794SDimitry Andric                      }))) {
467247395794SDimitry Andric       D.Diag(diag::warn_ignoring_fdiscard_for_bitcode);
467347395794SDimitry Andric     }
46740b57cec5SDimitry Andric     CmdArgs.push_back("-discard-value-names");
467547395794SDimitry Andric   }
46760b57cec5SDimitry Andric 
46770b57cec5SDimitry Andric   // Set the main file name, so that debug info works even with
46780b57cec5SDimitry Andric   // -save-temps.
46790b57cec5SDimitry Andric   CmdArgs.push_back("-main-file-name");
46800b57cec5SDimitry Andric   CmdArgs.push_back(getBaseInputName(Args, Input));
46810b57cec5SDimitry Andric 
46820b57cec5SDimitry Andric   // Some flags which affect the language (via preprocessor
46830b57cec5SDimitry Andric   // defines).
46840b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_static))
46850b57cec5SDimitry Andric     CmdArgs.push_back("-static-define");
46860b57cec5SDimitry Andric 
46870b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_municode))
46880b57cec5SDimitry Andric     CmdArgs.push_back("-DUNICODE");
46890b57cec5SDimitry Andric 
46900b57cec5SDimitry Andric   if (isa<AnalyzeJobAction>(JA))
46910b57cec5SDimitry Andric     RenderAnalyzerOptions(Args, CmdArgs, Triple, Input);
46920b57cec5SDimitry Andric 
4693a7dea167SDimitry Andric   if (isa<AnalyzeJobAction>(JA) ||
4694a7dea167SDimitry Andric       (isa<PreprocessJobAction>(JA) && Args.hasArg(options::OPT__analyze)))
4695a7dea167SDimitry Andric     CmdArgs.push_back("-setup-static-analyzer");
4696a7dea167SDimitry Andric 
46970b57cec5SDimitry Andric   // Enable compatilibily mode to avoid analyzer-config related errors.
46980b57cec5SDimitry Andric   // Since we can't access frontend flags through hasArg, let's manually iterate
46990b57cec5SDimitry Andric   // through them.
47000b57cec5SDimitry Andric   bool FoundAnalyzerConfig = false;
47010b57cec5SDimitry Andric   for (auto Arg : Args.filtered(options::OPT_Xclang))
47020b57cec5SDimitry Andric     if (StringRef(Arg->getValue()) == "-analyzer-config") {
47030b57cec5SDimitry Andric       FoundAnalyzerConfig = true;
47040b57cec5SDimitry Andric       break;
47050b57cec5SDimitry Andric     }
47060b57cec5SDimitry Andric   if (!FoundAnalyzerConfig)
47070b57cec5SDimitry Andric     for (auto Arg : Args.filtered(options::OPT_Xanalyzer))
47080b57cec5SDimitry Andric       if (StringRef(Arg->getValue()) == "-analyzer-config") {
47090b57cec5SDimitry Andric         FoundAnalyzerConfig = true;
47100b57cec5SDimitry Andric         break;
47110b57cec5SDimitry Andric       }
47120b57cec5SDimitry Andric   if (FoundAnalyzerConfig)
47130b57cec5SDimitry Andric     CmdArgs.push_back("-analyzer-config-compatibility-mode=true");
47140b57cec5SDimitry Andric 
47150b57cec5SDimitry Andric   CheckCodeGenerationOptions(D, Args);
47160b57cec5SDimitry Andric 
47170b57cec5SDimitry Andric   unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args);
47180b57cec5SDimitry Andric   assert(FunctionAlignment <= 31 && "function alignment will be truncated!");
47190b57cec5SDimitry Andric   if (FunctionAlignment) {
47200b57cec5SDimitry Andric     CmdArgs.push_back("-function-alignment");
47210b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment)));
47220b57cec5SDimitry Andric   }
47230b57cec5SDimitry Andric 
47240b57cec5SDimitry Andric   llvm::Reloc::Model RelocationModel;
47250b57cec5SDimitry Andric   unsigned PICLevel;
47260b57cec5SDimitry Andric   bool IsPIE;
47270b57cec5SDimitry Andric   std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(TC, Args);
47280b57cec5SDimitry Andric 
47295ffd83dbSDimitry Andric   bool IsROPI = RelocationModel == llvm::Reloc::ROPI ||
47305ffd83dbSDimitry Andric                 RelocationModel == llvm::Reloc::ROPI_RWPI;
47315ffd83dbSDimitry Andric   bool IsRWPI = RelocationModel == llvm::Reloc::RWPI ||
47325ffd83dbSDimitry Andric                 RelocationModel == llvm::Reloc::ROPI_RWPI;
47330b57cec5SDimitry Andric 
47345ffd83dbSDimitry Andric   if (Args.hasArg(options::OPT_mcmse) &&
47355ffd83dbSDimitry Andric       !Args.hasArg(options::OPT_fallow_unsupported)) {
47365ffd83dbSDimitry Andric     if (IsROPI)
47375ffd83dbSDimitry Andric       D.Diag(diag::err_cmse_pi_are_incompatible) << IsROPI;
47385ffd83dbSDimitry Andric     if (IsRWPI)
47395ffd83dbSDimitry Andric       D.Diag(diag::err_cmse_pi_are_incompatible) << !IsRWPI;
47405ffd83dbSDimitry Andric   }
47415ffd83dbSDimitry Andric 
47425ffd83dbSDimitry Andric   if (IsROPI && types::isCXX(Input.getType()) &&
47430b57cec5SDimitry Andric       !Args.hasArg(options::OPT_fallow_unsupported))
47440b57cec5SDimitry Andric     D.Diag(diag::err_drv_ropi_incompatible_with_cxx);
47450b57cec5SDimitry Andric 
47465ffd83dbSDimitry Andric   const char *RMName = RelocationModelName(RelocationModel);
47470b57cec5SDimitry Andric   if (RMName) {
47480b57cec5SDimitry Andric     CmdArgs.push_back("-mrelocation-model");
47490b57cec5SDimitry Andric     CmdArgs.push_back(RMName);
47500b57cec5SDimitry Andric   }
47510b57cec5SDimitry Andric   if (PICLevel > 0) {
47520b57cec5SDimitry Andric     CmdArgs.push_back("-pic-level");
47530b57cec5SDimitry Andric     CmdArgs.push_back(PICLevel == 1 ? "1" : "2");
47540b57cec5SDimitry Andric     if (IsPIE)
47550b57cec5SDimitry Andric       CmdArgs.push_back("-pic-is-pie");
47560b57cec5SDimitry Andric   }
47570b57cec5SDimitry Andric 
47580b57cec5SDimitry Andric   if (RelocationModel == llvm::Reloc::ROPI ||
47590b57cec5SDimitry Andric       RelocationModel == llvm::Reloc::ROPI_RWPI)
47600b57cec5SDimitry Andric     CmdArgs.push_back("-fropi");
47610b57cec5SDimitry Andric   if (RelocationModel == llvm::Reloc::RWPI ||
47620b57cec5SDimitry Andric       RelocationModel == llvm::Reloc::ROPI_RWPI)
47630b57cec5SDimitry Andric     CmdArgs.push_back("-frwpi");
47640b57cec5SDimitry Andric 
47650b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_meabi)) {
47660b57cec5SDimitry Andric     CmdArgs.push_back("-meabi");
47670b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
47680b57cec5SDimitry Andric   }
47690b57cec5SDimitry Andric 
4770e8d8bef9SDimitry Andric   // -fsemantic-interposition is forwarded to CC1: set the
4771e8d8bef9SDimitry Andric   // "SemanticInterposition" metadata to 1 (make some linkages interposable) and
4772e8d8bef9SDimitry Andric   // make default visibility external linkage definitions dso_preemptable.
4773e8d8bef9SDimitry Andric   //
4774e8d8bef9SDimitry Andric   // -fno-semantic-interposition: if the target supports .Lfoo$local local
4775e8d8bef9SDimitry Andric   // aliases (make default visibility external linkage definitions dso_local).
4776e8d8bef9SDimitry Andric   // This is the CC1 default for ELF to match COFF/Mach-O.
4777e8d8bef9SDimitry Andric   //
4778e8d8bef9SDimitry Andric   // Otherwise use Clang's traditional behavior: like
4779e8d8bef9SDimitry Andric   // -fno-semantic-interposition but local aliases are not used. So references
4780e8d8bef9SDimitry Andric   // can be interposed if not optimized out.
4781e8d8bef9SDimitry Andric   if (Triple.isOSBinFormatELF()) {
4782e8d8bef9SDimitry Andric     Arg *A = Args.getLastArg(options::OPT_fsemantic_interposition,
4783e8d8bef9SDimitry Andric                              options::OPT_fno_semantic_interposition);
4784e8d8bef9SDimitry Andric     if (RelocationModel != llvm::Reloc::Static && !IsPIE) {
4785e8d8bef9SDimitry Andric       // The supported targets need to call AsmPrinter::getSymbolPreferLocal.
4786fe6060f1SDimitry Andric       bool SupportsLocalAlias =
4787fe6060f1SDimitry Andric           Triple.isAArch64() || Triple.isRISCV() || Triple.isX86();
4788e8d8bef9SDimitry Andric       if (!A)
4789e8d8bef9SDimitry Andric         CmdArgs.push_back("-fhalf-no-semantic-interposition");
4790e8d8bef9SDimitry Andric       else if (A->getOption().matches(options::OPT_fsemantic_interposition))
47915ffd83dbSDimitry Andric         A->render(Args, CmdArgs);
4792e8d8bef9SDimitry Andric       else if (!SupportsLocalAlias)
4793e8d8bef9SDimitry Andric         CmdArgs.push_back("-fhalf-no-semantic-interposition");
4794e8d8bef9SDimitry Andric     }
4795e8d8bef9SDimitry Andric   }
47965ffd83dbSDimitry Andric 
47975ffd83dbSDimitry Andric   {
47985ffd83dbSDimitry Andric     std::string Model;
47990b57cec5SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) {
48000b57cec5SDimitry Andric       if (!TC.isThreadModelSupported(A->getValue()))
48010b57cec5SDimitry Andric         D.Diag(diag::err_drv_invalid_thread_model_for_target)
48020b57cec5SDimitry Andric             << A->getValue() << A->getAsString(Args);
48035ffd83dbSDimitry Andric       Model = A->getValue();
48045ffd83dbSDimitry Andric     } else
48055ffd83dbSDimitry Andric       Model = TC.getThreadModel();
48065ffd83dbSDimitry Andric     if (Model != "posix") {
48075ffd83dbSDimitry Andric       CmdArgs.push_back("-mthread-model");
48085ffd83dbSDimitry Andric       CmdArgs.push_back(Args.MakeArgString(Model));
48090b57cec5SDimitry Andric     }
48105ffd83dbSDimitry Andric   }
48110b57cec5SDimitry Andric 
48120b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fveclib);
48130b57cec5SDimitry Andric 
48140b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fmerge_all_constants,
48150b57cec5SDimitry Andric                    options::OPT_fno_merge_all_constants, false))
48160b57cec5SDimitry Andric     CmdArgs.push_back("-fmerge-all-constants");
48170b57cec5SDimitry Andric 
48180b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fno_delete_null_pointer_checks,
48190b57cec5SDimitry Andric                    options::OPT_fdelete_null_pointer_checks, false))
48200b57cec5SDimitry Andric     CmdArgs.push_back("-fno-delete-null-pointer-checks");
48210b57cec5SDimitry Andric 
48220b57cec5SDimitry Andric   // LLVM Code Generator Options.
48230b57cec5SDimitry Andric 
4824fe6060f1SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_frewrite_map_file_EQ)) {
48250b57cec5SDimitry Andric     StringRef Map = A->getValue();
48260b57cec5SDimitry Andric     if (!llvm::sys::fs::exists(Map)) {
48270b57cec5SDimitry Andric       D.Diag(diag::err_drv_no_such_file) << Map;
48280b57cec5SDimitry Andric     } else {
4829fe6060f1SDimitry Andric       A->render(Args, CmdArgs);
48300b57cec5SDimitry Andric       A->claim();
48310b57cec5SDimitry Andric     }
48320b57cec5SDimitry Andric   }
48330b57cec5SDimitry Andric 
4834e8d8bef9SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ_vec_extabi,
4835e8d8bef9SDimitry Andric                                options::OPT_mabi_EQ_vec_default)) {
4836e8d8bef9SDimitry Andric     if (!Triple.isOSAIX())
4837e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
4838e8d8bef9SDimitry Andric           << A->getSpelling() << RawTriple.str();
4839d409305fSDimitry Andric     if (A->getOption().getID() == options::OPT_mabi_EQ_vec_extabi)
4840d409305fSDimitry Andric       CmdArgs.push_back("-mabi=vec-extabi");
4841d409305fSDimitry Andric     else
4842fe6060f1SDimitry Andric       CmdArgs.push_back("-mabi=vec-default");
4843fe6060f1SDimitry Andric   }
4844fe6060f1SDimitry Andric 
4845fe6060f1SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mlong_double_128)) {
4846fe6060f1SDimitry Andric     // Emit the unsupported option error until the Clang's library integration
4847fe6060f1SDimitry Andric     // support for 128-bit long double is available for AIX.
4848fe6060f1SDimitry Andric     if (Triple.isOSAIX())
4849fe6060f1SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
4850fe6060f1SDimitry Andric           << A->getSpelling() << RawTriple.str();
4851e8d8bef9SDimitry Andric   }
4852e8d8bef9SDimitry Andric 
48530b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) {
48540b57cec5SDimitry Andric     StringRef v = A->getValue();
4855fe6060f1SDimitry Andric     // FIXME: Validate the argument here so we don't produce meaningless errors
4856fe6060f1SDimitry Andric     // about -fwarn-stack-size=.
4857fe6060f1SDimitry Andric     if (v.empty())
4858fe6060f1SDimitry Andric       D.Diag(diag::err_drv_missing_argument) << A->getSpelling() << 1;
4859fe6060f1SDimitry Andric     else
4860fe6060f1SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-fwarn-stack-size=" + v));
48610b57cec5SDimitry Andric     A->claim();
48620b57cec5SDimitry Andric   }
48630b57cec5SDimitry Andric 
48640b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fjump_tables, options::OPT_fno_jump_tables,
48650b57cec5SDimitry Andric                     true))
48660b57cec5SDimitry Andric     CmdArgs.push_back("-fno-jump-tables");
48670b57cec5SDimitry Andric 
48680b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fprofile_sample_accurate,
48690b57cec5SDimitry Andric                    options::OPT_fno_profile_sample_accurate, false))
48700b57cec5SDimitry Andric     CmdArgs.push_back("-fprofile-sample-accurate");
48710b57cec5SDimitry Andric 
48720b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fpreserve_as_comments,
48730b57cec5SDimitry Andric                     options::OPT_fno_preserve_as_comments, true))
48740b57cec5SDimitry Andric     CmdArgs.push_back("-fno-preserve-as-comments");
48750b57cec5SDimitry Andric 
48760b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
48770b57cec5SDimitry Andric     CmdArgs.push_back("-mregparm");
48780b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
48790b57cec5SDimitry Andric   }
48800b57cec5SDimitry Andric 
4881e86cf8adSDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_maix_struct_return,
4882e86cf8adSDimitry Andric                                options::OPT_msvr4_struct_return)) {
4883e8d8bef9SDimitry Andric     if (!TC.getTriple().isPPC32()) {
4884e86cf8adSDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
4885e86cf8adSDimitry Andric           << A->getSpelling() << RawTriple.str();
4886e86cf8adSDimitry Andric     } else if (A->getOption().matches(options::OPT_maix_struct_return)) {
4887e86cf8adSDimitry Andric       CmdArgs.push_back("-maix-struct-return");
4888e86cf8adSDimitry Andric     } else {
4889e86cf8adSDimitry Andric       assert(A->getOption().matches(options::OPT_msvr4_struct_return));
4890e86cf8adSDimitry Andric       CmdArgs.push_back("-msvr4-struct-return");
4891e86cf8adSDimitry Andric     }
4892e86cf8adSDimitry Andric   }
4893e86cf8adSDimitry Andric 
48940b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return,
48950b57cec5SDimitry Andric                                options::OPT_freg_struct_return)) {
48960b57cec5SDimitry Andric     if (TC.getArch() != llvm::Triple::x86) {
48970b57cec5SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
48980b57cec5SDimitry Andric           << A->getSpelling() << RawTriple.str();
48990b57cec5SDimitry Andric     } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) {
49000b57cec5SDimitry Andric       CmdArgs.push_back("-fpcc-struct-return");
49010b57cec5SDimitry Andric     } else {
49020b57cec5SDimitry Andric       assert(A->getOption().matches(options::OPT_freg_struct_return));
49030b57cec5SDimitry Andric       CmdArgs.push_back("-freg-struct-return");
49040b57cec5SDimitry Andric     }
49050b57cec5SDimitry Andric   }
49060b57cec5SDimitry Andric 
49070b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
49080b57cec5SDimitry Andric     CmdArgs.push_back("-fdefault-calling-conv=stdcall");
49090b57cec5SDimitry Andric 
49105ffd83dbSDimitry Andric   if (Args.hasArg(options::OPT_fenable_matrix)) {
49115ffd83dbSDimitry Andric     // enable-matrix is needed by both the LangOpts and by LLVM.
49125ffd83dbSDimitry Andric     CmdArgs.push_back("-fenable-matrix");
49135ffd83dbSDimitry Andric     CmdArgs.push_back("-mllvm");
49145ffd83dbSDimitry Andric     CmdArgs.push_back("-enable-matrix");
49155ffd83dbSDimitry Andric   }
49165ffd83dbSDimitry Andric 
4917a7dea167SDimitry Andric   CodeGenOptions::FramePointerKind FPKeepKind =
4918a7dea167SDimitry Andric                   getFramePointerKind(Args, RawTriple);
4919a7dea167SDimitry Andric   const char *FPKeepKindStr = nullptr;
4920a7dea167SDimitry Andric   switch (FPKeepKind) {
4921a7dea167SDimitry Andric   case CodeGenOptions::FramePointerKind::None:
4922a7dea167SDimitry Andric     FPKeepKindStr = "-mframe-pointer=none";
4923a7dea167SDimitry Andric     break;
4924a7dea167SDimitry Andric   case CodeGenOptions::FramePointerKind::NonLeaf:
4925a7dea167SDimitry Andric     FPKeepKindStr = "-mframe-pointer=non-leaf";
4926a7dea167SDimitry Andric     break;
4927a7dea167SDimitry Andric   case CodeGenOptions::FramePointerKind::All:
4928a7dea167SDimitry Andric     FPKeepKindStr = "-mframe-pointer=all";
4929a7dea167SDimitry Andric     break;
4930a7dea167SDimitry Andric   }
4931a7dea167SDimitry Andric   assert(FPKeepKindStr && "unknown FramePointerKind");
4932a7dea167SDimitry Andric   CmdArgs.push_back(FPKeepKindStr);
4933a7dea167SDimitry Andric 
49340b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
49355ffd83dbSDimitry Andric                     options::OPT_fno_zero_initialized_in_bss, true))
49365ffd83dbSDimitry Andric     CmdArgs.push_back("-fno-zero-initialized-in-bss");
49370b57cec5SDimitry Andric 
49380b57cec5SDimitry Andric   bool OFastEnabled = isOptimizationLevelFast(Args);
49390b57cec5SDimitry Andric   // If -Ofast is the optimization level, then -fstrict-aliasing should be
49400b57cec5SDimitry Andric   // enabled.  This alias option is being used to simplify the hasFlag logic.
49410b57cec5SDimitry Andric   OptSpecifier StrictAliasingAliasOption =
49420b57cec5SDimitry Andric       OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing;
49430b57cec5SDimitry Andric   // We turn strict aliasing off by default if we're in CL mode, since MSVC
49440b57cec5SDimitry Andric   // doesn't do any TBAA.
49450b57cec5SDimitry Andric   bool TBAAOnByDefault = !D.IsCLMode();
49460b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
49470b57cec5SDimitry Andric                     options::OPT_fno_strict_aliasing, TBAAOnByDefault))
49480b57cec5SDimitry Andric     CmdArgs.push_back("-relaxed-aliasing");
49490b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fstruct_path_tbaa,
49500b57cec5SDimitry Andric                     options::OPT_fno_struct_path_tbaa))
49510b57cec5SDimitry Andric     CmdArgs.push_back("-no-struct-path-tbaa");
49520b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
49530b57cec5SDimitry Andric                    false))
49540b57cec5SDimitry Andric     CmdArgs.push_back("-fstrict-enums");
49550b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fstrict_return, options::OPT_fno_strict_return,
49560b57cec5SDimitry Andric                     true))
49570b57cec5SDimitry Andric     CmdArgs.push_back("-fno-strict-return");
49580b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fallow_editor_placeholders,
49590b57cec5SDimitry Andric                    options::OPT_fno_allow_editor_placeholders, false))
49600b57cec5SDimitry Andric     CmdArgs.push_back("-fallow-editor-placeholders");
49610b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fstrict_vtable_pointers,
49620b57cec5SDimitry Andric                    options::OPT_fno_strict_vtable_pointers,
49630b57cec5SDimitry Andric                    false))
49640b57cec5SDimitry Andric     CmdArgs.push_back("-fstrict-vtable-pointers");
49650b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fforce_emit_vtables,
49660b57cec5SDimitry Andric                    options::OPT_fno_force_emit_vtables,
49670b57cec5SDimitry Andric                    false))
49680b57cec5SDimitry Andric     CmdArgs.push_back("-fforce-emit-vtables");
49690b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
49700b57cec5SDimitry Andric                     options::OPT_fno_optimize_sibling_calls))
49710b57cec5SDimitry Andric     CmdArgs.push_back("-mdisable-tail-calls");
49720b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fno_escaping_block_tail_calls,
49730b57cec5SDimitry Andric                    options::OPT_fescaping_block_tail_calls, false))
49740b57cec5SDimitry Andric     CmdArgs.push_back("-fno-escaping-block-tail-calls");
49750b57cec5SDimitry Andric 
49760b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ffine_grained_bitfield_accesses,
49770b57cec5SDimitry Andric                   options::OPT_fno_fine_grained_bitfield_accesses);
49780b57cec5SDimitry Andric 
4979fe6060f1SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables,
4980fe6060f1SDimitry Andric                   options::OPT_fno_experimental_relative_cxx_abi_vtables);
4981fe6060f1SDimitry Andric 
49820b57cec5SDimitry Andric   // Handle segmented stacks.
4983fe6060f1SDimitry Andric   if (Args.hasFlag(options::OPT_fsplit_stack, options::OPT_fno_split_stack,
4984fe6060f1SDimitry Andric                    false))
4985fe6060f1SDimitry Andric     CmdArgs.push_back("-fsplit-stack");
4986fe6060f1SDimitry Andric 
4987fe6060f1SDimitry Andric   // -fprotect-parens=0 is default.
4988fe6060f1SDimitry Andric   if (Args.hasFlag(options::OPT_fprotect_parens,
4989fe6060f1SDimitry Andric                    options::OPT_fno_protect_parens, false))
4990fe6060f1SDimitry Andric     CmdArgs.push_back("-fprotect-parens");
49910b57cec5SDimitry Andric 
49925ffd83dbSDimitry Andric   RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs, JA);
49935ffd83dbSDimitry Andric 
4994fe6060f1SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fextend_args_EQ)) {
4995fe6060f1SDimitry Andric     const llvm::Triple::ArchType Arch = TC.getArch();
4996fe6060f1SDimitry Andric     if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
4997fe6060f1SDimitry Andric       StringRef V = A->getValue();
4998fe6060f1SDimitry Andric       if (V == "64")
4999fe6060f1SDimitry Andric         CmdArgs.push_back("-fextend-arguments=64");
5000fe6060f1SDimitry Andric       else if (V != "32")
5001fe6060f1SDimitry Andric         D.Diag(diag::err_drv_invalid_argument_to_option)
5002fe6060f1SDimitry Andric             << A->getValue() << A->getOption().getName();
5003fe6060f1SDimitry Andric     } else
5004fe6060f1SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
5005fe6060f1SDimitry Andric           << A->getOption().getName() << TripleStr;
5006fe6060f1SDimitry Andric   }
5007fe6060f1SDimitry Andric 
50085ffd83dbSDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mdouble_EQ)) {
50095ffd83dbSDimitry Andric     if (TC.getArch() == llvm::Triple::avr)
50105ffd83dbSDimitry Andric       A->render(Args, CmdArgs);
50115ffd83dbSDimitry Andric     else
50125ffd83dbSDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
50135ffd83dbSDimitry Andric           << A->getAsString(Args) << TripleStr;
50145ffd83dbSDimitry Andric   }
50150b57cec5SDimitry Andric 
5016a7dea167SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_LongDouble_Group)) {
5017480093f4SDimitry Andric     if (TC.getTriple().isX86())
5018a7dea167SDimitry Andric       A->render(Args, CmdArgs);
5019e8d8bef9SDimitry Andric     else if (TC.getTriple().isPPC() &&
5020a7dea167SDimitry Andric              (A->getOption().getID() != options::OPT_mlong_double_80))
50210b57cec5SDimitry Andric       A->render(Args, CmdArgs);
50220b57cec5SDimitry Andric     else
50230b57cec5SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
50240b57cec5SDimitry Andric           << A->getAsString(Args) << TripleStr;
50250b57cec5SDimitry Andric   }
50260b57cec5SDimitry Andric 
50270b57cec5SDimitry Andric   // Decide whether to use verbose asm. Verbose assembly is the default on
50280b57cec5SDimitry Andric   // toolchains which have the integrated assembler on by default.
50290b57cec5SDimitry Andric   bool IsIntegratedAssemblerDefault = TC.IsIntegratedAssemblerDefault();
50305ffd83dbSDimitry Andric   if (!Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
5031a7dea167SDimitry Andric                     IsIntegratedAssemblerDefault))
50325ffd83dbSDimitry Andric     CmdArgs.push_back("-fno-verbose-asm");
50330b57cec5SDimitry Andric 
5034e8d8bef9SDimitry Andric   // Parse 'none' or '$major.$minor'. Disallow -fbinutils-version=0 because we
5035e8d8bef9SDimitry Andric   // use that to indicate the MC default in the backend.
5036e8d8bef9SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fbinutils_version_EQ)) {
5037e8d8bef9SDimitry Andric     StringRef V = A->getValue();
5038e8d8bef9SDimitry Andric     unsigned Num;
5039e8d8bef9SDimitry Andric     if (V == "none")
5040e8d8bef9SDimitry Andric       A->render(Args, CmdArgs);
5041e8d8bef9SDimitry Andric     else if (!V.consumeInteger(10, Num) && Num > 0 &&
5042e8d8bef9SDimitry Andric              (V.empty() || (V.consume_front(".") &&
5043e8d8bef9SDimitry Andric                             !V.consumeInteger(10, Num) && V.empty())))
5044e8d8bef9SDimitry Andric       A->render(Args, CmdArgs);
5045e8d8bef9SDimitry Andric     else
5046e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_invalid_argument_to_option)
5047e8d8bef9SDimitry Andric           << A->getValue() << A->getOption().getName();
5048e8d8bef9SDimitry Andric   }
5049e8d8bef9SDimitry Andric 
5050fe6060f1SDimitry Andric   // If toolchain choose to use MCAsmParser for inline asm don't pass the
5051fe6060f1SDimitry Andric   // option to disable integrated-as explictly.
5052fe6060f1SDimitry Andric   if (!TC.useIntegratedAs() && !TC.parseInlineAsmUsingAsmParser())
50530b57cec5SDimitry Andric     CmdArgs.push_back("-no-integrated-as");
50540b57cec5SDimitry Andric 
50550b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
50560b57cec5SDimitry Andric     CmdArgs.push_back("-mdebug-pass");
50570b57cec5SDimitry Andric     CmdArgs.push_back("Structure");
50580b57cec5SDimitry Andric   }
50590b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
50600b57cec5SDimitry Andric     CmdArgs.push_back("-mdebug-pass");
50610b57cec5SDimitry Andric     CmdArgs.push_back("Arguments");
50620b57cec5SDimitry Andric   }
50630b57cec5SDimitry Andric 
50640b57cec5SDimitry Andric   // Enable -mconstructor-aliases except on darwin, where we have to work around
5065fe6060f1SDimitry Andric   // a linker bug (see <rdar://problem/7651567>), and CUDA/AMDGPU device code,
5066fe6060f1SDimitry Andric   // where aliases aren't supported.
5067fe6060f1SDimitry Andric   if (!RawTriple.isOSDarwin() && !RawTriple.isNVPTX() && !RawTriple.isAMDGPU())
50680b57cec5SDimitry Andric     CmdArgs.push_back("-mconstructor-aliases");
50690b57cec5SDimitry Andric 
50700b57cec5SDimitry Andric   // Darwin's kernel doesn't support guard variables; just die if we
50710b57cec5SDimitry Andric   // try to use them.
50720b57cec5SDimitry Andric   if (KernelOrKext && RawTriple.isOSDarwin())
50730b57cec5SDimitry Andric     CmdArgs.push_back("-fforbid-guard-variables");
50740b57cec5SDimitry Andric 
50750b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_mms_bitfields, options::OPT_mno_ms_bitfields,
50765ffd83dbSDimitry Andric                    Triple.isWindowsGNUEnvironment())) {
50770b57cec5SDimitry Andric     CmdArgs.push_back("-mms-bitfields");
50780b57cec5SDimitry Andric   }
50790b57cec5SDimitry Andric 
5080e8d8bef9SDimitry Andric   // Non-PIC code defaults to -fdirect-access-external-data while PIC code
5081e8d8bef9SDimitry Andric   // defaults to -fno-direct-access-external-data. Pass the option if different
5082e8d8bef9SDimitry Andric   // from the default.
5083e8d8bef9SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fdirect_access_external_data,
5084e8d8bef9SDimitry Andric                                options::OPT_fno_direct_access_external_data))
5085e8d8bef9SDimitry Andric     if (A->getOption().matches(options::OPT_fdirect_access_external_data) !=
5086e8d8bef9SDimitry Andric         (PICLevel == 0))
5087e8d8bef9SDimitry Andric       A->render(Args, CmdArgs);
50880b57cec5SDimitry Andric 
50890b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) {
50900b57cec5SDimitry Andric     CmdArgs.push_back("-fno-plt");
50910b57cec5SDimitry Andric   }
50920b57cec5SDimitry Andric 
50930b57cec5SDimitry Andric   // -fhosted is default.
50940b57cec5SDimitry Andric   // TODO: Audit uses of KernelOrKext and see where it'd be more appropriate to
50950b57cec5SDimitry Andric   // use Freestanding.
50960b57cec5SDimitry Andric   bool Freestanding =
50970b57cec5SDimitry Andric       Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
50980b57cec5SDimitry Andric       KernelOrKext;
50990b57cec5SDimitry Andric   if (Freestanding)
51000b57cec5SDimitry Andric     CmdArgs.push_back("-ffreestanding");
51010b57cec5SDimitry Andric 
51020b57cec5SDimitry Andric   // This is a coarse approximation of what llvm-gcc actually does, both
51030b57cec5SDimitry Andric   // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
51040b57cec5SDimitry Andric   // complicated ways.
5105fe6060f1SDimitry Andric   bool UnwindTables =
51060b57cec5SDimitry Andric       Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
51070b57cec5SDimitry Andric                    options::OPT_fno_asynchronous_unwind_tables,
51080b57cec5SDimitry Andric                    (TC.IsUnwindTablesDefault(Args) ||
51090b57cec5SDimitry Andric                     TC.getSanitizerArgs().needsUnwindTables()) &&
51100b57cec5SDimitry Andric                        !Freestanding);
5111fe6060f1SDimitry Andric   UnwindTables = Args.hasFlag(options::OPT_funwind_tables,
5112fe6060f1SDimitry Andric                               options::OPT_fno_unwind_tables, UnwindTables);
5113fe6060f1SDimitry Andric   if (UnwindTables)
51140b57cec5SDimitry Andric     CmdArgs.push_back("-munwind-tables");
51150b57cec5SDimitry Andric 
51165ffd83dbSDimitry Andric   // Prepare `-aux-target-cpu` and `-aux-target-feature` unless
51175ffd83dbSDimitry Andric   // `--gpu-use-aux-triple-only` is specified.
51185ffd83dbSDimitry Andric   if (!Args.getLastArg(options::OPT_gpu_use_aux_triple_only) &&
5119fe6060f1SDimitry Andric       (IsCudaDevice || IsHIPDevice)) {
51205ffd83dbSDimitry Andric     const ArgList &HostArgs =
51215ffd83dbSDimitry Andric         C.getArgsForToolChain(nullptr, StringRef(), Action::OFK_None);
51225ffd83dbSDimitry Andric     std::string HostCPU =
51235ffd83dbSDimitry Andric         getCPUName(HostArgs, *TC.getAuxTriple(), /*FromAs*/ false);
51245ffd83dbSDimitry Andric     if (!HostCPU.empty()) {
51255ffd83dbSDimitry Andric       CmdArgs.push_back("-aux-target-cpu");
51265ffd83dbSDimitry Andric       CmdArgs.push_back(Args.MakeArgString(HostCPU));
51275ffd83dbSDimitry Andric     }
51285ffd83dbSDimitry Andric     getTargetFeatures(D, *TC.getAuxTriple(), HostArgs, CmdArgs,
51295ffd83dbSDimitry Andric                       /*ForAS*/ false, /*IsAux*/ true);
51305ffd83dbSDimitry Andric   }
51315ffd83dbSDimitry Andric 
51320b57cec5SDimitry Andric   TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind());
51330b57cec5SDimitry Andric 
51340b57cec5SDimitry Andric   // FIXME: Handle -mtune=.
51350b57cec5SDimitry Andric   (void)Args.hasArg(options::OPT_mtune_EQ);
51360b57cec5SDimitry Andric 
51370b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
51385ffd83dbSDimitry Andric     StringRef CM = A->getValue();
51395ffd83dbSDimitry Andric     if (CM == "small" || CM == "kernel" || CM == "medium" || CM == "large" ||
5140fe6060f1SDimitry Andric         CM == "tiny") {
5141fe6060f1SDimitry Andric       if (Triple.isOSAIX() && CM == "medium")
5142fe6060f1SDimitry Andric         CmdArgs.push_back("-mcmodel=large");
51435ffd83dbSDimitry Andric       else
5144fe6060f1SDimitry Andric         A->render(Args, CmdArgs);
5145fe6060f1SDimitry Andric     } else {
51465ffd83dbSDimitry Andric       D.Diag(diag::err_drv_invalid_argument_to_option)
51475ffd83dbSDimitry Andric           << CM << A->getOption().getName();
51480b57cec5SDimitry Andric     }
5149fe6060f1SDimitry Andric   }
51500b57cec5SDimitry Andric 
5151480093f4SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mtls_size_EQ)) {
5152480093f4SDimitry Andric     StringRef Value = A->getValue();
5153480093f4SDimitry Andric     unsigned TLSSize = 0;
5154480093f4SDimitry Andric     Value.getAsInteger(10, TLSSize);
5155480093f4SDimitry Andric     if (!Triple.isAArch64() || !Triple.isOSBinFormatELF())
5156480093f4SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
5157480093f4SDimitry Andric           << A->getOption().getName() << TripleStr;
5158480093f4SDimitry Andric     if (TLSSize != 12 && TLSSize != 24 && TLSSize != 32 && TLSSize != 48)
5159480093f4SDimitry Andric       D.Diag(diag::err_drv_invalid_int_value)
5160480093f4SDimitry Andric           << A->getOption().getName() << Value;
5161480093f4SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_mtls_size_EQ);
5162480093f4SDimitry Andric   }
5163480093f4SDimitry Andric 
51640b57cec5SDimitry Andric   // Add the target cpu
51650b57cec5SDimitry Andric   std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false);
51660b57cec5SDimitry Andric   if (!CPU.empty()) {
51670b57cec5SDimitry Andric     CmdArgs.push_back("-target-cpu");
51680b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(CPU));
51690b57cec5SDimitry Andric   }
51700b57cec5SDimitry Andric 
51710b57cec5SDimitry Andric   RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs);
51720b57cec5SDimitry Andric 
5173fe6060f1SDimitry Andric   // FIXME: For now we want to demote any errors to warnings, when they have
5174fe6060f1SDimitry Andric   // been raised for asking the wrong question of scalable vectors, such as
5175fe6060f1SDimitry Andric   // asking for the fixed number of elements. This may happen because code that
5176fe6060f1SDimitry Andric   // is not yet ported to work for scalable vectors uses the wrong interfaces,
5177fe6060f1SDimitry Andric   // whereas the behaviour is actually correct. Emitting a warning helps bring
5178fe6060f1SDimitry Andric   // up scalable vector support in an incremental way. When scalable vector
5179fe6060f1SDimitry Andric   // support is stable enough, all uses of wrong interfaces should be considered
5180fe6060f1SDimitry Andric   // as errors, but until then, we can live with a warning being emitted by the
5181fe6060f1SDimitry Andric   // compiler. This way, Clang can be used to compile code with scalable vectors
5182fe6060f1SDimitry Andric   // and identify possible issues.
5183fe6060f1SDimitry Andric   if (isa<BackendJobAction>(JA)) {
5184fe6060f1SDimitry Andric     CmdArgs.push_back("-mllvm");
5185fe6060f1SDimitry Andric     CmdArgs.push_back("-treat-scalable-fixed-error-as-warning");
5186fe6060f1SDimitry Andric   }
5187fe6060f1SDimitry Andric 
51880b57cec5SDimitry Andric   // These two are potentially updated by AddClangCLArgs.
51890b57cec5SDimitry Andric   codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo;
51900b57cec5SDimitry Andric   bool EmitCodeView = false;
51910b57cec5SDimitry Andric 
51920b57cec5SDimitry Andric   // Add clang-cl arguments.
51930b57cec5SDimitry Andric   types::ID InputType = Input.getType();
51940b57cec5SDimitry Andric   if (D.IsCLMode())
51950b57cec5SDimitry Andric     AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView);
51960b57cec5SDimitry Andric 
5197e8d8bef9SDimitry Andric   DwarfFissionKind DwarfFission = DwarfFissionKind::None;
5198e8d8bef9SDimitry Andric   renderDebugOptions(TC, D, RawTriple, Args, EmitCodeView,
5199e8d8bef9SDimitry Andric                      types::isLLVMIR(InputType), CmdArgs, DebugInfoKind,
5200e8d8bef9SDimitry Andric                      DwarfFission);
52010b57cec5SDimitry Andric 
52020b57cec5SDimitry Andric   // Add the split debug info name to the command lines here so we
52030b57cec5SDimitry Andric   // can propagate it to the backend.
52040b57cec5SDimitry Andric   bool SplitDWARF = (DwarfFission != DwarfFissionKind::None) &&
5205e8d8bef9SDimitry Andric                     (TC.getTriple().isOSBinFormatELF() ||
5206e8d8bef9SDimitry Andric                      TC.getTriple().isOSBinFormatWasm()) &&
52070b57cec5SDimitry Andric                     (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) ||
52080b57cec5SDimitry Andric                      isa<BackendJobAction>(JA));
52090b57cec5SDimitry Andric   if (SplitDWARF) {
5210e8d8bef9SDimitry Andric     const char *SplitDWARFOut = SplitDebugName(JA, Args, Input, Output);
52110b57cec5SDimitry Andric     CmdArgs.push_back("-split-dwarf-file");
52120b57cec5SDimitry Andric     CmdArgs.push_back(SplitDWARFOut);
52130b57cec5SDimitry Andric     if (DwarfFission == DwarfFissionKind::Split) {
52140b57cec5SDimitry Andric       CmdArgs.push_back("-split-dwarf-output");
52150b57cec5SDimitry Andric       CmdArgs.push_back(SplitDWARFOut);
52160b57cec5SDimitry Andric     }
52170b57cec5SDimitry Andric   }
52180b57cec5SDimitry Andric 
52190b57cec5SDimitry Andric   // Pass the linker version in use.
52200b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
52210b57cec5SDimitry Andric     CmdArgs.push_back("-target-linker-version");
52220b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
52230b57cec5SDimitry Andric   }
52240b57cec5SDimitry Andric 
52250b57cec5SDimitry Andric   // Explicitly error on some things we know we don't support and can't just
52260b57cec5SDimitry Andric   // ignore.
52270b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_fallow_unsupported)) {
52280b57cec5SDimitry Andric     Arg *Unsupported;
52290b57cec5SDimitry Andric     if (types::isCXX(InputType) && RawTriple.isOSDarwin() &&
52300b57cec5SDimitry Andric         TC.getArch() == llvm::Triple::x86) {
52310b57cec5SDimitry Andric       if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
52320b57cec5SDimitry Andric           (Unsupported = Args.getLastArg(options::OPT_mkernel)))
52330b57cec5SDimitry Andric         D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
52340b57cec5SDimitry Andric             << Unsupported->getOption().getName();
52350b57cec5SDimitry Andric     }
52360b57cec5SDimitry Andric     // The faltivec option has been superseded by the maltivec option.
52370b57cec5SDimitry Andric     if ((Unsupported = Args.getLastArg(options::OPT_faltivec)))
52380b57cec5SDimitry Andric       D.Diag(diag::err_drv_clang_unsupported_opt_faltivec)
52390b57cec5SDimitry Andric           << Unsupported->getOption().getName()
52400b57cec5SDimitry Andric           << "please use -maltivec and include altivec.h explicitly";
52410b57cec5SDimitry Andric     if ((Unsupported = Args.getLastArg(options::OPT_fno_altivec)))
52420b57cec5SDimitry Andric       D.Diag(diag::err_drv_clang_unsupported_opt_faltivec)
52430b57cec5SDimitry Andric           << Unsupported->getOption().getName() << "please use -mno-altivec";
52440b57cec5SDimitry Andric   }
52450b57cec5SDimitry Andric 
52460b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_v);
52475ffd83dbSDimitry Andric 
52485ffd83dbSDimitry Andric   if (Args.getLastArg(options::OPT_H)) {
52495ffd83dbSDimitry Andric     CmdArgs.push_back("-H");
52505ffd83dbSDimitry Andric     CmdArgs.push_back("-sys-header-deps");
52515ffd83dbSDimitry Andric   }
5252fe6060f1SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_fshow_skipped_includes);
52535ffd83dbSDimitry Andric 
52540b57cec5SDimitry Andric   if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
52550b57cec5SDimitry Andric     CmdArgs.push_back("-header-include-file");
5256fe6060f1SDimitry Andric     CmdArgs.push_back(!D.CCPrintHeadersFilename.empty()
5257fe6060f1SDimitry Andric                           ? D.CCPrintHeadersFilename.c_str()
52580b57cec5SDimitry Andric                           : "-");
52595ffd83dbSDimitry Andric     CmdArgs.push_back("-sys-header-deps");
52600b57cec5SDimitry Andric   }
52610b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_P);
52620b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
52630b57cec5SDimitry Andric 
52640b57cec5SDimitry Andric   if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
52650b57cec5SDimitry Andric     CmdArgs.push_back("-diagnostic-log-file");
5266fe6060f1SDimitry Andric     CmdArgs.push_back(!D.CCLogDiagnosticsFilename.empty()
5267fe6060f1SDimitry Andric                           ? D.CCLogDiagnosticsFilename.c_str()
52680b57cec5SDimitry Andric                           : "-");
52690b57cec5SDimitry Andric   }
52700b57cec5SDimitry Andric 
527113138422SDimitry Andric   // Give the gen diagnostics more chances to succeed, by avoiding intentional
527213138422SDimitry Andric   // crashes.
527313138422SDimitry Andric   if (D.CCGenDiagnostics)
527413138422SDimitry Andric     CmdArgs.push_back("-disable-pragma-debug-crash");
527513138422SDimitry Andric 
5276fe6060f1SDimitry Andric   // Allow backend to put its diagnostic files in the same place as frontend
5277fe6060f1SDimitry Andric   // crash diagnostics files.
5278fe6060f1SDimitry Andric   if (Args.hasArg(options::OPT_fcrash_diagnostics_dir)) {
5279fe6060f1SDimitry Andric     StringRef Dir = Args.getLastArgValue(options::OPT_fcrash_diagnostics_dir);
5280fe6060f1SDimitry Andric     CmdArgs.push_back("-mllvm");
5281fe6060f1SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-crash-diagnostics-dir=" + Dir));
5282fe6060f1SDimitry Andric   }
5283fe6060f1SDimitry Andric 
52840b57cec5SDimitry Andric   bool UseSeparateSections = isUseSeparateSections(Triple);
52850b57cec5SDimitry Andric 
52860b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_ffunction_sections,
52870b57cec5SDimitry Andric                    options::OPT_fno_function_sections, UseSeparateSections)) {
52880b57cec5SDimitry Andric     CmdArgs.push_back("-ffunction-sections");
52890b57cec5SDimitry Andric   }
52900b57cec5SDimitry Andric 
52915ffd83dbSDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fbasic_block_sections_EQ)) {
52925ffd83dbSDimitry Andric     StringRef Val = A->getValue();
5293fe6060f1SDimitry Andric     if (Triple.isX86() && Triple.isOSBinFormatELF()) {
52945ffd83dbSDimitry Andric       if (Val != "all" && Val != "labels" && Val != "none" &&
5295e8d8bef9SDimitry Andric           !Val.startswith("list="))
52965ffd83dbSDimitry Andric         D.Diag(diag::err_drv_invalid_value)
52975ffd83dbSDimitry Andric             << A->getAsString(Args) << A->getValue();
52985ffd83dbSDimitry Andric       else
52995ffd83dbSDimitry Andric         A->render(Args, CmdArgs);
5300fe6060f1SDimitry Andric     } else if (Triple.isNVPTX()) {
5301fe6060f1SDimitry Andric       // Do not pass the option to the GPU compilation. We still want it enabled
5302fe6060f1SDimitry Andric       // for the host-side compilation, so seeing it here is not an error.
5303fe6060f1SDimitry Andric     } else if (Val != "none") {
5304fe6060f1SDimitry Andric       // =none is allowed everywhere. It's useful for overriding the option
5305fe6060f1SDimitry Andric       // and is the same as not specifying the option.
5306e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
5307e8d8bef9SDimitry Andric           << A->getAsString(Args) << TripleStr;
5308e8d8bef9SDimitry Andric     }
53095ffd83dbSDimitry Andric   }
53105ffd83dbSDimitry Andric 
5311e8d8bef9SDimitry Andric   bool HasDefaultDataSections = Triple.isOSBinFormatXCOFF();
53120b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections,
5313e8d8bef9SDimitry Andric                    UseSeparateSections || HasDefaultDataSections)) {
53140b57cec5SDimitry Andric     CmdArgs.push_back("-fdata-sections");
53150b57cec5SDimitry Andric   }
53160b57cec5SDimitry Andric 
53170b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_funique_section_names,
53180b57cec5SDimitry Andric                     options::OPT_fno_unique_section_names, true))
53190b57cec5SDimitry Andric     CmdArgs.push_back("-fno-unique-section-names");
53200b57cec5SDimitry Andric 
53215ffd83dbSDimitry Andric   if (Args.hasFlag(options::OPT_funique_internal_linkage_names,
53225ffd83dbSDimitry Andric                    options::OPT_fno_unique_internal_linkage_names, false))
53235ffd83dbSDimitry Andric     CmdArgs.push_back("-funique-internal-linkage-names");
53245ffd83dbSDimitry Andric 
53255ffd83dbSDimitry Andric   if (Args.hasFlag(options::OPT_funique_basic_block_section_names,
53265ffd83dbSDimitry Andric                    options::OPT_fno_unique_basic_block_section_names, false))
53275ffd83dbSDimitry Andric     CmdArgs.push_back("-funique-basic-block-section-names");
53285ffd83dbSDimitry Andric 
5329e8d8bef9SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fsplit_machine_functions,
5330e8d8bef9SDimitry Andric                                options::OPT_fno_split_machine_functions)) {
5331e8d8bef9SDimitry Andric     // This codegen pass is only available on x86-elf targets.
5332e8d8bef9SDimitry Andric     if (Triple.isX86() && Triple.isOSBinFormatELF()) {
5333e8d8bef9SDimitry Andric       if (A->getOption().matches(options::OPT_fsplit_machine_functions))
5334e8d8bef9SDimitry Andric         A->render(Args, CmdArgs);
5335e8d8bef9SDimitry Andric     } else {
5336e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
5337e8d8bef9SDimitry Andric           << A->getAsString(Args) << TripleStr;
5338e8d8bef9SDimitry Andric     }
5339e8d8bef9SDimitry Andric   }
5340e8d8bef9SDimitry Andric 
53410b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_finstrument_functions,
53420b57cec5SDimitry Andric                   options::OPT_finstrument_functions_after_inlining,
53430b57cec5SDimitry Andric                   options::OPT_finstrument_function_entry_bare);
53440b57cec5SDimitry Andric 
53455ffd83dbSDimitry Andric   // NVPTX/AMDGCN doesn't support PGO or coverage. There's no runtime support
53465ffd83dbSDimitry Andric   // for sampling, overhead of call arc collection is way too high and there's
53475ffd83dbSDimitry Andric   // no way to collect the output.
53485ffd83dbSDimitry Andric   if (!Triple.isNVPTX() && !Triple.isAMDGCN())
53490b57cec5SDimitry Andric     addPGOAndCoverageFlags(TC, C, D, Output, Args, CmdArgs);
53500b57cec5SDimitry Andric 
53510b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fclang_abi_compat_EQ);
53520b57cec5SDimitry Andric 
53530b57cec5SDimitry Andric   // Add runtime flag for PS4 when PGO, coverage, or sanitizers are enabled.
53540b57cec5SDimitry Andric   if (RawTriple.isPS4CPU() &&
53550b57cec5SDimitry Andric       !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
53560b57cec5SDimitry Andric     PS4cpu::addProfileRTArgs(TC, Args, CmdArgs);
53570b57cec5SDimitry Andric     PS4cpu::addSanitizerArgs(TC, CmdArgs);
53580b57cec5SDimitry Andric   }
53590b57cec5SDimitry Andric 
53600b57cec5SDimitry Andric   // Pass options for controlling the default header search paths.
53610b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_nostdinc)) {
53620b57cec5SDimitry Andric     CmdArgs.push_back("-nostdsysteminc");
53630b57cec5SDimitry Andric     CmdArgs.push_back("-nobuiltininc");
53640b57cec5SDimitry Andric   } else {
53650b57cec5SDimitry Andric     if (Args.hasArg(options::OPT_nostdlibinc))
53660b57cec5SDimitry Andric       CmdArgs.push_back("-nostdsysteminc");
53670b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
53680b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
53690b57cec5SDimitry Andric   }
53700b57cec5SDimitry Andric 
53710b57cec5SDimitry Andric   // Pass the path to compiler resource files.
53720b57cec5SDimitry Andric   CmdArgs.push_back("-resource-dir");
53730b57cec5SDimitry Andric   CmdArgs.push_back(D.ResourceDir.c_str());
53740b57cec5SDimitry Andric 
53750b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_working_directory);
53760b57cec5SDimitry Andric 
53770b57cec5SDimitry Andric   RenderARCMigrateToolOptions(D, Args, CmdArgs);
53780b57cec5SDimitry Andric 
53790b57cec5SDimitry Andric   // Add preprocessing options like -I, -D, etc. if we are using the
53800b57cec5SDimitry Andric   // preprocessor.
53810b57cec5SDimitry Andric   //
53820b57cec5SDimitry Andric   // FIXME: Support -fpreprocessed
53830b57cec5SDimitry Andric   if (types::getPreprocessedType(InputType) != types::TY_INVALID)
53840b57cec5SDimitry Andric     AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
53850b57cec5SDimitry Andric 
53860b57cec5SDimitry Andric   // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
53870b57cec5SDimitry Andric   // that "The compiler can only warn and ignore the option if not recognized".
53880b57cec5SDimitry Andric   // When building with ccache, it will pass -D options to clang even on
53890b57cec5SDimitry Andric   // preprocessed inputs and configure concludes that -fPIC is not supported.
53900b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_D);
53910b57cec5SDimitry Andric 
53920b57cec5SDimitry Andric   // Manually translate -O4 to -O3; let clang reject others.
53930b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
53940b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_O4)) {
53950b57cec5SDimitry Andric       CmdArgs.push_back("-O3");
53960b57cec5SDimitry Andric       D.Diag(diag::warn_O4_is_O3);
53970b57cec5SDimitry Andric     } else {
53980b57cec5SDimitry Andric       A->render(Args, CmdArgs);
53990b57cec5SDimitry Andric     }
54000b57cec5SDimitry Andric   }
54010b57cec5SDimitry Andric 
54020b57cec5SDimitry Andric   // Warn about ignored options to clang.
54030b57cec5SDimitry Andric   for (const Arg *A :
54040b57cec5SDimitry Andric        Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) {
54050b57cec5SDimitry Andric     D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args);
54060b57cec5SDimitry Andric     A->claim();
54070b57cec5SDimitry Andric   }
54080b57cec5SDimitry Andric 
54090b57cec5SDimitry Andric   for (const Arg *A :
54100b57cec5SDimitry Andric        Args.filtered(options::OPT_clang_ignored_legacy_options_Group)) {
54110b57cec5SDimitry Andric     D.Diag(diag::warn_ignored_clang_option) << A->getAsString(Args);
54120b57cec5SDimitry Andric     A->claim();
54130b57cec5SDimitry Andric   }
54140b57cec5SDimitry Andric 
54150b57cec5SDimitry Andric   claimNoWarnArgs(Args);
54160b57cec5SDimitry Andric 
54170b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_R_Group);
54180b57cec5SDimitry Andric 
54190b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
54200b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
54210b57cec5SDimitry Andric     CmdArgs.push_back("-pedantic");
54220b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
54230b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_w);
54240b57cec5SDimitry Andric 
54250b57cec5SDimitry Andric   // Fixed point flags
54260b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_ffixed_point, options::OPT_fno_fixed_point,
54270b57cec5SDimitry Andric                    /*Default=*/false))
54280b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_ffixed_point);
54290b57cec5SDimitry Andric 
5430fe6060f1SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fcxx_abi_EQ))
5431fe6060f1SDimitry Andric     A->render(Args, CmdArgs);
5432fe6060f1SDimitry Andric 
5433fe6060f1SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables,
5434fe6060f1SDimitry Andric                   options::OPT_fno_experimental_relative_cxx_abi_vtables);
5435fe6060f1SDimitry Andric 
54360b57cec5SDimitry Andric   // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
54370b57cec5SDimitry Andric   // (-ansi is equivalent to -std=c89 or -std=c++98).
54380b57cec5SDimitry Andric   //
54390b57cec5SDimitry Andric   // If a std is supplied, only add -trigraphs if it follows the
54400b57cec5SDimitry Andric   // option.
5441fe6060f1SDimitry Andric   bool ImplyVCPPCVer = false;
54420b57cec5SDimitry Andric   bool ImplyVCPPCXXVer = false;
54430b57cec5SDimitry Andric   const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi);
54440b57cec5SDimitry Andric   if (Std) {
54450b57cec5SDimitry Andric     if (Std->getOption().matches(options::OPT_ansi))
54460b57cec5SDimitry Andric       if (types::isCXX(InputType))
54470b57cec5SDimitry Andric         CmdArgs.push_back("-std=c++98");
54480b57cec5SDimitry Andric       else
54490b57cec5SDimitry Andric         CmdArgs.push_back("-std=c89");
54500b57cec5SDimitry Andric     else
54510b57cec5SDimitry Andric       Std->render(Args, CmdArgs);
54520b57cec5SDimitry Andric 
54530b57cec5SDimitry Andric     // If -f(no-)trigraphs appears after the language standard flag, honor it.
54540b57cec5SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
54550b57cec5SDimitry Andric                                  options::OPT_ftrigraphs,
54560b57cec5SDimitry Andric                                  options::OPT_fno_trigraphs))
54570b57cec5SDimitry Andric       if (A != Std)
54580b57cec5SDimitry Andric         A->render(Args, CmdArgs);
54590b57cec5SDimitry Andric   } else {
54600b57cec5SDimitry Andric     // Honor -std-default.
54610b57cec5SDimitry Andric     //
54620b57cec5SDimitry Andric     // FIXME: Clang doesn't correctly handle -std= when the input language
54630b57cec5SDimitry Andric     // doesn't match. For the time being just ignore this for C++ inputs;
54640b57cec5SDimitry Andric     // eventually we want to do all the standard defaulting here instead of
54650b57cec5SDimitry Andric     // splitting it between the driver and clang -cc1.
5466fe6060f1SDimitry Andric     if (!types::isCXX(InputType)) {
5467fe6060f1SDimitry Andric       if (!Args.hasArg(options::OPT__SLASH_std)) {
54680b57cec5SDimitry Andric         Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=",
54690b57cec5SDimitry Andric                                   /*Joined=*/true);
5470fe6060f1SDimitry Andric       } else
5471fe6060f1SDimitry Andric         ImplyVCPPCVer = true;
5472fe6060f1SDimitry Andric     }
54730b57cec5SDimitry Andric     else if (IsWindowsMSVC)
54740b57cec5SDimitry Andric       ImplyVCPPCXXVer = true;
54750b57cec5SDimitry Andric 
54760b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_ftrigraphs,
54770b57cec5SDimitry Andric                     options::OPT_fno_trigraphs);
54785ffd83dbSDimitry Andric 
54795ffd83dbSDimitry Andric     // HIP headers has minimum C++ standard requirements. Therefore set the
54805ffd83dbSDimitry Andric     // default language standard.
54815ffd83dbSDimitry Andric     if (IsHIP)
54825ffd83dbSDimitry Andric       CmdArgs.push_back(IsWindowsMSVC ? "-std=c++14" : "-std=c++11");
54830b57cec5SDimitry Andric   }
54840b57cec5SDimitry Andric 
54850b57cec5SDimitry Andric   // GCC's behavior for -Wwrite-strings is a bit strange:
54860b57cec5SDimitry Andric   //  * In C, this "warning flag" changes the types of string literals from
54870b57cec5SDimitry Andric   //    'char[N]' to 'const char[N]', and thus triggers an unrelated warning
54880b57cec5SDimitry Andric   //    for the discarded qualifier.
54890b57cec5SDimitry Andric   //  * In C++, this is just a normal warning flag.
54900b57cec5SDimitry Andric   //
54910b57cec5SDimitry Andric   // Implementing this warning correctly in C is hard, so we follow GCC's
54920b57cec5SDimitry Andric   // behavior for now. FIXME: Directly diagnose uses of a string literal as
54930b57cec5SDimitry Andric   // a non-const char* in C, rather than using this crude hack.
54940b57cec5SDimitry Andric   if (!types::isCXX(InputType)) {
54950b57cec5SDimitry Andric     // FIXME: This should behave just like a warning flag, and thus should also
54960b57cec5SDimitry Andric     // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on.
54970b57cec5SDimitry Andric     Arg *WriteStrings =
54980b57cec5SDimitry Andric         Args.getLastArg(options::OPT_Wwrite_strings,
54990b57cec5SDimitry Andric                         options::OPT_Wno_write_strings, options::OPT_w);
55000b57cec5SDimitry Andric     if (WriteStrings &&
55010b57cec5SDimitry Andric         WriteStrings->getOption().matches(options::OPT_Wwrite_strings))
55020b57cec5SDimitry Andric       CmdArgs.push_back("-fconst-strings");
55030b57cec5SDimitry Andric   }
55040b57cec5SDimitry Andric 
55050b57cec5SDimitry Andric   // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
55060b57cec5SDimitry Andric   // during C++ compilation, which it is by default. GCC keeps this define even
55070b57cec5SDimitry Andric   // in the presence of '-w', match this behavior bug-for-bug.
55080b57cec5SDimitry Andric   if (types::isCXX(InputType) &&
55090b57cec5SDimitry Andric       Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
55100b57cec5SDimitry Andric                    true)) {
55110b57cec5SDimitry Andric     CmdArgs.push_back("-fdeprecated-macro");
55120b57cec5SDimitry Andric   }
55130b57cec5SDimitry Andric 
55140b57cec5SDimitry Andric   // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
55150b57cec5SDimitry Andric   if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
55160b57cec5SDimitry Andric     if (Asm->getOption().matches(options::OPT_fasm))
55170b57cec5SDimitry Andric       CmdArgs.push_back("-fgnu-keywords");
55180b57cec5SDimitry Andric     else
55190b57cec5SDimitry Andric       CmdArgs.push_back("-fno-gnu-keywords");
55200b57cec5SDimitry Andric   }
55210b57cec5SDimitry Andric 
5522480093f4SDimitry Andric   if (!ShouldEnableAutolink(Args, TC, JA))
55230b57cec5SDimitry Andric     CmdArgs.push_back("-fno-autolink");
55240b57cec5SDimitry Andric 
55250b57cec5SDimitry Andric   // Add in -fdebug-compilation-dir if necessary.
5526a7dea167SDimitry Andric   addDebugCompDirArg(Args, CmdArgs, D.getVFS());
55270b57cec5SDimitry Andric 
55280b57cec5SDimitry Andric   addDebugPrefixMapArg(D, Args, CmdArgs);
55290b57cec5SDimitry Andric 
55300b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
55310b57cec5SDimitry Andric                                options::OPT_ftemplate_depth_EQ)) {
55320b57cec5SDimitry Andric     CmdArgs.push_back("-ftemplate-depth");
55330b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
55340b57cec5SDimitry Andric   }
55350b57cec5SDimitry Andric 
55360b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) {
55370b57cec5SDimitry Andric     CmdArgs.push_back("-foperator-arrow-depth");
55380b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
55390b57cec5SDimitry Andric   }
55400b57cec5SDimitry Andric 
55410b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
55420b57cec5SDimitry Andric     CmdArgs.push_back("-fconstexpr-depth");
55430b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
55440b57cec5SDimitry Andric   }
55450b57cec5SDimitry Andric 
55460b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) {
55470b57cec5SDimitry Andric     CmdArgs.push_back("-fconstexpr-steps");
55480b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
55490b57cec5SDimitry Andric   }
55500b57cec5SDimitry Andric 
5551a7dea167SDimitry Andric   if (Args.hasArg(options::OPT_fexperimental_new_constant_interpreter))
5552a7dea167SDimitry Andric     CmdArgs.push_back("-fexperimental-new-constant-interpreter");
5553a7dea167SDimitry Andric 
55540b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
55550b57cec5SDimitry Andric     CmdArgs.push_back("-fbracket-depth");
55560b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
55570b57cec5SDimitry Andric   }
55580b57cec5SDimitry Andric 
55590b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
55600b57cec5SDimitry Andric                                options::OPT_Wlarge_by_value_copy_def)) {
55610b57cec5SDimitry Andric     if (A->getNumValues()) {
55620b57cec5SDimitry Andric       StringRef bytes = A->getValue();
55630b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
55640b57cec5SDimitry Andric     } else
55650b57cec5SDimitry Andric       CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
55660b57cec5SDimitry Andric   }
55670b57cec5SDimitry Andric 
55680b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_relocatable_pch))
55690b57cec5SDimitry Andric     CmdArgs.push_back("-relocatable-pch");
55700b57cec5SDimitry Andric 
55710b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fcf_runtime_abi_EQ)) {
55720b57cec5SDimitry Andric     static const char *kCFABIs[] = {
55730b57cec5SDimitry Andric       "standalone", "objc", "swift", "swift-5.0", "swift-4.2", "swift-4.1",
55740b57cec5SDimitry Andric     };
55750b57cec5SDimitry Andric 
55760b57cec5SDimitry Andric     if (find(kCFABIs, StringRef(A->getValue())) == std::end(kCFABIs))
55770b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_cf_runtime_abi) << A->getValue();
55780b57cec5SDimitry Andric     else
55790b57cec5SDimitry Andric       A->render(Args, CmdArgs);
55800b57cec5SDimitry Andric   }
55810b57cec5SDimitry Andric 
55820b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
55830b57cec5SDimitry Andric     CmdArgs.push_back("-fconstant-string-class");
55840b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
55850b57cec5SDimitry Andric   }
55860b57cec5SDimitry Andric 
55870b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
55880b57cec5SDimitry Andric     CmdArgs.push_back("-ftabstop");
55890b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
55900b57cec5SDimitry Andric   }
55910b57cec5SDimitry Andric 
55920b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fstack_size_section,
55930b57cec5SDimitry Andric                    options::OPT_fno_stack_size_section, RawTriple.isPS4()))
55940b57cec5SDimitry Andric     CmdArgs.push_back("-fstack-size-section");
55950b57cec5SDimitry Andric 
5596fe6060f1SDimitry Andric   if (Args.hasArg(options::OPT_fstack_usage)) {
5597fe6060f1SDimitry Andric     CmdArgs.push_back("-stack-usage-file");
5598fe6060f1SDimitry Andric 
5599fe6060f1SDimitry Andric     if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
5600fe6060f1SDimitry Andric       SmallString<128> OutputFilename(OutputOpt->getValue());
5601fe6060f1SDimitry Andric       llvm::sys::path::replace_extension(OutputFilename, "su");
5602fe6060f1SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(OutputFilename));
5603fe6060f1SDimitry Andric     } else
5604fe6060f1SDimitry Andric       CmdArgs.push_back(
5605fe6060f1SDimitry Andric           Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".su"));
5606fe6060f1SDimitry Andric   }
5607fe6060f1SDimitry Andric 
56080b57cec5SDimitry Andric   CmdArgs.push_back("-ferror-limit");
56090b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
56100b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
56110b57cec5SDimitry Andric   else
56120b57cec5SDimitry Andric     CmdArgs.push_back("19");
56130b57cec5SDimitry Andric 
56140b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
56150b57cec5SDimitry Andric     CmdArgs.push_back("-fmacro-backtrace-limit");
56160b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
56170b57cec5SDimitry Andric   }
56180b57cec5SDimitry Andric 
56190b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
56200b57cec5SDimitry Andric     CmdArgs.push_back("-ftemplate-backtrace-limit");
56210b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
56220b57cec5SDimitry Andric   }
56230b57cec5SDimitry Andric 
56240b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
56250b57cec5SDimitry Andric     CmdArgs.push_back("-fconstexpr-backtrace-limit");
56260b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
56270b57cec5SDimitry Andric   }
56280b57cec5SDimitry Andric 
56290b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fspell_checking_limit_EQ)) {
56300b57cec5SDimitry Andric     CmdArgs.push_back("-fspell-checking-limit");
56310b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
56320b57cec5SDimitry Andric   }
56330b57cec5SDimitry Andric 
56340b57cec5SDimitry Andric   // Pass -fmessage-length=.
56355ffd83dbSDimitry Andric   unsigned MessageLength = 0;
56360b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
56375ffd83dbSDimitry Andric     StringRef V(A->getValue());
56385ffd83dbSDimitry Andric     if (V.getAsInteger(0, MessageLength))
56395ffd83dbSDimitry Andric       D.Diag(diag::err_drv_invalid_argument_to_option)
56405ffd83dbSDimitry Andric           << V << A->getOption().getName();
56410b57cec5SDimitry Andric   } else {
56420b57cec5SDimitry Andric     // If -fmessage-length=N was not specified, determine whether this is a
56430b57cec5SDimitry Andric     // terminal and, if so, implicitly define -fmessage-length appropriately.
56445ffd83dbSDimitry Andric     MessageLength = llvm::sys::Process::StandardErrColumns();
56450b57cec5SDimitry Andric   }
56465ffd83dbSDimitry Andric   if (MessageLength != 0)
56475ffd83dbSDimitry Andric     CmdArgs.push_back(
56485ffd83dbSDimitry Andric         Args.MakeArgString("-fmessage-length=" + Twine(MessageLength)));
56490b57cec5SDimitry Andric 
56500b57cec5SDimitry Andric   // -fvisibility= and -fvisibility-ms-compat are of a piece.
56510b57cec5SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
56520b57cec5SDimitry Andric                                      options::OPT_fvisibility_ms_compat)) {
56530b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
56540b57cec5SDimitry Andric       CmdArgs.push_back("-fvisibility");
56550b57cec5SDimitry Andric       CmdArgs.push_back(A->getValue());
56560b57cec5SDimitry Andric     } else {
56570b57cec5SDimitry Andric       assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
56580b57cec5SDimitry Andric       CmdArgs.push_back("-fvisibility");
56590b57cec5SDimitry Andric       CmdArgs.push_back("hidden");
56600b57cec5SDimitry Andric       CmdArgs.push_back("-ftype-visibility");
56610b57cec5SDimitry Andric       CmdArgs.push_back("default");
56620b57cec5SDimitry Andric     }
56630b57cec5SDimitry Andric   }
56640b57cec5SDimitry Andric 
5665e8d8bef9SDimitry Andric   if (!RawTriple.isPS4())
5666e8d8bef9SDimitry Andric     if (const Arg *A =
5667e8d8bef9SDimitry Andric             Args.getLastArg(options::OPT_fvisibility_from_dllstorageclass,
5668e8d8bef9SDimitry Andric                             options::OPT_fno_visibility_from_dllstorageclass)) {
5669e8d8bef9SDimitry Andric       if (A->getOption().matches(
5670e8d8bef9SDimitry Andric               options::OPT_fvisibility_from_dllstorageclass)) {
5671e8d8bef9SDimitry Andric         CmdArgs.push_back("-fvisibility-from-dllstorageclass");
5672e8d8bef9SDimitry Andric         Args.AddLastArg(CmdArgs, options::OPT_fvisibility_dllexport_EQ);
5673e8d8bef9SDimitry Andric         Args.AddLastArg(CmdArgs, options::OPT_fvisibility_nodllstorageclass_EQ);
5674e8d8bef9SDimitry Andric         Args.AddLastArg(CmdArgs, options::OPT_fvisibility_externs_dllimport_EQ);
5675e8d8bef9SDimitry Andric         Args.AddLastArg(CmdArgs,
5676e8d8bef9SDimitry Andric                         options::OPT_fvisibility_externs_nodllstorageclass_EQ);
5677e8d8bef9SDimitry Andric       }
5678e8d8bef9SDimitry Andric     }
5679e8d8bef9SDimitry Andric 
5680e8d8bef9SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_mignore_xcoff_visibility)) {
5681e8d8bef9SDimitry Andric     if (Triple.isOSAIX())
5682e8d8bef9SDimitry Andric       CmdArgs.push_back("-mignore-xcoff-visibility");
5683e8d8bef9SDimitry Andric     else
5684e8d8bef9SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
5685e8d8bef9SDimitry Andric           << A->getAsString(Args) << TripleStr;
5686e8d8bef9SDimitry Andric   }
5687e8d8bef9SDimitry Andric 
5688fe6060f1SDimitry Andric 
5689fe6060f1SDimitry Andric   if (Args.hasFlag(options::OPT_fvisibility_inlines_hidden,
5690fe6060f1SDimitry Andric                     options::OPT_fno_visibility_inlines_hidden, false))
5691fe6060f1SDimitry Andric     CmdArgs.push_back("-fvisibility-inlines-hidden");
5692fe6060f1SDimitry Andric 
5693e8d8bef9SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden_static_local_var,
5694e8d8bef9SDimitry Andric                            options::OPT_fno_visibility_inlines_hidden_static_local_var);
56950b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden);
56960b57cec5SDimitry Andric 
56970b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
56980b57cec5SDimitry Andric 
5699fe6060f1SDimitry Andric   if (Args.hasFlag(options::OPT_fno_operator_names,
5700fe6060f1SDimitry Andric                    options::OPT_foperator_names, false))
5701fe6060f1SDimitry Andric     CmdArgs.push_back("-fno-operator-names");
5702fe6060f1SDimitry Andric 
57030b57cec5SDimitry Andric   // Forward -f (flag) options which we can pass directly.
57040b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
57050b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
57060b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs);
57070b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_femulated_tls,
57080b57cec5SDimitry Andric                   options::OPT_fno_emulated_tls);
57090b57cec5SDimitry Andric 
57100b57cec5SDimitry Andric   // AltiVec-like language extensions aren't relevant for assembling.
57110b57cec5SDimitry Andric   if (!isa<PreprocessJobAction>(JA) || Output.getType() != types::TY_PP_Asm)
57120b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fzvector);
57130b57cec5SDimitry Andric 
57140b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
57150b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
57160b57cec5SDimitry Andric 
57170b57cec5SDimitry Andric   // Forward flags for OpenMP. We don't do this if the current action is an
57180b57cec5SDimitry Andric   // device offloading action other than OpenMP.
57190b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
57200b57cec5SDimitry Andric                    options::OPT_fno_openmp, false) &&
57210b57cec5SDimitry Andric       (JA.isDeviceOffloading(Action::OFK_None) ||
57220b57cec5SDimitry Andric        JA.isDeviceOffloading(Action::OFK_OpenMP))) {
57230b57cec5SDimitry Andric     switch (D.getOpenMPRuntime(Args)) {
57240b57cec5SDimitry Andric     case Driver::OMPRT_OMP:
57250b57cec5SDimitry Andric     case Driver::OMPRT_IOMP5:
57260b57cec5SDimitry Andric       // Clang can generate useful OpenMP code for these two runtime libraries.
57270b57cec5SDimitry Andric       CmdArgs.push_back("-fopenmp");
57280b57cec5SDimitry Andric 
57290b57cec5SDimitry Andric       // If no option regarding the use of TLS in OpenMP codegeneration is
57300b57cec5SDimitry Andric       // given, decide a default based on the target. Otherwise rely on the
57310b57cec5SDimitry Andric       // options and pass the right information to the frontend.
57320b57cec5SDimitry Andric       if (!Args.hasFlag(options::OPT_fopenmp_use_tls,
57330b57cec5SDimitry Andric                         options::OPT_fnoopenmp_use_tls, /*Default=*/true))
57340b57cec5SDimitry Andric         CmdArgs.push_back("-fnoopenmp-use-tls");
57350b57cec5SDimitry Andric       Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd,
57360b57cec5SDimitry Andric                       options::OPT_fno_openmp_simd);
5737480093f4SDimitry Andric       Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_enable_irbuilder);
57380b57cec5SDimitry Andric       Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ);
57390b57cec5SDimitry Andric       Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_number_of_sm_EQ);
57400b57cec5SDimitry Andric       Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_blocks_per_sm_EQ);
57410b57cec5SDimitry Andric       Args.AddAllArgs(CmdArgs,
57420b57cec5SDimitry Andric                       options::OPT_fopenmp_cuda_teams_reduction_recs_num_EQ);
57430b57cec5SDimitry Andric       if (Args.hasFlag(options::OPT_fopenmp_optimistic_collapse,
57440b57cec5SDimitry Andric                        options::OPT_fno_openmp_optimistic_collapse,
57450b57cec5SDimitry Andric                        /*Default=*/false))
57460b57cec5SDimitry Andric         CmdArgs.push_back("-fopenmp-optimistic-collapse");
57470b57cec5SDimitry Andric 
57480b57cec5SDimitry Andric       // When in OpenMP offloading mode with NVPTX target, forward
57490b57cec5SDimitry Andric       // cuda-mode flag
57500b57cec5SDimitry Andric       if (Args.hasFlag(options::OPT_fopenmp_cuda_mode,
57510b57cec5SDimitry Andric                        options::OPT_fno_openmp_cuda_mode, /*Default=*/false))
57520b57cec5SDimitry Andric         CmdArgs.push_back("-fopenmp-cuda-mode");
57530b57cec5SDimitry Andric 
57540b57cec5SDimitry Andric       // When in OpenMP offloading mode with NVPTX target, check if full runtime
57550b57cec5SDimitry Andric       // is required.
57560b57cec5SDimitry Andric       if (Args.hasFlag(options::OPT_fopenmp_cuda_force_full_runtime,
57570b57cec5SDimitry Andric                        options::OPT_fno_openmp_cuda_force_full_runtime,
57580b57cec5SDimitry Andric                        /*Default=*/false))
57590b57cec5SDimitry Andric         CmdArgs.push_back("-fopenmp-cuda-force-full-runtime");
57600b57cec5SDimitry Andric       break;
57610b57cec5SDimitry Andric     default:
57620b57cec5SDimitry Andric       // By default, if Clang doesn't know how to generate useful OpenMP code
57630b57cec5SDimitry Andric       // for a specific runtime library, we just don't pass the '-fopenmp' flag
57640b57cec5SDimitry Andric       // down to the actual compilation.
57650b57cec5SDimitry Andric       // FIXME: It would be better to have a mode which *only* omits IR
57660b57cec5SDimitry Andric       // generation based on the OpenMP support so that we get consistent
57670b57cec5SDimitry Andric       // semantic analysis, etc.
57680b57cec5SDimitry Andric       break;
57690b57cec5SDimitry Andric     }
57700b57cec5SDimitry Andric   } else {
57710b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd,
57720b57cec5SDimitry Andric                     options::OPT_fno_openmp_simd);
57730b57cec5SDimitry Andric     Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ);
57740b57cec5SDimitry Andric   }
57750b57cec5SDimitry Andric 
57760b57cec5SDimitry Andric   const SanitizerArgs &Sanitize = TC.getSanitizerArgs();
57770b57cec5SDimitry Andric   Sanitize.addArgs(TC, Args, CmdArgs, InputType);
57780b57cec5SDimitry Andric 
57790b57cec5SDimitry Andric   const XRayArgs &XRay = TC.getXRayArgs();
57800b57cec5SDimitry Andric   XRay.addArgs(TC, Args, CmdArgs, InputType);
57810b57cec5SDimitry Andric 
5782e8d8bef9SDimitry Andric   for (const auto &Filename :
5783e8d8bef9SDimitry Andric        Args.getAllArgValues(options::OPT_fprofile_list_EQ)) {
5784e8d8bef9SDimitry Andric     if (D.getVFS().exists(Filename))
5785e8d8bef9SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-fprofile-list=" + Filename));
5786e8d8bef9SDimitry Andric     else
5787e8d8bef9SDimitry Andric       D.Diag(clang::diag::err_drv_no_such_file) << Filename;
5788e8d8bef9SDimitry Andric   }
5789e8d8bef9SDimitry Andric 
5790480093f4SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ)) {
5791480093f4SDimitry Andric     StringRef S0 = A->getValue(), S = S0;
579255e4f9d5SDimitry Andric     unsigned Size, Offset = 0;
5793fe6060f1SDimitry Andric     if (!Triple.isAArch64() && !Triple.isRISCV() && !Triple.isX86())
5794480093f4SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
5795480093f4SDimitry Andric           << A->getAsString(Args) << TripleStr;
5796480093f4SDimitry Andric     else if (S.consumeInteger(10, Size) ||
5797480093f4SDimitry Andric              (!S.empty() && (!S.consume_front(",") ||
579855e4f9d5SDimitry Andric                              S.consumeInteger(10, Offset) || !S.empty())))
5799480093f4SDimitry Andric       D.Diag(diag::err_drv_invalid_argument_to_option)
5800480093f4SDimitry Andric           << S0 << A->getOption().getName();
580155e4f9d5SDimitry Andric     else if (Size < Offset)
5802480093f4SDimitry Andric       D.Diag(diag::err_drv_unsupported_fpatchable_function_entry_argument);
580355e4f9d5SDimitry Andric     else {
5804480093f4SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(A->getSpelling() + Twine(Size)));
580555e4f9d5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(
580655e4f9d5SDimitry Andric           "-fpatchable-function-entry-offset=" + Twine(Offset)));
580755e4f9d5SDimitry Andric     }
5808480093f4SDimitry Andric   }
5809480093f4SDimitry Andric 
5810480093f4SDimitry Andric   if (TC.SupportsProfiling()) {
58110b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_pg);
58120b57cec5SDimitry Andric 
5813480093f4SDimitry Andric     llvm::Triple::ArchType Arch = TC.getArch();
5814480093f4SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_mfentry)) {
5815480093f4SDimitry Andric       if (Arch == llvm::Triple::systemz || TC.getTriple().isX86())
5816480093f4SDimitry Andric         A->render(Args, CmdArgs);
5817480093f4SDimitry Andric       else
5818480093f4SDimitry Andric         D.Diag(diag::err_drv_unsupported_opt_for_target)
5819480093f4SDimitry Andric             << A->getAsString(Args) << TripleStr;
5820480093f4SDimitry Andric     }
5821480093f4SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_mnop_mcount)) {
5822480093f4SDimitry Andric       if (Arch == llvm::Triple::systemz)
5823480093f4SDimitry Andric         A->render(Args, CmdArgs);
5824480093f4SDimitry Andric       else
5825480093f4SDimitry Andric         D.Diag(diag::err_drv_unsupported_opt_for_target)
5826480093f4SDimitry Andric             << A->getAsString(Args) << TripleStr;
5827480093f4SDimitry Andric     }
5828480093f4SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_mrecord_mcount)) {
5829480093f4SDimitry Andric       if (Arch == llvm::Triple::systemz)
5830480093f4SDimitry Andric         A->render(Args, CmdArgs);
5831480093f4SDimitry Andric       else
5832480093f4SDimitry Andric         D.Diag(diag::err_drv_unsupported_opt_for_target)
5833480093f4SDimitry Andric             << A->getAsString(Args) << TripleStr;
5834480093f4SDimitry Andric     }
5835480093f4SDimitry Andric   }
58360b57cec5SDimitry Andric 
58370b57cec5SDimitry Andric   if (Args.getLastArg(options::OPT_fapple_kext) ||
58380b57cec5SDimitry Andric       (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType)))
58390b57cec5SDimitry Andric     CmdArgs.push_back("-fapple-kext");
58400b57cec5SDimitry Andric 
5841fe6060f1SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_altivec_src_compat);
5842a7dea167SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_flax_vector_conversions_EQ);
58430b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
58440b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
58450b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
58460b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
5847e8d8bef9SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftime_report_EQ);
58480b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftime_trace);
5849a7dea167SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_granularity_EQ);
58500b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
58510b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_malign_double);
5852480093f4SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fno_temp_file);
58530b57cec5SDimitry Andric 
58540b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
58550b57cec5SDimitry Andric     CmdArgs.push_back("-ftrapv-handler");
58560b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
58570b57cec5SDimitry Andric   }
58580b57cec5SDimitry Andric 
58590b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
58600b57cec5SDimitry Andric 
58610b57cec5SDimitry Andric   // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
58620b57cec5SDimitry Andric   // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
58630b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fwrapv, options::OPT_fno_wrapv)) {
58640b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fwrapv))
58650b57cec5SDimitry Andric       CmdArgs.push_back("-fwrapv");
58660b57cec5SDimitry Andric   } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
58670b57cec5SDimitry Andric                                       options::OPT_fno_strict_overflow)) {
58680b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fno_strict_overflow))
58690b57cec5SDimitry Andric       CmdArgs.push_back("-fwrapv");
58700b57cec5SDimitry Andric   }
58710b57cec5SDimitry Andric 
58720b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_freroll_loops,
58730b57cec5SDimitry Andric                                options::OPT_fno_reroll_loops))
58740b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_freroll_loops))
58750b57cec5SDimitry Andric       CmdArgs.push_back("-freroll-loops");
58760b57cec5SDimitry Andric 
5877d409305fSDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_ffinite_loops,
5878d409305fSDimitry Andric                   options::OPT_fno_finite_loops);
5879d409305fSDimitry Andric 
58800b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
58810b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_funroll_loops,
58820b57cec5SDimitry Andric                   options::OPT_fno_unroll_loops);
58830b57cec5SDimitry Andric 
58840b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_pthread);
58850b57cec5SDimitry Andric 
58865ffd83dbSDimitry Andric   if (Args.hasFlag(options::OPT_mspeculative_load_hardening,
58875ffd83dbSDimitry Andric                    options::OPT_mno_speculative_load_hardening, false))
58880b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-mspeculative-load-hardening"));
58890b57cec5SDimitry Andric 
5890e8d8bef9SDimitry Andric   RenderSSPOptions(D, TC, Args, CmdArgs, KernelOrKext);
58915ffd83dbSDimitry Andric   RenderSCPOptions(TC, Args, CmdArgs);
58920b57cec5SDimitry Andric   RenderTrivialAutoVarInitOptions(D, TC, Args, CmdArgs);
58930b57cec5SDimitry Andric 
58940b57cec5SDimitry Andric   // Translate -mstackrealign
58950b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
58960b57cec5SDimitry Andric                    false))
58970b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
58980b57cec5SDimitry Andric 
58990b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_mstack_alignment)) {
59000b57cec5SDimitry Andric     StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
59010b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
59020b57cec5SDimitry Andric   }
59030b57cec5SDimitry Andric 
59040b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_mstack_probe_size)) {
59050b57cec5SDimitry Andric     StringRef Size = Args.getLastArgValue(options::OPT_mstack_probe_size);
59060b57cec5SDimitry Andric 
59070b57cec5SDimitry Andric     if (!Size.empty())
59080b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-mstack-probe-size=" + Size));
59090b57cec5SDimitry Andric     else
59100b57cec5SDimitry Andric       CmdArgs.push_back("-mstack-probe-size=0");
59110b57cec5SDimitry Andric   }
59120b57cec5SDimitry Andric 
59130b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_mstack_arg_probe,
59140b57cec5SDimitry Andric                     options::OPT_mno_stack_arg_probe, true))
59150b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-mno-stack-arg-probe"));
59160b57cec5SDimitry Andric 
59170b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it,
59180b57cec5SDimitry Andric                                options::OPT_mno_restrict_it)) {
59190b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_mrestrict_it)) {
59200b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
59210b57cec5SDimitry Andric       CmdArgs.push_back("-arm-restrict-it");
59220b57cec5SDimitry Andric     } else {
59230b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
59240b57cec5SDimitry Andric       CmdArgs.push_back("-arm-no-restrict-it");
59250b57cec5SDimitry Andric     }
59260b57cec5SDimitry Andric   } else if (Triple.isOSWindows() &&
59270b57cec5SDimitry Andric              (Triple.getArch() == llvm::Triple::arm ||
59280b57cec5SDimitry Andric               Triple.getArch() == llvm::Triple::thumb)) {
59290b57cec5SDimitry Andric     // Windows on ARM expects restricted IT blocks
59300b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
59310b57cec5SDimitry Andric     CmdArgs.push_back("-arm-restrict-it");
59320b57cec5SDimitry Andric   }
59330b57cec5SDimitry Andric 
59340b57cec5SDimitry Andric   // Forward -cl options to -cc1
5935fe6060f1SDimitry Andric   RenderOpenCLOptions(Args, CmdArgs, InputType);
59360b57cec5SDimitry Andric 
5937e8d8bef9SDimitry Andric   if (IsHIP) {
5938e8d8bef9SDimitry Andric     if (Args.hasFlag(options::OPT_fhip_new_launch_api,
59395ffd83dbSDimitry Andric                      options::OPT_fno_hip_new_launch_api, true))
5940a7dea167SDimitry Andric       CmdArgs.push_back("-fhip-new-launch-api");
5941e8d8bef9SDimitry Andric     if (Args.hasFlag(options::OPT_fgpu_allow_device_init,
5942e8d8bef9SDimitry Andric                      options::OPT_fno_gpu_allow_device_init, false))
5943e8d8bef9SDimitry Andric       CmdArgs.push_back("-fgpu-allow-device-init");
5944e8d8bef9SDimitry Andric   }
5945e8d8bef9SDimitry Andric 
5946e8d8bef9SDimitry Andric   if (IsCuda || IsHIP) {
5947fe6060f1SDimitry Andric     if (Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false))
5948fe6060f1SDimitry Andric       CmdArgs.push_back("-fgpu-rdc");
5949e8d8bef9SDimitry Andric     if (Args.hasFlag(options::OPT_fgpu_defer_diag,
5950e8d8bef9SDimitry Andric                      options::OPT_fno_gpu_defer_diag, false))
5951e8d8bef9SDimitry Andric       CmdArgs.push_back("-fgpu-defer-diag");
5952e8d8bef9SDimitry Andric     if (Args.hasFlag(options::OPT_fgpu_exclude_wrong_side_overloads,
5953e8d8bef9SDimitry Andric                      options::OPT_fno_gpu_exclude_wrong_side_overloads,
5954e8d8bef9SDimitry Andric                      false)) {
5955e8d8bef9SDimitry Andric       CmdArgs.push_back("-fgpu-exclude-wrong-side-overloads");
5956e8d8bef9SDimitry Andric       CmdArgs.push_back("-fgpu-defer-diag");
5957e8d8bef9SDimitry Andric     }
5958e8d8bef9SDimitry Andric   }
5959a7dea167SDimitry Andric 
59600b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) {
59610b57cec5SDimitry Andric     CmdArgs.push_back(
59620b57cec5SDimitry Andric         Args.MakeArgString(Twine("-fcf-protection=") + A->getValue()));
59630b57cec5SDimitry Andric   }
59640b57cec5SDimitry Andric 
5965fe6060f1SDimitry Andric   // Forward -f options with positive and negative forms; we translate these by
5966fe6060f1SDimitry Andric   // hand.  Do not propagate PGO options to the GPU-side compilations as the
5967fe6060f1SDimitry Andric   // profile info is for the host-side compilation only.
5968fe6060f1SDimitry Andric   if (!(IsCudaDevice || IsHIPDevice)) {
59690b57cec5SDimitry Andric     if (Arg *A = getLastProfileSampleUseArg(Args)) {
59700b57cec5SDimitry Andric       auto *PGOArg = Args.getLastArg(
59710b57cec5SDimitry Andric           options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ,
5972fe6060f1SDimitry Andric           options::OPT_fcs_profile_generate,
5973fe6060f1SDimitry Andric           options::OPT_fcs_profile_generate_EQ, options::OPT_fprofile_use,
5974fe6060f1SDimitry Andric           options::OPT_fprofile_use_EQ);
59750b57cec5SDimitry Andric       if (PGOArg)
59760b57cec5SDimitry Andric         D.Diag(diag::err_drv_argument_not_allowed_with)
59770b57cec5SDimitry Andric             << "SampleUse with PGO options";
59780b57cec5SDimitry Andric 
59790b57cec5SDimitry Andric       StringRef fname = A->getValue();
59800b57cec5SDimitry Andric       if (!llvm::sys::fs::exists(fname))
59810b57cec5SDimitry Andric         D.Diag(diag::err_drv_no_such_file) << fname;
59820b57cec5SDimitry Andric       else
59830b57cec5SDimitry Andric         A->render(Args, CmdArgs);
59840b57cec5SDimitry Andric     }
59850b57cec5SDimitry Andric     Args.AddLastArg(CmdArgs, options::OPT_fprofile_remapping_file_EQ);
59860b57cec5SDimitry Andric 
5987e8d8bef9SDimitry Andric     if (Args.hasFlag(options::OPT_fpseudo_probe_for_profiling,
5988fe6060f1SDimitry Andric                      options::OPT_fno_pseudo_probe_for_profiling, false)) {
5989e8d8bef9SDimitry Andric       CmdArgs.push_back("-fpseudo-probe-for-profiling");
5990fe6060f1SDimitry Andric       // Enforce -funique-internal-linkage-names if it's not explicitly turned
5991fe6060f1SDimitry Andric       // off.
5992fe6060f1SDimitry Andric       if (Args.hasFlag(options::OPT_funique_internal_linkage_names,
5993fe6060f1SDimitry Andric                        options::OPT_fno_unique_internal_linkage_names, true))
5994fe6060f1SDimitry Andric         CmdArgs.push_back("-funique-internal-linkage-names");
5995fe6060f1SDimitry Andric     }
5996fe6060f1SDimitry Andric   }
59970b57cec5SDimitry Andric   RenderBuiltinOptions(TC, RawTriple, Args, CmdArgs);
59980b57cec5SDimitry Andric 
59990b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
60000b57cec5SDimitry Andric                     options::OPT_fno_assume_sane_operator_new))
60010b57cec5SDimitry Andric     CmdArgs.push_back("-fno-assume-sane-operator-new");
60020b57cec5SDimitry Andric 
60030b57cec5SDimitry Andric   // -fblocks=0 is default.
60040b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
60050b57cec5SDimitry Andric                    TC.IsBlocksDefault()) ||
60060b57cec5SDimitry Andric       (Args.hasArg(options::OPT_fgnu_runtime) &&
60070b57cec5SDimitry Andric        Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
60080b57cec5SDimitry Andric        !Args.hasArg(options::OPT_fno_blocks))) {
60090b57cec5SDimitry Andric     CmdArgs.push_back("-fblocks");
60100b57cec5SDimitry Andric 
60110b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_fgnu_runtime) && !TC.hasBlocksRuntime())
60120b57cec5SDimitry Andric       CmdArgs.push_back("-fblocks-runtime-optional");
60130b57cec5SDimitry Andric   }
60140b57cec5SDimitry Andric 
60150b57cec5SDimitry Andric   // -fencode-extended-block-signature=1 is default.
60160b57cec5SDimitry Andric   if (TC.IsEncodeExtendedBlockSignatureDefault())
60170b57cec5SDimitry Andric     CmdArgs.push_back("-fencode-extended-block-signature");
60180b57cec5SDimitry Andric 
60190b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fcoroutines_ts, options::OPT_fno_coroutines_ts,
60200b57cec5SDimitry Andric                    false) &&
60210b57cec5SDimitry Andric       types::isCXX(InputType)) {
60220b57cec5SDimitry Andric     CmdArgs.push_back("-fcoroutines-ts");
60230b57cec5SDimitry Andric   }
60240b57cec5SDimitry Andric 
60250b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fdouble_square_bracket_attributes,
60260b57cec5SDimitry Andric                   options::OPT_fno_double_square_bracket_attributes);
60270b57cec5SDimitry Andric 
60280b57cec5SDimitry Andric   // -faccess-control is default.
60290b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fno_access_control,
60300b57cec5SDimitry Andric                    options::OPT_faccess_control, false))
60310b57cec5SDimitry Andric     CmdArgs.push_back("-fno-access-control");
60320b57cec5SDimitry Andric 
60330b57cec5SDimitry Andric   // -felide-constructors is the default.
60340b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fno_elide_constructors,
60350b57cec5SDimitry Andric                    options::OPT_felide_constructors, false))
60360b57cec5SDimitry Andric     CmdArgs.push_back("-fno-elide-constructors");
60370b57cec5SDimitry Andric 
60380b57cec5SDimitry Andric   ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
60390b57cec5SDimitry Andric 
60400b57cec5SDimitry Andric   if (KernelOrKext || (types::isCXX(InputType) &&
60410b57cec5SDimitry Andric                        (RTTIMode == ToolChain::RM_Disabled)))
60420b57cec5SDimitry Andric     CmdArgs.push_back("-fno-rtti");
60430b57cec5SDimitry Andric 
6044e8d8bef9SDimitry Andric   // -fshort-enums=0 is default for all architectures except Hexagon and z/OS.
60450b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fshort_enums, options::OPT_fno_short_enums,
6046e8d8bef9SDimitry Andric                    TC.getArch() == llvm::Triple::hexagon || Triple.isOSzOS()))
60470b57cec5SDimitry Andric     CmdArgs.push_back("-fshort-enums");
60480b57cec5SDimitry Andric 
60490b57cec5SDimitry Andric   RenderCharacterOptions(Args, AuxTriple ? *AuxTriple : RawTriple, CmdArgs);
60500b57cec5SDimitry Andric 
60510b57cec5SDimitry Andric   // -fuse-cxa-atexit is default.
60520b57cec5SDimitry Andric   if (!Args.hasFlag(
60530b57cec5SDimitry Andric           options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
60545ffd83dbSDimitry Andric           !RawTriple.isOSAIX() && !RawTriple.isOSWindows() &&
60550b57cec5SDimitry Andric               TC.getArch() != llvm::Triple::xcore &&
60560b57cec5SDimitry Andric               ((RawTriple.getVendor() != llvm::Triple::MipsTechnologies) ||
60570b57cec5SDimitry Andric                RawTriple.hasEnvironment())) ||
60580b57cec5SDimitry Andric       KernelOrKext)
60590b57cec5SDimitry Andric     CmdArgs.push_back("-fno-use-cxa-atexit");
60600b57cec5SDimitry Andric 
60610b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fregister_global_dtors_with_atexit,
60620b57cec5SDimitry Andric                    options::OPT_fno_register_global_dtors_with_atexit,
60630b57cec5SDimitry Andric                    RawTriple.isOSDarwin() && !KernelOrKext))
60640b57cec5SDimitry Andric     CmdArgs.push_back("-fregister-global-dtors-with-atexit");
60650b57cec5SDimitry Andric 
60660b57cec5SDimitry Andric   // -fno-use-line-directives is default.
60670b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fuse_line_directives,
60680b57cec5SDimitry Andric                    options::OPT_fno_use_line_directives, false))
60690b57cec5SDimitry Andric     CmdArgs.push_back("-fuse-line-directives");
60700b57cec5SDimitry Andric 
6071fe6060f1SDimitry Andric   // -fno-minimize-whitespace is default.
6072fe6060f1SDimitry Andric   if (Args.hasFlag(options::OPT_fminimize_whitespace,
6073fe6060f1SDimitry Andric                    options::OPT_fno_minimize_whitespace, false)) {
6074fe6060f1SDimitry Andric     types::ID InputType = Inputs[0].getType();
6075fe6060f1SDimitry Andric     if (!isDerivedFromC(InputType))
6076fe6060f1SDimitry Andric       D.Diag(diag::err_drv_minws_unsupported_input_type)
6077fe6060f1SDimitry Andric           << types::getTypeName(InputType);
6078fe6060f1SDimitry Andric     CmdArgs.push_back("-fminimize-whitespace");
6079fe6060f1SDimitry Andric   }
6080fe6060f1SDimitry Andric 
60815ffd83dbSDimitry Andric   // -fms-extensions=0 is default.
60825ffd83dbSDimitry Andric   if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
60835ffd83dbSDimitry Andric                    IsWindowsMSVC))
60845ffd83dbSDimitry Andric     CmdArgs.push_back("-fms-extensions");
60855ffd83dbSDimitry Andric 
60860b57cec5SDimitry Andric   // -fms-compatibility=0 is default.
6087a7dea167SDimitry Andric   bool IsMSVCCompat = Args.hasFlag(
6088a7dea167SDimitry Andric       options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility,
6089a7dea167SDimitry Andric       (IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions,
6090a7dea167SDimitry Andric                                      options::OPT_fno_ms_extensions, true)));
6091a7dea167SDimitry Andric   if (IsMSVCCompat)
60920b57cec5SDimitry Andric     CmdArgs.push_back("-fms-compatibility");
60930b57cec5SDimitry Andric 
6094a7dea167SDimitry Andric   // Handle -fgcc-version, if present.
6095a7dea167SDimitry Andric   VersionTuple GNUCVer;
6096a7dea167SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fgnuc_version_EQ)) {
6097a7dea167SDimitry Andric     // Check that the version has 1 to 3 components and the minor and patch
6098a7dea167SDimitry Andric     // versions fit in two decimal digits.
6099a7dea167SDimitry Andric     StringRef Val = A->getValue();
6100a7dea167SDimitry Andric     Val = Val.empty() ? "0" : Val; // Treat "" as 0 or disable.
6101a7dea167SDimitry Andric     bool Invalid = GNUCVer.tryParse(Val);
6102a7dea167SDimitry Andric     unsigned Minor = GNUCVer.getMinor().getValueOr(0);
6103a7dea167SDimitry Andric     unsigned Patch = GNUCVer.getSubminor().getValueOr(0);
6104a7dea167SDimitry Andric     if (Invalid || GNUCVer.getBuild() || Minor >= 100 || Patch >= 100) {
6105a7dea167SDimitry Andric       D.Diag(diag::err_drv_invalid_value)
6106a7dea167SDimitry Andric           << A->getAsString(Args) << A->getValue();
6107a7dea167SDimitry Andric     }
6108a7dea167SDimitry Andric   } else if (!IsMSVCCompat) {
6109a7dea167SDimitry Andric     // Imitate GCC 4.2.1 by default if -fms-compatibility is not in effect.
6110a7dea167SDimitry Andric     GNUCVer = VersionTuple(4, 2, 1);
6111a7dea167SDimitry Andric   }
6112a7dea167SDimitry Andric   if (!GNUCVer.empty()) {
6113a7dea167SDimitry Andric     CmdArgs.push_back(
6114a7dea167SDimitry Andric         Args.MakeArgString("-fgnuc-version=" + GNUCVer.getAsString()));
6115a7dea167SDimitry Andric   }
6116a7dea167SDimitry Andric 
61170b57cec5SDimitry Andric   VersionTuple MSVT = TC.computeMSVCVersion(&D, Args);
61180b57cec5SDimitry Andric   if (!MSVT.empty())
61190b57cec5SDimitry Andric     CmdArgs.push_back(
61200b57cec5SDimitry Andric         Args.MakeArgString("-fms-compatibility-version=" + MSVT.getAsString()));
61210b57cec5SDimitry Andric 
61220b57cec5SDimitry Andric   bool IsMSVC2015Compatible = MSVT.getMajor() >= 19;
6123fe6060f1SDimitry Andric   if (ImplyVCPPCVer) {
6124fe6060f1SDimitry Andric     StringRef LanguageStandard;
6125fe6060f1SDimitry Andric     if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) {
6126fe6060f1SDimitry Andric       Std = StdArg;
6127fe6060f1SDimitry Andric       LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue())
6128fe6060f1SDimitry Andric                              .Case("c11", "-std=c11")
6129fe6060f1SDimitry Andric                              .Case("c17", "-std=c17")
6130fe6060f1SDimitry Andric                              .Default("");
6131fe6060f1SDimitry Andric       if (LanguageStandard.empty())
6132fe6060f1SDimitry Andric         D.Diag(clang::diag::warn_drv_unused_argument)
6133fe6060f1SDimitry Andric             << StdArg->getAsString(Args);
6134fe6060f1SDimitry Andric     }
6135fe6060f1SDimitry Andric     CmdArgs.push_back(LanguageStandard.data());
6136fe6060f1SDimitry Andric   }
61370b57cec5SDimitry Andric   if (ImplyVCPPCXXVer) {
61380b57cec5SDimitry Andric     StringRef LanguageStandard;
61390b57cec5SDimitry Andric     if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) {
61400b57cec5SDimitry Andric       Std = StdArg;
61410b57cec5SDimitry Andric       LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue())
61420b57cec5SDimitry Andric                              .Case("c++14", "-std=c++14")
61430b57cec5SDimitry Andric                              .Case("c++17", "-std=c++17")
6144fe6060f1SDimitry Andric                              .Case("c++20", "-std=c++20")
6145fe6060f1SDimitry Andric                              .Case("c++latest", "-std=c++2b")
61460b57cec5SDimitry Andric                              .Default("");
61470b57cec5SDimitry Andric       if (LanguageStandard.empty())
61480b57cec5SDimitry Andric         D.Diag(clang::diag::warn_drv_unused_argument)
61490b57cec5SDimitry Andric             << StdArg->getAsString(Args);
61500b57cec5SDimitry Andric     }
61510b57cec5SDimitry Andric 
61520b57cec5SDimitry Andric     if (LanguageStandard.empty()) {
61530b57cec5SDimitry Andric       if (IsMSVC2015Compatible)
61540b57cec5SDimitry Andric         LanguageStandard = "-std=c++14";
61550b57cec5SDimitry Andric       else
61560b57cec5SDimitry Andric         LanguageStandard = "-std=c++11";
61570b57cec5SDimitry Andric     }
61580b57cec5SDimitry Andric 
61590b57cec5SDimitry Andric     CmdArgs.push_back(LanguageStandard.data());
61600b57cec5SDimitry Andric   }
61610b57cec5SDimitry Andric 
61620b57cec5SDimitry Andric   // -fno-borland-extensions is default.
61630b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fborland_extensions,
61640b57cec5SDimitry Andric                    options::OPT_fno_borland_extensions, false))
61650b57cec5SDimitry Andric     CmdArgs.push_back("-fborland-extensions");
61660b57cec5SDimitry Andric 
61670b57cec5SDimitry Andric   // -fno-declspec is default, except for PS4.
61680b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec,
61690b57cec5SDimitry Andric                    RawTriple.isPS4()))
61700b57cec5SDimitry Andric     CmdArgs.push_back("-fdeclspec");
61710b57cec5SDimitry Andric   else if (Args.hasArg(options::OPT_fno_declspec))
61720b57cec5SDimitry Andric     CmdArgs.push_back("-fno-declspec"); // Explicitly disabling __declspec.
61730b57cec5SDimitry Andric 
61740b57cec5SDimitry Andric   // -fthreadsafe-static is default, except for MSVC compatibility versions less
61750b57cec5SDimitry Andric   // than 19.
61760b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
61770b57cec5SDimitry Andric                     options::OPT_fno_threadsafe_statics,
61780b57cec5SDimitry Andric                     !IsWindowsMSVC || IsMSVC2015Compatible))
61790b57cec5SDimitry Andric     CmdArgs.push_back("-fno-threadsafe-statics");
61800b57cec5SDimitry Andric 
61810b57cec5SDimitry Andric   // -fno-delayed-template-parsing is default, except when targeting MSVC.
61820b57cec5SDimitry Andric   // Many old Windows SDK versions require this to parse.
61830b57cec5SDimitry Andric   // FIXME: MSVC introduced /Zc:twoPhase- to disable this behavior in their
61840b57cec5SDimitry Andric   // compiler. We should be able to disable this by default at some point.
61850b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
61860b57cec5SDimitry Andric                    options::OPT_fno_delayed_template_parsing, IsWindowsMSVC))
61870b57cec5SDimitry Andric     CmdArgs.push_back("-fdelayed-template-parsing");
61880b57cec5SDimitry Andric 
61890b57cec5SDimitry Andric   // -fgnu-keywords default varies depending on language; only pass if
61900b57cec5SDimitry Andric   // specified.
61910b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fgnu_keywords,
61920b57cec5SDimitry Andric                   options::OPT_fno_gnu_keywords);
61930b57cec5SDimitry Andric 
61940b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fgnu89_inline, options::OPT_fno_gnu89_inline,
61950b57cec5SDimitry Andric                    false))
61960b57cec5SDimitry Andric     CmdArgs.push_back("-fgnu89-inline");
61970b57cec5SDimitry Andric 
61980b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fno_inline))
61990b57cec5SDimitry Andric     CmdArgs.push_back("-fno-inline");
62000b57cec5SDimitry Andric 
62010b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_finline_functions,
62020b57cec5SDimitry Andric                   options::OPT_finline_hint_functions,
62030b57cec5SDimitry Andric                   options::OPT_fno_inline_functions);
62040b57cec5SDimitry Andric 
62050b57cec5SDimitry Andric   // FIXME: Find a better way to determine whether the language has modules
62060b57cec5SDimitry Andric   // support by default, or just assume that all languages do.
62070b57cec5SDimitry Andric   bool HaveModules =
6208e8d8bef9SDimitry Andric       Std && (Std->containsValue("c++2a") || Std->containsValue("c++20") ||
6209e8d8bef9SDimitry Andric               Std->containsValue("c++latest"));
62100b57cec5SDimitry Andric   RenderModulesOptions(C, D, Args, Input, Output, CmdArgs, HaveModules);
62110b57cec5SDimitry Andric 
6212a7dea167SDimitry Andric   if (Args.hasFlag(options::OPT_fpch_validate_input_files_content,
6213a7dea167SDimitry Andric                    options::OPT_fno_pch_validate_input_files_content, false))
6214a7dea167SDimitry Andric     CmdArgs.push_back("-fvalidate-ast-input-files-content");
62155ffd83dbSDimitry Andric   if (Args.hasFlag(options::OPT_fpch_instantiate_templates,
62165ffd83dbSDimitry Andric                    options::OPT_fno_pch_instantiate_templates, false))
62175ffd83dbSDimitry Andric     CmdArgs.push_back("-fpch-instantiate-templates");
6218590d96feSDimitry Andric   if (Args.hasFlag(options::OPT_fpch_codegen, options::OPT_fno_pch_codegen,
6219590d96feSDimitry Andric                    false))
6220590d96feSDimitry Andric     CmdArgs.push_back("-fmodules-codegen");
6221590d96feSDimitry Andric   if (Args.hasFlag(options::OPT_fpch_debuginfo, options::OPT_fno_pch_debuginfo,
6222590d96feSDimitry Andric                    false))
6223590d96feSDimitry Andric     CmdArgs.push_back("-fmodules-debuginfo");
6224a7dea167SDimitry Andric 
6225e8d8bef9SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_flegacy_pass_manager,
6226e8d8bef9SDimitry Andric                   options::OPT_fno_legacy_pass_manager);
62270b57cec5SDimitry Andric 
62285ffd83dbSDimitry Andric   ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, Inputs, CmdArgs, rewriteKind);
62290b57cec5SDimitry Andric   RenderObjCOptions(TC, D, RawTriple, Args, Runtime, rewriteKind != RK_None,
62300b57cec5SDimitry Andric                     Input, CmdArgs);
62310b57cec5SDimitry Andric 
6232fe6060f1SDimitry Andric   if (types::isObjC(Input.getType()) &&
6233fe6060f1SDimitry Andric       Args.hasFlag(options::OPT_fobjc_encode_cxx_class_template_spec,
6234fe6060f1SDimitry Andric                    options::OPT_fno_objc_encode_cxx_class_template_spec,
6235fe6060f1SDimitry Andric                    !Runtime.isNeXTFamily()))
6236fe6060f1SDimitry Andric     CmdArgs.push_back("-fobjc-encode-cxx-class-template-spec");
6237fe6060f1SDimitry Andric 
62380b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fapplication_extension,
62390b57cec5SDimitry Andric                    options::OPT_fno_application_extension, false))
62400b57cec5SDimitry Andric     CmdArgs.push_back("-fapplication-extension");
62410b57cec5SDimitry Andric 
62420b57cec5SDimitry Andric   // Handle GCC-style exception args.
6243fe6060f1SDimitry Andric   bool EH = false;
62440b57cec5SDimitry Andric   if (!C.getDriver().IsCLMode())
6245fe6060f1SDimitry Andric     EH = addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs);
62460b57cec5SDimitry Andric 
62470b57cec5SDimitry Andric   // Handle exception personalities
6248a7dea167SDimitry Andric   Arg *A = Args.getLastArg(
6249a7dea167SDimitry Andric       options::OPT_fsjlj_exceptions, options::OPT_fseh_exceptions,
6250a7dea167SDimitry Andric       options::OPT_fdwarf_exceptions, options::OPT_fwasm_exceptions);
62510b57cec5SDimitry Andric   if (A) {
62520b57cec5SDimitry Andric     const Option &Opt = A->getOption();
62530b57cec5SDimitry Andric     if (Opt.matches(options::OPT_fsjlj_exceptions))
6254e8d8bef9SDimitry Andric       CmdArgs.push_back("-exception-model=sjlj");
62550b57cec5SDimitry Andric     if (Opt.matches(options::OPT_fseh_exceptions))
6256e8d8bef9SDimitry Andric       CmdArgs.push_back("-exception-model=seh");
62570b57cec5SDimitry Andric     if (Opt.matches(options::OPT_fdwarf_exceptions))
6258e8d8bef9SDimitry Andric       CmdArgs.push_back("-exception-model=dwarf");
6259a7dea167SDimitry Andric     if (Opt.matches(options::OPT_fwasm_exceptions))
6260e8d8bef9SDimitry Andric       CmdArgs.push_back("-exception-model=wasm");
62610b57cec5SDimitry Andric   } else {
62620b57cec5SDimitry Andric     switch (TC.GetExceptionModel(Args)) {
62630b57cec5SDimitry Andric     default:
62640b57cec5SDimitry Andric       break;
62650b57cec5SDimitry Andric     case llvm::ExceptionHandling::DwarfCFI:
6266e8d8bef9SDimitry Andric       CmdArgs.push_back("-exception-model=dwarf");
62670b57cec5SDimitry Andric       break;
62680b57cec5SDimitry Andric     case llvm::ExceptionHandling::SjLj:
6269e8d8bef9SDimitry Andric       CmdArgs.push_back("-exception-model=sjlj");
62700b57cec5SDimitry Andric       break;
62710b57cec5SDimitry Andric     case llvm::ExceptionHandling::WinEH:
6272e8d8bef9SDimitry Andric       CmdArgs.push_back("-exception-model=seh");
62730b57cec5SDimitry Andric       break;
62740b57cec5SDimitry Andric     }
62750b57cec5SDimitry Andric   }
62760b57cec5SDimitry Andric 
62770b57cec5SDimitry Andric   // C++ "sane" operator new.
62780b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
62790b57cec5SDimitry Andric                     options::OPT_fno_assume_sane_operator_new))
62800b57cec5SDimitry Andric     CmdArgs.push_back("-fno-assume-sane-operator-new");
62810b57cec5SDimitry Andric 
62820b57cec5SDimitry Andric   // -frelaxed-template-template-args is off by default, as it is a severe
62830b57cec5SDimitry Andric   // breaking change until a corresponding change to template partial ordering
62840b57cec5SDimitry Andric   // is provided.
62850b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_frelaxed_template_template_args,
62860b57cec5SDimitry Andric                    options::OPT_fno_relaxed_template_template_args, false))
62870b57cec5SDimitry Andric     CmdArgs.push_back("-frelaxed-template-template-args");
62880b57cec5SDimitry Andric 
62890b57cec5SDimitry Andric   // -fsized-deallocation is off by default, as it is an ABI-breaking change for
62900b57cec5SDimitry Andric   // most platforms.
62910b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fsized_deallocation,
62920b57cec5SDimitry Andric                    options::OPT_fno_sized_deallocation, false))
62930b57cec5SDimitry Andric     CmdArgs.push_back("-fsized-deallocation");
62940b57cec5SDimitry Andric 
62950b57cec5SDimitry Andric   // -faligned-allocation is on by default in C++17 onwards and otherwise off
62960b57cec5SDimitry Andric   // by default.
62970b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_faligned_allocation,
62980b57cec5SDimitry Andric                                options::OPT_fno_aligned_allocation,
62990b57cec5SDimitry Andric                                options::OPT_faligned_new_EQ)) {
63000b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fno_aligned_allocation))
63010b57cec5SDimitry Andric       CmdArgs.push_back("-fno-aligned-allocation");
63020b57cec5SDimitry Andric     else
63030b57cec5SDimitry Andric       CmdArgs.push_back("-faligned-allocation");
63040b57cec5SDimitry Andric   }
63050b57cec5SDimitry Andric 
63060b57cec5SDimitry Andric   // The default new alignment can be specified using a dedicated option or via
63070b57cec5SDimitry Andric   // a GCC-compatible option that also turns on aligned allocation.
63080b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fnew_alignment_EQ,
63090b57cec5SDimitry Andric                                options::OPT_faligned_new_EQ))
63100b57cec5SDimitry Andric     CmdArgs.push_back(
63110b57cec5SDimitry Andric         Args.MakeArgString(Twine("-fnew-alignment=") + A->getValue()));
63120b57cec5SDimitry Andric 
63130b57cec5SDimitry Andric   // -fconstant-cfstrings is default, and may be subject to argument translation
63140b57cec5SDimitry Andric   // on Darwin.
63150b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
63160b57cec5SDimitry Andric                     options::OPT_fno_constant_cfstrings) ||
63170b57cec5SDimitry Andric       !Args.hasFlag(options::OPT_mconstant_cfstrings,
63180b57cec5SDimitry Andric                     options::OPT_mno_constant_cfstrings))
63190b57cec5SDimitry Andric     CmdArgs.push_back("-fno-constant-cfstrings");
63200b57cec5SDimitry Andric 
63210b57cec5SDimitry Andric   // -fno-pascal-strings is default, only pass non-default.
63220b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fpascal_strings,
63230b57cec5SDimitry Andric                    options::OPT_fno_pascal_strings, false))
63240b57cec5SDimitry Andric     CmdArgs.push_back("-fpascal-strings");
63250b57cec5SDimitry Andric 
63260b57cec5SDimitry Andric   // Honor -fpack-struct= and -fpack-struct, if given. Note that
63270b57cec5SDimitry Andric   // -fno-pack-struct doesn't apply to -fpack-struct=.
63280b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
63290b57cec5SDimitry Andric     std::string PackStructStr = "-fpack-struct=";
63300b57cec5SDimitry Andric     PackStructStr += A->getValue();
63310b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(PackStructStr));
63320b57cec5SDimitry Andric   } else if (Args.hasFlag(options::OPT_fpack_struct,
63330b57cec5SDimitry Andric                           options::OPT_fno_pack_struct, false)) {
63340b57cec5SDimitry Andric     CmdArgs.push_back("-fpack-struct=1");
63350b57cec5SDimitry Andric   }
63360b57cec5SDimitry Andric 
63370b57cec5SDimitry Andric   // Handle -fmax-type-align=N and -fno-type-align
63380b57cec5SDimitry Andric   bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align);
63390b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) {
63400b57cec5SDimitry Andric     if (!SkipMaxTypeAlign) {
63410b57cec5SDimitry Andric       std::string MaxTypeAlignStr = "-fmax-type-align=";
63420b57cec5SDimitry Andric       MaxTypeAlignStr += A->getValue();
63430b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
63440b57cec5SDimitry Andric     }
63450b57cec5SDimitry Andric   } else if (RawTriple.isOSDarwin()) {
63460b57cec5SDimitry Andric     if (!SkipMaxTypeAlign) {
63470b57cec5SDimitry Andric       std::string MaxTypeAlignStr = "-fmax-type-align=16";
63480b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
63490b57cec5SDimitry Andric     }
63500b57cec5SDimitry Andric   }
63510b57cec5SDimitry Andric 
63520b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_Qy, options::OPT_Qn, true))
63530b57cec5SDimitry Andric     CmdArgs.push_back("-Qn");
63540b57cec5SDimitry Andric 
63555ffd83dbSDimitry Andric   // -fno-common is the default, set -fcommon only when that flag is set.
63565ffd83dbSDimitry Andric   if (Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common, false))
63575ffd83dbSDimitry Andric     CmdArgs.push_back("-fcommon");
63580b57cec5SDimitry Andric 
63590b57cec5SDimitry Andric   // -fsigned-bitfields is default, and clang doesn't yet support
63600b57cec5SDimitry Andric   // -funsigned-bitfields.
63610b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fsigned_bitfields,
63620b57cec5SDimitry Andric                     options::OPT_funsigned_bitfields))
63630b57cec5SDimitry Andric     D.Diag(diag::warn_drv_clang_unsupported)
63640b57cec5SDimitry Andric         << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
63650b57cec5SDimitry Andric 
63660b57cec5SDimitry Andric   // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
63670b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope))
63680b57cec5SDimitry Andric     D.Diag(diag::err_drv_clang_unsupported)
63690b57cec5SDimitry Andric         << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
63700b57cec5SDimitry Andric 
63710b57cec5SDimitry Andric   // -finput_charset=UTF-8 is default. Reject others
63720b57cec5SDimitry Andric   if (Arg *inputCharset = Args.getLastArg(options::OPT_finput_charset_EQ)) {
63730b57cec5SDimitry Andric     StringRef value = inputCharset->getValue();
6374fe6060f1SDimitry Andric     if (!value.equals_insensitive("utf-8"))
63750b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args)
63760b57cec5SDimitry Andric                                           << value;
63770b57cec5SDimitry Andric   }
63780b57cec5SDimitry Andric 
63790b57cec5SDimitry Andric   // -fexec_charset=UTF-8 is default. Reject others
63800b57cec5SDimitry Andric   if (Arg *execCharset = Args.getLastArg(options::OPT_fexec_charset_EQ)) {
63810b57cec5SDimitry Andric     StringRef value = execCharset->getValue();
6382fe6060f1SDimitry Andric     if (!value.equals_insensitive("utf-8"))
63830b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args)
63840b57cec5SDimitry Andric                                           << value;
63850b57cec5SDimitry Andric   }
63860b57cec5SDimitry Andric 
63870b57cec5SDimitry Andric   RenderDiagnosticsOptions(D, Args, CmdArgs);
63880b57cec5SDimitry Andric 
63890b57cec5SDimitry Andric   // -fno-asm-blocks is default.
63900b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
63910b57cec5SDimitry Andric                    false))
63920b57cec5SDimitry Andric     CmdArgs.push_back("-fasm-blocks");
63930b57cec5SDimitry Andric 
63940b57cec5SDimitry Andric   // -fgnu-inline-asm is default.
63950b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fgnu_inline_asm,
63960b57cec5SDimitry Andric                     options::OPT_fno_gnu_inline_asm, true))
63970b57cec5SDimitry Andric     CmdArgs.push_back("-fno-gnu-inline-asm");
63980b57cec5SDimitry Andric 
63990b57cec5SDimitry Andric   // Enable vectorization per default according to the optimization level
64000b57cec5SDimitry Andric   // selected. For optimization levels that want vectorization we use the alias
64010b57cec5SDimitry Andric   // option to simplify the hasFlag logic.
64020b57cec5SDimitry Andric   bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false);
64030b57cec5SDimitry Andric   OptSpecifier VectorizeAliasOption =
64040b57cec5SDimitry Andric       EnableVec ? options::OPT_O_Group : options::OPT_fvectorize;
64050b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
64060b57cec5SDimitry Andric                    options::OPT_fno_vectorize, EnableVec))
64070b57cec5SDimitry Andric     CmdArgs.push_back("-vectorize-loops");
64080b57cec5SDimitry Andric 
64090b57cec5SDimitry Andric   // -fslp-vectorize is enabled based on the optimization level selected.
64100b57cec5SDimitry Andric   bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true);
64110b57cec5SDimitry Andric   OptSpecifier SLPVectAliasOption =
64120b57cec5SDimitry Andric       EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize;
64130b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption,
64140b57cec5SDimitry Andric                    options::OPT_fno_slp_vectorize, EnableSLPVec))
64150b57cec5SDimitry Andric     CmdArgs.push_back("-vectorize-slp");
64160b57cec5SDimitry Andric 
64170b57cec5SDimitry Andric   ParseMPreferVectorWidth(D, Args, CmdArgs);
64180b57cec5SDimitry Andric 
64190b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fshow_overloads_EQ);
64200b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs,
64210b57cec5SDimitry Andric                   options::OPT_fsanitize_undefined_strip_path_components_EQ);
64220b57cec5SDimitry Andric 
64230b57cec5SDimitry Andric   // -fdollars-in-identifiers default varies depending on platform and
64240b57cec5SDimitry Andric   // language; only pass if specified.
64250b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
64260b57cec5SDimitry Andric                                options::OPT_fno_dollars_in_identifiers)) {
64270b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
64280b57cec5SDimitry Andric       CmdArgs.push_back("-fdollars-in-identifiers");
64290b57cec5SDimitry Andric     else
64300b57cec5SDimitry Andric       CmdArgs.push_back("-fno-dollars-in-identifiers");
64310b57cec5SDimitry Andric   }
64320b57cec5SDimitry Andric 
64330b57cec5SDimitry Andric   // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
64340b57cec5SDimitry Andric   // practical purposes.
64350b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
64360b57cec5SDimitry Andric                                options::OPT_fno_unit_at_a_time)) {
64370b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
64380b57cec5SDimitry Andric       D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
64390b57cec5SDimitry Andric   }
64400b57cec5SDimitry Andric 
64410b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fapple_pragma_pack,
64420b57cec5SDimitry Andric                    options::OPT_fno_apple_pragma_pack, false))
64430b57cec5SDimitry Andric     CmdArgs.push_back("-fapple-pragma-pack");
64440b57cec5SDimitry Andric 
6445e8d8bef9SDimitry Andric   if (Args.hasFlag(options::OPT_fxl_pragma_pack,
6446e8d8bef9SDimitry Andric                    options::OPT_fno_xl_pragma_pack, RawTriple.isOSAIX()))
6447e8d8bef9SDimitry Andric     CmdArgs.push_back("-fxl-pragma-pack");
6448e8d8bef9SDimitry Andric 
64490b57cec5SDimitry Andric   // Remarks can be enabled with any of the `-f.*optimization-record.*` flags.
6450480093f4SDimitry Andric   if (willEmitRemarks(Args) && checkRemarksOptions(D, Args, Triple))
6451480093f4SDimitry Andric     renderRemarksOptions(Args, CmdArgs, Triple, Input, Output, JA);
64520b57cec5SDimitry Andric 
64530b57cec5SDimitry Andric   bool RewriteImports = Args.hasFlag(options::OPT_frewrite_imports,
64540b57cec5SDimitry Andric                                      options::OPT_fno_rewrite_imports, false);
64550b57cec5SDimitry Andric   if (RewriteImports)
64560b57cec5SDimitry Andric     CmdArgs.push_back("-frewrite-imports");
64570b57cec5SDimitry Andric 
64580b57cec5SDimitry Andric   // Enable rewrite includes if the user's asked for it or if we're generating
64590b57cec5SDimitry Andric   // diagnostics.
64600b57cec5SDimitry Andric   // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be
64610b57cec5SDimitry Andric   // nice to enable this when doing a crashdump for modules as well.
64620b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_frewrite_includes,
64630b57cec5SDimitry Andric                    options::OPT_fno_rewrite_includes, false) ||
64640b57cec5SDimitry Andric       (C.isForDiagnostics() && !HaveModules))
64650b57cec5SDimitry Andric     CmdArgs.push_back("-frewrite-includes");
64660b57cec5SDimitry Andric 
64670b57cec5SDimitry Andric   // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
64680b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_traditional,
64690b57cec5SDimitry Andric                                options::OPT_traditional_cpp)) {
64700b57cec5SDimitry Andric     if (isa<PreprocessJobAction>(JA))
64710b57cec5SDimitry Andric       CmdArgs.push_back("-traditional-cpp");
64720b57cec5SDimitry Andric     else
64730b57cec5SDimitry Andric       D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
64740b57cec5SDimitry Andric   }
64750b57cec5SDimitry Andric 
64760b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_dM);
64770b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_dD);
64780b57cec5SDimitry Andric 
64795ffd83dbSDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_fmax_tokens_EQ);
64805ffd83dbSDimitry Andric 
64810b57cec5SDimitry Andric   // Handle serialized diagnostics.
64820b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
64830b57cec5SDimitry Andric     CmdArgs.push_back("-serialize-diagnostic-file");
64840b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(A->getValue()));
64850b57cec5SDimitry Andric   }
64860b57cec5SDimitry Andric 
64870b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
64880b57cec5SDimitry Andric     CmdArgs.push_back("-fretain-comments-from-system-headers");
64890b57cec5SDimitry Andric 
64900b57cec5SDimitry Andric   // Forward -fcomment-block-commands to -cc1.
64910b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
64920b57cec5SDimitry Andric   // Forward -fparse-all-comments to -cc1.
64930b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments);
64940b57cec5SDimitry Andric 
64950b57cec5SDimitry Andric   // Turn -fplugin=name.so into -load name.so
64960b57cec5SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_fplugin_EQ)) {
64970b57cec5SDimitry Andric     CmdArgs.push_back("-load");
64980b57cec5SDimitry Andric     CmdArgs.push_back(A->getValue());
64990b57cec5SDimitry Andric     A->claim();
65000b57cec5SDimitry Andric   }
65010b57cec5SDimitry Andric 
65020b57cec5SDimitry Andric   // Forward -fpass-plugin=name.so to -cc1.
65030b57cec5SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_fpass_plugin_EQ)) {
65040b57cec5SDimitry Andric     CmdArgs.push_back(
65050b57cec5SDimitry Andric         Args.MakeArgString(Twine("-fpass-plugin=") + A->getValue()));
65060b57cec5SDimitry Andric     A->claim();
65070b57cec5SDimitry Andric   }
65080b57cec5SDimitry Andric 
65090b57cec5SDimitry Andric   // Setup statistics file output.
65100b57cec5SDimitry Andric   SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D);
65110b57cec5SDimitry Andric   if (!StatsFile.empty())
65120b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Twine("-stats-file=") + StatsFile));
65130b57cec5SDimitry Andric 
65140b57cec5SDimitry Andric   // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
65150b57cec5SDimitry Andric   // parser.
65160b57cec5SDimitry Andric   // -finclude-default-header flag is for preprocessor,
65170b57cec5SDimitry Andric   // do not pass it to other cc1 commands when save-temps is enabled
65180b57cec5SDimitry Andric   if (C.getDriver().isSaveTempsEnabled() &&
65190b57cec5SDimitry Andric       !isa<PreprocessJobAction>(JA)) {
65200b57cec5SDimitry Andric     for (auto Arg : Args.filtered(options::OPT_Xclang)) {
65210b57cec5SDimitry Andric       Arg->claim();
65220b57cec5SDimitry Andric       if (StringRef(Arg->getValue()) != "-finclude-default-header")
65230b57cec5SDimitry Andric         CmdArgs.push_back(Arg->getValue());
65240b57cec5SDimitry Andric     }
65250b57cec5SDimitry Andric   }
65260b57cec5SDimitry Andric   else {
65270b57cec5SDimitry Andric     Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
65280b57cec5SDimitry Andric   }
65290b57cec5SDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
65300b57cec5SDimitry Andric     A->claim();
65310b57cec5SDimitry Andric 
65320b57cec5SDimitry Andric     // We translate this by hand to the -cc1 argument, since nightly test uses
65330b57cec5SDimitry Andric     // it and developers have been trained to spell it with -mllvm. Both
65340b57cec5SDimitry Andric     // spellings are now deprecated and should be removed.
65350b57cec5SDimitry Andric     if (StringRef(A->getValue(0)) == "-disable-llvm-optzns") {
65360b57cec5SDimitry Andric       CmdArgs.push_back("-disable-llvm-optzns");
65370b57cec5SDimitry Andric     } else {
65380b57cec5SDimitry Andric       A->render(Args, CmdArgs);
65390b57cec5SDimitry Andric     }
65400b57cec5SDimitry Andric   }
65410b57cec5SDimitry Andric 
65420b57cec5SDimitry Andric   // With -save-temps, we want to save the unoptimized bitcode output from the
65430b57cec5SDimitry Andric   // CompileJobAction, use -disable-llvm-passes to get pristine IR generated
65440b57cec5SDimitry Andric   // by the frontend.
65450b57cec5SDimitry Andric   // When -fembed-bitcode is enabled, optimized bitcode is emitted because it
65460b57cec5SDimitry Andric   // has slightly different breakdown between stages.
65470b57cec5SDimitry Andric   // FIXME: -fembed-bitcode -save-temps will save optimized bitcode instead of
65480b57cec5SDimitry Andric   // pristine IR generated by the frontend. Ideally, a new compile action should
65490b57cec5SDimitry Andric   // be added so both IR can be captured.
65505ffd83dbSDimitry Andric   if ((C.getDriver().isSaveTempsEnabled() ||
65515ffd83dbSDimitry Andric        JA.isHostOffloading(Action::OFK_OpenMP)) &&
6552fe6060f1SDimitry Andric       !(C.getDriver().embedBitcodeInObject() && !IsUsingLTO) &&
65530b57cec5SDimitry Andric       isa<CompileJobAction>(JA))
65540b57cec5SDimitry Andric     CmdArgs.push_back("-disable-llvm-passes");
65550b57cec5SDimitry Andric 
65560b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_undef);
65570b57cec5SDimitry Andric 
65580b57cec5SDimitry Andric   const char *Exec = D.getClangProgramPath();
65590b57cec5SDimitry Andric 
65600b57cec5SDimitry Andric   // Optionally embed the -cc1 level arguments into the debug info or a
65610b57cec5SDimitry Andric   // section, for build analysis.
65620b57cec5SDimitry Andric   // Also record command line arguments into the debug info if
65630b57cec5SDimitry Andric   // -grecord-gcc-switches options is set on.
65640b57cec5SDimitry Andric   // By default, -gno-record-gcc-switches is set on and no recording.
65650b57cec5SDimitry Andric   auto GRecordSwitches =
65660b57cec5SDimitry Andric       Args.hasFlag(options::OPT_grecord_command_line,
65670b57cec5SDimitry Andric                    options::OPT_gno_record_command_line, false);
65680b57cec5SDimitry Andric   auto FRecordSwitches =
65690b57cec5SDimitry Andric       Args.hasFlag(options::OPT_frecord_command_line,
65700b57cec5SDimitry Andric                    options::OPT_fno_record_command_line, false);
65710b57cec5SDimitry Andric   if (FRecordSwitches && !Triple.isOSBinFormatELF())
65720b57cec5SDimitry Andric     D.Diag(diag::err_drv_unsupported_opt_for_target)
65730b57cec5SDimitry Andric         << Args.getLastArg(options::OPT_frecord_command_line)->getAsString(Args)
65740b57cec5SDimitry Andric         << TripleStr;
65750b57cec5SDimitry Andric   if (TC.UseDwarfDebugFlags() || GRecordSwitches || FRecordSwitches) {
65760b57cec5SDimitry Andric     ArgStringList OriginalArgs;
65770b57cec5SDimitry Andric     for (const auto &Arg : Args)
65780b57cec5SDimitry Andric       Arg->render(Args, OriginalArgs);
65790b57cec5SDimitry Andric 
65800b57cec5SDimitry Andric     SmallString<256> Flags;
65815ffd83dbSDimitry Andric     EscapeSpacesAndBackslashes(Exec, Flags);
65820b57cec5SDimitry Andric     for (const char *OriginalArg : OriginalArgs) {
65830b57cec5SDimitry Andric       SmallString<128> EscapedArg;
65840b57cec5SDimitry Andric       EscapeSpacesAndBackslashes(OriginalArg, EscapedArg);
65850b57cec5SDimitry Andric       Flags += " ";
65860b57cec5SDimitry Andric       Flags += EscapedArg;
65870b57cec5SDimitry Andric     }
65880b57cec5SDimitry Andric     auto FlagsArgString = Args.MakeArgString(Flags);
65890b57cec5SDimitry Andric     if (TC.UseDwarfDebugFlags() || GRecordSwitches) {
65900b57cec5SDimitry Andric       CmdArgs.push_back("-dwarf-debug-flags");
65910b57cec5SDimitry Andric       CmdArgs.push_back(FlagsArgString);
65920b57cec5SDimitry Andric     }
65930b57cec5SDimitry Andric     if (FRecordSwitches) {
65940b57cec5SDimitry Andric       CmdArgs.push_back("-record-command-line");
65950b57cec5SDimitry Andric       CmdArgs.push_back(FlagsArgString);
65960b57cec5SDimitry Andric     }
65970b57cec5SDimitry Andric   }
65980b57cec5SDimitry Andric 
65990b57cec5SDimitry Andric   // Host-side cuda compilation receives all device-side outputs in a single
66000b57cec5SDimitry Andric   // fatbin as Inputs[1]. Include the binary with -fcuda-include-gpubinary.
66010b57cec5SDimitry Andric   if ((IsCuda || IsHIP) && CudaDeviceInput) {
66020b57cec5SDimitry Andric       CmdArgs.push_back("-fcuda-include-gpubinary");
66030b57cec5SDimitry Andric       CmdArgs.push_back(CudaDeviceInput->getFilename());
66040b57cec5SDimitry Andric       if (Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false))
66050b57cec5SDimitry Andric         CmdArgs.push_back("-fgpu-rdc");
66060b57cec5SDimitry Andric   }
66070b57cec5SDimitry Andric 
66080b57cec5SDimitry Andric   if (IsCuda) {
66090b57cec5SDimitry Andric     if (Args.hasFlag(options::OPT_fcuda_short_ptr,
66100b57cec5SDimitry Andric                      options::OPT_fno_cuda_short_ptr, false))
66110b57cec5SDimitry Andric       CmdArgs.push_back("-fcuda-short-ptr");
66120b57cec5SDimitry Andric   }
66130b57cec5SDimitry Andric 
6614fe6060f1SDimitry Andric   if (IsCuda || IsHIP) {
6615fe6060f1SDimitry Andric     // Determine the original source input.
6616fe6060f1SDimitry Andric     const Action *SourceAction = &JA;
6617fe6060f1SDimitry Andric     while (SourceAction->getKind() != Action::InputClass) {
6618fe6060f1SDimitry Andric       assert(!SourceAction->getInputs().empty() && "unexpected root action!");
6619fe6060f1SDimitry Andric       SourceAction = SourceAction->getInputs()[0];
6620fe6060f1SDimitry Andric     }
6621fe6060f1SDimitry Andric     auto CUID = cast<InputAction>(SourceAction)->getId();
6622fe6060f1SDimitry Andric     if (!CUID.empty())
6623fe6060f1SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(Twine("-cuid=") + Twine(CUID)));
6624fe6060f1SDimitry Andric   }
6625fe6060f1SDimitry Andric 
6626480093f4SDimitry Andric   if (IsHIP)
6627480093f4SDimitry Andric     CmdArgs.push_back("-fcuda-allow-variadic-functions");
6628480093f4SDimitry Andric 
6629fe6060f1SDimitry Andric   if (IsCudaDevice || IsHIPDevice) {
6630fe6060f1SDimitry Andric     StringRef InlineThresh =
6631fe6060f1SDimitry Andric         Args.getLastArgValue(options::OPT_fgpu_inline_threshold_EQ);
6632fe6060f1SDimitry Andric     if (!InlineThresh.empty()) {
6633fe6060f1SDimitry Andric       std::string ArgStr =
6634fe6060f1SDimitry Andric           std::string("-inline-threshold=") + InlineThresh.str();
6635fe6060f1SDimitry Andric       CmdArgs.append({"-mllvm", Args.MakeArgStringRef(ArgStr)});
6636fe6060f1SDimitry Andric     }
6637fe6060f1SDimitry Andric   }
6638fe6060f1SDimitry Andric 
66390b57cec5SDimitry Andric   // OpenMP offloading device jobs take the argument -fopenmp-host-ir-file-path
66400b57cec5SDimitry Andric   // to specify the result of the compile phase on the host, so the meaningful
66410b57cec5SDimitry Andric   // device declarations can be identified. Also, -fopenmp-is-device is passed
66420b57cec5SDimitry Andric   // along to tell the frontend that it is generating code for a device, so that
66430b57cec5SDimitry Andric   // only the relevant declarations are emitted.
66440b57cec5SDimitry Andric   if (IsOpenMPDevice) {
66450b57cec5SDimitry Andric     CmdArgs.push_back("-fopenmp-is-device");
66460b57cec5SDimitry Andric     if (OpenMPDeviceInput) {
66470b57cec5SDimitry Andric       CmdArgs.push_back("-fopenmp-host-ir-file-path");
66480b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(OpenMPDeviceInput->getFilename()));
66490b57cec5SDimitry Andric     }
66500b57cec5SDimitry Andric   }
66510b57cec5SDimitry Andric 
6652e8d8bef9SDimitry Andric   if (Triple.isAMDGPU()) {
6653e8d8bef9SDimitry Andric     handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs);
6654e8d8bef9SDimitry Andric 
6655e8d8bef9SDimitry Andric     if (Args.hasFlag(options::OPT_munsafe_fp_atomics,
6656fe6060f1SDimitry Andric                      options::OPT_mno_unsafe_fp_atomics, /*Default=*/false))
6657e8d8bef9SDimitry Andric       CmdArgs.push_back("-munsafe-fp-atomics");
6658e8d8bef9SDimitry Andric   }
6659e8d8bef9SDimitry Andric 
66600b57cec5SDimitry Andric   // For all the host OpenMP offloading compile jobs we need to pass the targets
66610b57cec5SDimitry Andric   // information using -fopenmp-targets= option.
66620b57cec5SDimitry Andric   if (JA.isHostOffloading(Action::OFK_OpenMP)) {
66630b57cec5SDimitry Andric     SmallString<128> TargetInfo("-fopenmp-targets=");
66640b57cec5SDimitry Andric 
66650b57cec5SDimitry Andric     Arg *Tgts = Args.getLastArg(options::OPT_fopenmp_targets_EQ);
66660b57cec5SDimitry Andric     assert(Tgts && Tgts->getNumValues() &&
66670b57cec5SDimitry Andric            "OpenMP offloading has to have targets specified.");
66680b57cec5SDimitry Andric     for (unsigned i = 0; i < Tgts->getNumValues(); ++i) {
66690b57cec5SDimitry Andric       if (i)
66700b57cec5SDimitry Andric         TargetInfo += ',';
66710b57cec5SDimitry Andric       // We need to get the string from the triple because it may be not exactly
66720b57cec5SDimitry Andric       // the same as the one we get directly from the arguments.
66730b57cec5SDimitry Andric       llvm::Triple T(Tgts->getValue(i));
66740b57cec5SDimitry Andric       TargetInfo += T.getTriple();
66750b57cec5SDimitry Andric     }
66760b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(TargetInfo.str()));
66770b57cec5SDimitry Andric   }
66780b57cec5SDimitry Andric 
6679a7dea167SDimitry Andric   bool VirtualFunctionElimination =
6680a7dea167SDimitry Andric       Args.hasFlag(options::OPT_fvirtual_function_elimination,
6681a7dea167SDimitry Andric                    options::OPT_fno_virtual_function_elimination, false);
6682a7dea167SDimitry Andric   if (VirtualFunctionElimination) {
6683a7dea167SDimitry Andric     // VFE requires full LTO (currently, this might be relaxed to allow ThinLTO
6684a7dea167SDimitry Andric     // in the future).
6685fe6060f1SDimitry Andric     if (LTOMode != LTOK_Full)
6686a7dea167SDimitry Andric       D.Diag(diag::err_drv_argument_only_allowed_with)
6687a7dea167SDimitry Andric           << "-fvirtual-function-elimination"
6688a7dea167SDimitry Andric           << "-flto=full";
6689a7dea167SDimitry Andric 
6690a7dea167SDimitry Andric     CmdArgs.push_back("-fvirtual-function-elimination");
6691a7dea167SDimitry Andric   }
6692a7dea167SDimitry Andric 
6693a7dea167SDimitry Andric   // VFE requires whole-program-vtables, and enables it by default.
6694a7dea167SDimitry Andric   bool WholeProgramVTables = Args.hasFlag(
6695a7dea167SDimitry Andric       options::OPT_fwhole_program_vtables,
6696a7dea167SDimitry Andric       options::OPT_fno_whole_program_vtables, VirtualFunctionElimination);
6697a7dea167SDimitry Andric   if (VirtualFunctionElimination && !WholeProgramVTables) {
6698a7dea167SDimitry Andric     D.Diag(diag::err_drv_argument_not_allowed_with)
6699a7dea167SDimitry Andric         << "-fno-whole-program-vtables"
6700a7dea167SDimitry Andric         << "-fvirtual-function-elimination";
6701a7dea167SDimitry Andric   }
6702a7dea167SDimitry Andric 
67030b57cec5SDimitry Andric   if (WholeProgramVTables) {
6704fe6060f1SDimitry Andric     // Propagate -fwhole-program-vtables if this is an LTO compile.
6705fe6060f1SDimitry Andric     if (IsUsingLTO)
6706fe6060f1SDimitry Andric       CmdArgs.push_back("-fwhole-program-vtables");
6707fe6060f1SDimitry Andric     // Check if we passed LTO options but they were suppressed because this is a
6708fe6060f1SDimitry Andric     // device offloading action, or we passed device offload LTO options which
6709fe6060f1SDimitry Andric     // were suppressed because this is not the device offload action.
6710fe6060f1SDimitry Andric     // Otherwise, issue an error.
6711fe6060f1SDimitry Andric     else if (!D.isUsingLTO(!IsDeviceOffloadAction))
67120b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_only_allowed_with)
67130b57cec5SDimitry Andric           << "-fwhole-program-vtables"
67140b57cec5SDimitry Andric           << "-flto";
67150b57cec5SDimitry Andric   }
67160b57cec5SDimitry Andric 
6717480093f4SDimitry Andric   bool DefaultsSplitLTOUnit =
6718480093f4SDimitry Andric       (WholeProgramVTables || Sanitize.needsLTO()) &&
6719fe6060f1SDimitry Andric       (LTOMode == LTOK_Full || TC.canSplitThinLTOUnit());
67200b57cec5SDimitry Andric   bool SplitLTOUnit =
67210b57cec5SDimitry Andric       Args.hasFlag(options::OPT_fsplit_lto_unit,
6722a7dea167SDimitry Andric                    options::OPT_fno_split_lto_unit, DefaultsSplitLTOUnit);
6723a7dea167SDimitry Andric   if (Sanitize.needsLTO() && !SplitLTOUnit)
6724a7dea167SDimitry Andric     D.Diag(diag::err_drv_argument_not_allowed_with) << "-fno-split-lto-unit"
6725a7dea167SDimitry Andric                                                     << "-fsanitize=cfi";
67260b57cec5SDimitry Andric   if (SplitLTOUnit)
67270b57cec5SDimitry Andric     CmdArgs.push_back("-fsplit-lto-unit");
67280b57cec5SDimitry Andric 
67295ffd83dbSDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fglobal_isel,
67305ffd83dbSDimitry Andric                                options::OPT_fno_global_isel)) {
67310b57cec5SDimitry Andric     CmdArgs.push_back("-mllvm");
67325ffd83dbSDimitry Andric     if (A->getOption().matches(options::OPT_fglobal_isel)) {
67330b57cec5SDimitry Andric       CmdArgs.push_back("-global-isel=1");
67340b57cec5SDimitry Andric 
67350b57cec5SDimitry Andric       // GISel is on by default on AArch64 -O0, so don't bother adding
67360b57cec5SDimitry Andric       // the fallback remarks for it. Other combinations will add a warning of
67370b57cec5SDimitry Andric       // some kind.
67380b57cec5SDimitry Andric       bool IsArchSupported = Triple.getArch() == llvm::Triple::aarch64;
67390b57cec5SDimitry Andric       bool IsOptLevelSupported = false;
67400b57cec5SDimitry Andric 
67410b57cec5SDimitry Andric       Arg *A = Args.getLastArg(options::OPT_O_Group);
67420b57cec5SDimitry Andric       if (Triple.getArch() == llvm::Triple::aarch64) {
67430b57cec5SDimitry Andric         if (!A || A->getOption().matches(options::OPT_O0))
67440b57cec5SDimitry Andric           IsOptLevelSupported = true;
67450b57cec5SDimitry Andric       }
67460b57cec5SDimitry Andric       if (!IsArchSupported || !IsOptLevelSupported) {
67470b57cec5SDimitry Andric         CmdArgs.push_back("-mllvm");
67480b57cec5SDimitry Andric         CmdArgs.push_back("-global-isel-abort=2");
67490b57cec5SDimitry Andric 
67500b57cec5SDimitry Andric         if (!IsArchSupported)
67515ffd83dbSDimitry Andric           D.Diag(diag::warn_drv_global_isel_incomplete) << Triple.getArchName();
67520b57cec5SDimitry Andric         else
67535ffd83dbSDimitry Andric           D.Diag(diag::warn_drv_global_isel_incomplete_opt);
67540b57cec5SDimitry Andric       }
67550b57cec5SDimitry Andric     } else {
67560b57cec5SDimitry Andric       CmdArgs.push_back("-global-isel=0");
67570b57cec5SDimitry Andric     }
67580b57cec5SDimitry Andric   }
67590b57cec5SDimitry Andric 
67600b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_forder_file_instrumentation)) {
67610b57cec5SDimitry Andric      CmdArgs.push_back("-forder-file-instrumentation");
67620b57cec5SDimitry Andric      // Enable order file instrumentation when ThinLTO is not on. When ThinLTO is
67630b57cec5SDimitry Andric      // on, we need to pass these flags as linker flags and that will be handled
67640b57cec5SDimitry Andric      // outside of the compiler.
6765fe6060f1SDimitry Andric      if (!IsUsingLTO) {
67660b57cec5SDimitry Andric        CmdArgs.push_back("-mllvm");
67670b57cec5SDimitry Andric        CmdArgs.push_back("-enable-order-file-instrumentation");
67680b57cec5SDimitry Andric      }
67690b57cec5SDimitry Andric   }
67700b57cec5SDimitry Andric 
67710b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fforce_enable_int128,
67720b57cec5SDimitry Andric                                options::OPT_fno_force_enable_int128)) {
67730b57cec5SDimitry Andric     if (A->getOption().matches(options::OPT_fforce_enable_int128))
67740b57cec5SDimitry Andric       CmdArgs.push_back("-fforce-enable-int128");
67750b57cec5SDimitry Andric   }
67760b57cec5SDimitry Andric 
67775ffd83dbSDimitry Andric   if (Args.hasFlag(options::OPT_fkeep_static_consts,
67785ffd83dbSDimitry Andric                    options::OPT_fno_keep_static_consts, false))
67795ffd83dbSDimitry Andric     CmdArgs.push_back("-fkeep-static-consts");
67805ffd83dbSDimitry Andric 
67810b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_fcomplete_member_pointers,
67820b57cec5SDimitry Andric                    options::OPT_fno_complete_member_pointers, false))
67830b57cec5SDimitry Andric     CmdArgs.push_back("-fcomplete-member-pointers");
67840b57cec5SDimitry Andric 
67850b57cec5SDimitry Andric   if (!Args.hasFlag(options::OPT_fcxx_static_destructors,
67860b57cec5SDimitry Andric                     options::OPT_fno_cxx_static_destructors, true))
67870b57cec5SDimitry Andric     CmdArgs.push_back("-fno-c++-static-destructors");
67880b57cec5SDimitry Andric 
6789e8d8bef9SDimitry Andric   addMachineOutlinerArgs(D, Args, CmdArgs, Triple, /*IsLTO=*/false);
6790e8d8bef9SDimitry Andric 
6791e8d8bef9SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_moutline_atomics,
6792e8d8bef9SDimitry Andric                                options::OPT_mno_outline_atomics)) {
6793e8d8bef9SDimitry Andric     if (A->getOption().matches(options::OPT_moutline_atomics)) {
6794e8d8bef9SDimitry Andric       // Option -moutline-atomics supported for AArch64 target only.
6795e8d8bef9SDimitry Andric       if (!Triple.isAArch64()) {
6796e8d8bef9SDimitry Andric         D.Diag(diag::warn_drv_moutline_atomics_unsupported_opt)
6797e8d8bef9SDimitry Andric             << Triple.getArchName();
67980b57cec5SDimitry Andric       } else {
6799e8d8bef9SDimitry Andric         CmdArgs.push_back("-target-feature");
6800e8d8bef9SDimitry Andric         CmdArgs.push_back("+outline-atomics");
68010b57cec5SDimitry Andric       }
68020b57cec5SDimitry Andric     } else {
6803e8d8bef9SDimitry Andric       CmdArgs.push_back("-target-feature");
6804e8d8bef9SDimitry Andric       CmdArgs.push_back("-outline-atomics");
68050b57cec5SDimitry Andric     }
6806fe6060f1SDimitry Andric   } else if (Triple.isAArch64() &&
6807fe6060f1SDimitry Andric              getToolChain().IsAArch64OutlineAtomicsDefault(Args)) {
6808fe6060f1SDimitry Andric     CmdArgs.push_back("-target-feature");
6809fe6060f1SDimitry Andric     CmdArgs.push_back("+outline-atomics");
68100b57cec5SDimitry Andric   }
68110b57cec5SDimitry Andric 
68120b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT_faddrsig, options::OPT_fno_addrsig,
68130b57cec5SDimitry Andric                    (TC.getTriple().isOSBinFormatELF() ||
68140b57cec5SDimitry Andric                     TC.getTriple().isOSBinFormatCOFF()) &&
6815e8d8bef9SDimitry Andric                        !TC.getTriple().isPS4() && !TC.getTriple().isVE() &&
68160b57cec5SDimitry Andric                        !TC.getTriple().isOSNetBSD() &&
6817480093f4SDimitry Andric                        !Distro(D.getVFS(), TC.getTriple()).IsGentoo() &&
6818e8d8bef9SDimitry Andric                        !TC.getTriple().isAndroid() && TC.useIntegratedAs()))
68190b57cec5SDimitry Andric     CmdArgs.push_back("-faddrsig");
68200b57cec5SDimitry Andric 
6821fe6060f1SDimitry Andric   if ((Triple.isOSBinFormatELF() || Triple.isOSBinFormatMachO()) &&
6822fe6060f1SDimitry Andric       (EH || UnwindTables || DebugInfoKind != codegenoptions::NoDebugInfo))
6823fe6060f1SDimitry Andric     CmdArgs.push_back("-D__GCC_HAVE_DWARF2_CFI_ASM=1");
6824fe6060f1SDimitry Andric 
68250b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_fsymbol_partition_EQ)) {
68260b57cec5SDimitry Andric     std::string Str = A->getAsString(Args);
68270b57cec5SDimitry Andric     if (!TC.getTriple().isOSBinFormatELF())
68280b57cec5SDimitry Andric       D.Diag(diag::err_drv_unsupported_opt_for_target)
68290b57cec5SDimitry Andric           << Str << TC.getTripleString();
68300b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Str));
68310b57cec5SDimitry Andric   }
68320b57cec5SDimitry Andric 
68330b57cec5SDimitry Andric   // Add the "-o out -x type src.c" flags last. This is done primarily to make
68340b57cec5SDimitry Andric   // the -cc1 command easier to edit when reproducing compiler crashes.
68350b57cec5SDimitry Andric   if (Output.getType() == types::TY_Dependencies) {
68360b57cec5SDimitry Andric     // Handled with other dependency code.
68370b57cec5SDimitry Andric   } else if (Output.isFilename()) {
6838480093f4SDimitry Andric     if (Output.getType() == clang::driver::types::TY_IFS_CPP ||
6839480093f4SDimitry Andric         Output.getType() == clang::driver::types::TY_IFS) {
6840480093f4SDimitry Andric       SmallString<128> OutputFilename(Output.getFilename());
6841480093f4SDimitry Andric       llvm::sys::path::replace_extension(OutputFilename, "ifs");
6842480093f4SDimitry Andric       CmdArgs.push_back("-o");
6843480093f4SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(OutputFilename));
6844480093f4SDimitry Andric     } else {
68450b57cec5SDimitry Andric       CmdArgs.push_back("-o");
68460b57cec5SDimitry Andric       CmdArgs.push_back(Output.getFilename());
6847480093f4SDimitry Andric     }
68480b57cec5SDimitry Andric   } else {
68490b57cec5SDimitry Andric     assert(Output.isNothing() && "Invalid output.");
68500b57cec5SDimitry Andric   }
68510b57cec5SDimitry Andric 
68520b57cec5SDimitry Andric   addDashXForInput(Args, Input, CmdArgs);
68530b57cec5SDimitry Andric 
68540b57cec5SDimitry Andric   ArrayRef<InputInfo> FrontendInputs = Input;
68550b57cec5SDimitry Andric   if (IsHeaderModulePrecompile)
68560b57cec5SDimitry Andric     FrontendInputs = ModuleHeaderInputs;
68570b57cec5SDimitry Andric   else if (Input.isNothing())
68580b57cec5SDimitry Andric     FrontendInputs = {};
68590b57cec5SDimitry Andric 
68600b57cec5SDimitry Andric   for (const InputInfo &Input : FrontendInputs) {
68610b57cec5SDimitry Andric     if (Input.isFilename())
68620b57cec5SDimitry Andric       CmdArgs.push_back(Input.getFilename());
68630b57cec5SDimitry Andric     else
68640b57cec5SDimitry Andric       Input.getInputArg().renderAsInput(Args, CmdArgs);
68650b57cec5SDimitry Andric   }
68660b57cec5SDimitry Andric 
6867fe6060f1SDimitry Andric   if (D.CC1Main && !D.CCGenDiagnostics) {
6868480093f4SDimitry Andric     // Invoke the CC1 directly in this process
6869e8d8bef9SDimitry Andric     C.addCommand(std::make_unique<CC1Command>(JA, *this,
6870e8d8bef9SDimitry Andric                                               ResponseFileSupport::AtFileUTF8(),
6871e8d8bef9SDimitry Andric                                               Exec, CmdArgs, Inputs, Output));
68720b57cec5SDimitry Andric   } else {
6873e8d8bef9SDimitry Andric     C.addCommand(std::make_unique<Command>(JA, *this,
6874e8d8bef9SDimitry Andric                                            ResponseFileSupport::AtFileUTF8(),
6875e8d8bef9SDimitry Andric                                            Exec, CmdArgs, Inputs, Output));
68760b57cec5SDimitry Andric   }
68770b57cec5SDimitry Andric 
68780b57cec5SDimitry Andric   // Make the compile command echo its inputs for /showFilenames.
68790b57cec5SDimitry Andric   if (Output.getType() == types::TY_Object &&
68800b57cec5SDimitry Andric       Args.hasFlag(options::OPT__SLASH_showFilenames,
68810b57cec5SDimitry Andric                    options::OPT__SLASH_showFilenames_, false)) {
688213138422SDimitry Andric     C.getJobs().getJobs().back()->PrintInputFilenames = true;
68830b57cec5SDimitry Andric   }
68840b57cec5SDimitry Andric 
68850b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_pg))
68865ffd83dbSDimitry Andric     if (FPKeepKind == CodeGenOptions::FramePointerKind::None &&
68875ffd83dbSDimitry Andric         !Args.hasArg(options::OPT_mfentry))
68880b57cec5SDimitry Andric       D.Diag(diag::err_drv_argument_not_allowed_with) << "-fomit-frame-pointer"
68890b57cec5SDimitry Andric                                                       << A->getAsString(Args);
68900b57cec5SDimitry Andric 
68910b57cec5SDimitry Andric   // Claim some arguments which clang supports automatically.
68920b57cec5SDimitry Andric 
68930b57cec5SDimitry Andric   // -fpch-preprocess is used with gcc to add a special marker in the output to
68940b57cec5SDimitry Andric   // include the PCH file.
68950b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_fpch_preprocess);
68960b57cec5SDimitry Andric 
68970b57cec5SDimitry Andric   // Claim some arguments which clang doesn't support, but we don't
68980b57cec5SDimitry Andric   // care to warn the user about.
68990b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
69000b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
69010b57cec5SDimitry Andric 
69020b57cec5SDimitry Andric   // Disable warnings for clang -E -emit-llvm foo.c
69030b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_emit_llvm);
69040b57cec5SDimitry Andric }
69050b57cec5SDimitry Andric 
69060b57cec5SDimitry Andric Clang::Clang(const ToolChain &TC)
69070b57cec5SDimitry Andric     // CAUTION! The first constructor argument ("clang") is not arbitrary,
69080b57cec5SDimitry Andric     // as it is for other tools. Some operations on a Tool actually test
69090b57cec5SDimitry Andric     // whether that tool is Clang based on the Tool's Name as a string.
69105ffd83dbSDimitry Andric     : Tool("clang", "clang frontend", TC) {}
69110b57cec5SDimitry Andric 
69120b57cec5SDimitry Andric Clang::~Clang() {}
69130b57cec5SDimitry Andric 
69140b57cec5SDimitry Andric /// Add options related to the Objective-C runtime/ABI.
69150b57cec5SDimitry Andric ///
69160b57cec5SDimitry Andric /// Returns true if the runtime is non-fragile.
69170b57cec5SDimitry Andric ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
69185ffd83dbSDimitry Andric                                       const InputInfoList &inputs,
69190b57cec5SDimitry Andric                                       ArgStringList &cmdArgs,
69200b57cec5SDimitry Andric                                       RewriteKind rewriteKind) const {
69210b57cec5SDimitry Andric   // Look for the controlling runtime option.
69220b57cec5SDimitry Andric   Arg *runtimeArg =
69230b57cec5SDimitry Andric       args.getLastArg(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
69240b57cec5SDimitry Andric                       options::OPT_fobjc_runtime_EQ);
69250b57cec5SDimitry Andric 
69260b57cec5SDimitry Andric   // Just forward -fobjc-runtime= to the frontend.  This supercedes
69270b57cec5SDimitry Andric   // options about fragility.
69280b57cec5SDimitry Andric   if (runtimeArg &&
69290b57cec5SDimitry Andric       runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
69300b57cec5SDimitry Andric     ObjCRuntime runtime;
69310b57cec5SDimitry Andric     StringRef value = runtimeArg->getValue();
69320b57cec5SDimitry Andric     if (runtime.tryParse(value)) {
69330b57cec5SDimitry Andric       getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
69340b57cec5SDimitry Andric           << value;
69350b57cec5SDimitry Andric     }
69360b57cec5SDimitry Andric     if ((runtime.getKind() == ObjCRuntime::GNUstep) &&
69370b57cec5SDimitry Andric         (runtime.getVersion() >= VersionTuple(2, 0)))
69380b57cec5SDimitry Andric       if (!getToolChain().getTriple().isOSBinFormatELF() &&
69390b57cec5SDimitry Andric           !getToolChain().getTriple().isOSBinFormatCOFF()) {
69400b57cec5SDimitry Andric         getToolChain().getDriver().Diag(
69410b57cec5SDimitry Andric             diag::err_drv_gnustep_objc_runtime_incompatible_binary)
69420b57cec5SDimitry Andric           << runtime.getVersion().getMajor();
69430b57cec5SDimitry Andric       }
69440b57cec5SDimitry Andric 
69450b57cec5SDimitry Andric     runtimeArg->render(args, cmdArgs);
69460b57cec5SDimitry Andric     return runtime;
69470b57cec5SDimitry Andric   }
69480b57cec5SDimitry Andric 
69490b57cec5SDimitry Andric   // Otherwise, we'll need the ABI "version".  Version numbers are
69500b57cec5SDimitry Andric   // slightly confusing for historical reasons:
69510b57cec5SDimitry Andric   //   1 - Traditional "fragile" ABI
69520b57cec5SDimitry Andric   //   2 - Non-fragile ABI, version 1
69530b57cec5SDimitry Andric   //   3 - Non-fragile ABI, version 2
69540b57cec5SDimitry Andric   unsigned objcABIVersion = 1;
69550b57cec5SDimitry Andric   // If -fobjc-abi-version= is present, use that to set the version.
69560b57cec5SDimitry Andric   if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
69570b57cec5SDimitry Andric     StringRef value = abiArg->getValue();
69580b57cec5SDimitry Andric     if (value == "1")
69590b57cec5SDimitry Andric       objcABIVersion = 1;
69600b57cec5SDimitry Andric     else if (value == "2")
69610b57cec5SDimitry Andric       objcABIVersion = 2;
69620b57cec5SDimitry Andric     else if (value == "3")
69630b57cec5SDimitry Andric       objcABIVersion = 3;
69640b57cec5SDimitry Andric     else
69650b57cec5SDimitry Andric       getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) << value;
69660b57cec5SDimitry Andric   } else {
69670b57cec5SDimitry Andric     // Otherwise, determine if we are using the non-fragile ABI.
69680b57cec5SDimitry Andric     bool nonFragileABIIsDefault =
69690b57cec5SDimitry Andric         (rewriteKind == RK_NonFragile ||
69700b57cec5SDimitry Andric          (rewriteKind == RK_None &&
69710b57cec5SDimitry Andric           getToolChain().IsObjCNonFragileABIDefault()));
69720b57cec5SDimitry Andric     if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
69730b57cec5SDimitry Andric                      options::OPT_fno_objc_nonfragile_abi,
69740b57cec5SDimitry Andric                      nonFragileABIIsDefault)) {
69750b57cec5SDimitry Andric // Determine the non-fragile ABI version to use.
69760b57cec5SDimitry Andric #ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
69770b57cec5SDimitry Andric       unsigned nonFragileABIVersion = 1;
69780b57cec5SDimitry Andric #else
69790b57cec5SDimitry Andric       unsigned nonFragileABIVersion = 2;
69800b57cec5SDimitry Andric #endif
69810b57cec5SDimitry Andric 
69820b57cec5SDimitry Andric       if (Arg *abiArg =
69830b57cec5SDimitry Andric               args.getLastArg(options::OPT_fobjc_nonfragile_abi_version_EQ)) {
69840b57cec5SDimitry Andric         StringRef value = abiArg->getValue();
69850b57cec5SDimitry Andric         if (value == "1")
69860b57cec5SDimitry Andric           nonFragileABIVersion = 1;
69870b57cec5SDimitry Andric         else if (value == "2")
69880b57cec5SDimitry Andric           nonFragileABIVersion = 2;
69890b57cec5SDimitry Andric         else
69900b57cec5SDimitry Andric           getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
69910b57cec5SDimitry Andric               << value;
69920b57cec5SDimitry Andric       }
69930b57cec5SDimitry Andric 
69940b57cec5SDimitry Andric       objcABIVersion = 1 + nonFragileABIVersion;
69950b57cec5SDimitry Andric     } else {
69960b57cec5SDimitry Andric       objcABIVersion = 1;
69970b57cec5SDimitry Andric     }
69980b57cec5SDimitry Andric   }
69990b57cec5SDimitry Andric 
70000b57cec5SDimitry Andric   // We don't actually care about the ABI version other than whether
70010b57cec5SDimitry Andric   // it's non-fragile.
70020b57cec5SDimitry Andric   bool isNonFragile = objcABIVersion != 1;
70030b57cec5SDimitry Andric 
70040b57cec5SDimitry Andric   // If we have no runtime argument, ask the toolchain for its default runtime.
70050b57cec5SDimitry Andric   // However, the rewriter only really supports the Mac runtime, so assume that.
70060b57cec5SDimitry Andric   ObjCRuntime runtime;
70070b57cec5SDimitry Andric   if (!runtimeArg) {
70080b57cec5SDimitry Andric     switch (rewriteKind) {
70090b57cec5SDimitry Andric     case RK_None:
70100b57cec5SDimitry Andric       runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
70110b57cec5SDimitry Andric       break;
70120b57cec5SDimitry Andric     case RK_Fragile:
70130b57cec5SDimitry Andric       runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
70140b57cec5SDimitry Andric       break;
70150b57cec5SDimitry Andric     case RK_NonFragile:
70160b57cec5SDimitry Andric       runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
70170b57cec5SDimitry Andric       break;
70180b57cec5SDimitry Andric     }
70190b57cec5SDimitry Andric 
70200b57cec5SDimitry Andric     // -fnext-runtime
70210b57cec5SDimitry Andric   } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
70220b57cec5SDimitry Andric     // On Darwin, make this use the default behavior for the toolchain.
70230b57cec5SDimitry Andric     if (getToolChain().getTriple().isOSDarwin()) {
70240b57cec5SDimitry Andric       runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
70250b57cec5SDimitry Andric 
70260b57cec5SDimitry Andric       // Otherwise, build for a generic macosx port.
70270b57cec5SDimitry Andric     } else {
70280b57cec5SDimitry Andric       runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
70290b57cec5SDimitry Andric     }
70300b57cec5SDimitry Andric 
70310b57cec5SDimitry Andric     // -fgnu-runtime
70320b57cec5SDimitry Andric   } else {
70330b57cec5SDimitry Andric     assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
70340b57cec5SDimitry Andric     // Legacy behaviour is to target the gnustep runtime if we are in
70350b57cec5SDimitry Andric     // non-fragile mode or the GCC runtime in fragile mode.
70360b57cec5SDimitry Andric     if (isNonFragile)
70370b57cec5SDimitry Andric       runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(2, 0));
70380b57cec5SDimitry Andric     else
70390b57cec5SDimitry Andric       runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
70400b57cec5SDimitry Andric   }
70410b57cec5SDimitry Andric 
70425ffd83dbSDimitry Andric   if (llvm::any_of(inputs, [](const InputInfo &input) {
70435ffd83dbSDimitry Andric         return types::isObjC(input.getType());
70445ffd83dbSDimitry Andric       }))
70450b57cec5SDimitry Andric     cmdArgs.push_back(
70460b57cec5SDimitry Andric         args.MakeArgString("-fobjc-runtime=" + runtime.getAsString()));
70470b57cec5SDimitry Andric   return runtime;
70480b57cec5SDimitry Andric }
70490b57cec5SDimitry Andric 
70500b57cec5SDimitry Andric static bool maybeConsumeDash(const std::string &EH, size_t &I) {
70510b57cec5SDimitry Andric   bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-');
70520b57cec5SDimitry Andric   I += HaveDash;
70530b57cec5SDimitry Andric   return !HaveDash;
70540b57cec5SDimitry Andric }
70550b57cec5SDimitry Andric 
70560b57cec5SDimitry Andric namespace {
70570b57cec5SDimitry Andric struct EHFlags {
70580b57cec5SDimitry Andric   bool Synch = false;
70590b57cec5SDimitry Andric   bool Asynch = false;
70600b57cec5SDimitry Andric   bool NoUnwindC = false;
70610b57cec5SDimitry Andric };
70620b57cec5SDimitry Andric } // end anonymous namespace
70630b57cec5SDimitry Andric 
70640b57cec5SDimitry Andric /// /EH controls whether to run destructor cleanups when exceptions are
70650b57cec5SDimitry Andric /// thrown.  There are three modifiers:
70660b57cec5SDimitry Andric /// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions.
70670b57cec5SDimitry Andric /// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions.
70680b57cec5SDimitry Andric ///      The 'a' modifier is unimplemented and fundamentally hard in LLVM IR.
70690b57cec5SDimitry Andric /// - c: Assume that extern "C" functions are implicitly nounwind.
70700b57cec5SDimitry Andric /// The default is /EHs-c-, meaning cleanups are disabled.
70710b57cec5SDimitry Andric static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) {
70720b57cec5SDimitry Andric   EHFlags EH;
70730b57cec5SDimitry Andric 
70740b57cec5SDimitry Andric   std::vector<std::string> EHArgs =
70750b57cec5SDimitry Andric       Args.getAllArgValues(options::OPT__SLASH_EH);
70760b57cec5SDimitry Andric   for (auto EHVal : EHArgs) {
70770b57cec5SDimitry Andric     for (size_t I = 0, E = EHVal.size(); I != E; ++I) {
70780b57cec5SDimitry Andric       switch (EHVal[I]) {
70790b57cec5SDimitry Andric       case 'a':
70800b57cec5SDimitry Andric         EH.Asynch = maybeConsumeDash(EHVal, I);
70810b57cec5SDimitry Andric         if (EH.Asynch)
70820b57cec5SDimitry Andric           EH.Synch = false;
70830b57cec5SDimitry Andric         continue;
70840b57cec5SDimitry Andric       case 'c':
70850b57cec5SDimitry Andric         EH.NoUnwindC = maybeConsumeDash(EHVal, I);
70860b57cec5SDimitry Andric         continue;
70870b57cec5SDimitry Andric       case 's':
70880b57cec5SDimitry Andric         EH.Synch = maybeConsumeDash(EHVal, I);
70890b57cec5SDimitry Andric         if (EH.Synch)
70900b57cec5SDimitry Andric           EH.Asynch = false;
70910b57cec5SDimitry Andric         continue;
70920b57cec5SDimitry Andric       default:
70930b57cec5SDimitry Andric         break;
70940b57cec5SDimitry Andric       }
70950b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_invalid_value) << "/EH" << EHVal;
70960b57cec5SDimitry Andric       break;
70970b57cec5SDimitry Andric     }
70980b57cec5SDimitry Andric   }
70990b57cec5SDimitry Andric   // The /GX, /GX- flags are only processed if there are not /EH flags.
71000b57cec5SDimitry Andric   // The default is that /GX is not specified.
71010b57cec5SDimitry Andric   if (EHArgs.empty() &&
71020b57cec5SDimitry Andric       Args.hasFlag(options::OPT__SLASH_GX, options::OPT__SLASH_GX_,
71030b57cec5SDimitry Andric                    /*Default=*/false)) {
71040b57cec5SDimitry Andric     EH.Synch = true;
71050b57cec5SDimitry Andric     EH.NoUnwindC = true;
71060b57cec5SDimitry Andric   }
71070b57cec5SDimitry Andric 
71080b57cec5SDimitry Andric   return EH;
71090b57cec5SDimitry Andric }
71100b57cec5SDimitry Andric 
71110b57cec5SDimitry Andric void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
71120b57cec5SDimitry Andric                            ArgStringList &CmdArgs,
71130b57cec5SDimitry Andric                            codegenoptions::DebugInfoKind *DebugInfoKind,
71140b57cec5SDimitry Andric                            bool *EmitCodeView) const {
71150b57cec5SDimitry Andric   unsigned RTOptionID = options::OPT__SLASH_MT;
71165ffd83dbSDimitry Andric   bool isNVPTX = getToolChain().getTriple().isNVPTX();
71170b57cec5SDimitry Andric 
71180b57cec5SDimitry Andric   if (Args.hasArg(options::OPT__SLASH_LDd))
71190b57cec5SDimitry Andric     // The /LDd option implies /MTd. The dependent lib part can be overridden,
71200b57cec5SDimitry Andric     // but defining _DEBUG is sticky.
71210b57cec5SDimitry Andric     RTOptionID = options::OPT__SLASH_MTd;
71220b57cec5SDimitry Andric 
71230b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group))
71240b57cec5SDimitry Andric     RTOptionID = A->getOption().getID();
71250b57cec5SDimitry Andric 
71260b57cec5SDimitry Andric   StringRef FlagForCRT;
71270b57cec5SDimitry Andric   switch (RTOptionID) {
71280b57cec5SDimitry Andric   case options::OPT__SLASH_MD:
71290b57cec5SDimitry Andric     if (Args.hasArg(options::OPT__SLASH_LDd))
71300b57cec5SDimitry Andric       CmdArgs.push_back("-D_DEBUG");
71310b57cec5SDimitry Andric     CmdArgs.push_back("-D_MT");
71320b57cec5SDimitry Andric     CmdArgs.push_back("-D_DLL");
71330b57cec5SDimitry Andric     FlagForCRT = "--dependent-lib=msvcrt";
71340b57cec5SDimitry Andric     break;
71350b57cec5SDimitry Andric   case options::OPT__SLASH_MDd:
71360b57cec5SDimitry Andric     CmdArgs.push_back("-D_DEBUG");
71370b57cec5SDimitry Andric     CmdArgs.push_back("-D_MT");
71380b57cec5SDimitry Andric     CmdArgs.push_back("-D_DLL");
71390b57cec5SDimitry Andric     FlagForCRT = "--dependent-lib=msvcrtd";
71400b57cec5SDimitry Andric     break;
71410b57cec5SDimitry Andric   case options::OPT__SLASH_MT:
71420b57cec5SDimitry Andric     if (Args.hasArg(options::OPT__SLASH_LDd))
71430b57cec5SDimitry Andric       CmdArgs.push_back("-D_DEBUG");
71440b57cec5SDimitry Andric     CmdArgs.push_back("-D_MT");
71450b57cec5SDimitry Andric     CmdArgs.push_back("-flto-visibility-public-std");
71460b57cec5SDimitry Andric     FlagForCRT = "--dependent-lib=libcmt";
71470b57cec5SDimitry Andric     break;
71480b57cec5SDimitry Andric   case options::OPT__SLASH_MTd:
71490b57cec5SDimitry Andric     CmdArgs.push_back("-D_DEBUG");
71500b57cec5SDimitry Andric     CmdArgs.push_back("-D_MT");
71510b57cec5SDimitry Andric     CmdArgs.push_back("-flto-visibility-public-std");
71520b57cec5SDimitry Andric     FlagForCRT = "--dependent-lib=libcmtd";
71530b57cec5SDimitry Andric     break;
71540b57cec5SDimitry Andric   default:
71550b57cec5SDimitry Andric     llvm_unreachable("Unexpected option ID.");
71560b57cec5SDimitry Andric   }
71570b57cec5SDimitry Andric 
71580b57cec5SDimitry Andric   if (Args.hasArg(options::OPT__SLASH_Zl)) {
71590b57cec5SDimitry Andric     CmdArgs.push_back("-D_VC_NODEFAULTLIB");
71600b57cec5SDimitry Andric   } else {
71610b57cec5SDimitry Andric     CmdArgs.push_back(FlagForCRT.data());
71620b57cec5SDimitry Andric 
71630b57cec5SDimitry Andric     // This provides POSIX compatibility (maps 'open' to '_open'), which most
71640b57cec5SDimitry Andric     // users want.  The /Za flag to cl.exe turns this off, but it's not
71650b57cec5SDimitry Andric     // implemented in clang.
71660b57cec5SDimitry Andric     CmdArgs.push_back("--dependent-lib=oldnames");
71670b57cec5SDimitry Andric   }
71680b57cec5SDimitry Andric 
71695ffd83dbSDimitry Andric   if (Arg *ShowIncludes =
71705ffd83dbSDimitry Andric           Args.getLastArg(options::OPT__SLASH_showIncludes,
71715ffd83dbSDimitry Andric                           options::OPT__SLASH_showIncludes_user)) {
71725ffd83dbSDimitry Andric     CmdArgs.push_back("--show-includes");
71735ffd83dbSDimitry Andric     if (ShowIncludes->getOption().matches(options::OPT__SLASH_showIncludes))
71745ffd83dbSDimitry Andric       CmdArgs.push_back("-sys-header-deps");
71755ffd83dbSDimitry Andric   }
71760b57cec5SDimitry Andric 
71770b57cec5SDimitry Andric   // This controls whether or not we emit RTTI data for polymorphic types.
71780b57cec5SDimitry Andric   if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,
71790b57cec5SDimitry Andric                    /*Default=*/false))
71800b57cec5SDimitry Andric     CmdArgs.push_back("-fno-rtti-data");
71810b57cec5SDimitry Andric 
71820b57cec5SDimitry Andric   // This controls whether or not we emit stack-protector instrumentation.
71830b57cec5SDimitry Andric   // In MSVC, Buffer Security Check (/GS) is on by default.
71845ffd83dbSDimitry Andric   if (!isNVPTX && Args.hasFlag(options::OPT__SLASH_GS, options::OPT__SLASH_GS_,
71850b57cec5SDimitry Andric                                /*Default=*/true)) {
71860b57cec5SDimitry Andric     CmdArgs.push_back("-stack-protector");
71870b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Twine(LangOptions::SSPStrong)));
71880b57cec5SDimitry Andric   }
71890b57cec5SDimitry Andric 
7190e8d8bef9SDimitry Andric   // Emit CodeView if -Z7 or -gline-tables-only are present.
7191e8d8bef9SDimitry Andric   if (Arg *DebugInfoArg = Args.getLastArg(options::OPT__SLASH_Z7,
71920b57cec5SDimitry Andric                                           options::OPT_gline_tables_only)) {
71930b57cec5SDimitry Andric     *EmitCodeView = true;
71940b57cec5SDimitry Andric     if (DebugInfoArg->getOption().matches(options::OPT__SLASH_Z7))
7195fe6060f1SDimitry Andric       *DebugInfoKind = codegenoptions::DebugInfoConstructor;
71960b57cec5SDimitry Andric     else
71970b57cec5SDimitry Andric       *DebugInfoKind = codegenoptions::DebugLineTablesOnly;
71980b57cec5SDimitry Andric   } else {
71990b57cec5SDimitry Andric     *EmitCodeView = false;
72000b57cec5SDimitry Andric   }
72010b57cec5SDimitry Andric 
72020b57cec5SDimitry Andric   const Driver &D = getToolChain().getDriver();
72030b57cec5SDimitry Andric   EHFlags EH = parseClangCLEHFlags(D, Args);
72045ffd83dbSDimitry Andric   if (!isNVPTX && (EH.Synch || EH.Asynch)) {
72050b57cec5SDimitry Andric     if (types::isCXX(InputType))
72060b57cec5SDimitry Andric       CmdArgs.push_back("-fcxx-exceptions");
72070b57cec5SDimitry Andric     CmdArgs.push_back("-fexceptions");
72080b57cec5SDimitry Andric   }
72090b57cec5SDimitry Andric   if (types::isCXX(InputType) && EH.Synch && EH.NoUnwindC)
72100b57cec5SDimitry Andric     CmdArgs.push_back("-fexternc-nounwind");
72110b57cec5SDimitry Andric 
72120b57cec5SDimitry Andric   // /EP should expand to -E -P.
72130b57cec5SDimitry Andric   if (Args.hasArg(options::OPT__SLASH_EP)) {
72140b57cec5SDimitry Andric     CmdArgs.push_back("-E");
72150b57cec5SDimitry Andric     CmdArgs.push_back("-P");
72160b57cec5SDimitry Andric   }
72170b57cec5SDimitry Andric 
72180b57cec5SDimitry Andric   unsigned VolatileOptionID;
7219480093f4SDimitry Andric   if (getToolChain().getTriple().isX86())
72200b57cec5SDimitry Andric     VolatileOptionID = options::OPT__SLASH_volatile_ms;
72210b57cec5SDimitry Andric   else
72220b57cec5SDimitry Andric     VolatileOptionID = options::OPT__SLASH_volatile_iso;
72230b57cec5SDimitry Andric 
72240b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT__SLASH_volatile_Group))
72250b57cec5SDimitry Andric     VolatileOptionID = A->getOption().getID();
72260b57cec5SDimitry Andric 
72270b57cec5SDimitry Andric   if (VolatileOptionID == options::OPT__SLASH_volatile_ms)
72280b57cec5SDimitry Andric     CmdArgs.push_back("-fms-volatile");
72290b57cec5SDimitry Andric 
72300b57cec5SDimitry Andric  if (Args.hasFlag(options::OPT__SLASH_Zc_dllexportInlines_,
72310b57cec5SDimitry Andric                   options::OPT__SLASH_Zc_dllexportInlines,
72320b57cec5SDimitry Andric                   false)) {
72330b57cec5SDimitry Andric   CmdArgs.push_back("-fno-dllexport-inlines");
72340b57cec5SDimitry Andric  }
72350b57cec5SDimitry Andric 
72360b57cec5SDimitry Andric   Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg);
72370b57cec5SDimitry Andric   Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb);
72380b57cec5SDimitry Andric   if (MostGeneralArg && BestCaseArg)
72390b57cec5SDimitry Andric     D.Diag(clang::diag::err_drv_argument_not_allowed_with)
72400b57cec5SDimitry Andric         << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args);
72410b57cec5SDimitry Andric 
72420b57cec5SDimitry Andric   if (MostGeneralArg) {
72430b57cec5SDimitry Andric     Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms);
72440b57cec5SDimitry Andric     Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm);
72450b57cec5SDimitry Andric     Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv);
72460b57cec5SDimitry Andric 
72470b57cec5SDimitry Andric     Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg;
72480b57cec5SDimitry Andric     Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg;
72490b57cec5SDimitry Andric     if (FirstConflict && SecondConflict && FirstConflict != SecondConflict)
72500b57cec5SDimitry Andric       D.Diag(clang::diag::err_drv_argument_not_allowed_with)
72510b57cec5SDimitry Andric           << FirstConflict->getAsString(Args)
72520b57cec5SDimitry Andric           << SecondConflict->getAsString(Args);
72530b57cec5SDimitry Andric 
72540b57cec5SDimitry Andric     if (SingleArg)
72550b57cec5SDimitry Andric       CmdArgs.push_back("-fms-memptr-rep=single");
72560b57cec5SDimitry Andric     else if (MultipleArg)
72570b57cec5SDimitry Andric       CmdArgs.push_back("-fms-memptr-rep=multiple");
72580b57cec5SDimitry Andric     else
72590b57cec5SDimitry Andric       CmdArgs.push_back("-fms-memptr-rep=virtual");
72600b57cec5SDimitry Andric   }
72610b57cec5SDimitry Andric 
72620b57cec5SDimitry Andric   // Parse the default calling convention options.
72630b57cec5SDimitry Andric   if (Arg *CCArg =
72640b57cec5SDimitry Andric           Args.getLastArg(options::OPT__SLASH_Gd, options::OPT__SLASH_Gr,
72650b57cec5SDimitry Andric                           options::OPT__SLASH_Gz, options::OPT__SLASH_Gv,
72660b57cec5SDimitry Andric                           options::OPT__SLASH_Gregcall)) {
72670b57cec5SDimitry Andric     unsigned DCCOptId = CCArg->getOption().getID();
72680b57cec5SDimitry Andric     const char *DCCFlag = nullptr;
72695ffd83dbSDimitry Andric     bool ArchSupported = !isNVPTX;
72700b57cec5SDimitry Andric     llvm::Triple::ArchType Arch = getToolChain().getArch();
72710b57cec5SDimitry Andric     switch (DCCOptId) {
72720b57cec5SDimitry Andric     case options::OPT__SLASH_Gd:
72730b57cec5SDimitry Andric       DCCFlag = "-fdefault-calling-conv=cdecl";
72740b57cec5SDimitry Andric       break;
72750b57cec5SDimitry Andric     case options::OPT__SLASH_Gr:
72760b57cec5SDimitry Andric       ArchSupported = Arch == llvm::Triple::x86;
72770b57cec5SDimitry Andric       DCCFlag = "-fdefault-calling-conv=fastcall";
72780b57cec5SDimitry Andric       break;
72790b57cec5SDimitry Andric     case options::OPT__SLASH_Gz:
72800b57cec5SDimitry Andric       ArchSupported = Arch == llvm::Triple::x86;
72810b57cec5SDimitry Andric       DCCFlag = "-fdefault-calling-conv=stdcall";
72820b57cec5SDimitry Andric       break;
72830b57cec5SDimitry Andric     case options::OPT__SLASH_Gv:
72840b57cec5SDimitry Andric       ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64;
72850b57cec5SDimitry Andric       DCCFlag = "-fdefault-calling-conv=vectorcall";
72860b57cec5SDimitry Andric       break;
72870b57cec5SDimitry Andric     case options::OPT__SLASH_Gregcall:
72880b57cec5SDimitry Andric       ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64;
72890b57cec5SDimitry Andric       DCCFlag = "-fdefault-calling-conv=regcall";
72900b57cec5SDimitry Andric       break;
72910b57cec5SDimitry Andric     }
72920b57cec5SDimitry Andric 
72930b57cec5SDimitry Andric     // MSVC doesn't warn if /Gr or /Gz is used on x64, so we don't either.
72940b57cec5SDimitry Andric     if (ArchSupported && DCCFlag)
72950b57cec5SDimitry Andric       CmdArgs.push_back(DCCFlag);
72960b57cec5SDimitry Andric   }
72970b57cec5SDimitry Andric 
72980b57cec5SDimitry Andric   Args.AddLastArg(CmdArgs, options::OPT_vtordisp_mode_EQ);
72990b57cec5SDimitry Andric 
73000b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) {
73010b57cec5SDimitry Andric     CmdArgs.push_back("-fdiagnostics-format");
73020b57cec5SDimitry Andric     CmdArgs.push_back("msvc");
73030b57cec5SDimitry Andric   }
73040b57cec5SDimitry Andric 
73050b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT__SLASH_guard)) {
7306480093f4SDimitry Andric     StringRef GuardArgs = A->getValue();
7307fe6060f1SDimitry Andric     // The only valid options are "cf", "cf,nochecks", "cf-", "ehcont" and
7308fe6060f1SDimitry Andric     // "ehcont-".
7309fe6060f1SDimitry Andric     if (GuardArgs.equals_insensitive("cf")) {
7310480093f4SDimitry Andric       // Emit CFG instrumentation and the table of address-taken functions.
73110b57cec5SDimitry Andric       CmdArgs.push_back("-cfguard");
7312fe6060f1SDimitry Andric     } else if (GuardArgs.equals_insensitive("cf,nochecks")) {
7313480093f4SDimitry Andric       // Emit only the table of address-taken functions.
7314480093f4SDimitry Andric       CmdArgs.push_back("-cfguard-no-checks");
7315fe6060f1SDimitry Andric     } else if (GuardArgs.equals_insensitive("ehcont")) {
7316fe6060f1SDimitry Andric       // Emit EH continuation table.
7317fe6060f1SDimitry Andric       CmdArgs.push_back("-ehcontguard");
7318fe6060f1SDimitry Andric     } else if (GuardArgs.equals_insensitive("cf-") ||
7319fe6060f1SDimitry Andric                GuardArgs.equals_insensitive("ehcont-")) {
7320480093f4SDimitry Andric       // Do nothing, but we might want to emit a security warning in future.
7321480093f4SDimitry Andric     } else {
7322480093f4SDimitry Andric       D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << GuardArgs;
7323480093f4SDimitry Andric     }
73240b57cec5SDimitry Andric   }
73250b57cec5SDimitry Andric }
73260b57cec5SDimitry Andric 
73270b57cec5SDimitry Andric const char *Clang::getBaseInputName(const ArgList &Args,
73280b57cec5SDimitry Andric                                     const InputInfo &Input) {
73290b57cec5SDimitry Andric   return Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput()));
73300b57cec5SDimitry Andric }
73310b57cec5SDimitry Andric 
73320b57cec5SDimitry Andric const char *Clang::getBaseInputStem(const ArgList &Args,
73330b57cec5SDimitry Andric                                     const InputInfoList &Inputs) {
73340b57cec5SDimitry Andric   const char *Str = getBaseInputName(Args, Inputs[0]);
73350b57cec5SDimitry Andric 
73360b57cec5SDimitry Andric   if (const char *End = strrchr(Str, '.'))
73370b57cec5SDimitry Andric     return Args.MakeArgString(std::string(Str, End));
73380b57cec5SDimitry Andric 
73390b57cec5SDimitry Andric   return Str;
73400b57cec5SDimitry Andric }
73410b57cec5SDimitry Andric 
73420b57cec5SDimitry Andric const char *Clang::getDependencyFileName(const ArgList &Args,
73430b57cec5SDimitry Andric                                          const InputInfoList &Inputs) {
73440b57cec5SDimitry Andric   // FIXME: Think about this more.
73450b57cec5SDimitry Andric 
73460b57cec5SDimitry Andric   if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
7347a7dea167SDimitry Andric     SmallString<128> OutputFilename(OutputOpt->getValue());
7348a7dea167SDimitry Andric     llvm::sys::path::replace_extension(OutputFilename, llvm::Twine('d'));
7349a7dea167SDimitry Andric     return Args.MakeArgString(OutputFilename);
73500b57cec5SDimitry Andric   }
7351a7dea167SDimitry Andric 
7352a7dea167SDimitry Andric   return Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".d");
73530b57cec5SDimitry Andric }
73540b57cec5SDimitry Andric 
73550b57cec5SDimitry Andric // Begin ClangAs
73560b57cec5SDimitry Andric 
73570b57cec5SDimitry Andric void ClangAs::AddMIPSTargetArgs(const ArgList &Args,
73580b57cec5SDimitry Andric                                 ArgStringList &CmdArgs) const {
73590b57cec5SDimitry Andric   StringRef CPUName;
73600b57cec5SDimitry Andric   StringRef ABIName;
73610b57cec5SDimitry Andric   const llvm::Triple &Triple = getToolChain().getTriple();
73620b57cec5SDimitry Andric   mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
73630b57cec5SDimitry Andric 
73640b57cec5SDimitry Andric   CmdArgs.push_back("-target-abi");
73650b57cec5SDimitry Andric   CmdArgs.push_back(ABIName.data());
73660b57cec5SDimitry Andric }
73670b57cec5SDimitry Andric 
73680b57cec5SDimitry Andric void ClangAs::AddX86TargetArgs(const ArgList &Args,
73690b57cec5SDimitry Andric                                ArgStringList &CmdArgs) const {
73705ffd83dbSDimitry Andric   addX86AlignBranchArgs(getToolChain().getDriver(), Args, CmdArgs,
73715ffd83dbSDimitry Andric                         /*IsLTO=*/false);
7372480093f4SDimitry Andric 
73730b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
73740b57cec5SDimitry Andric     StringRef Value = A->getValue();
73750b57cec5SDimitry Andric     if (Value == "intel" || Value == "att") {
73760b57cec5SDimitry Andric       CmdArgs.push_back("-mllvm");
73770b57cec5SDimitry Andric       CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
73780b57cec5SDimitry Andric     } else {
73790b57cec5SDimitry Andric       getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
73800b57cec5SDimitry Andric           << A->getOption().getName() << Value;
73810b57cec5SDimitry Andric     }
73820b57cec5SDimitry Andric   }
73830b57cec5SDimitry Andric }
73840b57cec5SDimitry Andric 
73850b57cec5SDimitry Andric void ClangAs::AddRISCVTargetArgs(const ArgList &Args,
73860b57cec5SDimitry Andric                                ArgStringList &CmdArgs) const {
73870b57cec5SDimitry Andric   const llvm::Triple &Triple = getToolChain().getTriple();
73880b57cec5SDimitry Andric   StringRef ABIName = riscv::getRISCVABI(Args, Triple);
73890b57cec5SDimitry Andric 
73900b57cec5SDimitry Andric   CmdArgs.push_back("-target-abi");
73910b57cec5SDimitry Andric   CmdArgs.push_back(ABIName.data());
73920b57cec5SDimitry Andric }
73930b57cec5SDimitry Andric 
73940b57cec5SDimitry Andric void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
73950b57cec5SDimitry Andric                            const InputInfo &Output, const InputInfoList &Inputs,
73960b57cec5SDimitry Andric                            const ArgList &Args,
73970b57cec5SDimitry Andric                            const char *LinkingOutput) const {
73980b57cec5SDimitry Andric   ArgStringList CmdArgs;
73990b57cec5SDimitry Andric 
74000b57cec5SDimitry Andric   assert(Inputs.size() == 1 && "Unexpected number of inputs.");
74010b57cec5SDimitry Andric   const InputInfo &Input = Inputs[0];
74020b57cec5SDimitry Andric 
74030b57cec5SDimitry Andric   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
74040b57cec5SDimitry Andric   const std::string &TripleStr = Triple.getTriple();
74050b57cec5SDimitry Andric   const auto &D = getToolChain().getDriver();
74060b57cec5SDimitry Andric 
74070b57cec5SDimitry Andric   // Don't warn about "clang -w -c foo.s"
74080b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_w);
74090b57cec5SDimitry Andric   // and "clang -emit-llvm -c foo.s"
74100b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_emit_llvm);
74110b57cec5SDimitry Andric 
74120b57cec5SDimitry Andric   claimNoWarnArgs(Args);
74130b57cec5SDimitry Andric 
74140b57cec5SDimitry Andric   // Invoke ourselves in -cc1as mode.
74150b57cec5SDimitry Andric   //
74160b57cec5SDimitry Andric   // FIXME: Implement custom jobs for internal actions.
74170b57cec5SDimitry Andric   CmdArgs.push_back("-cc1as");
74180b57cec5SDimitry Andric 
74190b57cec5SDimitry Andric   // Add the "effective" target triple.
74200b57cec5SDimitry Andric   CmdArgs.push_back("-triple");
74210b57cec5SDimitry Andric   CmdArgs.push_back(Args.MakeArgString(TripleStr));
74220b57cec5SDimitry Andric 
74230b57cec5SDimitry Andric   // Set the output mode, we currently only expect to be used as a real
74240b57cec5SDimitry Andric   // assembler.
74250b57cec5SDimitry Andric   CmdArgs.push_back("-filetype");
74260b57cec5SDimitry Andric   CmdArgs.push_back("obj");
74270b57cec5SDimitry Andric 
74280b57cec5SDimitry Andric   // Set the main file name, so that debug info works even with
74290b57cec5SDimitry Andric   // -save-temps or preprocessed assembly.
74300b57cec5SDimitry Andric   CmdArgs.push_back("-main-file-name");
74310b57cec5SDimitry Andric   CmdArgs.push_back(Clang::getBaseInputName(Args, Input));
74320b57cec5SDimitry Andric 
74330b57cec5SDimitry Andric   // Add the target cpu
74340b57cec5SDimitry Andric   std::string CPU = getCPUName(Args, Triple, /*FromAs*/ true);
74350b57cec5SDimitry Andric   if (!CPU.empty()) {
74360b57cec5SDimitry Andric     CmdArgs.push_back("-target-cpu");
74370b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(CPU));
74380b57cec5SDimitry Andric   }
74390b57cec5SDimitry Andric 
74400b57cec5SDimitry Andric   // Add the target features
74415ffd83dbSDimitry Andric   getTargetFeatures(D, Triple, Args, CmdArgs, true);
74420b57cec5SDimitry Andric 
74430b57cec5SDimitry Andric   // Ignore explicit -force_cpusubtype_ALL option.
74440b57cec5SDimitry Andric   (void)Args.hasArg(options::OPT_force__cpusubtype__ALL);
74450b57cec5SDimitry Andric 
74460b57cec5SDimitry Andric   // Pass along any -I options so we get proper .include search paths.
74470b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_I_Group);
74480b57cec5SDimitry Andric 
74490b57cec5SDimitry Andric   // Determine the original source input.
74500b57cec5SDimitry Andric   const Action *SourceAction = &JA;
74510b57cec5SDimitry Andric   while (SourceAction->getKind() != Action::InputClass) {
74520b57cec5SDimitry Andric     assert(!SourceAction->getInputs().empty() && "unexpected root action!");
74530b57cec5SDimitry Andric     SourceAction = SourceAction->getInputs()[0];
74540b57cec5SDimitry Andric   }
74550b57cec5SDimitry Andric 
74560b57cec5SDimitry Andric   // Forward -g and handle debug info related flags, assuming we are dealing
74570b57cec5SDimitry Andric   // with an actual assembly file.
74580b57cec5SDimitry Andric   bool WantDebug = false;
74590b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_g_Group);
7460fe6060f1SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_g_Group))
74610b57cec5SDimitry Andric     WantDebug = !A->getOption().matches(options::OPT_g0) &&
74620b57cec5SDimitry Andric                 !A->getOption().matches(options::OPT_ggdb0);
7463480093f4SDimitry Andric 
7464fe6060f1SDimitry Andric   unsigned DwarfVersion = ParseDebugDefaultVersion(getToolChain(), Args);
7465fe6060f1SDimitry Andric   if (const Arg *GDwarfN = getDwarfNArg(Args))
7466fe6060f1SDimitry Andric     DwarfVersion = DwarfVersionNum(GDwarfN->getSpelling());
7467480093f4SDimitry Andric 
74680b57cec5SDimitry Andric   if (DwarfVersion == 0)
74690b57cec5SDimitry Andric     DwarfVersion = getToolChain().GetDefaultDwarfVersion();
74700b57cec5SDimitry Andric 
74710b57cec5SDimitry Andric   codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo;
74720b57cec5SDimitry Andric 
74730b57cec5SDimitry Andric   if (SourceAction->getType() == types::TY_Asm ||
74740b57cec5SDimitry Andric       SourceAction->getType() == types::TY_PP_Asm) {
74750b57cec5SDimitry Andric     // You might think that it would be ok to set DebugInfoKind outside of
74760b57cec5SDimitry Andric     // the guard for source type, however there is a test which asserts
74770b57cec5SDimitry Andric     // that some assembler invocation receives no -debug-info-kind,
74780b57cec5SDimitry Andric     // and it's not clear whether that test is just overly restrictive.
7479fe6060f1SDimitry Andric     DebugInfoKind = (WantDebug ? codegenoptions::DebugInfoConstructor
74800b57cec5SDimitry Andric                                : codegenoptions::NoDebugInfo);
74810b57cec5SDimitry Andric     // Add the -fdebug-compilation-dir flag if needed.
7482a7dea167SDimitry Andric     addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS());
74830b57cec5SDimitry Andric 
74840b57cec5SDimitry Andric     addDebugPrefixMapArg(getToolChain().getDriver(), Args, CmdArgs);
74850b57cec5SDimitry Andric 
74860b57cec5SDimitry Andric     // Set the AT_producer to the clang version when using the integrated
74870b57cec5SDimitry Andric     // assembler on assembly source files.
74880b57cec5SDimitry Andric     CmdArgs.push_back("-dwarf-debug-producer");
74890b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
74900b57cec5SDimitry Andric 
74910b57cec5SDimitry Andric     // And pass along -I options
74920b57cec5SDimitry Andric     Args.AddAllArgs(CmdArgs, options::OPT_I);
74930b57cec5SDimitry Andric   }
74940b57cec5SDimitry Andric   RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion,
74950b57cec5SDimitry Andric                           llvm::DebuggerKind::Default);
7496fe6060f1SDimitry Andric   renderDwarfFormat(D, Triple, Args, CmdArgs, DwarfVersion);
74970b57cec5SDimitry Andric   RenderDebugInfoCompressionArgs(Args, CmdArgs, D, getToolChain());
74980b57cec5SDimitry Andric 
74990b57cec5SDimitry Andric 
75000b57cec5SDimitry Andric   // Handle -fPIC et al -- the relocation-model affects the assembler
75010b57cec5SDimitry Andric   // for some targets.
75020b57cec5SDimitry Andric   llvm::Reloc::Model RelocationModel;
75030b57cec5SDimitry Andric   unsigned PICLevel;
75040b57cec5SDimitry Andric   bool IsPIE;
75050b57cec5SDimitry Andric   std::tie(RelocationModel, PICLevel, IsPIE) =
75060b57cec5SDimitry Andric       ParsePICArgs(getToolChain(), Args);
75070b57cec5SDimitry Andric 
75080b57cec5SDimitry Andric   const char *RMName = RelocationModelName(RelocationModel);
75090b57cec5SDimitry Andric   if (RMName) {
75100b57cec5SDimitry Andric     CmdArgs.push_back("-mrelocation-model");
75110b57cec5SDimitry Andric     CmdArgs.push_back(RMName);
75120b57cec5SDimitry Andric   }
75130b57cec5SDimitry Andric 
75140b57cec5SDimitry Andric   // Optionally embed the -cc1as level arguments into the debug info, for build
75150b57cec5SDimitry Andric   // analysis.
75160b57cec5SDimitry Andric   if (getToolChain().UseDwarfDebugFlags()) {
75170b57cec5SDimitry Andric     ArgStringList OriginalArgs;
75180b57cec5SDimitry Andric     for (const auto &Arg : Args)
75190b57cec5SDimitry Andric       Arg->render(Args, OriginalArgs);
75200b57cec5SDimitry Andric 
75210b57cec5SDimitry Andric     SmallString<256> Flags;
75220b57cec5SDimitry Andric     const char *Exec = getToolChain().getDriver().getClangProgramPath();
75235ffd83dbSDimitry Andric     EscapeSpacesAndBackslashes(Exec, Flags);
75240b57cec5SDimitry Andric     for (const char *OriginalArg : OriginalArgs) {
75250b57cec5SDimitry Andric       SmallString<128> EscapedArg;
75260b57cec5SDimitry Andric       EscapeSpacesAndBackslashes(OriginalArg, EscapedArg);
75270b57cec5SDimitry Andric       Flags += " ";
75280b57cec5SDimitry Andric       Flags += EscapedArg;
75290b57cec5SDimitry Andric     }
75300b57cec5SDimitry Andric     CmdArgs.push_back("-dwarf-debug-flags");
75310b57cec5SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Flags));
75320b57cec5SDimitry Andric   }
75330b57cec5SDimitry Andric 
75340b57cec5SDimitry Andric   // FIXME: Add -static support, once we have it.
75350b57cec5SDimitry Andric 
75360b57cec5SDimitry Andric   // Add target specific flags.
75370b57cec5SDimitry Andric   switch (getToolChain().getArch()) {
75380b57cec5SDimitry Andric   default:
75390b57cec5SDimitry Andric     break;
75400b57cec5SDimitry Andric 
75410b57cec5SDimitry Andric   case llvm::Triple::mips:
75420b57cec5SDimitry Andric   case llvm::Triple::mipsel:
75430b57cec5SDimitry Andric   case llvm::Triple::mips64:
75440b57cec5SDimitry Andric   case llvm::Triple::mips64el:
75450b57cec5SDimitry Andric     AddMIPSTargetArgs(Args, CmdArgs);
75460b57cec5SDimitry Andric     break;
75470b57cec5SDimitry Andric 
75480b57cec5SDimitry Andric   case llvm::Triple::x86:
75490b57cec5SDimitry Andric   case llvm::Triple::x86_64:
75500b57cec5SDimitry Andric     AddX86TargetArgs(Args, CmdArgs);
75510b57cec5SDimitry Andric     break;
75520b57cec5SDimitry Andric 
75530b57cec5SDimitry Andric   case llvm::Triple::arm:
75540b57cec5SDimitry Andric   case llvm::Triple::armeb:
75550b57cec5SDimitry Andric   case llvm::Triple::thumb:
75560b57cec5SDimitry Andric   case llvm::Triple::thumbeb:
75570b57cec5SDimitry Andric     // This isn't in AddARMTargetArgs because we want to do this for assembly
75580b57cec5SDimitry Andric     // only, not C/C++.
75590b57cec5SDimitry Andric     if (Args.hasFlag(options::OPT_mdefault_build_attributes,
75600b57cec5SDimitry Andric                      options::OPT_mno_default_build_attributes, true)) {
75610b57cec5SDimitry Andric         CmdArgs.push_back("-mllvm");
75620b57cec5SDimitry Andric         CmdArgs.push_back("-arm-add-build-attributes");
75630b57cec5SDimitry Andric     }
75640b57cec5SDimitry Andric     break;
75650b57cec5SDimitry Andric 
7566e8d8bef9SDimitry Andric   case llvm::Triple::aarch64:
7567e8d8bef9SDimitry Andric   case llvm::Triple::aarch64_32:
7568e8d8bef9SDimitry Andric   case llvm::Triple::aarch64_be:
7569e8d8bef9SDimitry Andric     if (Args.hasArg(options::OPT_mmark_bti_property)) {
7570e8d8bef9SDimitry Andric       CmdArgs.push_back("-mllvm");
7571e8d8bef9SDimitry Andric       CmdArgs.push_back("-aarch64-mark-bti-property");
7572e8d8bef9SDimitry Andric     }
7573e8d8bef9SDimitry Andric     break;
7574e8d8bef9SDimitry Andric 
75750b57cec5SDimitry Andric   case llvm::Triple::riscv32:
75760b57cec5SDimitry Andric   case llvm::Triple::riscv64:
75770b57cec5SDimitry Andric     AddRISCVTargetArgs(Args, CmdArgs);
75780b57cec5SDimitry Andric     break;
75790b57cec5SDimitry Andric   }
75800b57cec5SDimitry Andric 
75810b57cec5SDimitry Andric   // Consume all the warning flags. Usually this would be handled more
75820b57cec5SDimitry Andric   // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as
75830b57cec5SDimitry Andric   // doesn't handle that so rather than warning about unused flags that are
75840b57cec5SDimitry Andric   // actually used, we'll lie by omission instead.
75850b57cec5SDimitry Andric   // FIXME: Stop lying and consume only the appropriate driver flags
75860b57cec5SDimitry Andric   Args.ClaimAllArgs(options::OPT_W_Group);
75870b57cec5SDimitry Andric 
75880b57cec5SDimitry Andric   CollectArgsForIntegratedAssembler(C, Args, CmdArgs,
75890b57cec5SDimitry Andric                                     getToolChain().getDriver());
75900b57cec5SDimitry Andric 
75910b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
75920b57cec5SDimitry Andric 
75930b57cec5SDimitry Andric   assert(Output.isFilename() && "Unexpected lipo output.");
75940b57cec5SDimitry Andric   CmdArgs.push_back("-o");
75950b57cec5SDimitry Andric   CmdArgs.push_back(Output.getFilename());
75960b57cec5SDimitry Andric 
75970b57cec5SDimitry Andric   const llvm::Triple &T = getToolChain().getTriple();
75980b57cec5SDimitry Andric   Arg *A;
75990b57cec5SDimitry Andric   if (getDebugFissionKind(D, Args, A) == DwarfFissionKind::Split &&
76000b57cec5SDimitry Andric       T.isOSBinFormatELF()) {
76010b57cec5SDimitry Andric     CmdArgs.push_back("-split-dwarf-output");
7602e8d8bef9SDimitry Andric     CmdArgs.push_back(SplitDebugName(JA, Args, Input, Output));
76030b57cec5SDimitry Andric   }
76040b57cec5SDimitry Andric 
7605e8d8bef9SDimitry Andric   if (Triple.isAMDGPU())
7606e8d8bef9SDimitry Andric     handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs);
7607e8d8bef9SDimitry Andric 
76080b57cec5SDimitry Andric   assert(Input.isFilename() && "Invalid input.");
76090b57cec5SDimitry Andric   CmdArgs.push_back(Input.getFilename());
76100b57cec5SDimitry Andric 
76110b57cec5SDimitry Andric   const char *Exec = getToolChain().getDriver().getClangProgramPath();
7612e8d8bef9SDimitry Andric   if (D.CC1Main && !D.CCGenDiagnostics) {
7613e8d8bef9SDimitry Andric     // Invoke cc1as directly in this process.
7614e8d8bef9SDimitry Andric     C.addCommand(std::make_unique<CC1Command>(JA, *this,
7615e8d8bef9SDimitry Andric                                               ResponseFileSupport::AtFileUTF8(),
7616e8d8bef9SDimitry Andric                                               Exec, CmdArgs, Inputs, Output));
7617e8d8bef9SDimitry Andric   } else {
7618e8d8bef9SDimitry Andric     C.addCommand(std::make_unique<Command>(JA, *this,
7619e8d8bef9SDimitry Andric                                            ResponseFileSupport::AtFileUTF8(),
7620e8d8bef9SDimitry Andric                                            Exec, CmdArgs, Inputs, Output));
7621e8d8bef9SDimitry Andric   }
76220b57cec5SDimitry Andric }
76230b57cec5SDimitry Andric 
76240b57cec5SDimitry Andric // Begin OffloadBundler
76250b57cec5SDimitry Andric 
76260b57cec5SDimitry Andric void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA,
76270b57cec5SDimitry Andric                                   const InputInfo &Output,
76280b57cec5SDimitry Andric                                   const InputInfoList &Inputs,
76290b57cec5SDimitry Andric                                   const llvm::opt::ArgList &TCArgs,
76300b57cec5SDimitry Andric                                   const char *LinkingOutput) const {
76310b57cec5SDimitry Andric   // The version with only one output is expected to refer to a bundling job.
76320b57cec5SDimitry Andric   assert(isa<OffloadBundlingJobAction>(JA) && "Expecting bundling job!");
76330b57cec5SDimitry Andric 
76340b57cec5SDimitry Andric   // The bundling command looks like this:
76350b57cec5SDimitry Andric   // clang-offload-bundler -type=bc
76360b57cec5SDimitry Andric   //   -targets=host-triple,openmp-triple1,openmp-triple2
76370b57cec5SDimitry Andric   //   -outputs=input_file
76380b57cec5SDimitry Andric   //   -inputs=unbundle_file_host,unbundle_file_tgt1,unbundle_file_tgt2"
76390b57cec5SDimitry Andric 
76400b57cec5SDimitry Andric   ArgStringList CmdArgs;
76410b57cec5SDimitry Andric 
76420b57cec5SDimitry Andric   // Get the type.
76430b57cec5SDimitry Andric   CmdArgs.push_back(TCArgs.MakeArgString(
76440b57cec5SDimitry Andric       Twine("-type=") + types::getTypeTempSuffix(Output.getType())));
76450b57cec5SDimitry Andric 
76460b57cec5SDimitry Andric   assert(JA.getInputs().size() == Inputs.size() &&
76470b57cec5SDimitry Andric          "Not have inputs for all dependence actions??");
76480b57cec5SDimitry Andric 
76490b57cec5SDimitry Andric   // Get the targets.
76500b57cec5SDimitry Andric   SmallString<128> Triples;
76510b57cec5SDimitry Andric   Triples += "-targets=";
76520b57cec5SDimitry Andric   for (unsigned I = 0; I < Inputs.size(); ++I) {
76530b57cec5SDimitry Andric     if (I)
76540b57cec5SDimitry Andric       Triples += ',';
76550b57cec5SDimitry Andric 
76560b57cec5SDimitry Andric     // Find ToolChain for this input.
76570b57cec5SDimitry Andric     Action::OffloadKind CurKind = Action::OFK_Host;
76580b57cec5SDimitry Andric     const ToolChain *CurTC = &getToolChain();
76590b57cec5SDimitry Andric     const Action *CurDep = JA.getInputs()[I];
76600b57cec5SDimitry Andric 
76610b57cec5SDimitry Andric     if (const auto *OA = dyn_cast<OffloadAction>(CurDep)) {
76620b57cec5SDimitry Andric       CurTC = nullptr;
76630b57cec5SDimitry Andric       OA->doOnEachDependence([&](Action *A, const ToolChain *TC, const char *) {
76640b57cec5SDimitry Andric         assert(CurTC == nullptr && "Expected one dependence!");
76650b57cec5SDimitry Andric         CurKind = A->getOffloadingDeviceKind();
76660b57cec5SDimitry Andric         CurTC = TC;
76670b57cec5SDimitry Andric       });
76680b57cec5SDimitry Andric     }
76690b57cec5SDimitry Andric     Triples += Action::GetOffloadKindName(CurKind);
7670fe6060f1SDimitry Andric     Triples += "-";
7671fe6060f1SDimitry Andric     std::string NormalizedTriple = CurTC->getTriple().normalize();
7672fe6060f1SDimitry Andric     Triples += NormalizedTriple;
7673fe6060f1SDimitry Andric 
7674fe6060f1SDimitry Andric     if (CurDep->getOffloadingArch() != nullptr) {
7675fe6060f1SDimitry Andric       // If OffloadArch is present it can only appear as the 6th hypen
7676fe6060f1SDimitry Andric       // sepearated field of Bundle Entry ID. So, pad required number of
7677fe6060f1SDimitry Andric       // hyphens in Triple.
7678fe6060f1SDimitry Andric       for (int i = 4 - StringRef(NormalizedTriple).count("-"); i > 0; i--)
7679fe6060f1SDimitry Andric         Triples += "-";
76800b57cec5SDimitry Andric       Triples += CurDep->getOffloadingArch();
76810b57cec5SDimitry Andric     }
76820b57cec5SDimitry Andric   }
76830b57cec5SDimitry Andric   CmdArgs.push_back(TCArgs.MakeArgString(Triples));
76840b57cec5SDimitry Andric 
76850b57cec5SDimitry Andric   // Get bundled file command.
76860b57cec5SDimitry Andric   CmdArgs.push_back(
76870b57cec5SDimitry Andric       TCArgs.MakeArgString(Twine("-outputs=") + Output.getFilename()));
76880b57cec5SDimitry Andric 
76890b57cec5SDimitry Andric   // Get unbundled files command.
76900b57cec5SDimitry Andric   SmallString<128> UB;
76910b57cec5SDimitry Andric   UB += "-inputs=";
76920b57cec5SDimitry Andric   for (unsigned I = 0; I < Inputs.size(); ++I) {
76930b57cec5SDimitry Andric     if (I)
76940b57cec5SDimitry Andric       UB += ',';
76950b57cec5SDimitry Andric 
76960b57cec5SDimitry Andric     // Find ToolChain for this input.
76970b57cec5SDimitry Andric     const ToolChain *CurTC = &getToolChain();
76980b57cec5SDimitry Andric     if (const auto *OA = dyn_cast<OffloadAction>(JA.getInputs()[I])) {
76990b57cec5SDimitry Andric       CurTC = nullptr;
77000b57cec5SDimitry Andric       OA->doOnEachDependence([&](Action *, const ToolChain *TC, const char *) {
77010b57cec5SDimitry Andric         assert(CurTC == nullptr && "Expected one dependence!");
77020b57cec5SDimitry Andric         CurTC = TC;
77030b57cec5SDimitry Andric       });
77040b57cec5SDimitry Andric     }
77050b57cec5SDimitry Andric     UB += CurTC->getInputFilename(Inputs[I]);
77060b57cec5SDimitry Andric   }
77070b57cec5SDimitry Andric   CmdArgs.push_back(TCArgs.MakeArgString(UB));
77080b57cec5SDimitry Andric 
77090b57cec5SDimitry Andric   // All the inputs are encoded as commands.
7710a7dea167SDimitry Andric   C.addCommand(std::make_unique<Command>(
77115ffd83dbSDimitry Andric       JA, *this, ResponseFileSupport::None(),
77120b57cec5SDimitry Andric       TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
7713e8d8bef9SDimitry Andric       CmdArgs, None, Output));
77140b57cec5SDimitry Andric }
77150b57cec5SDimitry Andric 
77160b57cec5SDimitry Andric void OffloadBundler::ConstructJobMultipleOutputs(
77170b57cec5SDimitry Andric     Compilation &C, const JobAction &JA, const InputInfoList &Outputs,
77180b57cec5SDimitry Andric     const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs,
77190b57cec5SDimitry Andric     const char *LinkingOutput) const {
77200b57cec5SDimitry Andric   // The version with multiple outputs is expected to refer to a unbundling job.
77210b57cec5SDimitry Andric   auto &UA = cast<OffloadUnbundlingJobAction>(JA);
77220b57cec5SDimitry Andric 
77230b57cec5SDimitry Andric   // The unbundling command looks like this:
77240b57cec5SDimitry Andric   // clang-offload-bundler -type=bc
77250b57cec5SDimitry Andric   //   -targets=host-triple,openmp-triple1,openmp-triple2
77260b57cec5SDimitry Andric   //   -inputs=input_file
77270b57cec5SDimitry Andric   //   -outputs=unbundle_file_host,unbundle_file_tgt1,unbundle_file_tgt2"
77280b57cec5SDimitry Andric   //   -unbundle
77290b57cec5SDimitry Andric 
77300b57cec5SDimitry Andric   ArgStringList CmdArgs;
77310b57cec5SDimitry Andric 
77320b57cec5SDimitry Andric   assert(Inputs.size() == 1 && "Expecting to unbundle a single file!");
77330b57cec5SDimitry Andric   InputInfo Input = Inputs.front();
77340b57cec5SDimitry Andric 
77350b57cec5SDimitry Andric   // Get the type.
77360b57cec5SDimitry Andric   CmdArgs.push_back(TCArgs.MakeArgString(
77370b57cec5SDimitry Andric       Twine("-type=") + types::getTypeTempSuffix(Input.getType())));
77380b57cec5SDimitry Andric 
77390b57cec5SDimitry Andric   // Get the targets.
77400b57cec5SDimitry Andric   SmallString<128> Triples;
77410b57cec5SDimitry Andric   Triples += "-targets=";
77420b57cec5SDimitry Andric   auto DepInfo = UA.getDependentActionsInfo();
77430b57cec5SDimitry Andric   for (unsigned I = 0; I < DepInfo.size(); ++I) {
77440b57cec5SDimitry Andric     if (I)
77450b57cec5SDimitry Andric       Triples += ',';
77460b57cec5SDimitry Andric 
77470b57cec5SDimitry Andric     auto &Dep = DepInfo[I];
77480b57cec5SDimitry Andric     Triples += Action::GetOffloadKindName(Dep.DependentOffloadKind);
7749fe6060f1SDimitry Andric     Triples += "-";
7750fe6060f1SDimitry Andric     std::string NormalizedTriple =
7751fe6060f1SDimitry Andric         Dep.DependentToolChain->getTriple().normalize();
7752fe6060f1SDimitry Andric     Triples += NormalizedTriple;
7753fe6060f1SDimitry Andric 
7754fe6060f1SDimitry Andric     if (!Dep.DependentBoundArch.empty()) {
7755fe6060f1SDimitry Andric       // If OffloadArch is present it can only appear as the 6th hypen
7756fe6060f1SDimitry Andric       // sepearated field of Bundle Entry ID. So, pad required number of
7757fe6060f1SDimitry Andric       // hyphens in Triple.
7758fe6060f1SDimitry Andric       for (int i = 4 - StringRef(NormalizedTriple).count("-"); i > 0; i--)
7759fe6060f1SDimitry Andric         Triples += "-";
77600b57cec5SDimitry Andric       Triples += Dep.DependentBoundArch;
77610b57cec5SDimitry Andric     }
77620b57cec5SDimitry Andric   }
77630b57cec5SDimitry Andric 
77640b57cec5SDimitry Andric   CmdArgs.push_back(TCArgs.MakeArgString(Triples));
77650b57cec5SDimitry Andric 
77660b57cec5SDimitry Andric   // Get bundled file command.
77670b57cec5SDimitry Andric   CmdArgs.push_back(
77680b57cec5SDimitry Andric       TCArgs.MakeArgString(Twine("-inputs=") + Input.getFilename()));
77690b57cec5SDimitry Andric 
77700b57cec5SDimitry Andric   // Get unbundled files command.
77710b57cec5SDimitry Andric   SmallString<128> UB;
77720b57cec5SDimitry Andric   UB += "-outputs=";
77730b57cec5SDimitry Andric   for (unsigned I = 0; I < Outputs.size(); ++I) {
77740b57cec5SDimitry Andric     if (I)
77750b57cec5SDimitry Andric       UB += ',';
77760b57cec5SDimitry Andric     UB += DepInfo[I].DependentToolChain->getInputFilename(Outputs[I]);
77770b57cec5SDimitry Andric   }
77780b57cec5SDimitry Andric   CmdArgs.push_back(TCArgs.MakeArgString(UB));
77790b57cec5SDimitry Andric   CmdArgs.push_back("-unbundle");
7780e8d8bef9SDimitry Andric   CmdArgs.push_back("-allow-missing-bundles");
77810b57cec5SDimitry Andric 
77820b57cec5SDimitry Andric   // All the inputs are encoded as commands.
7783a7dea167SDimitry Andric   C.addCommand(std::make_unique<Command>(
77845ffd83dbSDimitry Andric       JA, *this, ResponseFileSupport::None(),
77850b57cec5SDimitry Andric       TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
7786e8d8bef9SDimitry Andric       CmdArgs, None, Outputs));
77870b57cec5SDimitry Andric }
7788a7dea167SDimitry Andric 
7789a7dea167SDimitry Andric void OffloadWrapper::ConstructJob(Compilation &C, const JobAction &JA,
7790a7dea167SDimitry Andric                                   const InputInfo &Output,
7791a7dea167SDimitry Andric                                   const InputInfoList &Inputs,
7792a7dea167SDimitry Andric                                   const ArgList &Args,
7793a7dea167SDimitry Andric                                   const char *LinkingOutput) const {
7794a7dea167SDimitry Andric   ArgStringList CmdArgs;
7795a7dea167SDimitry Andric 
7796a7dea167SDimitry Andric   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
7797a7dea167SDimitry Andric 
7798a7dea167SDimitry Andric   // Add the "effective" target triple.
7799a7dea167SDimitry Andric   CmdArgs.push_back("-target");
7800a7dea167SDimitry Andric   CmdArgs.push_back(Args.MakeArgString(Triple.getTriple()));
7801a7dea167SDimitry Andric 
7802a7dea167SDimitry Andric   // Add the output file name.
7803a7dea167SDimitry Andric   assert(Output.isFilename() && "Invalid output.");
7804a7dea167SDimitry Andric   CmdArgs.push_back("-o");
7805a7dea167SDimitry Andric   CmdArgs.push_back(Output.getFilename());
7806a7dea167SDimitry Andric 
7807a7dea167SDimitry Andric   // Add inputs.
7808a7dea167SDimitry Andric   for (const InputInfo &I : Inputs) {
7809a7dea167SDimitry Andric     assert(I.isFilename() && "Invalid input.");
7810a7dea167SDimitry Andric     CmdArgs.push_back(I.getFilename());
7811a7dea167SDimitry Andric   }
7812a7dea167SDimitry Andric 
7813a7dea167SDimitry Andric   C.addCommand(std::make_unique<Command>(
78145ffd83dbSDimitry Andric       JA, *this, ResponseFileSupport::None(),
7815a7dea167SDimitry Andric       Args.MakeArgString(getToolChain().GetProgramPath(getShortName())),
7816e8d8bef9SDimitry Andric       CmdArgs, Inputs, Output));
7817a7dea167SDimitry Andric }
7818