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" 25349cc55cSDimitry Andric #include "clang/Basic/CLWarnings.h" 260b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h" 27a7dea167SDimitry Andric #include "clang/Basic/CodeGenOptions.h" 280b57cec5SDimitry Andric #include "clang/Basic/LangOptions.h" 290b57cec5SDimitry Andric #include "clang/Basic/ObjCRuntime.h" 300b57cec5SDimitry Andric #include "clang/Basic/Version.h" 310b57cec5SDimitry Andric #include "clang/Driver/Distro.h" 320b57cec5SDimitry Andric #include "clang/Driver/DriverDiagnostic.h" 33fe6060f1SDimitry Andric #include "clang/Driver/InputInfo.h" 340b57cec5SDimitry Andric #include "clang/Driver/Options.h" 350b57cec5SDimitry Andric #include "clang/Driver/SanitizerArgs.h" 3604eeddc0SDimitry Andric #include "clang/Driver/Types.h" 370b57cec5SDimitry Andric #include "clang/Driver/XRayArgs.h" 380b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 390b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 400b57cec5SDimitry Andric #include "llvm/Option/ArgList.h" 410b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h" 425ffd83dbSDimitry Andric #include "llvm/Support/Compiler.h" 430b57cec5SDimitry Andric #include "llvm/Support/Compression.h" 440b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 45e8d8bef9SDimitry Andric #include "llvm/Support/Host.h" 460b57cec5SDimitry Andric #include "llvm/Support/Path.h" 470b57cec5SDimitry Andric #include "llvm/Support/Process.h" 480b57cec5SDimitry Andric #include "llvm/Support/TargetParser.h" 490b57cec5SDimitry Andric #include "llvm/Support/YAMLParser.h" 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric using namespace clang::driver; 520b57cec5SDimitry Andric using namespace clang::driver::tools; 530b57cec5SDimitry Andric using namespace clang; 540b57cec5SDimitry Andric using namespace llvm::opt; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) { 57349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(clang::driver::options::OPT_C, options::OPT_CC, 58349cc55cSDimitry Andric options::OPT_fminimize_whitespace, 59349cc55cSDimitry Andric options::OPT_fno_minimize_whitespace)) { 600b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_P) && 610b57cec5SDimitry Andric !Args.hasArg(options::OPT__SLASH_EP) && !D.CCCIsCPP()) { 620b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 630b57cec5SDimitry Andric << A->getBaseArg().getAsString(Args) 640b57cec5SDimitry Andric << (D.IsCLMode() ? "/E, /P or /EP" : "-E"); 650b57cec5SDimitry Andric } 660b57cec5SDimitry Andric } 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) { 700b57cec5SDimitry Andric // In gcc, only ARM checks this, but it seems reasonable to check universally. 710b57cec5SDimitry Andric if (Args.hasArg(options::OPT_static)) 720b57cec5SDimitry Andric if (const Arg *A = 730b57cec5SDimitry Andric Args.getLastArg(options::OPT_dynamic, options::OPT_mdynamic_no_pic)) 740b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args) 750b57cec5SDimitry Andric << "-static"; 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric // Add backslashes to escape spaces and other backslashes. 790b57cec5SDimitry Andric // This is used for the space-separated argument list specified with 800b57cec5SDimitry Andric // the -dwarf-debug-flags option. 810b57cec5SDimitry Andric static void EscapeSpacesAndBackslashes(const char *Arg, 820b57cec5SDimitry Andric SmallVectorImpl<char> &Res) { 830b57cec5SDimitry Andric for (; *Arg; ++Arg) { 840b57cec5SDimitry Andric switch (*Arg) { 850b57cec5SDimitry Andric default: 860b57cec5SDimitry Andric break; 870b57cec5SDimitry Andric case ' ': 880b57cec5SDimitry Andric case '\\': 890b57cec5SDimitry Andric Res.push_back('\\'); 900b57cec5SDimitry Andric break; 910b57cec5SDimitry Andric } 920b57cec5SDimitry Andric Res.push_back(*Arg); 930b57cec5SDimitry Andric } 940b57cec5SDimitry Andric } 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric // Quote target names for inclusion in GNU Make dependency files. 970b57cec5SDimitry Andric // Only the characters '$', '#', ' ', '\t' are quoted. 980b57cec5SDimitry Andric static void QuoteTarget(StringRef Target, SmallVectorImpl<char> &Res) { 990b57cec5SDimitry Andric for (unsigned i = 0, e = Target.size(); i != e; ++i) { 1000b57cec5SDimitry Andric switch (Target[i]) { 1010b57cec5SDimitry Andric case ' ': 1020b57cec5SDimitry Andric case '\t': 1030b57cec5SDimitry Andric // Escape the preceding backslashes 1040b57cec5SDimitry Andric for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j) 1050b57cec5SDimitry Andric Res.push_back('\\'); 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric // Escape the space/tab 1080b57cec5SDimitry Andric Res.push_back('\\'); 1090b57cec5SDimitry Andric break; 1100b57cec5SDimitry Andric case '$': 1110b57cec5SDimitry Andric Res.push_back('$'); 1120b57cec5SDimitry Andric break; 1130b57cec5SDimitry Andric case '#': 1140b57cec5SDimitry Andric Res.push_back('\\'); 1150b57cec5SDimitry Andric break; 1160b57cec5SDimitry Andric default: 1170b57cec5SDimitry Andric break; 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric Res.push_back(Target[i]); 1210b57cec5SDimitry Andric } 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric /// Apply \a Work on the current tool chain \a RegularToolChain and any other 1250b57cec5SDimitry Andric /// offloading tool chain that is associated with the current action \a JA. 1260b57cec5SDimitry Andric static void 1270b57cec5SDimitry Andric forAllAssociatedToolChains(Compilation &C, const JobAction &JA, 1280b57cec5SDimitry Andric const ToolChain &RegularToolChain, 1290b57cec5SDimitry Andric llvm::function_ref<void(const ToolChain &)> Work) { 1300b57cec5SDimitry Andric // Apply Work on the current/regular tool chain. 1310b57cec5SDimitry Andric Work(RegularToolChain); 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric // Apply Work on all the offloading tool chains associated with the current 1340b57cec5SDimitry Andric // action. 1350b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_Cuda)) 1360b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Cuda>()); 1370b57cec5SDimitry Andric else if (JA.isDeviceOffloading(Action::OFK_Cuda)) 1380b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1390b57cec5SDimitry Andric else if (JA.isHostOffloading(Action::OFK_HIP)) 1400b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_HIP>()); 1410b57cec5SDimitry Andric else if (JA.isDeviceOffloading(Action::OFK_HIP)) 1420b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_OpenMP)) { 1450b57cec5SDimitry Andric auto TCs = C.getOffloadToolChains<Action::OFK_OpenMP>(); 1460b57cec5SDimitry Andric for (auto II = TCs.first, IE = TCs.second; II != IE; ++II) 1470b57cec5SDimitry Andric Work(*II->second); 1480b57cec5SDimitry Andric } else if (JA.isDeviceOffloading(Action::OFK_OpenMP)) 1490b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric // 1520b57cec5SDimitry Andric // TODO: Add support for other offloading programming models here. 1530b57cec5SDimitry Andric // 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric /// This is a helper function for validating the optional refinement step 1570b57cec5SDimitry Andric /// parameter in reciprocal argument strings. Return false if there is an error 1580b57cec5SDimitry Andric /// parsing the refinement step. Otherwise, return true and set the Position 1590b57cec5SDimitry Andric /// of the refinement step in the input string. 1600b57cec5SDimitry Andric static bool getRefinementStep(StringRef In, const Driver &D, 1610b57cec5SDimitry Andric const Arg &A, size_t &Position) { 1620b57cec5SDimitry Andric const char RefinementStepToken = ':'; 1630b57cec5SDimitry Andric Position = In.find(RefinementStepToken); 1640b57cec5SDimitry Andric if (Position != StringRef::npos) { 1650b57cec5SDimitry Andric StringRef Option = A.getOption().getName(); 1660b57cec5SDimitry Andric StringRef RefStep = In.substr(Position + 1); 1670b57cec5SDimitry Andric // Allow exactly one numeric character for the additional refinement 1680b57cec5SDimitry Andric // step parameter. This is reasonable for all currently-supported 1690b57cec5SDimitry Andric // operations and architectures because we would expect that a larger value 1700b57cec5SDimitry Andric // of refinement steps would cause the estimate "optimization" to 1710b57cec5SDimitry Andric // under-perform the native operation. Also, if the estimate does not 1720b57cec5SDimitry Andric // converge quickly, it probably will not ever converge, so further 1730b57cec5SDimitry Andric // refinement steps will not produce a better answer. 1740b57cec5SDimitry Andric if (RefStep.size() != 1) { 1750b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << Option << RefStep; 1760b57cec5SDimitry Andric return false; 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric char RefStepChar = RefStep[0]; 1790b57cec5SDimitry Andric if (RefStepChar < '0' || RefStepChar > '9') { 1800b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << Option << RefStep; 1810b57cec5SDimitry Andric return false; 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric } 1840b57cec5SDimitry Andric return true; 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric /// The -mrecip flag requires processing of many optional parameters. 1880b57cec5SDimitry Andric static void ParseMRecip(const Driver &D, const ArgList &Args, 1890b57cec5SDimitry Andric ArgStringList &OutStrings) { 1900b57cec5SDimitry Andric StringRef DisabledPrefixIn = "!"; 1910b57cec5SDimitry Andric StringRef DisabledPrefixOut = "!"; 1920b57cec5SDimitry Andric StringRef EnabledPrefixOut = ""; 1930b57cec5SDimitry Andric StringRef Out = "-mrecip="; 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_mrecip, options::OPT_mrecip_EQ); 1960b57cec5SDimitry Andric if (!A) 1970b57cec5SDimitry Andric return; 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric unsigned NumOptions = A->getNumValues(); 2000b57cec5SDimitry Andric if (NumOptions == 0) { 2010b57cec5SDimitry Andric // No option is the same as "all". 2020b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out + "all")); 2030b57cec5SDimitry Andric return; 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric // Pass through "all", "none", or "default" with an optional refinement step. 2070b57cec5SDimitry Andric if (NumOptions == 1) { 2080b57cec5SDimitry Andric StringRef Val = A->getValue(0); 2090b57cec5SDimitry Andric size_t RefStepLoc; 2100b57cec5SDimitry Andric if (!getRefinementStep(Val, D, *A, RefStepLoc)) 2110b57cec5SDimitry Andric return; 2120b57cec5SDimitry Andric StringRef ValBase = Val.slice(0, RefStepLoc); 2130b57cec5SDimitry Andric if (ValBase == "all" || ValBase == "none" || ValBase == "default") { 2140b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out + Val)); 2150b57cec5SDimitry Andric return; 2160b57cec5SDimitry Andric } 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric // Each reciprocal type may be enabled or disabled individually. 2200b57cec5SDimitry Andric // Check each input value for validity, concatenate them all back together, 2210b57cec5SDimitry Andric // and pass through. 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric llvm::StringMap<bool> OptionStrings; 2240b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("divd", false)); 2250b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("divf", false)); 2260b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-divd", false)); 2270b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-divf", false)); 2280b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("sqrtd", false)); 2290b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("sqrtf", false)); 2300b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrtd", false)); 2310b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrtf", false)); 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric for (unsigned i = 0; i != NumOptions; ++i) { 2340b57cec5SDimitry Andric StringRef Val = A->getValue(i); 2350b57cec5SDimitry Andric 2360b57cec5SDimitry Andric bool IsDisabled = Val.startswith(DisabledPrefixIn); 2370b57cec5SDimitry Andric // Ignore the disablement token for string matching. 2380b57cec5SDimitry Andric if (IsDisabled) 2390b57cec5SDimitry Andric Val = Val.substr(1); 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric size_t RefStep; 2420b57cec5SDimitry Andric if (!getRefinementStep(Val, D, *A, RefStep)) 2430b57cec5SDimitry Andric return; 2440b57cec5SDimitry Andric 2450b57cec5SDimitry Andric StringRef ValBase = Val.slice(0, RefStep); 2460b57cec5SDimitry Andric llvm::StringMap<bool>::iterator OptionIter = OptionStrings.find(ValBase); 2470b57cec5SDimitry Andric if (OptionIter == OptionStrings.end()) { 2480b57cec5SDimitry Andric // Try again specifying float suffix. 2490b57cec5SDimitry Andric OptionIter = OptionStrings.find(ValBase.str() + 'f'); 2500b57cec5SDimitry Andric if (OptionIter == OptionStrings.end()) { 2510b57cec5SDimitry Andric // The input name did not match any known option string. 2520b57cec5SDimitry Andric D.Diag(diag::err_drv_unknown_argument) << Val; 2530b57cec5SDimitry Andric return; 2540b57cec5SDimitry Andric } 2550b57cec5SDimitry Andric // The option was specified without a float or double suffix. 2560b57cec5SDimitry Andric // Make sure that the double entry was not already specified. 2570b57cec5SDimitry Andric // The float entry will be checked below. 2580b57cec5SDimitry Andric if (OptionStrings[ValBase.str() + 'd']) { 2590b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; 2600b57cec5SDimitry Andric return; 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric } 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric if (OptionIter->second == true) { 2650b57cec5SDimitry Andric // Duplicate option specified. 2660b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; 2670b57cec5SDimitry Andric return; 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric // Mark the matched option as found. Do not allow duplicate specifiers. 2710b57cec5SDimitry Andric OptionIter->second = true; 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric // If the precision was not specified, also mark the double entry as found. 2740b57cec5SDimitry Andric if (ValBase.back() != 'f' && ValBase.back() != 'd') 2750b57cec5SDimitry Andric OptionStrings[ValBase.str() + 'd'] = true; 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric // Build the output string. 2780b57cec5SDimitry Andric StringRef Prefix = IsDisabled ? DisabledPrefixOut : EnabledPrefixOut; 2790b57cec5SDimitry Andric Out = Args.MakeArgString(Out + Prefix + Val); 2800b57cec5SDimitry Andric if (i != NumOptions - 1) 2810b57cec5SDimitry Andric Out = Args.MakeArgString(Out + ","); 2820b57cec5SDimitry Andric } 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out)); 2850b57cec5SDimitry Andric } 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric /// The -mprefer-vector-width option accepts either a positive integer 2880b57cec5SDimitry Andric /// or the string "none". 2890b57cec5SDimitry Andric static void ParseMPreferVectorWidth(const Driver &D, const ArgList &Args, 2900b57cec5SDimitry Andric ArgStringList &CmdArgs) { 2910b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_mprefer_vector_width_EQ); 2920b57cec5SDimitry Andric if (!A) 2930b57cec5SDimitry Andric return; 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric StringRef Value = A->getValue(); 2960b57cec5SDimitry Andric if (Value == "none") { 2970b57cec5SDimitry Andric CmdArgs.push_back("-mprefer-vector-width=none"); 2980b57cec5SDimitry Andric } else { 2990b57cec5SDimitry Andric unsigned Width; 3000b57cec5SDimitry Andric if (Value.getAsInteger(10, Width)) { 3010b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 3020b57cec5SDimitry Andric return; 3030b57cec5SDimitry Andric } 3040b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mprefer-vector-width=" + Value)); 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric } 3070b57cec5SDimitry Andric 308480093f4SDimitry Andric static void getWebAssemblyTargetFeatures(const ArgList &Args, 309480093f4SDimitry Andric std::vector<StringRef> &Features) { 310480093f4SDimitry Andric handleTargetFeaturesGroup(Args, Features, options::OPT_m_wasm_Features_Group); 311480093f4SDimitry Andric } 312480093f4SDimitry Andric 3135ffd83dbSDimitry Andric static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple, 314480093f4SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs, 3155ffd83dbSDimitry Andric bool ForAS, bool IsAux = false) { 316480093f4SDimitry Andric std::vector<StringRef> Features; 317480093f4SDimitry Andric switch (Triple.getArch()) { 318480093f4SDimitry Andric default: 319480093f4SDimitry Andric break; 320480093f4SDimitry Andric case llvm::Triple::mips: 321480093f4SDimitry Andric case llvm::Triple::mipsel: 322480093f4SDimitry Andric case llvm::Triple::mips64: 323480093f4SDimitry Andric case llvm::Triple::mips64el: 324480093f4SDimitry Andric mips::getMIPSTargetFeatures(D, Triple, Args, Features); 325480093f4SDimitry Andric break; 326480093f4SDimitry Andric 327480093f4SDimitry Andric case llvm::Triple::arm: 328480093f4SDimitry Andric case llvm::Triple::armeb: 329480093f4SDimitry Andric case llvm::Triple::thumb: 330480093f4SDimitry Andric case llvm::Triple::thumbeb: 3315ffd83dbSDimitry Andric arm::getARMTargetFeatures(D, Triple, Args, CmdArgs, Features, ForAS); 332480093f4SDimitry Andric break; 333480093f4SDimitry Andric 334480093f4SDimitry Andric case llvm::Triple::ppc: 335e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 336480093f4SDimitry Andric case llvm::Triple::ppc64: 337480093f4SDimitry Andric case llvm::Triple::ppc64le: 338480093f4SDimitry Andric ppc::getPPCTargetFeatures(D, Triple, Args, Features); 339480093f4SDimitry Andric break; 340480093f4SDimitry Andric case llvm::Triple::riscv32: 341480093f4SDimitry Andric case llvm::Triple::riscv64: 342480093f4SDimitry Andric riscv::getRISCVTargetFeatures(D, Triple, Args, Features); 343480093f4SDimitry Andric break; 344480093f4SDimitry Andric case llvm::Triple::systemz: 3455ffd83dbSDimitry Andric systemz::getSystemZTargetFeatures(D, Args, Features); 346480093f4SDimitry Andric break; 347480093f4SDimitry Andric case llvm::Triple::aarch64: 348480093f4SDimitry Andric case llvm::Triple::aarch64_32: 349480093f4SDimitry Andric case llvm::Triple::aarch64_be: 35004eeddc0SDimitry Andric aarch64::getAArch64TargetFeatures(D, Triple, Args, CmdArgs, Features, 35104eeddc0SDimitry Andric ForAS); 352480093f4SDimitry Andric break; 353480093f4SDimitry Andric case llvm::Triple::x86: 354480093f4SDimitry Andric case llvm::Triple::x86_64: 355480093f4SDimitry Andric x86::getX86TargetFeatures(D, Triple, Args, Features); 356480093f4SDimitry Andric break; 357480093f4SDimitry Andric case llvm::Triple::hexagon: 358480093f4SDimitry Andric hexagon::getHexagonTargetFeatures(D, Args, Features); 359480093f4SDimitry Andric break; 360480093f4SDimitry Andric case llvm::Triple::wasm32: 361480093f4SDimitry Andric case llvm::Triple::wasm64: 362480093f4SDimitry Andric getWebAssemblyTargetFeatures(Args, Features); 363480093f4SDimitry Andric break; 364480093f4SDimitry Andric case llvm::Triple::sparc: 365480093f4SDimitry Andric case llvm::Triple::sparcel: 366480093f4SDimitry Andric case llvm::Triple::sparcv9: 367480093f4SDimitry Andric sparc::getSparcTargetFeatures(D, Args, Features); 368480093f4SDimitry Andric break; 369480093f4SDimitry Andric case llvm::Triple::r600: 370480093f4SDimitry Andric case llvm::Triple::amdgcn: 371e8d8bef9SDimitry Andric amdgpu::getAMDGPUTargetFeatures(D, Triple, Args, Features); 372480093f4SDimitry Andric break; 373fe6060f1SDimitry Andric case llvm::Triple::m68k: 374fe6060f1SDimitry Andric m68k::getM68kTargetFeatures(D, Triple, Args, Features); 375fe6060f1SDimitry Andric break; 376480093f4SDimitry Andric case llvm::Triple::msp430: 377480093f4SDimitry Andric msp430::getMSP430TargetFeatures(D, Args, Features); 3785ffd83dbSDimitry Andric break; 3795ffd83dbSDimitry Andric case llvm::Triple::ve: 3805ffd83dbSDimitry Andric ve::getVETargetFeatures(D, Args, Features); 381fe6060f1SDimitry Andric break; 382480093f4SDimitry Andric } 383480093f4SDimitry Andric 3845ffd83dbSDimitry Andric for (auto Feature : unifyTargetFeatures(Features)) { 3855ffd83dbSDimitry Andric CmdArgs.push_back(IsAux ? "-aux-target-feature" : "-target-feature"); 3865ffd83dbSDimitry Andric CmdArgs.push_back(Feature.data()); 387480093f4SDimitry Andric } 388480093f4SDimitry Andric } 389480093f4SDimitry Andric 3900b57cec5SDimitry Andric static bool 3910b57cec5SDimitry Andric shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime, 3920b57cec5SDimitry Andric const llvm::Triple &Triple) { 3930b57cec5SDimitry Andric // We use the zero-cost exception tables for Objective-C if the non-fragile 3940b57cec5SDimitry Andric // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and 3950b57cec5SDimitry Andric // later. 3960b57cec5SDimitry Andric if (runtime.isNonFragile()) 3970b57cec5SDimitry Andric return true; 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric if (!Triple.isMacOSX()) 4000b57cec5SDimitry Andric return false; 4010b57cec5SDimitry Andric 4020b57cec5SDimitry Andric return (!Triple.isMacOSXVersionLT(10, 5) && 4030b57cec5SDimitry Andric (Triple.getArch() == llvm::Triple::x86_64 || 4040b57cec5SDimitry Andric Triple.getArch() == llvm::Triple::arm)); 4050b57cec5SDimitry Andric } 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric /// Adds exception related arguments to the driver command arguments. There's a 4084824e7fdSDimitry Andric /// main flag, -fexceptions and also language specific flags to enable/disable 4090b57cec5SDimitry Andric /// C++ and Objective-C exceptions. This makes it possible to for example 4100b57cec5SDimitry Andric /// disable C++ exceptions but enable Objective-C exceptions. 411fe6060f1SDimitry Andric static bool addExceptionArgs(const ArgList &Args, types::ID InputType, 4120b57cec5SDimitry Andric const ToolChain &TC, bool KernelOrKext, 4130b57cec5SDimitry Andric const ObjCRuntime &objcRuntime, 4140b57cec5SDimitry Andric ArgStringList &CmdArgs) { 4150b57cec5SDimitry Andric const llvm::Triple &Triple = TC.getTriple(); 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric if (KernelOrKext) { 4180b57cec5SDimitry Andric // -mkernel and -fapple-kext imply no exceptions, so claim exception related 4190b57cec5SDimitry Andric // arguments now to avoid warnings about unused arguments. 4200b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fexceptions); 4210b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_exceptions); 4220b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fobjc_exceptions); 4230b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_objc_exceptions); 4240b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fcxx_exceptions); 4250b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions); 426fe6060f1SDimitry Andric Args.ClaimAllArgs(options::OPT_fasync_exceptions); 427fe6060f1SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_async_exceptions); 428fe6060f1SDimitry Andric return false; 4290b57cec5SDimitry Andric } 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric // See if the user explicitly enabled exceptions. 4320b57cec5SDimitry Andric bool EH = Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions, 4330b57cec5SDimitry Andric false); 4340b57cec5SDimitry Andric 435fe6060f1SDimitry Andric bool EHa = Args.hasFlag(options::OPT_fasync_exceptions, 436fe6060f1SDimitry Andric options::OPT_fno_async_exceptions, false); 437fe6060f1SDimitry Andric if (EHa) { 438fe6060f1SDimitry Andric CmdArgs.push_back("-fasync-exceptions"); 439fe6060f1SDimitry Andric EH = true; 440fe6060f1SDimitry Andric } 441fe6060f1SDimitry Andric 4420b57cec5SDimitry Andric // Obj-C exceptions are enabled by default, regardless of -fexceptions. This 4430b57cec5SDimitry Andric // is not necessarily sensible, but follows GCC. 4440b57cec5SDimitry Andric if (types::isObjC(InputType) && 4450b57cec5SDimitry Andric Args.hasFlag(options::OPT_fobjc_exceptions, 4460b57cec5SDimitry Andric options::OPT_fno_objc_exceptions, true)) { 4470b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-exceptions"); 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric EH |= shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple); 4500b57cec5SDimitry Andric } 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric if (types::isCXX(InputType)) { 4530b57cec5SDimitry Andric // Disable C++ EH by default on XCore and PS4. 4540b57cec5SDimitry Andric bool CXXExceptionsEnabled = 4550b57cec5SDimitry Andric Triple.getArch() != llvm::Triple::xcore && !Triple.isPS4CPU(); 4560b57cec5SDimitry Andric Arg *ExceptionArg = Args.getLastArg( 4570b57cec5SDimitry Andric options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions, 4580b57cec5SDimitry Andric options::OPT_fexceptions, options::OPT_fno_exceptions); 4590b57cec5SDimitry Andric if (ExceptionArg) 4600b57cec5SDimitry Andric CXXExceptionsEnabled = 4610b57cec5SDimitry Andric ExceptionArg->getOption().matches(options::OPT_fcxx_exceptions) || 4620b57cec5SDimitry Andric ExceptionArg->getOption().matches(options::OPT_fexceptions); 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric if (CXXExceptionsEnabled) { 4650b57cec5SDimitry Andric CmdArgs.push_back("-fcxx-exceptions"); 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric EH = true; 4680b57cec5SDimitry Andric } 4690b57cec5SDimitry Andric } 4700b57cec5SDimitry Andric 4715ffd83dbSDimitry Andric // OPT_fignore_exceptions means exception could still be thrown, 4725ffd83dbSDimitry Andric // but no clean up or catch would happen in current module. 4735ffd83dbSDimitry Andric // So we do not set EH to false. 4745ffd83dbSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fignore_exceptions); 4755ffd83dbSDimitry Andric 4760b57cec5SDimitry Andric if (EH) 4770b57cec5SDimitry Andric CmdArgs.push_back("-fexceptions"); 478fe6060f1SDimitry Andric return EH; 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric 481480093f4SDimitry Andric static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC, 482480093f4SDimitry Andric const JobAction &JA) { 4830b57cec5SDimitry Andric bool Default = true; 4840b57cec5SDimitry Andric if (TC.getTriple().isOSDarwin()) { 4850b57cec5SDimitry Andric // The native darwin assembler doesn't support the linker_option directives, 4860b57cec5SDimitry Andric // so we disable them if we think the .s file will be passed to it. 4870b57cec5SDimitry Andric Default = TC.useIntegratedAs(); 4880b57cec5SDimitry Andric } 489480093f4SDimitry Andric // The linker_option directives are intended for host compilation. 490480093f4SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_Cuda) || 491480093f4SDimitry Andric JA.isDeviceOffloading(Action::OFK_HIP)) 492480093f4SDimitry Andric Default = false; 493480093f4SDimitry Andric return Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink, 4940b57cec5SDimitry Andric Default); 4950b57cec5SDimitry Andric } 4960b57cec5SDimitry Andric 4970b57cec5SDimitry Andric // Convert an arg of the form "-gN" or "-ggdbN" or one of their aliases 4980b57cec5SDimitry Andric // to the corresponding DebugInfoKind. 4990b57cec5SDimitry Andric static codegenoptions::DebugInfoKind DebugLevelToInfoKind(const Arg &A) { 5000b57cec5SDimitry Andric assert(A.getOption().matches(options::OPT_gN_Group) && 5010b57cec5SDimitry Andric "Not a -g option that specifies a debug-info level"); 5020b57cec5SDimitry Andric if (A.getOption().matches(options::OPT_g0) || 5030b57cec5SDimitry Andric A.getOption().matches(options::OPT_ggdb0)) 5040b57cec5SDimitry Andric return codegenoptions::NoDebugInfo; 5050b57cec5SDimitry Andric if (A.getOption().matches(options::OPT_gline_tables_only) || 5060b57cec5SDimitry Andric A.getOption().matches(options::OPT_ggdb1)) 5070b57cec5SDimitry Andric return codegenoptions::DebugLineTablesOnly; 5080b57cec5SDimitry Andric if (A.getOption().matches(options::OPT_gline_directives_only)) 5090b57cec5SDimitry Andric return codegenoptions::DebugDirectivesOnly; 510fe6060f1SDimitry Andric return codegenoptions::DebugInfoConstructor; 5110b57cec5SDimitry Andric } 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric static bool mustUseNonLeafFramePointerForTarget(const llvm::Triple &Triple) { 5140b57cec5SDimitry Andric switch (Triple.getArch()){ 5150b57cec5SDimitry Andric default: 5160b57cec5SDimitry Andric return false; 5170b57cec5SDimitry Andric case llvm::Triple::arm: 5180b57cec5SDimitry Andric case llvm::Triple::thumb: 5190b57cec5SDimitry Andric // ARM Darwin targets require a frame pointer to be always present to aid 5200b57cec5SDimitry Andric // offline debugging via backtraces. 5210b57cec5SDimitry Andric return Triple.isOSDarwin(); 5220b57cec5SDimitry Andric } 5230b57cec5SDimitry Andric } 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric static bool useFramePointerForTargetByDefault(const ArgList &Args, 5260b57cec5SDimitry Andric const llvm::Triple &Triple) { 5275ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_pg) && !Args.hasArg(options::OPT_mfentry)) 528a7dea167SDimitry Andric return true; 529a7dea167SDimitry Andric 5300b57cec5SDimitry Andric switch (Triple.getArch()) { 5310b57cec5SDimitry Andric case llvm::Triple::xcore: 5320b57cec5SDimitry Andric case llvm::Triple::wasm32: 5330b57cec5SDimitry Andric case llvm::Triple::wasm64: 5340b57cec5SDimitry Andric case llvm::Triple::msp430: 5350b57cec5SDimitry Andric // XCore never wants frame pointers, regardless of OS. 5360b57cec5SDimitry Andric // WebAssembly never wants frame pointers. 5370b57cec5SDimitry Andric return false; 5380b57cec5SDimitry Andric case llvm::Triple::ppc: 539e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 5400b57cec5SDimitry Andric case llvm::Triple::ppc64: 5410b57cec5SDimitry Andric case llvm::Triple::ppc64le: 5420b57cec5SDimitry Andric case llvm::Triple::riscv32: 5430b57cec5SDimitry Andric case llvm::Triple::riscv64: 544480093f4SDimitry Andric case llvm::Triple::amdgcn: 545480093f4SDimitry Andric case llvm::Triple::r600: 5460b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 5470b57cec5SDimitry Andric default: 5480b57cec5SDimitry Andric break; 5490b57cec5SDimitry Andric } 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric if (Triple.isOSNetBSD()) { 5520b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 5530b57cec5SDimitry Andric } 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric if (Triple.isOSLinux() || Triple.getOS() == llvm::Triple::CloudABI || 5560b57cec5SDimitry Andric Triple.isOSHurd()) { 5570b57cec5SDimitry Andric switch (Triple.getArch()) { 5580b57cec5SDimitry Andric // Don't use a frame pointer on linux if optimizing for certain targets. 5595ffd83dbSDimitry Andric case llvm::Triple::arm: 5605ffd83dbSDimitry Andric case llvm::Triple::armeb: 5615ffd83dbSDimitry Andric case llvm::Triple::thumb: 5625ffd83dbSDimitry Andric case llvm::Triple::thumbeb: 5635ffd83dbSDimitry Andric if (Triple.isAndroid()) 5645ffd83dbSDimitry Andric return true; 5655ffd83dbSDimitry Andric LLVM_FALLTHROUGH; 5660b57cec5SDimitry Andric case llvm::Triple::mips64: 5670b57cec5SDimitry Andric case llvm::Triple::mips64el: 5680b57cec5SDimitry Andric case llvm::Triple::mips: 5690b57cec5SDimitry Andric case llvm::Triple::mipsel: 5700b57cec5SDimitry Andric case llvm::Triple::systemz: 5710b57cec5SDimitry Andric case llvm::Triple::x86: 5720b57cec5SDimitry Andric case llvm::Triple::x86_64: 5730b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 5740b57cec5SDimitry Andric default: 5750b57cec5SDimitry Andric return true; 5760b57cec5SDimitry Andric } 5770b57cec5SDimitry Andric } 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric if (Triple.isOSWindows()) { 5800b57cec5SDimitry Andric switch (Triple.getArch()) { 5810b57cec5SDimitry Andric case llvm::Triple::x86: 5820b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 5830b57cec5SDimitry Andric case llvm::Triple::x86_64: 5840b57cec5SDimitry Andric return Triple.isOSBinFormatMachO(); 5850b57cec5SDimitry Andric case llvm::Triple::arm: 5860b57cec5SDimitry Andric case llvm::Triple::thumb: 5870b57cec5SDimitry Andric // Windows on ARM builds with FPO disabled to aid fast stack walking 5880b57cec5SDimitry Andric return true; 5890b57cec5SDimitry Andric default: 5900b57cec5SDimitry Andric // All other supported Windows ISAs use xdata unwind information, so frame 5910b57cec5SDimitry Andric // pointers are not generally useful. 5920b57cec5SDimitry Andric return false; 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric } 5950b57cec5SDimitry Andric 5960b57cec5SDimitry Andric return true; 5970b57cec5SDimitry Andric } 5980b57cec5SDimitry Andric 599a7dea167SDimitry Andric static CodeGenOptions::FramePointerKind 600a7dea167SDimitry Andric getFramePointerKind(const ArgList &Args, const llvm::Triple &Triple) { 601a7dea167SDimitry Andric // We have 4 states: 602a7dea167SDimitry Andric // 603a7dea167SDimitry Andric // 00) leaf retained, non-leaf retained 604a7dea167SDimitry Andric // 01) leaf retained, non-leaf omitted (this is invalid) 605a7dea167SDimitry Andric // 10) leaf omitted, non-leaf retained 606a7dea167SDimitry Andric // (what -momit-leaf-frame-pointer was designed for) 607a7dea167SDimitry Andric // 11) leaf omitted, non-leaf omitted 608a7dea167SDimitry Andric // 609a7dea167SDimitry Andric // "omit" options taking precedence over "no-omit" options is the only way 610a7dea167SDimitry Andric // to make 3 valid states representable 611a7dea167SDimitry Andric Arg *A = Args.getLastArg(options::OPT_fomit_frame_pointer, 612a7dea167SDimitry Andric options::OPT_fno_omit_frame_pointer); 613a7dea167SDimitry Andric bool OmitFP = A && A->getOption().matches(options::OPT_fomit_frame_pointer); 614a7dea167SDimitry Andric bool NoOmitFP = 615a7dea167SDimitry Andric A && A->getOption().matches(options::OPT_fno_omit_frame_pointer); 616e8d8bef9SDimitry Andric bool OmitLeafFP = Args.hasFlag(options::OPT_momit_leaf_frame_pointer, 617480093f4SDimitry Andric options::OPT_mno_omit_leaf_frame_pointer, 618349cc55cSDimitry Andric Triple.isAArch64() || Triple.isPS4CPU() || 619349cc55cSDimitry Andric Triple.isVE()); 620a7dea167SDimitry Andric if (NoOmitFP || mustUseNonLeafFramePointerForTarget(Triple) || 621a7dea167SDimitry Andric (!OmitFP && useFramePointerForTargetByDefault(Args, Triple))) { 622e8d8bef9SDimitry Andric if (OmitLeafFP) 623a7dea167SDimitry Andric return CodeGenOptions::FramePointerKind::NonLeaf; 624a7dea167SDimitry Andric return CodeGenOptions::FramePointerKind::All; 6250b57cec5SDimitry Andric } 626a7dea167SDimitry Andric return CodeGenOptions::FramePointerKind::None; 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric /// Add a CC1 option to specify the debug compilation directory. 6300eae32dcSDimitry Andric static const char *addDebugCompDirArg(const ArgList &Args, 6310eae32dcSDimitry Andric ArgStringList &CmdArgs, 632a7dea167SDimitry Andric const llvm::vfs::FileSystem &VFS) { 633fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ, 634fe6060f1SDimitry Andric options::OPT_fdebug_compilation_dir_EQ)) { 635fe6060f1SDimitry Andric if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ)) 636fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fdebug-compilation-dir=") + 637fe6060f1SDimitry Andric A->getValue())); 638fe6060f1SDimitry Andric else 639fe6060f1SDimitry Andric A->render(Args, CmdArgs); 640a7dea167SDimitry Andric } else if (llvm::ErrorOr<std::string> CWD = 641a7dea167SDimitry Andric VFS.getCurrentWorkingDirectory()) { 642fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fdebug-compilation-dir=" + *CWD)); 6430b57cec5SDimitry Andric } 6440eae32dcSDimitry Andric StringRef Path(CmdArgs.back()); 6450eae32dcSDimitry Andric return Path.substr(Path.find('=') + 1).data(); 6460eae32dcSDimitry Andric } 6470eae32dcSDimitry Andric 6480eae32dcSDimitry Andric static void addDebugObjectName(const ArgList &Args, ArgStringList &CmdArgs, 6490eae32dcSDimitry Andric const char *DebugCompilationDir, 6500eae32dcSDimitry Andric const char *OutputFileName) { 6510eae32dcSDimitry Andric // No need to generate a value for -object-file-name if it was provided. 6520eae32dcSDimitry Andric for (auto *Arg : Args.filtered(options::OPT_Xclang)) 6530eae32dcSDimitry Andric if (StringRef(Arg->getValue()).startswith("-object-file-name")) 6540eae32dcSDimitry Andric return; 6550eae32dcSDimitry Andric 6560eae32dcSDimitry Andric if (Args.hasArg(options::OPT_object_file_name_EQ)) 6570eae32dcSDimitry Andric return; 6580eae32dcSDimitry Andric 6590eae32dcSDimitry Andric SmallString<128> ObjFileNameForDebug(OutputFileName); 6600eae32dcSDimitry Andric if (ObjFileNameForDebug != "-" && 6610eae32dcSDimitry Andric !llvm::sys::path::is_absolute(ObjFileNameForDebug) && 6620eae32dcSDimitry Andric (!DebugCompilationDir || 6630eae32dcSDimitry Andric llvm::sys::path::is_absolute(DebugCompilationDir))) { 6640eae32dcSDimitry Andric // Make the path absolute in the debug infos like MSVC does. 6650eae32dcSDimitry Andric llvm::sys::fs::make_absolute(ObjFileNameForDebug); 6660eae32dcSDimitry Andric } 6670eae32dcSDimitry Andric CmdArgs.push_back( 6680eae32dcSDimitry Andric Args.MakeArgString(Twine("-object-file-name=") + ObjFileNameForDebug)); 6690b57cec5SDimitry Andric } 6700b57cec5SDimitry Andric 6710b57cec5SDimitry Andric /// Add a CC1 and CC1AS option to specify the debug file path prefix map. 6720b57cec5SDimitry Andric static void addDebugPrefixMapArg(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs) { 673480093f4SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 674480093f4SDimitry Andric options::OPT_fdebug_prefix_map_EQ)) { 6750b57cec5SDimitry Andric StringRef Map = A->getValue(); 676349cc55cSDimitry Andric if (!Map.contains('=')) 677480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 678480093f4SDimitry Andric << Map << A->getOption().getName(); 6790b57cec5SDimitry Andric else 6800b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map)); 6810b57cec5SDimitry Andric A->claim(); 6820b57cec5SDimitry Andric } 6830b57cec5SDimitry Andric } 6840b57cec5SDimitry Andric 685480093f4SDimitry Andric /// Add a CC1 and CC1AS option to specify the macro file path prefix map. 686480093f4SDimitry Andric static void addMacroPrefixMapArg(const Driver &D, const ArgList &Args, 687480093f4SDimitry Andric ArgStringList &CmdArgs) { 688480093f4SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 689480093f4SDimitry Andric options::OPT_fmacro_prefix_map_EQ)) { 690480093f4SDimitry Andric StringRef Map = A->getValue(); 691349cc55cSDimitry Andric if (!Map.contains('=')) 692480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 693480093f4SDimitry Andric << Map << A->getOption().getName(); 694480093f4SDimitry Andric else 695480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fmacro-prefix-map=" + Map)); 696480093f4SDimitry Andric A->claim(); 697480093f4SDimitry Andric } 698480093f4SDimitry Andric } 699480093f4SDimitry Andric 700e8d8bef9SDimitry Andric /// Add a CC1 and CC1AS option to specify the coverage file path prefix map. 701fe6060f1SDimitry Andric static void addCoveragePrefixMapArg(const Driver &D, const ArgList &Args, 702e8d8bef9SDimitry Andric ArgStringList &CmdArgs) { 703e8d8bef9SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 704fe6060f1SDimitry Andric options::OPT_fcoverage_prefix_map_EQ)) { 705e8d8bef9SDimitry Andric StringRef Map = A->getValue(); 706349cc55cSDimitry Andric if (!Map.contains('=')) 707e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 708e8d8bef9SDimitry Andric << Map << A->getOption().getName(); 709e8d8bef9SDimitry Andric else 710fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fcoverage-prefix-map=" + Map)); 711e8d8bef9SDimitry Andric A->claim(); 712e8d8bef9SDimitry Andric } 713e8d8bef9SDimitry Andric } 714e8d8bef9SDimitry Andric 7150b57cec5SDimitry Andric /// Vectorize at all optimization levels greater than 1 except for -Oz. 7160b57cec5SDimitry Andric /// For -Oz the loop vectorizer is disabled, while the slp vectorizer is 7170b57cec5SDimitry Andric /// enabled. 7180b57cec5SDimitry Andric static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) { 7190b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 7200b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 7210b57cec5SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 7220b57cec5SDimitry Andric return true; 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O0)) 7250b57cec5SDimitry Andric return false; 7260b57cec5SDimitry Andric 7270b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag"); 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric // Vectorize -Os. 7300b57cec5SDimitry Andric StringRef S(A->getValue()); 7310b57cec5SDimitry Andric if (S == "s") 7320b57cec5SDimitry Andric return true; 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric // Don't vectorize -Oz, unless it's the slp vectorizer. 7350b57cec5SDimitry Andric if (S == "z") 7360b57cec5SDimitry Andric return isSlpVec; 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric unsigned OptLevel = 0; 7390b57cec5SDimitry Andric if (S.getAsInteger(10, OptLevel)) 7400b57cec5SDimitry Andric return false; 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric return OptLevel > 1; 7430b57cec5SDimitry Andric } 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric return false; 7460b57cec5SDimitry Andric } 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric /// Add -x lang to \p CmdArgs for \p Input. 7490b57cec5SDimitry Andric static void addDashXForInput(const ArgList &Args, const InputInfo &Input, 7500b57cec5SDimitry Andric ArgStringList &CmdArgs) { 7510b57cec5SDimitry Andric // When using -verify-pch, we don't want to provide the type 7520b57cec5SDimitry Andric // 'precompiled-header' if it was inferred from the file extension 7530b57cec5SDimitry Andric if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH) 7540b57cec5SDimitry Andric return; 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric CmdArgs.push_back("-x"); 7570b57cec5SDimitry Andric if (Args.hasArg(options::OPT_rewrite_objc)) 7580b57cec5SDimitry Andric CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX)); 7590b57cec5SDimitry Andric else { 7600b57cec5SDimitry Andric // Map the driver type to the frontend type. This is mostly an identity 7610b57cec5SDimitry Andric // mapping, except that the distinction between module interface units 7620b57cec5SDimitry Andric // and other source files does not exist at the frontend layer. 7630b57cec5SDimitry Andric const char *ClangType; 7640b57cec5SDimitry Andric switch (Input.getType()) { 7650b57cec5SDimitry Andric case types::TY_CXXModule: 7660b57cec5SDimitry Andric ClangType = "c++"; 7670b57cec5SDimitry Andric break; 7680b57cec5SDimitry Andric case types::TY_PP_CXXModule: 7690b57cec5SDimitry Andric ClangType = "c++-cpp-output"; 7700b57cec5SDimitry Andric break; 7710b57cec5SDimitry Andric default: 7720b57cec5SDimitry Andric ClangType = types::getTypeName(Input.getType()); 7730b57cec5SDimitry Andric break; 7740b57cec5SDimitry Andric } 7750b57cec5SDimitry Andric CmdArgs.push_back(ClangType); 7760b57cec5SDimitry Andric } 7770b57cec5SDimitry Andric } 7780b57cec5SDimitry Andric 7790b57cec5SDimitry Andric static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, 7800b57cec5SDimitry Andric const Driver &D, const InputInfo &Output, 781349cc55cSDimitry Andric const ArgList &Args, SanitizerArgs &SanArgs, 7820b57cec5SDimitry Andric ArgStringList &CmdArgs) { 7830b57cec5SDimitry Andric 7840b57cec5SDimitry Andric auto *PGOGenerateArg = Args.getLastArg(options::OPT_fprofile_generate, 7850b57cec5SDimitry Andric options::OPT_fprofile_generate_EQ, 7860b57cec5SDimitry Andric options::OPT_fno_profile_generate); 7870b57cec5SDimitry Andric if (PGOGenerateArg && 7880b57cec5SDimitry Andric PGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)) 7890b57cec5SDimitry Andric PGOGenerateArg = nullptr; 7900b57cec5SDimitry Andric 7910b57cec5SDimitry Andric auto *CSPGOGenerateArg = Args.getLastArg(options::OPT_fcs_profile_generate, 7920b57cec5SDimitry Andric options::OPT_fcs_profile_generate_EQ, 7930b57cec5SDimitry Andric options::OPT_fno_profile_generate); 7940b57cec5SDimitry Andric if (CSPGOGenerateArg && 7950b57cec5SDimitry Andric CSPGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)) 7960b57cec5SDimitry Andric CSPGOGenerateArg = nullptr; 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric auto *ProfileGenerateArg = Args.getLastArg( 7990b57cec5SDimitry Andric options::OPT_fprofile_instr_generate, 8000b57cec5SDimitry Andric options::OPT_fprofile_instr_generate_EQ, 8010b57cec5SDimitry Andric options::OPT_fno_profile_instr_generate); 8020b57cec5SDimitry Andric if (ProfileGenerateArg && 8030b57cec5SDimitry Andric ProfileGenerateArg->getOption().matches( 8040b57cec5SDimitry Andric options::OPT_fno_profile_instr_generate)) 8050b57cec5SDimitry Andric ProfileGenerateArg = nullptr; 8060b57cec5SDimitry Andric 8070b57cec5SDimitry Andric if (PGOGenerateArg && ProfileGenerateArg) 8080b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 8090b57cec5SDimitry Andric << PGOGenerateArg->getSpelling() << ProfileGenerateArg->getSpelling(); 8100b57cec5SDimitry Andric 8110b57cec5SDimitry Andric auto *ProfileUseArg = getLastProfileUseArg(Args); 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric if (PGOGenerateArg && ProfileUseArg) 8140b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 8150b57cec5SDimitry Andric << ProfileUseArg->getSpelling() << PGOGenerateArg->getSpelling(); 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric if (ProfileGenerateArg && ProfileUseArg) 8180b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 8190b57cec5SDimitry Andric << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling(); 8200b57cec5SDimitry Andric 821e8d8bef9SDimitry Andric if (CSPGOGenerateArg && PGOGenerateArg) { 8220b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 8230b57cec5SDimitry Andric << CSPGOGenerateArg->getSpelling() << PGOGenerateArg->getSpelling(); 824e8d8bef9SDimitry Andric PGOGenerateArg = nullptr; 825e8d8bef9SDimitry Andric } 8260b57cec5SDimitry Andric 827fe6060f1SDimitry Andric if (TC.getTriple().isOSAIX()) { 828fe6060f1SDimitry Andric if (ProfileGenerateArg) 829fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 830fe6060f1SDimitry Andric << ProfileGenerateArg->getSpelling() << TC.getTriple().str(); 831fe6060f1SDimitry Andric if (Arg *ProfileSampleUseArg = getLastProfileSampleUseArg(Args)) 832fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 833fe6060f1SDimitry Andric << ProfileSampleUseArg->getSpelling() << TC.getTriple().str(); 834fe6060f1SDimitry Andric } 835fe6060f1SDimitry Andric 8360b57cec5SDimitry Andric if (ProfileGenerateArg) { 8370b57cec5SDimitry Andric if (ProfileGenerateArg->getOption().matches( 8380b57cec5SDimitry Andric options::OPT_fprofile_instr_generate_EQ)) 8390b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-instrument-path=") + 8400b57cec5SDimitry Andric ProfileGenerateArg->getValue())); 8410b57cec5SDimitry Andric // The default is to use Clang Instrumentation. 8420b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=clang"); 8430b57cec5SDimitry Andric if (TC.getTriple().isWindowsMSVCEnvironment()) { 8440b57cec5SDimitry Andric // Add dependent lib for clang_rt.profile 8455ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8465ffd83dbSDimitry Andric "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile"))); 8470b57cec5SDimitry Andric } 8480b57cec5SDimitry Andric } 8490b57cec5SDimitry Andric 8500b57cec5SDimitry Andric Arg *PGOGenArg = nullptr; 8510b57cec5SDimitry Andric if (PGOGenerateArg) { 8520b57cec5SDimitry Andric assert(!CSPGOGenerateArg); 8530b57cec5SDimitry Andric PGOGenArg = PGOGenerateArg; 8540b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=llvm"); 8550b57cec5SDimitry Andric } 8560b57cec5SDimitry Andric if (CSPGOGenerateArg) { 8570b57cec5SDimitry Andric assert(!PGOGenerateArg); 8580b57cec5SDimitry Andric PGOGenArg = CSPGOGenerateArg; 8590b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=csllvm"); 8600b57cec5SDimitry Andric } 8610b57cec5SDimitry Andric if (PGOGenArg) { 8620b57cec5SDimitry Andric if (TC.getTriple().isWindowsMSVCEnvironment()) { 8635ffd83dbSDimitry Andric // Add dependent lib for clang_rt.profile 8645ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8655ffd83dbSDimitry Andric "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile"))); 8660b57cec5SDimitry Andric } 8670b57cec5SDimitry Andric if (PGOGenArg->getOption().matches( 8680b57cec5SDimitry Andric PGOGenerateArg ? options::OPT_fprofile_generate_EQ 8690b57cec5SDimitry Andric : options::OPT_fcs_profile_generate_EQ)) { 8700b57cec5SDimitry Andric SmallString<128> Path(PGOGenArg->getValue()); 8710b57cec5SDimitry Andric llvm::sys::path::append(Path, "default_%m.profraw"); 8720b57cec5SDimitry Andric CmdArgs.push_back( 8730b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-instrument-path=") + Path)); 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric } 8760b57cec5SDimitry Andric 8770b57cec5SDimitry Andric if (ProfileUseArg) { 8780b57cec5SDimitry Andric if (ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ)) 8790b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8800b57cec5SDimitry Andric Twine("-fprofile-instrument-use-path=") + ProfileUseArg->getValue())); 8810b57cec5SDimitry Andric else if ((ProfileUseArg->getOption().matches( 8820b57cec5SDimitry Andric options::OPT_fprofile_use_EQ) || 8830b57cec5SDimitry Andric ProfileUseArg->getOption().matches( 8840b57cec5SDimitry Andric options::OPT_fprofile_instr_use))) { 8850b57cec5SDimitry Andric SmallString<128> Path( 8860b57cec5SDimitry Andric ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue()); 8870b57cec5SDimitry Andric if (Path.empty() || llvm::sys::fs::is_directory(Path)) 8880b57cec5SDimitry Andric llvm::sys::path::append(Path, "default.profdata"); 8890b57cec5SDimitry Andric CmdArgs.push_back( 8900b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-instrument-use-path=") + Path)); 8910b57cec5SDimitry Andric } 8920b57cec5SDimitry Andric } 8930b57cec5SDimitry Andric 8945ffd83dbSDimitry Andric bool EmitCovNotes = Args.hasFlag(options::OPT_ftest_coverage, 8955ffd83dbSDimitry Andric options::OPT_fno_test_coverage, false) || 896a7dea167SDimitry Andric Args.hasArg(options::OPT_coverage); 8975ffd83dbSDimitry Andric bool EmitCovData = TC.needsGCovInstrumentation(Args); 898a7dea167SDimitry Andric if (EmitCovNotes) 899e8d8bef9SDimitry Andric CmdArgs.push_back("-ftest-coverage"); 900a7dea167SDimitry Andric if (EmitCovData) 901e8d8bef9SDimitry Andric CmdArgs.push_back("-fprofile-arcs"); 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcoverage_mapping, 9040b57cec5SDimitry Andric options::OPT_fno_coverage_mapping, false)) { 9050b57cec5SDimitry Andric if (!ProfileGenerateArg) 9060b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 9070b57cec5SDimitry Andric << "-fcoverage-mapping" 9080b57cec5SDimitry Andric << "-fprofile-instr-generate"; 9090b57cec5SDimitry Andric 9100b57cec5SDimitry Andric CmdArgs.push_back("-fcoverage-mapping"); 9110b57cec5SDimitry Andric } 9120b57cec5SDimitry Andric 913fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ, 914fe6060f1SDimitry Andric options::OPT_fcoverage_compilation_dir_EQ)) { 915fe6060f1SDimitry Andric if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ)) 916fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 917fe6060f1SDimitry Andric Twine("-fcoverage-compilation-dir=") + A->getValue())); 918fe6060f1SDimitry Andric else 919fe6060f1SDimitry Andric A->render(Args, CmdArgs); 920fe6060f1SDimitry Andric } else if (llvm::ErrorOr<std::string> CWD = 921fe6060f1SDimitry Andric D.getVFS().getCurrentWorkingDirectory()) { 922fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fcoverage-compilation-dir=" + *CWD)); 923fe6060f1SDimitry Andric } 924fe6060f1SDimitry Andric 9250b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) { 9260b57cec5SDimitry Andric auto *Arg = Args.getLastArg(options::OPT_fprofile_exclude_files_EQ); 9270b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_coverage)) 9280b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 9290b57cec5SDimitry Andric << "-fprofile-exclude-files=" 9300b57cec5SDimitry Andric << "--coverage"; 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric StringRef v = Arg->getValue(); 9330b57cec5SDimitry Andric CmdArgs.push_back( 9340b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-exclude-files=" + v))); 9350b57cec5SDimitry Andric } 9360b57cec5SDimitry Andric 9370b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fprofile_filter_files_EQ)) { 9380b57cec5SDimitry Andric auto *Arg = Args.getLastArg(options::OPT_fprofile_filter_files_EQ); 9390b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_coverage)) 9400b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 9410b57cec5SDimitry Andric << "-fprofile-filter-files=" 9420b57cec5SDimitry Andric << "--coverage"; 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric StringRef v = Arg->getValue(); 9450b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-filter-files=" + v))); 9460b57cec5SDimitry Andric } 9470b57cec5SDimitry Andric 948e8d8bef9SDimitry Andric if (const auto *A = Args.getLastArg(options::OPT_fprofile_update_EQ)) { 949e8d8bef9SDimitry Andric StringRef Val = A->getValue(); 950e8d8bef9SDimitry Andric if (Val == "atomic" || Val == "prefer-atomic") 951e8d8bef9SDimitry Andric CmdArgs.push_back("-fprofile-update=atomic"); 952e8d8bef9SDimitry Andric else if (Val != "single") 953e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 954e8d8bef9SDimitry Andric << A->getOption().getName() << Val; 955349cc55cSDimitry Andric } else if (SanArgs.needsTsanRt()) { 956e8d8bef9SDimitry Andric CmdArgs.push_back("-fprofile-update=atomic"); 957e8d8bef9SDimitry Andric } 958e8d8bef9SDimitry Andric 959a7dea167SDimitry Andric // Leave -fprofile-dir= an unused argument unless .gcda emission is 960a7dea167SDimitry Andric // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider 961a7dea167SDimitry Andric // the flag used. There is no -fno-profile-dir, so the user has no 962a7dea167SDimitry Andric // targeted way to suppress the warning. 963a7dea167SDimitry Andric Arg *FProfileDir = nullptr; 964a7dea167SDimitry Andric if (Args.hasArg(options::OPT_fprofile_arcs) || 965a7dea167SDimitry Andric Args.hasArg(options::OPT_coverage)) 966a7dea167SDimitry Andric FProfileDir = Args.getLastArg(options::OPT_fprofile_dir); 967a7dea167SDimitry Andric 968a7dea167SDimitry Andric // Put the .gcno and .gcda files (if needed) next to the object file or 969a7dea167SDimitry Andric // bitcode file in the case of LTO. 970a7dea167SDimitry Andric // FIXME: There should be a simpler way to find the object file for this 971a7dea167SDimitry Andric // input, and this code probably does the wrong thing for commands that 972a7dea167SDimitry Andric // compile and link all at once. 973a7dea167SDimitry Andric if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) && 974a7dea167SDimitry Andric (EmitCovNotes || EmitCovData) && Output.isFilename()) { 9750b57cec5SDimitry Andric SmallString<128> OutputFilename; 976480093f4SDimitry Andric if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT__SLASH_Fo)) 977480093f4SDimitry Andric OutputFilename = FinalOutput->getValue(); 978480093f4SDimitry Andric else if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) 9790b57cec5SDimitry Andric OutputFilename = FinalOutput->getValue(); 9800b57cec5SDimitry Andric else 9810b57cec5SDimitry Andric OutputFilename = llvm::sys::path::filename(Output.getBaseInput()); 9820b57cec5SDimitry Andric SmallString<128> CoverageFilename = OutputFilename; 983a7dea167SDimitry Andric if (llvm::sys::path::is_relative(CoverageFilename)) 984a7dea167SDimitry Andric (void)D.getVFS().makeAbsolute(CoverageFilename); 9850b57cec5SDimitry Andric llvm::sys::path::replace_extension(CoverageFilename, "gcno"); 986a7dea167SDimitry Andric 987a7dea167SDimitry Andric CmdArgs.push_back("-coverage-notes-file"); 9880b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); 9890b57cec5SDimitry Andric 990a7dea167SDimitry Andric if (EmitCovData) { 991a7dea167SDimitry Andric if (FProfileDir) { 9920b57cec5SDimitry Andric CoverageFilename = FProfileDir->getValue(); 9930b57cec5SDimitry Andric llvm::sys::path::append(CoverageFilename, OutputFilename); 9940b57cec5SDimitry Andric } 9950b57cec5SDimitry Andric llvm::sys::path::replace_extension(CoverageFilename, "gcda"); 996a7dea167SDimitry Andric CmdArgs.push_back("-coverage-data-file"); 9970b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); 9980b57cec5SDimitry Andric } 9990b57cec5SDimitry Andric } 10000b57cec5SDimitry Andric } 10010b57cec5SDimitry Andric 10020b57cec5SDimitry Andric /// Check whether the given input tree contains any compilation actions. 10030b57cec5SDimitry Andric static bool ContainsCompileAction(const Action *A) { 10040b57cec5SDimitry Andric if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A)) 10050b57cec5SDimitry Andric return true; 10060b57cec5SDimitry Andric 10070eae32dcSDimitry Andric return llvm::any_of(A->inputs(), ContainsCompileAction); 10080b57cec5SDimitry Andric } 10090b57cec5SDimitry Andric 10100b57cec5SDimitry Andric /// Check if -relax-all should be passed to the internal assembler. 10110b57cec5SDimitry Andric /// This is done by default when compiling non-assembler source with -O0. 10120b57cec5SDimitry Andric static bool UseRelaxAll(Compilation &C, const ArgList &Args) { 10130b57cec5SDimitry Andric bool RelaxDefault = true; 10140b57cec5SDimitry Andric 10150b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) 10160b57cec5SDimitry Andric RelaxDefault = A->getOption().matches(options::OPT_O0); 10170b57cec5SDimitry Andric 10180b57cec5SDimitry Andric if (RelaxDefault) { 10190b57cec5SDimitry Andric RelaxDefault = false; 10200b57cec5SDimitry Andric for (const auto &Act : C.getActions()) { 10210b57cec5SDimitry Andric if (ContainsCompileAction(Act)) { 10220b57cec5SDimitry Andric RelaxDefault = true; 10230b57cec5SDimitry Andric break; 10240b57cec5SDimitry Andric } 10250b57cec5SDimitry Andric } 10260b57cec5SDimitry Andric } 10270b57cec5SDimitry Andric 10280b57cec5SDimitry Andric return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all, 10290b57cec5SDimitry Andric RelaxDefault); 10300b57cec5SDimitry Andric } 10310b57cec5SDimitry Andric 10320b57cec5SDimitry Andric // Extract the integer N from a string spelled "-dwarf-N", returning 0 10330b57cec5SDimitry Andric // on mismatch. The StringRef input (rather than an Arg) allows 10340b57cec5SDimitry Andric // for use by the "-Xassembler" option parser. 10350b57cec5SDimitry Andric static unsigned DwarfVersionNum(StringRef ArgValue) { 10360b57cec5SDimitry Andric return llvm::StringSwitch<unsigned>(ArgValue) 10370b57cec5SDimitry Andric .Case("-gdwarf-2", 2) 10380b57cec5SDimitry Andric .Case("-gdwarf-3", 3) 10390b57cec5SDimitry Andric .Case("-gdwarf-4", 4) 10400b57cec5SDimitry Andric .Case("-gdwarf-5", 5) 10410b57cec5SDimitry Andric .Default(0); 10420b57cec5SDimitry Andric } 10430b57cec5SDimitry Andric 1044fe6060f1SDimitry Andric // Find a DWARF format version option. 1045fe6060f1SDimitry Andric // This function is a complementary for DwarfVersionNum(). 1046fe6060f1SDimitry Andric static const Arg *getDwarfNArg(const ArgList &Args) { 1047fe6060f1SDimitry Andric return Args.getLastArg(options::OPT_gdwarf_2, options::OPT_gdwarf_3, 1048fe6060f1SDimitry Andric options::OPT_gdwarf_4, options::OPT_gdwarf_5, 1049fe6060f1SDimitry Andric options::OPT_gdwarf); 1050fe6060f1SDimitry Andric } 1051fe6060f1SDimitry Andric 10520b57cec5SDimitry Andric static void RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs, 10530b57cec5SDimitry Andric codegenoptions::DebugInfoKind DebugInfoKind, 10540b57cec5SDimitry Andric unsigned DwarfVersion, 10550b57cec5SDimitry Andric llvm::DebuggerKind DebuggerTuning) { 10560b57cec5SDimitry Andric switch (DebugInfoKind) { 10570b57cec5SDimitry Andric case codegenoptions::DebugDirectivesOnly: 10580b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-kind=line-directives-only"); 10590b57cec5SDimitry Andric break; 10600b57cec5SDimitry Andric case codegenoptions::DebugLineTablesOnly: 10610b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-kind=line-tables-only"); 10620b57cec5SDimitry Andric break; 1063480093f4SDimitry Andric case codegenoptions::DebugInfoConstructor: 1064480093f4SDimitry Andric CmdArgs.push_back("-debug-info-kind=constructor"); 1065480093f4SDimitry Andric break; 10660b57cec5SDimitry Andric case codegenoptions::LimitedDebugInfo: 10670b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-kind=limited"); 10680b57cec5SDimitry Andric break; 10690b57cec5SDimitry Andric case codegenoptions::FullDebugInfo: 10700b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-kind=standalone"); 10710b57cec5SDimitry Andric break; 1072e8d8bef9SDimitry Andric case codegenoptions::UnusedTypeInfo: 1073e8d8bef9SDimitry Andric CmdArgs.push_back("-debug-info-kind=unused-types"); 1074e8d8bef9SDimitry Andric break; 10750b57cec5SDimitry Andric default: 10760b57cec5SDimitry Andric break; 10770b57cec5SDimitry Andric } 10780b57cec5SDimitry Andric if (DwarfVersion > 0) 10790b57cec5SDimitry Andric CmdArgs.push_back( 10800b57cec5SDimitry Andric Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion))); 10810b57cec5SDimitry Andric switch (DebuggerTuning) { 10820b57cec5SDimitry Andric case llvm::DebuggerKind::GDB: 10830b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=gdb"); 10840b57cec5SDimitry Andric break; 10850b57cec5SDimitry Andric case llvm::DebuggerKind::LLDB: 10860b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=lldb"); 10870b57cec5SDimitry Andric break; 10880b57cec5SDimitry Andric case llvm::DebuggerKind::SCE: 10890b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=sce"); 10900b57cec5SDimitry Andric break; 1091fe6060f1SDimitry Andric case llvm::DebuggerKind::DBX: 1092fe6060f1SDimitry Andric CmdArgs.push_back("-debugger-tuning=dbx"); 1093fe6060f1SDimitry Andric break; 10940b57cec5SDimitry Andric default: 10950b57cec5SDimitry Andric break; 10960b57cec5SDimitry Andric } 10970b57cec5SDimitry Andric } 10980b57cec5SDimitry Andric 10990b57cec5SDimitry Andric static bool checkDebugInfoOption(const Arg *A, const ArgList &Args, 11000b57cec5SDimitry Andric const Driver &D, const ToolChain &TC) { 11010b57cec5SDimitry Andric assert(A && "Expected non-nullptr argument."); 11020b57cec5SDimitry Andric if (TC.supportsDebugInfoOption(A)) 11030b57cec5SDimitry Andric return true; 11040b57cec5SDimitry Andric D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target) 11050b57cec5SDimitry Andric << A->getAsString(Args) << TC.getTripleString(); 11060b57cec5SDimitry Andric return false; 11070b57cec5SDimitry Andric } 11080b57cec5SDimitry Andric 11090b57cec5SDimitry Andric static void RenderDebugInfoCompressionArgs(const ArgList &Args, 11100b57cec5SDimitry Andric ArgStringList &CmdArgs, 11110b57cec5SDimitry Andric const Driver &D, 11120b57cec5SDimitry Andric const ToolChain &TC) { 1113e8d8bef9SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_gz_EQ); 11140b57cec5SDimitry Andric if (!A) 11150b57cec5SDimitry Andric return; 11160b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) { 11170b57cec5SDimitry Andric StringRef Value = A->getValue(); 11180b57cec5SDimitry Andric if (Value == "none") { 11190b57cec5SDimitry Andric CmdArgs.push_back("--compress-debug-sections=none"); 112004eeddc0SDimitry Andric } else if (Value == "zlib") { 11210b57cec5SDimitry Andric if (llvm::zlib::isAvailable()) { 11220b57cec5SDimitry Andric CmdArgs.push_back( 11230b57cec5SDimitry Andric Args.MakeArgString("--compress-debug-sections=" + Twine(Value))); 11240b57cec5SDimitry Andric } else { 11250b57cec5SDimitry Andric D.Diag(diag::warn_debug_compression_unavailable); 11260b57cec5SDimitry Andric } 11270b57cec5SDimitry Andric } else { 11280b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 11290b57cec5SDimitry Andric << A->getOption().getName() << Value; 11300b57cec5SDimitry Andric } 11310b57cec5SDimitry Andric } 11320b57cec5SDimitry Andric } 11330b57cec5SDimitry Andric 11340b57cec5SDimitry Andric static const char *RelocationModelName(llvm::Reloc::Model Model) { 11350b57cec5SDimitry Andric switch (Model) { 11360b57cec5SDimitry Andric case llvm::Reloc::Static: 11370b57cec5SDimitry Andric return "static"; 11380b57cec5SDimitry Andric case llvm::Reloc::PIC_: 11390b57cec5SDimitry Andric return "pic"; 11400b57cec5SDimitry Andric case llvm::Reloc::DynamicNoPIC: 11410b57cec5SDimitry Andric return "dynamic-no-pic"; 11420b57cec5SDimitry Andric case llvm::Reloc::ROPI: 11430b57cec5SDimitry Andric return "ropi"; 11440b57cec5SDimitry Andric case llvm::Reloc::RWPI: 11450b57cec5SDimitry Andric return "rwpi"; 11460b57cec5SDimitry Andric case llvm::Reloc::ROPI_RWPI: 11470b57cec5SDimitry Andric return "ropi-rwpi"; 11480b57cec5SDimitry Andric } 11490b57cec5SDimitry Andric llvm_unreachable("Unknown Reloc::Model kind"); 11500b57cec5SDimitry Andric } 1151e8d8bef9SDimitry Andric static void handleAMDGPUCodeObjectVersionOptions(const Driver &D, 1152e8d8bef9SDimitry Andric const ArgList &Args, 1153e8d8bef9SDimitry Andric ArgStringList &CmdArgs) { 1154fe6060f1SDimitry Andric // If no version was requested by the user, use the default value from the 1155fe6060f1SDimitry Andric // back end. This is consistent with the value returned from 1156fe6060f1SDimitry Andric // getAMDGPUCodeObjectVersion. This lets clang emit IR for amdgpu without 1157fe6060f1SDimitry Andric // requiring the corresponding llvm to have the AMDGPU target enabled, 1158fe6060f1SDimitry Andric // provided the user (e.g. front end tests) can use the default. 1159fe6060f1SDimitry Andric if (haveAMDGPUCodeObjectVersionArgument(D, Args)) { 1160fe6060f1SDimitry Andric unsigned CodeObjVer = getAMDGPUCodeObjectVersion(D, Args); 1161e8d8bef9SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, 1162e8d8bef9SDimitry Andric Args.MakeArgString(Twine("--amdhsa-code-object-version=") + 1163e8d8bef9SDimitry Andric Twine(CodeObjVer))); 1164e8d8bef9SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, "-mllvm"); 1165e8d8bef9SDimitry Andric } 1166fe6060f1SDimitry Andric } 11670b57cec5SDimitry Andric 11680b57cec5SDimitry Andric void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, 11690b57cec5SDimitry Andric const Driver &D, const ArgList &Args, 11700b57cec5SDimitry Andric ArgStringList &CmdArgs, 11710b57cec5SDimitry Andric const InputInfo &Output, 11720b57cec5SDimitry Andric const InputInfoList &Inputs) const { 11730b57cec5SDimitry Andric const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU(); 11740b57cec5SDimitry Andric 11750b57cec5SDimitry Andric CheckPreprocessingOptions(D, Args); 11760b57cec5SDimitry Andric 11770b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_C); 11780b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_CC); 11790b57cec5SDimitry Andric 11800b57cec5SDimitry Andric // Handle dependency file generation. 1181a7dea167SDimitry Andric Arg *ArgM = Args.getLastArg(options::OPT_MM); 1182a7dea167SDimitry Andric if (!ArgM) 1183a7dea167SDimitry Andric ArgM = Args.getLastArg(options::OPT_M); 1184a7dea167SDimitry Andric Arg *ArgMD = Args.getLastArg(options::OPT_MMD); 1185a7dea167SDimitry Andric if (!ArgMD) 1186a7dea167SDimitry Andric ArgMD = Args.getLastArg(options::OPT_MD); 1187a7dea167SDimitry Andric 1188a7dea167SDimitry Andric // -M and -MM imply -w. 1189a7dea167SDimitry Andric if (ArgM) 1190a7dea167SDimitry Andric CmdArgs.push_back("-w"); 1191a7dea167SDimitry Andric else 1192a7dea167SDimitry Andric ArgM = ArgMD; 1193a7dea167SDimitry Andric 1194a7dea167SDimitry Andric if (ArgM) { 11950b57cec5SDimitry Andric // Determine the output location. 11960b57cec5SDimitry Andric const char *DepFile; 11970b57cec5SDimitry Andric if (Arg *MF = Args.getLastArg(options::OPT_MF)) { 11980b57cec5SDimitry Andric DepFile = MF->getValue(); 11990b57cec5SDimitry Andric C.addFailureResultFile(DepFile, &JA); 12000b57cec5SDimitry Andric } else if (Output.getType() == types::TY_Dependencies) { 12010b57cec5SDimitry Andric DepFile = Output.getFilename(); 1202a7dea167SDimitry Andric } else if (!ArgMD) { 12030b57cec5SDimitry Andric DepFile = "-"; 12040b57cec5SDimitry Andric } else { 12050b57cec5SDimitry Andric DepFile = getDependencyFileName(Args, Inputs); 12060b57cec5SDimitry Andric C.addFailureResultFile(DepFile, &JA); 12070b57cec5SDimitry Andric } 12080b57cec5SDimitry Andric CmdArgs.push_back("-dependency-file"); 12090b57cec5SDimitry Andric CmdArgs.push_back(DepFile); 12100b57cec5SDimitry Andric 1211a7dea167SDimitry Andric bool HasTarget = false; 1212a7dea167SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_MT, options::OPT_MQ)) { 1213a7dea167SDimitry Andric HasTarget = true; 1214a7dea167SDimitry Andric A->claim(); 1215a7dea167SDimitry Andric if (A->getOption().matches(options::OPT_MT)) { 1216a7dea167SDimitry Andric A->render(Args, CmdArgs); 1217a7dea167SDimitry Andric } else { 1218a7dea167SDimitry Andric CmdArgs.push_back("-MT"); 1219a7dea167SDimitry Andric SmallString<128> Quoted; 1220a7dea167SDimitry Andric QuoteTarget(A->getValue(), Quoted); 1221a7dea167SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Quoted)); 1222a7dea167SDimitry Andric } 1223a7dea167SDimitry Andric } 1224a7dea167SDimitry Andric 12250b57cec5SDimitry Andric // Add a default target if one wasn't specified. 1226a7dea167SDimitry Andric if (!HasTarget) { 12270b57cec5SDimitry Andric const char *DepTarget; 12280b57cec5SDimitry Andric 12290b57cec5SDimitry Andric // If user provided -o, that is the dependency target, except 12300b57cec5SDimitry Andric // when we are only generating a dependency file. 12310b57cec5SDimitry Andric Arg *OutputOpt = Args.getLastArg(options::OPT_o); 12320b57cec5SDimitry Andric if (OutputOpt && Output.getType() != types::TY_Dependencies) { 12330b57cec5SDimitry Andric DepTarget = OutputOpt->getValue(); 12340b57cec5SDimitry Andric } else { 12350b57cec5SDimitry Andric // Otherwise derive from the base input. 12360b57cec5SDimitry Andric // 12370b57cec5SDimitry Andric // FIXME: This should use the computed output file location. 12380b57cec5SDimitry Andric SmallString<128> P(Inputs[0].getBaseInput()); 12390b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "o"); 12400b57cec5SDimitry Andric DepTarget = Args.MakeArgString(llvm::sys::path::filename(P)); 12410b57cec5SDimitry Andric } 12420b57cec5SDimitry Andric 12430b57cec5SDimitry Andric CmdArgs.push_back("-MT"); 12440b57cec5SDimitry Andric SmallString<128> Quoted; 12450b57cec5SDimitry Andric QuoteTarget(DepTarget, Quoted); 12460b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Quoted)); 12470b57cec5SDimitry Andric } 12480b57cec5SDimitry Andric 1249a7dea167SDimitry Andric if (ArgM->getOption().matches(options::OPT_M) || 1250a7dea167SDimitry Andric ArgM->getOption().matches(options::OPT_MD)) 12510b57cec5SDimitry Andric CmdArgs.push_back("-sys-header-deps"); 12520b57cec5SDimitry Andric if ((isa<PrecompileJobAction>(JA) && 12530b57cec5SDimitry Andric !Args.hasArg(options::OPT_fno_module_file_deps)) || 12540b57cec5SDimitry Andric Args.hasArg(options::OPT_fmodule_file_deps)) 12550b57cec5SDimitry Andric CmdArgs.push_back("-module-file-deps"); 12560b57cec5SDimitry Andric } 12570b57cec5SDimitry Andric 12580b57cec5SDimitry Andric if (Args.hasArg(options::OPT_MG)) { 1259a7dea167SDimitry Andric if (!ArgM || ArgM->getOption().matches(options::OPT_MD) || 1260a7dea167SDimitry Andric ArgM->getOption().matches(options::OPT_MMD)) 12610b57cec5SDimitry Andric D.Diag(diag::err_drv_mg_requires_m_or_mm); 12620b57cec5SDimitry Andric CmdArgs.push_back("-MG"); 12630b57cec5SDimitry Andric } 12640b57cec5SDimitry Andric 12650b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_MP); 12660b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_MV); 12670b57cec5SDimitry Andric 12685ffd83dbSDimitry Andric // Add offload include arguments specific for CUDA/HIP. This must happen 12695ffd83dbSDimitry Andric // before we -I or -include anything else, because we must pick up the 12705ffd83dbSDimitry Andric // CUDA/HIP headers from the particular CUDA/ROCm installation, rather than 12715ffd83dbSDimitry Andric // from e.g. /usr/local/include. 12720b57cec5SDimitry Andric if (JA.isOffloading(Action::OFK_Cuda)) 12730b57cec5SDimitry Andric getToolChain().AddCudaIncludeArgs(Args, CmdArgs); 12745ffd83dbSDimitry Andric if (JA.isOffloading(Action::OFK_HIP)) 12755ffd83dbSDimitry Andric getToolChain().AddHIPIncludeArgs(Args, CmdArgs); 12760b57cec5SDimitry Andric 12770b57cec5SDimitry Andric // If we are offloading to a target via OpenMP we need to include the 12780b57cec5SDimitry Andric // openmp_wrappers folder which contains alternative system headers. 12790b57cec5SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_OpenMP) && 128069ade1e0SDimitry Andric (getToolChain().getTriple().isNVPTX() || 128169ade1e0SDimitry Andric getToolChain().getTriple().isAMDGCN())) { 12820b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_nobuiltininc)) { 12830b57cec5SDimitry Andric // Add openmp_wrappers/* to our system include path. This lets us wrap 12840b57cec5SDimitry Andric // standard library headers. 12850b57cec5SDimitry Andric SmallString<128> P(D.ResourceDir); 12860b57cec5SDimitry Andric llvm::sys::path::append(P, "include"); 12870b57cec5SDimitry Andric llvm::sys::path::append(P, "openmp_wrappers"); 12880b57cec5SDimitry Andric CmdArgs.push_back("-internal-isystem"); 12890b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 12900b57cec5SDimitry Andric } 12910b57cec5SDimitry Andric 12920b57cec5SDimitry Andric CmdArgs.push_back("-include"); 12935ffd83dbSDimitry Andric CmdArgs.push_back("__clang_openmp_device_functions.h"); 12940b57cec5SDimitry Andric } 12950b57cec5SDimitry Andric 12960b57cec5SDimitry Andric // Add -i* options, and automatically translate to 12970b57cec5SDimitry Andric // -include-pch/-include-pth for transparent PCH support. It's 12980b57cec5SDimitry Andric // wonky, but we include looking for .gch so we can support seamless 12990b57cec5SDimitry Andric // replacement into a build system already set up to be generating 13000b57cec5SDimitry Andric // .gch files. 13010b57cec5SDimitry Andric 13020b57cec5SDimitry Andric if (getToolChain().getDriver().IsCLMode()) { 13030b57cec5SDimitry Andric const Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc); 13040b57cec5SDimitry Andric const Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu); 13050b57cec5SDimitry Andric if (YcArg && JA.getKind() >= Action::PrecompileJobClass && 13060b57cec5SDimitry Andric JA.getKind() <= Action::AssembleJobClass) { 13070b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-building-pch-with-obj")); 1308f91b0c1cSDimitry Andric // -fpch-instantiate-templates is the default when creating 1309f91b0c1cSDimitry Andric // precomp using /Yc 1310f91b0c1cSDimitry Andric if (Args.hasFlag(options::OPT_fpch_instantiate_templates, 1311f91b0c1cSDimitry Andric options::OPT_fno_pch_instantiate_templates, true)) 13125ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fpch-instantiate-templates")); 13130b57cec5SDimitry Andric } 13140b57cec5SDimitry Andric if (YcArg || YuArg) { 13150b57cec5SDimitry Andric StringRef ThroughHeader = YcArg ? YcArg->getValue() : YuArg->getValue(); 13160b57cec5SDimitry Andric if (!isa<PrecompileJobAction>(JA)) { 13170b57cec5SDimitry Andric CmdArgs.push_back("-include-pch"); 13180b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath( 13190b57cec5SDimitry Andric C, !ThroughHeader.empty() 13200b57cec5SDimitry Andric ? ThroughHeader 13210b57cec5SDimitry Andric : llvm::sys::path::filename(Inputs[0].getBaseInput())))); 13220b57cec5SDimitry Andric } 13230b57cec5SDimitry Andric 13240b57cec5SDimitry Andric if (ThroughHeader.empty()) { 13250b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 13260b57cec5SDimitry Andric Twine("-pch-through-hdrstop-") + (YcArg ? "create" : "use"))); 13270b57cec5SDimitry Andric } else { 13280b57cec5SDimitry Andric CmdArgs.push_back( 13290b57cec5SDimitry Andric Args.MakeArgString(Twine("-pch-through-header=") + ThroughHeader)); 13300b57cec5SDimitry Andric } 13310b57cec5SDimitry Andric } 13320b57cec5SDimitry Andric } 13330b57cec5SDimitry Andric 13340b57cec5SDimitry Andric bool RenderedImplicitInclude = false; 13350b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) { 13360b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_include)) { 13370b57cec5SDimitry Andric // Handling of gcc-style gch precompiled headers. 13380b57cec5SDimitry Andric bool IsFirstImplicitInclude = !RenderedImplicitInclude; 13390b57cec5SDimitry Andric RenderedImplicitInclude = true; 13400b57cec5SDimitry Andric 13410b57cec5SDimitry Andric bool FoundPCH = false; 13420b57cec5SDimitry Andric SmallString<128> P(A->getValue()); 13430b57cec5SDimitry Andric // We want the files to have a name like foo.h.pch. Add a dummy extension 13440b57cec5SDimitry Andric // so that replace_extension does the right thing. 13450b57cec5SDimitry Andric P += ".dummy"; 13460b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "pch"); 13470b57cec5SDimitry Andric if (llvm::sys::fs::exists(P)) 13480b57cec5SDimitry Andric FoundPCH = true; 13490b57cec5SDimitry Andric 13500b57cec5SDimitry Andric if (!FoundPCH) { 13510b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "gch"); 13520b57cec5SDimitry Andric if (llvm::sys::fs::exists(P)) { 13530b57cec5SDimitry Andric FoundPCH = true; 13540b57cec5SDimitry Andric } 13550b57cec5SDimitry Andric } 13560b57cec5SDimitry Andric 13570b57cec5SDimitry Andric if (FoundPCH) { 13580b57cec5SDimitry Andric if (IsFirstImplicitInclude) { 13590b57cec5SDimitry Andric A->claim(); 13600b57cec5SDimitry Andric CmdArgs.push_back("-include-pch"); 13610b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 13620b57cec5SDimitry Andric continue; 13630b57cec5SDimitry Andric } else { 13640b57cec5SDimitry Andric // Ignore the PCH if not first on command line and emit warning. 13650b57cec5SDimitry Andric D.Diag(diag::warn_drv_pch_not_first_include) << P 13660b57cec5SDimitry Andric << A->getAsString(Args); 13670b57cec5SDimitry Andric } 13680b57cec5SDimitry Andric } 13690b57cec5SDimitry Andric } else if (A->getOption().matches(options::OPT_isystem_after)) { 13700b57cec5SDimitry Andric // Handling of paths which must come late. These entries are handled by 13710b57cec5SDimitry Andric // the toolchain itself after the resource dir is inserted in the right 13720b57cec5SDimitry Andric // search order. 13730b57cec5SDimitry Andric // Do not claim the argument so that the use of the argument does not 13740b57cec5SDimitry Andric // silently go unnoticed on toolchains which do not honour the option. 13750b57cec5SDimitry Andric continue; 1376a7dea167SDimitry Andric } else if (A->getOption().matches(options::OPT_stdlibxx_isystem)) { 1377a7dea167SDimitry Andric // Translated to -internal-isystem by the driver, no need to pass to cc1. 1378a7dea167SDimitry Andric continue; 13790b57cec5SDimitry Andric } 13800b57cec5SDimitry Andric 13810b57cec5SDimitry Andric // Not translated, render as usual. 13820b57cec5SDimitry Andric A->claim(); 13830b57cec5SDimitry Andric A->render(Args, CmdArgs); 13840b57cec5SDimitry Andric } 13850b57cec5SDimitry Andric 13860b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, 13870b57cec5SDimitry Andric {options::OPT_D, options::OPT_U, options::OPT_I_Group, 13880b57cec5SDimitry Andric options::OPT_F, options::OPT_index_header_map}); 13890b57cec5SDimitry Andric 13900b57cec5SDimitry Andric // Add -Wp, and -Xpreprocessor if using the preprocessor. 13910b57cec5SDimitry Andric 13920b57cec5SDimitry Andric // FIXME: There is a very unfortunate problem here, some troubled 13930b57cec5SDimitry Andric // souls abuse -Wp, to pass preprocessor options in gcc syntax. To 13940b57cec5SDimitry Andric // really support that we would have to parse and then translate 13950b57cec5SDimitry Andric // those options. :( 13960b57cec5SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA, 13970b57cec5SDimitry Andric options::OPT_Xpreprocessor); 13980b57cec5SDimitry Andric 13990b57cec5SDimitry Andric // -I- is a deprecated GCC feature, reject it. 14000b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_I_)) 14010b57cec5SDimitry Andric D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args); 14020b57cec5SDimitry Andric 14030b57cec5SDimitry Andric // If we have a --sysroot, and don't have an explicit -isysroot flag, add an 14040b57cec5SDimitry Andric // -isysroot to the CC1 invocation. 14050b57cec5SDimitry Andric StringRef sysroot = C.getSysRoot(); 14060b57cec5SDimitry Andric if (sysroot != "") { 14070b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_isysroot)) { 14080b57cec5SDimitry Andric CmdArgs.push_back("-isysroot"); 14090b57cec5SDimitry Andric CmdArgs.push_back(C.getArgs().MakeArgString(sysroot)); 14100b57cec5SDimitry Andric } 14110b57cec5SDimitry Andric } 14120b57cec5SDimitry Andric 14130b57cec5SDimitry Andric // Parse additional include paths from environment variables. 14140b57cec5SDimitry Andric // FIXME: We should probably sink the logic for handling these from the 14150b57cec5SDimitry Andric // frontend into the driver. It will allow deleting 4 otherwise unused flags. 14160b57cec5SDimitry Andric // CPATH - included following the user specified includes (but prior to 14170b57cec5SDimitry Andric // builtin and standard includes). 14180b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-I", "CPATH"); 14190b57cec5SDimitry Andric // C_INCLUDE_PATH - system includes enabled when compiling C. 14200b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH"); 14210b57cec5SDimitry Andric // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++. 14220b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH"); 14230b57cec5SDimitry Andric // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC. 14240b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH"); 14250b57cec5SDimitry Andric // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++. 14260b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH"); 14270b57cec5SDimitry Andric 14280b57cec5SDimitry Andric // While adding the include arguments, we also attempt to retrieve the 14290b57cec5SDimitry Andric // arguments of related offloading toolchains or arguments that are specific 14300b57cec5SDimitry Andric // of an offloading programming model. 14310b57cec5SDimitry Andric 14320b57cec5SDimitry Andric // Add C++ include arguments, if needed. 1433a7dea167SDimitry Andric if (types::isCXX(Inputs[0].getType())) { 1434a7dea167SDimitry Andric bool HasStdlibxxIsystem = Args.hasArg(options::OPT_stdlibxx_isystem); 1435a7dea167SDimitry Andric forAllAssociatedToolChains( 1436a7dea167SDimitry Andric C, JA, getToolChain(), 1437a7dea167SDimitry Andric [&Args, &CmdArgs, HasStdlibxxIsystem](const ToolChain &TC) { 1438a7dea167SDimitry Andric HasStdlibxxIsystem ? TC.AddClangCXXStdlibIsystemArgs(Args, CmdArgs) 1439a7dea167SDimitry Andric : TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs); 14400b57cec5SDimitry Andric }); 1441a7dea167SDimitry Andric } 14420b57cec5SDimitry Andric 14430b57cec5SDimitry Andric // Add system include arguments for all targets but IAMCU. 14440b57cec5SDimitry Andric if (!IsIAMCU) 14450b57cec5SDimitry Andric forAllAssociatedToolChains(C, JA, getToolChain(), 14460b57cec5SDimitry Andric [&Args, &CmdArgs](const ToolChain &TC) { 14470b57cec5SDimitry Andric TC.AddClangSystemIncludeArgs(Args, CmdArgs); 14480b57cec5SDimitry Andric }); 14490b57cec5SDimitry Andric else { 14500b57cec5SDimitry Andric // For IAMCU add special include arguments. 14510b57cec5SDimitry Andric getToolChain().AddIAMCUIncludeArgs(Args, CmdArgs); 14520b57cec5SDimitry Andric } 1453480093f4SDimitry Andric 1454480093f4SDimitry Andric addMacroPrefixMapArg(D, Args, CmdArgs); 1455fe6060f1SDimitry Andric addCoveragePrefixMapArg(D, Args, CmdArgs); 14560b57cec5SDimitry Andric } 14570b57cec5SDimitry Andric 14580b57cec5SDimitry Andric // FIXME: Move to target hook. 14590b57cec5SDimitry Andric static bool isSignedCharDefault(const llvm::Triple &Triple) { 14600b57cec5SDimitry Andric switch (Triple.getArch()) { 14610b57cec5SDimitry Andric default: 14620b57cec5SDimitry Andric return true; 14630b57cec5SDimitry Andric 14640b57cec5SDimitry Andric case llvm::Triple::aarch64: 1465480093f4SDimitry Andric case llvm::Triple::aarch64_32: 14660b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 14670b57cec5SDimitry Andric case llvm::Triple::arm: 14680b57cec5SDimitry Andric case llvm::Triple::armeb: 14690b57cec5SDimitry Andric case llvm::Triple::thumb: 14700b57cec5SDimitry Andric case llvm::Triple::thumbeb: 14710b57cec5SDimitry Andric if (Triple.isOSDarwin() || Triple.isOSWindows()) 14720b57cec5SDimitry Andric return true; 14730b57cec5SDimitry Andric return false; 14740b57cec5SDimitry Andric 14750b57cec5SDimitry Andric case llvm::Triple::ppc: 14760b57cec5SDimitry Andric case llvm::Triple::ppc64: 14770b57cec5SDimitry Andric if (Triple.isOSDarwin()) 14780b57cec5SDimitry Andric return true; 14790b57cec5SDimitry Andric return false; 14800b57cec5SDimitry Andric 14810b57cec5SDimitry Andric case llvm::Triple::hexagon: 1482e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 14830b57cec5SDimitry Andric case llvm::Triple::ppc64le: 14840b57cec5SDimitry Andric case llvm::Triple::riscv32: 14850b57cec5SDimitry Andric case llvm::Triple::riscv64: 14860b57cec5SDimitry Andric case llvm::Triple::systemz: 14870b57cec5SDimitry Andric case llvm::Triple::xcore: 14880b57cec5SDimitry Andric return false; 14890b57cec5SDimitry Andric } 14900b57cec5SDimitry Andric } 14910b57cec5SDimitry Andric 1492480093f4SDimitry Andric static bool hasMultipleInvocations(const llvm::Triple &Triple, 1493480093f4SDimitry Andric const ArgList &Args) { 1494480093f4SDimitry Andric // Supported only on Darwin where we invoke the compiler multiple times 1495480093f4SDimitry Andric // followed by an invocation to lipo. 1496480093f4SDimitry Andric if (!Triple.isOSDarwin()) 1497480093f4SDimitry Andric return false; 1498480093f4SDimitry Andric // If more than one "-arch <arch>" is specified, we're targeting multiple 1499480093f4SDimitry Andric // architectures resulting in a fat binary. 1500480093f4SDimitry Andric return Args.getAllArgValues(options::OPT_arch).size() > 1; 1501480093f4SDimitry Andric } 1502480093f4SDimitry Andric 1503480093f4SDimitry Andric static bool checkRemarksOptions(const Driver &D, const ArgList &Args, 1504480093f4SDimitry Andric const llvm::Triple &Triple) { 1505480093f4SDimitry Andric // When enabling remarks, we need to error if: 1506480093f4SDimitry Andric // * The remark file is specified but we're targeting multiple architectures, 1507480093f4SDimitry Andric // which means more than one remark file is being generated. 1508480093f4SDimitry Andric bool hasMultipleInvocations = ::hasMultipleInvocations(Triple, Args); 1509480093f4SDimitry Andric bool hasExplicitOutputFile = 1510480093f4SDimitry Andric Args.getLastArg(options::OPT_foptimization_record_file_EQ); 1511480093f4SDimitry Andric if (hasMultipleInvocations && hasExplicitOutputFile) { 1512480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_output_with_multiple_archs) 1513480093f4SDimitry Andric << "-foptimization-record-file"; 1514480093f4SDimitry Andric return false; 1515480093f4SDimitry Andric } 1516480093f4SDimitry Andric return true; 1517480093f4SDimitry Andric } 1518480093f4SDimitry Andric 1519480093f4SDimitry Andric static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs, 1520480093f4SDimitry Andric const llvm::Triple &Triple, 1521480093f4SDimitry Andric const InputInfo &Input, 1522480093f4SDimitry Andric const InputInfo &Output, const JobAction &JA) { 1523480093f4SDimitry Andric StringRef Format = "yaml"; 1524480093f4SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fsave_optimization_record_EQ)) 1525480093f4SDimitry Andric Format = A->getValue(); 1526480093f4SDimitry Andric 1527480093f4SDimitry Andric CmdArgs.push_back("-opt-record-file"); 1528480093f4SDimitry Andric 1529480093f4SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ); 1530480093f4SDimitry Andric if (A) { 1531480093f4SDimitry Andric CmdArgs.push_back(A->getValue()); 1532480093f4SDimitry Andric } else { 1533480093f4SDimitry Andric bool hasMultipleArchs = 1534480093f4SDimitry Andric Triple.isOSDarwin() && // Only supported on Darwin platforms. 1535480093f4SDimitry Andric Args.getAllArgValues(options::OPT_arch).size() > 1; 1536480093f4SDimitry Andric 1537480093f4SDimitry Andric SmallString<128> F; 1538480093f4SDimitry Andric 1539480093f4SDimitry Andric if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) { 1540480093f4SDimitry Andric if (Arg *FinalOutput = Args.getLastArg(options::OPT_o)) 1541480093f4SDimitry Andric F = FinalOutput->getValue(); 1542480093f4SDimitry Andric } else { 1543480093f4SDimitry Andric if (Format != "yaml" && // For YAML, keep the original behavior. 1544480093f4SDimitry Andric Triple.isOSDarwin() && // Enable this only on darwin, since it's the only platform supporting .dSYM bundles. 1545480093f4SDimitry Andric Output.isFilename()) 1546480093f4SDimitry Andric F = Output.getFilename(); 1547480093f4SDimitry Andric } 1548480093f4SDimitry Andric 1549480093f4SDimitry Andric if (F.empty()) { 1550480093f4SDimitry Andric // Use the input filename. 1551480093f4SDimitry Andric F = llvm::sys::path::stem(Input.getBaseInput()); 1552480093f4SDimitry Andric 1553480093f4SDimitry Andric // If we're compiling for an offload architecture (i.e. a CUDA device), 1554480093f4SDimitry Andric // we need to make the file name for the device compilation different 1555480093f4SDimitry Andric // from the host compilation. 1556480093f4SDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_None) && 1557480093f4SDimitry Andric !JA.isDeviceOffloading(Action::OFK_Host)) { 1558480093f4SDimitry Andric llvm::sys::path::replace_extension(F, ""); 1559480093f4SDimitry Andric F += Action::GetOffloadingFileNamePrefix(JA.getOffloadingDeviceKind(), 1560480093f4SDimitry Andric Triple.normalize()); 1561480093f4SDimitry Andric F += "-"; 1562480093f4SDimitry Andric F += JA.getOffloadingArch(); 1563480093f4SDimitry Andric } 1564480093f4SDimitry Andric } 1565480093f4SDimitry Andric 1566480093f4SDimitry Andric // If we're having more than one "-arch", we should name the files 1567480093f4SDimitry Andric // differently so that every cc1 invocation writes to a different file. 1568480093f4SDimitry Andric // We're doing that by appending "-<arch>" with "<arch>" being the arch 1569480093f4SDimitry Andric // name from the triple. 1570480093f4SDimitry Andric if (hasMultipleArchs) { 1571480093f4SDimitry Andric // First, remember the extension. 1572480093f4SDimitry Andric SmallString<64> OldExtension = llvm::sys::path::extension(F); 1573480093f4SDimitry Andric // then, remove it. 1574480093f4SDimitry Andric llvm::sys::path::replace_extension(F, ""); 1575480093f4SDimitry Andric // attach -<arch> to it. 1576480093f4SDimitry Andric F += "-"; 1577480093f4SDimitry Andric F += Triple.getArchName(); 1578480093f4SDimitry Andric // put back the extension. 1579480093f4SDimitry Andric llvm::sys::path::replace_extension(F, OldExtension); 1580480093f4SDimitry Andric } 1581480093f4SDimitry Andric 1582480093f4SDimitry Andric SmallString<32> Extension; 1583480093f4SDimitry Andric Extension += "opt."; 1584480093f4SDimitry Andric Extension += Format; 1585480093f4SDimitry Andric 1586480093f4SDimitry Andric llvm::sys::path::replace_extension(F, Extension); 1587480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(F)); 1588480093f4SDimitry Andric } 1589480093f4SDimitry Andric 1590480093f4SDimitry Andric if (const Arg *A = 1591480093f4SDimitry Andric Args.getLastArg(options::OPT_foptimization_record_passes_EQ)) { 1592480093f4SDimitry Andric CmdArgs.push_back("-opt-record-passes"); 1593480093f4SDimitry Andric CmdArgs.push_back(A->getValue()); 1594480093f4SDimitry Andric } 1595480093f4SDimitry Andric 1596480093f4SDimitry Andric if (!Format.empty()) { 1597480093f4SDimitry Andric CmdArgs.push_back("-opt-record-format"); 1598480093f4SDimitry Andric CmdArgs.push_back(Format.data()); 1599480093f4SDimitry Andric } 1600480093f4SDimitry Andric } 1601480093f4SDimitry Andric 1602fe6060f1SDimitry Andric void AddAAPCSVolatileBitfieldArgs(const ArgList &Args, ArgStringList &CmdArgs) { 1603fe6060f1SDimitry Andric if (!Args.hasFlag(options::OPT_faapcs_bitfield_width, 1604fe6060f1SDimitry Andric options::OPT_fno_aapcs_bitfield_width, true)) 1605fe6060f1SDimitry Andric CmdArgs.push_back("-fno-aapcs-bitfield-width"); 1606fe6060f1SDimitry Andric 1607fe6060f1SDimitry Andric if (Args.getLastArg(options::OPT_ForceAAPCSBitfieldLoad)) 1608fe6060f1SDimitry Andric CmdArgs.push_back("-faapcs-bitfield-load"); 1609fe6060f1SDimitry Andric } 1610fe6060f1SDimitry Andric 16110b57cec5SDimitry Andric namespace { 1612349cc55cSDimitry Andric void RenderARMABI(const Driver &D, const llvm::Triple &Triple, 1613349cc55cSDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 16140b57cec5SDimitry Andric // Select the ABI to use. 16150b57cec5SDimitry Andric // FIXME: Support -meabi. 16160b57cec5SDimitry Andric // FIXME: Parts of this are duplicated in the backend, unify this somehow. 16170b57cec5SDimitry Andric const char *ABIName = nullptr; 16180b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) { 16190b57cec5SDimitry Andric ABIName = A->getValue(); 16200b57cec5SDimitry Andric } else { 1621349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false); 16220b57cec5SDimitry Andric ABIName = llvm::ARM::computeDefaultTargetABI(Triple, CPU).data(); 16230b57cec5SDimitry Andric } 16240b57cec5SDimitry Andric 16250b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 16260b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 16270b57cec5SDimitry Andric } 16280b57cec5SDimitry Andric } 16290b57cec5SDimitry Andric 16301fd87a68SDimitry Andric static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args, 16314824e7fdSDimitry Andric ArgStringList &CmdArgs, bool isAArch64) { 16324824e7fdSDimitry Andric const Arg *A = isAArch64 16334824e7fdSDimitry Andric ? Args.getLastArg(options::OPT_msign_return_address_EQ, 16344824e7fdSDimitry Andric options::OPT_mbranch_protection_EQ) 16354824e7fdSDimitry Andric : Args.getLastArg(options::OPT_mbranch_protection_EQ); 16364824e7fdSDimitry Andric if (!A) 16374824e7fdSDimitry Andric return; 16384824e7fdSDimitry Andric 16391fd87a68SDimitry Andric const Driver &D = TC.getDriver(); 16401fd87a68SDimitry Andric const llvm::Triple &Triple = TC.getEffectiveTriple(); 16411fd87a68SDimitry Andric if (!(isAArch64 || (Triple.isArmT32() && Triple.isArmMClass()))) 1642d56accc7SDimitry Andric D.Diag(diag::warn_incompatible_branch_protection_option) 16431fd87a68SDimitry Andric << Triple.getArchName(); 16441fd87a68SDimitry Andric 16454824e7fdSDimitry Andric StringRef Scope, Key; 16464824e7fdSDimitry Andric bool IndirectBranches; 16474824e7fdSDimitry Andric 16484824e7fdSDimitry Andric if (A->getOption().matches(options::OPT_msign_return_address_EQ)) { 16494824e7fdSDimitry Andric Scope = A->getValue(); 16504824e7fdSDimitry Andric if (!Scope.equals("none") && !Scope.equals("non-leaf") && 16514824e7fdSDimitry Andric !Scope.equals("all")) 16524824e7fdSDimitry Andric D.Diag(diag::err_invalid_branch_protection) 16534824e7fdSDimitry Andric << Scope << A->getAsString(Args); 16544824e7fdSDimitry Andric Key = "a_key"; 16554824e7fdSDimitry Andric IndirectBranches = false; 16564824e7fdSDimitry Andric } else { 16574824e7fdSDimitry Andric StringRef DiagMsg; 16584824e7fdSDimitry Andric llvm::ARM::ParsedBranchProtection PBP; 16594824e7fdSDimitry Andric if (!llvm::ARM::parseBranchProtection(A->getValue(), PBP, DiagMsg)) 16604824e7fdSDimitry Andric D.Diag(diag::err_invalid_branch_protection) 16614824e7fdSDimitry Andric << DiagMsg << A->getAsString(Args); 16624824e7fdSDimitry Andric if (!isAArch64 && PBP.Key == "b_key") 16634824e7fdSDimitry Andric D.Diag(diag::warn_unsupported_branch_protection) 16644824e7fdSDimitry Andric << "b-key" << A->getAsString(Args); 16654824e7fdSDimitry Andric Scope = PBP.Scope; 16664824e7fdSDimitry Andric Key = PBP.Key; 16674824e7fdSDimitry Andric IndirectBranches = PBP.BranchTargetEnforcement; 16684824e7fdSDimitry Andric } 16694824e7fdSDimitry Andric 16704824e7fdSDimitry Andric CmdArgs.push_back( 16714824e7fdSDimitry Andric Args.MakeArgString(Twine("-msign-return-address=") + Scope)); 16724824e7fdSDimitry Andric if (!Scope.equals("none")) 16734824e7fdSDimitry Andric CmdArgs.push_back( 16744824e7fdSDimitry Andric Args.MakeArgString(Twine("-msign-return-address-key=") + Key)); 16754824e7fdSDimitry Andric if (IndirectBranches) 16764824e7fdSDimitry Andric CmdArgs.push_back("-mbranch-target-enforce"); 16774824e7fdSDimitry Andric } 16784824e7fdSDimitry Andric 16790b57cec5SDimitry Andric void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args, 16800b57cec5SDimitry Andric ArgStringList &CmdArgs, bool KernelOrKext) const { 1681349cc55cSDimitry Andric RenderARMABI(getToolChain().getDriver(), Triple, Args, CmdArgs); 16820b57cec5SDimitry Andric 16830b57cec5SDimitry Andric // Determine floating point ABI from the options & target defaults. 16840b57cec5SDimitry Andric arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args); 16850b57cec5SDimitry Andric if (ABI == arm::FloatABI::Soft) { 16860b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 16870b57cec5SDimitry Andric // FIXME: This changes CPP defines, we need -target-soft-float. 16880b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 16890b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 16900b57cec5SDimitry Andric CmdArgs.push_back("soft"); 16910b57cec5SDimitry Andric } else if (ABI == arm::FloatABI::SoftFP) { 16920b57cec5SDimitry Andric // Floating point operations are hard, but argument passing is soft. 16930b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 16940b57cec5SDimitry Andric CmdArgs.push_back("soft"); 16950b57cec5SDimitry Andric } else { 16960b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 16970b57cec5SDimitry Andric assert(ABI == arm::FloatABI::Hard && "Invalid float abi!"); 16980b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 16990b57cec5SDimitry Andric CmdArgs.push_back("hard"); 17000b57cec5SDimitry Andric } 17010b57cec5SDimitry Andric 17020b57cec5SDimitry Andric // Forward the -mglobal-merge option for explicit control over the pass. 17030b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, 17040b57cec5SDimitry Andric options::OPT_mno_global_merge)) { 17050b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 17060b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_global_merge)) 17070b57cec5SDimitry Andric CmdArgs.push_back("-arm-global-merge=false"); 17080b57cec5SDimitry Andric else 17090b57cec5SDimitry Andric CmdArgs.push_back("-arm-global-merge=true"); 17100b57cec5SDimitry Andric } 17110b57cec5SDimitry Andric 17120b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 17130b57cec5SDimitry Andric options::OPT_mno_implicit_float, true)) 17140b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 17150b57cec5SDimitry Andric 17160b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_mcmse)) 17170b57cec5SDimitry Andric CmdArgs.push_back("-mcmse"); 1718fe6060f1SDimitry Andric 1719fe6060f1SDimitry Andric AddAAPCSVolatileBitfieldArgs(Args, CmdArgs); 17204824e7fdSDimitry Andric 17214824e7fdSDimitry Andric // Enable/disable return address signing and indirect branch targets. 17221fd87a68SDimitry Andric CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, false /*isAArch64*/); 17230b57cec5SDimitry Andric } 17240b57cec5SDimitry Andric 17250b57cec5SDimitry Andric void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple, 17260b57cec5SDimitry Andric const ArgList &Args, bool KernelOrKext, 17270b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 17280b57cec5SDimitry Andric const ToolChain &TC = getToolChain(); 17290b57cec5SDimitry Andric 17300b57cec5SDimitry Andric // Add the target features 17315ffd83dbSDimitry Andric getTargetFeatures(TC.getDriver(), EffectiveTriple, Args, CmdArgs, false); 17320b57cec5SDimitry Andric 17330b57cec5SDimitry Andric // Add target specific flags. 17340b57cec5SDimitry Andric switch (TC.getArch()) { 17350b57cec5SDimitry Andric default: 17360b57cec5SDimitry Andric break; 17370b57cec5SDimitry Andric 17380b57cec5SDimitry Andric case llvm::Triple::arm: 17390b57cec5SDimitry Andric case llvm::Triple::armeb: 17400b57cec5SDimitry Andric case llvm::Triple::thumb: 17410b57cec5SDimitry Andric case llvm::Triple::thumbeb: 17420b57cec5SDimitry Andric // Use the effective triple, which takes into account the deployment target. 17430b57cec5SDimitry Andric AddARMTargetArgs(EffectiveTriple, Args, CmdArgs, KernelOrKext); 17440b57cec5SDimitry Andric CmdArgs.push_back("-fallow-half-arguments-and-returns"); 17450b57cec5SDimitry Andric break; 17460b57cec5SDimitry Andric 17470b57cec5SDimitry Andric case llvm::Triple::aarch64: 1748480093f4SDimitry Andric case llvm::Triple::aarch64_32: 17490b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 17500b57cec5SDimitry Andric AddAArch64TargetArgs(Args, CmdArgs); 17510b57cec5SDimitry Andric CmdArgs.push_back("-fallow-half-arguments-and-returns"); 17520b57cec5SDimitry Andric break; 17530b57cec5SDimitry Andric 17540b57cec5SDimitry Andric case llvm::Triple::mips: 17550b57cec5SDimitry Andric case llvm::Triple::mipsel: 17560b57cec5SDimitry Andric case llvm::Triple::mips64: 17570b57cec5SDimitry Andric case llvm::Triple::mips64el: 17580b57cec5SDimitry Andric AddMIPSTargetArgs(Args, CmdArgs); 17590b57cec5SDimitry Andric break; 17600b57cec5SDimitry Andric 17610b57cec5SDimitry Andric case llvm::Triple::ppc: 1762e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 17630b57cec5SDimitry Andric case llvm::Triple::ppc64: 17640b57cec5SDimitry Andric case llvm::Triple::ppc64le: 17650b57cec5SDimitry Andric AddPPCTargetArgs(Args, CmdArgs); 17660b57cec5SDimitry Andric break; 17670b57cec5SDimitry Andric 17680b57cec5SDimitry Andric case llvm::Triple::riscv32: 17690b57cec5SDimitry Andric case llvm::Triple::riscv64: 17700b57cec5SDimitry Andric AddRISCVTargetArgs(Args, CmdArgs); 17710b57cec5SDimitry Andric break; 17720b57cec5SDimitry Andric 17730b57cec5SDimitry Andric case llvm::Triple::sparc: 17740b57cec5SDimitry Andric case llvm::Triple::sparcel: 17750b57cec5SDimitry Andric case llvm::Triple::sparcv9: 17760b57cec5SDimitry Andric AddSparcTargetArgs(Args, CmdArgs); 17770b57cec5SDimitry Andric break; 17780b57cec5SDimitry Andric 17790b57cec5SDimitry Andric case llvm::Triple::systemz: 17800b57cec5SDimitry Andric AddSystemZTargetArgs(Args, CmdArgs); 17810b57cec5SDimitry Andric break; 17820b57cec5SDimitry Andric 17830b57cec5SDimitry Andric case llvm::Triple::x86: 17840b57cec5SDimitry Andric case llvm::Triple::x86_64: 17850b57cec5SDimitry Andric AddX86TargetArgs(Args, CmdArgs); 17860b57cec5SDimitry Andric break; 17870b57cec5SDimitry Andric 17880b57cec5SDimitry Andric case llvm::Triple::lanai: 17890b57cec5SDimitry Andric AddLanaiTargetArgs(Args, CmdArgs); 17900b57cec5SDimitry Andric break; 17910b57cec5SDimitry Andric 17920b57cec5SDimitry Andric case llvm::Triple::hexagon: 17930b57cec5SDimitry Andric AddHexagonTargetArgs(Args, CmdArgs); 17940b57cec5SDimitry Andric break; 17950b57cec5SDimitry Andric 17960b57cec5SDimitry Andric case llvm::Triple::wasm32: 17970b57cec5SDimitry Andric case llvm::Triple::wasm64: 17980b57cec5SDimitry Andric AddWebAssemblyTargetArgs(Args, CmdArgs); 17990b57cec5SDimitry Andric break; 18005ffd83dbSDimitry Andric 18015ffd83dbSDimitry Andric case llvm::Triple::ve: 18025ffd83dbSDimitry Andric AddVETargetArgs(Args, CmdArgs); 18035ffd83dbSDimitry Andric break; 18040b57cec5SDimitry Andric } 18050b57cec5SDimitry Andric } 18060b57cec5SDimitry Andric 18070b57cec5SDimitry Andric namespace { 18080b57cec5SDimitry Andric void RenderAArch64ABI(const llvm::Triple &Triple, const ArgList &Args, 18090b57cec5SDimitry Andric ArgStringList &CmdArgs) { 18100b57cec5SDimitry Andric const char *ABIName = nullptr; 18110b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) 18120b57cec5SDimitry Andric ABIName = A->getValue(); 18130b57cec5SDimitry Andric else if (Triple.isOSDarwin()) 18140b57cec5SDimitry Andric ABIName = "darwinpcs"; 18150b57cec5SDimitry Andric else 18160b57cec5SDimitry Andric ABIName = "aapcs"; 18170b57cec5SDimitry Andric 18180b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 18190b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 18200b57cec5SDimitry Andric } 18210b57cec5SDimitry Andric } 18220b57cec5SDimitry Andric 18230b57cec5SDimitry Andric void Clang::AddAArch64TargetArgs(const ArgList &Args, 18240b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 18250b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 18260b57cec5SDimitry Andric 18270b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) || 18280b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel) || 18290b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)) 18300b57cec5SDimitry Andric CmdArgs.push_back("-disable-red-zone"); 18310b57cec5SDimitry Andric 18320b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 18330b57cec5SDimitry Andric options::OPT_mno_implicit_float, true)) 18340b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 18350b57cec5SDimitry Andric 18360b57cec5SDimitry Andric RenderAArch64ABI(Triple, Args, CmdArgs); 18370b57cec5SDimitry Andric 18380b57cec5SDimitry Andric // Forward the -mglobal-merge option for explicit control over the pass. 18390b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, 18400b57cec5SDimitry Andric options::OPT_mno_global_merge)) { 18410b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18420b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_global_merge)) 18430b57cec5SDimitry Andric CmdArgs.push_back("-aarch64-enable-global-merge=false"); 18440b57cec5SDimitry Andric else 18450b57cec5SDimitry Andric CmdArgs.push_back("-aarch64-enable-global-merge=true"); 18460b57cec5SDimitry Andric } 18470b57cec5SDimitry Andric 18480b57cec5SDimitry Andric // Enable/disable return address signing and indirect branch targets. 18491fd87a68SDimitry Andric CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, true /*isAArch64*/); 1850e8d8bef9SDimitry Andric 1851e8d8bef9SDimitry Andric // Handle -msve_vector_bits=<bits> 1852e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) { 1853e8d8bef9SDimitry Andric StringRef Val = A->getValue(); 1854e8d8bef9SDimitry Andric const Driver &D = getToolChain().getDriver(); 1855e8d8bef9SDimitry Andric if (Val.equals("128") || Val.equals("256") || Val.equals("512") || 1856349cc55cSDimitry Andric Val.equals("1024") || Val.equals("2048") || Val.equals("128+") || 1857349cc55cSDimitry Andric Val.equals("256+") || Val.equals("512+") || Val.equals("1024+") || 1858349cc55cSDimitry Andric Val.equals("2048+")) { 1859349cc55cSDimitry Andric unsigned Bits = 0; 1860349cc55cSDimitry Andric if (Val.endswith("+")) 1861349cc55cSDimitry Andric Val = Val.substr(0, Val.size() - 1); 1862349cc55cSDimitry Andric else { 1863349cc55cSDimitry Andric bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid; 1864349cc55cSDimitry Andric assert(!Invalid && "Failed to parse value"); 1865e8d8bef9SDimitry Andric CmdArgs.push_back( 1866349cc55cSDimitry Andric Args.MakeArgString("-mvscale-max=" + llvm::Twine(Bits / 128))); 1867349cc55cSDimitry Andric } 1868349cc55cSDimitry Andric 1869349cc55cSDimitry Andric bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid; 1870349cc55cSDimitry Andric assert(!Invalid && "Failed to parse value"); 1871349cc55cSDimitry Andric CmdArgs.push_back( 1872349cc55cSDimitry Andric Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128))); 1873e8d8bef9SDimitry Andric // Silently drop requests for vector-length agnostic code as it's implied. 1874349cc55cSDimitry Andric } else if (!Val.equals("scalable")) 1875e8d8bef9SDimitry Andric // Handle the unsupported values passed to msve-vector-bits. 1876e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1877e8d8bef9SDimitry Andric << A->getOption().getName() << Val; 1878e8d8bef9SDimitry Andric } 1879fe6060f1SDimitry Andric 1880fe6060f1SDimitry Andric AddAAPCSVolatileBitfieldArgs(Args, CmdArgs); 1881349cc55cSDimitry Andric 1882349cc55cSDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 1883349cc55cSDimitry Andric StringRef Name = A->getValue(); 1884349cc55cSDimitry Andric 1885349cc55cSDimitry Andric std::string TuneCPU; 1886349cc55cSDimitry Andric if (Name == "native") 1887349cc55cSDimitry Andric TuneCPU = std::string(llvm::sys::getHostCPUName()); 1888349cc55cSDimitry Andric else 1889349cc55cSDimitry Andric TuneCPU = std::string(Name); 1890349cc55cSDimitry Andric 1891349cc55cSDimitry Andric if (!TuneCPU.empty()) { 1892349cc55cSDimitry Andric CmdArgs.push_back("-tune-cpu"); 1893349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 1894349cc55cSDimitry Andric } 1895349cc55cSDimitry Andric } 18960b57cec5SDimitry Andric } 18970b57cec5SDimitry Andric 18980b57cec5SDimitry Andric void Clang::AddMIPSTargetArgs(const ArgList &Args, 18990b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 19000b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 19010b57cec5SDimitry Andric StringRef CPUName; 19020b57cec5SDimitry Andric StringRef ABIName; 19030b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 19040b57cec5SDimitry Andric mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 19050b57cec5SDimitry Andric 19060b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 19070b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 19080b57cec5SDimitry Andric 19090b57cec5SDimitry Andric mips::FloatABI ABI = mips::getMipsFloatABI(D, Args, Triple); 19100b57cec5SDimitry Andric if (ABI == mips::FloatABI::Soft) { 19110b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 19120b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 19130b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 19140b57cec5SDimitry Andric CmdArgs.push_back("soft"); 19150b57cec5SDimitry Andric } else { 19160b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 19170b57cec5SDimitry Andric assert(ABI == mips::FloatABI::Hard && "Invalid float abi!"); 19180b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 19190b57cec5SDimitry Andric CmdArgs.push_back("hard"); 19200b57cec5SDimitry Andric } 19210b57cec5SDimitry Andric 19220b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1, 19230b57cec5SDimitry Andric options::OPT_mno_ldc1_sdc1)) { 19240b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) { 19250b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19260b57cec5SDimitry Andric CmdArgs.push_back("-mno-ldc1-sdc1"); 19270b57cec5SDimitry Andric } 19280b57cec5SDimitry Andric } 19290b57cec5SDimitry Andric 19300b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division, 19310b57cec5SDimitry Andric options::OPT_mno_check_zero_division)) { 19320b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_check_zero_division)) { 19330b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19340b57cec5SDimitry Andric CmdArgs.push_back("-mno-check-zero-division"); 19350b57cec5SDimitry Andric } 19360b57cec5SDimitry Andric } 19370b57cec5SDimitry Andric 193804eeddc0SDimitry Andric if (Args.getLastArg(options::OPT_mfix4300)) { 193904eeddc0SDimitry Andric CmdArgs.push_back("-mllvm"); 194004eeddc0SDimitry Andric CmdArgs.push_back("-mfix4300"); 194104eeddc0SDimitry Andric } 194204eeddc0SDimitry Andric 19430b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_G)) { 19440b57cec5SDimitry Andric StringRef v = A->getValue(); 19450b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19460b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v)); 19470b57cec5SDimitry Andric A->claim(); 19480b57cec5SDimitry Andric } 19490b57cec5SDimitry Andric 19500b57cec5SDimitry Andric Arg *GPOpt = Args.getLastArg(options::OPT_mgpopt, options::OPT_mno_gpopt); 19510b57cec5SDimitry Andric Arg *ABICalls = 19520b57cec5SDimitry Andric Args.getLastArg(options::OPT_mabicalls, options::OPT_mno_abicalls); 19530b57cec5SDimitry Andric 19540b57cec5SDimitry Andric // -mabicalls is the default for many MIPS environments, even with -fno-pic. 19550b57cec5SDimitry Andric // -mgpopt is the default for static, -fno-pic environments but these two 19560b57cec5SDimitry Andric // options conflict. We want to be certain that -mno-abicalls -mgpopt is 19570b57cec5SDimitry Andric // the only case where -mllvm -mgpopt is passed. 19580b57cec5SDimitry Andric // NOTE: We need a warning here or in the backend to warn when -mgpopt is 19590b57cec5SDimitry Andric // passed explicitly when compiling something with -mabicalls 19600b57cec5SDimitry Andric // (implictly) in affect. Currently the warning is in the backend. 19610b57cec5SDimitry Andric // 19620b57cec5SDimitry Andric // When the ABI in use is N64, we also need to determine the PIC mode that 19630b57cec5SDimitry Andric // is in use, as -fno-pic for N64 implies -mno-abicalls. 19640b57cec5SDimitry Andric bool NoABICalls = 19650b57cec5SDimitry Andric ABICalls && ABICalls->getOption().matches(options::OPT_mno_abicalls); 19660b57cec5SDimitry Andric 19670b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 19680b57cec5SDimitry Andric unsigned PICLevel; 19690b57cec5SDimitry Andric bool IsPIE; 19700b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 19710b57cec5SDimitry Andric ParsePICArgs(getToolChain(), Args); 19720b57cec5SDimitry Andric 19730b57cec5SDimitry Andric NoABICalls = NoABICalls || 19740b57cec5SDimitry Andric (RelocationModel == llvm::Reloc::Static && ABIName == "n64"); 19750b57cec5SDimitry Andric 19760b57cec5SDimitry Andric bool WantGPOpt = GPOpt && GPOpt->getOption().matches(options::OPT_mgpopt); 19770b57cec5SDimitry Andric // We quietly ignore -mno-gpopt as the backend defaults to -mno-gpopt. 19780b57cec5SDimitry Andric if (NoABICalls && (!GPOpt || WantGPOpt)) { 19790b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19800b57cec5SDimitry Andric CmdArgs.push_back("-mgpopt"); 19810b57cec5SDimitry Andric 19820b57cec5SDimitry Andric Arg *LocalSData = Args.getLastArg(options::OPT_mlocal_sdata, 19830b57cec5SDimitry Andric options::OPT_mno_local_sdata); 19840b57cec5SDimitry Andric Arg *ExternSData = Args.getLastArg(options::OPT_mextern_sdata, 19850b57cec5SDimitry Andric options::OPT_mno_extern_sdata); 19860b57cec5SDimitry Andric Arg *EmbeddedData = Args.getLastArg(options::OPT_membedded_data, 19870b57cec5SDimitry Andric options::OPT_mno_embedded_data); 19880b57cec5SDimitry Andric if (LocalSData) { 19890b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19900b57cec5SDimitry Andric if (LocalSData->getOption().matches(options::OPT_mlocal_sdata)) { 19910b57cec5SDimitry Andric CmdArgs.push_back("-mlocal-sdata=1"); 19920b57cec5SDimitry Andric } else { 19930b57cec5SDimitry Andric CmdArgs.push_back("-mlocal-sdata=0"); 19940b57cec5SDimitry Andric } 19950b57cec5SDimitry Andric LocalSData->claim(); 19960b57cec5SDimitry Andric } 19970b57cec5SDimitry Andric 19980b57cec5SDimitry Andric if (ExternSData) { 19990b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 20000b57cec5SDimitry Andric if (ExternSData->getOption().matches(options::OPT_mextern_sdata)) { 20010b57cec5SDimitry Andric CmdArgs.push_back("-mextern-sdata=1"); 20020b57cec5SDimitry Andric } else { 20030b57cec5SDimitry Andric CmdArgs.push_back("-mextern-sdata=0"); 20040b57cec5SDimitry Andric } 20050b57cec5SDimitry Andric ExternSData->claim(); 20060b57cec5SDimitry Andric } 20070b57cec5SDimitry Andric 20080b57cec5SDimitry Andric if (EmbeddedData) { 20090b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 20100b57cec5SDimitry Andric if (EmbeddedData->getOption().matches(options::OPT_membedded_data)) { 20110b57cec5SDimitry Andric CmdArgs.push_back("-membedded-data=1"); 20120b57cec5SDimitry Andric } else { 20130b57cec5SDimitry Andric CmdArgs.push_back("-membedded-data=0"); 20140b57cec5SDimitry Andric } 20150b57cec5SDimitry Andric EmbeddedData->claim(); 20160b57cec5SDimitry Andric } 20170b57cec5SDimitry Andric 20180b57cec5SDimitry Andric } else if ((!ABICalls || (!NoABICalls && ABICalls)) && WantGPOpt) 20190b57cec5SDimitry Andric D.Diag(diag::warn_drv_unsupported_gpopt) << (ABICalls ? 0 : 1); 20200b57cec5SDimitry Andric 20210b57cec5SDimitry Andric if (GPOpt) 20220b57cec5SDimitry Andric GPOpt->claim(); 20230b57cec5SDimitry Andric 20240b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcompact_branches_EQ)) { 20250b57cec5SDimitry Andric StringRef Val = StringRef(A->getValue()); 20260b57cec5SDimitry Andric if (mips::hasCompactBranches(CPUName)) { 20270b57cec5SDimitry Andric if (Val == "never" || Val == "always" || Val == "optimal") { 20280b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 20290b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mips-compact-branches=" + Val)); 20300b57cec5SDimitry Andric } else 20310b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 20320b57cec5SDimitry Andric << A->getOption().getName() << Val; 20330b57cec5SDimitry Andric } else 20340b57cec5SDimitry Andric D.Diag(diag::warn_target_unsupported_compact_branches) << CPUName; 20350b57cec5SDimitry Andric } 20360b57cec5SDimitry Andric 20370b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrelax_pic_calls, 20380b57cec5SDimitry Andric options::OPT_mno_relax_pic_calls)) { 20390b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_relax_pic_calls)) { 20400b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 20410b57cec5SDimitry Andric CmdArgs.push_back("-mips-jalr-reloc=0"); 20420b57cec5SDimitry Andric } 20430b57cec5SDimitry Andric } 20440b57cec5SDimitry Andric } 20450b57cec5SDimitry Andric 20460b57cec5SDimitry Andric void Clang::AddPPCTargetArgs(const ArgList &Args, 20470b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 20480b57cec5SDimitry Andric // Select the ABI to use. 20490b57cec5SDimitry Andric const char *ABIName = nullptr; 2050480093f4SDimitry Andric const llvm::Triple &T = getToolChain().getTriple(); 2051480093f4SDimitry Andric if (T.isOSBinFormatELF()) { 20520b57cec5SDimitry Andric switch (getToolChain().getArch()) { 20530b57cec5SDimitry Andric case llvm::Triple::ppc64: { 205475b4d546SDimitry Andric if ((T.isOSFreeBSD() && T.getOSMajorVersion() >= 13) || 205575b4d546SDimitry Andric T.isOSOpenBSD() || T.isMusl()) 2056480093f4SDimitry Andric ABIName = "elfv2"; 2057480093f4SDimitry Andric else 20580b57cec5SDimitry Andric ABIName = "elfv1"; 20590b57cec5SDimitry Andric break; 20600b57cec5SDimitry Andric } 20610b57cec5SDimitry Andric case llvm::Triple::ppc64le: 20620b57cec5SDimitry Andric ABIName = "elfv2"; 20630b57cec5SDimitry Andric break; 20640b57cec5SDimitry Andric default: 20650b57cec5SDimitry Andric break; 20660b57cec5SDimitry Andric } 2067480093f4SDimitry Andric } 20680b57cec5SDimitry Andric 206904eeddc0SDimitry Andric bool IEEELongDouble = getToolChain().defaultToIEEELongDouble(); 20700b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mabi_EQ)) { 20710b57cec5SDimitry Andric StringRef V = A->getValue(); 20720b57cec5SDimitry Andric if (V == "ieeelongdouble") 20730b57cec5SDimitry Andric IEEELongDouble = true; 20740b57cec5SDimitry Andric else if (V == "ibmlongdouble") 20750b57cec5SDimitry Andric IEEELongDouble = false; 20760b57cec5SDimitry Andric else if (V != "altivec") 20770b57cec5SDimitry Andric // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore 20780b57cec5SDimitry Andric // the option if given as we don't have backend support for any targets 20790b57cec5SDimitry Andric // that don't use the altivec abi. 20800b57cec5SDimitry Andric ABIName = A->getValue(); 20810b57cec5SDimitry Andric } 20820b57cec5SDimitry Andric if (IEEELongDouble) 20830b57cec5SDimitry Andric CmdArgs.push_back("-mabi=ieeelongdouble"); 20840b57cec5SDimitry Andric 20850b57cec5SDimitry Andric ppc::FloatABI FloatABI = 20860b57cec5SDimitry Andric ppc::getPPCFloatABI(getToolChain().getDriver(), Args); 20870b57cec5SDimitry Andric 20880b57cec5SDimitry Andric if (FloatABI == ppc::FloatABI::Soft) { 20890b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 20900b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 20910b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 20920b57cec5SDimitry Andric CmdArgs.push_back("soft"); 20930b57cec5SDimitry Andric } else { 20940b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 20950b57cec5SDimitry Andric assert(FloatABI == ppc::FloatABI::Hard && "Invalid float abi!"); 20960b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 20970b57cec5SDimitry Andric CmdArgs.push_back("hard"); 20980b57cec5SDimitry Andric } 20990b57cec5SDimitry Andric 21000b57cec5SDimitry Andric if (ABIName) { 21010b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 21020b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 21030b57cec5SDimitry Andric } 21040b57cec5SDimitry Andric } 21050b57cec5SDimitry Andric 21065ffd83dbSDimitry Andric static void SetRISCVSmallDataLimit(const ToolChain &TC, const ArgList &Args, 21075ffd83dbSDimitry Andric ArgStringList &CmdArgs) { 21085ffd83dbSDimitry Andric const Driver &D = TC.getDriver(); 21095ffd83dbSDimitry Andric const llvm::Triple &Triple = TC.getTriple(); 21105ffd83dbSDimitry Andric // Default small data limitation is eight. 21115ffd83dbSDimitry Andric const char *SmallDataLimit = "8"; 21125ffd83dbSDimitry Andric // Get small data limitation. 21135ffd83dbSDimitry Andric if (Args.getLastArg(options::OPT_shared, options::OPT_fpic, 21145ffd83dbSDimitry Andric options::OPT_fPIC)) { 21155ffd83dbSDimitry Andric // Not support linker relaxation for PIC. 21165ffd83dbSDimitry Andric SmallDataLimit = "0"; 21175ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_G)) { 21185ffd83dbSDimitry Andric D.Diag(diag::warn_drv_unsupported_sdata); 21195ffd83dbSDimitry Andric } 21205ffd83dbSDimitry Andric } else if (Args.getLastArgValue(options::OPT_mcmodel_EQ) 2121fe6060f1SDimitry Andric .equals_insensitive("large") && 21225ffd83dbSDimitry Andric (Triple.getArch() == llvm::Triple::riscv64)) { 21235ffd83dbSDimitry Andric // Not support linker relaxation for RV64 with large code model. 21245ffd83dbSDimitry Andric SmallDataLimit = "0"; 21255ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_G)) { 21265ffd83dbSDimitry Andric D.Diag(diag::warn_drv_unsupported_sdata); 21275ffd83dbSDimitry Andric } 21285ffd83dbSDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_G)) { 21295ffd83dbSDimitry Andric SmallDataLimit = A->getValue(); 21305ffd83dbSDimitry Andric } 21315ffd83dbSDimitry Andric // Forward the -msmall-data-limit= option. 21325ffd83dbSDimitry Andric CmdArgs.push_back("-msmall-data-limit"); 21335ffd83dbSDimitry Andric CmdArgs.push_back(SmallDataLimit); 21345ffd83dbSDimitry Andric } 21355ffd83dbSDimitry Andric 21360b57cec5SDimitry Andric void Clang::AddRISCVTargetArgs(const ArgList &Args, 21370b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 21380b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 2139a7dea167SDimitry Andric StringRef ABIName = riscv::getRISCVABI(Args, Triple); 21400b57cec5SDimitry Andric 21410b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 2142a7dea167SDimitry Andric CmdArgs.push_back(ABIName.data()); 21435ffd83dbSDimitry Andric 21445ffd83dbSDimitry Andric SetRISCVSmallDataLimit(getToolChain(), Args, CmdArgs); 2145e8d8bef9SDimitry Andric 2146e8d8bef9SDimitry Andric std::string TuneCPU; 2147e8d8bef9SDimitry Andric 2148e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 2149e8d8bef9SDimitry Andric StringRef Name = A->getValue(); 2150e8d8bef9SDimitry Andric 2151e8d8bef9SDimitry Andric Name = llvm::RISCV::resolveTuneCPUAlias(Name, Triple.isArch64Bit()); 2152e8d8bef9SDimitry Andric TuneCPU = std::string(Name); 2153e8d8bef9SDimitry Andric } 2154e8d8bef9SDimitry Andric 2155e8d8bef9SDimitry Andric if (!TuneCPU.empty()) { 2156e8d8bef9SDimitry Andric CmdArgs.push_back("-tune-cpu"); 2157e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 2158e8d8bef9SDimitry Andric } 21590b57cec5SDimitry Andric } 21600b57cec5SDimitry Andric 21610b57cec5SDimitry Andric void Clang::AddSparcTargetArgs(const ArgList &Args, 21620b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 21630b57cec5SDimitry Andric sparc::FloatABI FloatABI = 21640b57cec5SDimitry Andric sparc::getSparcFloatABI(getToolChain().getDriver(), Args); 21650b57cec5SDimitry Andric 21660b57cec5SDimitry Andric if (FloatABI == sparc::FloatABI::Soft) { 21670b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 21680b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 21690b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 21700b57cec5SDimitry Andric CmdArgs.push_back("soft"); 21710b57cec5SDimitry Andric } else { 21720b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 21730b57cec5SDimitry Andric assert(FloatABI == sparc::FloatABI::Hard && "Invalid float abi!"); 21740b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 21750b57cec5SDimitry Andric CmdArgs.push_back("hard"); 21760b57cec5SDimitry Andric } 21770b57cec5SDimitry Andric } 21780b57cec5SDimitry Andric 21790b57cec5SDimitry Andric void Clang::AddSystemZTargetArgs(const ArgList &Args, 21800b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 2181480093f4SDimitry Andric bool HasBackchain = Args.hasFlag(options::OPT_mbackchain, 2182480093f4SDimitry Andric options::OPT_mno_backchain, false); 2183480093f4SDimitry Andric bool HasPackedStack = Args.hasFlag(options::OPT_mpacked_stack, 2184480093f4SDimitry Andric options::OPT_mno_packed_stack, false); 21855ffd83dbSDimitry Andric systemz::FloatABI FloatABI = 21865ffd83dbSDimitry Andric systemz::getSystemZFloatABI(getToolChain().getDriver(), Args); 21875ffd83dbSDimitry Andric bool HasSoftFloat = (FloatABI == systemz::FloatABI::Soft); 21885ffd83dbSDimitry Andric if (HasBackchain && HasPackedStack && !HasSoftFloat) { 2189480093f4SDimitry Andric const Driver &D = getToolChain().getDriver(); 2190480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt) 21915ffd83dbSDimitry Andric << "-mpacked-stack -mbackchain -mhard-float"; 2192480093f4SDimitry Andric } 2193480093f4SDimitry Andric if (HasBackchain) 21940b57cec5SDimitry Andric CmdArgs.push_back("-mbackchain"); 2195480093f4SDimitry Andric if (HasPackedStack) 2196480093f4SDimitry Andric CmdArgs.push_back("-mpacked-stack"); 21975ffd83dbSDimitry Andric if (HasSoftFloat) { 21985ffd83dbSDimitry Andric // Floating point operations and argument passing are soft. 21995ffd83dbSDimitry Andric CmdArgs.push_back("-msoft-float"); 22005ffd83dbSDimitry Andric CmdArgs.push_back("-mfloat-abi"); 22015ffd83dbSDimitry Andric CmdArgs.push_back("soft"); 2202480093f4SDimitry Andric } 22030b57cec5SDimitry Andric } 22040b57cec5SDimitry Andric 22050b57cec5SDimitry Andric void Clang::AddX86TargetArgs(const ArgList &Args, 22060b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 2207480093f4SDimitry Andric const Driver &D = getToolChain().getDriver(); 22085ffd83dbSDimitry Andric addX86AlignBranchArgs(D, Args, CmdArgs, /*IsLTO=*/false); 2209480093f4SDimitry Andric 22100b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) || 22110b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel) || 22120b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)) 22130b57cec5SDimitry Andric CmdArgs.push_back("-disable-red-zone"); 22140b57cec5SDimitry Andric 22150b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs, 22160b57cec5SDimitry Andric options::OPT_mno_tls_direct_seg_refs, true)) 22170b57cec5SDimitry Andric CmdArgs.push_back("-mno-tls-direct-seg-refs"); 22180b57cec5SDimitry Andric 22190b57cec5SDimitry Andric // Default to avoid implicit floating-point for kernel/kext code, but allow 22200b57cec5SDimitry Andric // that to be overridden with -mno-soft-float. 22210b57cec5SDimitry Andric bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) || 22220b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)); 22230b57cec5SDimitry Andric if (Arg *A = Args.getLastArg( 22240b57cec5SDimitry Andric options::OPT_msoft_float, options::OPT_mno_soft_float, 22250b57cec5SDimitry Andric options::OPT_mimplicit_float, options::OPT_mno_implicit_float)) { 22260b57cec5SDimitry Andric const Option &O = A->getOption(); 22270b57cec5SDimitry Andric NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) || 22280b57cec5SDimitry Andric O.matches(options::OPT_msoft_float)); 22290b57cec5SDimitry Andric } 22300b57cec5SDimitry Andric if (NoImplicitFloat) 22310b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 22320b57cec5SDimitry Andric 22330b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) { 22340b57cec5SDimitry Andric StringRef Value = A->getValue(); 22350b57cec5SDimitry Andric if (Value == "intel" || Value == "att") { 22360b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22370b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value)); 2238349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-inline-asm=" + Value)); 22390b57cec5SDimitry Andric } else { 2240480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 22410b57cec5SDimitry Andric << A->getOption().getName() << Value; 22420b57cec5SDimitry Andric } 2243480093f4SDimitry Andric } else if (D.IsCLMode()) { 22440b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22450b57cec5SDimitry Andric CmdArgs.push_back("-x86-asm-syntax=intel"); 22460b57cec5SDimitry Andric } 22470b57cec5SDimitry Andric 2248349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mskip_rax_setup, 2249349cc55cSDimitry Andric options::OPT_mno_skip_rax_setup)) 2250349cc55cSDimitry Andric if (A->getOption().matches(options::OPT_mskip_rax_setup)) 2251349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mskip-rax-setup")); 2252349cc55cSDimitry Andric 22530b57cec5SDimitry Andric // Set flags to support MCU ABI. 22540b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) { 22550b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 22560b57cec5SDimitry Andric CmdArgs.push_back("soft"); 22570b57cec5SDimitry Andric CmdArgs.push_back("-mstack-alignment=4"); 22580b57cec5SDimitry Andric } 2259e8d8bef9SDimitry Andric 2260e8d8bef9SDimitry Andric // Handle -mtune. 2261e8d8bef9SDimitry Andric 2262e8d8bef9SDimitry Andric // Default to "generic" unless -march is present or targetting the PS4. 2263e8d8bef9SDimitry Andric std::string TuneCPU; 2264e8d8bef9SDimitry Andric if (!Args.hasArg(clang::driver::options::OPT_march_EQ) && 2265e8d8bef9SDimitry Andric !getToolChain().getTriple().isPS4CPU()) 2266e8d8bef9SDimitry Andric TuneCPU = "generic"; 2267e8d8bef9SDimitry Andric 2268e8d8bef9SDimitry Andric // Override based on -mtune. 2269e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 2270e8d8bef9SDimitry Andric StringRef Name = A->getValue(); 2271e8d8bef9SDimitry Andric 2272e8d8bef9SDimitry Andric if (Name == "native") { 2273e8d8bef9SDimitry Andric Name = llvm::sys::getHostCPUName(); 2274e8d8bef9SDimitry Andric if (!Name.empty()) 2275e8d8bef9SDimitry Andric TuneCPU = std::string(Name); 2276e8d8bef9SDimitry Andric } else 2277e8d8bef9SDimitry Andric TuneCPU = std::string(Name); 2278e8d8bef9SDimitry Andric } 2279e8d8bef9SDimitry Andric 2280e8d8bef9SDimitry Andric if (!TuneCPU.empty()) { 2281e8d8bef9SDimitry Andric CmdArgs.push_back("-tune-cpu"); 2282e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 2283e8d8bef9SDimitry Andric } 22840b57cec5SDimitry Andric } 22850b57cec5SDimitry Andric 22860b57cec5SDimitry Andric void Clang::AddHexagonTargetArgs(const ArgList &Args, 22870b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 22880b57cec5SDimitry Andric CmdArgs.push_back("-mqdsp6-compat"); 22890b57cec5SDimitry Andric CmdArgs.push_back("-Wreturn-type"); 22900b57cec5SDimitry Andric 22910b57cec5SDimitry Andric if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) { 22920b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22930b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-hexagon-small-data-threshold=" + 22940b57cec5SDimitry Andric Twine(G.getValue()))); 22950b57cec5SDimitry Andric } 22960b57cec5SDimitry Andric 22970b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fno_short_enums)) 22980b57cec5SDimitry Andric CmdArgs.push_back("-fshort-enums"); 22990b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_mieee_rnd_near)) { 23000b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 23010b57cec5SDimitry Andric CmdArgs.push_back("-enable-hexagon-ieee-rnd-near"); 23020b57cec5SDimitry Andric } 23030b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 23040b57cec5SDimitry Andric CmdArgs.push_back("-machine-sink-split=0"); 23050b57cec5SDimitry Andric } 23060b57cec5SDimitry Andric 23070b57cec5SDimitry Andric void Clang::AddLanaiTargetArgs(const ArgList &Args, 23080b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 23090b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { 23100b57cec5SDimitry Andric StringRef CPUName = A->getValue(); 23110b57cec5SDimitry Andric 23120b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 23130b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPUName)); 23140b57cec5SDimitry Andric } 23150b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { 23160b57cec5SDimitry Andric StringRef Value = A->getValue(); 23170b57cec5SDimitry Andric // Only support mregparm=4 to support old usage. Report error for all other 23180b57cec5SDimitry Andric // cases. 23190b57cec5SDimitry Andric int Mregparm; 23200b57cec5SDimitry Andric if (Value.getAsInteger(10, Mregparm)) { 23210b57cec5SDimitry Andric if (Mregparm != 4) { 23220b57cec5SDimitry Andric getToolChain().getDriver().Diag( 23230b57cec5SDimitry Andric diag::err_drv_unsupported_option_argument) 23240b57cec5SDimitry Andric << A->getOption().getName() << Value; 23250b57cec5SDimitry Andric } 23260b57cec5SDimitry Andric } 23270b57cec5SDimitry Andric } 23280b57cec5SDimitry Andric } 23290b57cec5SDimitry Andric 23300b57cec5SDimitry Andric void Clang::AddWebAssemblyTargetArgs(const ArgList &Args, 23310b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 23320b57cec5SDimitry Andric // Default to "hidden" visibility. 23330b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fvisibility_EQ, 23340b57cec5SDimitry Andric options::OPT_fvisibility_ms_compat)) { 23350b57cec5SDimitry Andric CmdArgs.push_back("-fvisibility"); 23360b57cec5SDimitry Andric CmdArgs.push_back("hidden"); 23370b57cec5SDimitry Andric } 23380b57cec5SDimitry Andric } 23390b57cec5SDimitry Andric 23405ffd83dbSDimitry Andric void Clang::AddVETargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const { 23415ffd83dbSDimitry Andric // Floating point operations and argument passing are hard. 23425ffd83dbSDimitry Andric CmdArgs.push_back("-mfloat-abi"); 23435ffd83dbSDimitry Andric CmdArgs.push_back("hard"); 23445ffd83dbSDimitry Andric } 23455ffd83dbSDimitry Andric 23460b57cec5SDimitry Andric void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename, 23470b57cec5SDimitry Andric StringRef Target, const InputInfo &Output, 23480b57cec5SDimitry Andric const InputInfo &Input, const ArgList &Args) const { 23490b57cec5SDimitry Andric // If this is a dry run, do not create the compilation database file. 23500b57cec5SDimitry Andric if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) 23510b57cec5SDimitry Andric return; 23520b57cec5SDimitry Andric 23530b57cec5SDimitry Andric using llvm::yaml::escape; 23540b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 23550b57cec5SDimitry Andric 23560b57cec5SDimitry Andric if (!CompilationDatabase) { 23570b57cec5SDimitry Andric std::error_code EC; 2358fe6060f1SDimitry Andric auto File = std::make_unique<llvm::raw_fd_ostream>( 2359fe6060f1SDimitry Andric Filename, EC, llvm::sys::fs::OF_TextWithCRLF); 23600b57cec5SDimitry Andric if (EC) { 23610b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_compilationdatabase) << Filename 23620b57cec5SDimitry Andric << EC.message(); 23630b57cec5SDimitry Andric return; 23640b57cec5SDimitry Andric } 23650b57cec5SDimitry Andric CompilationDatabase = std::move(File); 23660b57cec5SDimitry Andric } 23670b57cec5SDimitry Andric auto &CDB = *CompilationDatabase; 2368a7dea167SDimitry Andric auto CWD = D.getVFS().getCurrentWorkingDirectory(); 2369a7dea167SDimitry Andric if (!CWD) 2370a7dea167SDimitry Andric CWD = "."; 2371a7dea167SDimitry Andric CDB << "{ \"directory\": \"" << escape(*CWD) << "\""; 23720b57cec5SDimitry Andric CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\""; 23730b57cec5SDimitry Andric CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\""; 23740b57cec5SDimitry Andric CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\""; 2375a7dea167SDimitry Andric SmallString<128> Buf; 23760b57cec5SDimitry Andric Buf = "-x"; 23770b57cec5SDimitry Andric Buf += types::getTypeName(Input.getType()); 23780b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\""; 23790b57cec5SDimitry Andric if (!D.SysRoot.empty() && !Args.hasArg(options::OPT__sysroot_EQ)) { 23800b57cec5SDimitry Andric Buf = "--sysroot="; 23810b57cec5SDimitry Andric Buf += D.SysRoot; 23820b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\""; 23830b57cec5SDimitry Andric } 23840b57cec5SDimitry Andric CDB << ", \"" << escape(Input.getFilename()) << "\""; 23850b57cec5SDimitry Andric for (auto &A: Args) { 23860b57cec5SDimitry Andric auto &O = A->getOption(); 23870b57cec5SDimitry Andric // Skip language selection, which is positional. 23880b57cec5SDimitry Andric if (O.getID() == options::OPT_x) 23890b57cec5SDimitry Andric continue; 23900b57cec5SDimitry Andric // Skip writing dependency output and the compilation database itself. 23910b57cec5SDimitry Andric if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group) 23920b57cec5SDimitry Andric continue; 2393a7dea167SDimitry Andric if (O.getID() == options::OPT_gen_cdb_fragment_path) 2394a7dea167SDimitry Andric continue; 23950b57cec5SDimitry Andric // Skip inputs. 23960b57cec5SDimitry Andric if (O.getKind() == Option::InputClass) 23970b57cec5SDimitry Andric continue; 23980b57cec5SDimitry Andric // All other arguments are quoted and appended. 23990b57cec5SDimitry Andric ArgStringList ASL; 24000b57cec5SDimitry Andric A->render(Args, ASL); 24010b57cec5SDimitry Andric for (auto &it: ASL) 24020b57cec5SDimitry Andric CDB << ", \"" << escape(it) << "\""; 24030b57cec5SDimitry Andric } 24040b57cec5SDimitry Andric Buf = "--target="; 24050b57cec5SDimitry Andric Buf += Target; 24060b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\"]},\n"; 24070b57cec5SDimitry Andric } 24080b57cec5SDimitry Andric 2409a7dea167SDimitry Andric void Clang::DumpCompilationDatabaseFragmentToDir( 2410a7dea167SDimitry Andric StringRef Dir, Compilation &C, StringRef Target, const InputInfo &Output, 2411a7dea167SDimitry Andric const InputInfo &Input, const llvm::opt::ArgList &Args) const { 2412a7dea167SDimitry Andric // If this is a dry run, do not create the compilation database file. 2413a7dea167SDimitry Andric if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) 2414a7dea167SDimitry Andric return; 2415a7dea167SDimitry Andric 2416a7dea167SDimitry Andric if (CompilationDatabase) 2417a7dea167SDimitry Andric DumpCompilationDatabase(C, "", Target, Output, Input, Args); 2418a7dea167SDimitry Andric 2419a7dea167SDimitry Andric SmallString<256> Path = Dir; 2420a7dea167SDimitry Andric const auto &Driver = C.getDriver(); 2421a7dea167SDimitry Andric Driver.getVFS().makeAbsolute(Path); 2422a7dea167SDimitry Andric auto Err = llvm::sys::fs::create_directory(Path, /*IgnoreExisting=*/true); 2423a7dea167SDimitry Andric if (Err) { 2424a7dea167SDimitry Andric Driver.Diag(diag::err_drv_compilationdatabase) << Dir << Err.message(); 2425a7dea167SDimitry Andric return; 2426a7dea167SDimitry Andric } 2427a7dea167SDimitry Andric 2428a7dea167SDimitry Andric llvm::sys::path::append( 2429a7dea167SDimitry Andric Path, 2430a7dea167SDimitry Andric Twine(llvm::sys::path::filename(Input.getFilename())) + ".%%%%.json"); 2431a7dea167SDimitry Andric int FD; 2432a7dea167SDimitry Andric SmallString<256> TempPath; 2433fe6060f1SDimitry Andric Err = llvm::sys::fs::createUniqueFile(Path, FD, TempPath, 2434fe6060f1SDimitry Andric llvm::sys::fs::OF_Text); 2435a7dea167SDimitry Andric if (Err) { 2436a7dea167SDimitry Andric Driver.Diag(diag::err_drv_compilationdatabase) << Path << Err.message(); 2437a7dea167SDimitry Andric return; 2438a7dea167SDimitry Andric } 2439a7dea167SDimitry Andric CompilationDatabase = 2440a7dea167SDimitry Andric std::make_unique<llvm::raw_fd_ostream>(FD, /*shouldClose=*/true); 2441a7dea167SDimitry Andric DumpCompilationDatabase(C, "", Target, Output, Input, Args); 2442a7dea167SDimitry Andric } 2443a7dea167SDimitry Andric 2444fe6060f1SDimitry Andric static bool CheckARMImplicitITArg(StringRef Value) { 2445fe6060f1SDimitry Andric return Value == "always" || Value == "never" || Value == "arm" || 2446fe6060f1SDimitry Andric Value == "thumb"; 2447fe6060f1SDimitry Andric } 2448fe6060f1SDimitry Andric 2449fe6060f1SDimitry Andric static void AddARMImplicitITArgs(const ArgList &Args, ArgStringList &CmdArgs, 2450fe6060f1SDimitry Andric StringRef Value) { 2451fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 2452fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-arm-implicit-it=" + Value)); 2453fe6060f1SDimitry Andric } 2454fe6060f1SDimitry Andric 24550b57cec5SDimitry Andric static void CollectArgsForIntegratedAssembler(Compilation &C, 24560b57cec5SDimitry Andric const ArgList &Args, 24570b57cec5SDimitry Andric ArgStringList &CmdArgs, 24580b57cec5SDimitry Andric const Driver &D) { 24590b57cec5SDimitry Andric if (UseRelaxAll(C, Args)) 24600b57cec5SDimitry Andric CmdArgs.push_back("-mrelax-all"); 24610b57cec5SDimitry Andric 24620b57cec5SDimitry Andric // Only default to -mincremental-linker-compatible if we think we are 24630b57cec5SDimitry Andric // targeting the MSVC linker. 24640b57cec5SDimitry Andric bool DefaultIncrementalLinkerCompatible = 24650b57cec5SDimitry Andric C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment(); 24660b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mincremental_linker_compatible, 24670b57cec5SDimitry Andric options::OPT_mno_incremental_linker_compatible, 24680b57cec5SDimitry Andric DefaultIncrementalLinkerCompatible)) 24690b57cec5SDimitry Andric CmdArgs.push_back("-mincremental-linker-compatible"); 24700b57cec5SDimitry Andric 2471a7dea167SDimitry Andric // If you add more args here, also add them to the block below that 2472a7dea167SDimitry Andric // starts with "// If CollectArgsForIntegratedAssembler() isn't called below". 2473a7dea167SDimitry Andric 24740b57cec5SDimitry Andric // When passing -I arguments to the assembler we sometimes need to 24750b57cec5SDimitry Andric // unconditionally take the next argument. For example, when parsing 24760b57cec5SDimitry Andric // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the 24770b57cec5SDimitry Andric // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo' 24780b57cec5SDimitry Andric // arg after parsing the '-I' arg. 24790b57cec5SDimitry Andric bool TakeNextArg = false; 24800b57cec5SDimitry Andric 24810b57cec5SDimitry Andric bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations(); 2482349cc55cSDimitry Andric bool UseNoExecStack = false; 24830b57cec5SDimitry Andric const char *MipsTargetFeature = nullptr; 2484fe6060f1SDimitry Andric StringRef ImplicitIt; 24850b57cec5SDimitry Andric for (const Arg *A : 2486fe6060f1SDimitry Andric Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler, 2487fe6060f1SDimitry Andric options::OPT_mimplicit_it_EQ)) { 24880b57cec5SDimitry Andric A->claim(); 24890b57cec5SDimitry Andric 2490fe6060f1SDimitry Andric if (A->getOption().getID() == options::OPT_mimplicit_it_EQ) { 2491fe6060f1SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 2492fe6060f1SDimitry Andric case llvm::Triple::arm: 2493fe6060f1SDimitry Andric case llvm::Triple::armeb: 2494fe6060f1SDimitry Andric case llvm::Triple::thumb: 2495fe6060f1SDimitry Andric case llvm::Triple::thumbeb: 2496fe6060f1SDimitry Andric // Only store the value; the last value set takes effect. 2497fe6060f1SDimitry Andric ImplicitIt = A->getValue(); 2498fe6060f1SDimitry Andric if (!CheckARMImplicitITArg(ImplicitIt)) 2499fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2500fe6060f1SDimitry Andric << A->getOption().getName() << ImplicitIt; 2501fe6060f1SDimitry Andric continue; 2502fe6060f1SDimitry Andric default: 2503fe6060f1SDimitry Andric break; 2504fe6060f1SDimitry Andric } 2505fe6060f1SDimitry Andric } 2506fe6060f1SDimitry Andric 25070b57cec5SDimitry Andric for (StringRef Value : A->getValues()) { 25080b57cec5SDimitry Andric if (TakeNextArg) { 25090b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 25100b57cec5SDimitry Andric TakeNextArg = false; 25110b57cec5SDimitry Andric continue; 25120b57cec5SDimitry Andric } 25130b57cec5SDimitry Andric 25140b57cec5SDimitry Andric if (C.getDefaultToolChain().getTriple().isOSBinFormatCOFF() && 25150b57cec5SDimitry Andric Value == "-mbig-obj") 25160b57cec5SDimitry Andric continue; // LLVM handles bigobj automatically 25170b57cec5SDimitry Andric 25180b57cec5SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 25190b57cec5SDimitry Andric default: 25200b57cec5SDimitry Andric break; 25210b57cec5SDimitry Andric case llvm::Triple::thumb: 25220b57cec5SDimitry Andric case llvm::Triple::thumbeb: 25230b57cec5SDimitry Andric case llvm::Triple::arm: 25240b57cec5SDimitry Andric case llvm::Triple::armeb: 2525fe6060f1SDimitry Andric if (Value.startswith("-mimplicit-it=")) { 2526fe6060f1SDimitry Andric // Only store the value; the last value set takes effect. 2527fe6060f1SDimitry Andric ImplicitIt = Value.split("=").second; 2528fe6060f1SDimitry Andric if (CheckARMImplicitITArg(ImplicitIt)) 2529fe6060f1SDimitry Andric continue; 2530fe6060f1SDimitry Andric } 25310b57cec5SDimitry Andric if (Value == "-mthumb") 25320b57cec5SDimitry Andric // -mthumb has already been processed in ComputeLLVMTriple() 25330b57cec5SDimitry Andric // recognize but skip over here. 25340b57cec5SDimitry Andric continue; 25350b57cec5SDimitry Andric break; 25360b57cec5SDimitry Andric case llvm::Triple::mips: 25370b57cec5SDimitry Andric case llvm::Triple::mipsel: 25380b57cec5SDimitry Andric case llvm::Triple::mips64: 25390b57cec5SDimitry Andric case llvm::Triple::mips64el: 25400b57cec5SDimitry Andric if (Value == "--trap") { 25410b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25420b57cec5SDimitry Andric CmdArgs.push_back("+use-tcc-in-div"); 25430b57cec5SDimitry Andric continue; 25440b57cec5SDimitry Andric } 25450b57cec5SDimitry Andric if (Value == "--break") { 25460b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25470b57cec5SDimitry Andric CmdArgs.push_back("-use-tcc-in-div"); 25480b57cec5SDimitry Andric continue; 25490b57cec5SDimitry Andric } 25500b57cec5SDimitry Andric if (Value.startswith("-msoft-float")) { 25510b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25520b57cec5SDimitry Andric CmdArgs.push_back("+soft-float"); 25530b57cec5SDimitry Andric continue; 25540b57cec5SDimitry Andric } 25550b57cec5SDimitry Andric if (Value.startswith("-mhard-float")) { 25560b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25570b57cec5SDimitry Andric CmdArgs.push_back("-soft-float"); 25580b57cec5SDimitry Andric continue; 25590b57cec5SDimitry Andric } 25600b57cec5SDimitry Andric 25610b57cec5SDimitry Andric MipsTargetFeature = llvm::StringSwitch<const char *>(Value) 25620b57cec5SDimitry Andric .Case("-mips1", "+mips1") 25630b57cec5SDimitry Andric .Case("-mips2", "+mips2") 25640b57cec5SDimitry Andric .Case("-mips3", "+mips3") 25650b57cec5SDimitry Andric .Case("-mips4", "+mips4") 25660b57cec5SDimitry Andric .Case("-mips5", "+mips5") 25670b57cec5SDimitry Andric .Case("-mips32", "+mips32") 25680b57cec5SDimitry Andric .Case("-mips32r2", "+mips32r2") 25690b57cec5SDimitry Andric .Case("-mips32r3", "+mips32r3") 25700b57cec5SDimitry Andric .Case("-mips32r5", "+mips32r5") 25710b57cec5SDimitry Andric .Case("-mips32r6", "+mips32r6") 25720b57cec5SDimitry Andric .Case("-mips64", "+mips64") 25730b57cec5SDimitry Andric .Case("-mips64r2", "+mips64r2") 25740b57cec5SDimitry Andric .Case("-mips64r3", "+mips64r3") 25750b57cec5SDimitry Andric .Case("-mips64r5", "+mips64r5") 25760b57cec5SDimitry Andric .Case("-mips64r6", "+mips64r6") 25770b57cec5SDimitry Andric .Default(nullptr); 25780b57cec5SDimitry Andric if (MipsTargetFeature) 25790b57cec5SDimitry Andric continue; 25800b57cec5SDimitry Andric } 25810b57cec5SDimitry Andric 25820b57cec5SDimitry Andric if (Value == "-force_cpusubtype_ALL") { 25830b57cec5SDimitry Andric // Do nothing, this is the default and we don't support anything else. 25840b57cec5SDimitry Andric } else if (Value == "-L") { 25850b57cec5SDimitry Andric CmdArgs.push_back("-msave-temp-labels"); 25860b57cec5SDimitry Andric } else if (Value == "--fatal-warnings") { 25870b57cec5SDimitry Andric CmdArgs.push_back("-massembler-fatal-warnings"); 2588a7dea167SDimitry Andric } else if (Value == "--no-warn" || Value == "-W") { 2589a7dea167SDimitry Andric CmdArgs.push_back("-massembler-no-warn"); 25900b57cec5SDimitry Andric } else if (Value == "--noexecstack") { 25910b57cec5SDimitry Andric UseNoExecStack = true; 25920b57cec5SDimitry Andric } else if (Value.startswith("-compress-debug-sections") || 25930b57cec5SDimitry Andric Value.startswith("--compress-debug-sections") || 25940b57cec5SDimitry Andric Value == "-nocompress-debug-sections" || 25950b57cec5SDimitry Andric Value == "--nocompress-debug-sections") { 25960b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 25970b57cec5SDimitry Andric } else if (Value == "-mrelax-relocations=yes" || 25980b57cec5SDimitry Andric Value == "--mrelax-relocations=yes") { 25990b57cec5SDimitry Andric UseRelaxRelocations = true; 26000b57cec5SDimitry Andric } else if (Value == "-mrelax-relocations=no" || 26010b57cec5SDimitry Andric Value == "--mrelax-relocations=no") { 26020b57cec5SDimitry Andric UseRelaxRelocations = false; 26030b57cec5SDimitry Andric } else if (Value.startswith("-I")) { 26040b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26050b57cec5SDimitry Andric // We need to consume the next argument if the current arg is a plain 26060b57cec5SDimitry Andric // -I. The next arg will be the include directory. 26070b57cec5SDimitry Andric if (Value == "-I") 26080b57cec5SDimitry Andric TakeNextArg = true; 26090b57cec5SDimitry Andric } else if (Value.startswith("-gdwarf-")) { 26100b57cec5SDimitry Andric // "-gdwarf-N" options are not cc1as options. 26110b57cec5SDimitry Andric unsigned DwarfVersion = DwarfVersionNum(Value); 26120b57cec5SDimitry Andric if (DwarfVersion == 0) { // Send it onward, and let cc1as complain. 26130b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26140b57cec5SDimitry Andric } else { 26150b57cec5SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, 2616fe6060f1SDimitry Andric codegenoptions::DebugInfoConstructor, 26170b57cec5SDimitry Andric DwarfVersion, llvm::DebuggerKind::Default); 26180b57cec5SDimitry Andric } 26190b57cec5SDimitry Andric } else if (Value.startswith("-mcpu") || Value.startswith("-mfpu") || 26200b57cec5SDimitry Andric Value.startswith("-mhwdiv") || Value.startswith("-march")) { 26210b57cec5SDimitry Andric // Do nothing, we'll validate it later. 26220b57cec5SDimitry Andric } else if (Value == "-defsym") { 26230b57cec5SDimitry Andric if (A->getNumValues() != 2) { 26240b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_format) << Value; 26250b57cec5SDimitry Andric break; 26260b57cec5SDimitry Andric } 26270b57cec5SDimitry Andric const char *S = A->getValue(1); 26280b57cec5SDimitry Andric auto Pair = StringRef(S).split('='); 26290b57cec5SDimitry Andric auto Sym = Pair.first; 26300b57cec5SDimitry Andric auto SVal = Pair.second; 26310b57cec5SDimitry Andric 26320b57cec5SDimitry Andric if (Sym.empty() || SVal.empty()) { 26330b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_format) << S; 26340b57cec5SDimitry Andric break; 26350b57cec5SDimitry Andric } 26360b57cec5SDimitry Andric int64_t IVal; 26370b57cec5SDimitry Andric if (SVal.getAsInteger(0, IVal)) { 26380b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_symval) << SVal; 26390b57cec5SDimitry Andric break; 26400b57cec5SDimitry Andric } 26410b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26420b57cec5SDimitry Andric TakeNextArg = true; 26430b57cec5SDimitry Andric } else if (Value == "-fdebug-compilation-dir") { 26440b57cec5SDimitry Andric CmdArgs.push_back("-fdebug-compilation-dir"); 26450b57cec5SDimitry Andric TakeNextArg = true; 2646480093f4SDimitry Andric } else if (Value.consume_front("-fdebug-compilation-dir=")) { 2647480093f4SDimitry Andric // The flag is a -Wa / -Xassembler argument and Options doesn't 2648480093f4SDimitry Andric // parse the argument, so this isn't automatically aliased to 2649480093f4SDimitry Andric // -fdebug-compilation-dir (without '=') here. 2650480093f4SDimitry Andric CmdArgs.push_back("-fdebug-compilation-dir"); 2651480093f4SDimitry Andric CmdArgs.push_back(Value.data()); 2652fe6060f1SDimitry Andric } else if (Value == "--version") { 2653fe6060f1SDimitry Andric D.PrintVersion(C, llvm::outs()); 26540b57cec5SDimitry Andric } else { 26550b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 26560b57cec5SDimitry Andric << A->getOption().getName() << Value; 26570b57cec5SDimitry Andric } 26580b57cec5SDimitry Andric } 26590b57cec5SDimitry Andric } 2660fe6060f1SDimitry Andric if (ImplicitIt.size()) 2661fe6060f1SDimitry Andric AddARMImplicitITArgs(Args, CmdArgs, ImplicitIt); 26620b57cec5SDimitry Andric if (UseRelaxRelocations) 26630b57cec5SDimitry Andric CmdArgs.push_back("--mrelax-relocations"); 26640b57cec5SDimitry Andric if (UseNoExecStack) 26650b57cec5SDimitry Andric CmdArgs.push_back("-mnoexecstack"); 26660b57cec5SDimitry Andric if (MipsTargetFeature != nullptr) { 26670b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 26680b57cec5SDimitry Andric CmdArgs.push_back(MipsTargetFeature); 26690b57cec5SDimitry Andric } 26700b57cec5SDimitry Andric 26710b57cec5SDimitry Andric // forward -fembed-bitcode to assmebler 26720b57cec5SDimitry Andric if (C.getDriver().embedBitcodeEnabled() || 26730b57cec5SDimitry Andric C.getDriver().embedBitcodeMarkerOnly()) 26740b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); 26750b57cec5SDimitry Andric } 26760b57cec5SDimitry Andric 26770b57cec5SDimitry Andric static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, 26780b57cec5SDimitry Andric bool OFastEnabled, const ArgList &Args, 26795ffd83dbSDimitry Andric ArgStringList &CmdArgs, 26805ffd83dbSDimitry Andric const JobAction &JA) { 26810b57cec5SDimitry Andric // Handle various floating point optimization flags, mapping them to the 26820b57cec5SDimitry Andric // appropriate LLVM code generation flags. This is complicated by several 26830b57cec5SDimitry Andric // "umbrella" flags, so we do this by stepping through the flags incrementally 26840b57cec5SDimitry Andric // adjusting what we think is enabled/disabled, then at the end setting the 26850b57cec5SDimitry Andric // LLVM flags based on the final state. 26860b57cec5SDimitry Andric bool HonorINFs = true; 26870b57cec5SDimitry Andric bool HonorNaNs = true; 2688349cc55cSDimitry Andric bool ApproxFunc = false; 26890b57cec5SDimitry Andric // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes. 26900b57cec5SDimitry Andric bool MathErrno = TC.IsMathErrnoDefault(); 26910b57cec5SDimitry Andric bool AssociativeMath = false; 26920b57cec5SDimitry Andric bool ReciprocalMath = false; 26930b57cec5SDimitry Andric bool SignedZeros = true; 2694480093f4SDimitry Andric bool TrappingMath = false; // Implemented via -ffp-exception-behavior 2695480093f4SDimitry Andric bool TrappingMathPresent = false; // Is trapping-math in args, and not 2696480093f4SDimitry Andric // overriden by ffp-exception-behavior? 2697480093f4SDimitry Andric bool RoundingFPMath = false; 2698480093f4SDimitry Andric bool RoundingMathPresent = false; // Is rounding-math in args? 2699480093f4SDimitry Andric // -ffp-model values: strict, fast, precise 2700480093f4SDimitry Andric StringRef FPModel = ""; 2701480093f4SDimitry Andric // -ffp-exception-behavior options: strict, maytrap, ignore 2702480093f4SDimitry Andric StringRef FPExceptionBehavior = ""; 27035ffd83dbSDimitry Andric const llvm::DenormalMode DefaultDenormalFPMath = 27045ffd83dbSDimitry Andric TC.getDefaultDenormalModeForType(Args, JA); 27055ffd83dbSDimitry Andric const llvm::DenormalMode DefaultDenormalFP32Math = 27065ffd83dbSDimitry Andric TC.getDefaultDenormalModeForType(Args, JA, &llvm::APFloat::IEEEsingle()); 27075ffd83dbSDimitry Andric 27085ffd83dbSDimitry Andric llvm::DenormalMode DenormalFPMath = DefaultDenormalFPMath; 27095ffd83dbSDimitry Andric llvm::DenormalMode DenormalFP32Math = DefaultDenormalFP32Math; 2710349cc55cSDimitry Andric // CUDA and HIP don't rely on the frontend to pass an ffp-contract option. 2711349cc55cSDimitry Andric // If one wasn't given by the user, don't pass it here. 2712349cc55cSDimitry Andric StringRef FPContract; 2713349cc55cSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 2714349cc55cSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) 2715349cc55cSDimitry Andric FPContract = "on"; 2716480093f4SDimitry Andric bool StrictFPModel = false; 27170b57cec5SDimitry Andric 27180b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) { 27190b57cec5SDimitry Andric CmdArgs.push_back("-mlimit-float-precision"); 27200b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 27210b57cec5SDimitry Andric } 27220b57cec5SDimitry Andric 27230b57cec5SDimitry Andric for (const Arg *A : Args) { 2724480093f4SDimitry Andric auto optID = A->getOption().getID(); 2725480093f4SDimitry Andric bool PreciseFPModel = false; 2726480093f4SDimitry Andric switch (optID) { 2727480093f4SDimitry Andric default: 2728480093f4SDimitry Andric break; 2729480093f4SDimitry Andric case options::OPT_ffp_model_EQ: { 2730480093f4SDimitry Andric // If -ffp-model= is seen, reset to fno-fast-math 2731480093f4SDimitry Andric HonorINFs = true; 2732480093f4SDimitry Andric HonorNaNs = true; 2733480093f4SDimitry Andric // Turning *off* -ffast-math restores the toolchain default. 2734480093f4SDimitry Andric MathErrno = TC.IsMathErrnoDefault(); 2735480093f4SDimitry Andric AssociativeMath = false; 2736480093f4SDimitry Andric ReciprocalMath = false; 2737480093f4SDimitry Andric SignedZeros = true; 2738480093f4SDimitry Andric // -fno_fast_math restores default denormal and fpcontract handling 2739349cc55cSDimitry Andric FPContract = "on"; 27405ffd83dbSDimitry Andric DenormalFPMath = llvm::DenormalMode::getIEEE(); 27415ffd83dbSDimitry Andric 27425ffd83dbSDimitry Andric // FIXME: The target may have picked a non-IEEE default mode here based on 27435ffd83dbSDimitry Andric // -cl-denorms-are-zero. Should the target consider -fp-model interaction? 27445ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 27455ffd83dbSDimitry Andric 2746480093f4SDimitry Andric StringRef Val = A->getValue(); 2747480093f4SDimitry Andric if (OFastEnabled && !Val.equals("fast")) { 2748480093f4SDimitry Andric // Only -ffp-model=fast is compatible with OFast, ignore. 2749480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2750480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + Val) 2751480093f4SDimitry Andric << "-Ofast"; 2752480093f4SDimitry Andric break; 2753480093f4SDimitry Andric } 2754480093f4SDimitry Andric StrictFPModel = false; 2755480093f4SDimitry Andric PreciseFPModel = true; 2756480093f4SDimitry Andric // ffp-model= is a Driver option, it is entirely rewritten into more 2757480093f4SDimitry Andric // granular options before being passed into cc1. 2758480093f4SDimitry Andric // Use the gcc option in the switch below. 2759349cc55cSDimitry Andric if (!FPModel.empty() && !FPModel.equals(Val)) 2760480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2761480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + FPModel) 2762480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + Val); 2763480093f4SDimitry Andric if (Val.equals("fast")) { 2764480093f4SDimitry Andric optID = options::OPT_ffast_math; 2765480093f4SDimitry Andric FPModel = Val; 27666e75b2fbSDimitry Andric FPContract = "fast"; 2767480093f4SDimitry Andric } else if (Val.equals("precise")) { 2768480093f4SDimitry Andric optID = options::OPT_ffp_contract; 2769480093f4SDimitry Andric FPModel = Val; 2770349cc55cSDimitry Andric FPContract = "on"; 2771480093f4SDimitry Andric PreciseFPModel = true; 2772480093f4SDimitry Andric } else if (Val.equals("strict")) { 2773480093f4SDimitry Andric StrictFPModel = true; 2774480093f4SDimitry Andric optID = options::OPT_frounding_math; 2775480093f4SDimitry Andric FPExceptionBehavior = "strict"; 2776480093f4SDimitry Andric FPModel = Val; 27775ffd83dbSDimitry Andric FPContract = "off"; 2778480093f4SDimitry Andric TrappingMath = true; 2779480093f4SDimitry Andric } else 2780480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2781480093f4SDimitry Andric << A->getOption().getName() << Val; 2782480093f4SDimitry Andric break; 2783480093f4SDimitry Andric } 2784480093f4SDimitry Andric } 2785480093f4SDimitry Andric 2786480093f4SDimitry Andric switch (optID) { 27870b57cec5SDimitry Andric // If this isn't an FP option skip the claim below 27880b57cec5SDimitry Andric default: continue; 27890b57cec5SDimitry Andric 27900b57cec5SDimitry Andric // Options controlling individual features 27910b57cec5SDimitry Andric case options::OPT_fhonor_infinities: HonorINFs = true; break; 27920b57cec5SDimitry Andric case options::OPT_fno_honor_infinities: HonorINFs = false; break; 27930b57cec5SDimitry Andric case options::OPT_fhonor_nans: HonorNaNs = true; break; 27940b57cec5SDimitry Andric case options::OPT_fno_honor_nans: HonorNaNs = false; break; 2795349cc55cSDimitry Andric case options::OPT_fapprox_func: ApproxFunc = true; break; 2796349cc55cSDimitry Andric case options::OPT_fno_approx_func: ApproxFunc = false; break; 27970b57cec5SDimitry Andric case options::OPT_fmath_errno: MathErrno = true; break; 27980b57cec5SDimitry Andric case options::OPT_fno_math_errno: MathErrno = false; break; 27990b57cec5SDimitry Andric case options::OPT_fassociative_math: AssociativeMath = true; break; 28000b57cec5SDimitry Andric case options::OPT_fno_associative_math: AssociativeMath = false; break; 28010b57cec5SDimitry Andric case options::OPT_freciprocal_math: ReciprocalMath = true; break; 28020b57cec5SDimitry Andric case options::OPT_fno_reciprocal_math: ReciprocalMath = false; break; 28030b57cec5SDimitry Andric case options::OPT_fsigned_zeros: SignedZeros = true; break; 28040b57cec5SDimitry Andric case options::OPT_fno_signed_zeros: SignedZeros = false; break; 2805480093f4SDimitry Andric case options::OPT_ftrapping_math: 2806480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2807480093f4SDimitry Andric !FPExceptionBehavior.equals("strict")) 2808480093f4SDimitry Andric // Warn that previous value of option is overridden. 2809480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2810480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) 2811480093f4SDimitry Andric << "-ftrapping-math"; 2812480093f4SDimitry Andric TrappingMath = true; 2813480093f4SDimitry Andric TrappingMathPresent = true; 2814480093f4SDimitry Andric FPExceptionBehavior = "strict"; 2815480093f4SDimitry Andric break; 2816480093f4SDimitry Andric case options::OPT_fno_trapping_math: 2817480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2818480093f4SDimitry Andric !FPExceptionBehavior.equals("ignore")) 2819480093f4SDimitry Andric // Warn that previous value of option is overridden. 2820480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2821480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) 2822480093f4SDimitry Andric << "-fno-trapping-math"; 2823480093f4SDimitry Andric TrappingMath = false; 2824480093f4SDimitry Andric TrappingMathPresent = true; 2825480093f4SDimitry Andric FPExceptionBehavior = "ignore"; 2826480093f4SDimitry Andric break; 2827480093f4SDimitry Andric 2828480093f4SDimitry Andric case options::OPT_frounding_math: 2829480093f4SDimitry Andric RoundingFPMath = true; 2830480093f4SDimitry Andric RoundingMathPresent = true; 2831480093f4SDimitry Andric break; 2832480093f4SDimitry Andric 2833480093f4SDimitry Andric case options::OPT_fno_rounding_math: 2834480093f4SDimitry Andric RoundingFPMath = false; 2835480093f4SDimitry Andric RoundingMathPresent = false; 2836480093f4SDimitry Andric break; 28370b57cec5SDimitry Andric 28380b57cec5SDimitry Andric case options::OPT_fdenormal_fp_math_EQ: 28395ffd83dbSDimitry Andric DenormalFPMath = llvm::parseDenormalFPAttribute(A->getValue()); 28405ffd83dbSDimitry Andric if (!DenormalFPMath.isValid()) { 28415ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 28425ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 28435ffd83dbSDimitry Andric } 28445ffd83dbSDimitry Andric break; 28455ffd83dbSDimitry Andric 28465ffd83dbSDimitry Andric case options::OPT_fdenormal_fp_math_f32_EQ: 28475ffd83dbSDimitry Andric DenormalFP32Math = llvm::parseDenormalFPAttribute(A->getValue()); 28485ffd83dbSDimitry Andric if (!DenormalFP32Math.isValid()) { 28495ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 28505ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 28515ffd83dbSDimitry Andric } 28520b57cec5SDimitry Andric break; 28530b57cec5SDimitry Andric 2854480093f4SDimitry Andric // Validate and pass through -ffp-contract option. 28550b57cec5SDimitry Andric case options::OPT_ffp_contract: { 28560b57cec5SDimitry Andric StringRef Val = A->getValue(); 2857480093f4SDimitry Andric if (PreciseFPModel) { 2858349cc55cSDimitry Andric // -ffp-model=precise enables ffp-contract=on. 2859349cc55cSDimitry Andric // -ffp-model=precise sets PreciseFPModel to on and Val to 2860349cc55cSDimitry Andric // "precise". FPContract is set. 2861480093f4SDimitry Andric ; 2862480093f4SDimitry Andric } else if (Val.equals("fast") || Val.equals("on") || Val.equals("off")) 28630b57cec5SDimitry Andric FPContract = Val; 28640b57cec5SDimitry Andric else 28650b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 28660b57cec5SDimitry Andric << A->getOption().getName() << Val; 28670b57cec5SDimitry Andric break; 28680b57cec5SDimitry Andric } 28690b57cec5SDimitry Andric 2870480093f4SDimitry Andric // Validate and pass through -ffp-model option. 2871480093f4SDimitry Andric case options::OPT_ffp_model_EQ: 2872480093f4SDimitry Andric // This should only occur in the error case 2873480093f4SDimitry Andric // since the optID has been replaced by a more granular 2874480093f4SDimitry Andric // floating point option. 2875480093f4SDimitry Andric break; 2876480093f4SDimitry Andric 2877480093f4SDimitry Andric // Validate and pass through -ffp-exception-behavior option. 2878480093f4SDimitry Andric case options::OPT_ffp_exception_behavior_EQ: { 2879480093f4SDimitry Andric StringRef Val = A->getValue(); 2880480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2881480093f4SDimitry Andric !FPExceptionBehavior.equals(Val)) 2882480093f4SDimitry Andric // Warn that previous value of option is overridden. 2883480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2884480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) 2885480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + Val); 2886480093f4SDimitry Andric TrappingMath = TrappingMathPresent = false; 2887480093f4SDimitry Andric if (Val.equals("ignore") || Val.equals("maytrap")) 2888480093f4SDimitry Andric FPExceptionBehavior = Val; 2889480093f4SDimitry Andric else if (Val.equals("strict")) { 2890480093f4SDimitry Andric FPExceptionBehavior = Val; 2891480093f4SDimitry Andric TrappingMath = TrappingMathPresent = true; 2892480093f4SDimitry Andric } else 2893480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2894480093f4SDimitry Andric << A->getOption().getName() << Val; 2895480093f4SDimitry Andric break; 2896480093f4SDimitry Andric } 2897480093f4SDimitry Andric 28980b57cec5SDimitry Andric case options::OPT_ffinite_math_only: 28990b57cec5SDimitry Andric HonorINFs = false; 29000b57cec5SDimitry Andric HonorNaNs = false; 29010b57cec5SDimitry Andric break; 29020b57cec5SDimitry Andric case options::OPT_fno_finite_math_only: 29030b57cec5SDimitry Andric HonorINFs = true; 29040b57cec5SDimitry Andric HonorNaNs = true; 29050b57cec5SDimitry Andric break; 29060b57cec5SDimitry Andric 29070b57cec5SDimitry Andric case options::OPT_funsafe_math_optimizations: 29080b57cec5SDimitry Andric AssociativeMath = true; 29090b57cec5SDimitry Andric ReciprocalMath = true; 29100b57cec5SDimitry Andric SignedZeros = false; 291104eeddc0SDimitry Andric ApproxFunc = true; 29120b57cec5SDimitry Andric TrappingMath = false; 2913480093f4SDimitry Andric FPExceptionBehavior = ""; 29140b57cec5SDimitry Andric break; 29150b57cec5SDimitry Andric case options::OPT_fno_unsafe_math_optimizations: 29160b57cec5SDimitry Andric AssociativeMath = false; 29170b57cec5SDimitry Andric ReciprocalMath = false; 29180b57cec5SDimitry Andric SignedZeros = true; 291904eeddc0SDimitry Andric ApproxFunc = false; 29200b57cec5SDimitry Andric TrappingMath = true; 2921480093f4SDimitry Andric FPExceptionBehavior = "strict"; 29225ffd83dbSDimitry Andric 29235ffd83dbSDimitry Andric // The target may have opted to flush by default, so force IEEE. 29245ffd83dbSDimitry Andric DenormalFPMath = llvm::DenormalMode::getIEEE(); 29255ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 29260b57cec5SDimitry Andric break; 29270b57cec5SDimitry Andric 29280b57cec5SDimitry Andric case options::OPT_Ofast: 29290b57cec5SDimitry Andric // If -Ofast is the optimization level, then -ffast-math should be enabled 29300b57cec5SDimitry Andric if (!OFastEnabled) 29310b57cec5SDimitry Andric continue; 29320b57cec5SDimitry Andric LLVM_FALLTHROUGH; 29330b57cec5SDimitry Andric case options::OPT_ffast_math: 29340b57cec5SDimitry Andric HonorINFs = false; 29350b57cec5SDimitry Andric HonorNaNs = false; 29360b57cec5SDimitry Andric MathErrno = false; 29370b57cec5SDimitry Andric AssociativeMath = true; 29380b57cec5SDimitry Andric ReciprocalMath = true; 293904eeddc0SDimitry Andric ApproxFunc = true; 29400b57cec5SDimitry Andric SignedZeros = false; 29410b57cec5SDimitry Andric TrappingMath = false; 2942480093f4SDimitry Andric RoundingFPMath = false; 29430b57cec5SDimitry Andric // If fast-math is set then set the fp-contract mode to fast. 29440b57cec5SDimitry Andric FPContract = "fast"; 29450b57cec5SDimitry Andric break; 29460b57cec5SDimitry Andric case options::OPT_fno_fast_math: 29470b57cec5SDimitry Andric HonorINFs = true; 29480b57cec5SDimitry Andric HonorNaNs = true; 29490b57cec5SDimitry Andric // Turning on -ffast-math (with either flag) removes the need for 29500b57cec5SDimitry Andric // MathErrno. However, turning *off* -ffast-math merely restores the 29510b57cec5SDimitry Andric // toolchain default (which may be false). 29520b57cec5SDimitry Andric MathErrno = TC.IsMathErrnoDefault(); 29530b57cec5SDimitry Andric AssociativeMath = false; 29540b57cec5SDimitry Andric ReciprocalMath = false; 295504eeddc0SDimitry Andric ApproxFunc = false; 29560b57cec5SDimitry Andric SignedZeros = true; 29570b57cec5SDimitry Andric // -fno_fast_math restores default denormal and fpcontract handling 29585ffd83dbSDimitry Andric DenormalFPMath = DefaultDenormalFPMath; 29595ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 2960349cc55cSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 2961349cc55cSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) 2962349cc55cSDimitry Andric if (FPContract == "fast") { 2963349cc55cSDimitry Andric FPContract = "on"; 2964349cc55cSDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2965349cc55cSDimitry Andric << "-ffp-contract=fast" 2966349cc55cSDimitry Andric << "-ffp-contract=on"; 2967349cc55cSDimitry Andric } 29680b57cec5SDimitry Andric break; 29690b57cec5SDimitry Andric } 2970480093f4SDimitry Andric if (StrictFPModel) { 2971480093f4SDimitry Andric // If -ffp-model=strict has been specified on command line but 2972480093f4SDimitry Andric // subsequent options conflict then emit warning diagnostic. 2973349cc55cSDimitry Andric if (HonorINFs && HonorNaNs && !AssociativeMath && !ReciprocalMath && 297404eeddc0SDimitry Andric SignedZeros && TrappingMath && RoundingFPMath && !ApproxFunc && 29755ffd83dbSDimitry Andric DenormalFPMath == llvm::DenormalMode::getIEEE() && 2976349cc55cSDimitry Andric DenormalFP32Math == llvm::DenormalMode::getIEEE() && 2977349cc55cSDimitry Andric FPContract.equals("off")) 2978480093f4SDimitry Andric // OK: Current Arg doesn't conflict with -ffp-model=strict 2979480093f4SDimitry Andric ; 2980480093f4SDimitry Andric else { 2981480093f4SDimitry Andric StrictFPModel = false; 2982480093f4SDimitry Andric FPModel = ""; 2983480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2984480093f4SDimitry Andric << "-ffp-model=strict" << 2985480093f4SDimitry Andric ((A->getNumValues() == 0) ? A->getSpelling() 2986480093f4SDimitry Andric : Args.MakeArgString(A->getSpelling() + A->getValue())); 2987480093f4SDimitry Andric } 2988480093f4SDimitry Andric } 29890b57cec5SDimitry Andric 29900b57cec5SDimitry Andric // If we handled this option claim it 29910b57cec5SDimitry Andric A->claim(); 29920b57cec5SDimitry Andric } 29930b57cec5SDimitry Andric 29940b57cec5SDimitry Andric if (!HonorINFs) 29950b57cec5SDimitry Andric CmdArgs.push_back("-menable-no-infs"); 29960b57cec5SDimitry Andric 29970b57cec5SDimitry Andric if (!HonorNaNs) 29980b57cec5SDimitry Andric CmdArgs.push_back("-menable-no-nans"); 29990b57cec5SDimitry Andric 3000349cc55cSDimitry Andric if (ApproxFunc) 3001349cc55cSDimitry Andric CmdArgs.push_back("-fapprox-func"); 3002349cc55cSDimitry Andric 30030b57cec5SDimitry Andric if (MathErrno) 30040b57cec5SDimitry Andric CmdArgs.push_back("-fmath-errno"); 30050b57cec5SDimitry Andric 30060b57cec5SDimitry Andric if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros && 300704eeddc0SDimitry Andric ApproxFunc && !TrappingMath) 30080b57cec5SDimitry Andric CmdArgs.push_back("-menable-unsafe-fp-math"); 30090b57cec5SDimitry Andric 30100b57cec5SDimitry Andric if (!SignedZeros) 30110b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-zeros"); 30120b57cec5SDimitry Andric 30130b57cec5SDimitry Andric if (AssociativeMath && !SignedZeros && !TrappingMath) 30140b57cec5SDimitry Andric CmdArgs.push_back("-mreassociate"); 30150b57cec5SDimitry Andric 30160b57cec5SDimitry Andric if (ReciprocalMath) 30170b57cec5SDimitry Andric CmdArgs.push_back("-freciprocal-math"); 30180b57cec5SDimitry Andric 3019480093f4SDimitry Andric if (TrappingMath) { 3020480093f4SDimitry Andric // FP Exception Behavior is also set to strict 3021480093f4SDimitry Andric assert(FPExceptionBehavior.equals("strict")); 3022e8d8bef9SDimitry Andric } 30230b57cec5SDimitry Andric 30245ffd83dbSDimitry Andric // The default is IEEE. 30255ffd83dbSDimitry Andric if (DenormalFPMath != llvm::DenormalMode::getIEEE()) { 30265ffd83dbSDimitry Andric llvm::SmallString<64> DenormFlag; 30275ffd83dbSDimitry Andric llvm::raw_svector_ostream ArgStr(DenormFlag); 30285ffd83dbSDimitry Andric ArgStr << "-fdenormal-fp-math=" << DenormalFPMath; 30295ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ArgStr.str())); 30305ffd83dbSDimitry Andric } 30315ffd83dbSDimitry Andric 30325ffd83dbSDimitry Andric // Add f32 specific denormal mode flag if it's different. 30335ffd83dbSDimitry Andric if (DenormalFP32Math != DenormalFPMath) { 30345ffd83dbSDimitry Andric llvm::SmallString<64> DenormFlag; 30355ffd83dbSDimitry Andric llvm::raw_svector_ostream ArgStr(DenormFlag); 30365ffd83dbSDimitry Andric ArgStr << "-fdenormal-fp-math-f32=" << DenormalFP32Math; 30375ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ArgStr.str())); 30385ffd83dbSDimitry Andric } 30390b57cec5SDimitry Andric 30400b57cec5SDimitry Andric if (!FPContract.empty()) 30410b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract)); 30420b57cec5SDimitry Andric 3043480093f4SDimitry Andric if (!RoundingFPMath) 3044480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fno-rounding-math")); 3045480093f4SDimitry Andric 3046480093f4SDimitry Andric if (RoundingFPMath && RoundingMathPresent) 3047480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-frounding-math")); 3048480093f4SDimitry Andric 3049480093f4SDimitry Andric if (!FPExceptionBehavior.empty()) 3050480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-exception-behavior=" + 3051480093f4SDimitry Andric FPExceptionBehavior)); 3052480093f4SDimitry Andric 30530b57cec5SDimitry Andric ParseMRecip(D, Args, CmdArgs); 30540b57cec5SDimitry Andric 30550b57cec5SDimitry Andric // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for the 30560b57cec5SDimitry Andric // individual features enabled by -ffast-math instead of the option itself as 30570b57cec5SDimitry Andric // that's consistent with gcc's behaviour. 305804eeddc0SDimitry Andric if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath && ApproxFunc && 3059480093f4SDimitry Andric ReciprocalMath && !SignedZeros && !TrappingMath && !RoundingFPMath) { 30600b57cec5SDimitry Andric CmdArgs.push_back("-ffast-math"); 3061480093f4SDimitry Andric if (FPModel.equals("fast")) { 3062480093f4SDimitry Andric if (FPContract.equals("fast")) 3063480093f4SDimitry Andric // All set, do nothing. 3064480093f4SDimitry Andric ; 3065480093f4SDimitry Andric else if (FPContract.empty()) 3066480093f4SDimitry Andric // Enable -ffp-contract=fast 3067480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast")); 3068480093f4SDimitry Andric else 3069480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 3070480093f4SDimitry Andric << "-ffp-model=fast" 3071480093f4SDimitry Andric << Args.MakeArgString("-ffp-contract=" + FPContract); 3072480093f4SDimitry Andric } 3073480093f4SDimitry Andric } 30740b57cec5SDimitry Andric 30750b57cec5SDimitry Andric // Handle __FINITE_MATH_ONLY__ similarly. 30760b57cec5SDimitry Andric if (!HonorINFs && !HonorNaNs) 30770b57cec5SDimitry Andric CmdArgs.push_back("-ffinite-math-only"); 30780b57cec5SDimitry Andric 30790b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) { 30800b57cec5SDimitry Andric CmdArgs.push_back("-mfpmath"); 30810b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 30820b57cec5SDimitry Andric } 30830b57cec5SDimitry Andric 30840b57cec5SDimitry Andric // Disable a codegen optimization for floating-point casts. 30850b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_strict_float_cast_overflow, 30860b57cec5SDimitry Andric options::OPT_fstrict_float_cast_overflow, false)) 30870b57cec5SDimitry Andric CmdArgs.push_back("-fno-strict-float-cast-overflow"); 30880b57cec5SDimitry Andric } 30890b57cec5SDimitry Andric 30900b57cec5SDimitry Andric static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs, 30910b57cec5SDimitry Andric const llvm::Triple &Triple, 30920b57cec5SDimitry Andric const InputInfo &Input) { 30930b57cec5SDimitry Andric // Enable region store model by default. 30940b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-store=region"); 30950b57cec5SDimitry Andric 30960b57cec5SDimitry Andric // Treat blocks as analysis entry points. 30970b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks"); 30980b57cec5SDimitry Andric 30990b57cec5SDimitry Andric // Add default argument set. 31000b57cec5SDimitry Andric if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) { 31010b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=core"); 31020b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=apiModeling"); 31030b57cec5SDimitry Andric 31040b57cec5SDimitry Andric if (!Triple.isWindowsMSVCEnvironment()) { 31050b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix"); 31060b57cec5SDimitry Andric } else { 31070b57cec5SDimitry Andric // Enable "unix" checkers that also work on Windows. 31080b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.API"); 31090b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.Malloc"); 31100b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.MallocSizeof"); 31110b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.MismatchedDeallocator"); 31120b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.cstring.BadSizeArg"); 31130b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.cstring.NullArg"); 31140b57cec5SDimitry Andric } 31150b57cec5SDimitry Andric 31160b57cec5SDimitry Andric // Disable some unix checkers for PS4. 31170b57cec5SDimitry Andric if (Triple.isPS4CPU()) { 31180b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-disable-checker=unix.API"); 31190b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-disable-checker=unix.Vfork"); 31200b57cec5SDimitry Andric } 31210b57cec5SDimitry Andric 3122480093f4SDimitry Andric if (Triple.isOSDarwin()) { 31230b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=osx"); 3124480093f4SDimitry Andric CmdArgs.push_back( 3125480093f4SDimitry Andric "-analyzer-checker=security.insecureAPI.decodeValueOfObjCType"); 3126480093f4SDimitry Andric } 3127480093f4SDimitry Andric else if (Triple.isOSFuchsia()) 3128480093f4SDimitry Andric CmdArgs.push_back("-analyzer-checker=fuchsia"); 31290b57cec5SDimitry Andric 31300b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=deadcode"); 31310b57cec5SDimitry Andric 31320b57cec5SDimitry Andric if (types::isCXX(Input.getType())) 31330b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=cplusplus"); 31340b57cec5SDimitry Andric 31350b57cec5SDimitry Andric if (!Triple.isPS4CPU()) { 31360b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn"); 31370b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw"); 31380b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets"); 31390b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp"); 31400b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp"); 31410b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork"); 31420b57cec5SDimitry Andric } 31430b57cec5SDimitry Andric 31440b57cec5SDimitry Andric // Default nullability checks. 31450b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=nullability.NullPassedToNonnull"); 31460b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=nullability.NullReturnedFromNonnull"); 31470b57cec5SDimitry Andric } 31480b57cec5SDimitry Andric 31490b57cec5SDimitry Andric // Set the output format. The default is plist, for (lame) historical reasons. 31500b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-output"); 31510b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__analyzer_output)) 31520b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 31530b57cec5SDimitry Andric else 31540b57cec5SDimitry Andric CmdArgs.push_back("plist"); 31550b57cec5SDimitry Andric 31560b57cec5SDimitry Andric // Disable the presentation of standard compiler warnings when using 31570b57cec5SDimitry Andric // --analyze. We only want to show static analyzer diagnostics or frontend 31580b57cec5SDimitry Andric // errors. 31590b57cec5SDimitry Andric CmdArgs.push_back("-w"); 31600b57cec5SDimitry Andric 31610b57cec5SDimitry Andric // Add -Xanalyzer arguments when running as analyzer. 31620b57cec5SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer); 31630b57cec5SDimitry Andric } 31640b57cec5SDimitry Andric 3165e8d8bef9SDimitry Andric static void RenderSSPOptions(const Driver &D, const ToolChain &TC, 3166e8d8bef9SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs, 3167e8d8bef9SDimitry Andric bool KernelOrKext) { 31680b57cec5SDimitry Andric const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple(); 31690b57cec5SDimitry Andric 31700b57cec5SDimitry Andric // NVPTX doesn't support stack protectors; from the compiler's perspective, it 31710b57cec5SDimitry Andric // doesn't even have a stack! 31720b57cec5SDimitry Andric if (EffectiveTriple.isNVPTX()) 31730b57cec5SDimitry Andric return; 31740b57cec5SDimitry Andric 31750b57cec5SDimitry Andric // -stack-protector=0 is default. 3176e8d8bef9SDimitry Andric LangOptions::StackProtectorMode StackProtectorLevel = LangOptions::SSPOff; 3177e8d8bef9SDimitry Andric LangOptions::StackProtectorMode DefaultStackProtectorLevel = 31780b57cec5SDimitry Andric TC.GetDefaultStackProtectorLevel(KernelOrKext); 31790b57cec5SDimitry Andric 31800b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector, 31810b57cec5SDimitry Andric options::OPT_fstack_protector_all, 31820b57cec5SDimitry Andric options::OPT_fstack_protector_strong, 31830b57cec5SDimitry Andric options::OPT_fstack_protector)) { 31840b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fstack_protector)) 31850b57cec5SDimitry Andric StackProtectorLevel = 3186e8d8bef9SDimitry Andric std::max<>(LangOptions::SSPOn, DefaultStackProtectorLevel); 31870b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_fstack_protector_strong)) 31880b57cec5SDimitry Andric StackProtectorLevel = LangOptions::SSPStrong; 31890b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_fstack_protector_all)) 31900b57cec5SDimitry Andric StackProtectorLevel = LangOptions::SSPReq; 31910b57cec5SDimitry Andric } else { 31920b57cec5SDimitry Andric StackProtectorLevel = DefaultStackProtectorLevel; 31930b57cec5SDimitry Andric } 31940b57cec5SDimitry Andric 31950b57cec5SDimitry Andric if (StackProtectorLevel) { 31960b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector"); 31970b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel))); 31980b57cec5SDimitry Andric } 31990b57cec5SDimitry Andric 32000b57cec5SDimitry Andric // --param ssp-buffer-size= 32010b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT__param)) { 32020b57cec5SDimitry Andric StringRef Str(A->getValue()); 32030b57cec5SDimitry Andric if (Str.startswith("ssp-buffer-size=")) { 32040b57cec5SDimitry Andric if (StackProtectorLevel) { 32050b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector-buffer-size"); 32060b57cec5SDimitry Andric // FIXME: Verify the argument is a valid integer. 32070b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16))); 32080b57cec5SDimitry Andric } 32090b57cec5SDimitry Andric A->claim(); 32100b57cec5SDimitry Andric } 32110b57cec5SDimitry Andric } 3212e8d8bef9SDimitry Andric 3213e8d8bef9SDimitry Andric const std::string &TripleStr = EffectiveTriple.getTriple(); 3214e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_EQ)) { 3215e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3216349cc55cSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() && 3217349cc55cSDimitry Andric !EffectiveTriple.isARM() && !EffectiveTriple.isThumb()) 3218e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3219e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3220349cc55cSDimitry Andric if ((EffectiveTriple.isX86() || EffectiveTriple.isARM() || 3221349cc55cSDimitry Andric EffectiveTriple.isThumb()) && 3222349cc55cSDimitry Andric Value != "tls" && Value != "global") { 3223e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3224fe6060f1SDimitry Andric << A->getOption().getName() << Value << "tls global"; 3225fe6060f1SDimitry Andric return; 3226fe6060f1SDimitry Andric } 3227349cc55cSDimitry Andric if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) && 3228349cc55cSDimitry Andric Value == "tls") { 3229349cc55cSDimitry Andric if (!Args.hasArg(options::OPT_mstack_protector_guard_offset_EQ)) { 3230349cc55cSDimitry Andric D.Diag(diag::err_drv_ssp_missing_offset_argument) 3231349cc55cSDimitry Andric << A->getAsString(Args); 3232349cc55cSDimitry Andric return; 3233349cc55cSDimitry Andric } 3234349cc55cSDimitry Andric // Check whether the target subarch supports the hardware TLS register 323504eeddc0SDimitry Andric if (!arm::isHardTPSupported(EffectiveTriple)) { 3236349cc55cSDimitry Andric D.Diag(diag::err_target_unsupported_tp_hard) 3237349cc55cSDimitry Andric << EffectiveTriple.getArchName(); 3238349cc55cSDimitry Andric return; 3239349cc55cSDimitry Andric } 3240349cc55cSDimitry Andric // Check whether the user asked for something other than -mtp=cp15 3241349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) { 3242349cc55cSDimitry Andric StringRef Value = A->getValue(); 3243349cc55cSDimitry Andric if (Value != "cp15") { 3244349cc55cSDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 3245349cc55cSDimitry Andric << A->getAsString(Args) << "-mstack-protector-guard=tls"; 3246349cc55cSDimitry Andric return; 3247349cc55cSDimitry Andric } 3248349cc55cSDimitry Andric } 3249349cc55cSDimitry Andric CmdArgs.push_back("-target-feature"); 3250349cc55cSDimitry Andric CmdArgs.push_back("+read-tp-hard"); 3251349cc55cSDimitry Andric } 3252fe6060f1SDimitry Andric if (EffectiveTriple.isAArch64() && Value != "sysreg" && Value != "global") { 3253fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3254fe6060f1SDimitry Andric << A->getOption().getName() << Value << "sysreg global"; 3255e8d8bef9SDimitry Andric return; 3256e8d8bef9SDimitry Andric } 3257e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3258e8d8bef9SDimitry Andric } 3259e8d8bef9SDimitry Andric 3260e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_offset_EQ)) { 3261e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3262349cc55cSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() && 3263349cc55cSDimitry Andric !EffectiveTriple.isARM() && !EffectiveTriple.isThumb()) 3264e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3265e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3266fe6060f1SDimitry Andric int Offset; 3267e8d8bef9SDimitry Andric if (Value.getAsInteger(10, Offset)) { 3268e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 3269e8d8bef9SDimitry Andric return; 3270e8d8bef9SDimitry Andric } 3271349cc55cSDimitry Andric if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) && 3272349cc55cSDimitry Andric (Offset < 0 || Offset > 0xfffff)) { 3273349cc55cSDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 3274349cc55cSDimitry Andric << A->getOption().getName() << Value; 3275349cc55cSDimitry Andric return; 3276349cc55cSDimitry Andric } 3277e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3278e8d8bef9SDimitry Andric } 3279e8d8bef9SDimitry Andric 3280e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_reg_EQ)) { 3281e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3282fe6060f1SDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64()) 3283e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3284e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3285e8d8bef9SDimitry Andric if (EffectiveTriple.isX86() && (Value != "fs" && Value != "gs")) { 3286e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3287fe6060f1SDimitry Andric << A->getOption().getName() << Value << "fs gs"; 3288fe6060f1SDimitry Andric return; 3289fe6060f1SDimitry Andric } 3290fe6060f1SDimitry Andric if (EffectiveTriple.isAArch64() && Value != "sp_el0") { 3291fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 3292e8d8bef9SDimitry Andric return; 3293e8d8bef9SDimitry Andric } 3294e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3295e8d8bef9SDimitry Andric } 32960b57cec5SDimitry Andric } 32970b57cec5SDimitry Andric 32985ffd83dbSDimitry Andric static void RenderSCPOptions(const ToolChain &TC, const ArgList &Args, 32995ffd83dbSDimitry Andric ArgStringList &CmdArgs) { 33005ffd83dbSDimitry Andric const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple(); 33015ffd83dbSDimitry Andric 33023063e1e5SEd Maste if (!EffectiveTriple.isOSFreeBSD() && !EffectiveTriple.isOSLinux()) 33035ffd83dbSDimitry Andric return; 33045ffd83dbSDimitry Andric 33055ffd83dbSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isSystemZ() && 33065ffd83dbSDimitry Andric !EffectiveTriple.isPPC64()) 33075ffd83dbSDimitry Andric return; 33085ffd83dbSDimitry Andric 33095ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fstack_clash_protection, 331075b4d546SDimitry Andric options::OPT_fno_stack_clash_protection, false)) 33115ffd83dbSDimitry Andric CmdArgs.push_back("-fstack-clash-protection"); 33125ffd83dbSDimitry Andric } 33135ffd83dbSDimitry Andric 33140b57cec5SDimitry Andric static void RenderTrivialAutoVarInitOptions(const Driver &D, 33150b57cec5SDimitry Andric const ToolChain &TC, 33160b57cec5SDimitry Andric const ArgList &Args, 33170b57cec5SDimitry Andric ArgStringList &CmdArgs) { 33180b57cec5SDimitry Andric auto DefaultTrivialAutoVarInit = TC.GetDefaultTrivialAutoVarInit(); 33190b57cec5SDimitry Andric StringRef TrivialAutoVarInit = ""; 33200b57cec5SDimitry Andric 33210b57cec5SDimitry Andric for (const Arg *A : Args) { 33220b57cec5SDimitry Andric switch (A->getOption().getID()) { 33230b57cec5SDimitry Andric default: 33240b57cec5SDimitry Andric continue; 33250b57cec5SDimitry Andric case options::OPT_ftrivial_auto_var_init: { 33260b57cec5SDimitry Andric A->claim(); 33270b57cec5SDimitry Andric StringRef Val = A->getValue(); 33280b57cec5SDimitry Andric if (Val == "uninitialized" || Val == "zero" || Val == "pattern") 33290b57cec5SDimitry Andric TrivialAutoVarInit = Val; 33300b57cec5SDimitry Andric else 33310b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 33320b57cec5SDimitry Andric << A->getOption().getName() << Val; 33330b57cec5SDimitry Andric break; 33340b57cec5SDimitry Andric } 33350b57cec5SDimitry Andric } 33360b57cec5SDimitry Andric } 33370b57cec5SDimitry Andric 33380b57cec5SDimitry Andric if (TrivialAutoVarInit.empty()) 33390b57cec5SDimitry Andric switch (DefaultTrivialAutoVarInit) { 33400b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Uninitialized: 33410b57cec5SDimitry Andric break; 33420b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Pattern: 33430b57cec5SDimitry Andric TrivialAutoVarInit = "pattern"; 33440b57cec5SDimitry Andric break; 33450b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Zero: 33460b57cec5SDimitry Andric TrivialAutoVarInit = "zero"; 33470b57cec5SDimitry Andric break; 33480b57cec5SDimitry Andric } 33490b57cec5SDimitry Andric 33500b57cec5SDimitry Andric if (!TrivialAutoVarInit.empty()) { 33510b57cec5SDimitry Andric if (TrivialAutoVarInit == "zero" && !Args.hasArg(options::OPT_enable_trivial_var_init_zero)) 33520b57cec5SDimitry Andric D.Diag(diag::err_drv_trivial_auto_var_init_zero_disabled); 33530b57cec5SDimitry Andric CmdArgs.push_back( 33540b57cec5SDimitry Andric Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit)); 33550b57cec5SDimitry Andric } 33565ffd83dbSDimitry Andric 33575ffd83dbSDimitry Andric if (Arg *A = 33585ffd83dbSDimitry Andric Args.getLastArg(options::OPT_ftrivial_auto_var_init_stop_after)) { 33595ffd83dbSDimitry Andric if (!Args.hasArg(options::OPT_ftrivial_auto_var_init) || 33605ffd83dbSDimitry Andric StringRef( 33615ffd83dbSDimitry Andric Args.getLastArg(options::OPT_ftrivial_auto_var_init)->getValue()) == 33625ffd83dbSDimitry Andric "uninitialized") 33635ffd83dbSDimitry Andric D.Diag(diag::err_drv_trivial_auto_var_init_stop_after_missing_dependency); 33645ffd83dbSDimitry Andric A->claim(); 33655ffd83dbSDimitry Andric StringRef Val = A->getValue(); 33665ffd83dbSDimitry Andric if (std::stoi(Val.str()) <= 0) 33675ffd83dbSDimitry Andric D.Diag(diag::err_drv_trivial_auto_var_init_stop_after_invalid_value); 33685ffd83dbSDimitry Andric CmdArgs.push_back( 33695ffd83dbSDimitry Andric Args.MakeArgString("-ftrivial-auto-var-init-stop-after=" + Val)); 33705ffd83dbSDimitry Andric } 33710b57cec5SDimitry Andric } 33720b57cec5SDimitry Andric 3373fe6060f1SDimitry Andric static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs, 3374fe6060f1SDimitry Andric types::ID InputType) { 33755ffd83dbSDimitry Andric // cl-denorms-are-zero is not forwarded. It is translated into a generic flag 33765ffd83dbSDimitry Andric // for denormal flushing handling based on the target. 33770b57cec5SDimitry Andric const unsigned ForwardedArguments[] = { 33780b57cec5SDimitry Andric options::OPT_cl_opt_disable, 33790b57cec5SDimitry Andric options::OPT_cl_strict_aliasing, 33800b57cec5SDimitry Andric options::OPT_cl_single_precision_constant, 33810b57cec5SDimitry Andric options::OPT_cl_finite_math_only, 33820b57cec5SDimitry Andric options::OPT_cl_kernel_arg_info, 33830b57cec5SDimitry Andric options::OPT_cl_unsafe_math_optimizations, 33840b57cec5SDimitry Andric options::OPT_cl_fast_relaxed_math, 33850b57cec5SDimitry Andric options::OPT_cl_mad_enable, 33860b57cec5SDimitry Andric options::OPT_cl_no_signed_zeros, 33870b57cec5SDimitry Andric options::OPT_cl_fp32_correctly_rounded_divide_sqrt, 33880b57cec5SDimitry Andric options::OPT_cl_uniform_work_group_size 33890b57cec5SDimitry Andric }; 33900b57cec5SDimitry Andric 33910b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_cl_std_EQ)) { 33920b57cec5SDimitry Andric std::string CLStdStr = std::string("-cl-std=") + A->getValue(); 33930b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CLStdStr)); 33940b57cec5SDimitry Andric } 33950b57cec5SDimitry Andric 33960b57cec5SDimitry Andric for (const auto &Arg : ForwardedArguments) 33970b57cec5SDimitry Andric if (const auto *A = Args.getLastArg(Arg)) 33980b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName())); 3399fe6060f1SDimitry Andric 3400fe6060f1SDimitry Andric // Only add the default headers if we are compiling OpenCL sources. 3401fe6060f1SDimitry Andric if ((types::isOpenCL(InputType) || 3402fe6060f1SDimitry Andric (Args.hasArg(options::OPT_cl_std_EQ) && types::isSrcFile(InputType))) && 3403fe6060f1SDimitry Andric !Args.hasArg(options::OPT_cl_no_stdinc)) { 3404fe6060f1SDimitry Andric CmdArgs.push_back("-finclude-default-header"); 3405fe6060f1SDimitry Andric CmdArgs.push_back("-fdeclare-opencl-builtins"); 3406fe6060f1SDimitry Andric } 34070b57cec5SDimitry Andric } 34080b57cec5SDimitry Andric 34090b57cec5SDimitry Andric static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args, 34100b57cec5SDimitry Andric ArgStringList &CmdArgs) { 34110b57cec5SDimitry Andric bool ARCMTEnabled = false; 34120b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) { 34130b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check, 34140b57cec5SDimitry Andric options::OPT_ccc_arcmt_modify, 34150b57cec5SDimitry Andric options::OPT_ccc_arcmt_migrate)) { 34160b57cec5SDimitry Andric ARCMTEnabled = true; 34170b57cec5SDimitry Andric switch (A->getOption().getID()) { 34180b57cec5SDimitry Andric default: llvm_unreachable("missed a case"); 34190b57cec5SDimitry Andric case options::OPT_ccc_arcmt_check: 3420e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=check"); 34210b57cec5SDimitry Andric break; 34220b57cec5SDimitry Andric case options::OPT_ccc_arcmt_modify: 3423e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=modify"); 34240b57cec5SDimitry Andric break; 34250b57cec5SDimitry Andric case options::OPT_ccc_arcmt_migrate: 3426e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=migrate"); 34270b57cec5SDimitry Andric CmdArgs.push_back("-mt-migrate-directory"); 34280b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 34290b57cec5SDimitry Andric 34300b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output); 34310b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors); 34320b57cec5SDimitry Andric break; 34330b57cec5SDimitry Andric } 34340b57cec5SDimitry Andric } 34350b57cec5SDimitry Andric } else { 34360b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_check); 34370b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify); 34380b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate); 34390b57cec5SDimitry Andric } 34400b57cec5SDimitry Andric 34410b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) { 34420b57cec5SDimitry Andric if (ARCMTEnabled) 34430b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 34440b57cec5SDimitry Andric << A->getAsString(Args) << "-ccc-arcmt-migrate"; 34450b57cec5SDimitry Andric 34460b57cec5SDimitry Andric CmdArgs.push_back("-mt-migrate-directory"); 34470b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 34480b57cec5SDimitry Andric 34490b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_objcmt_migrate_literals, 34500b57cec5SDimitry Andric options::OPT_objcmt_migrate_subscripting, 34510b57cec5SDimitry Andric options::OPT_objcmt_migrate_property)) { 34520b57cec5SDimitry Andric // None specified, means enable them all. 34530b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-literals"); 34540b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-subscripting"); 34550b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-property"); 34560b57cec5SDimitry Andric } else { 34570b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals); 34580b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting); 34590b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property); 34600b57cec5SDimitry Andric } 34610b57cec5SDimitry Andric } else { 34620b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals); 34630b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting); 34640b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property); 34650b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all); 34660b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property); 34670b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property); 34680b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property_dot_syntax); 34690b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation); 34700b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype); 34710b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros); 34720b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance); 34730b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property); 34740b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property); 34750b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly); 34760b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init); 3477349cc55cSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_allowlist_dir_path); 34780b57cec5SDimitry Andric } 34790b57cec5SDimitry Andric } 34800b57cec5SDimitry Andric 34810b57cec5SDimitry Andric static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T, 34820b57cec5SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 34830b57cec5SDimitry Andric // -fbuiltin is default unless -mkernel is used. 34840b57cec5SDimitry Andric bool UseBuiltins = 34850b57cec5SDimitry Andric Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin, 34860b57cec5SDimitry Andric !Args.hasArg(options::OPT_mkernel)); 34870b57cec5SDimitry Andric if (!UseBuiltins) 34880b57cec5SDimitry Andric CmdArgs.push_back("-fno-builtin"); 34890b57cec5SDimitry Andric 34900b57cec5SDimitry Andric // -ffreestanding implies -fno-builtin. 34910b57cec5SDimitry Andric if (Args.hasArg(options::OPT_ffreestanding)) 34920b57cec5SDimitry Andric UseBuiltins = false; 34930b57cec5SDimitry Andric 34940b57cec5SDimitry Andric // Process the -fno-builtin-* options. 34950b57cec5SDimitry Andric for (const auto &Arg : Args) { 34960b57cec5SDimitry Andric const Option &O = Arg->getOption(); 34970b57cec5SDimitry Andric if (!O.matches(options::OPT_fno_builtin_)) 34980b57cec5SDimitry Andric continue; 34990b57cec5SDimitry Andric 35000b57cec5SDimitry Andric Arg->claim(); 35010b57cec5SDimitry Andric 35020b57cec5SDimitry Andric // If -fno-builtin is specified, then there's no need to pass the option to 35030b57cec5SDimitry Andric // the frontend. 35040b57cec5SDimitry Andric if (!UseBuiltins) 35050b57cec5SDimitry Andric continue; 35060b57cec5SDimitry Andric 35070b57cec5SDimitry Andric StringRef FuncName = Arg->getValue(); 35080b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fno-builtin-" + FuncName)); 35090b57cec5SDimitry Andric } 35100b57cec5SDimitry Andric 35110b57cec5SDimitry Andric // le32-specific flags: 35120b57cec5SDimitry Andric // -fno-math-builtin: clang should not convert math builtins to intrinsics 35130b57cec5SDimitry Andric // by default. 35140b57cec5SDimitry Andric if (TC.getArch() == llvm::Triple::le32) 35150b57cec5SDimitry Andric CmdArgs.push_back("-fno-math-builtin"); 35160b57cec5SDimitry Andric } 35170b57cec5SDimitry Andric 35185ffd83dbSDimitry Andric bool Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) { 35195ffd83dbSDimitry Andric if (llvm::sys::path::cache_directory(Result)) { 35205ffd83dbSDimitry Andric llvm::sys::path::append(Result, "clang"); 35210b57cec5SDimitry Andric llvm::sys::path::append(Result, "ModuleCache"); 35225ffd83dbSDimitry Andric return true; 35235ffd83dbSDimitry Andric } 35245ffd83dbSDimitry Andric return false; 35250b57cec5SDimitry Andric } 35260b57cec5SDimitry Andric 35270b57cec5SDimitry Andric static void RenderModulesOptions(Compilation &C, const Driver &D, 35280b57cec5SDimitry Andric const ArgList &Args, const InputInfo &Input, 35290b57cec5SDimitry Andric const InputInfo &Output, 35300b57cec5SDimitry Andric ArgStringList &CmdArgs, bool &HaveModules) { 35310b57cec5SDimitry Andric // -fmodules enables the use of precompiled modules (off by default). 35320b57cec5SDimitry Andric // Users can pass -fno-cxx-modules to turn off modules support for 35330b57cec5SDimitry Andric // C++/Objective-C++ programs. 35340b57cec5SDimitry Andric bool HaveClangModules = false; 35350b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) { 35360b57cec5SDimitry Andric bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules, 35370b57cec5SDimitry Andric options::OPT_fno_cxx_modules, true); 35380b57cec5SDimitry Andric if (AllowedInCXX || !types::isCXX(Input.getType())) { 35390b57cec5SDimitry Andric CmdArgs.push_back("-fmodules"); 35400b57cec5SDimitry Andric HaveClangModules = true; 35410b57cec5SDimitry Andric } 35420b57cec5SDimitry Andric } 35430b57cec5SDimitry Andric 35440b57cec5SDimitry Andric HaveModules |= HaveClangModules; 35450b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fmodules_ts)) { 35460b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-ts"); 35470b57cec5SDimitry Andric HaveModules = true; 35480b57cec5SDimitry Andric } 35490b57cec5SDimitry Andric 35500b57cec5SDimitry Andric // -fmodule-maps enables implicit reading of module map files. By default, 35510b57cec5SDimitry Andric // this is enabled if we are using Clang's flavor of precompiled modules. 35520b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fimplicit_module_maps, 35530b57cec5SDimitry Andric options::OPT_fno_implicit_module_maps, HaveClangModules)) 35540b57cec5SDimitry Andric CmdArgs.push_back("-fimplicit-module-maps"); 35550b57cec5SDimitry Andric 35560b57cec5SDimitry Andric // -fmodules-decluse checks that modules used are declared so (off by default) 35570b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_decluse, 35580b57cec5SDimitry Andric options::OPT_fno_modules_decluse, false)) 35590b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-decluse"); 35600b57cec5SDimitry Andric 35610b57cec5SDimitry Andric // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that 35620b57cec5SDimitry Andric // all #included headers are part of modules. 35630b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_strict_decluse, 35640b57cec5SDimitry Andric options::OPT_fno_modules_strict_decluse, false)) 35650b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-strict-decluse"); 35660b57cec5SDimitry Andric 35670b57cec5SDimitry Andric // -fno-implicit-modules turns off implicitly compiling modules on demand. 35680b57cec5SDimitry Andric bool ImplicitModules = false; 35690b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fimplicit_modules, 35700b57cec5SDimitry Andric options::OPT_fno_implicit_modules, HaveClangModules)) { 35710b57cec5SDimitry Andric if (HaveModules) 35720b57cec5SDimitry Andric CmdArgs.push_back("-fno-implicit-modules"); 35730b57cec5SDimitry Andric } else if (HaveModules) { 35740b57cec5SDimitry Andric ImplicitModules = true; 35750b57cec5SDimitry Andric // -fmodule-cache-path specifies where our implicitly-built module files 35760b57cec5SDimitry Andric // should be written. 35770b57cec5SDimitry Andric SmallString<128> Path; 35780b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path)) 35790b57cec5SDimitry Andric Path = A->getValue(); 35800b57cec5SDimitry Andric 35815ffd83dbSDimitry Andric bool HasPath = true; 35820b57cec5SDimitry Andric if (C.isForDiagnostics()) { 35830b57cec5SDimitry Andric // When generating crash reports, we want to emit the modules along with 35840b57cec5SDimitry Andric // the reproduction sources, so we ignore any provided module path. 35850b57cec5SDimitry Andric Path = Output.getFilename(); 35860b57cec5SDimitry Andric llvm::sys::path::replace_extension(Path, ".cache"); 35870b57cec5SDimitry Andric llvm::sys::path::append(Path, "modules"); 35880b57cec5SDimitry Andric } else if (Path.empty()) { 35890b57cec5SDimitry Andric // No module path was provided: use the default. 35905ffd83dbSDimitry Andric HasPath = Driver::getDefaultModuleCachePath(Path); 35910b57cec5SDimitry Andric } 35920b57cec5SDimitry Andric 35935ffd83dbSDimitry Andric // `HasPath` will only be false if getDefaultModuleCachePath() fails. 35945ffd83dbSDimitry Andric // That being said, that failure is unlikely and not caching is harmless. 35955ffd83dbSDimitry Andric if (HasPath) { 35960b57cec5SDimitry Andric const char Arg[] = "-fmodules-cache-path="; 35970b57cec5SDimitry Andric Path.insert(Path.begin(), Arg, Arg + strlen(Arg)); 35980b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Path)); 35990b57cec5SDimitry Andric } 36005ffd83dbSDimitry Andric } 36010b57cec5SDimitry Andric 36020b57cec5SDimitry Andric if (HaveModules) { 36030b57cec5SDimitry Andric // -fprebuilt-module-path specifies where to load the prebuilt module files. 36040b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) { 36050b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 36060b57cec5SDimitry Andric std::string("-fprebuilt-module-path=") + A->getValue())); 36070b57cec5SDimitry Andric A->claim(); 36080b57cec5SDimitry Andric } 3609e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fprebuilt_implicit_modules, 3610e8d8bef9SDimitry Andric options::OPT_fno_prebuilt_implicit_modules, false)) 3611e8d8bef9SDimitry Andric CmdArgs.push_back("-fprebuilt-implicit-modules"); 3612a7dea167SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_validate_input_files_content, 3613a7dea167SDimitry Andric options::OPT_fno_modules_validate_input_files_content, 3614a7dea167SDimitry Andric false)) 3615a7dea167SDimitry Andric CmdArgs.push_back("-fvalidate-ast-input-files-content"); 36160b57cec5SDimitry Andric } 36170b57cec5SDimitry Andric 36180b57cec5SDimitry Andric // -fmodule-name specifies the module that is currently being built (or 36190b57cec5SDimitry Andric // used for header checking by -fmodule-maps). 36200b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodule_name_EQ); 36210b57cec5SDimitry Andric 36220b57cec5SDimitry Andric // -fmodule-map-file can be used to specify files containing module 36230b57cec5SDimitry Andric // definitions. 36240b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file); 36250b57cec5SDimitry Andric 36260b57cec5SDimitry Andric // -fbuiltin-module-map can be used to load the clang 36270b57cec5SDimitry Andric // builtin headers modulemap file. 36280b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fbuiltin_module_map)) { 36290b57cec5SDimitry Andric SmallString<128> BuiltinModuleMap(D.ResourceDir); 36300b57cec5SDimitry Andric llvm::sys::path::append(BuiltinModuleMap, "include"); 36310b57cec5SDimitry Andric llvm::sys::path::append(BuiltinModuleMap, "module.modulemap"); 36320b57cec5SDimitry Andric if (llvm::sys::fs::exists(BuiltinModuleMap)) 36330b57cec5SDimitry Andric CmdArgs.push_back( 36340b57cec5SDimitry Andric Args.MakeArgString("-fmodule-map-file=" + BuiltinModuleMap)); 36350b57cec5SDimitry Andric } 36360b57cec5SDimitry Andric 36370b57cec5SDimitry Andric // The -fmodule-file=<name>=<file> form specifies the mapping of module 36380b57cec5SDimitry Andric // names to precompiled module files (the module is loaded only if used). 36390b57cec5SDimitry Andric // The -fmodule-file=<file> form can be used to unconditionally load 36400b57cec5SDimitry Andric // precompiled module files (whether used or not). 36410b57cec5SDimitry Andric if (HaveModules) 36420b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file); 36430b57cec5SDimitry Andric else 36440b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fmodule_file); 36450b57cec5SDimitry Andric 36460b57cec5SDimitry Andric // When building modules and generating crashdumps, we need to dump a module 36470b57cec5SDimitry Andric // dependency VFS alongside the output. 36480b57cec5SDimitry Andric if (HaveClangModules && C.isForDiagnostics()) { 36490b57cec5SDimitry Andric SmallString<128> VFSDir(Output.getFilename()); 36500b57cec5SDimitry Andric llvm::sys::path::replace_extension(VFSDir, ".cache"); 36510b57cec5SDimitry Andric // Add the cache directory as a temp so the crash diagnostics pick it up. 36520b57cec5SDimitry Andric C.addTempFile(Args.MakeArgString(VFSDir)); 36530b57cec5SDimitry Andric 36540b57cec5SDimitry Andric llvm::sys::path::append(VFSDir, "vfs"); 36550b57cec5SDimitry Andric CmdArgs.push_back("-module-dependency-dir"); 36560b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(VFSDir)); 36570b57cec5SDimitry Andric } 36580b57cec5SDimitry Andric 36590b57cec5SDimitry Andric if (HaveClangModules) 36600b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path); 36610b57cec5SDimitry Andric 36620b57cec5SDimitry Andric // Pass through all -fmodules-ignore-macro arguments. 36630b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro); 36640b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval); 36650b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after); 36660b57cec5SDimitry Andric 36670b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp); 36680b57cec5SDimitry Andric 36690b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) { 36700b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fbuild_session_timestamp)) 36710b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 36720b57cec5SDimitry Andric << A->getAsString(Args) << "-fbuild-session-timestamp"; 36730b57cec5SDimitry Andric 36740b57cec5SDimitry Andric llvm::sys::fs::file_status Status; 36750b57cec5SDimitry Andric if (llvm::sys::fs::status(A->getValue(), Status)) 36760b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << A->getValue(); 3677349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 3678349cc55cSDimitry Andric "-fbuild-session-timestamp=" + 3679349cc55cSDimitry Andric Twine((uint64_t)std::chrono::duration_cast<std::chrono::seconds>( 3680349cc55cSDimitry Andric Status.getLastModificationTime().time_since_epoch()) 36810b57cec5SDimitry Andric .count()))); 36820b57cec5SDimitry Andric } 36830b57cec5SDimitry Andric 36840b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_fmodules_validate_once_per_build_session)) { 36850b57cec5SDimitry Andric if (!Args.getLastArg(options::OPT_fbuild_session_timestamp, 36860b57cec5SDimitry Andric options::OPT_fbuild_session_file)) 36870b57cec5SDimitry Andric D.Diag(diag::err_drv_modules_validate_once_requires_timestamp); 36880b57cec5SDimitry Andric 36890b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, 36900b57cec5SDimitry Andric options::OPT_fmodules_validate_once_per_build_session); 36910b57cec5SDimitry Andric } 36920b57cec5SDimitry Andric 36930b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_validate_system_headers, 36940b57cec5SDimitry Andric options::OPT_fno_modules_validate_system_headers, 36950b57cec5SDimitry Andric ImplicitModules)) 36960b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-validate-system-headers"); 36970b57cec5SDimitry Andric 36980b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_disable_diagnostic_validation); 36990b57cec5SDimitry Andric } 37000b57cec5SDimitry Andric 37010b57cec5SDimitry Andric static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T, 37020b57cec5SDimitry Andric ArgStringList &CmdArgs) { 37030b57cec5SDimitry Andric // -fsigned-char is default. 37040b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fsigned_char, 37050b57cec5SDimitry Andric options::OPT_fno_signed_char, 37060b57cec5SDimitry Andric options::OPT_funsigned_char, 37070b57cec5SDimitry Andric options::OPT_fno_unsigned_char)) { 37080b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_funsigned_char) || 37090b57cec5SDimitry Andric A->getOption().matches(options::OPT_fno_signed_char)) { 37100b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-char"); 37110b57cec5SDimitry Andric } 37120b57cec5SDimitry Andric } else if (!isSignedCharDefault(T)) { 37130b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-char"); 37140b57cec5SDimitry Andric } 37150b57cec5SDimitry Andric 37160b57cec5SDimitry Andric // The default depends on the language standard. 37170b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fchar8__t, options::OPT_fno_char8__t); 37180b57cec5SDimitry Andric 37190b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fshort_wchar, 37200b57cec5SDimitry Andric options::OPT_fno_short_wchar)) { 37210b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fshort_wchar)) { 37220b57cec5SDimitry Andric CmdArgs.push_back("-fwchar-type=short"); 37230b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-wchar"); 37240b57cec5SDimitry Andric } else { 37250b57cec5SDimitry Andric bool IsARM = T.isARM() || T.isThumb() || T.isAArch64(); 37260b57cec5SDimitry Andric CmdArgs.push_back("-fwchar-type=int"); 3727e8d8bef9SDimitry Andric if (T.isOSzOS() || 3728e8d8bef9SDimitry Andric (IsARM && !(T.isOSWindows() || T.isOSNetBSD() || T.isOSOpenBSD()))) 37290b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-wchar"); 37300b57cec5SDimitry Andric else 37310b57cec5SDimitry Andric CmdArgs.push_back("-fsigned-wchar"); 37320b57cec5SDimitry Andric } 37330b57cec5SDimitry Andric } 37340b57cec5SDimitry Andric } 37350b57cec5SDimitry Andric 37360b57cec5SDimitry Andric static void RenderObjCOptions(const ToolChain &TC, const Driver &D, 37370b57cec5SDimitry Andric const llvm::Triple &T, const ArgList &Args, 37380b57cec5SDimitry Andric ObjCRuntime &Runtime, bool InferCovariantReturns, 37390b57cec5SDimitry Andric const InputInfo &Input, ArgStringList &CmdArgs) { 37400b57cec5SDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 37410b57cec5SDimitry Andric 37420b57cec5SDimitry Andric // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and legacy 37430b57cec5SDimitry Andric // is the default. Except for deployment target of 10.5, next runtime is 37440b57cec5SDimitry Andric // always legacy dispatch and -fno-objc-legacy-dispatch gets ignored silently. 37450b57cec5SDimitry Andric if (Runtime.isNonFragile()) { 37460b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch, 37470b57cec5SDimitry Andric options::OPT_fno_objc_legacy_dispatch, 37480b57cec5SDimitry Andric Runtime.isLegacyDispatchDefaultForArch(Arch))) { 37490b57cec5SDimitry Andric if (TC.UseObjCMixedDispatch()) 37500b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-dispatch-method=mixed"); 37510b57cec5SDimitry Andric else 37520b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-dispatch-method=non-legacy"); 37530b57cec5SDimitry Andric } 37540b57cec5SDimitry Andric } 37550b57cec5SDimitry Andric 37560b57cec5SDimitry Andric // When ObjectiveC legacy runtime is in effect on MacOSX, turn on the option 37570b57cec5SDimitry Andric // to do Array/Dictionary subscripting by default. 37580b57cec5SDimitry Andric if (Arch == llvm::Triple::x86 && T.isMacOSX() && 37590b57cec5SDimitry Andric Runtime.getKind() == ObjCRuntime::FragileMacOSX && Runtime.isNeXTFamily()) 37600b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-subscripting-legacy-runtime"); 37610b57cec5SDimitry Andric 37620b57cec5SDimitry Andric // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc. 37630b57cec5SDimitry Andric // NOTE: This logic is duplicated in ToolChains.cpp. 37640b57cec5SDimitry Andric if (isObjCAutoRefCount(Args)) { 37650b57cec5SDimitry Andric TC.CheckObjCARC(); 37660b57cec5SDimitry Andric 37670b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc"); 37680b57cec5SDimitry Andric 37690b57cec5SDimitry Andric // FIXME: It seems like this entire block, and several around it should be 37700b57cec5SDimitry Andric // wrapped in isObjC, but for now we just use it here as this is where it 37710b57cec5SDimitry Andric // was being used previously. 37720b57cec5SDimitry Andric if (types::isCXX(Input.getType()) && types::isObjC(Input.getType())) { 37730b57cec5SDimitry Andric if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) 37740b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-cxxlib=libc++"); 37750b57cec5SDimitry Andric else 37760b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++"); 37770b57cec5SDimitry Andric } 37780b57cec5SDimitry Andric 37790b57cec5SDimitry Andric // Allow the user to enable full exceptions code emission. 37800b57cec5SDimitry Andric // We default off for Objective-C, on for Objective-C++. 37810b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fobjc_arc_exceptions, 37820b57cec5SDimitry Andric options::OPT_fno_objc_arc_exceptions, 37830b57cec5SDimitry Andric /*Default=*/types::isCXX(Input.getType()))) 37840b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-exceptions"); 37850b57cec5SDimitry Andric } 37860b57cec5SDimitry Andric 37870b57cec5SDimitry Andric // Silence warning for full exception code emission options when explicitly 37880b57cec5SDimitry Andric // set to use no ARC. 37890b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fno_objc_arc)) { 37900b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fobjc_arc_exceptions); 37910b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_objc_arc_exceptions); 37920b57cec5SDimitry Andric } 37930b57cec5SDimitry Andric 37940b57cec5SDimitry Andric // Allow the user to control whether messages can be converted to runtime 37950b57cec5SDimitry Andric // functions. 37960b57cec5SDimitry Andric if (types::isObjC(Input.getType())) { 37970b57cec5SDimitry Andric auto *Arg = Args.getLastArg( 37980b57cec5SDimitry Andric options::OPT_fobjc_convert_messages_to_runtime_calls, 37990b57cec5SDimitry Andric options::OPT_fno_objc_convert_messages_to_runtime_calls); 38000b57cec5SDimitry Andric if (Arg && 38010b57cec5SDimitry Andric Arg->getOption().matches( 38020b57cec5SDimitry Andric options::OPT_fno_objc_convert_messages_to_runtime_calls)) 38030b57cec5SDimitry Andric CmdArgs.push_back("-fno-objc-convert-messages-to-runtime-calls"); 38040b57cec5SDimitry Andric } 38050b57cec5SDimitry Andric 38060b57cec5SDimitry Andric // -fobjc-infer-related-result-type is the default, except in the Objective-C 38070b57cec5SDimitry Andric // rewriter. 38080b57cec5SDimitry Andric if (InferCovariantReturns) 38090b57cec5SDimitry Andric CmdArgs.push_back("-fno-objc-infer-related-result-type"); 38100b57cec5SDimitry Andric 38110b57cec5SDimitry Andric // Pass down -fobjc-weak or -fno-objc-weak if present. 38120b57cec5SDimitry Andric if (types::isObjC(Input.getType())) { 38130b57cec5SDimitry Andric auto WeakArg = 38140b57cec5SDimitry Andric Args.getLastArg(options::OPT_fobjc_weak, options::OPT_fno_objc_weak); 38150b57cec5SDimitry Andric if (!WeakArg) { 38160b57cec5SDimitry Andric // nothing to do 38170b57cec5SDimitry Andric } else if (!Runtime.allowsWeak()) { 38180b57cec5SDimitry Andric if (WeakArg->getOption().matches(options::OPT_fobjc_weak)) 38190b57cec5SDimitry Andric D.Diag(diag::err_objc_weak_unsupported); 38200b57cec5SDimitry Andric } else { 38210b57cec5SDimitry Andric WeakArg->render(Args, CmdArgs); 38220b57cec5SDimitry Andric } 38230b57cec5SDimitry Andric } 3824fe6060f1SDimitry Andric 3825fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fobjc_disable_direct_methods_for_testing)) 3826fe6060f1SDimitry Andric CmdArgs.push_back("-fobjc-disable-direct-methods-for-testing"); 38270b57cec5SDimitry Andric } 38280b57cec5SDimitry Andric 38290b57cec5SDimitry Andric static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args, 38300b57cec5SDimitry Andric ArgStringList &CmdArgs) { 38310b57cec5SDimitry Andric bool CaretDefault = true; 38320b57cec5SDimitry Andric bool ColumnDefault = true; 38330b57cec5SDimitry Andric 38340b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diagnostics_classic, 38350b57cec5SDimitry Andric options::OPT__SLASH_diagnostics_column, 38360b57cec5SDimitry Andric options::OPT__SLASH_diagnostics_caret)) { 38370b57cec5SDimitry Andric switch (A->getOption().getID()) { 38380b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_caret: 38390b57cec5SDimitry Andric CaretDefault = true; 38400b57cec5SDimitry Andric ColumnDefault = true; 38410b57cec5SDimitry Andric break; 38420b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_column: 38430b57cec5SDimitry Andric CaretDefault = false; 38440b57cec5SDimitry Andric ColumnDefault = true; 38450b57cec5SDimitry Andric break; 38460b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_classic: 38470b57cec5SDimitry Andric CaretDefault = false; 38480b57cec5SDimitry Andric ColumnDefault = false; 38490b57cec5SDimitry Andric break; 38500b57cec5SDimitry Andric } 38510b57cec5SDimitry Andric } 38520b57cec5SDimitry Andric 38530b57cec5SDimitry Andric // -fcaret-diagnostics is default. 38540b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fcaret_diagnostics, 38550b57cec5SDimitry Andric options::OPT_fno_caret_diagnostics, CaretDefault)) 38560b57cec5SDimitry Andric CmdArgs.push_back("-fno-caret-diagnostics"); 38570b57cec5SDimitry Andric 38580b57cec5SDimitry Andric // -fdiagnostics-fixit-info is default, only pass non-default. 38590b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info, 38600b57cec5SDimitry Andric options::OPT_fno_diagnostics_fixit_info)) 38610b57cec5SDimitry Andric CmdArgs.push_back("-fno-diagnostics-fixit-info"); 38620b57cec5SDimitry Andric 38630b57cec5SDimitry Andric // Enable -fdiagnostics-show-option by default. 38645ffd83dbSDimitry Andric if (!Args.hasFlag(options::OPT_fdiagnostics_show_option, 38655ffd83dbSDimitry Andric options::OPT_fno_diagnostics_show_option, true)) 38665ffd83dbSDimitry Andric CmdArgs.push_back("-fno-diagnostics-show-option"); 38670b57cec5SDimitry Andric 38680b57cec5SDimitry Andric if (const Arg *A = 38690b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) { 38700b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-show-category"); 38710b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 38720b57cec5SDimitry Andric } 38730b57cec5SDimitry Andric 38740b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdiagnostics_show_hotness, 38750b57cec5SDimitry Andric options::OPT_fno_diagnostics_show_hotness, false)) 38760b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-show-hotness"); 38770b57cec5SDimitry Andric 38780b57cec5SDimitry Andric if (const Arg *A = 38790b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) { 38800b57cec5SDimitry Andric std::string Opt = 38810b57cec5SDimitry Andric std::string("-fdiagnostics-hotness-threshold=") + A->getValue(); 38820b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Opt)); 38830b57cec5SDimitry Andric } 38840b57cec5SDimitry Andric 38850b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) { 38860b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-format"); 38870b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 38880b57cec5SDimitry Andric } 38890b57cec5SDimitry Andric 38900b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg( 38910b57cec5SDimitry Andric options::OPT_fdiagnostics_show_note_include_stack, 38920b57cec5SDimitry Andric options::OPT_fno_diagnostics_show_note_include_stack)) { 38930b57cec5SDimitry Andric const Option &O = A->getOption(); 38940b57cec5SDimitry Andric if (O.matches(options::OPT_fdiagnostics_show_note_include_stack)) 38950b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-show-note-include-stack"); 38960b57cec5SDimitry Andric else 38970b57cec5SDimitry Andric CmdArgs.push_back("-fno-diagnostics-show-note-include-stack"); 38980b57cec5SDimitry Andric } 38990b57cec5SDimitry Andric 39000b57cec5SDimitry Andric // Color diagnostics are parsed by the driver directly from argv and later 39010b57cec5SDimitry Andric // re-parsed to construct this job; claim any possible color diagnostic here 39020b57cec5SDimitry Andric // to avoid warn_drv_unused_argument and diagnose bad 39030b57cec5SDimitry Andric // OPT_fdiagnostics_color_EQ values. 39040b57cec5SDimitry Andric for (const Arg *A : Args) { 39050b57cec5SDimitry Andric const Option &O = A->getOption(); 39060b57cec5SDimitry Andric if (!O.matches(options::OPT_fcolor_diagnostics) && 39070b57cec5SDimitry Andric !O.matches(options::OPT_fdiagnostics_color) && 39080b57cec5SDimitry Andric !O.matches(options::OPT_fno_color_diagnostics) && 39090b57cec5SDimitry Andric !O.matches(options::OPT_fno_diagnostics_color) && 39100b57cec5SDimitry Andric !O.matches(options::OPT_fdiagnostics_color_EQ)) 39110b57cec5SDimitry Andric continue; 39120b57cec5SDimitry Andric 39130b57cec5SDimitry Andric if (O.matches(options::OPT_fdiagnostics_color_EQ)) { 39140b57cec5SDimitry Andric StringRef Value(A->getValue()); 39150b57cec5SDimitry Andric if (Value != "always" && Value != "never" && Value != "auto") 39160b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) 39170b57cec5SDimitry Andric << ("-fdiagnostics-color=" + Value).str(); 39180b57cec5SDimitry Andric } 39190b57cec5SDimitry Andric A->claim(); 39200b57cec5SDimitry Andric } 39210b57cec5SDimitry Andric 39220b57cec5SDimitry Andric if (D.getDiags().getDiagnosticOptions().ShowColors) 39230b57cec5SDimitry Andric CmdArgs.push_back("-fcolor-diagnostics"); 39240b57cec5SDimitry Andric 39250b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fansi_escape_codes)) 39260b57cec5SDimitry Andric CmdArgs.push_back("-fansi-escape-codes"); 39270b57cec5SDimitry Andric 39280b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fshow_source_location, 39290b57cec5SDimitry Andric options::OPT_fno_show_source_location)) 39300b57cec5SDimitry Andric CmdArgs.push_back("-fno-show-source-location"); 39310b57cec5SDimitry Andric 39320b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdiagnostics_absolute_paths)) 39330b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-absolute-paths"); 39340b57cec5SDimitry Andric 39350b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fshow_column, options::OPT_fno_show_column, 39360b57cec5SDimitry Andric ColumnDefault)) 39370b57cec5SDimitry Andric CmdArgs.push_back("-fno-show-column"); 39380b57cec5SDimitry Andric 39390b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fspell_checking, 39400b57cec5SDimitry Andric options::OPT_fno_spell_checking)) 39410b57cec5SDimitry Andric CmdArgs.push_back("-fno-spell-checking"); 39420b57cec5SDimitry Andric } 39430b57cec5SDimitry Andric 39440b57cec5SDimitry Andric enum class DwarfFissionKind { None, Split, Single }; 39450b57cec5SDimitry Andric 39460b57cec5SDimitry Andric static DwarfFissionKind getDebugFissionKind(const Driver &D, 39470b57cec5SDimitry Andric const ArgList &Args, Arg *&Arg) { 3948e8d8bef9SDimitry Andric Arg = Args.getLastArg(options::OPT_gsplit_dwarf, options::OPT_gsplit_dwarf_EQ, 3949e8d8bef9SDimitry Andric options::OPT_gno_split_dwarf); 3950e8d8bef9SDimitry Andric if (!Arg || Arg->getOption().matches(options::OPT_gno_split_dwarf)) 39510b57cec5SDimitry Andric return DwarfFissionKind::None; 39520b57cec5SDimitry Andric 39530b57cec5SDimitry Andric if (Arg->getOption().matches(options::OPT_gsplit_dwarf)) 39540b57cec5SDimitry Andric return DwarfFissionKind::Split; 39550b57cec5SDimitry Andric 39560b57cec5SDimitry Andric StringRef Value = Arg->getValue(); 39570b57cec5SDimitry Andric if (Value == "split") 39580b57cec5SDimitry Andric return DwarfFissionKind::Split; 39590b57cec5SDimitry Andric if (Value == "single") 39600b57cec5SDimitry Andric return DwarfFissionKind::Single; 39610b57cec5SDimitry Andric 39620b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 39630b57cec5SDimitry Andric << Arg->getOption().getName() << Arg->getValue(); 39640b57cec5SDimitry Andric return DwarfFissionKind::None; 39650b57cec5SDimitry Andric } 39660b57cec5SDimitry Andric 3967fe6060f1SDimitry Andric static void renderDwarfFormat(const Driver &D, const llvm::Triple &T, 3968fe6060f1SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs, 3969fe6060f1SDimitry Andric unsigned DwarfVersion) { 3970fe6060f1SDimitry Andric auto *DwarfFormatArg = 3971fe6060f1SDimitry Andric Args.getLastArg(options::OPT_gdwarf64, options::OPT_gdwarf32); 3972fe6060f1SDimitry Andric if (!DwarfFormatArg) 3973fe6060f1SDimitry Andric return; 3974fe6060f1SDimitry Andric 3975fe6060f1SDimitry Andric if (DwarfFormatArg->getOption().matches(options::OPT_gdwarf64)) { 3976fe6060f1SDimitry Andric if (DwarfVersion < 3) 3977fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 3978fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "DWARFv3 or greater"; 3979fe6060f1SDimitry Andric else if (!T.isArch64Bit()) 3980fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 3981fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "64 bit architecture"; 3982fe6060f1SDimitry Andric else if (!T.isOSBinFormatELF()) 3983fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 3984fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "ELF platforms"; 3985fe6060f1SDimitry Andric } 3986fe6060f1SDimitry Andric 3987fe6060f1SDimitry Andric DwarfFormatArg->render(Args, CmdArgs); 3988fe6060f1SDimitry Andric } 3989fe6060f1SDimitry Andric 3990e8d8bef9SDimitry Andric static void renderDebugOptions(const ToolChain &TC, const Driver &D, 39910b57cec5SDimitry Andric const llvm::Triple &T, const ArgList &Args, 3992e8d8bef9SDimitry Andric bool EmitCodeView, bool IRInput, 3993e8d8bef9SDimitry Andric ArgStringList &CmdArgs, 39940b57cec5SDimitry Andric codegenoptions::DebugInfoKind &DebugInfoKind, 39950b57cec5SDimitry Andric DwarfFissionKind &DwarfFission) { 39960b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_info_for_profiling, 39970b57cec5SDimitry Andric options::OPT_fno_debug_info_for_profiling, false) && 39980b57cec5SDimitry Andric checkDebugInfoOption( 39990b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdebug_info_for_profiling), Args, D, TC)) 40000b57cec5SDimitry Andric CmdArgs.push_back("-fdebug-info-for-profiling"); 40010b57cec5SDimitry Andric 40020b57cec5SDimitry Andric // The 'g' groups options involve a somewhat intricate sequence of decisions 40030b57cec5SDimitry Andric // about what to pass from the driver to the frontend, but by the time they 40040b57cec5SDimitry Andric // reach cc1 they've been factored into three well-defined orthogonal choices: 40050b57cec5SDimitry Andric // * what level of debug info to generate 40060b57cec5SDimitry Andric // * what dwarf version to write 40070b57cec5SDimitry Andric // * what debugger tuning to use 40080b57cec5SDimitry Andric // This avoids having to monkey around further in cc1 other than to disable 40090b57cec5SDimitry Andric // codeview if not running in a Windows environment. Perhaps even that 40100b57cec5SDimitry Andric // decision should be made in the driver as well though. 40110b57cec5SDimitry Andric llvm::DebuggerKind DebuggerTuning = TC.getDefaultDebuggerTuning(); 40120b57cec5SDimitry Andric 40130b57cec5SDimitry Andric bool SplitDWARFInlining = 40140b57cec5SDimitry Andric Args.hasFlag(options::OPT_fsplit_dwarf_inlining, 4015480093f4SDimitry Andric options::OPT_fno_split_dwarf_inlining, false); 40160b57cec5SDimitry Andric 4017e8d8bef9SDimitry Andric // Normally -gsplit-dwarf is only useful with -gN. For IR input, Clang does 4018e8d8bef9SDimitry Andric // object file generation and no IR generation, -gN should not be needed. So 4019e8d8bef9SDimitry Andric // allow -gsplit-dwarf with either -gN or IR input. 4020e8d8bef9SDimitry Andric if (IRInput || Args.hasArg(options::OPT_g_Group)) { 40210b57cec5SDimitry Andric Arg *SplitDWARFArg; 40220b57cec5SDimitry Andric DwarfFission = getDebugFissionKind(D, Args, SplitDWARFArg); 40230b57cec5SDimitry Andric if (DwarfFission != DwarfFissionKind::None && 40240b57cec5SDimitry Andric !checkDebugInfoOption(SplitDWARFArg, Args, D, TC)) { 40250b57cec5SDimitry Andric DwarfFission = DwarfFissionKind::None; 40260b57cec5SDimitry Andric SplitDWARFInlining = false; 40270b57cec5SDimitry Andric } 4028e8d8bef9SDimitry Andric } 4029e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { 4030fe6060f1SDimitry Andric DebugInfoKind = codegenoptions::DebugInfoConstructor; 40310b57cec5SDimitry Andric 40320b57cec5SDimitry Andric // If the last option explicitly specified a debug-info level, use it. 40330b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC) && 40340b57cec5SDimitry Andric A->getOption().matches(options::OPT_gN_Group)) { 40350b57cec5SDimitry Andric DebugInfoKind = DebugLevelToInfoKind(*A); 40360b57cec5SDimitry Andric // For -g0 or -gline-tables-only, drop -gsplit-dwarf. This gets a bit more 40370b57cec5SDimitry Andric // complicated if you've disabled inline info in the skeleton CUs 40380b57cec5SDimitry Andric // (SplitDWARFInlining) - then there's value in composing split-dwarf and 40390b57cec5SDimitry Andric // line-tables-only, so let those compose naturally in that case. 40400b57cec5SDimitry Andric if (DebugInfoKind == codegenoptions::NoDebugInfo || 40410b57cec5SDimitry Andric DebugInfoKind == codegenoptions::DebugDirectivesOnly || 40420b57cec5SDimitry Andric (DebugInfoKind == codegenoptions::DebugLineTablesOnly && 40430b57cec5SDimitry Andric SplitDWARFInlining)) 40440b57cec5SDimitry Andric DwarfFission = DwarfFissionKind::None; 40450b57cec5SDimitry Andric } 40460b57cec5SDimitry Andric } 40470b57cec5SDimitry Andric 40480b57cec5SDimitry Andric // If a debugger tuning argument appeared, remember it. 40490b57cec5SDimitry Andric if (const Arg *A = 40500b57cec5SDimitry Andric Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) { 40510b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) { 40520b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_glldb)) 40530b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::LLDB; 40540b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_gsce)) 40550b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::SCE; 4056fe6060f1SDimitry Andric else if (A->getOption().matches(options::OPT_gdbx)) 4057fe6060f1SDimitry Andric DebuggerTuning = llvm::DebuggerKind::DBX; 40580b57cec5SDimitry Andric else 40590b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::GDB; 40600b57cec5SDimitry Andric } 40610b57cec5SDimitry Andric } 40620b57cec5SDimitry Andric 40630b57cec5SDimitry Andric // If a -gdwarf argument appeared, remember it. 4064fe6060f1SDimitry Andric const Arg *GDwarfN = getDwarfNArg(Args); 4065480093f4SDimitry Andric bool EmitDwarf = false; 4066480093f4SDimitry Andric if (GDwarfN) { 4067480093f4SDimitry Andric if (checkDebugInfoOption(GDwarfN, Args, D, TC)) 4068480093f4SDimitry Andric EmitDwarf = true; 4069480093f4SDimitry Andric else 4070480093f4SDimitry Andric GDwarfN = nullptr; 4071480093f4SDimitry Andric } 40720b57cec5SDimitry Andric 40730b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) { 40740b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) 40750b57cec5SDimitry Andric EmitCodeView = true; 40760b57cec5SDimitry Andric } 40770b57cec5SDimitry Andric 40780b57cec5SDimitry Andric // If the user asked for debug info but did not explicitly specify -gcodeview 40790b57cec5SDimitry Andric // or -gdwarf, ask the toolchain for the default format. 4080480093f4SDimitry Andric if (!EmitCodeView && !EmitDwarf && 40810b57cec5SDimitry Andric DebugInfoKind != codegenoptions::NoDebugInfo) { 40820b57cec5SDimitry Andric switch (TC.getDefaultDebugFormat()) { 40830b57cec5SDimitry Andric case codegenoptions::DIF_CodeView: 40840b57cec5SDimitry Andric EmitCodeView = true; 40850b57cec5SDimitry Andric break; 40860b57cec5SDimitry Andric case codegenoptions::DIF_DWARF: 4087480093f4SDimitry Andric EmitDwarf = true; 40880b57cec5SDimitry Andric break; 40890b57cec5SDimitry Andric } 40900b57cec5SDimitry Andric } 40910b57cec5SDimitry Andric 4092e8d8bef9SDimitry Andric unsigned RequestedDWARFVersion = 0; // DWARF version requested by the user 4093e8d8bef9SDimitry Andric unsigned EffectiveDWARFVersion = 0; // DWARF version TC can generate. It may 4094e8d8bef9SDimitry Andric // be lower than what the user wanted. 4095480093f4SDimitry Andric unsigned DefaultDWARFVersion = ParseDebugDefaultVersion(TC, Args); 4096480093f4SDimitry Andric if (EmitDwarf) { 4097480093f4SDimitry Andric // Start with the platform default DWARF version 4098e8d8bef9SDimitry Andric RequestedDWARFVersion = TC.GetDefaultDwarfVersion(); 4099e8d8bef9SDimitry Andric assert(RequestedDWARFVersion && 4100e8d8bef9SDimitry Andric "toolchain default DWARF version must be nonzero"); 4101480093f4SDimitry Andric 4102480093f4SDimitry Andric // If the user specified a default DWARF version, that takes precedence 4103480093f4SDimitry Andric // over the platform default. 4104480093f4SDimitry Andric if (DefaultDWARFVersion) 4105e8d8bef9SDimitry Andric RequestedDWARFVersion = DefaultDWARFVersion; 4106480093f4SDimitry Andric 4107480093f4SDimitry Andric // Override with a user-specified DWARF version 4108480093f4SDimitry Andric if (GDwarfN) 4109480093f4SDimitry Andric if (auto ExplicitVersion = DwarfVersionNum(GDwarfN->getSpelling())) 4110e8d8bef9SDimitry Andric RequestedDWARFVersion = ExplicitVersion; 4111e8d8bef9SDimitry Andric // Clamp effective DWARF version to the max supported by the toolchain. 4112e8d8bef9SDimitry Andric EffectiveDWARFVersion = 4113e8d8bef9SDimitry Andric std::min(RequestedDWARFVersion, TC.getMaxDwarfVersion()); 4114480093f4SDimitry Andric } 4115480093f4SDimitry Andric 41160b57cec5SDimitry Andric // -gline-directives-only supported only for the DWARF debug info. 4117e8d8bef9SDimitry Andric if (RequestedDWARFVersion == 0 && 4118e8d8bef9SDimitry Andric DebugInfoKind == codegenoptions::DebugDirectivesOnly) 41190b57cec5SDimitry Andric DebugInfoKind = codegenoptions::NoDebugInfo; 41200b57cec5SDimitry Andric 4121fe6060f1SDimitry Andric // strict DWARF is set to false by default. But for DBX, we need it to be set 4122fe6060f1SDimitry Andric // as true by default. 4123fe6060f1SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gstrict_dwarf)) 4124fe6060f1SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 4125fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_gstrict_dwarf, options::OPT_gno_strict_dwarf, 4126fe6060f1SDimitry Andric DebuggerTuning == llvm::DebuggerKind::DBX)) 4127fe6060f1SDimitry Andric CmdArgs.push_back("-gstrict-dwarf"); 4128fe6060f1SDimitry Andric 41290b57cec5SDimitry Andric // And we handle flag -grecord-gcc-switches later with DWARFDebugFlags. 41300b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_g_flags_Group); 41310b57cec5SDimitry Andric 41320b57cec5SDimitry Andric // Column info is included by default for everything except SCE and 41330b57cec5SDimitry Andric // CodeView. Clang doesn't track end columns, just starting columns, which, 41340b57cec5SDimitry Andric // in theory, is fine for CodeView (and PDB). In practice, however, the 4135fe6060f1SDimitry Andric // Microsoft debuggers don't handle missing end columns well, and the AIX 4136fe6060f1SDimitry Andric // debugger DBX also doesn't handle the columns well, so it's better not to 4137fe6060f1SDimitry Andric // include any column info. 41380b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gcolumn_info)) 41390b57cec5SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 41405ffd83dbSDimitry Andric if (!Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info, 4141fe6060f1SDimitry Andric !EmitCodeView && 4142fe6060f1SDimitry Andric (DebuggerTuning != llvm::DebuggerKind::SCE && 4143fe6060f1SDimitry Andric DebuggerTuning != llvm::DebuggerKind::DBX))) 41445ffd83dbSDimitry Andric CmdArgs.push_back("-gno-column-info"); 41450b57cec5SDimitry Andric 41460b57cec5SDimitry Andric // FIXME: Move backend command line options to the module. 41470b57cec5SDimitry Andric // If -gline-tables-only or -gline-directives-only is the last option it wins. 41480b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gmodules)) 41490b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) { 41500b57cec5SDimitry Andric if (DebugInfoKind != codegenoptions::DebugLineTablesOnly && 41510b57cec5SDimitry Andric DebugInfoKind != codegenoptions::DebugDirectivesOnly) { 4152fe6060f1SDimitry Andric DebugInfoKind = codegenoptions::DebugInfoConstructor; 41530b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-ext-refs"); 41540b57cec5SDimitry Andric CmdArgs.push_back("-fmodule-format=obj"); 41550b57cec5SDimitry Andric } 41560b57cec5SDimitry Andric } 41570b57cec5SDimitry Andric 4158fe6060f1SDimitry Andric if (T.isOSBinFormatELF() && SplitDWARFInlining) 4159fe6060f1SDimitry Andric CmdArgs.push_back("-fsplit-dwarf-inlining"); 41600b57cec5SDimitry Andric 41610b57cec5SDimitry Andric // After we've dealt with all combinations of things that could 41620b57cec5SDimitry Andric // make DebugInfoKind be other than None or DebugLineTablesOnly, 41630b57cec5SDimitry Andric // figure out if we need to "upgrade" it to standalone debug info. 41640b57cec5SDimitry Andric // We parse these two '-f' options whether or not they will be used, 41650b57cec5SDimitry Andric // to claim them even if you wrote "-fstandalone-debug -gline-tables-only" 41660b57cec5SDimitry Andric bool NeedFullDebug = Args.hasFlag( 41670b57cec5SDimitry Andric options::OPT_fstandalone_debug, options::OPT_fno_standalone_debug, 41680b57cec5SDimitry Andric DebuggerTuning == llvm::DebuggerKind::LLDB || 41690b57cec5SDimitry Andric TC.GetDefaultStandaloneDebug()); 41700b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fstandalone_debug)) 41710b57cec5SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 4172e8d8bef9SDimitry Andric 4173fe6060f1SDimitry Andric if (DebugInfoKind == codegenoptions::LimitedDebugInfo || 4174fe6060f1SDimitry Andric DebugInfoKind == codegenoptions::DebugInfoConstructor) { 4175e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fno_eliminate_unused_debug_types, 4176e8d8bef9SDimitry Andric options::OPT_feliminate_unused_debug_types, false)) 4177e8d8bef9SDimitry Andric DebugInfoKind = codegenoptions::UnusedTypeInfo; 4178e8d8bef9SDimitry Andric else if (NeedFullDebug) 41790b57cec5SDimitry Andric DebugInfoKind = codegenoptions::FullDebugInfo; 4180e8d8bef9SDimitry Andric } 41810b57cec5SDimitry Andric 41820b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source, 41830b57cec5SDimitry Andric false)) { 41840b57cec5SDimitry Andric // Source embedding is a vendor extension to DWARF v5. By now we have 41850b57cec5SDimitry Andric // checked if a DWARF version was stated explicitly, and have otherwise 41860b57cec5SDimitry Andric // fallen back to the target default, so if this is still not at least 5 41870b57cec5SDimitry Andric // we emit an error. 41880b57cec5SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_gembed_source); 4189e8d8bef9SDimitry Andric if (RequestedDWARFVersion < 5) 41900b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 41910b57cec5SDimitry Andric << A->getAsString(Args) << "-gdwarf-5"; 4192e8d8bef9SDimitry Andric else if (EffectiveDWARFVersion < 5) 4193e8d8bef9SDimitry Andric // The toolchain has reduced allowed dwarf version, so we can't enable 4194e8d8bef9SDimitry Andric // -gembed-source. 4195e8d8bef9SDimitry Andric D.Diag(diag::warn_drv_dwarf_version_limited_by_target) 4196e8d8bef9SDimitry Andric << A->getAsString(Args) << TC.getTripleString() << 5 4197e8d8bef9SDimitry Andric << EffectiveDWARFVersion; 41980b57cec5SDimitry Andric else if (checkDebugInfoOption(A, Args, D, TC)) 41990b57cec5SDimitry Andric CmdArgs.push_back("-gembed-source"); 42000b57cec5SDimitry Andric } 42010b57cec5SDimitry Andric 42020b57cec5SDimitry Andric if (EmitCodeView) { 42030b57cec5SDimitry Andric CmdArgs.push_back("-gcodeview"); 42040b57cec5SDimitry Andric 42050b57cec5SDimitry Andric // Emit codeview type hashes if requested. 42060b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_gcodeview_ghash, 42070b57cec5SDimitry Andric options::OPT_gno_codeview_ghash, false)) { 42080b57cec5SDimitry Andric CmdArgs.push_back("-gcodeview-ghash"); 42090b57cec5SDimitry Andric } 42100b57cec5SDimitry Andric } 42110b57cec5SDimitry Andric 4212480093f4SDimitry Andric // Omit inline line tables if requested. 4213480093f4SDimitry Andric if (Args.hasFlag(options::OPT_gno_inline_line_tables, 4214480093f4SDimitry Andric options::OPT_ginline_line_tables, false)) { 4215480093f4SDimitry Andric CmdArgs.push_back("-gno-inline-line-tables"); 4216480093f4SDimitry Andric } 4217480093f4SDimitry Andric 4218480093f4SDimitry Andric // When emitting remarks, we need at least debug lines in the output. 4219480093f4SDimitry Andric if (willEmitRemarks(Args) && 4220480093f4SDimitry Andric DebugInfoKind <= codegenoptions::DebugDirectivesOnly) 4221480093f4SDimitry Andric DebugInfoKind = codegenoptions::DebugLineTablesOnly; 4222480093f4SDimitry Andric 4223e8d8bef9SDimitry Andric // Adjust the debug info kind for the given toolchain. 4224e8d8bef9SDimitry Andric TC.adjustDebugInfoKind(DebugInfoKind, Args); 4225e8d8bef9SDimitry Andric 4226e8d8bef9SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, EffectiveDWARFVersion, 42270b57cec5SDimitry Andric DebuggerTuning); 42280b57cec5SDimitry Andric 42290b57cec5SDimitry Andric // -fdebug-macro turns on macro debug info generation. 42300b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_macro, options::OPT_fno_debug_macro, 42310b57cec5SDimitry Andric false)) 42320b57cec5SDimitry Andric if (checkDebugInfoOption(Args.getLastArg(options::OPT_fdebug_macro), Args, 42330b57cec5SDimitry Andric D, TC)) 42340b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-macro"); 42350b57cec5SDimitry Andric 42360b57cec5SDimitry Andric // -ggnu-pubnames turns on gnu style pubnames in the backend. 42370b57cec5SDimitry Andric const auto *PubnamesArg = 42380b57cec5SDimitry Andric Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames, 42390b57cec5SDimitry Andric options::OPT_gpubnames, options::OPT_gno_pubnames); 42400b57cec5SDimitry Andric if (DwarfFission != DwarfFissionKind::None || 42410b57cec5SDimitry Andric (PubnamesArg && checkDebugInfoOption(PubnamesArg, Args, D, TC))) 42420b57cec5SDimitry Andric if (!PubnamesArg || 42430b57cec5SDimitry Andric (!PubnamesArg->getOption().matches(options::OPT_gno_gnu_pubnames) && 42440b57cec5SDimitry Andric !PubnamesArg->getOption().matches(options::OPT_gno_pubnames))) 42450b57cec5SDimitry Andric CmdArgs.push_back(PubnamesArg && PubnamesArg->getOption().matches( 42460b57cec5SDimitry Andric options::OPT_gpubnames) 42470b57cec5SDimitry Andric ? "-gpubnames" 42480b57cec5SDimitry Andric : "-ggnu-pubnames"); 4249349cc55cSDimitry Andric const auto *SimpleTemplateNamesArg = 4250349cc55cSDimitry Andric Args.getLastArg(options::OPT_gsimple_template_names, options::OPT_gno_simple_template_names, 4251349cc55cSDimitry Andric options::OPT_gsimple_template_names_EQ); 4252349cc55cSDimitry Andric bool ForwardTemplateParams = DebuggerTuning == llvm::DebuggerKind::SCE; 4253349cc55cSDimitry Andric if (SimpleTemplateNamesArg && 4254349cc55cSDimitry Andric checkDebugInfoOption(SimpleTemplateNamesArg, Args, D, TC)) { 4255349cc55cSDimitry Andric const auto &Opt = SimpleTemplateNamesArg->getOption(); 4256349cc55cSDimitry Andric if (Opt.matches(options::OPT_gsimple_template_names)) { 4257349cc55cSDimitry Andric ForwardTemplateParams = true; 4258349cc55cSDimitry Andric CmdArgs.push_back("-gsimple-template-names=simple"); 4259349cc55cSDimitry Andric } else if (Opt.matches(options::OPT_gsimple_template_names_EQ)) { 4260349cc55cSDimitry Andric ForwardTemplateParams = true; 4261349cc55cSDimitry Andric StringRef Value = SimpleTemplateNamesArg->getValue(); 4262349cc55cSDimitry Andric if (Value == "simple") { 4263349cc55cSDimitry Andric CmdArgs.push_back("-gsimple-template-names=simple"); 4264349cc55cSDimitry Andric } else if (Value == "mangled") { 4265349cc55cSDimitry Andric CmdArgs.push_back("-gsimple-template-names=mangled"); 4266349cc55cSDimitry Andric } else { 4267349cc55cSDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 4268349cc55cSDimitry Andric << Opt.getName() << SimpleTemplateNamesArg->getValue(); 4269349cc55cSDimitry Andric } 4270349cc55cSDimitry Andric } 4271349cc55cSDimitry Andric } 42720b57cec5SDimitry Andric 42730b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_ranges_base_address, 42740b57cec5SDimitry Andric options::OPT_fno_debug_ranges_base_address, false)) { 42750b57cec5SDimitry Andric CmdArgs.push_back("-fdebug-ranges-base-address"); 42760b57cec5SDimitry Andric } 42770b57cec5SDimitry Andric 42780b57cec5SDimitry Andric // -gdwarf-aranges turns on the emission of the aranges section in the 42790b57cec5SDimitry Andric // backend. 42800b57cec5SDimitry Andric // Always enabled for SCE tuning. 42810b57cec5SDimitry Andric bool NeedAranges = DebuggerTuning == llvm::DebuggerKind::SCE; 42820b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gdwarf_aranges)) 42830b57cec5SDimitry Andric NeedAranges = checkDebugInfoOption(A, Args, D, TC) || NeedAranges; 42840b57cec5SDimitry Andric if (NeedAranges) { 42850b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 42860b57cec5SDimitry Andric CmdArgs.push_back("-generate-arange-section"); 42870b57cec5SDimitry Andric } 42880b57cec5SDimitry Andric 4289480093f4SDimitry Andric if (Args.hasFlag(options::OPT_fforce_dwarf_frame, 4290480093f4SDimitry Andric options::OPT_fno_force_dwarf_frame, false)) 4291480093f4SDimitry Andric CmdArgs.push_back("-fforce-dwarf-frame"); 4292480093f4SDimitry Andric 42930b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_types_section, 42940b57cec5SDimitry Andric options::OPT_fno_debug_types_section, false)) { 4295e8d8bef9SDimitry Andric if (!(T.isOSBinFormatELF() || T.isOSBinFormatWasm())) { 42960b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 42970b57cec5SDimitry Andric << Args.getLastArg(options::OPT_fdebug_types_section) 42980b57cec5SDimitry Andric ->getAsString(Args) 42990b57cec5SDimitry Andric << T.getTriple(); 43000b57cec5SDimitry Andric } else if (checkDebugInfoOption( 43010b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdebug_types_section), Args, D, 43020b57cec5SDimitry Andric TC)) { 43030b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 43040b57cec5SDimitry Andric CmdArgs.push_back("-generate-type-units"); 43050b57cec5SDimitry Andric } 43060b57cec5SDimitry Andric } 43070b57cec5SDimitry Andric 4308fe6060f1SDimitry Andric // To avoid join/split of directory+filename, the integrated assembler prefers 4309fe6060f1SDimitry Andric // the directory form of .file on all DWARF versions. GNU as doesn't allow the 4310fe6060f1SDimitry Andric // form before DWARF v5. 4311fe6060f1SDimitry Andric if (!Args.hasFlag(options::OPT_fdwarf_directory_asm, 4312fe6060f1SDimitry Andric options::OPT_fno_dwarf_directory_asm, 4313fe6060f1SDimitry Andric TC.useIntegratedAs() || EffectiveDWARFVersion >= 5)) 4314fe6060f1SDimitry Andric CmdArgs.push_back("-fno-dwarf-directory-asm"); 4315fe6060f1SDimitry Andric 43160b57cec5SDimitry Andric // Decide how to render forward declarations of template instantiations. 43170b57cec5SDimitry Andric // SCE wants full descriptions, others just get them in the name. 4318349cc55cSDimitry Andric if (ForwardTemplateParams) 43190b57cec5SDimitry Andric CmdArgs.push_back("-debug-forward-template-params"); 43200b57cec5SDimitry Andric 43210b57cec5SDimitry Andric // Do we need to explicitly import anonymous namespaces into the parent 43220b57cec5SDimitry Andric // scope? 43230b57cec5SDimitry Andric if (DebuggerTuning == llvm::DebuggerKind::SCE) 43240b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-explicit-import"); 43250b57cec5SDimitry Andric 4326fe6060f1SDimitry Andric renderDwarfFormat(D, T, Args, CmdArgs, EffectiveDWARFVersion); 43270b57cec5SDimitry Andric RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC); 43280b57cec5SDimitry Andric } 43290b57cec5SDimitry Andric 43300b57cec5SDimitry Andric void Clang::ConstructJob(Compilation &C, const JobAction &JA, 43310b57cec5SDimitry Andric const InputInfo &Output, const InputInfoList &Inputs, 43320b57cec5SDimitry Andric const ArgList &Args, const char *LinkingOutput) const { 43330b57cec5SDimitry Andric const auto &TC = getToolChain(); 43340b57cec5SDimitry Andric const llvm::Triple &RawTriple = TC.getTriple(); 43350b57cec5SDimitry Andric const llvm::Triple &Triple = TC.getEffectiveTriple(); 43360b57cec5SDimitry Andric const std::string &TripleStr = Triple.getTriple(); 43370b57cec5SDimitry Andric 43380b57cec5SDimitry Andric bool KernelOrKext = 43390b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext); 43400b57cec5SDimitry Andric const Driver &D = TC.getDriver(); 43410b57cec5SDimitry Andric ArgStringList CmdArgs; 43420b57cec5SDimitry Andric 43430b57cec5SDimitry Andric assert(Inputs.size() >= 1 && "Must have at least one input."); 43440b57cec5SDimitry Andric // CUDA/HIP compilation may have multiple inputs (source file + results of 43450b57cec5SDimitry Andric // device-side compilations). OpenMP device jobs also take the host IR as a 43460b57cec5SDimitry Andric // second input. Module precompilation accepts a list of header files to 43470b57cec5SDimitry Andric // include as part of the module. All other jobs are expected to have exactly 43480b57cec5SDimitry Andric // one input. 43490b57cec5SDimitry Andric bool IsCuda = JA.isOffloading(Action::OFK_Cuda); 4350fe6060f1SDimitry Andric bool IsCudaDevice = JA.isDeviceOffloading(Action::OFK_Cuda); 43510b57cec5SDimitry Andric bool IsHIP = JA.isOffloading(Action::OFK_HIP); 4352fe6060f1SDimitry Andric bool IsHIPDevice = JA.isDeviceOffloading(Action::OFK_HIP); 43530b57cec5SDimitry Andric bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP); 43541fd87a68SDimitry Andric bool IsOpenMPHost = JA.isHostOffloading(Action::OFK_OpenMP); 43550b57cec5SDimitry Andric bool IsHeaderModulePrecompile = isa<HeaderModulePrecompileJobAction>(JA); 4356fe6060f1SDimitry Andric bool IsDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) || 4357fe6060f1SDimitry Andric JA.isDeviceOffloading(Action::OFK_Host)); 4358fe6060f1SDimitry Andric bool IsUsingLTO = D.isUsingLTO(IsDeviceOffloadAction); 4359fe6060f1SDimitry Andric auto LTOMode = D.getLTOMode(IsDeviceOffloadAction); 43600b57cec5SDimitry Andric 43610b57cec5SDimitry Andric // A header module compilation doesn't have a main input file, so invent a 43620b57cec5SDimitry Andric // fake one as a placeholder. 43630b57cec5SDimitry Andric const char *ModuleName = [&]{ 43640b57cec5SDimitry Andric auto *ModuleNameArg = Args.getLastArg(options::OPT_fmodule_name_EQ); 43650b57cec5SDimitry Andric return ModuleNameArg ? ModuleNameArg->getValue() : ""; 43660b57cec5SDimitry Andric }(); 43670b57cec5SDimitry Andric InputInfo HeaderModuleInput(Inputs[0].getType(), ModuleName, ModuleName); 43680b57cec5SDimitry Andric 43690b57cec5SDimitry Andric const InputInfo &Input = 43700b57cec5SDimitry Andric IsHeaderModulePrecompile ? HeaderModuleInput : Inputs[0]; 43710b57cec5SDimitry Andric 43720b57cec5SDimitry Andric InputInfoList ModuleHeaderInputs; 43731fd87a68SDimitry Andric InputInfoList OpenMPHostInputs; 43740b57cec5SDimitry Andric const InputInfo *CudaDeviceInput = nullptr; 43750b57cec5SDimitry Andric const InputInfo *OpenMPDeviceInput = nullptr; 43760b57cec5SDimitry Andric for (const InputInfo &I : Inputs) { 43770b57cec5SDimitry Andric if (&I == &Input) { 43780b57cec5SDimitry Andric // This is the primary input. 43790b57cec5SDimitry Andric } else if (IsHeaderModulePrecompile && 43800b57cec5SDimitry Andric types::getPrecompiledType(I.getType()) == types::TY_PCH) { 43810b57cec5SDimitry Andric types::ID Expected = HeaderModuleInput.getType(); 43820b57cec5SDimitry Andric if (I.getType() != Expected) { 43830b57cec5SDimitry Andric D.Diag(diag::err_drv_module_header_wrong_kind) 43840b57cec5SDimitry Andric << I.getFilename() << types::getTypeName(I.getType()) 43850b57cec5SDimitry Andric << types::getTypeName(Expected); 43860b57cec5SDimitry Andric } 43870b57cec5SDimitry Andric ModuleHeaderInputs.push_back(I); 43880b57cec5SDimitry Andric } else if ((IsCuda || IsHIP) && !CudaDeviceInput) { 43890b57cec5SDimitry Andric CudaDeviceInput = &I; 43900b57cec5SDimitry Andric } else if (IsOpenMPDevice && !OpenMPDeviceInput) { 43910b57cec5SDimitry Andric OpenMPDeviceInput = &I; 43921fd87a68SDimitry Andric } else if (IsOpenMPHost) { 43931fd87a68SDimitry Andric OpenMPHostInputs.push_back(I); 43940b57cec5SDimitry Andric } else { 43950b57cec5SDimitry Andric llvm_unreachable("unexpectedly given multiple inputs"); 43960b57cec5SDimitry Andric } 43970b57cec5SDimitry Andric } 43980b57cec5SDimitry Andric 43995ffd83dbSDimitry Andric const llvm::Triple *AuxTriple = 44005ffd83dbSDimitry Andric (IsCuda || IsHIP) ? TC.getAuxTriple() : nullptr; 44010b57cec5SDimitry Andric bool IsWindowsMSVC = RawTriple.isWindowsMSVCEnvironment(); 44020b57cec5SDimitry Andric bool IsIAMCU = RawTriple.isOSIAMCU(); 44030b57cec5SDimitry Andric 44040b57cec5SDimitry Andric // Adjust IsWindowsXYZ for CUDA/HIP compilations. Even when compiling in 44050b57cec5SDimitry Andric // device mode (i.e., getToolchain().getTriple() is NVPTX/AMDGCN, not 44060b57cec5SDimitry Andric // Windows), we need to pass Windows-specific flags to cc1. 44070b57cec5SDimitry Andric if (IsCuda || IsHIP) 44080b57cec5SDimitry Andric IsWindowsMSVC |= AuxTriple && AuxTriple->isWindowsMSVCEnvironment(); 44090b57cec5SDimitry Andric 44100b57cec5SDimitry Andric // C++ is not supported for IAMCU. 44110b57cec5SDimitry Andric if (IsIAMCU && types::isCXX(Input.getType())) 44120b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU"; 44130b57cec5SDimitry Andric 44140b57cec5SDimitry Andric // Invoke ourselves in -cc1 mode. 44150b57cec5SDimitry Andric // 44160b57cec5SDimitry Andric // FIXME: Implement custom jobs for internal actions. 44170b57cec5SDimitry Andric CmdArgs.push_back("-cc1"); 44180b57cec5SDimitry Andric 44190b57cec5SDimitry Andric // Add the "effective" target triple. 44200b57cec5SDimitry Andric CmdArgs.push_back("-triple"); 44210b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TripleStr)); 44220b57cec5SDimitry Andric 44230b57cec5SDimitry Andric if (const Arg *MJ = Args.getLastArg(options::OPT_MJ)) { 44240b57cec5SDimitry Andric DumpCompilationDatabase(C, MJ->getValue(), TripleStr, Output, Input, Args); 44250b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_MJ); 4426a7dea167SDimitry Andric } else if (const Arg *GenCDBFragment = 4427a7dea167SDimitry Andric Args.getLastArg(options::OPT_gen_cdb_fragment_path)) { 4428a7dea167SDimitry Andric DumpCompilationDatabaseFragmentToDir(GenCDBFragment->getValue(), C, 4429a7dea167SDimitry Andric TripleStr, Output, Input, Args); 4430a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_gen_cdb_fragment_path); 44310b57cec5SDimitry Andric } 44320b57cec5SDimitry Andric 44330b57cec5SDimitry Andric if (IsCuda || IsHIP) { 44340b57cec5SDimitry Andric // We have to pass the triple of the host if compiling for a CUDA/HIP device 44350b57cec5SDimitry Andric // and vice-versa. 44360b57cec5SDimitry Andric std::string NormalizedTriple; 44370b57cec5SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_Cuda) || 44380b57cec5SDimitry Andric JA.isDeviceOffloading(Action::OFK_HIP)) 44390b57cec5SDimitry Andric NormalizedTriple = C.getSingleOffloadToolChain<Action::OFK_Host>() 44400b57cec5SDimitry Andric ->getTriple() 44410b57cec5SDimitry Andric .normalize(); 44420b57cec5SDimitry Andric else { 44430b57cec5SDimitry Andric // Host-side compilation. 44440b57cec5SDimitry Andric NormalizedTriple = 44450b57cec5SDimitry Andric (IsCuda ? C.getSingleOffloadToolChain<Action::OFK_Cuda>() 44460b57cec5SDimitry Andric : C.getSingleOffloadToolChain<Action::OFK_HIP>()) 44470b57cec5SDimitry Andric ->getTriple() 44480b57cec5SDimitry Andric .normalize(); 44490b57cec5SDimitry Andric if (IsCuda) { 44500b57cec5SDimitry Andric // We need to figure out which CUDA version we're compiling for, as that 44510b57cec5SDimitry Andric // determines how we load and launch GPU kernels. 44520b57cec5SDimitry Andric auto *CTC = static_cast<const toolchains::CudaToolChain *>( 44530b57cec5SDimitry Andric C.getSingleOffloadToolChain<Action::OFK_Cuda>()); 44540b57cec5SDimitry Andric assert(CTC && "Expected valid CUDA Toolchain."); 44550b57cec5SDimitry Andric if (CTC && CTC->CudaInstallation.version() != CudaVersion::UNKNOWN) 44560b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 44570b57cec5SDimitry Andric Twine("-target-sdk-version=") + 44580b57cec5SDimitry Andric CudaVersionToString(CTC->CudaInstallation.version()))); 44590b57cec5SDimitry Andric } 44600b57cec5SDimitry Andric } 44610b57cec5SDimitry Andric CmdArgs.push_back("-aux-triple"); 44620b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(NormalizedTriple)); 44630b57cec5SDimitry Andric } 44640b57cec5SDimitry Andric 44655ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) { 44665ffd83dbSDimitry Andric CmdArgs.push_back("-fsycl-is-device"); 44675ffd83dbSDimitry Andric 44685ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_sycl_std_EQ)) { 44695ffd83dbSDimitry Andric A->render(Args, CmdArgs); 44705ffd83dbSDimitry Andric } else { 4471fe6060f1SDimitry Andric // Ensure the default version in SYCL mode is 2020. 4472fe6060f1SDimitry Andric CmdArgs.push_back("-sycl-std=2020"); 44735ffd83dbSDimitry Andric } 44745ffd83dbSDimitry Andric } 44755ffd83dbSDimitry Andric 44760b57cec5SDimitry Andric if (IsOpenMPDevice) { 44770b57cec5SDimitry Andric // We have to pass the triple of the host if compiling for an OpenMP device. 44780b57cec5SDimitry Andric std::string NormalizedTriple = 44790b57cec5SDimitry Andric C.getSingleOffloadToolChain<Action::OFK_Host>() 44800b57cec5SDimitry Andric ->getTriple() 44810b57cec5SDimitry Andric .normalize(); 44820b57cec5SDimitry Andric CmdArgs.push_back("-aux-triple"); 44830b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(NormalizedTriple)); 44840b57cec5SDimitry Andric } 44850b57cec5SDimitry Andric 44860b57cec5SDimitry Andric if (Triple.isOSWindows() && (Triple.getArch() == llvm::Triple::arm || 44870b57cec5SDimitry Andric Triple.getArch() == llvm::Triple::thumb)) { 44880b57cec5SDimitry Andric unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 4 : 6; 44895ffd83dbSDimitry Andric unsigned Version = 0; 44905ffd83dbSDimitry Andric bool Failure = 44915ffd83dbSDimitry Andric Triple.getArchName().substr(Offset).consumeInteger(10, Version); 44925ffd83dbSDimitry Andric if (Failure || Version < 7) 44930b57cec5SDimitry Andric D.Diag(diag::err_target_unsupported_arch) << Triple.getArchName() 44940b57cec5SDimitry Andric << TripleStr; 44950b57cec5SDimitry Andric } 44960b57cec5SDimitry Andric 44970b57cec5SDimitry Andric // Push all default warning arguments that are specific to 44980b57cec5SDimitry Andric // the given target. These come before user provided warning options 44990b57cec5SDimitry Andric // are provided. 45000b57cec5SDimitry Andric TC.addClangWarningOptions(CmdArgs); 45010b57cec5SDimitry Andric 4502fe6060f1SDimitry Andric // FIXME: Subclass ToolChain for SPIR and move this to addClangWarningOptions. 4503349cc55cSDimitry Andric if (Triple.isSPIR() || Triple.isSPIRV()) 4504fe6060f1SDimitry Andric CmdArgs.push_back("-Wspir-compat"); 4505fe6060f1SDimitry Andric 45060b57cec5SDimitry Andric // Select the appropriate action. 45070b57cec5SDimitry Andric RewriteKind rewriteKind = RK_None; 45080b57cec5SDimitry Andric 4509a7dea167SDimitry Andric // If CollectArgsForIntegratedAssembler() isn't called below, claim the args 4510a7dea167SDimitry Andric // it claims when not running an assembler. Otherwise, clang would emit 4511a7dea167SDimitry Andric // "argument unused" warnings for assembler flags when e.g. adding "-E" to 4512a7dea167SDimitry Andric // flags while debugging something. That'd be somewhat inconvenient, and it's 4513a7dea167SDimitry Andric // also inconsistent with most other flags -- we don't warn on 4514a7dea167SDimitry Andric // -ffunction-sections not being used in -E mode either for example, even 4515a7dea167SDimitry Andric // though it's not really used either. 4516a7dea167SDimitry Andric if (!isa<AssembleJobAction>(JA)) { 4517a7dea167SDimitry Andric // The args claimed here should match the args used in 4518a7dea167SDimitry Andric // CollectArgsForIntegratedAssembler(). 4519a7dea167SDimitry Andric if (TC.useIntegratedAs()) { 4520a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mrelax_all); 4521a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mno_relax_all); 4522a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mincremental_linker_compatible); 4523a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mno_incremental_linker_compatible); 4524a7dea167SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 4525a7dea167SDimitry Andric case llvm::Triple::arm: 4526a7dea167SDimitry Andric case llvm::Triple::armeb: 4527a7dea167SDimitry Andric case llvm::Triple::thumb: 4528a7dea167SDimitry Andric case llvm::Triple::thumbeb: 4529a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mimplicit_it_EQ); 4530a7dea167SDimitry Andric break; 4531a7dea167SDimitry Andric default: 4532a7dea167SDimitry Andric break; 4533a7dea167SDimitry Andric } 4534a7dea167SDimitry Andric } 4535a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_Wa_COMMA); 4536a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_Xassembler); 4537a7dea167SDimitry Andric } 4538a7dea167SDimitry Andric 45390b57cec5SDimitry Andric if (isa<AnalyzeJobAction>(JA)) { 45400b57cec5SDimitry Andric assert(JA.getType() == types::TY_Plist && "Invalid output type."); 45410b57cec5SDimitry Andric CmdArgs.push_back("-analyze"); 45420b57cec5SDimitry Andric } else if (isa<MigrateJobAction>(JA)) { 45430b57cec5SDimitry Andric CmdArgs.push_back("-migrate"); 45440b57cec5SDimitry Andric } else if (isa<PreprocessJobAction>(JA)) { 45450b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) 45460b57cec5SDimitry Andric CmdArgs.push_back("-Eonly"); 45470b57cec5SDimitry Andric else { 45480b57cec5SDimitry Andric CmdArgs.push_back("-E"); 45490b57cec5SDimitry Andric if (Args.hasArg(options::OPT_rewrite_objc) && 45500b57cec5SDimitry Andric !Args.hasArg(options::OPT_g_Group)) 45510b57cec5SDimitry Andric CmdArgs.push_back("-P"); 45520b57cec5SDimitry Andric } 45530b57cec5SDimitry Andric } else if (isa<AssembleJobAction>(JA)) { 45540b57cec5SDimitry Andric CmdArgs.push_back("-emit-obj"); 45550b57cec5SDimitry Andric 45560b57cec5SDimitry Andric CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D); 45570b57cec5SDimitry Andric 45580b57cec5SDimitry Andric // Also ignore explicit -force_cpusubtype_ALL option. 45590b57cec5SDimitry Andric (void)Args.hasArg(options::OPT_force__cpusubtype__ALL); 45600b57cec5SDimitry Andric } else if (isa<PrecompileJobAction>(JA)) { 45610b57cec5SDimitry Andric if (JA.getType() == types::TY_Nothing) 45620b57cec5SDimitry Andric CmdArgs.push_back("-fsyntax-only"); 45630b57cec5SDimitry Andric else if (JA.getType() == types::TY_ModuleFile) 45640b57cec5SDimitry Andric CmdArgs.push_back(IsHeaderModulePrecompile 45650b57cec5SDimitry Andric ? "-emit-header-module" 45660b57cec5SDimitry Andric : "-emit-module-interface"); 45670b57cec5SDimitry Andric else 45680b57cec5SDimitry Andric CmdArgs.push_back("-emit-pch"); 45690b57cec5SDimitry Andric } else if (isa<VerifyPCHJobAction>(JA)) { 45700b57cec5SDimitry Andric CmdArgs.push_back("-verify-pch"); 45710b57cec5SDimitry Andric } else { 45720b57cec5SDimitry Andric assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) && 45730b57cec5SDimitry Andric "Invalid action for clang tool."); 45740b57cec5SDimitry Andric if (JA.getType() == types::TY_Nothing) { 45750b57cec5SDimitry Andric CmdArgs.push_back("-fsyntax-only"); 45760b57cec5SDimitry Andric } else if (JA.getType() == types::TY_LLVM_IR || 45770b57cec5SDimitry Andric JA.getType() == types::TY_LTO_IR) { 45780b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm"); 45790b57cec5SDimitry Andric } else if (JA.getType() == types::TY_LLVM_BC || 45800b57cec5SDimitry Andric JA.getType() == types::TY_LTO_BC) { 4581fe6060f1SDimitry Andric // Emit textual llvm IR for AMDGPU offloading for -emit-llvm -S 4582fe6060f1SDimitry Andric if (Triple.isAMDGCN() && IsOpenMPDevice && Args.hasArg(options::OPT_S) && 4583fe6060f1SDimitry Andric Args.hasArg(options::OPT_emit_llvm)) { 4584fe6060f1SDimitry Andric CmdArgs.push_back("-emit-llvm"); 4585fe6060f1SDimitry Andric } else { 45860b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm-bc"); 4587fe6060f1SDimitry Andric } 4588a7dea167SDimitry Andric } else if (JA.getType() == types::TY_IFS || 4589a7dea167SDimitry Andric JA.getType() == types::TY_IFS_CPP) { 4590a7dea167SDimitry Andric StringRef ArgStr = 4591a7dea167SDimitry Andric Args.hasArg(options::OPT_interface_stub_version_EQ) 4592a7dea167SDimitry Andric ? Args.getLastArgValue(options::OPT_interface_stub_version_EQ) 4593fe6060f1SDimitry Andric : "ifs-v1"; 45940b57cec5SDimitry Andric CmdArgs.push_back("-emit-interface-stubs"); 45950b57cec5SDimitry Andric CmdArgs.push_back( 4596a7dea167SDimitry Andric Args.MakeArgString(Twine("-interface-stub-version=") + ArgStr.str())); 45970b57cec5SDimitry Andric } else if (JA.getType() == types::TY_PP_Asm) { 45980b57cec5SDimitry Andric CmdArgs.push_back("-S"); 45990b57cec5SDimitry Andric } else if (JA.getType() == types::TY_AST) { 46000b57cec5SDimitry Andric CmdArgs.push_back("-emit-pch"); 46010b57cec5SDimitry Andric } else if (JA.getType() == types::TY_ModuleFile) { 46020b57cec5SDimitry Andric CmdArgs.push_back("-module-file-info"); 46030b57cec5SDimitry Andric } else if (JA.getType() == types::TY_RewrittenObjC) { 46040b57cec5SDimitry Andric CmdArgs.push_back("-rewrite-objc"); 46050b57cec5SDimitry Andric rewriteKind = RK_NonFragile; 46060b57cec5SDimitry Andric } else if (JA.getType() == types::TY_RewrittenLegacyObjC) { 46070b57cec5SDimitry Andric CmdArgs.push_back("-rewrite-objc"); 46080b57cec5SDimitry Andric rewriteKind = RK_Fragile; 460904eeddc0SDimitry Andric } else if (JA.getType() == types::TY_API_INFO) { 461004eeddc0SDimitry Andric CmdArgs.push_back("-extract-api"); 46110b57cec5SDimitry Andric } else { 46120b57cec5SDimitry Andric assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!"); 46130b57cec5SDimitry Andric } 46140b57cec5SDimitry Andric 46150b57cec5SDimitry Andric // Preserve use-list order by default when emitting bitcode, so that 46160b57cec5SDimitry Andric // loading the bitcode up in 'opt' or 'llc' and running passes gives the 46170b57cec5SDimitry Andric // same result as running passes here. For LTO, we don't need to preserve 46180b57cec5SDimitry Andric // the use-list order, since serialization to bitcode is part of the flow. 46190b57cec5SDimitry Andric if (JA.getType() == types::TY_LLVM_BC) 46200b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm-uselists"); 46210b57cec5SDimitry Andric 46221fd87a68SDimitry Andric if (IsUsingLTO && !Args.hasArg(options::OPT_fopenmp_new_driver)) { 4623349cc55cSDimitry Andric // Only AMDGPU supports device-side LTO. 4624349cc55cSDimitry Andric if (IsDeviceOffloadAction && !Triple.isAMDGPU()) { 4625fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 4626fe6060f1SDimitry Andric << Args.getLastArg(options::OPT_foffload_lto, 4627fe6060f1SDimitry Andric options::OPT_foffload_lto_EQ) 4628fe6060f1SDimitry Andric ->getAsString(Args) 4629fe6060f1SDimitry Andric << Triple.getTriple(); 4630349cc55cSDimitry Andric } else { 4631349cc55cSDimitry Andric assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin); 4632349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 4633349cc55cSDimitry Andric Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full"))); 4634349cc55cSDimitry Andric CmdArgs.push_back("-flto-unit"); 4635fe6060f1SDimitry Andric } 46360b57cec5SDimitry Andric } 46370b57cec5SDimitry Andric } 46380b57cec5SDimitry Andric 46390b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fthinlto_index_EQ)) { 46400b57cec5SDimitry Andric if (!types::isLLVMIR(Input.getType())) 46410b57cec5SDimitry Andric D.Diag(diag::err_drv_arg_requires_bitcode_input) << A->getAsString(Args); 46420b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ); 46430b57cec5SDimitry Andric } 46440b57cec5SDimitry Andric 4645480093f4SDimitry Andric if (Args.getLastArg(options::OPT_fthin_link_bitcode_EQ)) 4646480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fthin_link_bitcode_EQ); 4647480093f4SDimitry Andric 46480b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_save_temps_EQ)) 46490b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_save_temps_EQ); 46500b57cec5SDimitry Andric 4651e8d8bef9SDimitry Andric auto *MemProfArg = Args.getLastArg(options::OPT_fmemory_profile, 4652e8d8bef9SDimitry Andric options::OPT_fmemory_profile_EQ, 4653e8d8bef9SDimitry Andric options::OPT_fno_memory_profile); 4654e8d8bef9SDimitry Andric if (MemProfArg && 4655e8d8bef9SDimitry Andric !MemProfArg->getOption().matches(options::OPT_fno_memory_profile)) 4656e8d8bef9SDimitry Andric MemProfArg->render(Args, CmdArgs); 4657e8d8bef9SDimitry Andric 46580b57cec5SDimitry Andric // Embed-bitcode option. 46590b57cec5SDimitry Andric // Only white-listed flags below are allowed to be embedded. 4660fe6060f1SDimitry Andric if (C.getDriver().embedBitcodeInObject() && !IsUsingLTO && 46610b57cec5SDimitry Andric (isa<BackendJobAction>(JA) || isa<AssembleJobAction>(JA))) { 46620b57cec5SDimitry Andric // Add flags implied by -fembed-bitcode. 46630b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); 46640b57cec5SDimitry Andric // Disable all llvm IR level optimizations. 46650b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-passes"); 46660b57cec5SDimitry Andric 4667480093f4SDimitry Andric // Render target options. 46680b57cec5SDimitry Andric TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind()); 46690b57cec5SDimitry Andric 46700b57cec5SDimitry Andric // reject options that shouldn't be supported in bitcode 46710b57cec5SDimitry Andric // also reject kernel/kext 4672349cc55cSDimitry Andric static const constexpr unsigned kBitcodeOptionIgnorelist[] = { 46730b57cec5SDimitry Andric options::OPT_mkernel, 46740b57cec5SDimitry Andric options::OPT_fapple_kext, 46750b57cec5SDimitry Andric options::OPT_ffunction_sections, 46760b57cec5SDimitry Andric options::OPT_fno_function_sections, 46770b57cec5SDimitry Andric options::OPT_fdata_sections, 46780b57cec5SDimitry Andric options::OPT_fno_data_sections, 46795ffd83dbSDimitry Andric options::OPT_fbasic_block_sections_EQ, 46805ffd83dbSDimitry Andric options::OPT_funique_internal_linkage_names, 46815ffd83dbSDimitry Andric options::OPT_fno_unique_internal_linkage_names, 46820b57cec5SDimitry Andric options::OPT_funique_section_names, 46830b57cec5SDimitry Andric options::OPT_fno_unique_section_names, 46845ffd83dbSDimitry Andric options::OPT_funique_basic_block_section_names, 46855ffd83dbSDimitry Andric options::OPT_fno_unique_basic_block_section_names, 46860b57cec5SDimitry Andric options::OPT_mrestrict_it, 46870b57cec5SDimitry Andric options::OPT_mno_restrict_it, 46880b57cec5SDimitry Andric options::OPT_mstackrealign, 46890b57cec5SDimitry Andric options::OPT_mno_stackrealign, 46900b57cec5SDimitry Andric options::OPT_mstack_alignment, 46910b57cec5SDimitry Andric options::OPT_mcmodel_EQ, 46920b57cec5SDimitry Andric options::OPT_mlong_calls, 46930b57cec5SDimitry Andric options::OPT_mno_long_calls, 46940b57cec5SDimitry Andric options::OPT_ggnu_pubnames, 46950b57cec5SDimitry Andric options::OPT_gdwarf_aranges, 46960b57cec5SDimitry Andric options::OPT_fdebug_types_section, 46970b57cec5SDimitry Andric options::OPT_fno_debug_types_section, 46980b57cec5SDimitry Andric options::OPT_fdwarf_directory_asm, 46990b57cec5SDimitry Andric options::OPT_fno_dwarf_directory_asm, 47000b57cec5SDimitry Andric options::OPT_mrelax_all, 47010b57cec5SDimitry Andric options::OPT_mno_relax_all, 47020b57cec5SDimitry Andric options::OPT_ftrap_function_EQ, 47030b57cec5SDimitry Andric options::OPT_ffixed_r9, 47040b57cec5SDimitry Andric options::OPT_mfix_cortex_a53_835769, 47050b57cec5SDimitry Andric options::OPT_mno_fix_cortex_a53_835769, 47060b57cec5SDimitry Andric options::OPT_ffixed_x18, 47070b57cec5SDimitry Andric options::OPT_mglobal_merge, 47080b57cec5SDimitry Andric options::OPT_mno_global_merge, 47090b57cec5SDimitry Andric options::OPT_mred_zone, 47100b57cec5SDimitry Andric options::OPT_mno_red_zone, 47110b57cec5SDimitry Andric options::OPT_Wa_COMMA, 47120b57cec5SDimitry Andric options::OPT_Xassembler, 47130b57cec5SDimitry Andric options::OPT_mllvm, 47140b57cec5SDimitry Andric }; 47150b57cec5SDimitry Andric for (const auto &A : Args) 4716349cc55cSDimitry Andric if (llvm::is_contained(kBitcodeOptionIgnorelist, A->getOption().getID())) 47170b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_embed_bitcode) << A->getSpelling(); 47180b57cec5SDimitry Andric 47190b57cec5SDimitry Andric // Render the CodeGen options that need to be passed. 47200b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_foptimize_sibling_calls, 47210b57cec5SDimitry Andric options::OPT_fno_optimize_sibling_calls)) 47220b57cec5SDimitry Andric CmdArgs.push_back("-mdisable-tail-calls"); 47230b57cec5SDimitry Andric 47240b57cec5SDimitry Andric RenderFloatingPointOptions(TC, D, isOptimizationLevelFast(Args), Args, 47255ffd83dbSDimitry Andric CmdArgs, JA); 47260b57cec5SDimitry Andric 47270b57cec5SDimitry Andric // Render ABI arguments 47280b57cec5SDimitry Andric switch (TC.getArch()) { 47290b57cec5SDimitry Andric default: break; 47300b57cec5SDimitry Andric case llvm::Triple::arm: 47310b57cec5SDimitry Andric case llvm::Triple::armeb: 47320b57cec5SDimitry Andric case llvm::Triple::thumbeb: 4733349cc55cSDimitry Andric RenderARMABI(D, Triple, Args, CmdArgs); 47340b57cec5SDimitry Andric break; 47350b57cec5SDimitry Andric case llvm::Triple::aarch64: 4736480093f4SDimitry Andric case llvm::Triple::aarch64_32: 47370b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 47380b57cec5SDimitry Andric RenderAArch64ABI(Triple, Args, CmdArgs); 47390b57cec5SDimitry Andric break; 47400b57cec5SDimitry Andric } 47410b57cec5SDimitry Andric 47420b57cec5SDimitry Andric // Optimization level for CodeGen. 47430b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { 47440b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4)) { 47450b57cec5SDimitry Andric CmdArgs.push_back("-O3"); 47460b57cec5SDimitry Andric D.Diag(diag::warn_O4_is_O3); 47470b57cec5SDimitry Andric } else { 47480b57cec5SDimitry Andric A->render(Args, CmdArgs); 47490b57cec5SDimitry Andric } 47500b57cec5SDimitry Andric } 47510b57cec5SDimitry Andric 47520b57cec5SDimitry Andric // Input/Output file. 47530b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) { 47540b57cec5SDimitry Andric // Handled with other dependency code. 47550b57cec5SDimitry Andric } else if (Output.isFilename()) { 47560b57cec5SDimitry Andric CmdArgs.push_back("-o"); 47570b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 47580b57cec5SDimitry Andric } else { 47590b57cec5SDimitry Andric assert(Output.isNothing() && "Input output."); 47600b57cec5SDimitry Andric } 47610b57cec5SDimitry Andric 47620b57cec5SDimitry Andric for (const auto &II : Inputs) { 47630b57cec5SDimitry Andric addDashXForInput(Args, II, CmdArgs); 47640b57cec5SDimitry Andric if (II.isFilename()) 47650b57cec5SDimitry Andric CmdArgs.push_back(II.getFilename()); 47660b57cec5SDimitry Andric else 47670b57cec5SDimitry Andric II.getInputArg().renderAsInput(Args, CmdArgs); 47680b57cec5SDimitry Andric } 47690b57cec5SDimitry Andric 4770e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>( 4771e8d8bef9SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), D.getClangProgramPath(), 4772e8d8bef9SDimitry Andric CmdArgs, Inputs, Output)); 47730b57cec5SDimitry Andric return; 47740b57cec5SDimitry Andric } 47750b57cec5SDimitry Andric 4776fe6060f1SDimitry Andric if (C.getDriver().embedBitcodeMarkerOnly() && !IsUsingLTO) 47770b57cec5SDimitry Andric CmdArgs.push_back("-fembed-bitcode=marker"); 47780b57cec5SDimitry Andric 47790b57cec5SDimitry Andric // We normally speed up the clang process a bit by skipping destructors at 47800b57cec5SDimitry Andric // exit, but when we're generating diagnostics we can rely on some of the 47810b57cec5SDimitry Andric // cleanup. 47820b57cec5SDimitry Andric if (!C.isForDiagnostics()) 47830b57cec5SDimitry Andric CmdArgs.push_back("-disable-free"); 4784349cc55cSDimitry Andric CmdArgs.push_back("-clear-ast-before-backend"); 47850b57cec5SDimitry Andric 47860b57cec5SDimitry Andric #ifdef NDEBUG 47870b57cec5SDimitry Andric const bool IsAssertBuild = false; 47880b57cec5SDimitry Andric #else 47890b57cec5SDimitry Andric const bool IsAssertBuild = true; 47900b57cec5SDimitry Andric #endif 47910b57cec5SDimitry Andric 47920b57cec5SDimitry Andric // Disable the verification pass in -asserts builds. 47930b57cec5SDimitry Andric if (!IsAssertBuild) 47940b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-verifier"); 47950b57cec5SDimitry Andric 47960b57cec5SDimitry Andric // Discard value names in assert builds unless otherwise specified. 47970b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdiscard_value_names, 479847395794SDimitry Andric options::OPT_fno_discard_value_names, !IsAssertBuild)) { 479947395794SDimitry Andric if (Args.hasArg(options::OPT_fdiscard_value_names) && 4800349cc55cSDimitry Andric llvm::any_of(Inputs, [](const clang::driver::InputInfo &II) { 480147395794SDimitry Andric return types::isLLVMIR(II.getType()); 4802349cc55cSDimitry Andric })) { 480347395794SDimitry Andric D.Diag(diag::warn_ignoring_fdiscard_for_bitcode); 480447395794SDimitry Andric } 48050b57cec5SDimitry Andric CmdArgs.push_back("-discard-value-names"); 480647395794SDimitry Andric } 48070b57cec5SDimitry Andric 48080b57cec5SDimitry Andric // Set the main file name, so that debug info works even with 48090b57cec5SDimitry Andric // -save-temps. 48100b57cec5SDimitry Andric CmdArgs.push_back("-main-file-name"); 48110b57cec5SDimitry Andric CmdArgs.push_back(getBaseInputName(Args, Input)); 48120b57cec5SDimitry Andric 48130b57cec5SDimitry Andric // Some flags which affect the language (via preprocessor 48140b57cec5SDimitry Andric // defines). 48150b57cec5SDimitry Andric if (Args.hasArg(options::OPT_static)) 48160b57cec5SDimitry Andric CmdArgs.push_back("-static-define"); 48170b57cec5SDimitry Andric 48180b57cec5SDimitry Andric if (Args.hasArg(options::OPT_municode)) 48190b57cec5SDimitry Andric CmdArgs.push_back("-DUNICODE"); 48200b57cec5SDimitry Andric 48210b57cec5SDimitry Andric if (isa<AnalyzeJobAction>(JA)) 48220b57cec5SDimitry Andric RenderAnalyzerOptions(Args, CmdArgs, Triple, Input); 48230b57cec5SDimitry Andric 4824a7dea167SDimitry Andric if (isa<AnalyzeJobAction>(JA) || 4825a7dea167SDimitry Andric (isa<PreprocessJobAction>(JA) && Args.hasArg(options::OPT__analyze))) 4826a7dea167SDimitry Andric CmdArgs.push_back("-setup-static-analyzer"); 4827a7dea167SDimitry Andric 48280b57cec5SDimitry Andric // Enable compatilibily mode to avoid analyzer-config related errors. 48290b57cec5SDimitry Andric // Since we can't access frontend flags through hasArg, let's manually iterate 48300b57cec5SDimitry Andric // through them. 48310b57cec5SDimitry Andric bool FoundAnalyzerConfig = false; 48320b57cec5SDimitry Andric for (auto Arg : Args.filtered(options::OPT_Xclang)) 48330b57cec5SDimitry Andric if (StringRef(Arg->getValue()) == "-analyzer-config") { 48340b57cec5SDimitry Andric FoundAnalyzerConfig = true; 48350b57cec5SDimitry Andric break; 48360b57cec5SDimitry Andric } 48370b57cec5SDimitry Andric if (!FoundAnalyzerConfig) 48380b57cec5SDimitry Andric for (auto Arg : Args.filtered(options::OPT_Xanalyzer)) 48390b57cec5SDimitry Andric if (StringRef(Arg->getValue()) == "-analyzer-config") { 48400b57cec5SDimitry Andric FoundAnalyzerConfig = true; 48410b57cec5SDimitry Andric break; 48420b57cec5SDimitry Andric } 48430b57cec5SDimitry Andric if (FoundAnalyzerConfig) 48440b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-config-compatibility-mode=true"); 48450b57cec5SDimitry Andric 48460b57cec5SDimitry Andric CheckCodeGenerationOptions(D, Args); 48470b57cec5SDimitry Andric 48480b57cec5SDimitry Andric unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args); 48490b57cec5SDimitry Andric assert(FunctionAlignment <= 31 && "function alignment will be truncated!"); 48500b57cec5SDimitry Andric if (FunctionAlignment) { 48510b57cec5SDimitry Andric CmdArgs.push_back("-function-alignment"); 48520b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment))); 48530b57cec5SDimitry Andric } 48540b57cec5SDimitry Andric 4855349cc55cSDimitry Andric // We support -falign-loops=N where N is a power of 2. GCC supports more 4856349cc55cSDimitry Andric // forms. 4857349cc55cSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_falign_loops_EQ)) { 4858349cc55cSDimitry Andric unsigned Value = 0; 4859349cc55cSDimitry Andric if (StringRef(A->getValue()).getAsInteger(10, Value) || Value > 65536) 4860349cc55cSDimitry Andric TC.getDriver().Diag(diag::err_drv_invalid_int_value) 4861349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 4862349cc55cSDimitry Andric else if (Value & (Value - 1)) 4863349cc55cSDimitry Andric TC.getDriver().Diag(diag::err_drv_alignment_not_power_of_two) 4864349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 4865349cc55cSDimitry Andric // Treat =0 as unspecified (use the target preference). 4866349cc55cSDimitry Andric if (Value) 4867349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-falign-loops=" + 4868349cc55cSDimitry Andric Twine(std::min(Value, 65536u)))); 4869349cc55cSDimitry Andric } 4870349cc55cSDimitry Andric 48710b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 48720b57cec5SDimitry Andric unsigned PICLevel; 48730b57cec5SDimitry Andric bool IsPIE; 48740b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(TC, Args); 48750b57cec5SDimitry Andric 48765ffd83dbSDimitry Andric bool IsROPI = RelocationModel == llvm::Reloc::ROPI || 48775ffd83dbSDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI; 48785ffd83dbSDimitry Andric bool IsRWPI = RelocationModel == llvm::Reloc::RWPI || 48795ffd83dbSDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI; 48800b57cec5SDimitry Andric 48815ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_mcmse) && 48825ffd83dbSDimitry Andric !Args.hasArg(options::OPT_fallow_unsupported)) { 48835ffd83dbSDimitry Andric if (IsROPI) 48845ffd83dbSDimitry Andric D.Diag(diag::err_cmse_pi_are_incompatible) << IsROPI; 48855ffd83dbSDimitry Andric if (IsRWPI) 48865ffd83dbSDimitry Andric D.Diag(diag::err_cmse_pi_are_incompatible) << !IsRWPI; 48875ffd83dbSDimitry Andric } 48885ffd83dbSDimitry Andric 48895ffd83dbSDimitry Andric if (IsROPI && types::isCXX(Input.getType()) && 48900b57cec5SDimitry Andric !Args.hasArg(options::OPT_fallow_unsupported)) 48910b57cec5SDimitry Andric D.Diag(diag::err_drv_ropi_incompatible_with_cxx); 48920b57cec5SDimitry Andric 48935ffd83dbSDimitry Andric const char *RMName = RelocationModelName(RelocationModel); 48940b57cec5SDimitry Andric if (RMName) { 48950b57cec5SDimitry Andric CmdArgs.push_back("-mrelocation-model"); 48960b57cec5SDimitry Andric CmdArgs.push_back(RMName); 48970b57cec5SDimitry Andric } 48980b57cec5SDimitry Andric if (PICLevel > 0) { 48990b57cec5SDimitry Andric CmdArgs.push_back("-pic-level"); 49000b57cec5SDimitry Andric CmdArgs.push_back(PICLevel == 1 ? "1" : "2"); 49010b57cec5SDimitry Andric if (IsPIE) 49020b57cec5SDimitry Andric CmdArgs.push_back("-pic-is-pie"); 49030b57cec5SDimitry Andric } 49040b57cec5SDimitry Andric 49050b57cec5SDimitry Andric if (RelocationModel == llvm::Reloc::ROPI || 49060b57cec5SDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI) 49070b57cec5SDimitry Andric CmdArgs.push_back("-fropi"); 49080b57cec5SDimitry Andric if (RelocationModel == llvm::Reloc::RWPI || 49090b57cec5SDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI) 49100b57cec5SDimitry Andric CmdArgs.push_back("-frwpi"); 49110b57cec5SDimitry Andric 49120b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_meabi)) { 49130b57cec5SDimitry Andric CmdArgs.push_back("-meabi"); 49140b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 49150b57cec5SDimitry Andric } 49160b57cec5SDimitry Andric 4917e8d8bef9SDimitry Andric // -fsemantic-interposition is forwarded to CC1: set the 4918e8d8bef9SDimitry Andric // "SemanticInterposition" metadata to 1 (make some linkages interposable) and 4919e8d8bef9SDimitry Andric // make default visibility external linkage definitions dso_preemptable. 4920e8d8bef9SDimitry Andric // 4921e8d8bef9SDimitry Andric // -fno-semantic-interposition: if the target supports .Lfoo$local local 4922e8d8bef9SDimitry Andric // aliases (make default visibility external linkage definitions dso_local). 4923e8d8bef9SDimitry Andric // This is the CC1 default for ELF to match COFF/Mach-O. 4924e8d8bef9SDimitry Andric // 4925e8d8bef9SDimitry Andric // Otherwise use Clang's traditional behavior: like 4926e8d8bef9SDimitry Andric // -fno-semantic-interposition but local aliases are not used. So references 4927e8d8bef9SDimitry Andric // can be interposed if not optimized out. 4928e8d8bef9SDimitry Andric if (Triple.isOSBinFormatELF()) { 4929e8d8bef9SDimitry Andric Arg *A = Args.getLastArg(options::OPT_fsemantic_interposition, 4930e8d8bef9SDimitry Andric options::OPT_fno_semantic_interposition); 4931e8d8bef9SDimitry Andric if (RelocationModel != llvm::Reloc::Static && !IsPIE) { 4932e8d8bef9SDimitry Andric // The supported targets need to call AsmPrinter::getSymbolPreferLocal. 4933fe6060f1SDimitry Andric bool SupportsLocalAlias = 4934fe6060f1SDimitry Andric Triple.isAArch64() || Triple.isRISCV() || Triple.isX86(); 4935e8d8bef9SDimitry Andric if (!A) 4936e8d8bef9SDimitry Andric CmdArgs.push_back("-fhalf-no-semantic-interposition"); 4937e8d8bef9SDimitry Andric else if (A->getOption().matches(options::OPT_fsemantic_interposition)) 49385ffd83dbSDimitry Andric A->render(Args, CmdArgs); 4939e8d8bef9SDimitry Andric else if (!SupportsLocalAlias) 4940e8d8bef9SDimitry Andric CmdArgs.push_back("-fhalf-no-semantic-interposition"); 4941e8d8bef9SDimitry Andric } 4942e8d8bef9SDimitry Andric } 49435ffd83dbSDimitry Andric 49445ffd83dbSDimitry Andric { 49455ffd83dbSDimitry Andric std::string Model; 49460b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) { 49470b57cec5SDimitry Andric if (!TC.isThreadModelSupported(A->getValue())) 49480b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_thread_model_for_target) 49490b57cec5SDimitry Andric << A->getValue() << A->getAsString(Args); 49505ffd83dbSDimitry Andric Model = A->getValue(); 49515ffd83dbSDimitry Andric } else 49525ffd83dbSDimitry Andric Model = TC.getThreadModel(); 49535ffd83dbSDimitry Andric if (Model != "posix") { 49545ffd83dbSDimitry Andric CmdArgs.push_back("-mthread-model"); 49555ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Model)); 49560b57cec5SDimitry Andric } 49575ffd83dbSDimitry Andric } 49580b57cec5SDimitry Andric 49590b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fveclib); 49600b57cec5SDimitry Andric 49610b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmerge_all_constants, 49620b57cec5SDimitry Andric options::OPT_fno_merge_all_constants, false)) 49630b57cec5SDimitry Andric CmdArgs.push_back("-fmerge-all-constants"); 49640b57cec5SDimitry Andric 49650b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_delete_null_pointer_checks, 49660b57cec5SDimitry Andric options::OPT_fdelete_null_pointer_checks, false)) 49670b57cec5SDimitry Andric CmdArgs.push_back("-fno-delete-null-pointer-checks"); 49680b57cec5SDimitry Andric 49690b57cec5SDimitry Andric // LLVM Code Generator Options. 49700b57cec5SDimitry Andric 4971fe6060f1SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_frewrite_map_file_EQ)) { 49720b57cec5SDimitry Andric StringRef Map = A->getValue(); 49730b57cec5SDimitry Andric if (!llvm::sys::fs::exists(Map)) { 49740b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << Map; 49750b57cec5SDimitry Andric } else { 4976fe6060f1SDimitry Andric A->render(Args, CmdArgs); 49770b57cec5SDimitry Andric A->claim(); 49780b57cec5SDimitry Andric } 49790b57cec5SDimitry Andric } 49800b57cec5SDimitry Andric 4981e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ_vec_extabi, 4982e8d8bef9SDimitry Andric options::OPT_mabi_EQ_vec_default)) { 4983e8d8bef9SDimitry Andric if (!Triple.isOSAIX()) 4984e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 4985e8d8bef9SDimitry Andric << A->getSpelling() << RawTriple.str(); 4986d409305fSDimitry Andric if (A->getOption().getID() == options::OPT_mabi_EQ_vec_extabi) 4987d409305fSDimitry Andric CmdArgs.push_back("-mabi=vec-extabi"); 4988d409305fSDimitry Andric else 4989fe6060f1SDimitry Andric CmdArgs.push_back("-mabi=vec-default"); 4990fe6060f1SDimitry Andric } 4991fe6060f1SDimitry Andric 4992fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mlong_double_128)) { 4993fe6060f1SDimitry Andric // Emit the unsupported option error until the Clang's library integration 4994fe6060f1SDimitry Andric // support for 128-bit long double is available for AIX. 4995fe6060f1SDimitry Andric if (Triple.isOSAIX()) 4996fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 4997fe6060f1SDimitry Andric << A->getSpelling() << RawTriple.str(); 4998e8d8bef9SDimitry Andric } 4999e8d8bef9SDimitry Andric 50000b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) { 50010b57cec5SDimitry Andric StringRef v = A->getValue(); 5002fe6060f1SDimitry Andric // FIXME: Validate the argument here so we don't produce meaningless errors 5003fe6060f1SDimitry Andric // about -fwarn-stack-size=. 5004fe6060f1SDimitry Andric if (v.empty()) 5005fe6060f1SDimitry Andric D.Diag(diag::err_drv_missing_argument) << A->getSpelling() << 1; 5006fe6060f1SDimitry Andric else 5007fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fwarn-stack-size=" + v)); 50080b57cec5SDimitry Andric A->claim(); 50090b57cec5SDimitry Andric } 50100b57cec5SDimitry Andric 50110b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fjump_tables, options::OPT_fno_jump_tables, 50120b57cec5SDimitry Andric true)) 50130b57cec5SDimitry Andric CmdArgs.push_back("-fno-jump-tables"); 50140b57cec5SDimitry Andric 50150b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fprofile_sample_accurate, 50160b57cec5SDimitry Andric options::OPT_fno_profile_sample_accurate, false)) 50170b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-sample-accurate"); 50180b57cec5SDimitry Andric 50190b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fpreserve_as_comments, 50200b57cec5SDimitry Andric options::OPT_fno_preserve_as_comments, true)) 50210b57cec5SDimitry Andric CmdArgs.push_back("-fno-preserve-as-comments"); 50220b57cec5SDimitry Andric 50230b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { 50240b57cec5SDimitry Andric CmdArgs.push_back("-mregparm"); 50250b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 50260b57cec5SDimitry Andric } 50270b57cec5SDimitry Andric 5028e86cf8adSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_maix_struct_return, 5029e86cf8adSDimitry Andric options::OPT_msvr4_struct_return)) { 5030e8d8bef9SDimitry Andric if (!TC.getTriple().isPPC32()) { 5031e86cf8adSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5032e86cf8adSDimitry Andric << A->getSpelling() << RawTriple.str(); 5033e86cf8adSDimitry Andric } else if (A->getOption().matches(options::OPT_maix_struct_return)) { 5034e86cf8adSDimitry Andric CmdArgs.push_back("-maix-struct-return"); 5035e86cf8adSDimitry Andric } else { 5036e86cf8adSDimitry Andric assert(A->getOption().matches(options::OPT_msvr4_struct_return)); 5037e86cf8adSDimitry Andric CmdArgs.push_back("-msvr4-struct-return"); 5038e86cf8adSDimitry Andric } 5039e86cf8adSDimitry Andric } 5040e86cf8adSDimitry Andric 50410b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return, 50420b57cec5SDimitry Andric options::OPT_freg_struct_return)) { 50430b57cec5SDimitry Andric if (TC.getArch() != llvm::Triple::x86) { 50440b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 50450b57cec5SDimitry Andric << A->getSpelling() << RawTriple.str(); 50460b57cec5SDimitry Andric } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) { 50470b57cec5SDimitry Andric CmdArgs.push_back("-fpcc-struct-return"); 50480b57cec5SDimitry Andric } else { 50490b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_freg_struct_return)); 50500b57cec5SDimitry Andric CmdArgs.push_back("-freg-struct-return"); 50510b57cec5SDimitry Andric } 50520b57cec5SDimitry Andric } 50530b57cec5SDimitry Andric 50540b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false)) 50550b57cec5SDimitry Andric CmdArgs.push_back("-fdefault-calling-conv=stdcall"); 50560b57cec5SDimitry Andric 50575ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_fenable_matrix)) { 50585ffd83dbSDimitry Andric // enable-matrix is needed by both the LangOpts and by LLVM. 50595ffd83dbSDimitry Andric CmdArgs.push_back("-fenable-matrix"); 50605ffd83dbSDimitry Andric CmdArgs.push_back("-mllvm"); 50615ffd83dbSDimitry Andric CmdArgs.push_back("-enable-matrix"); 50625ffd83dbSDimitry Andric } 50635ffd83dbSDimitry Andric 5064a7dea167SDimitry Andric CodeGenOptions::FramePointerKind FPKeepKind = 5065a7dea167SDimitry Andric getFramePointerKind(Args, RawTriple); 5066a7dea167SDimitry Andric const char *FPKeepKindStr = nullptr; 5067a7dea167SDimitry Andric switch (FPKeepKind) { 5068a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::None: 5069a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=none"; 5070a7dea167SDimitry Andric break; 5071a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::NonLeaf: 5072a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=non-leaf"; 5073a7dea167SDimitry Andric break; 5074a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::All: 5075a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=all"; 5076a7dea167SDimitry Andric break; 5077a7dea167SDimitry Andric } 5078a7dea167SDimitry Andric assert(FPKeepKindStr && "unknown FramePointerKind"); 5079a7dea167SDimitry Andric CmdArgs.push_back(FPKeepKindStr); 5080a7dea167SDimitry Andric 50810b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss, 50825ffd83dbSDimitry Andric options::OPT_fno_zero_initialized_in_bss, true)) 50835ffd83dbSDimitry Andric CmdArgs.push_back("-fno-zero-initialized-in-bss"); 50840b57cec5SDimitry Andric 50850b57cec5SDimitry Andric bool OFastEnabled = isOptimizationLevelFast(Args); 50860b57cec5SDimitry Andric // If -Ofast is the optimization level, then -fstrict-aliasing should be 50870b57cec5SDimitry Andric // enabled. This alias option is being used to simplify the hasFlag logic. 50880b57cec5SDimitry Andric OptSpecifier StrictAliasingAliasOption = 50890b57cec5SDimitry Andric OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing; 50900b57cec5SDimitry Andric // We turn strict aliasing off by default if we're in CL mode, since MSVC 50910b57cec5SDimitry Andric // doesn't do any TBAA. 50920b57cec5SDimitry Andric bool TBAAOnByDefault = !D.IsCLMode(); 50930b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption, 50940b57cec5SDimitry Andric options::OPT_fno_strict_aliasing, TBAAOnByDefault)) 50950b57cec5SDimitry Andric CmdArgs.push_back("-relaxed-aliasing"); 50960b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fstruct_path_tbaa, 50970b57cec5SDimitry Andric options::OPT_fno_struct_path_tbaa)) 50980b57cec5SDimitry Andric CmdArgs.push_back("-no-struct-path-tbaa"); 50990b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums, 51000b57cec5SDimitry Andric false)) 51010b57cec5SDimitry Andric CmdArgs.push_back("-fstrict-enums"); 51020b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fstrict_return, options::OPT_fno_strict_return, 51030b57cec5SDimitry Andric true)) 51040b57cec5SDimitry Andric CmdArgs.push_back("-fno-strict-return"); 51050b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fallow_editor_placeholders, 51060b57cec5SDimitry Andric options::OPT_fno_allow_editor_placeholders, false)) 51070b57cec5SDimitry Andric CmdArgs.push_back("-fallow-editor-placeholders"); 51080b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fstrict_vtable_pointers, 51090b57cec5SDimitry Andric options::OPT_fno_strict_vtable_pointers, 51100b57cec5SDimitry Andric false)) 51110b57cec5SDimitry Andric CmdArgs.push_back("-fstrict-vtable-pointers"); 51120b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fforce_emit_vtables, 51130b57cec5SDimitry Andric options::OPT_fno_force_emit_vtables, 51140b57cec5SDimitry Andric false)) 51150b57cec5SDimitry Andric CmdArgs.push_back("-fforce-emit-vtables"); 51160b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_foptimize_sibling_calls, 51170b57cec5SDimitry Andric options::OPT_fno_optimize_sibling_calls)) 51180b57cec5SDimitry Andric CmdArgs.push_back("-mdisable-tail-calls"); 51190b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_escaping_block_tail_calls, 51200b57cec5SDimitry Andric options::OPT_fescaping_block_tail_calls, false)) 51210b57cec5SDimitry Andric CmdArgs.push_back("-fno-escaping-block-tail-calls"); 51220b57cec5SDimitry Andric 51230b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffine_grained_bitfield_accesses, 51240b57cec5SDimitry Andric options::OPT_fno_fine_grained_bitfield_accesses); 51250b57cec5SDimitry Andric 5126fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, 5127fe6060f1SDimitry Andric options::OPT_fno_experimental_relative_cxx_abi_vtables); 5128fe6060f1SDimitry Andric 51290b57cec5SDimitry Andric // Handle segmented stacks. 5130fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fsplit_stack, options::OPT_fno_split_stack, 5131fe6060f1SDimitry Andric false)) 5132fe6060f1SDimitry Andric CmdArgs.push_back("-fsplit-stack"); 5133fe6060f1SDimitry Andric 5134fe6060f1SDimitry Andric // -fprotect-parens=0 is default. 5135fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fprotect_parens, 5136fe6060f1SDimitry Andric options::OPT_fno_protect_parens, false)) 5137fe6060f1SDimitry Andric CmdArgs.push_back("-fprotect-parens"); 51380b57cec5SDimitry Andric 51395ffd83dbSDimitry Andric RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs, JA); 51405ffd83dbSDimitry Andric 5141fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fextend_args_EQ)) { 5142fe6060f1SDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 5143fe6060f1SDimitry Andric if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) { 5144fe6060f1SDimitry Andric StringRef V = A->getValue(); 5145fe6060f1SDimitry Andric if (V == "64") 5146fe6060f1SDimitry Andric CmdArgs.push_back("-fextend-arguments=64"); 5147fe6060f1SDimitry Andric else if (V != "32") 5148fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 5149fe6060f1SDimitry Andric << A->getValue() << A->getOption().getName(); 5150fe6060f1SDimitry Andric } else 5151fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5152fe6060f1SDimitry Andric << A->getOption().getName() << TripleStr; 5153fe6060f1SDimitry Andric } 5154fe6060f1SDimitry Andric 51555ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mdouble_EQ)) { 51565ffd83dbSDimitry Andric if (TC.getArch() == llvm::Triple::avr) 51575ffd83dbSDimitry Andric A->render(Args, CmdArgs); 51585ffd83dbSDimitry Andric else 51595ffd83dbSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 51605ffd83dbSDimitry Andric << A->getAsString(Args) << TripleStr; 51615ffd83dbSDimitry Andric } 51620b57cec5SDimitry Andric 5163a7dea167SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_LongDouble_Group)) { 5164480093f4SDimitry Andric if (TC.getTriple().isX86()) 5165a7dea167SDimitry Andric A->render(Args, CmdArgs); 5166e8d8bef9SDimitry Andric else if (TC.getTriple().isPPC() && 5167a7dea167SDimitry Andric (A->getOption().getID() != options::OPT_mlong_double_80)) 51680b57cec5SDimitry Andric A->render(Args, CmdArgs); 51690b57cec5SDimitry Andric else 51700b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 51710b57cec5SDimitry Andric << A->getAsString(Args) << TripleStr; 51720b57cec5SDimitry Andric } 51730b57cec5SDimitry Andric 51740b57cec5SDimitry Andric // Decide whether to use verbose asm. Verbose assembly is the default on 51750b57cec5SDimitry Andric // toolchains which have the integrated assembler on by default. 51760b57cec5SDimitry Andric bool IsIntegratedAssemblerDefault = TC.IsIntegratedAssemblerDefault(); 51775ffd83dbSDimitry Andric if (!Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm, 5178a7dea167SDimitry Andric IsIntegratedAssemblerDefault)) 51795ffd83dbSDimitry Andric CmdArgs.push_back("-fno-verbose-asm"); 51800b57cec5SDimitry Andric 5181e8d8bef9SDimitry Andric // Parse 'none' or '$major.$minor'. Disallow -fbinutils-version=0 because we 5182e8d8bef9SDimitry Andric // use that to indicate the MC default in the backend. 5183e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbinutils_version_EQ)) { 5184e8d8bef9SDimitry Andric StringRef V = A->getValue(); 5185e8d8bef9SDimitry Andric unsigned Num; 5186e8d8bef9SDimitry Andric if (V == "none") 5187e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5188e8d8bef9SDimitry Andric else if (!V.consumeInteger(10, Num) && Num > 0 && 5189e8d8bef9SDimitry Andric (V.empty() || (V.consume_front(".") && 5190e8d8bef9SDimitry Andric !V.consumeInteger(10, Num) && V.empty()))) 5191e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5192e8d8bef9SDimitry Andric else 5193e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 5194e8d8bef9SDimitry Andric << A->getValue() << A->getOption().getName(); 5195e8d8bef9SDimitry Andric } 5196e8d8bef9SDimitry Andric 5197fe6060f1SDimitry Andric // If toolchain choose to use MCAsmParser for inline asm don't pass the 5198fe6060f1SDimitry Andric // option to disable integrated-as explictly. 5199fe6060f1SDimitry Andric if (!TC.useIntegratedAs() && !TC.parseInlineAsmUsingAsmParser()) 52000b57cec5SDimitry Andric CmdArgs.push_back("-no-integrated-as"); 52010b57cec5SDimitry Andric 52020b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdebug_pass_structure)) { 52030b57cec5SDimitry Andric CmdArgs.push_back("-mdebug-pass"); 52040b57cec5SDimitry Andric CmdArgs.push_back("Structure"); 52050b57cec5SDimitry Andric } 52060b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdebug_pass_arguments)) { 52070b57cec5SDimitry Andric CmdArgs.push_back("-mdebug-pass"); 52080b57cec5SDimitry Andric CmdArgs.push_back("Arguments"); 52090b57cec5SDimitry Andric } 52100b57cec5SDimitry Andric 52110b57cec5SDimitry Andric // Enable -mconstructor-aliases except on darwin, where we have to work around 5212349cc55cSDimitry Andric // a linker bug (see <rdar://problem/7651567>), and CUDA device code, where 5213349cc55cSDimitry Andric // aliases aren't supported. 5214349cc55cSDimitry Andric if (!RawTriple.isOSDarwin() && !RawTriple.isNVPTX()) 52150b57cec5SDimitry Andric CmdArgs.push_back("-mconstructor-aliases"); 52160b57cec5SDimitry Andric 52170b57cec5SDimitry Andric // Darwin's kernel doesn't support guard variables; just die if we 52180b57cec5SDimitry Andric // try to use them. 52190b57cec5SDimitry Andric if (KernelOrKext && RawTriple.isOSDarwin()) 52200b57cec5SDimitry Andric CmdArgs.push_back("-fforbid-guard-variables"); 52210b57cec5SDimitry Andric 52220b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mms_bitfields, options::OPT_mno_ms_bitfields, 52235ffd83dbSDimitry Andric Triple.isWindowsGNUEnvironment())) { 52240b57cec5SDimitry Andric CmdArgs.push_back("-mms-bitfields"); 52250b57cec5SDimitry Andric } 52260b57cec5SDimitry Andric 5227e8d8bef9SDimitry Andric // Non-PIC code defaults to -fdirect-access-external-data while PIC code 5228e8d8bef9SDimitry Andric // defaults to -fno-direct-access-external-data. Pass the option if different 5229e8d8bef9SDimitry Andric // from the default. 5230e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fdirect_access_external_data, 5231e8d8bef9SDimitry Andric options::OPT_fno_direct_access_external_data)) 5232e8d8bef9SDimitry Andric if (A->getOption().matches(options::OPT_fdirect_access_external_data) != 5233e8d8bef9SDimitry Andric (PICLevel == 0)) 5234e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 52350b57cec5SDimitry Andric 52360b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) { 52370b57cec5SDimitry Andric CmdArgs.push_back("-fno-plt"); 52380b57cec5SDimitry Andric } 52390b57cec5SDimitry Andric 52400b57cec5SDimitry Andric // -fhosted is default. 52410b57cec5SDimitry Andric // TODO: Audit uses of KernelOrKext and see where it'd be more appropriate to 52420b57cec5SDimitry Andric // use Freestanding. 52430b57cec5SDimitry Andric bool Freestanding = 52440b57cec5SDimitry Andric Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) || 52450b57cec5SDimitry Andric KernelOrKext; 52460b57cec5SDimitry Andric if (Freestanding) 52470b57cec5SDimitry Andric CmdArgs.push_back("-ffreestanding"); 52480b57cec5SDimitry Andric 52490b57cec5SDimitry Andric // This is a coarse approximation of what llvm-gcc actually does, both 52500b57cec5SDimitry Andric // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more 52510b57cec5SDimitry Andric // complicated ways. 5252349cc55cSDimitry Andric auto SanitizeArgs = TC.getSanitizerArgs(Args); 5253349cc55cSDimitry Andric bool AsyncUnwindTables = Args.hasFlag( 5254349cc55cSDimitry Andric options::OPT_fasynchronous_unwind_tables, 52550b57cec5SDimitry Andric options::OPT_fno_asynchronous_unwind_tables, 5256349cc55cSDimitry Andric (TC.IsUnwindTablesDefault(Args) || SanitizeArgs.needsUnwindTables()) && 52570b57cec5SDimitry Andric !Freestanding); 5258349cc55cSDimitry Andric bool UnwindTables = Args.hasFlag(options::OPT_funwind_tables, 5259349cc55cSDimitry Andric options::OPT_fno_unwind_tables, false); 5260349cc55cSDimitry Andric if (AsyncUnwindTables) 5261349cc55cSDimitry Andric CmdArgs.push_back("-funwind-tables=2"); 5262349cc55cSDimitry Andric else if (UnwindTables) 5263349cc55cSDimitry Andric CmdArgs.push_back("-funwind-tables=1"); 52640b57cec5SDimitry Andric 52655ffd83dbSDimitry Andric // Prepare `-aux-target-cpu` and `-aux-target-feature` unless 52665ffd83dbSDimitry Andric // `--gpu-use-aux-triple-only` is specified. 52675ffd83dbSDimitry Andric if (!Args.getLastArg(options::OPT_gpu_use_aux_triple_only) && 5268fe6060f1SDimitry Andric (IsCudaDevice || IsHIPDevice)) { 52695ffd83dbSDimitry Andric const ArgList &HostArgs = 52705ffd83dbSDimitry Andric C.getArgsForToolChain(nullptr, StringRef(), Action::OFK_None); 52715ffd83dbSDimitry Andric std::string HostCPU = 5272349cc55cSDimitry Andric getCPUName(D, HostArgs, *TC.getAuxTriple(), /*FromAs*/ false); 52735ffd83dbSDimitry Andric if (!HostCPU.empty()) { 52745ffd83dbSDimitry Andric CmdArgs.push_back("-aux-target-cpu"); 52755ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(HostCPU)); 52765ffd83dbSDimitry Andric } 52775ffd83dbSDimitry Andric getTargetFeatures(D, *TC.getAuxTriple(), HostArgs, CmdArgs, 52785ffd83dbSDimitry Andric /*ForAS*/ false, /*IsAux*/ true); 52795ffd83dbSDimitry Andric } 52805ffd83dbSDimitry Andric 52810b57cec5SDimitry Andric TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind()); 52820b57cec5SDimitry Andric 52830b57cec5SDimitry Andric // FIXME: Handle -mtune=. 52840b57cec5SDimitry Andric (void)Args.hasArg(options::OPT_mtune_EQ); 52850b57cec5SDimitry Andric 52860b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) { 52875ffd83dbSDimitry Andric StringRef CM = A->getValue(); 52885ffd83dbSDimitry Andric if (CM == "small" || CM == "kernel" || CM == "medium" || CM == "large" || 5289fe6060f1SDimitry Andric CM == "tiny") { 5290fe6060f1SDimitry Andric if (Triple.isOSAIX() && CM == "medium") 5291fe6060f1SDimitry Andric CmdArgs.push_back("-mcmodel=large"); 52925ffd83dbSDimitry Andric else 5293fe6060f1SDimitry Andric A->render(Args, CmdArgs); 5294fe6060f1SDimitry Andric } else { 52955ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 52965ffd83dbSDimitry Andric << CM << A->getOption().getName(); 52970b57cec5SDimitry Andric } 5298fe6060f1SDimitry Andric } 52990b57cec5SDimitry Andric 5300480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mtls_size_EQ)) { 5301480093f4SDimitry Andric StringRef Value = A->getValue(); 5302480093f4SDimitry Andric unsigned TLSSize = 0; 5303480093f4SDimitry Andric Value.getAsInteger(10, TLSSize); 5304480093f4SDimitry Andric if (!Triple.isAArch64() || !Triple.isOSBinFormatELF()) 5305480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5306480093f4SDimitry Andric << A->getOption().getName() << TripleStr; 5307480093f4SDimitry Andric if (TLSSize != 12 && TLSSize != 24 && TLSSize != 32 && TLSSize != 48) 5308480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 5309480093f4SDimitry Andric << A->getOption().getName() << Value; 5310480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_mtls_size_EQ); 5311480093f4SDimitry Andric } 5312480093f4SDimitry Andric 53130b57cec5SDimitry Andric // Add the target cpu 5314349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false); 53150b57cec5SDimitry Andric if (!CPU.empty()) { 53160b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 53170b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 53180b57cec5SDimitry Andric } 53190b57cec5SDimitry Andric 53200b57cec5SDimitry Andric RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs); 53210b57cec5SDimitry Andric 5322fe6060f1SDimitry Andric // FIXME: For now we want to demote any errors to warnings, when they have 5323fe6060f1SDimitry Andric // been raised for asking the wrong question of scalable vectors, such as 5324fe6060f1SDimitry Andric // asking for the fixed number of elements. This may happen because code that 5325fe6060f1SDimitry Andric // is not yet ported to work for scalable vectors uses the wrong interfaces, 5326fe6060f1SDimitry Andric // whereas the behaviour is actually correct. Emitting a warning helps bring 5327fe6060f1SDimitry Andric // up scalable vector support in an incremental way. When scalable vector 5328fe6060f1SDimitry Andric // support is stable enough, all uses of wrong interfaces should be considered 5329fe6060f1SDimitry Andric // as errors, but until then, we can live with a warning being emitted by the 5330fe6060f1SDimitry Andric // compiler. This way, Clang can be used to compile code with scalable vectors 5331fe6060f1SDimitry Andric // and identify possible issues. 533204eeddc0SDimitry Andric if (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) || 533304eeddc0SDimitry Andric isa<BackendJobAction>(JA)) { 5334fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 5335fe6060f1SDimitry Andric CmdArgs.push_back("-treat-scalable-fixed-error-as-warning"); 5336fe6060f1SDimitry Andric } 5337fe6060f1SDimitry Andric 53380b57cec5SDimitry Andric // These two are potentially updated by AddClangCLArgs. 53390b57cec5SDimitry Andric codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo; 53400b57cec5SDimitry Andric bool EmitCodeView = false; 53410b57cec5SDimitry Andric 53420b57cec5SDimitry Andric // Add clang-cl arguments. 53430b57cec5SDimitry Andric types::ID InputType = Input.getType(); 53440b57cec5SDimitry Andric if (D.IsCLMode()) 53450b57cec5SDimitry Andric AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView); 53460b57cec5SDimitry Andric 5347e8d8bef9SDimitry Andric DwarfFissionKind DwarfFission = DwarfFissionKind::None; 5348e8d8bef9SDimitry Andric renderDebugOptions(TC, D, RawTriple, Args, EmitCodeView, 5349e8d8bef9SDimitry Andric types::isLLVMIR(InputType), CmdArgs, DebugInfoKind, 5350e8d8bef9SDimitry Andric DwarfFission); 53510b57cec5SDimitry Andric 53520b57cec5SDimitry Andric // Add the split debug info name to the command lines here so we 53530b57cec5SDimitry Andric // can propagate it to the backend. 53540b57cec5SDimitry Andric bool SplitDWARF = (DwarfFission != DwarfFissionKind::None) && 5355e8d8bef9SDimitry Andric (TC.getTriple().isOSBinFormatELF() || 5356e8d8bef9SDimitry Andric TC.getTriple().isOSBinFormatWasm()) && 53570b57cec5SDimitry Andric (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) || 53580b57cec5SDimitry Andric isa<BackendJobAction>(JA)); 53590b57cec5SDimitry Andric if (SplitDWARF) { 5360e8d8bef9SDimitry Andric const char *SplitDWARFOut = SplitDebugName(JA, Args, Input, Output); 53610b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-file"); 53620b57cec5SDimitry Andric CmdArgs.push_back(SplitDWARFOut); 53630b57cec5SDimitry Andric if (DwarfFission == DwarfFissionKind::Split) { 53640b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-output"); 53650b57cec5SDimitry Andric CmdArgs.push_back(SplitDWARFOut); 53660b57cec5SDimitry Andric } 53670b57cec5SDimitry Andric } 53680b57cec5SDimitry Andric 53690b57cec5SDimitry Andric // Pass the linker version in use. 53700b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) { 53710b57cec5SDimitry Andric CmdArgs.push_back("-target-linker-version"); 53720b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 53730b57cec5SDimitry Andric } 53740b57cec5SDimitry Andric 53750b57cec5SDimitry Andric // Explicitly error on some things we know we don't support and can't just 53760b57cec5SDimitry Andric // ignore. 53770b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fallow_unsupported)) { 53780b57cec5SDimitry Andric Arg *Unsupported; 53790b57cec5SDimitry Andric if (types::isCXX(InputType) && RawTriple.isOSDarwin() && 53800b57cec5SDimitry Andric TC.getArch() == llvm::Triple::x86) { 53810b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) || 53820b57cec5SDimitry Andric (Unsupported = Args.getLastArg(options::OPT_mkernel))) 53830b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386) 53840b57cec5SDimitry Andric << Unsupported->getOption().getName(); 53850b57cec5SDimitry Andric } 53860b57cec5SDimitry Andric // The faltivec option has been superseded by the maltivec option. 53870b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_faltivec))) 53880b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_faltivec) 53890b57cec5SDimitry Andric << Unsupported->getOption().getName() 53900b57cec5SDimitry Andric << "please use -maltivec and include altivec.h explicitly"; 53910b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_fno_altivec))) 53920b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_faltivec) 53930b57cec5SDimitry Andric << Unsupported->getOption().getName() << "please use -mno-altivec"; 53940b57cec5SDimitry Andric } 53950b57cec5SDimitry Andric 53960b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_v); 53975ffd83dbSDimitry Andric 53985ffd83dbSDimitry Andric if (Args.getLastArg(options::OPT_H)) { 53995ffd83dbSDimitry Andric CmdArgs.push_back("-H"); 54005ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 54015ffd83dbSDimitry Andric } 5402fe6060f1SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fshow_skipped_includes); 54035ffd83dbSDimitry Andric 54040b57cec5SDimitry Andric if (D.CCPrintHeaders && !D.CCGenDiagnostics) { 54050b57cec5SDimitry Andric CmdArgs.push_back("-header-include-file"); 5406fe6060f1SDimitry Andric CmdArgs.push_back(!D.CCPrintHeadersFilename.empty() 5407fe6060f1SDimitry Andric ? D.CCPrintHeadersFilename.c_str() 54080b57cec5SDimitry Andric : "-"); 54095ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 54100b57cec5SDimitry Andric } 54110b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_P); 54120b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout); 54130b57cec5SDimitry Andric 54140b57cec5SDimitry Andric if (D.CCLogDiagnostics && !D.CCGenDiagnostics) { 54150b57cec5SDimitry Andric CmdArgs.push_back("-diagnostic-log-file"); 5416fe6060f1SDimitry Andric CmdArgs.push_back(!D.CCLogDiagnosticsFilename.empty() 5417fe6060f1SDimitry Andric ? D.CCLogDiagnosticsFilename.c_str() 54180b57cec5SDimitry Andric : "-"); 54190b57cec5SDimitry Andric } 54200b57cec5SDimitry Andric 542113138422SDimitry Andric // Give the gen diagnostics more chances to succeed, by avoiding intentional 542213138422SDimitry Andric // crashes. 542313138422SDimitry Andric if (D.CCGenDiagnostics) 542413138422SDimitry Andric CmdArgs.push_back("-disable-pragma-debug-crash"); 542513138422SDimitry Andric 5426fe6060f1SDimitry Andric // Allow backend to put its diagnostic files in the same place as frontend 5427fe6060f1SDimitry Andric // crash diagnostics files. 5428fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fcrash_diagnostics_dir)) { 5429fe6060f1SDimitry Andric StringRef Dir = Args.getLastArgValue(options::OPT_fcrash_diagnostics_dir); 5430fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 5431fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-crash-diagnostics-dir=" + Dir)); 5432fe6060f1SDimitry Andric } 5433fe6060f1SDimitry Andric 54340b57cec5SDimitry Andric bool UseSeparateSections = isUseSeparateSections(Triple); 54350b57cec5SDimitry Andric 54360b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_ffunction_sections, 54370b57cec5SDimitry Andric options::OPT_fno_function_sections, UseSeparateSections)) { 54380b57cec5SDimitry Andric CmdArgs.push_back("-ffunction-sections"); 54390b57cec5SDimitry Andric } 54400b57cec5SDimitry Andric 54415ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbasic_block_sections_EQ)) { 54425ffd83dbSDimitry Andric StringRef Val = A->getValue(); 5443fe6060f1SDimitry Andric if (Triple.isX86() && Triple.isOSBinFormatELF()) { 54445ffd83dbSDimitry Andric if (Val != "all" && Val != "labels" && Val != "none" && 5445e8d8bef9SDimitry Andric !Val.startswith("list=")) 54465ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 54475ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 54485ffd83dbSDimitry Andric else 54495ffd83dbSDimitry Andric A->render(Args, CmdArgs); 5450fe6060f1SDimitry Andric } else if (Triple.isNVPTX()) { 5451fe6060f1SDimitry Andric // Do not pass the option to the GPU compilation. We still want it enabled 5452fe6060f1SDimitry Andric // for the host-side compilation, so seeing it here is not an error. 5453fe6060f1SDimitry Andric } else if (Val != "none") { 5454fe6060f1SDimitry Andric // =none is allowed everywhere. It's useful for overriding the option 5455fe6060f1SDimitry Andric // and is the same as not specifying the option. 5456e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5457e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 5458e8d8bef9SDimitry Andric } 54595ffd83dbSDimitry Andric } 54605ffd83dbSDimitry Andric 5461e8d8bef9SDimitry Andric bool HasDefaultDataSections = Triple.isOSBinFormatXCOFF(); 54620b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections, 5463e8d8bef9SDimitry Andric UseSeparateSections || HasDefaultDataSections)) { 54640b57cec5SDimitry Andric CmdArgs.push_back("-fdata-sections"); 54650b57cec5SDimitry Andric } 54660b57cec5SDimitry Andric 54670b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_funique_section_names, 54680b57cec5SDimitry Andric options::OPT_fno_unique_section_names, true)) 54690b57cec5SDimitry Andric CmdArgs.push_back("-fno-unique-section-names"); 54700b57cec5SDimitry Andric 54715ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_funique_internal_linkage_names, 54725ffd83dbSDimitry Andric options::OPT_fno_unique_internal_linkage_names, false)) 54735ffd83dbSDimitry Andric CmdArgs.push_back("-funique-internal-linkage-names"); 54745ffd83dbSDimitry Andric 54755ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_funique_basic_block_section_names, 54765ffd83dbSDimitry Andric options::OPT_fno_unique_basic_block_section_names, false)) 54775ffd83dbSDimitry Andric CmdArgs.push_back("-funique-basic-block-section-names"); 54785ffd83dbSDimitry Andric 5479e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fsplit_machine_functions, 5480e8d8bef9SDimitry Andric options::OPT_fno_split_machine_functions)) { 5481e8d8bef9SDimitry Andric // This codegen pass is only available on x86-elf targets. 5482e8d8bef9SDimitry Andric if (Triple.isX86() && Triple.isOSBinFormatELF()) { 5483e8d8bef9SDimitry Andric if (A->getOption().matches(options::OPT_fsplit_machine_functions)) 5484e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5485e8d8bef9SDimitry Andric } else { 5486e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5487e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 5488e8d8bef9SDimitry Andric } 5489e8d8bef9SDimitry Andric } 5490e8d8bef9SDimitry Andric 54910b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_finstrument_functions, 54920b57cec5SDimitry Andric options::OPT_finstrument_functions_after_inlining, 54930b57cec5SDimitry Andric options::OPT_finstrument_function_entry_bare); 54940b57cec5SDimitry Andric 54955ffd83dbSDimitry Andric // NVPTX/AMDGCN doesn't support PGO or coverage. There's no runtime support 54965ffd83dbSDimitry Andric // for sampling, overhead of call arc collection is way too high and there's 54975ffd83dbSDimitry Andric // no way to collect the output. 54985ffd83dbSDimitry Andric if (!Triple.isNVPTX() && !Triple.isAMDGCN()) 5499349cc55cSDimitry Andric addPGOAndCoverageFlags(TC, C, D, Output, Args, SanitizeArgs, CmdArgs); 55000b57cec5SDimitry Andric 55010b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fclang_abi_compat_EQ); 55020b57cec5SDimitry Andric 55030b57cec5SDimitry Andric // Add runtime flag for PS4 when PGO, coverage, or sanitizers are enabled. 55040b57cec5SDimitry Andric if (RawTriple.isPS4CPU() && 55050b57cec5SDimitry Andric !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 55060b57cec5SDimitry Andric PS4cpu::addProfileRTArgs(TC, Args, CmdArgs); 5507349cc55cSDimitry Andric PS4cpu::addSanitizerArgs(TC, Args, CmdArgs); 55080b57cec5SDimitry Andric } 55090b57cec5SDimitry Andric 55100b57cec5SDimitry Andric // Pass options for controlling the default header search paths. 55110b57cec5SDimitry Andric if (Args.hasArg(options::OPT_nostdinc)) { 55120b57cec5SDimitry Andric CmdArgs.push_back("-nostdsysteminc"); 55130b57cec5SDimitry Andric CmdArgs.push_back("-nobuiltininc"); 55140b57cec5SDimitry Andric } else { 55150b57cec5SDimitry Andric if (Args.hasArg(options::OPT_nostdlibinc)) 55160b57cec5SDimitry Andric CmdArgs.push_back("-nostdsysteminc"); 55170b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_nostdincxx); 55180b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc); 55190b57cec5SDimitry Andric } 55200b57cec5SDimitry Andric 55210b57cec5SDimitry Andric // Pass the path to compiler resource files. 55220b57cec5SDimitry Andric CmdArgs.push_back("-resource-dir"); 55230b57cec5SDimitry Andric CmdArgs.push_back(D.ResourceDir.c_str()); 55240b57cec5SDimitry Andric 55250b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_working_directory); 55260b57cec5SDimitry Andric 55270b57cec5SDimitry Andric RenderARCMigrateToolOptions(D, Args, CmdArgs); 55280b57cec5SDimitry Andric 55290b57cec5SDimitry Andric // Add preprocessing options like -I, -D, etc. if we are using the 55300b57cec5SDimitry Andric // preprocessor. 55310b57cec5SDimitry Andric // 55320b57cec5SDimitry Andric // FIXME: Support -fpreprocessed 55330b57cec5SDimitry Andric if (types::getPreprocessedType(InputType) != types::TY_INVALID) 55340b57cec5SDimitry Andric AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs); 55350b57cec5SDimitry Andric 55360b57cec5SDimitry Andric // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes 55370b57cec5SDimitry Andric // that "The compiler can only warn and ignore the option if not recognized". 55380b57cec5SDimitry Andric // When building with ccache, it will pass -D options to clang even on 55390b57cec5SDimitry Andric // preprocessed inputs and configure concludes that -fPIC is not supported. 55400b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_D); 55410b57cec5SDimitry Andric 55420b57cec5SDimitry Andric // Manually translate -O4 to -O3; let clang reject others. 55430b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 55440b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4)) { 55450b57cec5SDimitry Andric CmdArgs.push_back("-O3"); 55460b57cec5SDimitry Andric D.Diag(diag::warn_O4_is_O3); 55470b57cec5SDimitry Andric } else { 55480b57cec5SDimitry Andric A->render(Args, CmdArgs); 55490b57cec5SDimitry Andric } 55500b57cec5SDimitry Andric } 55510b57cec5SDimitry Andric 55520b57cec5SDimitry Andric // Warn about ignored options to clang. 55530b57cec5SDimitry Andric for (const Arg *A : 55540b57cec5SDimitry Andric Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) { 55550b57cec5SDimitry Andric D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args); 55560b57cec5SDimitry Andric A->claim(); 55570b57cec5SDimitry Andric } 55580b57cec5SDimitry Andric 55590b57cec5SDimitry Andric for (const Arg *A : 55600b57cec5SDimitry Andric Args.filtered(options::OPT_clang_ignored_legacy_options_Group)) { 55610b57cec5SDimitry Andric D.Diag(diag::warn_ignored_clang_option) << A->getAsString(Args); 55620b57cec5SDimitry Andric A->claim(); 55630b57cec5SDimitry Andric } 55640b57cec5SDimitry Andric 55650b57cec5SDimitry Andric claimNoWarnArgs(Args); 55660b57cec5SDimitry Andric 55670b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_R_Group); 55680b57cec5SDimitry Andric 5569349cc55cSDimitry Andric for (const Arg *A : 5570349cc55cSDimitry Andric Args.filtered(options::OPT_W_Group, options::OPT__SLASH_wd)) { 5571349cc55cSDimitry Andric A->claim(); 5572349cc55cSDimitry Andric if (A->getOption().getID() == options::OPT__SLASH_wd) { 5573349cc55cSDimitry Andric unsigned WarningNumber; 5574349cc55cSDimitry Andric if (StringRef(A->getValue()).getAsInteger(10, WarningNumber)) { 5575349cc55cSDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 5576349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 5577349cc55cSDimitry Andric continue; 5578349cc55cSDimitry Andric } 5579349cc55cSDimitry Andric 5580349cc55cSDimitry Andric if (auto Group = diagGroupFromCLWarningID(WarningNumber)) { 5581349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 5582349cc55cSDimitry Andric "-Wno-" + DiagnosticIDs::getWarningOptionForGroup(*Group))); 5583349cc55cSDimitry Andric } 5584349cc55cSDimitry Andric continue; 5585349cc55cSDimitry Andric } 5586349cc55cSDimitry Andric A->render(Args, CmdArgs); 5587349cc55cSDimitry Andric } 5588349cc55cSDimitry Andric 55890b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false)) 55900b57cec5SDimitry Andric CmdArgs.push_back("-pedantic"); 55910b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors); 55920b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_w); 55930b57cec5SDimitry Andric 55940b57cec5SDimitry Andric // Fixed point flags 55950b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_ffixed_point, options::OPT_fno_fixed_point, 55960b57cec5SDimitry Andric /*Default=*/false)) 55970b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffixed_point); 55980b57cec5SDimitry Andric 5599fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fcxx_abi_EQ)) 5600fe6060f1SDimitry Andric A->render(Args, CmdArgs); 5601fe6060f1SDimitry Andric 5602fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, 5603fe6060f1SDimitry Andric options::OPT_fno_experimental_relative_cxx_abi_vtables); 5604fe6060f1SDimitry Andric 5605349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffuchsia_api_level_EQ)) 5606349cc55cSDimitry Andric A->render(Args, CmdArgs); 5607349cc55cSDimitry Andric 56080b57cec5SDimitry Andric // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi} 56090b57cec5SDimitry Andric // (-ansi is equivalent to -std=c89 or -std=c++98). 56100b57cec5SDimitry Andric // 56110b57cec5SDimitry Andric // If a std is supplied, only add -trigraphs if it follows the 56120b57cec5SDimitry Andric // option. 5613fe6060f1SDimitry Andric bool ImplyVCPPCVer = false; 56140b57cec5SDimitry Andric bool ImplyVCPPCXXVer = false; 56150b57cec5SDimitry Andric const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi); 56160b57cec5SDimitry Andric if (Std) { 56170b57cec5SDimitry Andric if (Std->getOption().matches(options::OPT_ansi)) 56180b57cec5SDimitry Andric if (types::isCXX(InputType)) 56190b57cec5SDimitry Andric CmdArgs.push_back("-std=c++98"); 56200b57cec5SDimitry Andric else 56210b57cec5SDimitry Andric CmdArgs.push_back("-std=c89"); 56220b57cec5SDimitry Andric else 56230b57cec5SDimitry Andric Std->render(Args, CmdArgs); 56240b57cec5SDimitry Andric 56250b57cec5SDimitry Andric // If -f(no-)trigraphs appears after the language standard flag, honor it. 56260b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi, 56270b57cec5SDimitry Andric options::OPT_ftrigraphs, 56280b57cec5SDimitry Andric options::OPT_fno_trigraphs)) 56290b57cec5SDimitry Andric if (A != Std) 56300b57cec5SDimitry Andric A->render(Args, CmdArgs); 56310b57cec5SDimitry Andric } else { 56320b57cec5SDimitry Andric // Honor -std-default. 56330b57cec5SDimitry Andric // 56340b57cec5SDimitry Andric // FIXME: Clang doesn't correctly handle -std= when the input language 56350b57cec5SDimitry Andric // doesn't match. For the time being just ignore this for C++ inputs; 56360b57cec5SDimitry Andric // eventually we want to do all the standard defaulting here instead of 56370b57cec5SDimitry Andric // splitting it between the driver and clang -cc1. 5638fe6060f1SDimitry Andric if (!types::isCXX(InputType)) { 5639fe6060f1SDimitry Andric if (!Args.hasArg(options::OPT__SLASH_std)) { 56400b57cec5SDimitry Andric Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=", 56410b57cec5SDimitry Andric /*Joined=*/true); 5642fe6060f1SDimitry Andric } else 5643fe6060f1SDimitry Andric ImplyVCPPCVer = true; 5644fe6060f1SDimitry Andric } 56450b57cec5SDimitry Andric else if (IsWindowsMSVC) 56460b57cec5SDimitry Andric ImplyVCPPCXXVer = true; 56470b57cec5SDimitry Andric 56480b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrigraphs, 56490b57cec5SDimitry Andric options::OPT_fno_trigraphs); 56505ffd83dbSDimitry Andric 56515ffd83dbSDimitry Andric // HIP headers has minimum C++ standard requirements. Therefore set the 56525ffd83dbSDimitry Andric // default language standard. 56535ffd83dbSDimitry Andric if (IsHIP) 56545ffd83dbSDimitry Andric CmdArgs.push_back(IsWindowsMSVC ? "-std=c++14" : "-std=c++11"); 56550b57cec5SDimitry Andric } 56560b57cec5SDimitry Andric 56570b57cec5SDimitry Andric // GCC's behavior for -Wwrite-strings is a bit strange: 56580b57cec5SDimitry Andric // * In C, this "warning flag" changes the types of string literals from 56590b57cec5SDimitry Andric // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning 56600b57cec5SDimitry Andric // for the discarded qualifier. 56610b57cec5SDimitry Andric // * In C++, this is just a normal warning flag. 56620b57cec5SDimitry Andric // 56630b57cec5SDimitry Andric // Implementing this warning correctly in C is hard, so we follow GCC's 56640b57cec5SDimitry Andric // behavior for now. FIXME: Directly diagnose uses of a string literal as 56650b57cec5SDimitry Andric // a non-const char* in C, rather than using this crude hack. 56660b57cec5SDimitry Andric if (!types::isCXX(InputType)) { 56670b57cec5SDimitry Andric // FIXME: This should behave just like a warning flag, and thus should also 56680b57cec5SDimitry Andric // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on. 56690b57cec5SDimitry Andric Arg *WriteStrings = 56700b57cec5SDimitry Andric Args.getLastArg(options::OPT_Wwrite_strings, 56710b57cec5SDimitry Andric options::OPT_Wno_write_strings, options::OPT_w); 56720b57cec5SDimitry Andric if (WriteStrings && 56730b57cec5SDimitry Andric WriteStrings->getOption().matches(options::OPT_Wwrite_strings)) 56740b57cec5SDimitry Andric CmdArgs.push_back("-fconst-strings"); 56750b57cec5SDimitry Andric } 56760b57cec5SDimitry Andric 56770b57cec5SDimitry Andric // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active 56780b57cec5SDimitry Andric // during C++ compilation, which it is by default. GCC keeps this define even 56790b57cec5SDimitry Andric // in the presence of '-w', match this behavior bug-for-bug. 56800b57cec5SDimitry Andric if (types::isCXX(InputType) && 56810b57cec5SDimitry Andric Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated, 56820b57cec5SDimitry Andric true)) { 56830b57cec5SDimitry Andric CmdArgs.push_back("-fdeprecated-macro"); 56840b57cec5SDimitry Andric } 56850b57cec5SDimitry Andric 56860b57cec5SDimitry Andric // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'. 56870b57cec5SDimitry Andric if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) { 56880b57cec5SDimitry Andric if (Asm->getOption().matches(options::OPT_fasm)) 56890b57cec5SDimitry Andric CmdArgs.push_back("-fgnu-keywords"); 56900b57cec5SDimitry Andric else 56910b57cec5SDimitry Andric CmdArgs.push_back("-fno-gnu-keywords"); 56920b57cec5SDimitry Andric } 56930b57cec5SDimitry Andric 5694480093f4SDimitry Andric if (!ShouldEnableAutolink(Args, TC, JA)) 56950b57cec5SDimitry Andric CmdArgs.push_back("-fno-autolink"); 56960b57cec5SDimitry Andric 56970b57cec5SDimitry Andric // Add in -fdebug-compilation-dir if necessary. 56980eae32dcSDimitry Andric const char *DebugCompilationDir = 5699a7dea167SDimitry Andric addDebugCompDirArg(Args, CmdArgs, D.getVFS()); 57000b57cec5SDimitry Andric 57010b57cec5SDimitry Andric addDebugPrefixMapArg(D, Args, CmdArgs); 57020b57cec5SDimitry Andric 57030b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_, 57040b57cec5SDimitry Andric options::OPT_ftemplate_depth_EQ)) { 57050b57cec5SDimitry Andric CmdArgs.push_back("-ftemplate-depth"); 57060b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 57070b57cec5SDimitry Andric } 57080b57cec5SDimitry Andric 57090b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) { 57100b57cec5SDimitry Andric CmdArgs.push_back("-foperator-arrow-depth"); 57110b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 57120b57cec5SDimitry Andric } 57130b57cec5SDimitry Andric 57140b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) { 57150b57cec5SDimitry Andric CmdArgs.push_back("-fconstexpr-depth"); 57160b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 57170b57cec5SDimitry Andric } 57180b57cec5SDimitry Andric 57190b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) { 57200b57cec5SDimitry Andric CmdArgs.push_back("-fconstexpr-steps"); 57210b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 57220b57cec5SDimitry Andric } 57230b57cec5SDimitry Andric 5724a7dea167SDimitry Andric if (Args.hasArg(options::OPT_fexperimental_new_constant_interpreter)) 5725a7dea167SDimitry Andric CmdArgs.push_back("-fexperimental-new-constant-interpreter"); 5726a7dea167SDimitry Andric 57270b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) { 57280b57cec5SDimitry Andric CmdArgs.push_back("-fbracket-depth"); 57290b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 57300b57cec5SDimitry Andric } 57310b57cec5SDimitry Andric 57320b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ, 57330b57cec5SDimitry Andric options::OPT_Wlarge_by_value_copy_def)) { 57340b57cec5SDimitry Andric if (A->getNumValues()) { 57350b57cec5SDimitry Andric StringRef bytes = A->getValue(); 57360b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes)); 57370b57cec5SDimitry Andric } else 57380b57cec5SDimitry Andric CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value 57390b57cec5SDimitry Andric } 57400b57cec5SDimitry Andric 57410b57cec5SDimitry Andric if (Args.hasArg(options::OPT_relocatable_pch)) 57420b57cec5SDimitry Andric CmdArgs.push_back("-relocatable-pch"); 57430b57cec5SDimitry Andric 57440b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fcf_runtime_abi_EQ)) { 57450b57cec5SDimitry Andric static const char *kCFABIs[] = { 57460b57cec5SDimitry Andric "standalone", "objc", "swift", "swift-5.0", "swift-4.2", "swift-4.1", 57470b57cec5SDimitry Andric }; 57480b57cec5SDimitry Andric 57490b57cec5SDimitry Andric if (find(kCFABIs, StringRef(A->getValue())) == std::end(kCFABIs)) 57500b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_cf_runtime_abi) << A->getValue(); 57510b57cec5SDimitry Andric else 57520b57cec5SDimitry Andric A->render(Args, CmdArgs); 57530b57cec5SDimitry Andric } 57540b57cec5SDimitry Andric 57550b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) { 57560b57cec5SDimitry Andric CmdArgs.push_back("-fconstant-string-class"); 57570b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 57580b57cec5SDimitry Andric } 57590b57cec5SDimitry Andric 57600b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) { 57610b57cec5SDimitry Andric CmdArgs.push_back("-ftabstop"); 57620b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 57630b57cec5SDimitry Andric } 57640b57cec5SDimitry Andric 57650b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fstack_size_section, 57660b57cec5SDimitry Andric options::OPT_fno_stack_size_section, RawTriple.isPS4())) 57670b57cec5SDimitry Andric CmdArgs.push_back("-fstack-size-section"); 57680b57cec5SDimitry Andric 5769fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fstack_usage)) { 5770fe6060f1SDimitry Andric CmdArgs.push_back("-stack-usage-file"); 5771fe6060f1SDimitry Andric 5772fe6060f1SDimitry Andric if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { 5773fe6060f1SDimitry Andric SmallString<128> OutputFilename(OutputOpt->getValue()); 5774fe6060f1SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, "su"); 5775fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OutputFilename)); 5776fe6060f1SDimitry Andric } else 5777fe6060f1SDimitry Andric CmdArgs.push_back( 5778fe6060f1SDimitry Andric Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".su")); 5779fe6060f1SDimitry Andric } 5780fe6060f1SDimitry Andric 57810b57cec5SDimitry Andric CmdArgs.push_back("-ferror-limit"); 57820b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ)) 57830b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 57840b57cec5SDimitry Andric else 57850b57cec5SDimitry Andric CmdArgs.push_back("19"); 57860b57cec5SDimitry Andric 57870b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) { 57880b57cec5SDimitry Andric CmdArgs.push_back("-fmacro-backtrace-limit"); 57890b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 57900b57cec5SDimitry Andric } 57910b57cec5SDimitry Andric 57920b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) { 57930b57cec5SDimitry Andric CmdArgs.push_back("-ftemplate-backtrace-limit"); 57940b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 57950b57cec5SDimitry Andric } 57960b57cec5SDimitry Andric 57970b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) { 57980b57cec5SDimitry Andric CmdArgs.push_back("-fconstexpr-backtrace-limit"); 57990b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 58000b57cec5SDimitry Andric } 58010b57cec5SDimitry Andric 58020b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fspell_checking_limit_EQ)) { 58030b57cec5SDimitry Andric CmdArgs.push_back("-fspell-checking-limit"); 58040b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 58050b57cec5SDimitry Andric } 58060b57cec5SDimitry Andric 58070b57cec5SDimitry Andric // Pass -fmessage-length=. 58085ffd83dbSDimitry Andric unsigned MessageLength = 0; 58090b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) { 58105ffd83dbSDimitry Andric StringRef V(A->getValue()); 58115ffd83dbSDimitry Andric if (V.getAsInteger(0, MessageLength)) 58125ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 58135ffd83dbSDimitry Andric << V << A->getOption().getName(); 58140b57cec5SDimitry Andric } else { 58150b57cec5SDimitry Andric // If -fmessage-length=N was not specified, determine whether this is a 58160b57cec5SDimitry Andric // terminal and, if so, implicitly define -fmessage-length appropriately. 58175ffd83dbSDimitry Andric MessageLength = llvm::sys::Process::StandardErrColumns(); 58180b57cec5SDimitry Andric } 58195ffd83dbSDimitry Andric if (MessageLength != 0) 58205ffd83dbSDimitry Andric CmdArgs.push_back( 58215ffd83dbSDimitry Andric Args.MakeArgString("-fmessage-length=" + Twine(MessageLength))); 58220b57cec5SDimitry Andric 58230b57cec5SDimitry Andric // -fvisibility= and -fvisibility-ms-compat are of a piece. 58240b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ, 58250b57cec5SDimitry Andric options::OPT_fvisibility_ms_compat)) { 58260b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fvisibility_EQ)) { 58270b57cec5SDimitry Andric CmdArgs.push_back("-fvisibility"); 58280b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 58290b57cec5SDimitry Andric } else { 58300b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_fvisibility_ms_compat)); 58310b57cec5SDimitry Andric CmdArgs.push_back("-fvisibility"); 58320b57cec5SDimitry Andric CmdArgs.push_back("hidden"); 58330b57cec5SDimitry Andric CmdArgs.push_back("-ftype-visibility"); 58340b57cec5SDimitry Andric CmdArgs.push_back("default"); 58350b57cec5SDimitry Andric } 583604eeddc0SDimitry Andric } else if (IsOpenMPDevice) { 583704eeddc0SDimitry Andric // When compiling for the OpenMP device we want protected visibility by 583804eeddc0SDimitry Andric // default. This prevents the device from accidenally preempting code on the 583904eeddc0SDimitry Andric // host, makes the system more robust, and improves performance. 584004eeddc0SDimitry Andric CmdArgs.push_back("-fvisibility"); 584104eeddc0SDimitry Andric CmdArgs.push_back("protected"); 58420b57cec5SDimitry Andric } 58430b57cec5SDimitry Andric 5844e8d8bef9SDimitry Andric if (!RawTriple.isPS4()) 5845e8d8bef9SDimitry Andric if (const Arg *A = 5846e8d8bef9SDimitry Andric Args.getLastArg(options::OPT_fvisibility_from_dllstorageclass, 5847e8d8bef9SDimitry Andric options::OPT_fno_visibility_from_dllstorageclass)) { 5848e8d8bef9SDimitry Andric if (A->getOption().matches( 5849e8d8bef9SDimitry Andric options::OPT_fvisibility_from_dllstorageclass)) { 5850e8d8bef9SDimitry Andric CmdArgs.push_back("-fvisibility-from-dllstorageclass"); 5851e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_dllexport_EQ); 5852e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_nodllstorageclass_EQ); 5853e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_externs_dllimport_EQ); 5854e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, 5855e8d8bef9SDimitry Andric options::OPT_fvisibility_externs_nodllstorageclass_EQ); 5856e8d8bef9SDimitry Andric } 5857e8d8bef9SDimitry Andric } 5858e8d8bef9SDimitry Andric 5859e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mignore_xcoff_visibility)) { 5860e8d8bef9SDimitry Andric if (Triple.isOSAIX()) 5861e8d8bef9SDimitry Andric CmdArgs.push_back("-mignore-xcoff-visibility"); 5862e8d8bef9SDimitry Andric else 5863e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5864e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 5865e8d8bef9SDimitry Andric } 5866e8d8bef9SDimitry Andric 5867fe6060f1SDimitry Andric 5868fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fvisibility_inlines_hidden, 5869fe6060f1SDimitry Andric options::OPT_fno_visibility_inlines_hidden, false)) 5870fe6060f1SDimitry Andric CmdArgs.push_back("-fvisibility-inlines-hidden"); 5871fe6060f1SDimitry Andric 5872e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden_static_local_var, 5873e8d8bef9SDimitry Andric options::OPT_fno_visibility_inlines_hidden_static_local_var); 58740b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden); 58750b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ); 58760b57cec5SDimitry Andric 58774824e7fdSDimitry Andric if (Args.hasFlag(options::OPT_fnew_infallible, 58784824e7fdSDimitry Andric options::OPT_fno_new_infallible, false)) 58794824e7fdSDimitry Andric CmdArgs.push_back("-fnew-infallible"); 58804824e7fdSDimitry Andric 5881fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fno_operator_names, 5882fe6060f1SDimitry Andric options::OPT_foperator_names, false)) 5883fe6060f1SDimitry Andric CmdArgs.push_back("-fno-operator-names"); 5884fe6060f1SDimitry Andric 58850b57cec5SDimitry Andric // Forward -f (flag) options which we can pass directly. 58860b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls); 58870b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); 58880b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs); 58890b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_femulated_tls, 58900b57cec5SDimitry Andric options::OPT_fno_emulated_tls); 58910b57cec5SDimitry Andric 58920b57cec5SDimitry Andric // AltiVec-like language extensions aren't relevant for assembling. 58930b57cec5SDimitry Andric if (!isa<PreprocessJobAction>(JA) || Output.getType() != types::TY_PP_Asm) 58940b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fzvector); 58950b57cec5SDimitry Andric 58960b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree); 58970b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type); 58980b57cec5SDimitry Andric 58990b57cec5SDimitry Andric // Forward flags for OpenMP. We don't do this if the current action is an 59000b57cec5SDimitry Andric // device offloading action other than OpenMP. 59010b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ, 59020b57cec5SDimitry Andric options::OPT_fno_openmp, false) && 59030b57cec5SDimitry Andric (JA.isDeviceOffloading(Action::OFK_None) || 59040b57cec5SDimitry Andric JA.isDeviceOffloading(Action::OFK_OpenMP))) { 59050b57cec5SDimitry Andric switch (D.getOpenMPRuntime(Args)) { 59060b57cec5SDimitry Andric case Driver::OMPRT_OMP: 59070b57cec5SDimitry Andric case Driver::OMPRT_IOMP5: 59080b57cec5SDimitry Andric // Clang can generate useful OpenMP code for these two runtime libraries. 59090b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp"); 59100b57cec5SDimitry Andric 59110b57cec5SDimitry Andric // If no option regarding the use of TLS in OpenMP codegeneration is 59120b57cec5SDimitry Andric // given, decide a default based on the target. Otherwise rely on the 59130b57cec5SDimitry Andric // options and pass the right information to the frontend. 59140b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fopenmp_use_tls, 59150b57cec5SDimitry Andric options::OPT_fnoopenmp_use_tls, /*Default=*/true)) 59160b57cec5SDimitry Andric CmdArgs.push_back("-fnoopenmp-use-tls"); 59170b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd, 59180b57cec5SDimitry Andric options::OPT_fno_openmp_simd); 5919480093f4SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_enable_irbuilder); 59200b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ); 5921349cc55cSDimitry Andric if (!Args.hasFlag(options::OPT_fopenmp_extensions, 5922349cc55cSDimitry Andric options::OPT_fno_openmp_extensions, /*Default=*/true)) 5923349cc55cSDimitry Andric CmdArgs.push_back("-fno-openmp-extensions"); 59240b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_number_of_sm_EQ); 59250b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_blocks_per_sm_EQ); 59260b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, 59270b57cec5SDimitry Andric options::OPT_fopenmp_cuda_teams_reduction_recs_num_EQ); 59280b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_optimistic_collapse, 59290b57cec5SDimitry Andric options::OPT_fno_openmp_optimistic_collapse, 59300b57cec5SDimitry Andric /*Default=*/false)) 59310b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-optimistic-collapse"); 59320b57cec5SDimitry Andric 59330b57cec5SDimitry Andric // When in OpenMP offloading mode with NVPTX target, forward 59340b57cec5SDimitry Andric // cuda-mode flag 59350b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_cuda_mode, 59360b57cec5SDimitry Andric options::OPT_fno_openmp_cuda_mode, /*Default=*/false)) 59370b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-cuda-mode"); 59380b57cec5SDimitry Andric 5939349cc55cSDimitry Andric // When in OpenMP offloading mode, enable or disable the new device 5940349cc55cSDimitry Andric // runtime. 5941349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_target_new_runtime, 5942349cc55cSDimitry Andric options::OPT_fno_openmp_target_new_runtime, 59430eae32dcSDimitry Andric /*Default=*/true)) 5944349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-target-new-runtime"); 5945349cc55cSDimitry Andric 5946349cc55cSDimitry Andric // When in OpenMP offloading mode, enable debugging on the device. 5947349cc55cSDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_target_debug_EQ); 5948349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_target_debug, 5949349cc55cSDimitry Andric options::OPT_fno_openmp_target_debug, /*Default=*/false)) 5950349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-target-debug"); 5951349cc55cSDimitry Andric 59520b57cec5SDimitry Andric // When in OpenMP offloading mode with NVPTX target, check if full runtime 59530b57cec5SDimitry Andric // is required. 59540b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_cuda_force_full_runtime, 59550b57cec5SDimitry Andric options::OPT_fno_openmp_cuda_force_full_runtime, 59560b57cec5SDimitry Andric /*Default=*/false)) 59570b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-cuda-force-full-runtime"); 5958349cc55cSDimitry Andric 5959349cc55cSDimitry Andric // When in OpenMP offloading mode, forward assumptions information about 5960349cc55cSDimitry Andric // thread and team counts in the device. 5961349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_assume_teams_oversubscription, 5962349cc55cSDimitry Andric options::OPT_fno_openmp_assume_teams_oversubscription, 5963349cc55cSDimitry Andric /*Default=*/false)) 5964349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-assume-teams-oversubscription"); 5965349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_assume_threads_oversubscription, 5966349cc55cSDimitry Andric options::OPT_fno_openmp_assume_threads_oversubscription, 5967349cc55cSDimitry Andric /*Default=*/false)) 5968349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-assume-threads-oversubscription"); 59690b57cec5SDimitry Andric break; 59700b57cec5SDimitry Andric default: 59710b57cec5SDimitry Andric // By default, if Clang doesn't know how to generate useful OpenMP code 59720b57cec5SDimitry Andric // for a specific runtime library, we just don't pass the '-fopenmp' flag 59730b57cec5SDimitry Andric // down to the actual compilation. 59740b57cec5SDimitry Andric // FIXME: It would be better to have a mode which *only* omits IR 59750b57cec5SDimitry Andric // generation based on the OpenMP support so that we get consistent 59760b57cec5SDimitry Andric // semantic analysis, etc. 59770b57cec5SDimitry Andric break; 59780b57cec5SDimitry Andric } 59790b57cec5SDimitry Andric } else { 59800b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd, 59810b57cec5SDimitry Andric options::OPT_fno_openmp_simd); 59820b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ); 5983349cc55cSDimitry Andric if (!Args.hasFlag(options::OPT_fopenmp_extensions, 5984349cc55cSDimitry Andric options::OPT_fno_openmp_extensions, /*Default=*/true)) 5985349cc55cSDimitry Andric CmdArgs.push_back("-fno-openmp-extensions"); 59860b57cec5SDimitry Andric } 59870b57cec5SDimitry Andric 5988349cc55cSDimitry Andric SanitizeArgs.addArgs(TC, Args, CmdArgs, InputType); 59890b57cec5SDimitry Andric 59900b57cec5SDimitry Andric const XRayArgs &XRay = TC.getXRayArgs(); 59910b57cec5SDimitry Andric XRay.addArgs(TC, Args, CmdArgs, InputType); 59920b57cec5SDimitry Andric 5993e8d8bef9SDimitry Andric for (const auto &Filename : 5994e8d8bef9SDimitry Andric Args.getAllArgValues(options::OPT_fprofile_list_EQ)) { 5995e8d8bef9SDimitry Andric if (D.getVFS().exists(Filename)) 5996e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fprofile-list=" + Filename)); 5997e8d8bef9SDimitry Andric else 5998e8d8bef9SDimitry Andric D.Diag(clang::diag::err_drv_no_such_file) << Filename; 5999e8d8bef9SDimitry Andric } 6000e8d8bef9SDimitry Andric 6001480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ)) { 6002480093f4SDimitry Andric StringRef S0 = A->getValue(), S = S0; 600355e4f9d5SDimitry Andric unsigned Size, Offset = 0; 6004fe6060f1SDimitry Andric if (!Triple.isAArch64() && !Triple.isRISCV() && !Triple.isX86()) 6005480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6006480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6007480093f4SDimitry Andric else if (S.consumeInteger(10, Size) || 6008480093f4SDimitry Andric (!S.empty() && (!S.consume_front(",") || 600955e4f9d5SDimitry Andric S.consumeInteger(10, Offset) || !S.empty()))) 6010480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 6011480093f4SDimitry Andric << S0 << A->getOption().getName(); 601255e4f9d5SDimitry Andric else if (Size < Offset) 6013480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_fpatchable_function_entry_argument); 601455e4f9d5SDimitry Andric else { 6015480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getSpelling() + Twine(Size))); 601655e4f9d5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 601755e4f9d5SDimitry Andric "-fpatchable-function-entry-offset=" + Twine(Offset))); 601855e4f9d5SDimitry Andric } 6019480093f4SDimitry Andric } 6020480093f4SDimitry Andric 602104eeddc0SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fms_hotpatch); 602204eeddc0SDimitry Andric 6023480093f4SDimitry Andric if (TC.SupportsProfiling()) { 60240b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pg); 60250b57cec5SDimitry Andric 6026480093f4SDimitry Andric llvm::Triple::ArchType Arch = TC.getArch(); 6027480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mfentry)) { 6028480093f4SDimitry Andric if (Arch == llvm::Triple::systemz || TC.getTriple().isX86()) 6029480093f4SDimitry Andric A->render(Args, CmdArgs); 6030480093f4SDimitry Andric else 6031480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6032480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6033480093f4SDimitry Andric } 6034480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mnop_mcount)) { 6035480093f4SDimitry Andric if (Arch == llvm::Triple::systemz) 6036480093f4SDimitry Andric A->render(Args, CmdArgs); 6037480093f4SDimitry Andric else 6038480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6039480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6040480093f4SDimitry Andric } 6041480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrecord_mcount)) { 6042480093f4SDimitry Andric if (Arch == llvm::Triple::systemz) 6043480093f4SDimitry Andric A->render(Args, CmdArgs); 6044480093f4SDimitry Andric else 6045480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6046480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6047480093f4SDimitry Andric } 6048480093f4SDimitry Andric } 60490b57cec5SDimitry Andric 60500b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_fapple_kext) || 60510b57cec5SDimitry Andric (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType))) 60520b57cec5SDimitry Andric CmdArgs.push_back("-fapple-kext"); 60530b57cec5SDimitry Andric 6054fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_altivec_src_compat); 6055a7dea167SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_flax_vector_conversions_EQ); 60560b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch); 60570b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info); 60580b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits); 60590b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_report); 6060e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_report_EQ); 60610b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_trace); 6062a7dea167SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_granularity_EQ); 60630b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrapv); 60640b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_malign_double); 6065480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_temp_file); 60660b57cec5SDimitry Andric 60670b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) { 60680b57cec5SDimitry Andric CmdArgs.push_back("-ftrapv-handler"); 60690b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 60700b57cec5SDimitry Andric } 60710b57cec5SDimitry Andric 60720b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ); 60730b57cec5SDimitry Andric 60740b57cec5SDimitry Andric // -fno-strict-overflow implies -fwrapv if it isn't disabled, but 60750b57cec5SDimitry Andric // -fstrict-overflow won't turn off an explicitly enabled -fwrapv. 60760b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fwrapv, options::OPT_fno_wrapv)) { 60770b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fwrapv)) 60780b57cec5SDimitry Andric CmdArgs.push_back("-fwrapv"); 60790b57cec5SDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow, 60800b57cec5SDimitry Andric options::OPT_fno_strict_overflow)) { 60810b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fno_strict_overflow)) 60820b57cec5SDimitry Andric CmdArgs.push_back("-fwrapv"); 60830b57cec5SDimitry Andric } 60840b57cec5SDimitry Andric 60850b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_freroll_loops, 60860b57cec5SDimitry Andric options::OPT_fno_reroll_loops)) 60870b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_freroll_loops)) 60880b57cec5SDimitry Andric CmdArgs.push_back("-freroll-loops"); 60890b57cec5SDimitry Andric 6090d409305fSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffinite_loops, 6091d409305fSDimitry Andric options::OPT_fno_finite_loops); 6092d409305fSDimitry Andric 60930b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings); 60940b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_funroll_loops, 60950b57cec5SDimitry Andric options::OPT_fno_unroll_loops); 60960b57cec5SDimitry Andric 60970b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pthread); 60980b57cec5SDimitry Andric 60995ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_mspeculative_load_hardening, 61005ffd83dbSDimitry Andric options::OPT_mno_speculative_load_hardening, false)) 61010b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mspeculative-load-hardening")); 61020b57cec5SDimitry Andric 6103e8d8bef9SDimitry Andric RenderSSPOptions(D, TC, Args, CmdArgs, KernelOrKext); 61045ffd83dbSDimitry Andric RenderSCPOptions(TC, Args, CmdArgs); 61050b57cec5SDimitry Andric RenderTrivialAutoVarInitOptions(D, TC, Args, CmdArgs); 61060b57cec5SDimitry Andric 6107349cc55cSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fswift_async_fp_EQ); 6108349cc55cSDimitry Andric 61090b57cec5SDimitry Andric // Translate -mstackrealign 61100b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign, 61110b57cec5SDimitry Andric false)) 61120b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mstackrealign")); 61130b57cec5SDimitry Andric 61140b57cec5SDimitry Andric if (Args.hasArg(options::OPT_mstack_alignment)) { 61150b57cec5SDimitry Andric StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment); 61160b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment)); 61170b57cec5SDimitry Andric } 61180b57cec5SDimitry Andric 61190b57cec5SDimitry Andric if (Args.hasArg(options::OPT_mstack_probe_size)) { 61200b57cec5SDimitry Andric StringRef Size = Args.getLastArgValue(options::OPT_mstack_probe_size); 61210b57cec5SDimitry Andric 61220b57cec5SDimitry Andric if (!Size.empty()) 61230b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mstack-probe-size=" + Size)); 61240b57cec5SDimitry Andric else 61250b57cec5SDimitry Andric CmdArgs.push_back("-mstack-probe-size=0"); 61260b57cec5SDimitry Andric } 61270b57cec5SDimitry Andric 61280b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mstack_arg_probe, 61290b57cec5SDimitry Andric options::OPT_mno_stack_arg_probe, true)) 61300b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mno-stack-arg-probe")); 61310b57cec5SDimitry Andric 61320b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it, 61330b57cec5SDimitry Andric options::OPT_mno_restrict_it)) { 61340b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mrestrict_it)) { 61350b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 61360b57cec5SDimitry Andric CmdArgs.push_back("-arm-restrict-it"); 61370b57cec5SDimitry Andric } else { 61380b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 61390b57cec5SDimitry Andric CmdArgs.push_back("-arm-no-restrict-it"); 61400b57cec5SDimitry Andric } 61410b57cec5SDimitry Andric } else if (Triple.isOSWindows() && 61420b57cec5SDimitry Andric (Triple.getArch() == llvm::Triple::arm || 61430b57cec5SDimitry Andric Triple.getArch() == llvm::Triple::thumb)) { 61440b57cec5SDimitry Andric // Windows on ARM expects restricted IT blocks 61450b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 61460b57cec5SDimitry Andric CmdArgs.push_back("-arm-restrict-it"); 61470b57cec5SDimitry Andric } 61480b57cec5SDimitry Andric 61490b57cec5SDimitry Andric // Forward -cl options to -cc1 6150fe6060f1SDimitry Andric RenderOpenCLOptions(Args, CmdArgs, InputType); 61510b57cec5SDimitry Andric 6152e8d8bef9SDimitry Andric if (IsHIP) { 6153e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fhip_new_launch_api, 61545ffd83dbSDimitry Andric options::OPT_fno_hip_new_launch_api, true)) 6155a7dea167SDimitry Andric CmdArgs.push_back("-fhip-new-launch-api"); 6156e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_allow_device_init, 6157e8d8bef9SDimitry Andric options::OPT_fno_gpu_allow_device_init, false)) 6158e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-allow-device-init"); 6159e8d8bef9SDimitry Andric } 6160e8d8bef9SDimitry Andric 6161e8d8bef9SDimitry Andric if (IsCuda || IsHIP) { 6162fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false)) 6163fe6060f1SDimitry Andric CmdArgs.push_back("-fgpu-rdc"); 6164e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_defer_diag, 6165e8d8bef9SDimitry Andric options::OPT_fno_gpu_defer_diag, false)) 6166e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-defer-diag"); 6167e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_exclude_wrong_side_overloads, 6168e8d8bef9SDimitry Andric options::OPT_fno_gpu_exclude_wrong_side_overloads, 6169e8d8bef9SDimitry Andric false)) { 6170e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-exclude-wrong-side-overloads"); 6171e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-defer-diag"); 6172e8d8bef9SDimitry Andric } 6173e8d8bef9SDimitry Andric } 6174a7dea167SDimitry Andric 61750b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) { 61760b57cec5SDimitry Andric CmdArgs.push_back( 61770b57cec5SDimitry Andric Args.MakeArgString(Twine("-fcf-protection=") + A->getValue())); 61780b57cec5SDimitry Andric } 61790b57cec5SDimitry Andric 618004eeddc0SDimitry Andric if (IsUsingLTO) 618104eeddc0SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_mibt_seal); 618204eeddc0SDimitry Andric 6183fe6060f1SDimitry Andric // Forward -f options with positive and negative forms; we translate these by 6184fe6060f1SDimitry Andric // hand. Do not propagate PGO options to the GPU-side compilations as the 6185fe6060f1SDimitry Andric // profile info is for the host-side compilation only. 6186fe6060f1SDimitry Andric if (!(IsCudaDevice || IsHIPDevice)) { 61870b57cec5SDimitry Andric if (Arg *A = getLastProfileSampleUseArg(Args)) { 61880b57cec5SDimitry Andric auto *PGOArg = Args.getLastArg( 61890b57cec5SDimitry Andric options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ, 6190fe6060f1SDimitry Andric options::OPT_fcs_profile_generate, 6191fe6060f1SDimitry Andric options::OPT_fcs_profile_generate_EQ, options::OPT_fprofile_use, 6192fe6060f1SDimitry Andric options::OPT_fprofile_use_EQ); 61930b57cec5SDimitry Andric if (PGOArg) 61940b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 61950b57cec5SDimitry Andric << "SampleUse with PGO options"; 61960b57cec5SDimitry Andric 61970b57cec5SDimitry Andric StringRef fname = A->getValue(); 61980b57cec5SDimitry Andric if (!llvm::sys::fs::exists(fname)) 61990b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << fname; 62000b57cec5SDimitry Andric else 62010b57cec5SDimitry Andric A->render(Args, CmdArgs); 62020b57cec5SDimitry Andric } 62030b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fprofile_remapping_file_EQ); 62040b57cec5SDimitry Andric 6205e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fpseudo_probe_for_profiling, 6206fe6060f1SDimitry Andric options::OPT_fno_pseudo_probe_for_profiling, false)) { 6207e8d8bef9SDimitry Andric CmdArgs.push_back("-fpseudo-probe-for-profiling"); 6208fe6060f1SDimitry Andric // Enforce -funique-internal-linkage-names if it's not explicitly turned 6209fe6060f1SDimitry Andric // off. 6210fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_funique_internal_linkage_names, 6211fe6060f1SDimitry Andric options::OPT_fno_unique_internal_linkage_names, true)) 6212fe6060f1SDimitry Andric CmdArgs.push_back("-funique-internal-linkage-names"); 6213fe6060f1SDimitry Andric } 6214fe6060f1SDimitry Andric } 62150b57cec5SDimitry Andric RenderBuiltinOptions(TC, RawTriple, Args, CmdArgs); 62160b57cec5SDimitry Andric 62170b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fassume_sane_operator_new, 62180b57cec5SDimitry Andric options::OPT_fno_assume_sane_operator_new)) 62190b57cec5SDimitry Andric CmdArgs.push_back("-fno-assume-sane-operator-new"); 62200b57cec5SDimitry Andric 62210b57cec5SDimitry Andric // -fblocks=0 is default. 62220b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks, 62230b57cec5SDimitry Andric TC.IsBlocksDefault()) || 62240b57cec5SDimitry Andric (Args.hasArg(options::OPT_fgnu_runtime) && 62250b57cec5SDimitry Andric Args.hasArg(options::OPT_fobjc_nonfragile_abi) && 62260b57cec5SDimitry Andric !Args.hasArg(options::OPT_fno_blocks))) { 62270b57cec5SDimitry Andric CmdArgs.push_back("-fblocks"); 62280b57cec5SDimitry Andric 62290b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fgnu_runtime) && !TC.hasBlocksRuntime()) 62300b57cec5SDimitry Andric CmdArgs.push_back("-fblocks-runtime-optional"); 62310b57cec5SDimitry Andric } 62320b57cec5SDimitry Andric 62330b57cec5SDimitry Andric // -fencode-extended-block-signature=1 is default. 62340b57cec5SDimitry Andric if (TC.IsEncodeExtendedBlockSignatureDefault()) 62350b57cec5SDimitry Andric CmdArgs.push_back("-fencode-extended-block-signature"); 62360b57cec5SDimitry Andric 62370b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcoroutines_ts, options::OPT_fno_coroutines_ts, 62380b57cec5SDimitry Andric false) && 62390b57cec5SDimitry Andric types::isCXX(InputType)) { 62400b57cec5SDimitry Andric CmdArgs.push_back("-fcoroutines-ts"); 62410b57cec5SDimitry Andric } 62420b57cec5SDimitry Andric 62430b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdouble_square_bracket_attributes, 62440b57cec5SDimitry Andric options::OPT_fno_double_square_bracket_attributes); 62450b57cec5SDimitry Andric 62460b57cec5SDimitry Andric // -faccess-control is default. 62470b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_access_control, 62480b57cec5SDimitry Andric options::OPT_faccess_control, false)) 62490b57cec5SDimitry Andric CmdArgs.push_back("-fno-access-control"); 62500b57cec5SDimitry Andric 62510b57cec5SDimitry Andric // -felide-constructors is the default. 62520b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_elide_constructors, 62530b57cec5SDimitry Andric options::OPT_felide_constructors, false)) 62540b57cec5SDimitry Andric CmdArgs.push_back("-fno-elide-constructors"); 62550b57cec5SDimitry Andric 62560b57cec5SDimitry Andric ToolChain::RTTIMode RTTIMode = TC.getRTTIMode(); 62570b57cec5SDimitry Andric 62580b57cec5SDimitry Andric if (KernelOrKext || (types::isCXX(InputType) && 62590b57cec5SDimitry Andric (RTTIMode == ToolChain::RM_Disabled))) 62600b57cec5SDimitry Andric CmdArgs.push_back("-fno-rtti"); 62610b57cec5SDimitry Andric 6262e8d8bef9SDimitry Andric // -fshort-enums=0 is default for all architectures except Hexagon and z/OS. 62630b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fshort_enums, options::OPT_fno_short_enums, 6264e8d8bef9SDimitry Andric TC.getArch() == llvm::Triple::hexagon || Triple.isOSzOS())) 62650b57cec5SDimitry Andric CmdArgs.push_back("-fshort-enums"); 62660b57cec5SDimitry Andric 62670b57cec5SDimitry Andric RenderCharacterOptions(Args, AuxTriple ? *AuxTriple : RawTriple, CmdArgs); 62680b57cec5SDimitry Andric 62690b57cec5SDimitry Andric // -fuse-cxa-atexit is default. 62700b57cec5SDimitry Andric if (!Args.hasFlag( 62710b57cec5SDimitry Andric options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit, 62725ffd83dbSDimitry Andric !RawTriple.isOSAIX() && !RawTriple.isOSWindows() && 62730b57cec5SDimitry Andric ((RawTriple.getVendor() != llvm::Triple::MipsTechnologies) || 62740b57cec5SDimitry Andric RawTriple.hasEnvironment())) || 62750b57cec5SDimitry Andric KernelOrKext) 62760b57cec5SDimitry Andric CmdArgs.push_back("-fno-use-cxa-atexit"); 62770b57cec5SDimitry Andric 62780b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fregister_global_dtors_with_atexit, 62790b57cec5SDimitry Andric options::OPT_fno_register_global_dtors_with_atexit, 62800b57cec5SDimitry Andric RawTriple.isOSDarwin() && !KernelOrKext)) 62810b57cec5SDimitry Andric CmdArgs.push_back("-fregister-global-dtors-with-atexit"); 62820b57cec5SDimitry Andric 62830b57cec5SDimitry Andric // -fno-use-line-directives is default. 62840b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fuse_line_directives, 62850b57cec5SDimitry Andric options::OPT_fno_use_line_directives, false)) 62860b57cec5SDimitry Andric CmdArgs.push_back("-fuse-line-directives"); 62870b57cec5SDimitry Andric 6288349cc55cSDimitry Andric // -fno-minimize-whitespace is default. 6289349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fminimize_whitespace, 6290349cc55cSDimitry Andric options::OPT_fno_minimize_whitespace, false)) { 6291349cc55cSDimitry Andric types::ID InputType = Inputs[0].getType(); 6292349cc55cSDimitry Andric if (!isDerivedFromC(InputType)) 6293349cc55cSDimitry Andric D.Diag(diag::err_drv_minws_unsupported_input_type) 6294349cc55cSDimitry Andric << types::getTypeName(InputType); 6295349cc55cSDimitry Andric CmdArgs.push_back("-fminimize-whitespace"); 6296349cc55cSDimitry Andric } 6297349cc55cSDimitry Andric 62985ffd83dbSDimitry Andric // -fms-extensions=0 is default. 62995ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, 63005ffd83dbSDimitry Andric IsWindowsMSVC)) 63015ffd83dbSDimitry Andric CmdArgs.push_back("-fms-extensions"); 63025ffd83dbSDimitry Andric 63030b57cec5SDimitry Andric // -fms-compatibility=0 is default. 6304a7dea167SDimitry Andric bool IsMSVCCompat = Args.hasFlag( 6305a7dea167SDimitry Andric options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility, 6306a7dea167SDimitry Andric (IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions, 6307a7dea167SDimitry Andric options::OPT_fno_ms_extensions, true))); 6308a7dea167SDimitry Andric if (IsMSVCCompat) 63090b57cec5SDimitry Andric CmdArgs.push_back("-fms-compatibility"); 63100b57cec5SDimitry Andric 6311a7dea167SDimitry Andric // Handle -fgcc-version, if present. 6312a7dea167SDimitry Andric VersionTuple GNUCVer; 6313a7dea167SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fgnuc_version_EQ)) { 6314a7dea167SDimitry Andric // Check that the version has 1 to 3 components and the minor and patch 6315a7dea167SDimitry Andric // versions fit in two decimal digits. 6316a7dea167SDimitry Andric StringRef Val = A->getValue(); 6317a7dea167SDimitry Andric Val = Val.empty() ? "0" : Val; // Treat "" as 0 or disable. 6318a7dea167SDimitry Andric bool Invalid = GNUCVer.tryParse(Val); 6319a7dea167SDimitry Andric unsigned Minor = GNUCVer.getMinor().getValueOr(0); 6320a7dea167SDimitry Andric unsigned Patch = GNUCVer.getSubminor().getValueOr(0); 6321a7dea167SDimitry Andric if (Invalid || GNUCVer.getBuild() || Minor >= 100 || Patch >= 100) { 6322a7dea167SDimitry Andric D.Diag(diag::err_drv_invalid_value) 6323a7dea167SDimitry Andric << A->getAsString(Args) << A->getValue(); 6324a7dea167SDimitry Andric } 6325a7dea167SDimitry Andric } else if (!IsMSVCCompat) { 6326a7dea167SDimitry Andric // Imitate GCC 4.2.1 by default if -fms-compatibility is not in effect. 6327a7dea167SDimitry Andric GNUCVer = VersionTuple(4, 2, 1); 6328a7dea167SDimitry Andric } 6329a7dea167SDimitry Andric if (!GNUCVer.empty()) { 6330a7dea167SDimitry Andric CmdArgs.push_back( 6331a7dea167SDimitry Andric Args.MakeArgString("-fgnuc-version=" + GNUCVer.getAsString())); 6332a7dea167SDimitry Andric } 6333a7dea167SDimitry Andric 63340b57cec5SDimitry Andric VersionTuple MSVT = TC.computeMSVCVersion(&D, Args); 63350b57cec5SDimitry Andric if (!MSVT.empty()) 63360b57cec5SDimitry Andric CmdArgs.push_back( 63370b57cec5SDimitry Andric Args.MakeArgString("-fms-compatibility-version=" + MSVT.getAsString())); 63380b57cec5SDimitry Andric 63390b57cec5SDimitry Andric bool IsMSVC2015Compatible = MSVT.getMajor() >= 19; 6340fe6060f1SDimitry Andric if (ImplyVCPPCVer) { 6341fe6060f1SDimitry Andric StringRef LanguageStandard; 6342fe6060f1SDimitry Andric if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) { 6343fe6060f1SDimitry Andric Std = StdArg; 6344fe6060f1SDimitry Andric LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue()) 6345fe6060f1SDimitry Andric .Case("c11", "-std=c11") 6346fe6060f1SDimitry Andric .Case("c17", "-std=c17") 6347fe6060f1SDimitry Andric .Default(""); 6348fe6060f1SDimitry Andric if (LanguageStandard.empty()) 6349fe6060f1SDimitry Andric D.Diag(clang::diag::warn_drv_unused_argument) 6350fe6060f1SDimitry Andric << StdArg->getAsString(Args); 6351fe6060f1SDimitry Andric } 6352fe6060f1SDimitry Andric CmdArgs.push_back(LanguageStandard.data()); 6353fe6060f1SDimitry Andric } 63540b57cec5SDimitry Andric if (ImplyVCPPCXXVer) { 63550b57cec5SDimitry Andric StringRef LanguageStandard; 63560b57cec5SDimitry Andric if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) { 63570b57cec5SDimitry Andric Std = StdArg; 63580b57cec5SDimitry Andric LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue()) 63590b57cec5SDimitry Andric .Case("c++14", "-std=c++14") 63600b57cec5SDimitry Andric .Case("c++17", "-std=c++17") 6361fe6060f1SDimitry Andric .Case("c++20", "-std=c++20") 6362fe6060f1SDimitry Andric .Case("c++latest", "-std=c++2b") 63630b57cec5SDimitry Andric .Default(""); 63640b57cec5SDimitry Andric if (LanguageStandard.empty()) 63650b57cec5SDimitry Andric D.Diag(clang::diag::warn_drv_unused_argument) 63660b57cec5SDimitry Andric << StdArg->getAsString(Args); 63670b57cec5SDimitry Andric } 63680b57cec5SDimitry Andric 63690b57cec5SDimitry Andric if (LanguageStandard.empty()) { 63700b57cec5SDimitry Andric if (IsMSVC2015Compatible) 63710b57cec5SDimitry Andric LanguageStandard = "-std=c++14"; 63720b57cec5SDimitry Andric else 63730b57cec5SDimitry Andric LanguageStandard = "-std=c++11"; 63740b57cec5SDimitry Andric } 63750b57cec5SDimitry Andric 63760b57cec5SDimitry Andric CmdArgs.push_back(LanguageStandard.data()); 63770b57cec5SDimitry Andric } 63780b57cec5SDimitry Andric 63790b57cec5SDimitry Andric // -fno-borland-extensions is default. 63800b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fborland_extensions, 63810b57cec5SDimitry Andric options::OPT_fno_borland_extensions, false)) 63820b57cec5SDimitry Andric CmdArgs.push_back("-fborland-extensions"); 63830b57cec5SDimitry Andric 63840b57cec5SDimitry Andric // -fno-declspec is default, except for PS4. 63850b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec, 63860b57cec5SDimitry Andric RawTriple.isPS4())) 63870b57cec5SDimitry Andric CmdArgs.push_back("-fdeclspec"); 63880b57cec5SDimitry Andric else if (Args.hasArg(options::OPT_fno_declspec)) 63890b57cec5SDimitry Andric CmdArgs.push_back("-fno-declspec"); // Explicitly disabling __declspec. 63900b57cec5SDimitry Andric 63910b57cec5SDimitry Andric // -fthreadsafe-static is default, except for MSVC compatibility versions less 63920b57cec5SDimitry Andric // than 19. 63930b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fthreadsafe_statics, 63940b57cec5SDimitry Andric options::OPT_fno_threadsafe_statics, 6395349cc55cSDimitry Andric !types::isOpenCL(InputType) && 6396349cc55cSDimitry Andric (!IsWindowsMSVC || IsMSVC2015Compatible))) 63970b57cec5SDimitry Andric CmdArgs.push_back("-fno-threadsafe-statics"); 63980b57cec5SDimitry Andric 63990b57cec5SDimitry Andric // -fno-delayed-template-parsing is default, except when targeting MSVC. 64000b57cec5SDimitry Andric // Many old Windows SDK versions require this to parse. 64010b57cec5SDimitry Andric // FIXME: MSVC introduced /Zc:twoPhase- to disable this behavior in their 64020b57cec5SDimitry Andric // compiler. We should be able to disable this by default at some point. 64030b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdelayed_template_parsing, 64040b57cec5SDimitry Andric options::OPT_fno_delayed_template_parsing, IsWindowsMSVC)) 64050b57cec5SDimitry Andric CmdArgs.push_back("-fdelayed-template-parsing"); 64060b57cec5SDimitry Andric 64070b57cec5SDimitry Andric // -fgnu-keywords default varies depending on language; only pass if 64080b57cec5SDimitry Andric // specified. 64090b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fgnu_keywords, 64100b57cec5SDimitry Andric options::OPT_fno_gnu_keywords); 64110b57cec5SDimitry Andric 64120b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fgnu89_inline, options::OPT_fno_gnu89_inline, 64130b57cec5SDimitry Andric false)) 64140b57cec5SDimitry Andric CmdArgs.push_back("-fgnu89-inline"); 64150b57cec5SDimitry Andric 64160b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fno_inline)) 64170b57cec5SDimitry Andric CmdArgs.push_back("-fno-inline"); 64180b57cec5SDimitry Andric 64190b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_finline_functions, 64200b57cec5SDimitry Andric options::OPT_finline_hint_functions, 64210b57cec5SDimitry Andric options::OPT_fno_inline_functions); 64220b57cec5SDimitry Andric 64230b57cec5SDimitry Andric // FIXME: Find a better way to determine whether the language has modules 64240b57cec5SDimitry Andric // support by default, or just assume that all languages do. 64250b57cec5SDimitry Andric bool HaveModules = 6426e8d8bef9SDimitry Andric Std && (Std->containsValue("c++2a") || Std->containsValue("c++20") || 6427e8d8bef9SDimitry Andric Std->containsValue("c++latest")); 64280b57cec5SDimitry Andric RenderModulesOptions(C, D, Args, Input, Output, CmdArgs, HaveModules); 64290b57cec5SDimitry Andric 6430a7dea167SDimitry Andric if (Args.hasFlag(options::OPT_fpch_validate_input_files_content, 6431a7dea167SDimitry Andric options::OPT_fno_pch_validate_input_files_content, false)) 6432a7dea167SDimitry Andric CmdArgs.push_back("-fvalidate-ast-input-files-content"); 64335ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fpch_instantiate_templates, 64345ffd83dbSDimitry Andric options::OPT_fno_pch_instantiate_templates, false)) 64355ffd83dbSDimitry Andric CmdArgs.push_back("-fpch-instantiate-templates"); 6436590d96feSDimitry Andric if (Args.hasFlag(options::OPT_fpch_codegen, options::OPT_fno_pch_codegen, 6437590d96feSDimitry Andric false)) 6438590d96feSDimitry Andric CmdArgs.push_back("-fmodules-codegen"); 6439590d96feSDimitry Andric if (Args.hasFlag(options::OPT_fpch_debuginfo, options::OPT_fno_pch_debuginfo, 6440590d96feSDimitry Andric false)) 6441590d96feSDimitry Andric CmdArgs.push_back("-fmodules-debuginfo"); 6442a7dea167SDimitry Andric 6443e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_flegacy_pass_manager, 6444e8d8bef9SDimitry Andric options::OPT_fno_legacy_pass_manager); 64450b57cec5SDimitry Andric 64465ffd83dbSDimitry Andric ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, Inputs, CmdArgs, rewriteKind); 64470b57cec5SDimitry Andric RenderObjCOptions(TC, D, RawTriple, Args, Runtime, rewriteKind != RK_None, 64480b57cec5SDimitry Andric Input, CmdArgs); 64490b57cec5SDimitry Andric 6450fe6060f1SDimitry Andric if (types::isObjC(Input.getType()) && 6451fe6060f1SDimitry Andric Args.hasFlag(options::OPT_fobjc_encode_cxx_class_template_spec, 6452fe6060f1SDimitry Andric options::OPT_fno_objc_encode_cxx_class_template_spec, 6453fe6060f1SDimitry Andric !Runtime.isNeXTFamily())) 6454fe6060f1SDimitry Andric CmdArgs.push_back("-fobjc-encode-cxx-class-template-spec"); 6455fe6060f1SDimitry Andric 64560b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fapplication_extension, 64570b57cec5SDimitry Andric options::OPT_fno_application_extension, false)) 64580b57cec5SDimitry Andric CmdArgs.push_back("-fapplication-extension"); 64590b57cec5SDimitry Andric 64600b57cec5SDimitry Andric // Handle GCC-style exception args. 6461fe6060f1SDimitry Andric bool EH = false; 64620b57cec5SDimitry Andric if (!C.getDriver().IsCLMode()) 6463fe6060f1SDimitry Andric EH = addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs); 64640b57cec5SDimitry Andric 64650b57cec5SDimitry Andric // Handle exception personalities 6466a7dea167SDimitry Andric Arg *A = Args.getLastArg( 6467a7dea167SDimitry Andric options::OPT_fsjlj_exceptions, options::OPT_fseh_exceptions, 6468a7dea167SDimitry Andric options::OPT_fdwarf_exceptions, options::OPT_fwasm_exceptions); 64690b57cec5SDimitry Andric if (A) { 64700b57cec5SDimitry Andric const Option &Opt = A->getOption(); 64710b57cec5SDimitry Andric if (Opt.matches(options::OPT_fsjlj_exceptions)) 6472e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=sjlj"); 64730b57cec5SDimitry Andric if (Opt.matches(options::OPT_fseh_exceptions)) 6474e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=seh"); 64750b57cec5SDimitry Andric if (Opt.matches(options::OPT_fdwarf_exceptions)) 6476e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=dwarf"); 6477a7dea167SDimitry Andric if (Opt.matches(options::OPT_fwasm_exceptions)) 6478e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=wasm"); 64790b57cec5SDimitry Andric } else { 64800b57cec5SDimitry Andric switch (TC.GetExceptionModel(Args)) { 64810b57cec5SDimitry Andric default: 64820b57cec5SDimitry Andric break; 64830b57cec5SDimitry Andric case llvm::ExceptionHandling::DwarfCFI: 6484e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=dwarf"); 64850b57cec5SDimitry Andric break; 64860b57cec5SDimitry Andric case llvm::ExceptionHandling::SjLj: 6487e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=sjlj"); 64880b57cec5SDimitry Andric break; 64890b57cec5SDimitry Andric case llvm::ExceptionHandling::WinEH: 6490e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=seh"); 64910b57cec5SDimitry Andric break; 64920b57cec5SDimitry Andric } 64930b57cec5SDimitry Andric } 64940b57cec5SDimitry Andric 64950b57cec5SDimitry Andric // C++ "sane" operator new. 64960b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fassume_sane_operator_new, 64970b57cec5SDimitry Andric options::OPT_fno_assume_sane_operator_new)) 64980b57cec5SDimitry Andric CmdArgs.push_back("-fno-assume-sane-operator-new"); 64990b57cec5SDimitry Andric 65000b57cec5SDimitry Andric // -frelaxed-template-template-args is off by default, as it is a severe 65010b57cec5SDimitry Andric // breaking change until a corresponding change to template partial ordering 65020b57cec5SDimitry Andric // is provided. 65030b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_frelaxed_template_template_args, 65040b57cec5SDimitry Andric options::OPT_fno_relaxed_template_template_args, false)) 65050b57cec5SDimitry Andric CmdArgs.push_back("-frelaxed-template-template-args"); 65060b57cec5SDimitry Andric 65070b57cec5SDimitry Andric // -fsized-deallocation is off by default, as it is an ABI-breaking change for 65080b57cec5SDimitry Andric // most platforms. 65090b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fsized_deallocation, 65100b57cec5SDimitry Andric options::OPT_fno_sized_deallocation, false)) 65110b57cec5SDimitry Andric CmdArgs.push_back("-fsized-deallocation"); 65120b57cec5SDimitry Andric 65130b57cec5SDimitry Andric // -faligned-allocation is on by default in C++17 onwards and otherwise off 65140b57cec5SDimitry Andric // by default. 65150b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_faligned_allocation, 65160b57cec5SDimitry Andric options::OPT_fno_aligned_allocation, 65170b57cec5SDimitry Andric options::OPT_faligned_new_EQ)) { 65180b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fno_aligned_allocation)) 65190b57cec5SDimitry Andric CmdArgs.push_back("-fno-aligned-allocation"); 65200b57cec5SDimitry Andric else 65210b57cec5SDimitry Andric CmdArgs.push_back("-faligned-allocation"); 65220b57cec5SDimitry Andric } 65230b57cec5SDimitry Andric 65240b57cec5SDimitry Andric // The default new alignment can be specified using a dedicated option or via 65250b57cec5SDimitry Andric // a GCC-compatible option that also turns on aligned allocation. 65260b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fnew_alignment_EQ, 65270b57cec5SDimitry Andric options::OPT_faligned_new_EQ)) 65280b57cec5SDimitry Andric CmdArgs.push_back( 65290b57cec5SDimitry Andric Args.MakeArgString(Twine("-fnew-alignment=") + A->getValue())); 65300b57cec5SDimitry Andric 65310b57cec5SDimitry Andric // -fconstant-cfstrings is default, and may be subject to argument translation 65320b57cec5SDimitry Andric // on Darwin. 65330b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fconstant_cfstrings, 65340b57cec5SDimitry Andric options::OPT_fno_constant_cfstrings) || 65350b57cec5SDimitry Andric !Args.hasFlag(options::OPT_mconstant_cfstrings, 65360b57cec5SDimitry Andric options::OPT_mno_constant_cfstrings)) 65370b57cec5SDimitry Andric CmdArgs.push_back("-fno-constant-cfstrings"); 65380b57cec5SDimitry Andric 65390b57cec5SDimitry Andric // -fno-pascal-strings is default, only pass non-default. 65400b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fpascal_strings, 65410b57cec5SDimitry Andric options::OPT_fno_pascal_strings, false)) 65420b57cec5SDimitry Andric CmdArgs.push_back("-fpascal-strings"); 65430b57cec5SDimitry Andric 65440b57cec5SDimitry Andric // Honor -fpack-struct= and -fpack-struct, if given. Note that 65450b57cec5SDimitry Andric // -fno-pack-struct doesn't apply to -fpack-struct=. 65460b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) { 65470b57cec5SDimitry Andric std::string PackStructStr = "-fpack-struct="; 65480b57cec5SDimitry Andric PackStructStr += A->getValue(); 65490b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(PackStructStr)); 65500b57cec5SDimitry Andric } else if (Args.hasFlag(options::OPT_fpack_struct, 65510b57cec5SDimitry Andric options::OPT_fno_pack_struct, false)) { 65520b57cec5SDimitry Andric CmdArgs.push_back("-fpack-struct=1"); 65530b57cec5SDimitry Andric } 65540b57cec5SDimitry Andric 65550b57cec5SDimitry Andric // Handle -fmax-type-align=N and -fno-type-align 65560b57cec5SDimitry Andric bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align); 65570b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) { 65580b57cec5SDimitry Andric if (!SkipMaxTypeAlign) { 65590b57cec5SDimitry Andric std::string MaxTypeAlignStr = "-fmax-type-align="; 65600b57cec5SDimitry Andric MaxTypeAlignStr += A->getValue(); 65610b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr)); 65620b57cec5SDimitry Andric } 65630b57cec5SDimitry Andric } else if (RawTriple.isOSDarwin()) { 65640b57cec5SDimitry Andric if (!SkipMaxTypeAlign) { 65650b57cec5SDimitry Andric std::string MaxTypeAlignStr = "-fmax-type-align=16"; 65660b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr)); 65670b57cec5SDimitry Andric } 65680b57cec5SDimitry Andric } 65690b57cec5SDimitry Andric 65700b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_Qy, options::OPT_Qn, true)) 65710b57cec5SDimitry Andric CmdArgs.push_back("-Qn"); 65720b57cec5SDimitry Andric 65735ffd83dbSDimitry Andric // -fno-common is the default, set -fcommon only when that flag is set. 65745ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common, false)) 65755ffd83dbSDimitry Andric CmdArgs.push_back("-fcommon"); 65760b57cec5SDimitry Andric 65770b57cec5SDimitry Andric // -fsigned-bitfields is default, and clang doesn't yet support 65780b57cec5SDimitry Andric // -funsigned-bitfields. 65790b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fsigned_bitfields, 65800b57cec5SDimitry Andric options::OPT_funsigned_bitfields)) 65810b57cec5SDimitry Andric D.Diag(diag::warn_drv_clang_unsupported) 65820b57cec5SDimitry Andric << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args); 65830b57cec5SDimitry Andric 65840b57cec5SDimitry Andric // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope. 65850b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope)) 65860b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) 65870b57cec5SDimitry Andric << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args); 65880b57cec5SDimitry Andric 65890b57cec5SDimitry Andric // -finput_charset=UTF-8 is default. Reject others 65900b57cec5SDimitry Andric if (Arg *inputCharset = Args.getLastArg(options::OPT_finput_charset_EQ)) { 65910b57cec5SDimitry Andric StringRef value = inputCharset->getValue(); 6592fe6060f1SDimitry Andric if (!value.equals_insensitive("utf-8")) 65930b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args) 65940b57cec5SDimitry Andric << value; 65950b57cec5SDimitry Andric } 65960b57cec5SDimitry Andric 65970b57cec5SDimitry Andric // -fexec_charset=UTF-8 is default. Reject others 65980b57cec5SDimitry Andric if (Arg *execCharset = Args.getLastArg(options::OPT_fexec_charset_EQ)) { 65990b57cec5SDimitry Andric StringRef value = execCharset->getValue(); 6600fe6060f1SDimitry Andric if (!value.equals_insensitive("utf-8")) 66010b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args) 66020b57cec5SDimitry Andric << value; 66030b57cec5SDimitry Andric } 66040b57cec5SDimitry Andric 66050b57cec5SDimitry Andric RenderDiagnosticsOptions(D, Args, CmdArgs); 66060b57cec5SDimitry Andric 66070b57cec5SDimitry Andric // -fno-asm-blocks is default. 66080b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks, 66090b57cec5SDimitry Andric false)) 66100b57cec5SDimitry Andric CmdArgs.push_back("-fasm-blocks"); 66110b57cec5SDimitry Andric 66120b57cec5SDimitry Andric // -fgnu-inline-asm is default. 66130b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fgnu_inline_asm, 66140b57cec5SDimitry Andric options::OPT_fno_gnu_inline_asm, true)) 66150b57cec5SDimitry Andric CmdArgs.push_back("-fno-gnu-inline-asm"); 66160b57cec5SDimitry Andric 66170b57cec5SDimitry Andric // Enable vectorization per default according to the optimization level 66180b57cec5SDimitry Andric // selected. For optimization levels that want vectorization we use the alias 66190b57cec5SDimitry Andric // option to simplify the hasFlag logic. 66200b57cec5SDimitry Andric bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false); 66210b57cec5SDimitry Andric OptSpecifier VectorizeAliasOption = 66220b57cec5SDimitry Andric EnableVec ? options::OPT_O_Group : options::OPT_fvectorize; 66230b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption, 66240b57cec5SDimitry Andric options::OPT_fno_vectorize, EnableVec)) 66250b57cec5SDimitry Andric CmdArgs.push_back("-vectorize-loops"); 66260b57cec5SDimitry Andric 66270b57cec5SDimitry Andric // -fslp-vectorize is enabled based on the optimization level selected. 66280b57cec5SDimitry Andric bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true); 66290b57cec5SDimitry Andric OptSpecifier SLPVectAliasOption = 66300b57cec5SDimitry Andric EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize; 66310b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption, 66320b57cec5SDimitry Andric options::OPT_fno_slp_vectorize, EnableSLPVec)) 66330b57cec5SDimitry Andric CmdArgs.push_back("-vectorize-slp"); 66340b57cec5SDimitry Andric 66350b57cec5SDimitry Andric ParseMPreferVectorWidth(D, Args, CmdArgs); 66360b57cec5SDimitry Andric 66370b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fshow_overloads_EQ); 66380b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, 66390b57cec5SDimitry Andric options::OPT_fsanitize_undefined_strip_path_components_EQ); 66400b57cec5SDimitry Andric 66410b57cec5SDimitry Andric // -fdollars-in-identifiers default varies depending on platform and 66420b57cec5SDimitry Andric // language; only pass if specified. 66430b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers, 66440b57cec5SDimitry Andric options::OPT_fno_dollars_in_identifiers)) { 66450b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fdollars_in_identifiers)) 66460b57cec5SDimitry Andric CmdArgs.push_back("-fdollars-in-identifiers"); 66470b57cec5SDimitry Andric else 66480b57cec5SDimitry Andric CmdArgs.push_back("-fno-dollars-in-identifiers"); 66490b57cec5SDimitry Andric } 66500b57cec5SDimitry Andric 66510b57cec5SDimitry Andric // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for 66520b57cec5SDimitry Andric // practical purposes. 66530b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time, 66540b57cec5SDimitry Andric options::OPT_fno_unit_at_a_time)) { 66550b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fno_unit_at_a_time)) 66560b57cec5SDimitry Andric D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args); 66570b57cec5SDimitry Andric } 66580b57cec5SDimitry Andric 66590b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fapple_pragma_pack, 66600b57cec5SDimitry Andric options::OPT_fno_apple_pragma_pack, false)) 66610b57cec5SDimitry Andric CmdArgs.push_back("-fapple-pragma-pack"); 66620b57cec5SDimitry Andric 6663e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fxl_pragma_pack, 6664e8d8bef9SDimitry Andric options::OPT_fno_xl_pragma_pack, RawTriple.isOSAIX())) 6665e8d8bef9SDimitry Andric CmdArgs.push_back("-fxl-pragma-pack"); 6666e8d8bef9SDimitry Andric 66670b57cec5SDimitry Andric // Remarks can be enabled with any of the `-f.*optimization-record.*` flags. 6668480093f4SDimitry Andric if (willEmitRemarks(Args) && checkRemarksOptions(D, Args, Triple)) 6669480093f4SDimitry Andric renderRemarksOptions(Args, CmdArgs, Triple, Input, Output, JA); 66700b57cec5SDimitry Andric 66710b57cec5SDimitry Andric bool RewriteImports = Args.hasFlag(options::OPT_frewrite_imports, 66720b57cec5SDimitry Andric options::OPT_fno_rewrite_imports, false); 66730b57cec5SDimitry Andric if (RewriteImports) 66740b57cec5SDimitry Andric CmdArgs.push_back("-frewrite-imports"); 66750b57cec5SDimitry Andric 66760b57cec5SDimitry Andric // Enable rewrite includes if the user's asked for it or if we're generating 66770b57cec5SDimitry Andric // diagnostics. 66780b57cec5SDimitry Andric // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be 66790b57cec5SDimitry Andric // nice to enable this when doing a crashdump for modules as well. 66800b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_frewrite_includes, 66810b57cec5SDimitry Andric options::OPT_fno_rewrite_includes, false) || 66820b57cec5SDimitry Andric (C.isForDiagnostics() && !HaveModules)) 66830b57cec5SDimitry Andric CmdArgs.push_back("-frewrite-includes"); 66840b57cec5SDimitry Andric 66850b57cec5SDimitry Andric // Only allow -traditional or -traditional-cpp outside in preprocessing modes. 66860b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_traditional, 66870b57cec5SDimitry Andric options::OPT_traditional_cpp)) { 66880b57cec5SDimitry Andric if (isa<PreprocessJobAction>(JA)) 66890b57cec5SDimitry Andric CmdArgs.push_back("-traditional-cpp"); 66900b57cec5SDimitry Andric else 66910b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 66920b57cec5SDimitry Andric } 66930b57cec5SDimitry Andric 66940b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dM); 66950b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dD); 669604eeddc0SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dI); 66970b57cec5SDimitry Andric 66985ffd83dbSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmax_tokens_EQ); 66995ffd83dbSDimitry Andric 67000b57cec5SDimitry Andric // Handle serialized diagnostics. 67010b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) { 67020b57cec5SDimitry Andric CmdArgs.push_back("-serialize-diagnostic-file"); 67030b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getValue())); 67040b57cec5SDimitry Andric } 67050b57cec5SDimitry Andric 67060b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fretain_comments_from_system_headers)) 67070b57cec5SDimitry Andric CmdArgs.push_back("-fretain-comments-from-system-headers"); 67080b57cec5SDimitry Andric 67090b57cec5SDimitry Andric // Forward -fcomment-block-commands to -cc1. 67100b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands); 67110b57cec5SDimitry Andric // Forward -fparse-all-comments to -cc1. 67120b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments); 67130b57cec5SDimitry Andric 67140b57cec5SDimitry Andric // Turn -fplugin=name.so into -load name.so 67150b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fplugin_EQ)) { 67160b57cec5SDimitry Andric CmdArgs.push_back("-load"); 67170b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 67180b57cec5SDimitry Andric A->claim(); 67190b57cec5SDimitry Andric } 67200b57cec5SDimitry Andric 67214824e7fdSDimitry Andric // Turn -fplugin-arg-pluginname-key=value into 67224824e7fdSDimitry Andric // -plugin-arg-pluginname key=value 67234824e7fdSDimitry Andric // GCC has an actual plugin_argument struct with key/value pairs that it 67244824e7fdSDimitry Andric // passes to its plugins, but we don't, so just pass it on as-is. 67254824e7fdSDimitry Andric // 67264824e7fdSDimitry Andric // The syntax for -fplugin-arg- is ambiguous if both plugin name and 67274824e7fdSDimitry Andric // argument key are allowed to contain dashes. GCC therefore only 67284824e7fdSDimitry Andric // allows dashes in the key. We do the same. 67294824e7fdSDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fplugin_arg)) { 67304824e7fdSDimitry Andric auto ArgValue = StringRef(A->getValue()); 67314824e7fdSDimitry Andric auto FirstDashIndex = ArgValue.find('-'); 67324824e7fdSDimitry Andric StringRef PluginName = ArgValue.substr(0, FirstDashIndex); 67334824e7fdSDimitry Andric StringRef Arg = ArgValue.substr(FirstDashIndex + 1); 67344824e7fdSDimitry Andric 67354824e7fdSDimitry Andric A->claim(); 67364824e7fdSDimitry Andric if (FirstDashIndex == StringRef::npos || Arg.empty()) { 67374824e7fdSDimitry Andric if (PluginName.empty()) { 67384824e7fdSDimitry Andric D.Diag(diag::warn_drv_missing_plugin_name) << A->getAsString(Args); 67394824e7fdSDimitry Andric } else { 67404824e7fdSDimitry Andric D.Diag(diag::warn_drv_missing_plugin_arg) 67414824e7fdSDimitry Andric << PluginName << A->getAsString(Args); 67424824e7fdSDimitry Andric } 67434824e7fdSDimitry Andric continue; 67444824e7fdSDimitry Andric } 67454824e7fdSDimitry Andric 67464824e7fdSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-arg-") + PluginName)); 67474824e7fdSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Arg)); 67484824e7fdSDimitry Andric } 67494824e7fdSDimitry Andric 67500b57cec5SDimitry Andric // Forward -fpass-plugin=name.so to -cc1. 67510b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fpass_plugin_EQ)) { 67520b57cec5SDimitry Andric CmdArgs.push_back( 67530b57cec5SDimitry Andric Args.MakeArgString(Twine("-fpass-plugin=") + A->getValue())); 67540b57cec5SDimitry Andric A->claim(); 67550b57cec5SDimitry Andric } 67560b57cec5SDimitry Andric 67570b57cec5SDimitry Andric // Setup statistics file output. 67580b57cec5SDimitry Andric SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D); 67590b57cec5SDimitry Andric if (!StatsFile.empty()) 67600b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-stats-file=") + StatsFile)); 67610b57cec5SDimitry Andric 67620b57cec5SDimitry Andric // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option 67630b57cec5SDimitry Andric // parser. 67640b57cec5SDimitry Andric // -finclude-default-header flag is for preprocessor, 67650b57cec5SDimitry Andric // do not pass it to other cc1 commands when save-temps is enabled 67660b57cec5SDimitry Andric if (C.getDriver().isSaveTempsEnabled() && 67670b57cec5SDimitry Andric !isa<PreprocessJobAction>(JA)) { 67680b57cec5SDimitry Andric for (auto Arg : Args.filtered(options::OPT_Xclang)) { 67690b57cec5SDimitry Andric Arg->claim(); 67700b57cec5SDimitry Andric if (StringRef(Arg->getValue()) != "-finclude-default-header") 67710b57cec5SDimitry Andric CmdArgs.push_back(Arg->getValue()); 67720b57cec5SDimitry Andric } 67730b57cec5SDimitry Andric } 67740b57cec5SDimitry Andric else { 67750b57cec5SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Xclang); 67760b57cec5SDimitry Andric } 67770b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mllvm)) { 67780b57cec5SDimitry Andric A->claim(); 67790b57cec5SDimitry Andric 67800b57cec5SDimitry Andric // We translate this by hand to the -cc1 argument, since nightly test uses 67810b57cec5SDimitry Andric // it and developers have been trained to spell it with -mllvm. Both 67820b57cec5SDimitry Andric // spellings are now deprecated and should be removed. 67830b57cec5SDimitry Andric if (StringRef(A->getValue(0)) == "-disable-llvm-optzns") { 67840b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-optzns"); 67850b57cec5SDimitry Andric } else { 67860b57cec5SDimitry Andric A->render(Args, CmdArgs); 67870b57cec5SDimitry Andric } 67880b57cec5SDimitry Andric } 67890b57cec5SDimitry Andric 67900b57cec5SDimitry Andric // With -save-temps, we want to save the unoptimized bitcode output from the 67910b57cec5SDimitry Andric // CompileJobAction, use -disable-llvm-passes to get pristine IR generated 67920b57cec5SDimitry Andric // by the frontend. 67930b57cec5SDimitry Andric // When -fembed-bitcode is enabled, optimized bitcode is emitted because it 67940b57cec5SDimitry Andric // has slightly different breakdown between stages. 67950b57cec5SDimitry Andric // FIXME: -fembed-bitcode -save-temps will save optimized bitcode instead of 67960b57cec5SDimitry Andric // pristine IR generated by the frontend. Ideally, a new compile action should 67970b57cec5SDimitry Andric // be added so both IR can be captured. 67985ffd83dbSDimitry Andric if ((C.getDriver().isSaveTempsEnabled() || 67995ffd83dbSDimitry Andric JA.isHostOffloading(Action::OFK_OpenMP)) && 6800fe6060f1SDimitry Andric !(C.getDriver().embedBitcodeInObject() && !IsUsingLTO) && 68010b57cec5SDimitry Andric isa<CompileJobAction>(JA)) 68020b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-passes"); 68030b57cec5SDimitry Andric 68040b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_undef); 68050b57cec5SDimitry Andric 68060b57cec5SDimitry Andric const char *Exec = D.getClangProgramPath(); 68070b57cec5SDimitry Andric 68080b57cec5SDimitry Andric // Optionally embed the -cc1 level arguments into the debug info or a 68090b57cec5SDimitry Andric // section, for build analysis. 68100b57cec5SDimitry Andric // Also record command line arguments into the debug info if 68110b57cec5SDimitry Andric // -grecord-gcc-switches options is set on. 68120b57cec5SDimitry Andric // By default, -gno-record-gcc-switches is set on and no recording. 68130b57cec5SDimitry Andric auto GRecordSwitches = 68140b57cec5SDimitry Andric Args.hasFlag(options::OPT_grecord_command_line, 68150b57cec5SDimitry Andric options::OPT_gno_record_command_line, false); 68160b57cec5SDimitry Andric auto FRecordSwitches = 68170b57cec5SDimitry Andric Args.hasFlag(options::OPT_frecord_command_line, 68180b57cec5SDimitry Andric options::OPT_fno_record_command_line, false); 68190b57cec5SDimitry Andric if (FRecordSwitches && !Triple.isOSBinFormatELF()) 68200b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 68210b57cec5SDimitry Andric << Args.getLastArg(options::OPT_frecord_command_line)->getAsString(Args) 68220b57cec5SDimitry Andric << TripleStr; 68230b57cec5SDimitry Andric if (TC.UseDwarfDebugFlags() || GRecordSwitches || FRecordSwitches) { 68240b57cec5SDimitry Andric ArgStringList OriginalArgs; 68250b57cec5SDimitry Andric for (const auto &Arg : Args) 68260b57cec5SDimitry Andric Arg->render(Args, OriginalArgs); 68270b57cec5SDimitry Andric 68280b57cec5SDimitry Andric SmallString<256> Flags; 68295ffd83dbSDimitry Andric EscapeSpacesAndBackslashes(Exec, Flags); 68300b57cec5SDimitry Andric for (const char *OriginalArg : OriginalArgs) { 68310b57cec5SDimitry Andric SmallString<128> EscapedArg; 68320b57cec5SDimitry Andric EscapeSpacesAndBackslashes(OriginalArg, EscapedArg); 68330b57cec5SDimitry Andric Flags += " "; 68340b57cec5SDimitry Andric Flags += EscapedArg; 68350b57cec5SDimitry Andric } 68360b57cec5SDimitry Andric auto FlagsArgString = Args.MakeArgString(Flags); 68370b57cec5SDimitry Andric if (TC.UseDwarfDebugFlags() || GRecordSwitches) { 68380b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-flags"); 68390b57cec5SDimitry Andric CmdArgs.push_back(FlagsArgString); 68400b57cec5SDimitry Andric } 68410b57cec5SDimitry Andric if (FRecordSwitches) { 68420b57cec5SDimitry Andric CmdArgs.push_back("-record-command-line"); 68430b57cec5SDimitry Andric CmdArgs.push_back(FlagsArgString); 68440b57cec5SDimitry Andric } 68450b57cec5SDimitry Andric } 68460b57cec5SDimitry Andric 68470b57cec5SDimitry Andric // Host-side cuda compilation receives all device-side outputs in a single 68480b57cec5SDimitry Andric // fatbin as Inputs[1]. Include the binary with -fcuda-include-gpubinary. 68490b57cec5SDimitry Andric if ((IsCuda || IsHIP) && CudaDeviceInput) { 68500b57cec5SDimitry Andric CmdArgs.push_back("-fcuda-include-gpubinary"); 68510b57cec5SDimitry Andric CmdArgs.push_back(CudaDeviceInput->getFilename()); 68520b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false)) 68530b57cec5SDimitry Andric CmdArgs.push_back("-fgpu-rdc"); 68540b57cec5SDimitry Andric } 68550b57cec5SDimitry Andric 68560b57cec5SDimitry Andric if (IsCuda) { 68570b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcuda_short_ptr, 68580b57cec5SDimitry Andric options::OPT_fno_cuda_short_ptr, false)) 68590b57cec5SDimitry Andric CmdArgs.push_back("-fcuda-short-ptr"); 68600b57cec5SDimitry Andric } 68610b57cec5SDimitry Andric 6862fe6060f1SDimitry Andric if (IsCuda || IsHIP) { 6863fe6060f1SDimitry Andric // Determine the original source input. 6864fe6060f1SDimitry Andric const Action *SourceAction = &JA; 6865fe6060f1SDimitry Andric while (SourceAction->getKind() != Action::InputClass) { 6866fe6060f1SDimitry Andric assert(!SourceAction->getInputs().empty() && "unexpected root action!"); 6867fe6060f1SDimitry Andric SourceAction = SourceAction->getInputs()[0]; 6868fe6060f1SDimitry Andric } 6869fe6060f1SDimitry Andric auto CUID = cast<InputAction>(SourceAction)->getId(); 6870fe6060f1SDimitry Andric if (!CUID.empty()) 6871fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-cuid=") + Twine(CUID))); 6872fe6060f1SDimitry Andric } 6873fe6060f1SDimitry Andric 6874480093f4SDimitry Andric if (IsHIP) 6875480093f4SDimitry Andric CmdArgs.push_back("-fcuda-allow-variadic-functions"); 6876480093f4SDimitry Andric 6877fe6060f1SDimitry Andric if (IsCudaDevice || IsHIPDevice) { 6878fe6060f1SDimitry Andric StringRef InlineThresh = 6879fe6060f1SDimitry Andric Args.getLastArgValue(options::OPT_fgpu_inline_threshold_EQ); 6880fe6060f1SDimitry Andric if (!InlineThresh.empty()) { 6881fe6060f1SDimitry Andric std::string ArgStr = 6882fe6060f1SDimitry Andric std::string("-inline-threshold=") + InlineThresh.str(); 6883fe6060f1SDimitry Andric CmdArgs.append({"-mllvm", Args.MakeArgStringRef(ArgStr)}); 6884fe6060f1SDimitry Andric } 6885fe6060f1SDimitry Andric } 6886fe6060f1SDimitry Andric 68870b57cec5SDimitry Andric // OpenMP offloading device jobs take the argument -fopenmp-host-ir-file-path 68880b57cec5SDimitry Andric // to specify the result of the compile phase on the host, so the meaningful 68890b57cec5SDimitry Andric // device declarations can be identified. Also, -fopenmp-is-device is passed 68900b57cec5SDimitry Andric // along to tell the frontend that it is generating code for a device, so that 68910b57cec5SDimitry Andric // only the relevant declarations are emitted. 68920b57cec5SDimitry Andric if (IsOpenMPDevice) { 68930b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-is-device"); 68940b57cec5SDimitry Andric if (OpenMPDeviceInput) { 68950b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-host-ir-file-path"); 68960b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OpenMPDeviceInput->getFilename())); 68970b57cec5SDimitry Andric } 68980b57cec5SDimitry Andric } 68990b57cec5SDimitry Andric 69001fd87a68SDimitry Andric // Host-side OpenMP offloading recieves the device object files and embeds it 69011fd87a68SDimitry Andric // in a named section including the associated target triple and architecture. 69021fd87a68SDimitry Andric if (IsOpenMPHost && !OpenMPHostInputs.empty()) { 69031fd87a68SDimitry Andric auto InputFile = OpenMPHostInputs.begin(); 69041fd87a68SDimitry Andric auto OpenMPTCs = C.getOffloadToolChains<Action::OFK_OpenMP>(); 69051fd87a68SDimitry Andric for (auto TI = OpenMPTCs.first, TE = OpenMPTCs.second; TI != TE; 69061fd87a68SDimitry Andric ++TI, ++InputFile) { 69071fd87a68SDimitry Andric const ToolChain *TC = TI->second; 69081fd87a68SDimitry Andric const ArgList &TCArgs = C.getArgsForToolChain(TC, "", Action::OFK_OpenMP); 69091fd87a68SDimitry Andric StringRef File = 69101fd87a68SDimitry Andric C.getArgs().MakeArgString(TC->getInputFilename(*InputFile)); 69111fd87a68SDimitry Andric StringRef InputName = Clang::getBaseInputStem(Args, Inputs); 69121fd87a68SDimitry Andric 69131fd87a68SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 69141fd87a68SDimitry Andric "-fembed-offload-object=" + File + "," + TC->getTripleString() + "." + 69151fd87a68SDimitry Andric TCArgs.getLastArgValue(options::OPT_march_EQ) + "." + InputName)); 69161fd87a68SDimitry Andric } 69171fd87a68SDimitry Andric } 69181fd87a68SDimitry Andric 6919e8d8bef9SDimitry Andric if (Triple.isAMDGPU()) { 6920e8d8bef9SDimitry Andric handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs); 6921e8d8bef9SDimitry Andric 6922e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_munsafe_fp_atomics, 6923fe6060f1SDimitry Andric options::OPT_mno_unsafe_fp_atomics, /*Default=*/false)) 6924e8d8bef9SDimitry Andric CmdArgs.push_back("-munsafe-fp-atomics"); 6925e8d8bef9SDimitry Andric } 6926e8d8bef9SDimitry Andric 69270b57cec5SDimitry Andric // For all the host OpenMP offloading compile jobs we need to pass the targets 69280b57cec5SDimitry Andric // information using -fopenmp-targets= option. 69290b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_OpenMP)) { 69300b57cec5SDimitry Andric SmallString<128> TargetInfo("-fopenmp-targets="); 69310b57cec5SDimitry Andric 69320b57cec5SDimitry Andric Arg *Tgts = Args.getLastArg(options::OPT_fopenmp_targets_EQ); 69330b57cec5SDimitry Andric assert(Tgts && Tgts->getNumValues() && 69340b57cec5SDimitry Andric "OpenMP offloading has to have targets specified."); 69350b57cec5SDimitry Andric for (unsigned i = 0; i < Tgts->getNumValues(); ++i) { 69360b57cec5SDimitry Andric if (i) 69370b57cec5SDimitry Andric TargetInfo += ','; 69380b57cec5SDimitry Andric // We need to get the string from the triple because it may be not exactly 69390b57cec5SDimitry Andric // the same as the one we get directly from the arguments. 69400b57cec5SDimitry Andric llvm::Triple T(Tgts->getValue(i)); 69410b57cec5SDimitry Andric TargetInfo += T.getTriple(); 69420b57cec5SDimitry Andric } 69430b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TargetInfo.str())); 69440b57cec5SDimitry Andric } 69450b57cec5SDimitry Andric 6946a7dea167SDimitry Andric bool VirtualFunctionElimination = 6947a7dea167SDimitry Andric Args.hasFlag(options::OPT_fvirtual_function_elimination, 6948a7dea167SDimitry Andric options::OPT_fno_virtual_function_elimination, false); 6949a7dea167SDimitry Andric if (VirtualFunctionElimination) { 6950a7dea167SDimitry Andric // VFE requires full LTO (currently, this might be relaxed to allow ThinLTO 6951a7dea167SDimitry Andric // in the future). 6952fe6060f1SDimitry Andric if (LTOMode != LTOK_Full) 6953a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 6954a7dea167SDimitry Andric << "-fvirtual-function-elimination" 6955a7dea167SDimitry Andric << "-flto=full"; 6956a7dea167SDimitry Andric 6957a7dea167SDimitry Andric CmdArgs.push_back("-fvirtual-function-elimination"); 6958a7dea167SDimitry Andric } 6959a7dea167SDimitry Andric 6960a7dea167SDimitry Andric // VFE requires whole-program-vtables, and enables it by default. 6961a7dea167SDimitry Andric bool WholeProgramVTables = Args.hasFlag( 6962a7dea167SDimitry Andric options::OPT_fwhole_program_vtables, 6963a7dea167SDimitry Andric options::OPT_fno_whole_program_vtables, VirtualFunctionElimination); 6964a7dea167SDimitry Andric if (VirtualFunctionElimination && !WholeProgramVTables) { 6965a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 6966a7dea167SDimitry Andric << "-fno-whole-program-vtables" 6967a7dea167SDimitry Andric << "-fvirtual-function-elimination"; 6968a7dea167SDimitry Andric } 6969a7dea167SDimitry Andric 69700b57cec5SDimitry Andric if (WholeProgramVTables) { 6971fe6060f1SDimitry Andric // Propagate -fwhole-program-vtables if this is an LTO compile. 6972fe6060f1SDimitry Andric if (IsUsingLTO) 6973fe6060f1SDimitry Andric CmdArgs.push_back("-fwhole-program-vtables"); 6974fe6060f1SDimitry Andric // Check if we passed LTO options but they were suppressed because this is a 6975fe6060f1SDimitry Andric // device offloading action, or we passed device offload LTO options which 6976fe6060f1SDimitry Andric // were suppressed because this is not the device offload action. 6977fe6060f1SDimitry Andric // Otherwise, issue an error. 6978fe6060f1SDimitry Andric else if (!D.isUsingLTO(!IsDeviceOffloadAction)) 69790b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 69800b57cec5SDimitry Andric << "-fwhole-program-vtables" 69810b57cec5SDimitry Andric << "-flto"; 69820b57cec5SDimitry Andric } 69830b57cec5SDimitry Andric 6984480093f4SDimitry Andric bool DefaultsSplitLTOUnit = 6985349cc55cSDimitry Andric (WholeProgramVTables || SanitizeArgs.needsLTO()) && 6986fe6060f1SDimitry Andric (LTOMode == LTOK_Full || TC.canSplitThinLTOUnit()); 69870b57cec5SDimitry Andric bool SplitLTOUnit = 69880b57cec5SDimitry Andric Args.hasFlag(options::OPT_fsplit_lto_unit, 6989a7dea167SDimitry Andric options::OPT_fno_split_lto_unit, DefaultsSplitLTOUnit); 6990349cc55cSDimitry Andric if (SanitizeArgs.needsLTO() && !SplitLTOUnit) 6991a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "-fno-split-lto-unit" 6992a7dea167SDimitry Andric << "-fsanitize=cfi"; 69930b57cec5SDimitry Andric if (SplitLTOUnit) 69940b57cec5SDimitry Andric CmdArgs.push_back("-fsplit-lto-unit"); 69950b57cec5SDimitry Andric 69965ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fglobal_isel, 69975ffd83dbSDimitry Andric options::OPT_fno_global_isel)) { 69980b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 69995ffd83dbSDimitry Andric if (A->getOption().matches(options::OPT_fglobal_isel)) { 70000b57cec5SDimitry Andric CmdArgs.push_back("-global-isel=1"); 70010b57cec5SDimitry Andric 70020b57cec5SDimitry Andric // GISel is on by default on AArch64 -O0, so don't bother adding 70030b57cec5SDimitry Andric // the fallback remarks for it. Other combinations will add a warning of 70040b57cec5SDimitry Andric // some kind. 70050b57cec5SDimitry Andric bool IsArchSupported = Triple.getArch() == llvm::Triple::aarch64; 70060b57cec5SDimitry Andric bool IsOptLevelSupported = false; 70070b57cec5SDimitry Andric 70080b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_O_Group); 70090b57cec5SDimitry Andric if (Triple.getArch() == llvm::Triple::aarch64) { 70100b57cec5SDimitry Andric if (!A || A->getOption().matches(options::OPT_O0)) 70110b57cec5SDimitry Andric IsOptLevelSupported = true; 70120b57cec5SDimitry Andric } 70130b57cec5SDimitry Andric if (!IsArchSupported || !IsOptLevelSupported) { 70140b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 70150b57cec5SDimitry Andric CmdArgs.push_back("-global-isel-abort=2"); 70160b57cec5SDimitry Andric 70170b57cec5SDimitry Andric if (!IsArchSupported) 70185ffd83dbSDimitry Andric D.Diag(diag::warn_drv_global_isel_incomplete) << Triple.getArchName(); 70190b57cec5SDimitry Andric else 70205ffd83dbSDimitry Andric D.Diag(diag::warn_drv_global_isel_incomplete_opt); 70210b57cec5SDimitry Andric } 70220b57cec5SDimitry Andric } else { 70230b57cec5SDimitry Andric CmdArgs.push_back("-global-isel=0"); 70240b57cec5SDimitry Andric } 70250b57cec5SDimitry Andric } 70260b57cec5SDimitry Andric 70270b57cec5SDimitry Andric if (Args.hasArg(options::OPT_forder_file_instrumentation)) { 70280b57cec5SDimitry Andric CmdArgs.push_back("-forder-file-instrumentation"); 70290b57cec5SDimitry Andric // Enable order file instrumentation when ThinLTO is not on. When ThinLTO is 70300b57cec5SDimitry Andric // on, we need to pass these flags as linker flags and that will be handled 70310b57cec5SDimitry Andric // outside of the compiler. 7032fe6060f1SDimitry Andric if (!IsUsingLTO) { 70330b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 70340b57cec5SDimitry Andric CmdArgs.push_back("-enable-order-file-instrumentation"); 70350b57cec5SDimitry Andric } 70360b57cec5SDimitry Andric } 70370b57cec5SDimitry Andric 70380b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fforce_enable_int128, 70390b57cec5SDimitry Andric options::OPT_fno_force_enable_int128)) { 70400b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fforce_enable_int128)) 70410b57cec5SDimitry Andric CmdArgs.push_back("-fforce-enable-int128"); 70420b57cec5SDimitry Andric } 70430b57cec5SDimitry Andric 70445ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fkeep_static_consts, 70455ffd83dbSDimitry Andric options::OPT_fno_keep_static_consts, false)) 70465ffd83dbSDimitry Andric CmdArgs.push_back("-fkeep-static-consts"); 70475ffd83dbSDimitry Andric 70480b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcomplete_member_pointers, 70490b57cec5SDimitry Andric options::OPT_fno_complete_member_pointers, false)) 70500b57cec5SDimitry Andric CmdArgs.push_back("-fcomplete-member-pointers"); 70510b57cec5SDimitry Andric 70520b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fcxx_static_destructors, 70530b57cec5SDimitry Andric options::OPT_fno_cxx_static_destructors, true)) 70540b57cec5SDimitry Andric CmdArgs.push_back("-fno-c++-static-destructors"); 70550b57cec5SDimitry Andric 7056e8d8bef9SDimitry Andric addMachineOutlinerArgs(D, Args, CmdArgs, Triple, /*IsLTO=*/false); 7057e8d8bef9SDimitry Andric 7058e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_moutline_atomics, 7059e8d8bef9SDimitry Andric options::OPT_mno_outline_atomics)) { 7060e8d8bef9SDimitry Andric // Option -moutline-atomics supported for AArch64 target only. 7061e8d8bef9SDimitry Andric if (!Triple.isAArch64()) { 7062e8d8bef9SDimitry Andric D.Diag(diag::warn_drv_moutline_atomics_unsupported_opt) 70630eae32dcSDimitry Andric << Triple.getArchName() << A->getOption().getName(); 70640b57cec5SDimitry Andric } else { 70650eae32dcSDimitry Andric if (A->getOption().matches(options::OPT_moutline_atomics)) { 7066e8d8bef9SDimitry Andric CmdArgs.push_back("-target-feature"); 7067e8d8bef9SDimitry Andric CmdArgs.push_back("+outline-atomics"); 70680b57cec5SDimitry Andric } else { 7069e8d8bef9SDimitry Andric CmdArgs.push_back("-target-feature"); 7070e8d8bef9SDimitry Andric CmdArgs.push_back("-outline-atomics"); 70710b57cec5SDimitry Andric } 70720eae32dcSDimitry Andric } 7073fe6060f1SDimitry Andric } else if (Triple.isAArch64() && 7074fe6060f1SDimitry Andric getToolChain().IsAArch64OutlineAtomicsDefault(Args)) { 7075fe6060f1SDimitry Andric CmdArgs.push_back("-target-feature"); 7076fe6060f1SDimitry Andric CmdArgs.push_back("+outline-atomics"); 70770b57cec5SDimitry Andric } 70780b57cec5SDimitry Andric 70790b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_faddrsig, options::OPT_fno_addrsig, 70800b57cec5SDimitry Andric (TC.getTriple().isOSBinFormatELF() || 70810b57cec5SDimitry Andric TC.getTriple().isOSBinFormatCOFF()) && 7082e8d8bef9SDimitry Andric !TC.getTriple().isPS4() && !TC.getTriple().isVE() && 70830b57cec5SDimitry Andric !TC.getTriple().isOSNetBSD() && 7084480093f4SDimitry Andric !Distro(D.getVFS(), TC.getTriple()).IsGentoo() && 7085e8d8bef9SDimitry Andric !TC.getTriple().isAndroid() && TC.useIntegratedAs())) 70860b57cec5SDimitry Andric CmdArgs.push_back("-faddrsig"); 70870b57cec5SDimitry Andric 7088fe6060f1SDimitry Andric if ((Triple.isOSBinFormatELF() || Triple.isOSBinFormatMachO()) && 7089349cc55cSDimitry Andric (EH || AsyncUnwindTables || UnwindTables || 7090349cc55cSDimitry Andric DebugInfoKind != codegenoptions::NoDebugInfo)) 7091fe6060f1SDimitry Andric CmdArgs.push_back("-D__GCC_HAVE_DWARF2_CFI_ASM=1"); 7092fe6060f1SDimitry Andric 70930b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fsymbol_partition_EQ)) { 70940b57cec5SDimitry Andric std::string Str = A->getAsString(Args); 70950b57cec5SDimitry Andric if (!TC.getTriple().isOSBinFormatELF()) 70960b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 70970b57cec5SDimitry Andric << Str << TC.getTripleString(); 70980b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Str)); 70990b57cec5SDimitry Andric } 71000b57cec5SDimitry Andric 71010eae32dcSDimitry Andric // Add the output path to the object file for CodeView debug infos. 71020eae32dcSDimitry Andric if (EmitCodeView && Output.isFilename()) 71030eae32dcSDimitry Andric addDebugObjectName(Args, CmdArgs, DebugCompilationDir, 71040eae32dcSDimitry Andric Output.getFilename()); 71050eae32dcSDimitry Andric 71060b57cec5SDimitry Andric // Add the "-o out -x type src.c" flags last. This is done primarily to make 71070b57cec5SDimitry Andric // the -cc1 command easier to edit when reproducing compiler crashes. 71080b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) { 71090b57cec5SDimitry Andric // Handled with other dependency code. 71100b57cec5SDimitry Andric } else if (Output.isFilename()) { 7111480093f4SDimitry Andric if (Output.getType() == clang::driver::types::TY_IFS_CPP || 7112480093f4SDimitry Andric Output.getType() == clang::driver::types::TY_IFS) { 7113480093f4SDimitry Andric SmallString<128> OutputFilename(Output.getFilename()); 7114480093f4SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, "ifs"); 7115480093f4SDimitry Andric CmdArgs.push_back("-o"); 7116480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OutputFilename)); 7117480093f4SDimitry Andric } else { 71180b57cec5SDimitry Andric CmdArgs.push_back("-o"); 71190b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 7120480093f4SDimitry Andric } 71210b57cec5SDimitry Andric } else { 71220b57cec5SDimitry Andric assert(Output.isNothing() && "Invalid output."); 71230b57cec5SDimitry Andric } 71240b57cec5SDimitry Andric 71250b57cec5SDimitry Andric addDashXForInput(Args, Input, CmdArgs); 71260b57cec5SDimitry Andric 71270b57cec5SDimitry Andric ArrayRef<InputInfo> FrontendInputs = Input; 71280b57cec5SDimitry Andric if (IsHeaderModulePrecompile) 71290b57cec5SDimitry Andric FrontendInputs = ModuleHeaderInputs; 71300b57cec5SDimitry Andric else if (Input.isNothing()) 71310b57cec5SDimitry Andric FrontendInputs = {}; 71320b57cec5SDimitry Andric 71330b57cec5SDimitry Andric for (const InputInfo &Input : FrontendInputs) { 71340b57cec5SDimitry Andric if (Input.isFilename()) 71350b57cec5SDimitry Andric CmdArgs.push_back(Input.getFilename()); 71360b57cec5SDimitry Andric else 71370b57cec5SDimitry Andric Input.getInputArg().renderAsInput(Args, CmdArgs); 71380b57cec5SDimitry Andric } 71390b57cec5SDimitry Andric 7140fe6060f1SDimitry Andric if (D.CC1Main && !D.CCGenDiagnostics) { 7141480093f4SDimitry Andric // Invoke the CC1 directly in this process 7142e8d8bef9SDimitry Andric C.addCommand(std::make_unique<CC1Command>(JA, *this, 7143e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 7144e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 71450b57cec5SDimitry Andric } else { 7146e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, 7147e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 7148e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 71490b57cec5SDimitry Andric } 71500b57cec5SDimitry Andric 71510b57cec5SDimitry Andric // Make the compile command echo its inputs for /showFilenames. 71520b57cec5SDimitry Andric if (Output.getType() == types::TY_Object && 71530b57cec5SDimitry Andric Args.hasFlag(options::OPT__SLASH_showFilenames, 71540b57cec5SDimitry Andric options::OPT__SLASH_showFilenames_, false)) { 715513138422SDimitry Andric C.getJobs().getJobs().back()->PrintInputFilenames = true; 71560b57cec5SDimitry Andric } 71570b57cec5SDimitry Andric 71580b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_pg)) 71595ffd83dbSDimitry Andric if (FPKeepKind == CodeGenOptions::FramePointerKind::None && 71605ffd83dbSDimitry Andric !Args.hasArg(options::OPT_mfentry)) 71610b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "-fomit-frame-pointer" 71620b57cec5SDimitry Andric << A->getAsString(Args); 71630b57cec5SDimitry Andric 71640b57cec5SDimitry Andric // Claim some arguments which clang supports automatically. 71650b57cec5SDimitry Andric 71660b57cec5SDimitry Andric // -fpch-preprocess is used with gcc to add a special marker in the output to 71670b57cec5SDimitry Andric // include the PCH file. 71680b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fpch_preprocess); 71690b57cec5SDimitry Andric 71700b57cec5SDimitry Andric // Claim some arguments which clang doesn't support, but we don't 71710b57cec5SDimitry Andric // care to warn the user about. 71720b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group); 71730b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group); 71740b57cec5SDimitry Andric 71750b57cec5SDimitry Andric // Disable warnings for clang -E -emit-llvm foo.c 71760b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_emit_llvm); 71770b57cec5SDimitry Andric } 71780b57cec5SDimitry Andric 71790eae32dcSDimitry Andric Clang::Clang(const ToolChain &TC, bool HasIntegratedBackend) 71800b57cec5SDimitry Andric // CAUTION! The first constructor argument ("clang") is not arbitrary, 71810b57cec5SDimitry Andric // as it is for other tools. Some operations on a Tool actually test 71820b57cec5SDimitry Andric // whether that tool is Clang based on the Tool's Name as a string. 71830eae32dcSDimitry Andric : Tool("clang", "clang frontend", TC), HasBackend(HasIntegratedBackend) {} 71840b57cec5SDimitry Andric 71850b57cec5SDimitry Andric Clang::~Clang() {} 71860b57cec5SDimitry Andric 71870b57cec5SDimitry Andric /// Add options related to the Objective-C runtime/ABI. 71880b57cec5SDimitry Andric /// 71890b57cec5SDimitry Andric /// Returns true if the runtime is non-fragile. 71900b57cec5SDimitry Andric ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args, 71915ffd83dbSDimitry Andric const InputInfoList &inputs, 71920b57cec5SDimitry Andric ArgStringList &cmdArgs, 71930b57cec5SDimitry Andric RewriteKind rewriteKind) const { 71940b57cec5SDimitry Andric // Look for the controlling runtime option. 71950b57cec5SDimitry Andric Arg *runtimeArg = 71960b57cec5SDimitry Andric args.getLastArg(options::OPT_fnext_runtime, options::OPT_fgnu_runtime, 71970b57cec5SDimitry Andric options::OPT_fobjc_runtime_EQ); 71980b57cec5SDimitry Andric 71990b57cec5SDimitry Andric // Just forward -fobjc-runtime= to the frontend. This supercedes 72000b57cec5SDimitry Andric // options about fragility. 72010b57cec5SDimitry Andric if (runtimeArg && 72020b57cec5SDimitry Andric runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) { 72030b57cec5SDimitry Andric ObjCRuntime runtime; 72040b57cec5SDimitry Andric StringRef value = runtimeArg->getValue(); 72050b57cec5SDimitry Andric if (runtime.tryParse(value)) { 72060b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime) 72070b57cec5SDimitry Andric << value; 72080b57cec5SDimitry Andric } 72090b57cec5SDimitry Andric if ((runtime.getKind() == ObjCRuntime::GNUstep) && 72100b57cec5SDimitry Andric (runtime.getVersion() >= VersionTuple(2, 0))) 72110b57cec5SDimitry Andric if (!getToolChain().getTriple().isOSBinFormatELF() && 72120b57cec5SDimitry Andric !getToolChain().getTriple().isOSBinFormatCOFF()) { 72130b57cec5SDimitry Andric getToolChain().getDriver().Diag( 72140b57cec5SDimitry Andric diag::err_drv_gnustep_objc_runtime_incompatible_binary) 72150b57cec5SDimitry Andric << runtime.getVersion().getMajor(); 72160b57cec5SDimitry Andric } 72170b57cec5SDimitry Andric 72180b57cec5SDimitry Andric runtimeArg->render(args, cmdArgs); 72190b57cec5SDimitry Andric return runtime; 72200b57cec5SDimitry Andric } 72210b57cec5SDimitry Andric 72220b57cec5SDimitry Andric // Otherwise, we'll need the ABI "version". Version numbers are 72230b57cec5SDimitry Andric // slightly confusing for historical reasons: 72240b57cec5SDimitry Andric // 1 - Traditional "fragile" ABI 72250b57cec5SDimitry Andric // 2 - Non-fragile ABI, version 1 72260b57cec5SDimitry Andric // 3 - Non-fragile ABI, version 2 72270b57cec5SDimitry Andric unsigned objcABIVersion = 1; 72280b57cec5SDimitry Andric // If -fobjc-abi-version= is present, use that to set the version. 72290b57cec5SDimitry Andric if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) { 72300b57cec5SDimitry Andric StringRef value = abiArg->getValue(); 72310b57cec5SDimitry Andric if (value == "1") 72320b57cec5SDimitry Andric objcABIVersion = 1; 72330b57cec5SDimitry Andric else if (value == "2") 72340b57cec5SDimitry Andric objcABIVersion = 2; 72350b57cec5SDimitry Andric else if (value == "3") 72360b57cec5SDimitry Andric objcABIVersion = 3; 72370b57cec5SDimitry Andric else 72380b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) << value; 72390b57cec5SDimitry Andric } else { 72400b57cec5SDimitry Andric // Otherwise, determine if we are using the non-fragile ABI. 72410b57cec5SDimitry Andric bool nonFragileABIIsDefault = 72420b57cec5SDimitry Andric (rewriteKind == RK_NonFragile || 72430b57cec5SDimitry Andric (rewriteKind == RK_None && 72440b57cec5SDimitry Andric getToolChain().IsObjCNonFragileABIDefault())); 72450b57cec5SDimitry Andric if (args.hasFlag(options::OPT_fobjc_nonfragile_abi, 72460b57cec5SDimitry Andric options::OPT_fno_objc_nonfragile_abi, 72470b57cec5SDimitry Andric nonFragileABIIsDefault)) { 72480b57cec5SDimitry Andric // Determine the non-fragile ABI version to use. 72490b57cec5SDimitry Andric #ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO 72500b57cec5SDimitry Andric unsigned nonFragileABIVersion = 1; 72510b57cec5SDimitry Andric #else 72520b57cec5SDimitry Andric unsigned nonFragileABIVersion = 2; 72530b57cec5SDimitry Andric #endif 72540b57cec5SDimitry Andric 72550b57cec5SDimitry Andric if (Arg *abiArg = 72560b57cec5SDimitry Andric args.getLastArg(options::OPT_fobjc_nonfragile_abi_version_EQ)) { 72570b57cec5SDimitry Andric StringRef value = abiArg->getValue(); 72580b57cec5SDimitry Andric if (value == "1") 72590b57cec5SDimitry Andric nonFragileABIVersion = 1; 72600b57cec5SDimitry Andric else if (value == "2") 72610b57cec5SDimitry Andric nonFragileABIVersion = 2; 72620b57cec5SDimitry Andric else 72630b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) 72640b57cec5SDimitry Andric << value; 72650b57cec5SDimitry Andric } 72660b57cec5SDimitry Andric 72670b57cec5SDimitry Andric objcABIVersion = 1 + nonFragileABIVersion; 72680b57cec5SDimitry Andric } else { 72690b57cec5SDimitry Andric objcABIVersion = 1; 72700b57cec5SDimitry Andric } 72710b57cec5SDimitry Andric } 72720b57cec5SDimitry Andric 72730b57cec5SDimitry Andric // We don't actually care about the ABI version other than whether 72740b57cec5SDimitry Andric // it's non-fragile. 72750b57cec5SDimitry Andric bool isNonFragile = objcABIVersion != 1; 72760b57cec5SDimitry Andric 72770b57cec5SDimitry Andric // If we have no runtime argument, ask the toolchain for its default runtime. 72780b57cec5SDimitry Andric // However, the rewriter only really supports the Mac runtime, so assume that. 72790b57cec5SDimitry Andric ObjCRuntime runtime; 72800b57cec5SDimitry Andric if (!runtimeArg) { 72810b57cec5SDimitry Andric switch (rewriteKind) { 72820b57cec5SDimitry Andric case RK_None: 72830b57cec5SDimitry Andric runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 72840b57cec5SDimitry Andric break; 72850b57cec5SDimitry Andric case RK_Fragile: 72860b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple()); 72870b57cec5SDimitry Andric break; 72880b57cec5SDimitry Andric case RK_NonFragile: 72890b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 72900b57cec5SDimitry Andric break; 72910b57cec5SDimitry Andric } 72920b57cec5SDimitry Andric 72930b57cec5SDimitry Andric // -fnext-runtime 72940b57cec5SDimitry Andric } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) { 72950b57cec5SDimitry Andric // On Darwin, make this use the default behavior for the toolchain. 72960b57cec5SDimitry Andric if (getToolChain().getTriple().isOSDarwin()) { 72970b57cec5SDimitry Andric runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 72980b57cec5SDimitry Andric 72990b57cec5SDimitry Andric // Otherwise, build for a generic macosx port. 73000b57cec5SDimitry Andric } else { 73010b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 73020b57cec5SDimitry Andric } 73030b57cec5SDimitry Andric 73040b57cec5SDimitry Andric // -fgnu-runtime 73050b57cec5SDimitry Andric } else { 73060b57cec5SDimitry Andric assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime)); 73070b57cec5SDimitry Andric // Legacy behaviour is to target the gnustep runtime if we are in 73080b57cec5SDimitry Andric // non-fragile mode or the GCC runtime in fragile mode. 73090b57cec5SDimitry Andric if (isNonFragile) 73100b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(2, 0)); 73110b57cec5SDimitry Andric else 73120b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple()); 73130b57cec5SDimitry Andric } 73140b57cec5SDimitry Andric 73155ffd83dbSDimitry Andric if (llvm::any_of(inputs, [](const InputInfo &input) { 73165ffd83dbSDimitry Andric return types::isObjC(input.getType()); 73175ffd83dbSDimitry Andric })) 73180b57cec5SDimitry Andric cmdArgs.push_back( 73190b57cec5SDimitry Andric args.MakeArgString("-fobjc-runtime=" + runtime.getAsString())); 73200b57cec5SDimitry Andric return runtime; 73210b57cec5SDimitry Andric } 73220b57cec5SDimitry Andric 73230b57cec5SDimitry Andric static bool maybeConsumeDash(const std::string &EH, size_t &I) { 73240b57cec5SDimitry Andric bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-'); 73250b57cec5SDimitry Andric I += HaveDash; 73260b57cec5SDimitry Andric return !HaveDash; 73270b57cec5SDimitry Andric } 73280b57cec5SDimitry Andric 73290b57cec5SDimitry Andric namespace { 73300b57cec5SDimitry Andric struct EHFlags { 73310b57cec5SDimitry Andric bool Synch = false; 73320b57cec5SDimitry Andric bool Asynch = false; 73330b57cec5SDimitry Andric bool NoUnwindC = false; 73340b57cec5SDimitry Andric }; 73350b57cec5SDimitry Andric } // end anonymous namespace 73360b57cec5SDimitry Andric 73370b57cec5SDimitry Andric /// /EH controls whether to run destructor cleanups when exceptions are 73380b57cec5SDimitry Andric /// thrown. There are three modifiers: 73390b57cec5SDimitry Andric /// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions. 73400b57cec5SDimitry Andric /// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions. 73410b57cec5SDimitry Andric /// The 'a' modifier is unimplemented and fundamentally hard in LLVM IR. 73420b57cec5SDimitry Andric /// - c: Assume that extern "C" functions are implicitly nounwind. 73430b57cec5SDimitry Andric /// The default is /EHs-c-, meaning cleanups are disabled. 73440b57cec5SDimitry Andric static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) { 73450b57cec5SDimitry Andric EHFlags EH; 73460b57cec5SDimitry Andric 73470b57cec5SDimitry Andric std::vector<std::string> EHArgs = 73480b57cec5SDimitry Andric Args.getAllArgValues(options::OPT__SLASH_EH); 73490b57cec5SDimitry Andric for (auto EHVal : EHArgs) { 73500b57cec5SDimitry Andric for (size_t I = 0, E = EHVal.size(); I != E; ++I) { 73510b57cec5SDimitry Andric switch (EHVal[I]) { 73520b57cec5SDimitry Andric case 'a': 73530b57cec5SDimitry Andric EH.Asynch = maybeConsumeDash(EHVal, I); 73540b57cec5SDimitry Andric if (EH.Asynch) 73550b57cec5SDimitry Andric EH.Synch = false; 73560b57cec5SDimitry Andric continue; 73570b57cec5SDimitry Andric case 'c': 73580b57cec5SDimitry Andric EH.NoUnwindC = maybeConsumeDash(EHVal, I); 73590b57cec5SDimitry Andric continue; 73600b57cec5SDimitry Andric case 's': 73610b57cec5SDimitry Andric EH.Synch = maybeConsumeDash(EHVal, I); 73620b57cec5SDimitry Andric if (EH.Synch) 73630b57cec5SDimitry Andric EH.Asynch = false; 73640b57cec5SDimitry Andric continue; 73650b57cec5SDimitry Andric default: 73660b57cec5SDimitry Andric break; 73670b57cec5SDimitry Andric } 73680b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_invalid_value) << "/EH" << EHVal; 73690b57cec5SDimitry Andric break; 73700b57cec5SDimitry Andric } 73710b57cec5SDimitry Andric } 73720b57cec5SDimitry Andric // The /GX, /GX- flags are only processed if there are not /EH flags. 73730b57cec5SDimitry Andric // The default is that /GX is not specified. 73740b57cec5SDimitry Andric if (EHArgs.empty() && 73750b57cec5SDimitry Andric Args.hasFlag(options::OPT__SLASH_GX, options::OPT__SLASH_GX_, 73760b57cec5SDimitry Andric /*Default=*/false)) { 73770b57cec5SDimitry Andric EH.Synch = true; 73780b57cec5SDimitry Andric EH.NoUnwindC = true; 73790b57cec5SDimitry Andric } 73800b57cec5SDimitry Andric 73810b57cec5SDimitry Andric return EH; 73820b57cec5SDimitry Andric } 73830b57cec5SDimitry Andric 73840b57cec5SDimitry Andric void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, 73850b57cec5SDimitry Andric ArgStringList &CmdArgs, 73860b57cec5SDimitry Andric codegenoptions::DebugInfoKind *DebugInfoKind, 73870b57cec5SDimitry Andric bool *EmitCodeView) const { 73880b57cec5SDimitry Andric unsigned RTOptionID = options::OPT__SLASH_MT; 73895ffd83dbSDimitry Andric bool isNVPTX = getToolChain().getTriple().isNVPTX(); 73900b57cec5SDimitry Andric 73910b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 73920b57cec5SDimitry Andric // The /LDd option implies /MTd. The dependent lib part can be overridden, 73930b57cec5SDimitry Andric // but defining _DEBUG is sticky. 73940b57cec5SDimitry Andric RTOptionID = options::OPT__SLASH_MTd; 73950b57cec5SDimitry Andric 73960b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group)) 73970b57cec5SDimitry Andric RTOptionID = A->getOption().getID(); 73980b57cec5SDimitry Andric 73990b57cec5SDimitry Andric StringRef FlagForCRT; 74000b57cec5SDimitry Andric switch (RTOptionID) { 74010b57cec5SDimitry Andric case options::OPT__SLASH_MD: 74020b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 74030b57cec5SDimitry Andric CmdArgs.push_back("-D_DEBUG"); 74040b57cec5SDimitry Andric CmdArgs.push_back("-D_MT"); 74050b57cec5SDimitry Andric CmdArgs.push_back("-D_DLL"); 74060b57cec5SDimitry Andric FlagForCRT = "--dependent-lib=msvcrt"; 74070b57cec5SDimitry Andric break; 74080b57cec5SDimitry Andric case options::OPT__SLASH_MDd: 74090b57cec5SDimitry Andric CmdArgs.push_back("-D_DEBUG"); 74100b57cec5SDimitry Andric CmdArgs.push_back("-D_MT"); 74110b57cec5SDimitry Andric CmdArgs.push_back("-D_DLL"); 74120b57cec5SDimitry Andric FlagForCRT = "--dependent-lib=msvcrtd"; 74130b57cec5SDimitry Andric break; 74140b57cec5SDimitry Andric case options::OPT__SLASH_MT: 74150b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 74160b57cec5SDimitry Andric CmdArgs.push_back("-D_DEBUG"); 74170b57cec5SDimitry Andric CmdArgs.push_back("-D_MT"); 74180b57cec5SDimitry Andric CmdArgs.push_back("-flto-visibility-public-std"); 74190b57cec5SDimitry Andric FlagForCRT = "--dependent-lib=libcmt"; 74200b57cec5SDimitry Andric break; 74210b57cec5SDimitry Andric case options::OPT__SLASH_MTd: 74220b57cec5SDimitry Andric CmdArgs.push_back("-D_DEBUG"); 74230b57cec5SDimitry Andric CmdArgs.push_back("-D_MT"); 74240b57cec5SDimitry Andric CmdArgs.push_back("-flto-visibility-public-std"); 74250b57cec5SDimitry Andric FlagForCRT = "--dependent-lib=libcmtd"; 74260b57cec5SDimitry Andric break; 74270b57cec5SDimitry Andric default: 74280b57cec5SDimitry Andric llvm_unreachable("Unexpected option ID."); 74290b57cec5SDimitry Andric } 74300b57cec5SDimitry Andric 74310b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_Zl)) { 74320b57cec5SDimitry Andric CmdArgs.push_back("-D_VC_NODEFAULTLIB"); 74330b57cec5SDimitry Andric } else { 74340b57cec5SDimitry Andric CmdArgs.push_back(FlagForCRT.data()); 74350b57cec5SDimitry Andric 74360b57cec5SDimitry Andric // This provides POSIX compatibility (maps 'open' to '_open'), which most 74370b57cec5SDimitry Andric // users want. The /Za flag to cl.exe turns this off, but it's not 74380b57cec5SDimitry Andric // implemented in clang. 74390b57cec5SDimitry Andric CmdArgs.push_back("--dependent-lib=oldnames"); 74400b57cec5SDimitry Andric } 74410b57cec5SDimitry Andric 74425ffd83dbSDimitry Andric if (Arg *ShowIncludes = 74435ffd83dbSDimitry Andric Args.getLastArg(options::OPT__SLASH_showIncludes, 74445ffd83dbSDimitry Andric options::OPT__SLASH_showIncludes_user)) { 74455ffd83dbSDimitry Andric CmdArgs.push_back("--show-includes"); 74465ffd83dbSDimitry Andric if (ShowIncludes->getOption().matches(options::OPT__SLASH_showIncludes)) 74475ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 74485ffd83dbSDimitry Andric } 74490b57cec5SDimitry Andric 74500b57cec5SDimitry Andric // This controls whether or not we emit RTTI data for polymorphic types. 74510b57cec5SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR, 74520b57cec5SDimitry Andric /*Default=*/false)) 74530b57cec5SDimitry Andric CmdArgs.push_back("-fno-rtti-data"); 74540b57cec5SDimitry Andric 74550b57cec5SDimitry Andric // This controls whether or not we emit stack-protector instrumentation. 74560b57cec5SDimitry Andric // In MSVC, Buffer Security Check (/GS) is on by default. 74575ffd83dbSDimitry Andric if (!isNVPTX && Args.hasFlag(options::OPT__SLASH_GS, options::OPT__SLASH_GS_, 74580b57cec5SDimitry Andric /*Default=*/true)) { 74590b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector"); 74600b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine(LangOptions::SSPStrong))); 74610b57cec5SDimitry Andric } 74620b57cec5SDimitry Andric 7463e8d8bef9SDimitry Andric // Emit CodeView if -Z7 or -gline-tables-only are present. 7464e8d8bef9SDimitry Andric if (Arg *DebugInfoArg = Args.getLastArg(options::OPT__SLASH_Z7, 74650b57cec5SDimitry Andric options::OPT_gline_tables_only)) { 74660b57cec5SDimitry Andric *EmitCodeView = true; 74670b57cec5SDimitry Andric if (DebugInfoArg->getOption().matches(options::OPT__SLASH_Z7)) 7468fe6060f1SDimitry Andric *DebugInfoKind = codegenoptions::DebugInfoConstructor; 74690b57cec5SDimitry Andric else 74700b57cec5SDimitry Andric *DebugInfoKind = codegenoptions::DebugLineTablesOnly; 74710b57cec5SDimitry Andric } else { 74720b57cec5SDimitry Andric *EmitCodeView = false; 74730b57cec5SDimitry Andric } 74740b57cec5SDimitry Andric 74750b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 74760b57cec5SDimitry Andric EHFlags EH = parseClangCLEHFlags(D, Args); 74775ffd83dbSDimitry Andric if (!isNVPTX && (EH.Synch || EH.Asynch)) { 74780b57cec5SDimitry Andric if (types::isCXX(InputType)) 74790b57cec5SDimitry Andric CmdArgs.push_back("-fcxx-exceptions"); 74800b57cec5SDimitry Andric CmdArgs.push_back("-fexceptions"); 74810b57cec5SDimitry Andric } 74820b57cec5SDimitry Andric if (types::isCXX(InputType) && EH.Synch && EH.NoUnwindC) 74830b57cec5SDimitry Andric CmdArgs.push_back("-fexternc-nounwind"); 74840b57cec5SDimitry Andric 74850b57cec5SDimitry Andric // /EP should expand to -E -P. 74860b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_EP)) { 74870b57cec5SDimitry Andric CmdArgs.push_back("-E"); 74880b57cec5SDimitry Andric CmdArgs.push_back("-P"); 74890b57cec5SDimitry Andric } 74900b57cec5SDimitry Andric 74910b57cec5SDimitry Andric unsigned VolatileOptionID; 7492480093f4SDimitry Andric if (getToolChain().getTriple().isX86()) 74930b57cec5SDimitry Andric VolatileOptionID = options::OPT__SLASH_volatile_ms; 74940b57cec5SDimitry Andric else 74950b57cec5SDimitry Andric VolatileOptionID = options::OPT__SLASH_volatile_iso; 74960b57cec5SDimitry Andric 74970b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__SLASH_volatile_Group)) 74980b57cec5SDimitry Andric VolatileOptionID = A->getOption().getID(); 74990b57cec5SDimitry Andric 75000b57cec5SDimitry Andric if (VolatileOptionID == options::OPT__SLASH_volatile_ms) 75010b57cec5SDimitry Andric CmdArgs.push_back("-fms-volatile"); 75020b57cec5SDimitry Andric 75030b57cec5SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_Zc_dllexportInlines_, 75040b57cec5SDimitry Andric options::OPT__SLASH_Zc_dllexportInlines, 75050b57cec5SDimitry Andric false)) { 75060b57cec5SDimitry Andric CmdArgs.push_back("-fno-dllexport-inlines"); 75070b57cec5SDimitry Andric } 75080b57cec5SDimitry Andric 75090b57cec5SDimitry Andric Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg); 75100b57cec5SDimitry Andric Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb); 75110b57cec5SDimitry Andric if (MostGeneralArg && BestCaseArg) 75120b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_not_allowed_with) 75130b57cec5SDimitry Andric << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args); 75140b57cec5SDimitry Andric 75150b57cec5SDimitry Andric if (MostGeneralArg) { 75160b57cec5SDimitry Andric Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms); 75170b57cec5SDimitry Andric Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm); 75180b57cec5SDimitry Andric Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv); 75190b57cec5SDimitry Andric 75200b57cec5SDimitry Andric Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg; 75210b57cec5SDimitry Andric Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg; 75220b57cec5SDimitry Andric if (FirstConflict && SecondConflict && FirstConflict != SecondConflict) 75230b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_not_allowed_with) 75240b57cec5SDimitry Andric << FirstConflict->getAsString(Args) 75250b57cec5SDimitry Andric << SecondConflict->getAsString(Args); 75260b57cec5SDimitry Andric 75270b57cec5SDimitry Andric if (SingleArg) 75280b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=single"); 75290b57cec5SDimitry Andric else if (MultipleArg) 75300b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=multiple"); 75310b57cec5SDimitry Andric else 75320b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=virtual"); 75330b57cec5SDimitry Andric } 75340b57cec5SDimitry Andric 75350b57cec5SDimitry Andric // Parse the default calling convention options. 75360b57cec5SDimitry Andric if (Arg *CCArg = 75370b57cec5SDimitry Andric Args.getLastArg(options::OPT__SLASH_Gd, options::OPT__SLASH_Gr, 75380b57cec5SDimitry Andric options::OPT__SLASH_Gz, options::OPT__SLASH_Gv, 75390b57cec5SDimitry Andric options::OPT__SLASH_Gregcall)) { 75400b57cec5SDimitry Andric unsigned DCCOptId = CCArg->getOption().getID(); 75410b57cec5SDimitry Andric const char *DCCFlag = nullptr; 75425ffd83dbSDimitry Andric bool ArchSupported = !isNVPTX; 75430b57cec5SDimitry Andric llvm::Triple::ArchType Arch = getToolChain().getArch(); 75440b57cec5SDimitry Andric switch (DCCOptId) { 75450b57cec5SDimitry Andric case options::OPT__SLASH_Gd: 75460b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=cdecl"; 75470b57cec5SDimitry Andric break; 75480b57cec5SDimitry Andric case options::OPT__SLASH_Gr: 75490b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86; 75500b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=fastcall"; 75510b57cec5SDimitry Andric break; 75520b57cec5SDimitry Andric case options::OPT__SLASH_Gz: 75530b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86; 75540b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=stdcall"; 75550b57cec5SDimitry Andric break; 75560b57cec5SDimitry Andric case options::OPT__SLASH_Gv: 75570b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64; 75580b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=vectorcall"; 75590b57cec5SDimitry Andric break; 75600b57cec5SDimitry Andric case options::OPT__SLASH_Gregcall: 75610b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64; 75620b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=regcall"; 75630b57cec5SDimitry Andric break; 75640b57cec5SDimitry Andric } 75650b57cec5SDimitry Andric 75660b57cec5SDimitry Andric // MSVC doesn't warn if /Gr or /Gz is used on x64, so we don't either. 75670b57cec5SDimitry Andric if (ArchSupported && DCCFlag) 75680b57cec5SDimitry Andric CmdArgs.push_back(DCCFlag); 75690b57cec5SDimitry Andric } 75700b57cec5SDimitry Andric 75710b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_vtordisp_mode_EQ); 75720b57cec5SDimitry Andric 75730b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) { 75740b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-format"); 75750b57cec5SDimitry Andric CmdArgs.push_back("msvc"); 75760b57cec5SDimitry Andric } 75770b57cec5SDimitry Andric 75780b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__SLASH_guard)) { 7579480093f4SDimitry Andric StringRef GuardArgs = A->getValue(); 7580fe6060f1SDimitry Andric // The only valid options are "cf", "cf,nochecks", "cf-", "ehcont" and 7581fe6060f1SDimitry Andric // "ehcont-". 7582fe6060f1SDimitry Andric if (GuardArgs.equals_insensitive("cf")) { 7583480093f4SDimitry Andric // Emit CFG instrumentation and the table of address-taken functions. 75840b57cec5SDimitry Andric CmdArgs.push_back("-cfguard"); 7585fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("cf,nochecks")) { 7586480093f4SDimitry Andric // Emit only the table of address-taken functions. 7587480093f4SDimitry Andric CmdArgs.push_back("-cfguard-no-checks"); 7588fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("ehcont")) { 7589fe6060f1SDimitry Andric // Emit EH continuation table. 7590fe6060f1SDimitry Andric CmdArgs.push_back("-ehcontguard"); 7591fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("cf-") || 7592fe6060f1SDimitry Andric GuardArgs.equals_insensitive("ehcont-")) { 7593480093f4SDimitry Andric // Do nothing, but we might want to emit a security warning in future. 7594480093f4SDimitry Andric } else { 7595480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << GuardArgs; 7596480093f4SDimitry Andric } 75970b57cec5SDimitry Andric } 75980b57cec5SDimitry Andric } 75990b57cec5SDimitry Andric 76000b57cec5SDimitry Andric const char *Clang::getBaseInputName(const ArgList &Args, 76010b57cec5SDimitry Andric const InputInfo &Input) { 76020b57cec5SDimitry Andric return Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput())); 76030b57cec5SDimitry Andric } 76040b57cec5SDimitry Andric 76050b57cec5SDimitry Andric const char *Clang::getBaseInputStem(const ArgList &Args, 76060b57cec5SDimitry Andric const InputInfoList &Inputs) { 76070b57cec5SDimitry Andric const char *Str = getBaseInputName(Args, Inputs[0]); 76080b57cec5SDimitry Andric 76090b57cec5SDimitry Andric if (const char *End = strrchr(Str, '.')) 76100b57cec5SDimitry Andric return Args.MakeArgString(std::string(Str, End)); 76110b57cec5SDimitry Andric 76120b57cec5SDimitry Andric return Str; 76130b57cec5SDimitry Andric } 76140b57cec5SDimitry Andric 76150b57cec5SDimitry Andric const char *Clang::getDependencyFileName(const ArgList &Args, 76160b57cec5SDimitry Andric const InputInfoList &Inputs) { 76170b57cec5SDimitry Andric // FIXME: Think about this more. 76180b57cec5SDimitry Andric 76190b57cec5SDimitry Andric if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { 7620a7dea167SDimitry Andric SmallString<128> OutputFilename(OutputOpt->getValue()); 7621a7dea167SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, llvm::Twine('d')); 7622a7dea167SDimitry Andric return Args.MakeArgString(OutputFilename); 76230b57cec5SDimitry Andric } 7624a7dea167SDimitry Andric 7625a7dea167SDimitry Andric return Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".d"); 76260b57cec5SDimitry Andric } 76270b57cec5SDimitry Andric 76280b57cec5SDimitry Andric // Begin ClangAs 76290b57cec5SDimitry Andric 76300b57cec5SDimitry Andric void ClangAs::AddMIPSTargetArgs(const ArgList &Args, 76310b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 76320b57cec5SDimitry Andric StringRef CPUName; 76330b57cec5SDimitry Andric StringRef ABIName; 76340b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 76350b57cec5SDimitry Andric mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 76360b57cec5SDimitry Andric 76370b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 76380b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 76390b57cec5SDimitry Andric } 76400b57cec5SDimitry Andric 76410b57cec5SDimitry Andric void ClangAs::AddX86TargetArgs(const ArgList &Args, 76420b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 76435ffd83dbSDimitry Andric addX86AlignBranchArgs(getToolChain().getDriver(), Args, CmdArgs, 76445ffd83dbSDimitry Andric /*IsLTO=*/false); 7645480093f4SDimitry Andric 76460b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) { 76470b57cec5SDimitry Andric StringRef Value = A->getValue(); 76480b57cec5SDimitry Andric if (Value == "intel" || Value == "att") { 76490b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 76500b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value)); 76510b57cec5SDimitry Andric } else { 76520b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument) 76530b57cec5SDimitry Andric << A->getOption().getName() << Value; 76540b57cec5SDimitry Andric } 76550b57cec5SDimitry Andric } 76560b57cec5SDimitry Andric } 76570b57cec5SDimitry Andric 76580b57cec5SDimitry Andric void ClangAs::AddRISCVTargetArgs(const ArgList &Args, 76590b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 76600b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 76610b57cec5SDimitry Andric StringRef ABIName = riscv::getRISCVABI(Args, Triple); 76620b57cec5SDimitry Andric 76630b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 76640b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 76650b57cec5SDimitry Andric } 76660b57cec5SDimitry Andric 76670b57cec5SDimitry Andric void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, 76680b57cec5SDimitry Andric const InputInfo &Output, const InputInfoList &Inputs, 76690b57cec5SDimitry Andric const ArgList &Args, 76700b57cec5SDimitry Andric const char *LinkingOutput) const { 76710b57cec5SDimitry Andric ArgStringList CmdArgs; 76720b57cec5SDimitry Andric 76730b57cec5SDimitry Andric assert(Inputs.size() == 1 && "Unexpected number of inputs."); 76740b57cec5SDimitry Andric const InputInfo &Input = Inputs[0]; 76750b57cec5SDimitry Andric 76760b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 76770b57cec5SDimitry Andric const std::string &TripleStr = Triple.getTriple(); 76780b57cec5SDimitry Andric const auto &D = getToolChain().getDriver(); 76790b57cec5SDimitry Andric 76800b57cec5SDimitry Andric // Don't warn about "clang -w -c foo.s" 76810b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_w); 76820b57cec5SDimitry Andric // and "clang -emit-llvm -c foo.s" 76830b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_emit_llvm); 76840b57cec5SDimitry Andric 76850b57cec5SDimitry Andric claimNoWarnArgs(Args); 76860b57cec5SDimitry Andric 76870b57cec5SDimitry Andric // Invoke ourselves in -cc1as mode. 76880b57cec5SDimitry Andric // 76890b57cec5SDimitry Andric // FIXME: Implement custom jobs for internal actions. 76900b57cec5SDimitry Andric CmdArgs.push_back("-cc1as"); 76910b57cec5SDimitry Andric 76920b57cec5SDimitry Andric // Add the "effective" target triple. 76930b57cec5SDimitry Andric CmdArgs.push_back("-triple"); 76940b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TripleStr)); 76950b57cec5SDimitry Andric 76960b57cec5SDimitry Andric // Set the output mode, we currently only expect to be used as a real 76970b57cec5SDimitry Andric // assembler. 76980b57cec5SDimitry Andric CmdArgs.push_back("-filetype"); 76990b57cec5SDimitry Andric CmdArgs.push_back("obj"); 77000b57cec5SDimitry Andric 77010b57cec5SDimitry Andric // Set the main file name, so that debug info works even with 77020b57cec5SDimitry Andric // -save-temps or preprocessed assembly. 77030b57cec5SDimitry Andric CmdArgs.push_back("-main-file-name"); 77040b57cec5SDimitry Andric CmdArgs.push_back(Clang::getBaseInputName(Args, Input)); 77050b57cec5SDimitry Andric 77060b57cec5SDimitry Andric // Add the target cpu 7707349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ true); 77080b57cec5SDimitry Andric if (!CPU.empty()) { 77090b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 77100b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 77110b57cec5SDimitry Andric } 77120b57cec5SDimitry Andric 77130b57cec5SDimitry Andric // Add the target features 77145ffd83dbSDimitry Andric getTargetFeatures(D, Triple, Args, CmdArgs, true); 77150b57cec5SDimitry Andric 77160b57cec5SDimitry Andric // Ignore explicit -force_cpusubtype_ALL option. 77170b57cec5SDimitry Andric (void)Args.hasArg(options::OPT_force__cpusubtype__ALL); 77180b57cec5SDimitry Andric 77190b57cec5SDimitry Andric // Pass along any -I options so we get proper .include search paths. 77200b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_I_Group); 77210b57cec5SDimitry Andric 77220b57cec5SDimitry Andric // Determine the original source input. 77230eae32dcSDimitry Andric auto FindSource = [](const Action *S) -> const Action * { 77240eae32dcSDimitry Andric while (S->getKind() != Action::InputClass) { 77250eae32dcSDimitry Andric assert(!S->getInputs().empty() && "unexpected root action!"); 77260eae32dcSDimitry Andric S = S->getInputs()[0]; 77270b57cec5SDimitry Andric } 77280eae32dcSDimitry Andric return S; 77290eae32dcSDimitry Andric }; 77300eae32dcSDimitry Andric const Action *SourceAction = FindSource(&JA); 77310b57cec5SDimitry Andric 77320b57cec5SDimitry Andric // Forward -g and handle debug info related flags, assuming we are dealing 77330b57cec5SDimitry Andric // with an actual assembly file. 77340b57cec5SDimitry Andric bool WantDebug = false; 77350b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_g_Group); 7736fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_g_Group)) 77370b57cec5SDimitry Andric WantDebug = !A->getOption().matches(options::OPT_g0) && 77380b57cec5SDimitry Andric !A->getOption().matches(options::OPT_ggdb0); 7739480093f4SDimitry Andric 7740fe6060f1SDimitry Andric unsigned DwarfVersion = ParseDebugDefaultVersion(getToolChain(), Args); 7741fe6060f1SDimitry Andric if (const Arg *GDwarfN = getDwarfNArg(Args)) 7742fe6060f1SDimitry Andric DwarfVersion = DwarfVersionNum(GDwarfN->getSpelling()); 7743480093f4SDimitry Andric 77440b57cec5SDimitry Andric if (DwarfVersion == 0) 77450b57cec5SDimitry Andric DwarfVersion = getToolChain().GetDefaultDwarfVersion(); 77460b57cec5SDimitry Andric 77470b57cec5SDimitry Andric codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo; 77480b57cec5SDimitry Andric 77490eae32dcSDimitry Andric // Add the -fdebug-compilation-dir flag if needed. 77500eae32dcSDimitry Andric const char *DebugCompilationDir = 77510eae32dcSDimitry Andric addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS()); 77520eae32dcSDimitry Andric 77530b57cec5SDimitry Andric if (SourceAction->getType() == types::TY_Asm || 77540b57cec5SDimitry Andric SourceAction->getType() == types::TY_PP_Asm) { 77550b57cec5SDimitry Andric // You might think that it would be ok to set DebugInfoKind outside of 77560b57cec5SDimitry Andric // the guard for source type, however there is a test which asserts 77570b57cec5SDimitry Andric // that some assembler invocation receives no -debug-info-kind, 77580b57cec5SDimitry Andric // and it's not clear whether that test is just overly restrictive. 7759fe6060f1SDimitry Andric DebugInfoKind = (WantDebug ? codegenoptions::DebugInfoConstructor 77600b57cec5SDimitry Andric : codegenoptions::NoDebugInfo); 77610b57cec5SDimitry Andric 77620b57cec5SDimitry Andric addDebugPrefixMapArg(getToolChain().getDriver(), Args, CmdArgs); 77630b57cec5SDimitry Andric 77640b57cec5SDimitry Andric // Set the AT_producer to the clang version when using the integrated 77650b57cec5SDimitry Andric // assembler on assembly source files. 77660b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-producer"); 77670b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(getClangFullVersion())); 77680b57cec5SDimitry Andric 77690b57cec5SDimitry Andric // And pass along -I options 77700b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_I); 77710b57cec5SDimitry Andric } 77720b57cec5SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion, 77730b57cec5SDimitry Andric llvm::DebuggerKind::Default); 7774fe6060f1SDimitry Andric renderDwarfFormat(D, Triple, Args, CmdArgs, DwarfVersion); 77750b57cec5SDimitry Andric RenderDebugInfoCompressionArgs(Args, CmdArgs, D, getToolChain()); 77760b57cec5SDimitry Andric 77770b57cec5SDimitry Andric 77780b57cec5SDimitry Andric // Handle -fPIC et al -- the relocation-model affects the assembler 77790b57cec5SDimitry Andric // for some targets. 77800b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 77810b57cec5SDimitry Andric unsigned PICLevel; 77820b57cec5SDimitry Andric bool IsPIE; 77830b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 77840b57cec5SDimitry Andric ParsePICArgs(getToolChain(), Args); 77850b57cec5SDimitry Andric 77860b57cec5SDimitry Andric const char *RMName = RelocationModelName(RelocationModel); 77870b57cec5SDimitry Andric if (RMName) { 77880b57cec5SDimitry Andric CmdArgs.push_back("-mrelocation-model"); 77890b57cec5SDimitry Andric CmdArgs.push_back(RMName); 77900b57cec5SDimitry Andric } 77910b57cec5SDimitry Andric 77920b57cec5SDimitry Andric // Optionally embed the -cc1as level arguments into the debug info, for build 77930b57cec5SDimitry Andric // analysis. 77940b57cec5SDimitry Andric if (getToolChain().UseDwarfDebugFlags()) { 77950b57cec5SDimitry Andric ArgStringList OriginalArgs; 77960b57cec5SDimitry Andric for (const auto &Arg : Args) 77970b57cec5SDimitry Andric Arg->render(Args, OriginalArgs); 77980b57cec5SDimitry Andric 77990b57cec5SDimitry Andric SmallString<256> Flags; 78000b57cec5SDimitry Andric const char *Exec = getToolChain().getDriver().getClangProgramPath(); 78015ffd83dbSDimitry Andric EscapeSpacesAndBackslashes(Exec, Flags); 78020b57cec5SDimitry Andric for (const char *OriginalArg : OriginalArgs) { 78030b57cec5SDimitry Andric SmallString<128> EscapedArg; 78040b57cec5SDimitry Andric EscapeSpacesAndBackslashes(OriginalArg, EscapedArg); 78050b57cec5SDimitry Andric Flags += " "; 78060b57cec5SDimitry Andric Flags += EscapedArg; 78070b57cec5SDimitry Andric } 78080b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-flags"); 78090b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Flags)); 78100b57cec5SDimitry Andric } 78110b57cec5SDimitry Andric 78120b57cec5SDimitry Andric // FIXME: Add -static support, once we have it. 78130b57cec5SDimitry Andric 78140b57cec5SDimitry Andric // Add target specific flags. 78150b57cec5SDimitry Andric switch (getToolChain().getArch()) { 78160b57cec5SDimitry Andric default: 78170b57cec5SDimitry Andric break; 78180b57cec5SDimitry Andric 78190b57cec5SDimitry Andric case llvm::Triple::mips: 78200b57cec5SDimitry Andric case llvm::Triple::mipsel: 78210b57cec5SDimitry Andric case llvm::Triple::mips64: 78220b57cec5SDimitry Andric case llvm::Triple::mips64el: 78230b57cec5SDimitry Andric AddMIPSTargetArgs(Args, CmdArgs); 78240b57cec5SDimitry Andric break; 78250b57cec5SDimitry Andric 78260b57cec5SDimitry Andric case llvm::Triple::x86: 78270b57cec5SDimitry Andric case llvm::Triple::x86_64: 78280b57cec5SDimitry Andric AddX86TargetArgs(Args, CmdArgs); 78290b57cec5SDimitry Andric break; 78300b57cec5SDimitry Andric 78310b57cec5SDimitry Andric case llvm::Triple::arm: 78320b57cec5SDimitry Andric case llvm::Triple::armeb: 78330b57cec5SDimitry Andric case llvm::Triple::thumb: 78340b57cec5SDimitry Andric case llvm::Triple::thumbeb: 78350b57cec5SDimitry Andric // This isn't in AddARMTargetArgs because we want to do this for assembly 78360b57cec5SDimitry Andric // only, not C/C++. 78370b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mdefault_build_attributes, 78380b57cec5SDimitry Andric options::OPT_mno_default_build_attributes, true)) { 78390b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 78400b57cec5SDimitry Andric CmdArgs.push_back("-arm-add-build-attributes"); 78410b57cec5SDimitry Andric } 78420b57cec5SDimitry Andric break; 78430b57cec5SDimitry Andric 7844e8d8bef9SDimitry Andric case llvm::Triple::aarch64: 7845e8d8bef9SDimitry Andric case llvm::Triple::aarch64_32: 7846e8d8bef9SDimitry Andric case llvm::Triple::aarch64_be: 7847e8d8bef9SDimitry Andric if (Args.hasArg(options::OPT_mmark_bti_property)) { 7848e8d8bef9SDimitry Andric CmdArgs.push_back("-mllvm"); 7849e8d8bef9SDimitry Andric CmdArgs.push_back("-aarch64-mark-bti-property"); 7850e8d8bef9SDimitry Andric } 7851e8d8bef9SDimitry Andric break; 7852e8d8bef9SDimitry Andric 78530b57cec5SDimitry Andric case llvm::Triple::riscv32: 78540b57cec5SDimitry Andric case llvm::Triple::riscv64: 78550b57cec5SDimitry Andric AddRISCVTargetArgs(Args, CmdArgs); 78560b57cec5SDimitry Andric break; 78570b57cec5SDimitry Andric } 78580b57cec5SDimitry Andric 78590b57cec5SDimitry Andric // Consume all the warning flags. Usually this would be handled more 78600b57cec5SDimitry Andric // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as 78610b57cec5SDimitry Andric // doesn't handle that so rather than warning about unused flags that are 78620b57cec5SDimitry Andric // actually used, we'll lie by omission instead. 78630b57cec5SDimitry Andric // FIXME: Stop lying and consume only the appropriate driver flags 78640b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_W_Group); 78650b57cec5SDimitry Andric 78660b57cec5SDimitry Andric CollectArgsForIntegratedAssembler(C, Args, CmdArgs, 78670b57cec5SDimitry Andric getToolChain().getDriver()); 78680b57cec5SDimitry Andric 78690b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_mllvm); 78700b57cec5SDimitry Andric 78710eae32dcSDimitry Andric if (DebugInfoKind > codegenoptions::NoDebugInfo && Output.isFilename()) 78720eae32dcSDimitry Andric addDebugObjectName(Args, CmdArgs, DebugCompilationDir, 78730eae32dcSDimitry Andric Output.getFilename()); 78740eae32dcSDimitry Andric 78750eae32dcSDimitry Andric // Fixup any previous commands that use -object-file-name because when we 78760eae32dcSDimitry Andric // generated them, the final .obj name wasn't yet known. 78770eae32dcSDimitry Andric for (Command &J : C.getJobs()) { 78780eae32dcSDimitry Andric if (SourceAction != FindSource(&J.getSource())) 78790eae32dcSDimitry Andric continue; 78800eae32dcSDimitry Andric auto &JArgs = J.getArguments(); 78810eae32dcSDimitry Andric for (unsigned I = 0; I < JArgs.size(); ++I) { 78820eae32dcSDimitry Andric if (StringRef(JArgs[I]).startswith("-object-file-name=") && 78830eae32dcSDimitry Andric Output.isFilename()) { 78840eae32dcSDimitry Andric ArgStringList NewArgs(JArgs.begin(), JArgs.begin() + I); 78850eae32dcSDimitry Andric addDebugObjectName(Args, NewArgs, DebugCompilationDir, 78860eae32dcSDimitry Andric Output.getFilename()); 78870eae32dcSDimitry Andric NewArgs.append(JArgs.begin() + I + 1, JArgs.end()); 78880eae32dcSDimitry Andric J.replaceArguments(NewArgs); 78890eae32dcSDimitry Andric break; 78900eae32dcSDimitry Andric } 78910eae32dcSDimitry Andric } 78920eae32dcSDimitry Andric } 78930eae32dcSDimitry Andric 78940b57cec5SDimitry Andric assert(Output.isFilename() && "Unexpected lipo output."); 78950b57cec5SDimitry Andric CmdArgs.push_back("-o"); 78960b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 78970b57cec5SDimitry Andric 78980b57cec5SDimitry Andric const llvm::Triple &T = getToolChain().getTriple(); 78990b57cec5SDimitry Andric Arg *A; 79000b57cec5SDimitry Andric if (getDebugFissionKind(D, Args, A) == DwarfFissionKind::Split && 79010b57cec5SDimitry Andric T.isOSBinFormatELF()) { 79020b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-output"); 7903e8d8bef9SDimitry Andric CmdArgs.push_back(SplitDebugName(JA, Args, Input, Output)); 79040b57cec5SDimitry Andric } 79050b57cec5SDimitry Andric 7906e8d8bef9SDimitry Andric if (Triple.isAMDGPU()) 7907e8d8bef9SDimitry Andric handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs); 7908e8d8bef9SDimitry Andric 79090b57cec5SDimitry Andric assert(Input.isFilename() && "Invalid input."); 79100b57cec5SDimitry Andric CmdArgs.push_back(Input.getFilename()); 79110b57cec5SDimitry Andric 79120b57cec5SDimitry Andric const char *Exec = getToolChain().getDriver().getClangProgramPath(); 7913e8d8bef9SDimitry Andric if (D.CC1Main && !D.CCGenDiagnostics) { 7914e8d8bef9SDimitry Andric // Invoke cc1as directly in this process. 7915e8d8bef9SDimitry Andric C.addCommand(std::make_unique<CC1Command>(JA, *this, 7916e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 7917e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 7918e8d8bef9SDimitry Andric } else { 7919e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, 7920e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 7921e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 7922e8d8bef9SDimitry Andric } 79230b57cec5SDimitry Andric } 79240b57cec5SDimitry Andric 79250b57cec5SDimitry Andric // Begin OffloadBundler 79260b57cec5SDimitry Andric 79270b57cec5SDimitry Andric void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA, 79280b57cec5SDimitry Andric const InputInfo &Output, 79290b57cec5SDimitry Andric const InputInfoList &Inputs, 79300b57cec5SDimitry Andric const llvm::opt::ArgList &TCArgs, 79310b57cec5SDimitry Andric const char *LinkingOutput) const { 79320b57cec5SDimitry Andric // The version with only one output is expected to refer to a bundling job. 79330b57cec5SDimitry Andric assert(isa<OffloadBundlingJobAction>(JA) && "Expecting bundling job!"); 79340b57cec5SDimitry Andric 79350b57cec5SDimitry Andric // The bundling command looks like this: 79360b57cec5SDimitry Andric // clang-offload-bundler -type=bc 79370b57cec5SDimitry Andric // -targets=host-triple,openmp-triple1,openmp-triple2 79380b57cec5SDimitry Andric // -outputs=input_file 79390b57cec5SDimitry Andric // -inputs=unbundle_file_host,unbundle_file_tgt1,unbundle_file_tgt2" 79400b57cec5SDimitry Andric 79410b57cec5SDimitry Andric ArgStringList CmdArgs; 79420b57cec5SDimitry Andric 79430b57cec5SDimitry Andric // Get the type. 79440b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString( 79450b57cec5SDimitry Andric Twine("-type=") + types::getTypeTempSuffix(Output.getType()))); 79460b57cec5SDimitry Andric 79470b57cec5SDimitry Andric assert(JA.getInputs().size() == Inputs.size() && 79480b57cec5SDimitry Andric "Not have inputs for all dependence actions??"); 79490b57cec5SDimitry Andric 79500b57cec5SDimitry Andric // Get the targets. 79510b57cec5SDimitry Andric SmallString<128> Triples; 79520b57cec5SDimitry Andric Triples += "-targets="; 79530b57cec5SDimitry Andric for (unsigned I = 0; I < Inputs.size(); ++I) { 79540b57cec5SDimitry Andric if (I) 79550b57cec5SDimitry Andric Triples += ','; 79560b57cec5SDimitry Andric 79570b57cec5SDimitry Andric // Find ToolChain for this input. 79580b57cec5SDimitry Andric Action::OffloadKind CurKind = Action::OFK_Host; 79590b57cec5SDimitry Andric const ToolChain *CurTC = &getToolChain(); 79600b57cec5SDimitry Andric const Action *CurDep = JA.getInputs()[I]; 79610b57cec5SDimitry Andric 79620b57cec5SDimitry Andric if (const auto *OA = dyn_cast<OffloadAction>(CurDep)) { 79630b57cec5SDimitry Andric CurTC = nullptr; 79640b57cec5SDimitry Andric OA->doOnEachDependence([&](Action *A, const ToolChain *TC, const char *) { 79650b57cec5SDimitry Andric assert(CurTC == nullptr && "Expected one dependence!"); 79660b57cec5SDimitry Andric CurKind = A->getOffloadingDeviceKind(); 79670b57cec5SDimitry Andric CurTC = TC; 79680b57cec5SDimitry Andric }); 79690b57cec5SDimitry Andric } 79700b57cec5SDimitry Andric Triples += Action::GetOffloadKindName(CurKind); 7971349cc55cSDimitry Andric Triples += '-'; 7972349cc55cSDimitry Andric Triples += CurTC->getTriple().normalize(); 7973349cc55cSDimitry Andric if ((CurKind == Action::OFK_HIP || CurKind == Action::OFK_Cuda) && 79740eae32dcSDimitry Andric !StringRef(CurDep->getOffloadingArch()).empty()) { 7975349cc55cSDimitry Andric Triples += '-'; 79760b57cec5SDimitry Andric Triples += CurDep->getOffloadingArch(); 79770b57cec5SDimitry Andric } 7978349cc55cSDimitry Andric 7979349cc55cSDimitry Andric // TODO: Replace parsing of -march flag. Can be done by storing GPUArch 7980349cc55cSDimitry Andric // with each toolchain. 7981349cc55cSDimitry Andric StringRef GPUArchName; 7982349cc55cSDimitry Andric if (CurKind == Action::OFK_OpenMP) { 7983349cc55cSDimitry Andric // Extract GPUArch from -march argument in TC argument list. 7984349cc55cSDimitry Andric for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) { 7985349cc55cSDimitry Andric auto ArchStr = StringRef(TCArgs.getArgString(ArgIndex)); 7986349cc55cSDimitry Andric auto Arch = ArchStr.startswith_insensitive("-march="); 7987349cc55cSDimitry Andric if (Arch) { 7988349cc55cSDimitry Andric GPUArchName = ArchStr.substr(7); 7989349cc55cSDimitry Andric Triples += "-"; 7990349cc55cSDimitry Andric break; 7991349cc55cSDimitry Andric } 7992349cc55cSDimitry Andric } 7993349cc55cSDimitry Andric Triples += GPUArchName.str(); 7994349cc55cSDimitry Andric } 79950b57cec5SDimitry Andric } 79960b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(Triples)); 79970b57cec5SDimitry Andric 79980b57cec5SDimitry Andric // Get bundled file command. 79990b57cec5SDimitry Andric CmdArgs.push_back( 80000b57cec5SDimitry Andric TCArgs.MakeArgString(Twine("-outputs=") + Output.getFilename())); 80010b57cec5SDimitry Andric 80020b57cec5SDimitry Andric // Get unbundled files command. 80030b57cec5SDimitry Andric SmallString<128> UB; 80040b57cec5SDimitry Andric UB += "-inputs="; 80050b57cec5SDimitry Andric for (unsigned I = 0; I < Inputs.size(); ++I) { 80060b57cec5SDimitry Andric if (I) 80070b57cec5SDimitry Andric UB += ','; 80080b57cec5SDimitry Andric 80090b57cec5SDimitry Andric // Find ToolChain for this input. 80100b57cec5SDimitry Andric const ToolChain *CurTC = &getToolChain(); 80110b57cec5SDimitry Andric if (const auto *OA = dyn_cast<OffloadAction>(JA.getInputs()[I])) { 80120b57cec5SDimitry Andric CurTC = nullptr; 80130b57cec5SDimitry Andric OA->doOnEachDependence([&](Action *, const ToolChain *TC, const char *) { 80140b57cec5SDimitry Andric assert(CurTC == nullptr && "Expected one dependence!"); 80150b57cec5SDimitry Andric CurTC = TC; 80160b57cec5SDimitry Andric }); 80176e75b2fbSDimitry Andric UB += C.addTempFile( 80186e75b2fbSDimitry Andric C.getArgs().MakeArgString(CurTC->getInputFilename(Inputs[I]))); 80196e75b2fbSDimitry Andric } else { 80200b57cec5SDimitry Andric UB += CurTC->getInputFilename(Inputs[I]); 80210b57cec5SDimitry Andric } 80226e75b2fbSDimitry Andric } 80230b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(UB)); 80240b57cec5SDimitry Andric 80250b57cec5SDimitry Andric // All the inputs are encoded as commands. 8026a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>( 80275ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::None(), 80280b57cec5SDimitry Andric TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), 8029e8d8bef9SDimitry Andric CmdArgs, None, Output)); 80300b57cec5SDimitry Andric } 80310b57cec5SDimitry Andric 80320b57cec5SDimitry Andric void OffloadBundler::ConstructJobMultipleOutputs( 80330b57cec5SDimitry Andric Compilation &C, const JobAction &JA, const InputInfoList &Outputs, 80340b57cec5SDimitry Andric const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, 80350b57cec5SDimitry Andric const char *LinkingOutput) const { 80360b57cec5SDimitry Andric // The version with multiple outputs is expected to refer to a unbundling job. 80370b57cec5SDimitry Andric auto &UA = cast<OffloadUnbundlingJobAction>(JA); 80380b57cec5SDimitry Andric 80390b57cec5SDimitry Andric // The unbundling command looks like this: 80400b57cec5SDimitry Andric // clang-offload-bundler -type=bc 80410b57cec5SDimitry Andric // -targets=host-triple,openmp-triple1,openmp-triple2 80420b57cec5SDimitry Andric // -inputs=input_file 80430b57cec5SDimitry Andric // -outputs=unbundle_file_host,unbundle_file_tgt1,unbundle_file_tgt2" 80440b57cec5SDimitry Andric // -unbundle 80450b57cec5SDimitry Andric 80460b57cec5SDimitry Andric ArgStringList CmdArgs; 80470b57cec5SDimitry Andric 80480b57cec5SDimitry Andric assert(Inputs.size() == 1 && "Expecting to unbundle a single file!"); 80490b57cec5SDimitry Andric InputInfo Input = Inputs.front(); 80500b57cec5SDimitry Andric 80510b57cec5SDimitry Andric // Get the type. 80520b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString( 80530b57cec5SDimitry Andric Twine("-type=") + types::getTypeTempSuffix(Input.getType()))); 80540b57cec5SDimitry Andric 80550b57cec5SDimitry Andric // Get the targets. 80560b57cec5SDimitry Andric SmallString<128> Triples; 80570b57cec5SDimitry Andric Triples += "-targets="; 80580b57cec5SDimitry Andric auto DepInfo = UA.getDependentActionsInfo(); 80590b57cec5SDimitry Andric for (unsigned I = 0; I < DepInfo.size(); ++I) { 80600b57cec5SDimitry Andric if (I) 80610b57cec5SDimitry Andric Triples += ','; 80620b57cec5SDimitry Andric 80630b57cec5SDimitry Andric auto &Dep = DepInfo[I]; 80640b57cec5SDimitry Andric Triples += Action::GetOffloadKindName(Dep.DependentOffloadKind); 8065349cc55cSDimitry Andric Triples += '-'; 8066349cc55cSDimitry Andric Triples += Dep.DependentToolChain->getTriple().normalize(); 8067349cc55cSDimitry Andric if ((Dep.DependentOffloadKind == Action::OFK_HIP || 8068349cc55cSDimitry Andric Dep.DependentOffloadKind == Action::OFK_Cuda) && 8069349cc55cSDimitry Andric !Dep.DependentBoundArch.empty()) { 8070349cc55cSDimitry Andric Triples += '-'; 80710b57cec5SDimitry Andric Triples += Dep.DependentBoundArch; 80720b57cec5SDimitry Andric } 8073349cc55cSDimitry Andric // TODO: Replace parsing of -march flag. Can be done by storing GPUArch 8074349cc55cSDimitry Andric // with each toolchain. 8075349cc55cSDimitry Andric StringRef GPUArchName; 8076349cc55cSDimitry Andric if (Dep.DependentOffloadKind == Action::OFK_OpenMP) { 8077349cc55cSDimitry Andric // Extract GPUArch from -march argument in TC argument list. 8078349cc55cSDimitry Andric for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) { 8079349cc55cSDimitry Andric StringRef ArchStr = StringRef(TCArgs.getArgString(ArgIndex)); 8080349cc55cSDimitry Andric auto Arch = ArchStr.startswith_insensitive("-march="); 8081349cc55cSDimitry Andric if (Arch) { 8082349cc55cSDimitry Andric GPUArchName = ArchStr.substr(7); 8083349cc55cSDimitry Andric Triples += "-"; 8084349cc55cSDimitry Andric break; 8085349cc55cSDimitry Andric } 8086349cc55cSDimitry Andric } 8087349cc55cSDimitry Andric Triples += GPUArchName.str(); 8088349cc55cSDimitry Andric } 80890b57cec5SDimitry Andric } 80900b57cec5SDimitry Andric 80910b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(Triples)); 80920b57cec5SDimitry Andric 80930b57cec5SDimitry Andric // Get bundled file command. 80940b57cec5SDimitry Andric CmdArgs.push_back( 80950b57cec5SDimitry Andric TCArgs.MakeArgString(Twine("-inputs=") + Input.getFilename())); 80960b57cec5SDimitry Andric 80970b57cec5SDimitry Andric // Get unbundled files command. 80980b57cec5SDimitry Andric SmallString<128> UB; 80990b57cec5SDimitry Andric UB += "-outputs="; 81000b57cec5SDimitry Andric for (unsigned I = 0; I < Outputs.size(); ++I) { 81010b57cec5SDimitry Andric if (I) 81020b57cec5SDimitry Andric UB += ','; 81030b57cec5SDimitry Andric UB += DepInfo[I].DependentToolChain->getInputFilename(Outputs[I]); 81040b57cec5SDimitry Andric } 81050b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(UB)); 81060b57cec5SDimitry Andric CmdArgs.push_back("-unbundle"); 8107e8d8bef9SDimitry Andric CmdArgs.push_back("-allow-missing-bundles"); 81080b57cec5SDimitry Andric 81090b57cec5SDimitry Andric // All the inputs are encoded as commands. 8110a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>( 81115ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::None(), 81120b57cec5SDimitry Andric TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), 8113e8d8bef9SDimitry Andric CmdArgs, None, Outputs)); 81140b57cec5SDimitry Andric } 8115a7dea167SDimitry Andric 8116a7dea167SDimitry Andric void OffloadWrapper::ConstructJob(Compilation &C, const JobAction &JA, 8117a7dea167SDimitry Andric const InputInfo &Output, 8118a7dea167SDimitry Andric const InputInfoList &Inputs, 8119a7dea167SDimitry Andric const ArgList &Args, 8120a7dea167SDimitry Andric const char *LinkingOutput) const { 8121a7dea167SDimitry Andric ArgStringList CmdArgs; 8122a7dea167SDimitry Andric 8123a7dea167SDimitry Andric const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 8124a7dea167SDimitry Andric 8125a7dea167SDimitry Andric // Add the "effective" target triple. 8126a7dea167SDimitry Andric CmdArgs.push_back("-target"); 8127a7dea167SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Triple.getTriple())); 8128a7dea167SDimitry Andric 8129a7dea167SDimitry Andric // Add the output file name. 8130a7dea167SDimitry Andric assert(Output.isFilename() && "Invalid output."); 8131a7dea167SDimitry Andric CmdArgs.push_back("-o"); 8132a7dea167SDimitry Andric CmdArgs.push_back(Output.getFilename()); 8133a7dea167SDimitry Andric 8134a7dea167SDimitry Andric // Add inputs. 8135a7dea167SDimitry Andric for (const InputInfo &I : Inputs) { 8136a7dea167SDimitry Andric assert(I.isFilename() && "Invalid input."); 8137a7dea167SDimitry Andric CmdArgs.push_back(I.getFilename()); 8138a7dea167SDimitry Andric } 8139a7dea167SDimitry Andric 8140a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>( 81415ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::None(), 8142a7dea167SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath(getShortName())), 8143e8d8bef9SDimitry Andric CmdArgs, Inputs, Output)); 8144a7dea167SDimitry Andric } 81451fd87a68SDimitry Andric 81461fd87a68SDimitry Andric void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, 81471fd87a68SDimitry Andric const InputInfo &Output, 81481fd87a68SDimitry Andric const InputInfoList &Inputs, 81491fd87a68SDimitry Andric const ArgList &Args, 81501fd87a68SDimitry Andric const char *LinkingOutput) const { 8151d56accc7SDimitry Andric const Driver &D = getToolChain().getDriver(); 8152d56accc7SDimitry Andric const llvm::Triple TheTriple = getToolChain().getTriple(); 8153d56accc7SDimitry Andric auto OpenMPTCRange = C.getOffloadToolChains<Action::OFK_OpenMP>(); 81541fd87a68SDimitry Andric ArgStringList CmdArgs; 81551fd87a68SDimitry Andric 8156d56accc7SDimitry Andric // Pass the CUDA path to the linker wrapper tool. 8157d56accc7SDimitry Andric for (auto &I : llvm::make_range(OpenMPTCRange.first, OpenMPTCRange.second)) { 8158d56accc7SDimitry Andric const ToolChain *TC = I.second; 8159d56accc7SDimitry Andric if (TC->getTriple().isNVPTX()) { 8160d56accc7SDimitry Andric CudaInstallationDetector CudaInstallation(D, TheTriple, Args); 8161d56accc7SDimitry Andric if (CudaInstallation.isValid()) 8162d56accc7SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8163d56accc7SDimitry Andric "--cuda-path=" + CudaInstallation.getInstallPath())); 8164d56accc7SDimitry Andric break; 8165d56accc7SDimitry Andric } 8166d56accc7SDimitry Andric } 8167d56accc7SDimitry Andric 8168d56accc7SDimitry Andric if (D.isUsingLTO(/* IsOffload */ true)) { 81691fd87a68SDimitry Andric // Pass in target features for each toolchain. 81701fd87a68SDimitry Andric for (auto &I : 81711fd87a68SDimitry Andric llvm::make_range(OpenMPTCRange.first, OpenMPTCRange.second)) { 81721fd87a68SDimitry Andric const ToolChain *TC = I.second; 81731fd87a68SDimitry Andric const ArgList &TCArgs = C.getArgsForToolChain(TC, "", Action::OFK_OpenMP); 81741fd87a68SDimitry Andric ArgStringList FeatureArgs; 81751fd87a68SDimitry Andric TC->addClangTargetOptions(TCArgs, FeatureArgs, Action::OFK_OpenMP); 81761fd87a68SDimitry Andric auto FeatureIt = llvm::find(FeatureArgs, "-target-feature"); 81771fd87a68SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 81781fd87a68SDimitry Andric "-target-feature=" + TC->getTripleString() + "=" + *(FeatureIt + 1))); 81791fd87a68SDimitry Andric } 81801fd87a68SDimitry Andric 81811fd87a68SDimitry Andric // Pass in the bitcode library to be linked during LTO. 8182d56accc7SDimitry Andric for (auto &I : 8183d56accc7SDimitry Andric llvm::make_range(OpenMPTCRange.first, OpenMPTCRange.second)) { 81841fd87a68SDimitry Andric const ToolChain *TC = I.second; 8185d56accc7SDimitry Andric const Driver &TCDriver = TC->getDriver(); 81861fd87a68SDimitry Andric const ArgList &TCArgs = C.getArgsForToolChain(TC, "", Action::OFK_OpenMP); 81871fd87a68SDimitry Andric StringRef Arch = TCArgs.getLastArgValue(options::OPT_march_EQ); 81881fd87a68SDimitry Andric 81891fd87a68SDimitry Andric std::string BitcodeSuffix; 81901fd87a68SDimitry Andric if (TCArgs.hasFlag(options::OPT_fopenmp_target_new_runtime, 81911fd87a68SDimitry Andric options::OPT_fno_openmp_target_new_runtime, true)) 81921fd87a68SDimitry Andric BitcodeSuffix += "new-"; 81931fd87a68SDimitry Andric if (TC->getTriple().isNVPTX()) 81941fd87a68SDimitry Andric BitcodeSuffix += "nvptx-"; 81951fd87a68SDimitry Andric else if (TC->getTriple().isAMDGPU()) 81961fd87a68SDimitry Andric BitcodeSuffix += "amdgpu-"; 81971fd87a68SDimitry Andric BitcodeSuffix += Arch; 81981fd87a68SDimitry Andric 81991fd87a68SDimitry Andric ArgStringList BitcodeLibrary; 8200d56accc7SDimitry Andric addOpenMPDeviceRTL(TCDriver, TCArgs, BitcodeLibrary, BitcodeSuffix, 82011fd87a68SDimitry Andric TC->getTriple()); 82021fd87a68SDimitry Andric 82031fd87a68SDimitry Andric if (!BitcodeLibrary.empty()) 82041fd87a68SDimitry Andric CmdArgs.push_back( 82051fd87a68SDimitry Andric Args.MakeArgString("-target-library=" + TC->getTripleString() + 82061fd87a68SDimitry Andric "-" + Arch + "=" + BitcodeLibrary.back())); 82071fd87a68SDimitry Andric } 82081fd87a68SDimitry Andric 82091fd87a68SDimitry Andric // Pass in the optimization level to use for LTO. 82101fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { 82111fd87a68SDimitry Andric StringRef OOpt; 82121fd87a68SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 82131fd87a68SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 82141fd87a68SDimitry Andric OOpt = "3"; 82151fd87a68SDimitry Andric else if (A->getOption().matches(options::OPT_O)) { 82161fd87a68SDimitry Andric OOpt = A->getValue(); 82171fd87a68SDimitry Andric if (OOpt == "g") 82181fd87a68SDimitry Andric OOpt = "1"; 82191fd87a68SDimitry Andric else if (OOpt == "s" || OOpt == "z") 82201fd87a68SDimitry Andric OOpt = "2"; 82211fd87a68SDimitry Andric } else if (A->getOption().matches(options::OPT_O0)) 82221fd87a68SDimitry Andric OOpt = "0"; 82231fd87a68SDimitry Andric if (!OOpt.empty()) 82241fd87a68SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-opt-level=O") + OOpt)); 82251fd87a68SDimitry Andric } 82261fd87a68SDimitry Andric } 82271fd87a68SDimitry Andric 82281fd87a68SDimitry Andric CmdArgs.push_back("-host-triple"); 8229d56accc7SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TheTriple.getTriple())); 82301fd87a68SDimitry Andric if (Args.hasArg(options::OPT_v)) 82311fd87a68SDimitry Andric CmdArgs.push_back("-v"); 82321fd87a68SDimitry Andric 82331fd87a68SDimitry Andric // Add debug information if present. 82341fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { 82351fd87a68SDimitry Andric const Option &Opt = A->getOption(); 82361fd87a68SDimitry Andric if (Opt.matches(options::OPT_gN_Group)) { 82371fd87a68SDimitry Andric if (Opt.matches(options::OPT_gline_directives_only) || 82381fd87a68SDimitry Andric Opt.matches(options::OPT_gline_tables_only)) 82391fd87a68SDimitry Andric CmdArgs.push_back("-gline-directives-only"); 82401fd87a68SDimitry Andric } else 82411fd87a68SDimitry Andric CmdArgs.push_back("-g"); 82421fd87a68SDimitry Andric } 82431fd87a68SDimitry Andric 82441fd87a68SDimitry Andric for (const auto &A : Args.getAllArgValues(options::OPT_Xcuda_ptxas)) 82451fd87a68SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ptxas-args=" + A)); 82461fd87a68SDimitry Andric 82471fd87a68SDimitry Andric // Forward remarks passes to the LLVM backend in the wrapper. 82481fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_EQ)) 82491fd87a68SDimitry Andric CmdArgs.push_back( 82501fd87a68SDimitry Andric Args.MakeArgString(Twine("-pass-remarks=") + A->getValue())); 82511fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_missed_EQ)) 82521fd87a68SDimitry Andric CmdArgs.push_back( 82531fd87a68SDimitry Andric Args.MakeArgString(Twine("-pass-remarks-missed=") + A->getValue())); 82541fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_analysis_EQ)) 82551fd87a68SDimitry Andric CmdArgs.push_back( 82561fd87a68SDimitry Andric Args.MakeArgString(Twine("-pass-remarks-analysis=") + A->getValue())); 82571fd87a68SDimitry Andric if (Args.getLastArg(options::OPT_save_temps_EQ)) 82581fd87a68SDimitry Andric CmdArgs.push_back("-save-temps"); 82591fd87a68SDimitry Andric 8260d56accc7SDimitry Andric // Construct the link job so we can wrap around it. 8261d56accc7SDimitry Andric Linker->ConstructJob(C, JA, Output, Inputs, Args, LinkingOutput); 8262d56accc7SDimitry Andric const auto &LinkCommand = C.getJobs().getJobs().back(); 8263d56accc7SDimitry Andric 82641fd87a68SDimitry Andric // Add the linker arguments to be forwarded by the wrapper. 82651fd87a68SDimitry Andric CmdArgs.push_back("-linker-path"); 82661fd87a68SDimitry Andric CmdArgs.push_back(LinkCommand->getExecutable()); 82671fd87a68SDimitry Andric CmdArgs.push_back("--"); 82681fd87a68SDimitry Andric for (const char *LinkArg : LinkCommand->getArguments()) 82691fd87a68SDimitry Andric CmdArgs.push_back(LinkArg); 82701fd87a68SDimitry Andric 82711fd87a68SDimitry Andric const char *Exec = 82721fd87a68SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath("clang-linker-wrapper")); 82731fd87a68SDimitry Andric 82741fd87a68SDimitry Andric // Replace the executable and arguments of the link job with the 82751fd87a68SDimitry Andric // wrapper. 82761fd87a68SDimitry Andric LinkCommand->replaceExecutable(Exec); 82771fd87a68SDimitry Andric LinkCommand->replaceArguments(CmdArgs); 82781fd87a68SDimitry Andric } 8279