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