1//===-- Clang.cpp - Clang+LLVM ToolChain Implementations --------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Clang.h"
10#include "Arch/ARM.h"
11#include "Arch/LoongArch.h"
12#include "Arch/Mips.h"
13#include "Arch/PPC.h"
14#include "Arch/RISCV.h"
15#include "Arch/Sparc.h"
16#include "Arch/SystemZ.h"
17#include "Hexagon.h"
18#include "PS4CPU.h"
19#include "clang/Basic/CLWarnings.h"
20#include "clang/Basic/CodeGenOptions.h"
21#include "clang/Basic/HeaderInclude.h"
22#include "clang/Basic/LangOptions.h"
23#include "clang/Basic/MakeSupport.h"
24#include "clang/Basic/ObjCRuntime.h"
25#include "clang/Basic/Version.h"
26#include "clang/Config/config.h"
27#include "clang/Driver/Action.h"
28#include "clang/Driver/CommonArgs.h"
29#include "clang/Driver/Distro.h"
30#include "clang/Driver/InputInfo.h"
31#include "clang/Driver/Options.h"
32#include "clang/Driver/SanitizerArgs.h"
33#include "clang/Driver/Types.h"
34#include "clang/Driver/XRayArgs.h"
35#include "llvm/ADT/ScopeExit.h"
36#include "llvm/ADT/SmallSet.h"
37#include "llvm/ADT/StringExtras.h"
38#include "llvm/BinaryFormat/Magic.h"
39#include "llvm/Config/llvm-config.h"
40#include "llvm/Frontend/Debug/Options.h"
41#include "llvm/Object/ObjectFile.h"
42#include "llvm/Option/ArgList.h"
43#include "llvm/Support/CodeGen.h"
44#include "llvm/Support/Compiler.h"
45#include "llvm/Support/Compression.h"
46#include "llvm/Support/Error.h"
47#include "llvm/Support/FileSystem.h"
48#include "llvm/Support/Path.h"
49#include "llvm/Support/Process.h"
50#include "llvm/Support/YAMLParser.h"
51#include "llvm/TargetParser/AArch64TargetParser.h"
52#include "llvm/TargetParser/ARMTargetParserCommon.h"
53#include "llvm/TargetParser/Host.h"
54#include "llvm/TargetParser/LoongArchTargetParser.h"
55#include "llvm/TargetParser/PPCTargetParser.h"
56#include "llvm/TargetParser/RISCVISAInfo.h"
57#include "llvm/TargetParser/RISCVTargetParser.h"
58#include <cctype>
59
60using namespace clang::driver;
61using namespace clang::driver::tools;
62using namespace clang;
63using namespace llvm::opt;
64
65static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
66 if (Arg *A = Args.getLastArg(Ids: clang::driver::options::OPT_C, Ids: options::OPT_CC,
67 Ids: options::OPT_fminimize_whitespace,
68 Ids: options::OPT_fno_minimize_whitespace,
69 Ids: options::OPT_fkeep_system_includes,
70 Ids: options::OPT_fno_keep_system_includes)) {
71 if (!Args.hasArg(Ids: options::OPT_E) && !Args.hasArg(Ids: options::OPT__SLASH_P) &&
72 !Args.hasArg(Ids: options::OPT__SLASH_EP) && !D.CCCIsCPP()) {
73 D.Diag(DiagID: clang::diag::err_drv_argument_only_allowed_with)
74 << A->getBaseArg().getAsString(Args)
75 << (D.IsCLMode() ? "/E, /P or /EP" : "-E");
76 }
77 }
78}
79
80static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
81 // In gcc, only ARM checks this, but it seems reasonable to check universally.
82 if (Args.hasArg(Ids: options::OPT_static))
83 if (const Arg *A =
84 Args.getLastArg(Ids: options::OPT_dynamic, Ids: options::OPT_mdynamic_no_pic))
85 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with) << A->getAsString(Args)
86 << "-static";
87}
88
89/// Apply \a Work on the current tool chain \a RegularToolChain and any other
90/// offloading tool chain that is associated with the current action \a JA.
91static void
92forAllAssociatedToolChains(Compilation &C, const JobAction &JA,
93 const ToolChain &RegularToolChain,
94 llvm::function_ref<void(const ToolChain &)> Work) {
95 // Apply Work on the current/regular tool chain.
96 Work(RegularToolChain);
97
98 // Apply Work on all the offloading tool chains associated with the current
99 // action.
100 if (JA.isHostOffloading(OKind: Action::OFK_Cuda))
101 Work(*C.getSingleOffloadToolChain<Action::OFK_Cuda>());
102 else if (JA.isDeviceOffloading(OKind: Action::OFK_Cuda))
103 Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
104 else if (JA.isHostOffloading(OKind: Action::OFK_HIP))
105 Work(*C.getSingleOffloadToolChain<Action::OFK_HIP>());
106 else if (JA.isDeviceOffloading(OKind: Action::OFK_HIP))
107 Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
108
109 if (JA.isHostOffloading(OKind: Action::OFK_OpenMP)) {
110 auto TCs = C.getOffloadToolChains<Action::OFK_OpenMP>();
111 for (auto II = TCs.first, IE = TCs.second; II != IE; ++II)
112 Work(*II->second);
113 } else if (JA.isDeviceOffloading(OKind: Action::OFK_OpenMP))
114 Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
115
116 if (JA.isHostOffloading(OKind: Action::OFK_SYCL)) {
117 auto TCs = C.getOffloadToolChains<Action::OFK_SYCL>();
118 for (auto II = TCs.first, IE = TCs.second; II != IE; ++II)
119 Work(*II->second);
120 } else if (JA.isDeviceOffloading(OKind: Action::OFK_SYCL))
121 Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
122
123 //
124 // TODO: Add support for other offloading programming models here.
125 //
126}
127
128static bool
129shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
130 const llvm::Triple &Triple) {
131 // We use the zero-cost exception tables for Objective-C if the non-fragile
132 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
133 // later.
134 if (runtime.isNonFragile())
135 return true;
136
137 if (!Triple.isMacOSX())
138 return false;
139
140 return (!Triple.isMacOSXVersionLT(Major: 10, Minor: 5) &&
141 (Triple.getArch() == llvm::Triple::x86_64 ||
142 Triple.getArch() == llvm::Triple::arm));
143}
144
145/// Adds exception related arguments to the driver command arguments. There's a
146/// main flag, -fexceptions and also language specific flags to enable/disable
147/// C++ and Objective-C exceptions. This makes it possible to for example
148/// disable C++ exceptions but enable Objective-C exceptions.
149static bool addExceptionArgs(const ArgList &Args, types::ID InputType,
150 const ToolChain &TC, bool KernelOrKext,
151 const ObjCRuntime &objcRuntime,
152 ArgStringList &CmdArgs) {
153 const llvm::Triple &Triple = TC.getTriple();
154
155 if (KernelOrKext) {
156 // -mkernel and -fapple-kext imply no exceptions, so claim exception related
157 // arguments now to avoid warnings about unused arguments.
158 Args.ClaimAllArgs(Id0: options::OPT_fexceptions);
159 Args.ClaimAllArgs(Id0: options::OPT_fno_exceptions);
160 Args.ClaimAllArgs(Id0: options::OPT_fobjc_exceptions);
161 Args.ClaimAllArgs(Id0: options::OPT_fno_objc_exceptions);
162 Args.ClaimAllArgs(Id0: options::OPT_fcxx_exceptions);
163 Args.ClaimAllArgs(Id0: options::OPT_fno_cxx_exceptions);
164 Args.ClaimAllArgs(Id0: options::OPT_fasync_exceptions);
165 Args.ClaimAllArgs(Id0: options::OPT_fno_async_exceptions);
166 return false;
167 }
168
169 // See if the user explicitly enabled exceptions.
170 bool EH = Args.hasFlag(Pos: options::OPT_fexceptions, Neg: options::OPT_fno_exceptions,
171 Default: false);
172
173 // Async exceptions are Windows MSVC only.
174 if (Triple.isWindowsMSVCEnvironment()) {
175 bool EHa = Args.hasFlag(Pos: options::OPT_fasync_exceptions,
176 Neg: options::OPT_fno_async_exceptions, Default: false);
177 if (EHa) {
178 CmdArgs.push_back(Elt: "-fasync-exceptions");
179 EH = true;
180 }
181 }
182
183 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
184 // is not necessarily sensible, but follows GCC.
185 if (types::isObjC(Id: InputType) &&
186 Args.hasFlag(Pos: options::OPT_fobjc_exceptions,
187 Neg: options::OPT_fno_objc_exceptions, Default: true)) {
188 CmdArgs.push_back(Elt: "-fobjc-exceptions");
189
190 EH |= shouldUseExceptionTablesForObjCExceptions(runtime: objcRuntime, Triple);
191 }
192
193 if (types::isCXX(Id: InputType)) {
194 // Disable C++ EH by default on XCore and PS4/PS5.
195 bool CXXExceptionsEnabled = Triple.getArch() != llvm::Triple::xcore &&
196 !Triple.isPS() && !Triple.isDriverKit();
197 Arg *ExceptionArg = Args.getLastArg(
198 Ids: options::OPT_fcxx_exceptions, Ids: options::OPT_fno_cxx_exceptions,
199 Ids: options::OPT_fexceptions, Ids: options::OPT_fno_exceptions);
200 if (ExceptionArg)
201 CXXExceptionsEnabled =
202 ExceptionArg->getOption().matches(ID: options::OPT_fcxx_exceptions) ||
203 ExceptionArg->getOption().matches(ID: options::OPT_fexceptions);
204
205 if (CXXExceptionsEnabled) {
206 CmdArgs.push_back(Elt: "-fcxx-exceptions");
207
208 EH = true;
209 }
210 }
211
212 // OPT_fignore_exceptions means exception could still be thrown,
213 // but no clean up or catch would happen in current module.
214 // So we do not set EH to false.
215 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fignore_exceptions);
216
217 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fassume_nothrow_exception_dtor,
218 Neg: options::OPT_fno_assume_nothrow_exception_dtor);
219
220 if (EH)
221 CmdArgs.push_back(Elt: "-fexceptions");
222 return EH;
223}
224
225static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC,
226 const JobAction &JA) {
227 bool Default = true;
228 if (TC.getTriple().isOSDarwin()) {
229 // The native darwin assembler doesn't support the linker_option directives,
230 // so we disable them if we think the .s file will be passed to it.
231 Default = TC.useIntegratedAs();
232 }
233 // The linker_option directives are intended for host compilation.
234 if (JA.isDeviceOffloading(OKind: Action::OFK_Cuda) ||
235 JA.isDeviceOffloading(OKind: Action::OFK_HIP))
236 Default = false;
237 return Args.hasFlag(Pos: options::OPT_fautolink, Neg: options::OPT_fno_autolink,
238 Default);
239}
240
241/// Add a CC1 option to specify the debug compilation directory.
242static const char *addDebugCompDirArg(const ArgList &Args,
243 ArgStringList &CmdArgs,
244 const llvm::vfs::FileSystem &VFS) {
245 if (Arg *A = Args.getLastArg(Ids: options::OPT_ffile_compilation_dir_EQ,
246 Ids: options::OPT_fdebug_compilation_dir_EQ)) {
247 if (A->getOption().matches(ID: options::OPT_ffile_compilation_dir_EQ))
248 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-fdebug-compilation-dir=") +
249 A->getValue()));
250 else
251 A->render(Args, Output&: CmdArgs);
252 } else if (llvm::ErrorOr<std::string> CWD =
253 VFS.getCurrentWorkingDirectory()) {
254 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fdebug-compilation-dir=" + *CWD));
255 }
256 StringRef Path(CmdArgs.back());
257 return Path.substr(Start: Path.find(C: '=') + 1).data();
258}
259
260static void addDebugObjectName(const ArgList &Args, ArgStringList &CmdArgs,
261 const char *DebugCompilationDir,
262 const char *OutputFileName) {
263 // No need to generate a value for -object-file-name if it was provided.
264 for (auto *Arg : Args.filtered(Ids: options::OPT_Xclang))
265 if (StringRef(Arg->getValue()).starts_with(Prefix: "-object-file-name"))
266 return;
267
268 if (Args.hasArg(Ids: options::OPT_object_file_name_EQ))
269 return;
270
271 SmallString<128> ObjFileNameForDebug(OutputFileName);
272 if (ObjFileNameForDebug != "-" &&
273 !llvm::sys::path::is_absolute(path: ObjFileNameForDebug) &&
274 (!DebugCompilationDir ||
275 llvm::sys::path::is_absolute(path: DebugCompilationDir))) {
276 // Make the path absolute in the debug infos like MSVC does.
277 llvm::sys::fs::make_absolute(path&: ObjFileNameForDebug);
278 }
279 // If the object file name is a relative path, then always use Windows
280 // backslash style as -object-file-name is used for embedding object file path
281 // in codeview and it can only be generated when targeting on Windows.
282 // Otherwise, just use native absolute path.
283 llvm::sys::path::Style Style =
284 llvm::sys::path::is_absolute(path: ObjFileNameForDebug)
285 ? llvm::sys::path::Style::native
286 : llvm::sys::path::Style::windows_backslash;
287 llvm::sys::path::remove_dots(path&: ObjFileNameForDebug, /*remove_dot_dot=*/true,
288 style: Style);
289 CmdArgs.push_back(
290 Elt: Args.MakeArgString(Str: Twine("-object-file-name=") + ObjFileNameForDebug));
291}
292
293/// Add a CC1 and CC1AS option to specify the debug file path prefix map.
294static void addDebugPrefixMapArg(const Driver &D, const ToolChain &TC,
295 const ArgList &Args, ArgStringList &CmdArgs) {
296 auto AddOneArg = [&](StringRef Map, StringRef Name) {
297 if (!Map.contains(C: '='))
298 D.Diag(DiagID: diag::err_drv_invalid_argument_to_option) << Map << Name;
299 else
300 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fdebug-prefix-map=" + Map));
301 };
302
303 for (const Arg *A : Args.filtered(Ids: options::OPT_ffile_prefix_map_EQ,
304 Ids: options::OPT_fdebug_prefix_map_EQ)) {
305 AddOneArg(A->getValue(), A->getOption().getName());
306 A->claim();
307 }
308 std::string GlobalRemapEntry = TC.GetGlobalDebugPathRemapping();
309 if (GlobalRemapEntry.empty())
310 return;
311 AddOneArg(GlobalRemapEntry, "environment");
312}
313
314/// Add a CC1 and CC1AS option to specify the macro file path prefix map.
315static void addMacroPrefixMapArg(const Driver &D, const ArgList &Args,
316 ArgStringList &CmdArgs) {
317 for (const Arg *A : Args.filtered(Ids: options::OPT_ffile_prefix_map_EQ,
318 Ids: options::OPT_fmacro_prefix_map_EQ)) {
319 StringRef Map = A->getValue();
320 if (!Map.contains(C: '='))
321 D.Diag(DiagID: diag::err_drv_invalid_argument_to_option)
322 << Map << A->getOption().getName();
323 else
324 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fmacro-prefix-map=" + Map));
325 A->claim();
326 }
327}
328
329/// Add a CC1 and CC1AS option to specify the coverage file path prefix map.
330static void addCoveragePrefixMapArg(const Driver &D, const ArgList &Args,
331 ArgStringList &CmdArgs) {
332 for (const Arg *A : Args.filtered(Ids: options::OPT_ffile_prefix_map_EQ,
333 Ids: options::OPT_fcoverage_prefix_map_EQ)) {
334 StringRef Map = A->getValue();
335 if (!Map.contains(C: '='))
336 D.Diag(DiagID: diag::err_drv_invalid_argument_to_option)
337 << Map << A->getOption().getName();
338 else
339 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fcoverage-prefix-map=" + Map));
340 A->claim();
341 }
342}
343
344/// Add -x lang to \p CmdArgs for \p Input.
345static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
346 ArgStringList &CmdArgs) {
347 // When using -verify-pch, we don't want to provide the type
348 // 'precompiled-header' if it was inferred from the file extension
349 if (Args.hasArg(Ids: options::OPT_verify_pch) && Input.getType() == types::TY_PCH)
350 return;
351
352 CmdArgs.push_back(Elt: "-x");
353 if (Args.hasArg(Ids: options::OPT_rewrite_objc))
354 CmdArgs.push_back(Elt: types::getTypeName(Id: types::TY_ObjCXX));
355 else {
356 // Map the driver type to the frontend type. This is mostly an identity
357 // mapping, except that the distinction between module interface units
358 // and other source files does not exist at the frontend layer.
359 const char *ClangType;
360 switch (Input.getType()) {
361 case types::TY_CXXModule:
362 ClangType = "c++";
363 break;
364 case types::TY_PP_CXXModule:
365 ClangType = "c++-cpp-output";
366 break;
367 default:
368 ClangType = types::getTypeName(Id: Input.getType());
369 break;
370 }
371 CmdArgs.push_back(Elt: ClangType);
372 }
373}
374
375static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
376 const JobAction &JA, const InputInfo &Output,
377 const ArgList &Args, SanitizerArgs &SanArgs,
378 ArgStringList &CmdArgs) {
379 const Driver &D = TC.getDriver();
380 const llvm::Triple &T = TC.getTriple();
381 auto *PGOGenerateArg = Args.getLastArg(Ids: options::OPT_fprofile_generate,
382 Ids: options::OPT_fprofile_generate_EQ,
383 Ids: options::OPT_fno_profile_generate);
384 if (PGOGenerateArg &&
385 PGOGenerateArg->getOption().matches(ID: options::OPT_fno_profile_generate))
386 PGOGenerateArg = nullptr;
387
388 auto *CSPGOGenerateArg = getLastCSProfileGenerateArg(Args);
389
390 auto *ProfileGenerateArg = Args.getLastArg(
391 Ids: options::OPT_fprofile_instr_generate,
392 Ids: options::OPT_fprofile_instr_generate_EQ,
393 Ids: options::OPT_fno_profile_instr_generate);
394 if (ProfileGenerateArg &&
395 ProfileGenerateArg->getOption().matches(
396 ID: options::OPT_fno_profile_instr_generate))
397 ProfileGenerateArg = nullptr;
398
399 if (PGOGenerateArg && ProfileGenerateArg)
400 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
401 << PGOGenerateArg->getSpelling() << ProfileGenerateArg->getSpelling();
402
403 auto *ProfileUseArg = getLastProfileUseArg(Args);
404
405 if (PGOGenerateArg && ProfileUseArg)
406 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
407 << ProfileUseArg->getSpelling() << PGOGenerateArg->getSpelling();
408
409 if (ProfileGenerateArg && ProfileUseArg)
410 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
411 << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling();
412
413 if (CSPGOGenerateArg && PGOGenerateArg) {
414 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
415 << CSPGOGenerateArg->getSpelling() << PGOGenerateArg->getSpelling();
416 PGOGenerateArg = nullptr;
417 }
418
419 if (TC.getTriple().isOSAIX()) {
420 if (Arg *ProfileSampleUseArg = getLastProfileSampleUseArg(Args))
421 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
422 << ProfileSampleUseArg->getSpelling() << TC.getTriple().str();
423 }
424
425 if (ProfileGenerateArg) {
426 if (ProfileGenerateArg->getOption().matches(
427 ID: options::OPT_fprofile_instr_generate_EQ))
428 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-fprofile-instrument-path=") +
429 ProfileGenerateArg->getValue()));
430 // The default is to use Clang Instrumentation.
431 CmdArgs.push_back(Elt: "-fprofile-instrument=clang");
432 if (TC.getTriple().isWindowsMSVCEnvironment() &&
433 Args.hasFlag(Pos: options::OPT_frtlib_defaultlib,
434 Neg: options::OPT_fno_rtlib_defaultlib, Default: true)) {
435 // Add dependent lib for clang_rt.profile
436 CmdArgs.push_back(Elt: Args.MakeArgString(
437 Str: "--dependent-lib=" + TC.getCompilerRTBasename(Args, Component: "profile")));
438 }
439 }
440
441 if (auto *ColdFuncCoverageArg = Args.getLastArg(
442 Ids: options::OPT_fprofile_generate_cold_function_coverage,
443 Ids: options::OPT_fprofile_generate_cold_function_coverage_EQ)) {
444 SmallString<128> Path(
445 ColdFuncCoverageArg->getOption().matches(
446 ID: options::OPT_fprofile_generate_cold_function_coverage_EQ)
447 ? ColdFuncCoverageArg->getValue()
448 : "");
449 llvm::sys::path::append(path&: Path, a: "default_%m.profraw");
450 // FIXME: Idealy the file path should be passed through
451 // `-fprofile-instrument-path=`(InstrProfileOutput), however, this field is
452 // shared with other profile use path(see PGOOptions), we need to refactor
453 // PGOOptions to make it work.
454 CmdArgs.push_back(Elt: "-mllvm");
455 CmdArgs.push_back(Elt: Args.MakeArgString(
456 Str: Twine("--instrument-cold-function-only-path=") + Path));
457 CmdArgs.push_back(Elt: "-mllvm");
458 CmdArgs.push_back(Elt: "--pgo-instrument-cold-function-only");
459 CmdArgs.push_back(Elt: "-mllvm");
460 CmdArgs.push_back(Elt: "--pgo-function-entry-coverage");
461 CmdArgs.push_back(Elt: "-fprofile-instrument=sample-coldcov");
462 }
463
464 if (auto *A = Args.getLastArg(Ids: options::OPT_ftemporal_profile)) {
465 if (!PGOGenerateArg && !CSPGOGenerateArg)
466 D.Diag(DiagID: clang::diag::err_drv_argument_only_allowed_with)
467 << A->getSpelling() << "-fprofile-generate or -fcs-profile-generate";
468 CmdArgs.push_back(Elt: "-mllvm");
469 CmdArgs.push_back(Elt: "--pgo-temporal-instrumentation");
470 }
471
472 Arg *PGOGenArg = nullptr;
473 if (PGOGenerateArg) {
474 assert(!CSPGOGenerateArg);
475 PGOGenArg = PGOGenerateArg;
476 CmdArgs.push_back(Elt: "-fprofile-instrument=llvm");
477 }
478 if (CSPGOGenerateArg) {
479 assert(!PGOGenerateArg);
480 PGOGenArg = CSPGOGenerateArg;
481 CmdArgs.push_back(Elt: "-fprofile-instrument=csllvm");
482 }
483 if (PGOGenArg) {
484 if (TC.getTriple().isWindowsMSVCEnvironment() &&
485 Args.hasFlag(Pos: options::OPT_frtlib_defaultlib,
486 Neg: options::OPT_fno_rtlib_defaultlib, Default: true)) {
487 // Add dependent lib for clang_rt.profile
488 CmdArgs.push_back(Elt: Args.MakeArgString(
489 Str: "--dependent-lib=" + TC.getCompilerRTBasename(Args, Component: "profile")));
490 }
491 if (PGOGenArg->getOption().matches(
492 ID: PGOGenerateArg ? options::OPT_fprofile_generate_EQ
493 : options::OPT_fcs_profile_generate_EQ)) {
494 SmallString<128> Path(PGOGenArg->getValue());
495 llvm::sys::path::append(path&: Path, a: "default_%m.profraw");
496 CmdArgs.push_back(
497 Elt: Args.MakeArgString(Str: Twine("-fprofile-instrument-path=") + Path));
498 }
499 }
500
501 if (ProfileUseArg) {
502 if (ProfileUseArg->getOption().matches(ID: options::OPT_fprofile_instr_use_EQ))
503 CmdArgs.push_back(Elt: Args.MakeArgString(
504 Str: Twine("-fprofile-instrument-use-path=") + ProfileUseArg->getValue()));
505 else if ((ProfileUseArg->getOption().matches(
506 ID: options::OPT_fprofile_use_EQ) ||
507 ProfileUseArg->getOption().matches(
508 ID: options::OPT_fprofile_instr_use))) {
509 SmallString<128> Path(
510 ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue());
511 if (Path.empty() || llvm::sys::fs::is_directory(Path))
512 llvm::sys::path::append(path&: Path, a: "default.profdata");
513 CmdArgs.push_back(
514 Elt: Args.MakeArgString(Str: Twine("-fprofile-instrument-use-path=") + Path));
515 }
516 }
517
518 bool EmitCovNotes = Args.hasFlag(Pos: options::OPT_ftest_coverage,
519 Neg: options::OPT_fno_test_coverage, Default: false) ||
520 Args.hasArg(Ids: options::OPT_coverage);
521 bool EmitCovData = TC.needsGCovInstrumentation(Args);
522
523 if (Args.hasFlag(Pos: options::OPT_fcoverage_mapping,
524 Neg: options::OPT_fno_coverage_mapping, Default: false)) {
525 if (!ProfileGenerateArg)
526 D.Diag(DiagID: clang::diag::err_drv_argument_only_allowed_with)
527 << "-fcoverage-mapping"
528 << "-fprofile-instr-generate";
529
530 CmdArgs.push_back(Elt: "-fcoverage-mapping");
531 }
532
533 if (Args.hasFlag(Pos: options::OPT_fmcdc_coverage, Neg: options::OPT_fno_mcdc_coverage,
534 Default: false)) {
535 if (!Args.hasFlag(Pos: options::OPT_fcoverage_mapping,
536 Neg: options::OPT_fno_coverage_mapping, Default: false))
537 D.Diag(DiagID: clang::diag::err_drv_argument_only_allowed_with)
538 << "-fcoverage-mcdc"
539 << "-fcoverage-mapping";
540
541 CmdArgs.push_back(Elt: "-fcoverage-mcdc");
542 }
543
544 if (Arg *A = Args.getLastArg(Ids: options::OPT_ffile_compilation_dir_EQ,
545 Ids: options::OPT_fcoverage_compilation_dir_EQ)) {
546 if (A->getOption().matches(ID: options::OPT_ffile_compilation_dir_EQ))
547 CmdArgs.push_back(Elt: Args.MakeArgString(
548 Str: Twine("-fcoverage-compilation-dir=") + A->getValue()));
549 else
550 A->render(Args, Output&: CmdArgs);
551 } else if (llvm::ErrorOr<std::string> CWD =
552 D.getVFS().getCurrentWorkingDirectory()) {
553 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fcoverage-compilation-dir=" + *CWD));
554 }
555
556 if (Args.hasArg(Ids: options::OPT_fprofile_exclude_files_EQ)) {
557 auto *Arg = Args.getLastArg(Ids: options::OPT_fprofile_exclude_files_EQ);
558 if (!Args.hasArg(Ids: options::OPT_coverage))
559 D.Diag(DiagID: clang::diag::err_drv_argument_only_allowed_with)
560 << "-fprofile-exclude-files="
561 << "--coverage";
562
563 StringRef v = Arg->getValue();
564 CmdArgs.push_back(
565 Elt: Args.MakeArgString(Str: Twine("-fprofile-exclude-files=" + v)));
566 }
567
568 if (Args.hasArg(Ids: options::OPT_fprofile_filter_files_EQ)) {
569 auto *Arg = Args.getLastArg(Ids: options::OPT_fprofile_filter_files_EQ);
570 if (!Args.hasArg(Ids: options::OPT_coverage))
571 D.Diag(DiagID: clang::diag::err_drv_argument_only_allowed_with)
572 << "-fprofile-filter-files="
573 << "--coverage";
574
575 StringRef v = Arg->getValue();
576 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-fprofile-filter-files=" + v)));
577 }
578
579 if (const auto *A = Args.getLastArg(Ids: options::OPT_fprofile_update_EQ)) {
580 StringRef Val = A->getValue();
581 if (Val == "atomic" || Val == "prefer-atomic")
582 CmdArgs.push_back(Elt: "-fprofile-update=atomic");
583 else if (Val != "single")
584 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
585 << A->getSpelling() << Val;
586 }
587 if (const auto *A = Args.getLastArg(Ids: options::OPT_fprofile_continuous)) {
588 if (!PGOGenerateArg && !CSPGOGenerateArg && !ProfileGenerateArg)
589 D.Diag(DiagID: clang::diag::err_drv_argument_only_allowed_with)
590 << A->getSpelling()
591 << "-fprofile-generate, -fprofile-instr-generate, or "
592 "-fcs-profile-generate";
593 else {
594 CmdArgs.push_back(Elt: "-fprofile-continuous");
595 // Platforms that require a bias variable:
596 if (T.isOSBinFormatELF() || T.isOSAIX() || T.isOSWindows()) {
597 CmdArgs.push_back(Elt: "-mllvm");
598 CmdArgs.push_back(Elt: "-runtime-counter-relocation");
599 }
600 // -fprofile-instr-generate does not decide the profile file name in the
601 // FE, and so it does not define the filename symbol
602 // (__llvm_profile_filename). Instead, the runtime uses the name
603 // "default.profraw" for the profile file. When continuous mode is ON, we
604 // will create the filename symbol so that we can insert the "%c"
605 // modifier.
606 if (ProfileGenerateArg &&
607 (ProfileGenerateArg->getOption().matches(
608 ID: options::OPT_fprofile_instr_generate) ||
609 (ProfileGenerateArg->getOption().matches(
610 ID: options::OPT_fprofile_instr_generate_EQ) &&
611 strlen(s: ProfileGenerateArg->getValue()) == 0)))
612 CmdArgs.push_back(Elt: "-fprofile-instrument-path=default.profraw");
613 }
614 }
615
616 int FunctionGroups = 1;
617 int SelectedFunctionGroup = 0;
618 if (const auto *A = Args.getLastArg(Ids: options::OPT_fprofile_function_groups)) {
619 StringRef Val = A->getValue();
620 if (Val.getAsInteger(Radix: 0, Result&: FunctionGroups) || FunctionGroups < 1)
621 D.Diag(DiagID: diag::err_drv_invalid_int_value) << A->getAsString(Args) << Val;
622 }
623 if (const auto *A =
624 Args.getLastArg(Ids: options::OPT_fprofile_selected_function_group)) {
625 StringRef Val = A->getValue();
626 if (Val.getAsInteger(Radix: 0, Result&: SelectedFunctionGroup) ||
627 SelectedFunctionGroup < 0 || SelectedFunctionGroup >= FunctionGroups)
628 D.Diag(DiagID: diag::err_drv_invalid_int_value) << A->getAsString(Args) << Val;
629 }
630 if (FunctionGroups != 1)
631 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fprofile-function-groups=" +
632 Twine(FunctionGroups)));
633 if (SelectedFunctionGroup != 0)
634 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fprofile-selected-function-group=" +
635 Twine(SelectedFunctionGroup)));
636
637 // Leave -fprofile-dir= an unused argument unless .gcda emission is
638 // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider
639 // the flag used. There is no -fno-profile-dir, so the user has no
640 // targeted way to suppress the warning.
641 Arg *FProfileDir = nullptr;
642 if (Args.hasArg(Ids: options::OPT_fprofile_arcs) ||
643 Args.hasArg(Ids: options::OPT_coverage))
644 FProfileDir = Args.getLastArg(Ids: options::OPT_fprofile_dir);
645
646 // Put the .gcno and .gcda files (if needed) next to the primary output file,
647 // or fall back to a file in the current directory for `clang -c --coverage
648 // d/a.c` in the absence of -o.
649 if (EmitCovNotes || EmitCovData) {
650 SmallString<128> CoverageFilename;
651 if (Arg *DumpDir = Args.getLastArgNoClaim(Ids: options::OPT_dumpdir)) {
652 // Form ${dumpdir}${basename}.gcno. Note that dumpdir may not end with a
653 // path separator.
654 CoverageFilename = DumpDir->getValue();
655 CoverageFilename += llvm::sys::path::filename(path: Output.getBaseInput());
656 } else if (Arg *FinalOutput =
657 C.getArgs().getLastArg(Ids: options::OPT__SLASH_Fo)) {
658 CoverageFilename = FinalOutput->getValue();
659 } else if (Arg *FinalOutput = C.getArgs().getLastArg(Ids: options::OPT_o)) {
660 CoverageFilename = FinalOutput->getValue();
661 } else {
662 CoverageFilename = llvm::sys::path::filename(path: Output.getBaseInput());
663 }
664 if (llvm::sys::path::is_relative(path: CoverageFilename))
665 (void)D.getVFS().makeAbsolute(Path&: CoverageFilename);
666 llvm::sys::path::replace_extension(path&: CoverageFilename, extension: "gcno");
667 if (EmitCovNotes) {
668 CmdArgs.push_back(
669 Elt: Args.MakeArgString(Str: "-coverage-notes-file=" + CoverageFilename));
670 }
671
672 if (EmitCovData) {
673 if (FProfileDir) {
674 SmallString<128> Gcno = std::move(CoverageFilename);
675 CoverageFilename = FProfileDir->getValue();
676 llvm::sys::path::append(path&: CoverageFilename, a: Gcno);
677 }
678 llvm::sys::path::replace_extension(path&: CoverageFilename, extension: "gcda");
679 CmdArgs.push_back(
680 Elt: Args.MakeArgString(Str: "-coverage-data-file=" + CoverageFilename));
681 }
682 }
683}
684
685static void
686RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs,
687 llvm::codegenoptions::DebugInfoKind DebugInfoKind,
688 unsigned DwarfVersion,
689 llvm::DebuggerKind DebuggerTuning) {
690 addDebugInfoKind(CmdArgs, DebugInfoKind);
691 if (DwarfVersion > 0)
692 CmdArgs.push_back(
693 Elt: Args.MakeArgString(Str: "-dwarf-version=" + Twine(DwarfVersion)));
694 switch (DebuggerTuning) {
695 case llvm::DebuggerKind::GDB:
696 CmdArgs.push_back(Elt: "-debugger-tuning=gdb");
697 break;
698 case llvm::DebuggerKind::LLDB:
699 CmdArgs.push_back(Elt: "-debugger-tuning=lldb");
700 break;
701 case llvm::DebuggerKind::SCE:
702 CmdArgs.push_back(Elt: "-debugger-tuning=sce");
703 break;
704 case llvm::DebuggerKind::DBX:
705 CmdArgs.push_back(Elt: "-debugger-tuning=dbx");
706 break;
707 default:
708 break;
709 }
710}
711
712static bool checkDebugInfoOption(const Arg *A, const ArgList &Args,
713 const Driver &D, const ToolChain &TC) {
714 assert(A && "Expected non-nullptr argument.");
715 if (TC.supportsDebugInfoOption(A))
716 return true;
717 D.Diag(DiagID: diag::warn_drv_unsupported_debug_info_opt_for_target)
718 << A->getAsString(Args) << TC.getTripleString();
719 return false;
720}
721
722static void RenderDebugInfoCompressionArgs(const ArgList &Args,
723 ArgStringList &CmdArgs,
724 const Driver &D,
725 const ToolChain &TC) {
726 const Arg *A = Args.getLastArg(Ids: options::OPT_gz_EQ);
727 if (!A)
728 return;
729 if (checkDebugInfoOption(A, Args, D, TC)) {
730 StringRef Value = A->getValue();
731 if (Value == "none") {
732 CmdArgs.push_back(Elt: "--compress-debug-sections=none");
733 } else if (Value == "zlib") {
734 if (llvm::compression::zlib::isAvailable()) {
735 CmdArgs.push_back(
736 Elt: Args.MakeArgString(Str: "--compress-debug-sections=" + Twine(Value)));
737 } else {
738 D.Diag(DiagID: diag::warn_debug_compression_unavailable) << "zlib";
739 }
740 } else if (Value == "zstd") {
741 if (llvm::compression::zstd::isAvailable()) {
742 CmdArgs.push_back(
743 Elt: Args.MakeArgString(Str: "--compress-debug-sections=" + Twine(Value)));
744 } else {
745 D.Diag(DiagID: diag::warn_debug_compression_unavailable) << "zstd";
746 }
747 } else {
748 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
749 << A->getSpelling() << Value;
750 }
751 }
752}
753
754static void handleAMDGPUCodeObjectVersionOptions(const Driver &D,
755 const ArgList &Args,
756 ArgStringList &CmdArgs,
757 bool IsCC1As = false) {
758 // If no version was requested by the user, use the default value from the
759 // back end. This is consistent with the value returned from
760 // getAMDGPUCodeObjectVersion. This lets clang emit IR for amdgpu without
761 // requiring the corresponding llvm to have the AMDGPU target enabled,
762 // provided the user (e.g. front end tests) can use the default.
763 if (haveAMDGPUCodeObjectVersionArgument(D, Args)) {
764 unsigned CodeObjVer = getAMDGPUCodeObjectVersion(D, Args);
765 CmdArgs.insert(I: CmdArgs.begin() + 1,
766 Elt: Args.MakeArgString(Str: Twine("--amdhsa-code-object-version=") +
767 Twine(CodeObjVer)));
768 CmdArgs.insert(I: CmdArgs.begin() + 1, Elt: "-mllvm");
769 // -cc1as does not accept -mcode-object-version option.
770 if (!IsCC1As)
771 CmdArgs.insert(I: CmdArgs.begin() + 1,
772 Elt: Args.MakeArgString(Str: Twine("-mcode-object-version=") +
773 Twine(CodeObjVer)));
774 }
775}
776
777static bool maybeHasClangPchSignature(const Driver &D, StringRef Path) {
778 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MemBuf =
779 D.getVFS().getBufferForFile(Name: Path);
780 if (!MemBuf)
781 return false;
782 llvm::file_magic Magic = llvm::identify_magic(magic: (*MemBuf)->getBuffer());
783 if (Magic == llvm::file_magic::unknown)
784 return false;
785 // Return true for both raw Clang AST files and object files which may
786 // contain a __clangast section.
787 if (Magic == llvm::file_magic::clang_ast)
788 return true;
789 Expected<std::unique_ptr<llvm::object::ObjectFile>> Obj =
790 llvm::object::ObjectFile::createObjectFile(Object: **MemBuf, Type: Magic);
791 return !Obj.takeError();
792}
793
794static bool gchProbe(const Driver &D, StringRef Path) {
795 llvm::ErrorOr<llvm::vfs::Status> Status = D.getVFS().status(Path);
796 if (!Status)
797 return false;
798
799 if (Status->isDirectory()) {
800 std::error_code EC;
801 for (llvm::vfs::directory_iterator DI = D.getVFS().dir_begin(Dir: Path, EC), DE;
802 !EC && DI != DE; DI = DI.increment(EC)) {
803 if (maybeHasClangPchSignature(D, Path: DI->path()))
804 return true;
805 }
806 D.Diag(DiagID: diag::warn_drv_pch_ignoring_gch_dir) << Path;
807 return false;
808 }
809
810 if (maybeHasClangPchSignature(D, Path))
811 return true;
812 D.Diag(DiagID: diag::warn_drv_pch_ignoring_gch_file) << Path;
813 return false;
814}
815
816void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
817 const Driver &D, const ArgList &Args,
818 ArgStringList &CmdArgs,
819 const InputInfo &Output,
820 const InputInfoList &Inputs) const {
821 const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU();
822
823 CheckPreprocessingOptions(D, Args);
824
825 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_C);
826 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_CC);
827
828 // Handle dependency file generation.
829 Arg *ArgM = Args.getLastArg(Ids: options::OPT_MM);
830 if (!ArgM)
831 ArgM = Args.getLastArg(Ids: options::OPT_M);
832 Arg *ArgMD = Args.getLastArg(Ids: options::OPT_MMD);
833 if (!ArgMD)
834 ArgMD = Args.getLastArg(Ids: options::OPT_MD);
835
836 // -M and -MM imply -w.
837 if (ArgM)
838 CmdArgs.push_back(Elt: "-w");
839 else
840 ArgM = ArgMD;
841
842 if (ArgM) {
843 if (!JA.isDeviceOffloading(OKind: Action::OFK_HIP)) {
844 // Determine the output location.
845 const char *DepFile;
846 if (Arg *MF = Args.getLastArg(Ids: options::OPT_MF)) {
847 DepFile = MF->getValue();
848 C.addFailureResultFile(Name: DepFile, JA: &JA);
849 } else if (Output.getType() == types::TY_Dependencies) {
850 DepFile = Output.getFilename();
851 } else if (!ArgMD) {
852 DepFile = "-";
853 } else {
854 DepFile = getDependencyFileName(Args, Inputs);
855 C.addFailureResultFile(Name: DepFile, JA: &JA);
856 }
857 CmdArgs.push_back(Elt: "-dependency-file");
858 CmdArgs.push_back(Elt: DepFile);
859 }
860 // Cmake generates dependency files using all compilation options specified
861 // by users. Claim those not used for dependency files.
862 if (JA.isOffloading(OKind: Action::OFK_HIP)) {
863 Args.ClaimAllArgs(Id0: options::OPT_offload_compress);
864 Args.ClaimAllArgs(Id0: options::OPT_no_offload_compress);
865 Args.ClaimAllArgs(Id0: options::OPT_offload_jobs_EQ);
866 }
867
868 bool HasTarget = false;
869 for (const Arg *A : Args.filtered(Ids: options::OPT_MT, Ids: options::OPT_MQ)) {
870 HasTarget = true;
871 A->claim();
872 if (A->getOption().matches(ID: options::OPT_MT)) {
873 A->render(Args, Output&: CmdArgs);
874 } else {
875 CmdArgs.push_back(Elt: "-MT");
876 SmallString<128> Quoted;
877 quoteMakeTarget(Target: A->getValue(), Res&: Quoted);
878 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Quoted));
879 }
880 }
881
882 // Add a default target if one wasn't specified.
883 if (!HasTarget) {
884 const char *DepTarget;
885
886 // If user provided -o, that is the dependency target, except
887 // when we are only generating a dependency file.
888 Arg *OutputOpt = Args.getLastArg(Ids: options::OPT_o, Ids: options::OPT__SLASH_Fo);
889 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
890 DepTarget = OutputOpt->getValue();
891 } else {
892 // Otherwise derive from the base input.
893 //
894 // FIXME: This should use the computed output file location.
895 SmallString<128> P(Inputs[0].getBaseInput());
896 llvm::sys::path::replace_extension(path&: P, extension: "o");
897 DepTarget = Args.MakeArgString(Str: llvm::sys::path::filename(path: P));
898 }
899
900 CmdArgs.push_back(Elt: "-MT");
901 SmallString<128> Quoted;
902 quoteMakeTarget(Target: DepTarget, Res&: Quoted);
903 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Quoted));
904 }
905
906 if (ArgM->getOption().matches(ID: options::OPT_M) ||
907 ArgM->getOption().matches(ID: options::OPT_MD))
908 CmdArgs.push_back(Elt: "-sys-header-deps");
909 if ((isa<PrecompileJobAction>(Val: JA) &&
910 !Args.hasArg(Ids: options::OPT_fno_module_file_deps)) ||
911 Args.hasArg(Ids: options::OPT_fmodule_file_deps))
912 CmdArgs.push_back(Elt: "-module-file-deps");
913 }
914
915 if (Args.hasArg(Ids: options::OPT_MG)) {
916 if (!ArgM || ArgM->getOption().matches(ID: options::OPT_MD) ||
917 ArgM->getOption().matches(ID: options::OPT_MMD))
918 D.Diag(DiagID: diag::err_drv_mg_requires_m_or_mm);
919 CmdArgs.push_back(Elt: "-MG");
920 }
921
922 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_MP);
923 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_MV);
924
925 // Add offload include arguments specific for CUDA/HIP/SYCL. This must happen
926 // before we -I or -include anything else, because we must pick up the
927 // CUDA/HIP/SYCL headers from the particular CUDA/ROCm/SYCL installation,
928 // rather than from e.g. /usr/local/include.
929 if (JA.isOffloading(OKind: Action::OFK_Cuda))
930 getToolChain().AddCudaIncludeArgs(DriverArgs: Args, CC1Args&: CmdArgs);
931 if (JA.isOffloading(OKind: Action::OFK_HIP))
932 getToolChain().AddHIPIncludeArgs(DriverArgs: Args, CC1Args&: CmdArgs);
933 if (JA.isOffloading(OKind: Action::OFK_SYCL))
934 getToolChain().addSYCLIncludeArgs(DriverArgs: Args, CC1Args&: CmdArgs);
935
936 // If we are offloading to a target via OpenMP we need to include the
937 // openmp_wrappers folder which contains alternative system headers.
938 if (JA.isDeviceOffloading(OKind: Action::OFK_OpenMP) &&
939 !Args.hasArg(Ids: options::OPT_nostdinc) &&
940 Args.hasFlag(Pos: options::OPT_offload_inc, Neg: options::OPT_no_offload_inc,
941 Default: true) &&
942 getToolChain().getTriple().isGPU()) {
943 if (!Args.hasArg(Ids: options::OPT_nobuiltininc)) {
944 // Add openmp_wrappers/* to our system include path. This lets us wrap
945 // standard library headers.
946 SmallString<128> P(D.ResourceDir);
947 llvm::sys::path::append(path&: P, a: "include");
948 llvm::sys::path::append(path&: P, a: "openmp_wrappers");
949 CmdArgs.push_back(Elt: "-internal-isystem");
950 CmdArgs.push_back(Elt: Args.MakeArgString(Str: P));
951 }
952
953 CmdArgs.push_back(Elt: "-include");
954 CmdArgs.push_back(Elt: "__clang_openmp_device_functions.h");
955 }
956
957 if (Args.hasArg(Ids: options::OPT_foffload_via_llvm)) {
958 // Add llvm_wrappers/* to our system include path. This lets us wrap
959 // standard library headers and other headers.
960 SmallString<128> P(D.ResourceDir);
961 llvm::sys::path::append(path&: P, a: "include", b: "llvm_offload_wrappers");
962 CmdArgs.append(IL: {"-internal-isystem", Args.MakeArgString(Str: P), "-include"});
963 if (JA.isDeviceOffloading(OKind: Action::OFK_OpenMP))
964 CmdArgs.push_back(Elt: "__llvm_offload_device.h");
965 else
966 CmdArgs.push_back(Elt: "__llvm_offload_host.h");
967 }
968
969 // Add -i* options, and automatically translate to
970 // -include-pch/-include-pth for transparent PCH support. It's
971 // wonky, but we include looking for .gch so we can support seamless
972 // replacement into a build system already set up to be generating
973 // .gch files.
974
975 if (getToolChain().getDriver().IsCLMode()) {
976 const Arg *YcArg = Args.getLastArg(Ids: options::OPT__SLASH_Yc);
977 const Arg *YuArg = Args.getLastArg(Ids: options::OPT__SLASH_Yu);
978 if (YcArg && JA.getKind() >= Action::PrecompileJobClass &&
979 JA.getKind() <= Action::AssembleJobClass) {
980 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-building-pch-with-obj"));
981 // -fpch-instantiate-templates is the default when creating
982 // precomp using /Yc
983 if (Args.hasFlag(Pos: options::OPT_fpch_instantiate_templates,
984 Neg: options::OPT_fno_pch_instantiate_templates, Default: true))
985 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fpch-instantiate-templates"));
986 }
987 if (YcArg || YuArg) {
988 StringRef ThroughHeader = YcArg ? YcArg->getValue() : YuArg->getValue();
989 if (!isa<PrecompileJobAction>(Val: JA)) {
990 CmdArgs.push_back(Elt: "-include-pch");
991 CmdArgs.push_back(Elt: Args.MakeArgString(Str: D.GetClPchPath(
992 C, BaseName: !ThroughHeader.empty()
993 ? ThroughHeader
994 : llvm::sys::path::filename(path: Inputs[0].getBaseInput()))));
995 }
996
997 if (ThroughHeader.empty()) {
998 CmdArgs.push_back(Elt: Args.MakeArgString(
999 Str: Twine("-pch-through-hdrstop-") + (YcArg ? "create" : "use")));
1000 } else {
1001 CmdArgs.push_back(
1002 Elt: Args.MakeArgString(Str: Twine("-pch-through-header=") + ThroughHeader));
1003 }
1004 }
1005 }
1006
1007 bool RenderedImplicitInclude = false;
1008 for (const Arg *A : Args.filtered(Ids: options::OPT_clang_i_Group)) {
1009 if (A->getOption().matches(ID: options::OPT_include) &&
1010 D.getProbePrecompiled()) {
1011 // Handling of gcc-style gch precompiled headers.
1012 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
1013 RenderedImplicitInclude = true;
1014
1015 bool FoundPCH = false;
1016 SmallString<128> P(A->getValue());
1017 // We want the files to have a name like foo.h.pch. Add a dummy extension
1018 // so that replace_extension does the right thing.
1019 P += ".dummy";
1020 llvm::sys::path::replace_extension(path&: P, extension: "pch");
1021 if (D.getVFS().exists(Path: P))
1022 FoundPCH = true;
1023
1024 if (!FoundPCH) {
1025 // For GCC compat, probe for a file or directory ending in .gch instead.
1026 llvm::sys::path::replace_extension(path&: P, extension: "gch");
1027 FoundPCH = gchProbe(D, Path: P.str());
1028 }
1029
1030 if (FoundPCH) {
1031 if (IsFirstImplicitInclude) {
1032 A->claim();
1033 CmdArgs.push_back(Elt: "-include-pch");
1034 CmdArgs.push_back(Elt: Args.MakeArgString(Str: P));
1035 continue;
1036 } else {
1037 // Ignore the PCH if not first on command line and emit warning.
1038 D.Diag(DiagID: diag::warn_drv_pch_not_first_include) << P
1039 << A->getAsString(Args);
1040 }
1041 }
1042 } else if (A->getOption().matches(ID: options::OPT_isystem_after)) {
1043 // Handling of paths which must come late. These entries are handled by
1044 // the toolchain itself after the resource dir is inserted in the right
1045 // search order.
1046 // Do not claim the argument so that the use of the argument does not
1047 // silently go unnoticed on toolchains which do not honour the option.
1048 continue;
1049 } else if (A->getOption().matches(ID: options::OPT_stdlibxx_isystem)) {
1050 // Translated to -internal-isystem by the driver, no need to pass to cc1.
1051 continue;
1052 } else if (A->getOption().matches(ID: options::OPT_ibuiltininc)) {
1053 // This is used only by the driver. No need to pass to cc1.
1054 continue;
1055 }
1056
1057 // Not translated, render as usual.
1058 A->claim();
1059 A->render(Args, Output&: CmdArgs);
1060 }
1061
1062 Args.addAllArgs(Output&: CmdArgs,
1063 Ids: {options::OPT_D, options::OPT_U, options::OPT_I_Group,
1064 options::OPT_F, options::OPT_embed_dir_EQ});
1065
1066 // Add -Wp, and -Xpreprocessor if using the preprocessor.
1067
1068 // FIXME: There is a very unfortunate problem here, some troubled
1069 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
1070 // really support that we would have to parse and then translate
1071 // those options. :(
1072 Args.AddAllArgValues(Output&: CmdArgs, Id0: options::OPT_Wp_COMMA,
1073 Id1: options::OPT_Xpreprocessor);
1074
1075 // -I- is a deprecated GCC feature, reject it.
1076 if (Arg *A = Args.getLastArg(Ids: options::OPT_I_))
1077 D.Diag(DiagID: diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
1078
1079 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
1080 // -isysroot to the CC1 invocation.
1081 StringRef sysroot = C.getSysRoot();
1082 if (sysroot != "") {
1083 if (!Args.hasArg(Ids: options::OPT_isysroot)) {
1084 CmdArgs.push_back(Elt: "-isysroot");
1085 CmdArgs.push_back(Elt: C.getArgs().MakeArgString(Str: sysroot));
1086 }
1087 }
1088
1089 // Parse additional include paths from environment variables.
1090 // FIXME: We should probably sink the logic for handling these from the
1091 // frontend into the driver. It will allow deleting 4 otherwise unused flags.
1092 // CPATH - included following the user specified includes (but prior to
1093 // builtin and standard includes).
1094 addDirectoryList(Args, CmdArgs, ArgName: "-I", EnvVar: "CPATH");
1095 // C_INCLUDE_PATH - system includes enabled when compiling C.
1096 addDirectoryList(Args, CmdArgs, ArgName: "-c-isystem", EnvVar: "C_INCLUDE_PATH");
1097 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
1098 addDirectoryList(Args, CmdArgs, ArgName: "-cxx-isystem", EnvVar: "CPLUS_INCLUDE_PATH");
1099 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
1100 addDirectoryList(Args, CmdArgs, ArgName: "-objc-isystem", EnvVar: "OBJC_INCLUDE_PATH");
1101 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
1102 addDirectoryList(Args, CmdArgs, ArgName: "-objcxx-isystem", EnvVar: "OBJCPLUS_INCLUDE_PATH");
1103
1104 // While adding the include arguments, we also attempt to retrieve the
1105 // arguments of related offloading toolchains or arguments that are specific
1106 // of an offloading programming model.
1107
1108 // Add C++ include arguments, if needed.
1109 if (types::isCXX(Id: Inputs[0].getType())) {
1110 bool HasStdlibxxIsystem = Args.hasArg(Ids: options::OPT_stdlibxx_isystem);
1111 forAllAssociatedToolChains(
1112 C, JA, RegularToolChain: getToolChain(),
1113 Work: [&Args, &CmdArgs, HasStdlibxxIsystem](const ToolChain &TC) {
1114 HasStdlibxxIsystem ? TC.AddClangCXXStdlibIsystemArgs(DriverArgs: Args, CC1Args&: CmdArgs)
1115 : TC.AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args&: CmdArgs);
1116 });
1117 }
1118
1119 // If we are compiling for a GPU target we want to override the system headers
1120 // with ones created by the 'libc' project if present.
1121 // TODO: This should be moved to `AddClangSystemIncludeArgs` by passing the
1122 // OffloadKind as an argument.
1123 if (!Args.hasArg(Ids: options::OPT_nostdinc) &&
1124 Args.hasFlag(Pos: options::OPT_offload_inc, Neg: options::OPT_no_offload_inc,
1125 Default: true) &&
1126 !Args.hasArg(Ids: options::OPT_nobuiltininc)) {
1127 // Without an offloading language we will include these headers directly.
1128 // Offloading languages will instead only use the declarations stored in
1129 // the resource directory at clang/lib/Headers/llvm_libc_wrappers.
1130 if (getToolChain().getTriple().isGPU() &&
1131 C.getActiveOffloadKinds() == Action::OFK_None) {
1132 SmallString<128> P(llvm::sys::path::parent_path(path: D.Dir));
1133 llvm::sys::path::append(path&: P, a: "include");
1134 llvm::sys::path::append(path&: P, a: getToolChain().getTripleString());
1135 CmdArgs.push_back(Elt: "-internal-isystem");
1136 CmdArgs.push_back(Elt: Args.MakeArgString(Str: P));
1137 } else if (C.getActiveOffloadKinds() == Action::OFK_OpenMP) {
1138 // TODO: CUDA / HIP include their own headers for some common functions
1139 // implemented here. We'll need to clean those up so they do not conflict.
1140 SmallString<128> P(D.ResourceDir);
1141 llvm::sys::path::append(path&: P, a: "include");
1142 llvm::sys::path::append(path&: P, a: "llvm_libc_wrappers");
1143 CmdArgs.push_back(Elt: "-internal-isystem");
1144 CmdArgs.push_back(Elt: Args.MakeArgString(Str: P));
1145 }
1146 }
1147
1148 // Add system include arguments for all targets but IAMCU.
1149 if (!IsIAMCU)
1150 forAllAssociatedToolChains(C, JA, RegularToolChain: getToolChain(),
1151 Work: [&Args, &CmdArgs](const ToolChain &TC) {
1152 TC.AddClangSystemIncludeArgs(DriverArgs: Args, CC1Args&: CmdArgs);
1153 });
1154 else {
1155 // For IAMCU add special include arguments.
1156 getToolChain().AddIAMCUIncludeArgs(DriverArgs: Args, CC1Args&: CmdArgs);
1157 }
1158
1159 addMacroPrefixMapArg(D, Args, CmdArgs);
1160 addCoveragePrefixMapArg(D, Args, CmdArgs);
1161
1162 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ffile_reproducible,
1163 Ids: options::OPT_fno_file_reproducible);
1164
1165 if (const char *Epoch = std::getenv(name: "SOURCE_DATE_EPOCH")) {
1166 CmdArgs.push_back(Elt: "-source-date-epoch");
1167 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Epoch));
1168 }
1169
1170 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fdefine_target_os_macros,
1171 Neg: options::OPT_fno_define_target_os_macros);
1172}
1173
1174// FIXME: Move to target hook.
1175static bool isSignedCharDefault(const llvm::Triple &Triple) {
1176 switch (Triple.getArch()) {
1177 default:
1178 return true;
1179
1180 case llvm::Triple::aarch64:
1181 case llvm::Triple::aarch64_32:
1182 case llvm::Triple::aarch64_be:
1183 case llvm::Triple::arm:
1184 case llvm::Triple::armeb:
1185 case llvm::Triple::thumb:
1186 case llvm::Triple::thumbeb:
1187 if (Triple.isOSDarwin() || Triple.isOSWindows())
1188 return true;
1189 return false;
1190
1191 case llvm::Triple::ppc:
1192 case llvm::Triple::ppc64:
1193 if (Triple.isOSDarwin())
1194 return true;
1195 return false;
1196
1197 case llvm::Triple::csky:
1198 case llvm::Triple::hexagon:
1199 case llvm::Triple::msp430:
1200 case llvm::Triple::ppcle:
1201 case llvm::Triple::ppc64le:
1202 case llvm::Triple::riscv32:
1203 case llvm::Triple::riscv64:
1204 case llvm::Triple::systemz:
1205 case llvm::Triple::xcore:
1206 case llvm::Triple::xtensa:
1207 return false;
1208 }
1209}
1210
1211static bool hasMultipleInvocations(const llvm::Triple &Triple,
1212 const ArgList &Args) {
1213 // Supported only on Darwin where we invoke the compiler multiple times
1214 // followed by an invocation to lipo.
1215 if (!Triple.isOSDarwin())
1216 return false;
1217 // If more than one "-arch <arch>" is specified, we're targeting multiple
1218 // architectures resulting in a fat binary.
1219 return Args.getAllArgValues(Id: options::OPT_arch).size() > 1;
1220}
1221
1222static bool checkRemarksOptions(const Driver &D, const ArgList &Args,
1223 const llvm::Triple &Triple) {
1224 // When enabling remarks, we need to error if:
1225 // * The remark file is specified but we're targeting multiple architectures,
1226 // which means more than one remark file is being generated.
1227 bool hasMultipleInvocations = ::hasMultipleInvocations(Triple, Args);
1228 bool hasExplicitOutputFile =
1229 Args.getLastArg(Ids: options::OPT_foptimization_record_file_EQ);
1230 if (hasMultipleInvocations && hasExplicitOutputFile) {
1231 D.Diag(DiagID: diag::err_drv_invalid_output_with_multiple_archs)
1232 << "-foptimization-record-file";
1233 return false;
1234 }
1235 return true;
1236}
1237
1238static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
1239 const llvm::Triple &Triple,
1240 const InputInfo &Input,
1241 const InputInfo &Output, const JobAction &JA) {
1242 StringRef Format = "yaml";
1243 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fsave_optimization_record_EQ))
1244 Format = A->getValue();
1245
1246 CmdArgs.push_back(Elt: "-opt-record-file");
1247
1248 const Arg *A = Args.getLastArg(Ids: options::OPT_foptimization_record_file_EQ);
1249 if (A) {
1250 CmdArgs.push_back(Elt: A->getValue());
1251 } else {
1252 bool hasMultipleArchs =
1253 Triple.isOSDarwin() && // Only supported on Darwin platforms.
1254 Args.getAllArgValues(Id: options::OPT_arch).size() > 1;
1255
1256 SmallString<128> F;
1257
1258 if (Args.hasArg(Ids: options::OPT_c) || Args.hasArg(Ids: options::OPT_S)) {
1259 if (Arg *FinalOutput = Args.getLastArg(Ids: options::OPT_o))
1260 F = FinalOutput->getValue();
1261 } else {
1262 if (Format != "yaml" && // For YAML, keep the original behavior.
1263 Triple.isOSDarwin() && // Enable this only on darwin, since it's the only platform supporting .dSYM bundles.
1264 Output.isFilename())
1265 F = Output.getFilename();
1266 }
1267
1268 if (F.empty()) {
1269 // Use the input filename.
1270 F = llvm::sys::path::stem(path: Input.getBaseInput());
1271
1272 // If we're compiling for an offload architecture (i.e. a CUDA device),
1273 // we need to make the file name for the device compilation different
1274 // from the host compilation.
1275 if (!JA.isDeviceOffloading(OKind: Action::OFK_None) &&
1276 !JA.isDeviceOffloading(OKind: Action::OFK_Host)) {
1277 llvm::sys::path::replace_extension(path&: F, extension: "");
1278 F += Action::GetOffloadingFileNamePrefix(Kind: JA.getOffloadingDeviceKind(),
1279 NormalizedTriple: Triple.normalize());
1280 F += "-";
1281 F += JA.getOffloadingArch();
1282 }
1283 }
1284
1285 // If we're having more than one "-arch", we should name the files
1286 // differently so that every cc1 invocation writes to a different file.
1287 // We're doing that by appending "-<arch>" with "<arch>" being the arch
1288 // name from the triple.
1289 if (hasMultipleArchs) {
1290 // First, remember the extension.
1291 SmallString<64> OldExtension = llvm::sys::path::extension(path: F);
1292 // then, remove it.
1293 llvm::sys::path::replace_extension(path&: F, extension: "");
1294 // attach -<arch> to it.
1295 F += "-";
1296 F += Triple.getArchName();
1297 // put back the extension.
1298 llvm::sys::path::replace_extension(path&: F, extension: OldExtension);
1299 }
1300
1301 SmallString<32> Extension;
1302 Extension += "opt.";
1303 Extension += Format;
1304
1305 llvm::sys::path::replace_extension(path&: F, extension: Extension);
1306 CmdArgs.push_back(Elt: Args.MakeArgString(Str: F));
1307 }
1308
1309 if (const Arg *A =
1310 Args.getLastArg(Ids: options::OPT_foptimization_record_passes_EQ)) {
1311 CmdArgs.push_back(Elt: "-opt-record-passes");
1312 CmdArgs.push_back(Elt: A->getValue());
1313 }
1314
1315 if (!Format.empty()) {
1316 CmdArgs.push_back(Elt: "-opt-record-format");
1317 CmdArgs.push_back(Elt: Format.data());
1318 }
1319}
1320
1321void AddAAPCSVolatileBitfieldArgs(const ArgList &Args, ArgStringList &CmdArgs) {
1322 if (!Args.hasFlag(Pos: options::OPT_faapcs_bitfield_width,
1323 Neg: options::OPT_fno_aapcs_bitfield_width, Default: true))
1324 CmdArgs.push_back(Elt: "-fno-aapcs-bitfield-width");
1325
1326 if (Args.getLastArg(Ids: options::OPT_ForceAAPCSBitfieldLoad))
1327 CmdArgs.push_back(Elt: "-faapcs-bitfield-load");
1328}
1329
1330namespace {
1331void RenderARMABI(const Driver &D, const llvm::Triple &Triple,
1332 const ArgList &Args, ArgStringList &CmdArgs) {
1333 // Select the ABI to use.
1334 // FIXME: Support -meabi.
1335 // FIXME: Parts of this are duplicated in the backend, unify this somehow.
1336 const char *ABIName = nullptr;
1337 if (Arg *A = Args.getLastArg(Ids: options::OPT_mabi_EQ)) {
1338 ABIName = A->getValue();
1339 } else {
1340 std::string CPU = getCPUName(D, Args, T: Triple, /*FromAs*/ false);
1341 ABIName = llvm::ARM::computeDefaultTargetABI(TT: Triple, CPU).data();
1342 }
1343
1344 CmdArgs.push_back(Elt: "-target-abi");
1345 CmdArgs.push_back(Elt: ABIName);
1346}
1347
1348void AddUnalignedAccessWarning(ArgStringList &CmdArgs) {
1349 auto StrictAlignIter =
1350 llvm::find_if(Range: llvm::reverse(C&: CmdArgs), P: [](StringRef Arg) {
1351 return Arg == "+strict-align" || Arg == "-strict-align";
1352 });
1353 if (StrictAlignIter != CmdArgs.rend() &&
1354 StringRef(*StrictAlignIter) == "+strict-align")
1355 CmdArgs.push_back(Elt: "-Wunaligned-access");
1356}
1357}
1358
1359// Each combination of options here forms a signing schema, and in most cases
1360// each signing schema is its own incompatible ABI. The default values of the
1361// options represent the default signing schema.
1362static void handlePAuthABI(const ArgList &DriverArgs, ArgStringList &CC1Args) {
1363 if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_intrinsics,
1364 Ids: options::OPT_fno_ptrauth_intrinsics))
1365 CC1Args.push_back(Elt: "-fptrauth-intrinsics");
1366
1367 if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_calls,
1368 Ids: options::OPT_fno_ptrauth_calls))
1369 CC1Args.push_back(Elt: "-fptrauth-calls");
1370
1371 if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_returns,
1372 Ids: options::OPT_fno_ptrauth_returns))
1373 CC1Args.push_back(Elt: "-fptrauth-returns");
1374
1375 if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_auth_traps,
1376 Ids: options::OPT_fno_ptrauth_auth_traps))
1377 CC1Args.push_back(Elt: "-fptrauth-auth-traps");
1378
1379 if (!DriverArgs.hasArg(
1380 Ids: options::OPT_fptrauth_vtable_pointer_address_discrimination,
1381 Ids: options::OPT_fno_ptrauth_vtable_pointer_address_discrimination))
1382 CC1Args.push_back(Elt: "-fptrauth-vtable-pointer-address-discrimination");
1383
1384 if (!DriverArgs.hasArg(
1385 Ids: options::OPT_fptrauth_vtable_pointer_type_discrimination,
1386 Ids: options::OPT_fno_ptrauth_vtable_pointer_type_discrimination))
1387 CC1Args.push_back(Elt: "-fptrauth-vtable-pointer-type-discrimination");
1388
1389 if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_indirect_gotos,
1390 Ids: options::OPT_fno_ptrauth_indirect_gotos))
1391 CC1Args.push_back(Elt: "-fptrauth-indirect-gotos");
1392
1393 if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_init_fini,
1394 Ids: options::OPT_fno_ptrauth_init_fini))
1395 CC1Args.push_back(Elt: "-fptrauth-init-fini");
1396}
1397
1398static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
1399 ArgStringList &CmdArgs, bool isAArch64) {
1400 const llvm::Triple &Triple = TC.getEffectiveTriple();
1401 const Arg *A = isAArch64
1402 ? Args.getLastArg(Ids: options::OPT_msign_return_address_EQ,
1403 Ids: options::OPT_mbranch_protection_EQ)
1404 : Args.getLastArg(Ids: options::OPT_mbranch_protection_EQ);
1405 if (!A) {
1406 if (Triple.isOSOpenBSD() && isAArch64) {
1407 CmdArgs.push_back(Elt: "-msign-return-address=non-leaf");
1408 CmdArgs.push_back(Elt: "-msign-return-address-key=a_key");
1409 CmdArgs.push_back(Elt: "-mbranch-target-enforce");
1410 }
1411 return;
1412 }
1413
1414 const Driver &D = TC.getDriver();
1415 if (!(isAArch64 || (Triple.isArmT32() && Triple.isArmMClass())))
1416 D.Diag(DiagID: diag::warn_incompatible_branch_protection_option)
1417 << Triple.getArchName();
1418
1419 StringRef Scope, Key;
1420 bool IndirectBranches, BranchProtectionPAuthLR, GuardedControlStack;
1421
1422 if (A->getOption().matches(ID: options::OPT_msign_return_address_EQ)) {
1423 Scope = A->getValue();
1424 if (Scope != "none" && Scope != "non-leaf" && Scope != "all")
1425 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
1426 << A->getSpelling() << Scope;
1427 Key = "a_key";
1428 IndirectBranches = Triple.isOSOpenBSD() && isAArch64;
1429 BranchProtectionPAuthLR = false;
1430 GuardedControlStack = false;
1431 } else {
1432 StringRef DiagMsg;
1433 llvm::ARM::ParsedBranchProtection PBP;
1434 bool EnablePAuthLR = false;
1435
1436 // To know if we need to enable PAuth-LR As part of the standard branch
1437 // protection option, it needs to be determined if the feature has been
1438 // activated in the `march` argument. This information is stored within the
1439 // CmdArgs variable and can be found using a search.
1440 if (isAArch64) {
1441 auto isPAuthLR = [](const char *member) {
1442 llvm::AArch64::ExtensionInfo pauthlr_extension =
1443 llvm::AArch64::getExtensionByID(ExtID: llvm::AArch64::AEK_PAUTHLR);
1444 return pauthlr_extension.PosTargetFeature == member;
1445 };
1446
1447 if (llvm::any_of(Range&: CmdArgs, P: isPAuthLR))
1448 EnablePAuthLR = true;
1449 }
1450 if (!llvm::ARM::parseBranchProtection(Spec: A->getValue(), PBP, Err&: DiagMsg,
1451 EnablePAuthLR))
1452 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
1453 << A->getSpelling() << DiagMsg;
1454 if (!isAArch64 && PBP.Key == "b_key")
1455 D.Diag(DiagID: diag::warn_unsupported_branch_protection)
1456 << "b-key" << A->getAsString(Args);
1457 Scope = PBP.Scope;
1458 Key = PBP.Key;
1459 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
1460 IndirectBranches = PBP.BranchTargetEnforcement;
1461 GuardedControlStack = PBP.GuardedControlStack;
1462 }
1463
1464 bool HasPtrauthReturns = llvm::any_of(Range&: CmdArgs, P: [](const char *Arg) {
1465 return StringRef(Arg) == "-fptrauth-returns";
1466 });
1467 // GCS is currently untested with ptrauth-returns, but enabling this could be
1468 // allowed in future after testing with a suitable system.
1469 if (HasPtrauthReturns &&
1470 (Scope != "none" || BranchProtectionPAuthLR || GuardedControlStack)) {
1471 if (Triple.getEnvironment() == llvm::Triple::PAuthTest)
1472 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
1473 << A->getAsString(Args) << Triple.getTriple();
1474 else
1475 D.Diag(DiagID: diag::err_drv_incompatible_options)
1476 << A->getAsString(Args) << "-fptrauth-returns";
1477 }
1478
1479 CmdArgs.push_back(
1480 Elt: Args.MakeArgString(Str: Twine("-msign-return-address=") + Scope));
1481 if (Scope != "none")
1482 CmdArgs.push_back(
1483 Elt: Args.MakeArgString(Str: Twine("-msign-return-address-key=") + Key));
1484 if (BranchProtectionPAuthLR)
1485 CmdArgs.push_back(
1486 Elt: Args.MakeArgString(Str: Twine("-mbranch-protection-pauth-lr")));
1487 if (IndirectBranches)
1488 CmdArgs.push_back(Elt: "-mbranch-target-enforce");
1489
1490 if (GuardedControlStack)
1491 CmdArgs.push_back(Elt: "-mguarded-control-stack");
1492}
1493
1494void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args,
1495 ArgStringList &CmdArgs, bool KernelOrKext) const {
1496 RenderARMABI(D: getToolChain().getDriver(), Triple, Args, CmdArgs);
1497
1498 // Determine floating point ABI from the options & target defaults.
1499 arm::FloatABI ABI = arm::getARMFloatABI(TC: getToolChain(), Args);
1500 if (ABI == arm::FloatABI::Soft) {
1501 // Floating point operations and argument passing are soft.
1502 // FIXME: This changes CPP defines, we need -target-soft-float.
1503 CmdArgs.push_back(Elt: "-msoft-float");
1504 CmdArgs.push_back(Elt: "-mfloat-abi");
1505 CmdArgs.push_back(Elt: "soft");
1506 } else if (ABI == arm::FloatABI::SoftFP) {
1507 // Floating point operations are hard, but argument passing is soft.
1508 CmdArgs.push_back(Elt: "-mfloat-abi");
1509 CmdArgs.push_back(Elt: "soft");
1510 } else {
1511 // Floating point operations and argument passing are hard.
1512 assert(ABI == arm::FloatABI::Hard && "Invalid float abi!");
1513 CmdArgs.push_back(Elt: "-mfloat-abi");
1514 CmdArgs.push_back(Elt: "hard");
1515 }
1516
1517 // Forward the -mglobal-merge option for explicit control over the pass.
1518 if (Arg *A = Args.getLastArg(Ids: options::OPT_mglobal_merge,
1519 Ids: options::OPT_mno_global_merge)) {
1520 CmdArgs.push_back(Elt: "-mllvm");
1521 if (A->getOption().matches(ID: options::OPT_mno_global_merge))
1522 CmdArgs.push_back(Elt: "-arm-global-merge=false");
1523 else
1524 CmdArgs.push_back(Elt: "-arm-global-merge=true");
1525 }
1526
1527 if (!Args.hasFlag(Pos: options::OPT_mimplicit_float,
1528 Neg: options::OPT_mno_implicit_float, Default: true))
1529 CmdArgs.push_back(Elt: "-no-implicit-float");
1530
1531 if (Args.getLastArg(Ids: options::OPT_mcmse))
1532 CmdArgs.push_back(Elt: "-mcmse");
1533
1534 AddAAPCSVolatileBitfieldArgs(Args, CmdArgs);
1535
1536 // Enable/disable return address signing and indirect branch targets.
1537 CollectARMPACBTIOptions(TC: getToolChain(), Args, CmdArgs, isAArch64: false /*isAArch64*/);
1538
1539 AddUnalignedAccessWarning(CmdArgs);
1540}
1541
1542void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple,
1543 const ArgList &Args, bool KernelOrKext,
1544 ArgStringList &CmdArgs) const {
1545 const ToolChain &TC = getToolChain();
1546
1547 // Add the target features
1548 getTargetFeatures(D: TC.getDriver(), Triple: EffectiveTriple, Args, CmdArgs, ForAS: false);
1549
1550 // Add target specific flags.
1551 switch (TC.getArch()) {
1552 default:
1553 break;
1554
1555 case llvm::Triple::arm:
1556 case llvm::Triple::armeb:
1557 case llvm::Triple::thumb:
1558 case llvm::Triple::thumbeb:
1559 // Use the effective triple, which takes into account the deployment target.
1560 AddARMTargetArgs(Triple: EffectiveTriple, Args, CmdArgs, KernelOrKext);
1561 break;
1562
1563 case llvm::Triple::aarch64:
1564 case llvm::Triple::aarch64_32:
1565 case llvm::Triple::aarch64_be:
1566 AddAArch64TargetArgs(Args, CmdArgs);
1567 break;
1568
1569 case llvm::Triple::loongarch32:
1570 case llvm::Triple::loongarch64:
1571 AddLoongArchTargetArgs(Args, CmdArgs);
1572 break;
1573
1574 case llvm::Triple::mips:
1575 case llvm::Triple::mipsel:
1576 case llvm::Triple::mips64:
1577 case llvm::Triple::mips64el:
1578 AddMIPSTargetArgs(Args, CmdArgs);
1579 break;
1580
1581 case llvm::Triple::ppc:
1582 case llvm::Triple::ppcle:
1583 case llvm::Triple::ppc64:
1584 case llvm::Triple::ppc64le:
1585 AddPPCTargetArgs(Args, CmdArgs);
1586 break;
1587
1588 case llvm::Triple::riscv32:
1589 case llvm::Triple::riscv64:
1590 AddRISCVTargetArgs(Args, CmdArgs);
1591 break;
1592
1593 case llvm::Triple::sparc:
1594 case llvm::Triple::sparcel:
1595 case llvm::Triple::sparcv9:
1596 AddSparcTargetArgs(Args, CmdArgs);
1597 break;
1598
1599 case llvm::Triple::systemz:
1600 AddSystemZTargetArgs(Args, CmdArgs);
1601 break;
1602
1603 case llvm::Triple::x86:
1604 case llvm::Triple::x86_64:
1605 AddX86TargetArgs(Args, CmdArgs);
1606 break;
1607
1608 case llvm::Triple::lanai:
1609 AddLanaiTargetArgs(Args, CmdArgs);
1610 break;
1611
1612 case llvm::Triple::hexagon:
1613 AddHexagonTargetArgs(Args, CmdArgs);
1614 break;
1615
1616 case llvm::Triple::wasm32:
1617 case llvm::Triple::wasm64:
1618 AddWebAssemblyTargetArgs(Args, CmdArgs);
1619 break;
1620
1621 case llvm::Triple::ve:
1622 AddVETargetArgs(Args, CmdArgs);
1623 break;
1624 }
1625}
1626
1627namespace {
1628void RenderAArch64ABI(const llvm::Triple &Triple, const ArgList &Args,
1629 ArgStringList &CmdArgs) {
1630 const char *ABIName = nullptr;
1631 if (Arg *A = Args.getLastArg(Ids: options::OPT_mabi_EQ))
1632 ABIName = A->getValue();
1633 else if (Triple.isOSDarwin())
1634 ABIName = "darwinpcs";
1635 else if (Triple.getEnvironment() == llvm::Triple::PAuthTest)
1636 ABIName = "pauthtest";
1637 else
1638 ABIName = "aapcs";
1639
1640 CmdArgs.push_back(Elt: "-target-abi");
1641 CmdArgs.push_back(Elt: ABIName);
1642}
1643}
1644
1645void Clang::AddAArch64TargetArgs(const ArgList &Args,
1646 ArgStringList &CmdArgs) const {
1647 const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
1648
1649 if (!Args.hasFlag(Pos: options::OPT_mred_zone, Neg: options::OPT_mno_red_zone, Default: true) ||
1650 Args.hasArg(Ids: options::OPT_mkernel) ||
1651 Args.hasArg(Ids: options::OPT_fapple_kext))
1652 CmdArgs.push_back(Elt: "-disable-red-zone");
1653
1654 if (!Args.hasFlag(Pos: options::OPT_mimplicit_float,
1655 Neg: options::OPT_mno_implicit_float, Default: true))
1656 CmdArgs.push_back(Elt: "-no-implicit-float");
1657
1658 RenderAArch64ABI(Triple, Args, CmdArgs);
1659
1660 // Forward the -mglobal-merge option for explicit control over the pass.
1661 if (Arg *A = Args.getLastArg(Ids: options::OPT_mglobal_merge,
1662 Ids: options::OPT_mno_global_merge)) {
1663 CmdArgs.push_back(Elt: "-mllvm");
1664 if (A->getOption().matches(ID: options::OPT_mno_global_merge))
1665 CmdArgs.push_back(Elt: "-aarch64-enable-global-merge=false");
1666 else
1667 CmdArgs.push_back(Elt: "-aarch64-enable-global-merge=true");
1668 }
1669
1670 // Handle -msve_vector_bits=<bits>
1671 auto HandleVectorBits = [&](Arg *A, StringRef VScaleMin,
1672 StringRef VScaleMax) {
1673 StringRef Val = A->getValue();
1674 const Driver &D = getToolChain().getDriver();
1675 if (Val == "128" || Val == "256" || Val == "512" || Val == "1024" ||
1676 Val == "2048" || Val == "128+" || Val == "256+" || Val == "512+" ||
1677 Val == "1024+" || Val == "2048+") {
1678 unsigned Bits = 0;
1679 if (!Val.consume_back(Suffix: "+")) {
1680 bool Invalid = Val.getAsInteger(Radix: 10, Result&: Bits);
1681 (void)Invalid;
1682 assert(!Invalid && "Failed to parse value");
1683 CmdArgs.push_back(
1684 Elt: Args.MakeArgString(Str: VScaleMax + llvm::Twine(Bits / 128)));
1685 }
1686
1687 bool Invalid = Val.getAsInteger(Radix: 10, Result&: Bits);
1688 (void)Invalid;
1689 assert(!Invalid && "Failed to parse value");
1690
1691 CmdArgs.push_back(
1692 Elt: Args.MakeArgString(Str: VScaleMin + llvm::Twine(Bits / 128)));
1693 } else if (Val == "scalable") {
1694 // Silently drop requests for vector-length agnostic code as it's implied.
1695 } else {
1696 // Handle the unsupported values passed to msve-vector-bits.
1697 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
1698 << A->getSpelling() << Val;
1699 }
1700 };
1701 if (Arg *A = Args.getLastArg(Ids: options::OPT_msve_vector_bits_EQ))
1702 HandleVectorBits(A, "-mvscale-min=", "-mvscale-max=");
1703 if (Arg *A = Args.getLastArg(Ids: options::OPT_msve_streaming_vector_bits_EQ))
1704 HandleVectorBits(A, "-mvscale-streaming-min=", "-mvscale-streaming-max=");
1705
1706 AddAAPCSVolatileBitfieldArgs(Args, CmdArgs);
1707
1708 if (const Arg *A = Args.getLastArg(Ids: clang::driver::options::OPT_mtune_EQ)) {
1709 CmdArgs.push_back(Elt: "-tune-cpu");
1710 if (strcmp(s1: A->getValue(), s2: "native") == 0)
1711 CmdArgs.push_back(Elt: Args.MakeArgString(Str: llvm::sys::getHostCPUName()));
1712 else
1713 CmdArgs.push_back(Elt: A->getValue());
1714 }
1715
1716 AddUnalignedAccessWarning(CmdArgs);
1717
1718 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fptrauth_intrinsics,
1719 Neg: options::OPT_fno_ptrauth_intrinsics);
1720 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fptrauth_calls,
1721 Neg: options::OPT_fno_ptrauth_calls);
1722 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fptrauth_returns,
1723 Neg: options::OPT_fno_ptrauth_returns);
1724 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fptrauth_auth_traps,
1725 Neg: options::OPT_fno_ptrauth_auth_traps);
1726 Args.addOptInFlag(
1727 Output&: CmdArgs, Pos: options::OPT_fptrauth_vtable_pointer_address_discrimination,
1728 Neg: options::OPT_fno_ptrauth_vtable_pointer_address_discrimination);
1729 Args.addOptInFlag(
1730 Output&: CmdArgs, Pos: options::OPT_fptrauth_vtable_pointer_type_discrimination,
1731 Neg: options::OPT_fno_ptrauth_vtable_pointer_type_discrimination);
1732 Args.addOptInFlag(
1733 Output&: CmdArgs, Pos: options::OPT_fptrauth_type_info_vtable_pointer_discrimination,
1734 Neg: options::OPT_fno_ptrauth_type_info_vtable_pointer_discrimination);
1735 Args.addOptInFlag(
1736 Output&: CmdArgs, Pos: options::OPT_fptrauth_function_pointer_type_discrimination,
1737 Neg: options::OPT_fno_ptrauth_function_pointer_type_discrimination);
1738
1739 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fptrauth_indirect_gotos,
1740 Neg: options::OPT_fno_ptrauth_indirect_gotos);
1741 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fptrauth_init_fini,
1742 Neg: options::OPT_fno_ptrauth_init_fini);
1743 Args.addOptInFlag(Output&: CmdArgs,
1744 Pos: options::OPT_fptrauth_init_fini_address_discrimination,
1745 Neg: options::OPT_fno_ptrauth_init_fini_address_discrimination);
1746 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_faarch64_jump_table_hardening,
1747 Neg: options::OPT_fno_aarch64_jump_table_hardening);
1748
1749 if (Triple.getEnvironment() == llvm::Triple::PAuthTest)
1750 handlePAuthABI(DriverArgs: Args, CC1Args&: CmdArgs);
1751
1752 // Enable/disable return address signing and indirect branch targets.
1753 CollectARMPACBTIOptions(TC: getToolChain(), Args, CmdArgs, isAArch64: true /*isAArch64*/);
1754}
1755
1756void Clang::AddLoongArchTargetArgs(const ArgList &Args,
1757 ArgStringList &CmdArgs) const {
1758 const llvm::Triple &Triple = getToolChain().getTriple();
1759
1760 CmdArgs.push_back(Elt: "-target-abi");
1761 CmdArgs.push_back(
1762 Elt: loongarch::getLoongArchABI(D: getToolChain().getDriver(), Args, Triple)
1763 .data());
1764
1765 // Handle -mtune.
1766 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mtune_EQ)) {
1767 std::string TuneCPU = A->getValue();
1768 TuneCPU = loongarch::postProcessTargetCPUString(CPU: TuneCPU, Triple);
1769 CmdArgs.push_back(Elt: "-tune-cpu");
1770 CmdArgs.push_back(Elt: Args.MakeArgString(Str: TuneCPU));
1771 }
1772
1773 if (Arg *A = Args.getLastArg(Ids: options::OPT_mannotate_tablejump,
1774 Ids: options::OPT_mno_annotate_tablejump)) {
1775 if (A->getOption().matches(ID: options::OPT_mannotate_tablejump)) {
1776 CmdArgs.push_back(Elt: "-mllvm");
1777 CmdArgs.push_back(Elt: "-loongarch-annotate-tablejump");
1778 }
1779 }
1780}
1781
1782void Clang::AddMIPSTargetArgs(const ArgList &Args,
1783 ArgStringList &CmdArgs) const {
1784 const Driver &D = getToolChain().getDriver();
1785 StringRef CPUName;
1786 StringRef ABIName;
1787 const llvm::Triple &Triple = getToolChain().getTriple();
1788 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
1789
1790 CmdArgs.push_back(Elt: "-target-abi");
1791 CmdArgs.push_back(Elt: ABIName.data());
1792
1793 mips::FloatABI ABI = mips::getMipsFloatABI(D, Args, Triple);
1794 if (ABI == mips::FloatABI::Soft) {
1795 // Floating point operations and argument passing are soft.
1796 CmdArgs.push_back(Elt: "-msoft-float");
1797 CmdArgs.push_back(Elt: "-mfloat-abi");
1798 CmdArgs.push_back(Elt: "soft");
1799 } else {
1800 // Floating point operations and argument passing are hard.
1801 assert(ABI == mips::FloatABI::Hard && "Invalid float abi!");
1802 CmdArgs.push_back(Elt: "-mfloat-abi");
1803 CmdArgs.push_back(Elt: "hard");
1804 }
1805
1806 if (Arg *A = Args.getLastArg(Ids: options::OPT_mldc1_sdc1,
1807 Ids: options::OPT_mno_ldc1_sdc1)) {
1808 if (A->getOption().matches(ID: options::OPT_mno_ldc1_sdc1)) {
1809 CmdArgs.push_back(Elt: "-mllvm");
1810 CmdArgs.push_back(Elt: "-mno-ldc1-sdc1");
1811 }
1812 }
1813
1814 if (Arg *A = Args.getLastArg(Ids: options::OPT_mcheck_zero_division,
1815 Ids: options::OPT_mno_check_zero_division)) {
1816 if (A->getOption().matches(ID: options::OPT_mno_check_zero_division)) {
1817 CmdArgs.push_back(Elt: "-mllvm");
1818 CmdArgs.push_back(Elt: "-mno-check-zero-division");
1819 }
1820 }
1821
1822 if (Args.getLastArg(Ids: options::OPT_mfix4300)) {
1823 CmdArgs.push_back(Elt: "-mllvm");
1824 CmdArgs.push_back(Elt: "-mfix4300");
1825 }
1826
1827 if (Arg *A = Args.getLastArg(Ids: options::OPT_G)) {
1828 StringRef v = A->getValue();
1829 CmdArgs.push_back(Elt: "-mllvm");
1830 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mips-ssection-threshold=" + v));
1831 A->claim();
1832 }
1833
1834 Arg *GPOpt = Args.getLastArg(Ids: options::OPT_mgpopt, Ids: options::OPT_mno_gpopt);
1835 Arg *ABICalls =
1836 Args.getLastArg(Ids: options::OPT_mabicalls, Ids: options::OPT_mno_abicalls);
1837
1838 // -mabicalls is the default for many MIPS environments, even with -fno-pic.
1839 // -mgpopt is the default for static, -fno-pic environments but these two
1840 // options conflict. We want to be certain that -mno-abicalls -mgpopt is
1841 // the only case where -mllvm -mgpopt is passed.
1842 // NOTE: We need a warning here or in the backend to warn when -mgpopt is
1843 // passed explicitly when compiling something with -mabicalls
1844 // (implictly) in affect. Currently the warning is in the backend.
1845 //
1846 // When the ABI in use is N64, we also need to determine the PIC mode that
1847 // is in use, as -fno-pic for N64 implies -mno-abicalls.
1848 bool NoABICalls =
1849 ABICalls && ABICalls->getOption().matches(ID: options::OPT_mno_abicalls);
1850
1851 llvm::Reloc::Model RelocationModel;
1852 unsigned PICLevel;
1853 bool IsPIE;
1854 std::tie(args&: RelocationModel, args&: PICLevel, args&: IsPIE) =
1855 ParsePICArgs(ToolChain: getToolChain(), Args);
1856
1857 NoABICalls = NoABICalls ||
1858 (RelocationModel == llvm::Reloc::Static && ABIName == "n64");
1859
1860 bool WantGPOpt = GPOpt && GPOpt->getOption().matches(ID: options::OPT_mgpopt);
1861 // We quietly ignore -mno-gpopt as the backend defaults to -mno-gpopt.
1862 if (NoABICalls && (!GPOpt || WantGPOpt)) {
1863 CmdArgs.push_back(Elt: "-mllvm");
1864 CmdArgs.push_back(Elt: "-mgpopt");
1865
1866 Arg *LocalSData = Args.getLastArg(Ids: options::OPT_mlocal_sdata,
1867 Ids: options::OPT_mno_local_sdata);
1868 Arg *ExternSData = Args.getLastArg(Ids: options::OPT_mextern_sdata,
1869 Ids: options::OPT_mno_extern_sdata);
1870 Arg *EmbeddedData = Args.getLastArg(Ids: options::OPT_membedded_data,
1871 Ids: options::OPT_mno_embedded_data);
1872 if (LocalSData) {
1873 CmdArgs.push_back(Elt: "-mllvm");
1874 if (LocalSData->getOption().matches(ID: options::OPT_mlocal_sdata)) {
1875 CmdArgs.push_back(Elt: "-mlocal-sdata=1");
1876 } else {
1877 CmdArgs.push_back(Elt: "-mlocal-sdata=0");
1878 }
1879 LocalSData->claim();
1880 }
1881
1882 if (ExternSData) {
1883 CmdArgs.push_back(Elt: "-mllvm");
1884 if (ExternSData->getOption().matches(ID: options::OPT_mextern_sdata)) {
1885 CmdArgs.push_back(Elt: "-mextern-sdata=1");
1886 } else {
1887 CmdArgs.push_back(Elt: "-mextern-sdata=0");
1888 }
1889 ExternSData->claim();
1890 }
1891
1892 if (EmbeddedData) {
1893 CmdArgs.push_back(Elt: "-mllvm");
1894 if (EmbeddedData->getOption().matches(ID: options::OPT_membedded_data)) {
1895 CmdArgs.push_back(Elt: "-membedded-data=1");
1896 } else {
1897 CmdArgs.push_back(Elt: "-membedded-data=0");
1898 }
1899 EmbeddedData->claim();
1900 }
1901
1902 } else if ((!ABICalls || (!NoABICalls && ABICalls)) && WantGPOpt)
1903 D.Diag(DiagID: diag::warn_drv_unsupported_gpopt) << (ABICalls ? 0 : 1);
1904
1905 if (GPOpt)
1906 GPOpt->claim();
1907
1908 if (Arg *A = Args.getLastArg(Ids: options::OPT_mcompact_branches_EQ)) {
1909 StringRef Val = StringRef(A->getValue());
1910 if (mips::hasCompactBranches(CPU&: CPUName)) {
1911 if (Val == "never" || Val == "always" || Val == "optimal") {
1912 CmdArgs.push_back(Elt: "-mllvm");
1913 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mips-compact-branches=" + Val));
1914 } else
1915 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
1916 << A->getSpelling() << Val;
1917 } else
1918 D.Diag(DiagID: diag::warn_target_unsupported_compact_branches) << CPUName;
1919 }
1920
1921 if (Arg *A = Args.getLastArg(Ids: options::OPT_mrelax_pic_calls,
1922 Ids: options::OPT_mno_relax_pic_calls)) {
1923 if (A->getOption().matches(ID: options::OPT_mno_relax_pic_calls)) {
1924 CmdArgs.push_back(Elt: "-mllvm");
1925 CmdArgs.push_back(Elt: "-mips-jalr-reloc=0");
1926 }
1927 }
1928}
1929
1930void Clang::AddPPCTargetArgs(const ArgList &Args,
1931 ArgStringList &CmdArgs) const {
1932 const Driver &D = getToolChain().getDriver();
1933 const llvm::Triple &T = getToolChain().getTriple();
1934 if (Arg *A = Args.getLastArg(Ids: options::OPT_mtune_EQ)) {
1935 CmdArgs.push_back(Elt: "-tune-cpu");
1936 StringRef CPU = llvm::PPC::getNormalizedPPCTuneCPU(T, CPUName: A->getValue());
1937 CmdArgs.push_back(Elt: Args.MakeArgString(Str: CPU.str()));
1938 }
1939
1940 // Select the ABI to use.
1941 const char *ABIName = nullptr;
1942 if (T.isOSBinFormatELF()) {
1943 switch (getToolChain().getArch()) {
1944 case llvm::Triple::ppc64: {
1945 if (T.isPPC64ELFv2ABI())
1946 ABIName = "elfv2";
1947 else
1948 ABIName = "elfv1";
1949 break;
1950 }
1951 case llvm::Triple::ppc64le:
1952 ABIName = "elfv2";
1953 break;
1954 default:
1955 break;
1956 }
1957 }
1958
1959 bool IEEELongDouble = getToolChain().defaultToIEEELongDouble();
1960 bool VecExtabi = false;
1961 for (const Arg *A : Args.filtered(Ids: options::OPT_mabi_EQ)) {
1962 StringRef V = A->getValue();
1963 if (V == "ieeelongdouble") {
1964 IEEELongDouble = true;
1965 A->claim();
1966 } else if (V == "ibmlongdouble") {
1967 IEEELongDouble = false;
1968 A->claim();
1969 } else if (V == "vec-default") {
1970 VecExtabi = false;
1971 A->claim();
1972 } else if (V == "vec-extabi") {
1973 VecExtabi = true;
1974 A->claim();
1975 } else if (V == "elfv1") {
1976 ABIName = "elfv1";
1977 A->claim();
1978 } else if (V == "elfv2") {
1979 ABIName = "elfv2";
1980 A->claim();
1981 } else if (V != "altivec")
1982 // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore
1983 // the option if given as we don't have backend support for any targets
1984 // that don't use the altivec abi.
1985 ABIName = A->getValue();
1986 }
1987 if (IEEELongDouble)
1988 CmdArgs.push_back(Elt: "-mabi=ieeelongdouble");
1989 if (VecExtabi) {
1990 if (!T.isOSAIX())
1991 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
1992 << "-mabi=vec-extabi" << T.str();
1993 CmdArgs.push_back(Elt: "-mabi=vec-extabi");
1994 }
1995
1996 if (!Args.hasFlag(Pos: options::OPT_mred_zone, Neg: options::OPT_mno_red_zone, Default: true))
1997 CmdArgs.push_back(Elt: "-disable-red-zone");
1998
1999 ppc::FloatABI FloatABI = ppc::getPPCFloatABI(D, Args);
2000 if (FloatABI == ppc::FloatABI::Soft) {
2001 // Floating point operations and argument passing are soft.
2002 CmdArgs.push_back(Elt: "-msoft-float");
2003 CmdArgs.push_back(Elt: "-mfloat-abi");
2004 CmdArgs.push_back(Elt: "soft");
2005 } else {
2006 // Floating point operations and argument passing are hard.
2007 assert(FloatABI == ppc::FloatABI::Hard && "Invalid float abi!");
2008 CmdArgs.push_back(Elt: "-mfloat-abi");
2009 CmdArgs.push_back(Elt: "hard");
2010 }
2011
2012 if (ABIName) {
2013 CmdArgs.push_back(Elt: "-target-abi");
2014 CmdArgs.push_back(Elt: ABIName);
2015 }
2016}
2017
2018void Clang::AddRISCVTargetArgs(const ArgList &Args,
2019 ArgStringList &CmdArgs) const {
2020 const llvm::Triple &Triple = getToolChain().getTriple();
2021 StringRef ABIName = riscv::getRISCVABI(Args, Triple);
2022
2023 CmdArgs.push_back(Elt: "-target-abi");
2024 CmdArgs.push_back(Elt: ABIName.data());
2025
2026 if (Arg *A = Args.getLastArg(Ids: options::OPT_G)) {
2027 CmdArgs.push_back(Elt: "-msmall-data-limit");
2028 CmdArgs.push_back(Elt: A->getValue());
2029 }
2030
2031 if (!Args.hasFlag(Pos: options::OPT_mimplicit_float,
2032 Neg: options::OPT_mno_implicit_float, Default: true))
2033 CmdArgs.push_back(Elt: "-no-implicit-float");
2034
2035 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mtune_EQ)) {
2036 CmdArgs.push_back(Elt: "-tune-cpu");
2037 if (strcmp(s1: A->getValue(), s2: "native") == 0)
2038 CmdArgs.push_back(Elt: Args.MakeArgString(Str: llvm::sys::getHostCPUName()));
2039 else
2040 CmdArgs.push_back(Elt: A->getValue());
2041 }
2042
2043 // Handle -mrvv-vector-bits=<bits>
2044 if (Arg *A = Args.getLastArg(Ids: options::OPT_mrvv_vector_bits_EQ)) {
2045 StringRef Val = A->getValue();
2046 const Driver &D = getToolChain().getDriver();
2047
2048 // Get minimum VLen from march.
2049 unsigned MinVLen = 0;
2050 std::string Arch = riscv::getRISCVArch(Args, Triple);
2051 auto ISAInfo = llvm::RISCVISAInfo::parseArchString(
2052 Arch, /*EnableExperimentalExtensions*/ EnableExperimentalExtension: true);
2053 // Ignore parsing error.
2054 if (!errorToBool(Err: ISAInfo.takeError()))
2055 MinVLen = (*ISAInfo)->getMinVLen();
2056
2057 // If the value is "zvl", use MinVLen from march. Otherwise, try to parse
2058 // as integer as long as we have a MinVLen.
2059 unsigned Bits = 0;
2060 if (Val == "zvl" && MinVLen >= llvm::RISCV::RVVBitsPerBlock) {
2061 Bits = MinVLen;
2062 } else if (!Val.getAsInteger(Radix: 10, Result&: Bits)) {
2063 // Only accept power of 2 values beteen RVVBitsPerBlock and 65536 that
2064 // at least MinVLen.
2065 if (Bits < MinVLen || Bits < llvm::RISCV::RVVBitsPerBlock ||
2066 Bits > 65536 || !llvm::isPowerOf2_32(Value: Bits))
2067 Bits = 0;
2068 }
2069
2070 // If we got a valid value try to use it.
2071 if (Bits != 0) {
2072 unsigned VScaleMin = Bits / llvm::RISCV::RVVBitsPerBlock;
2073 CmdArgs.push_back(
2074 Elt: Args.MakeArgString(Str: "-mvscale-max=" + llvm::Twine(VScaleMin)));
2075 CmdArgs.push_back(
2076 Elt: Args.MakeArgString(Str: "-mvscale-min=" + llvm::Twine(VScaleMin)));
2077 } else if (Val != "scalable") {
2078 // Handle the unsupported values passed to mrvv-vector-bits.
2079 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
2080 << A->getSpelling() << Val;
2081 }
2082 }
2083}
2084
2085void Clang::AddSparcTargetArgs(const ArgList &Args,
2086 ArgStringList &CmdArgs) const {
2087 sparc::FloatABI FloatABI =
2088 sparc::getSparcFloatABI(D: getToolChain().getDriver(), Args);
2089
2090 if (FloatABI == sparc::FloatABI::Soft) {
2091 // Floating point operations and argument passing are soft.
2092 CmdArgs.push_back(Elt: "-msoft-float");
2093 CmdArgs.push_back(Elt: "-mfloat-abi");
2094 CmdArgs.push_back(Elt: "soft");
2095 } else {
2096 // Floating point operations and argument passing are hard.
2097 assert(FloatABI == sparc::FloatABI::Hard && "Invalid float abi!");
2098 CmdArgs.push_back(Elt: "-mfloat-abi");
2099 CmdArgs.push_back(Elt: "hard");
2100 }
2101
2102 if (const Arg *A = Args.getLastArg(Ids: clang::driver::options::OPT_mtune_EQ)) {
2103 StringRef Name = A->getValue();
2104 std::string TuneCPU;
2105 if (Name == "native")
2106 TuneCPU = std::string(llvm::sys::getHostCPUName());
2107 else
2108 TuneCPU = std::string(Name);
2109
2110 CmdArgs.push_back(Elt: "-tune-cpu");
2111 CmdArgs.push_back(Elt: Args.MakeArgString(Str: TuneCPU));
2112 }
2113}
2114
2115void Clang::AddSystemZTargetArgs(const ArgList &Args,
2116 ArgStringList &CmdArgs) const {
2117 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mtune_EQ)) {
2118 CmdArgs.push_back(Elt: "-tune-cpu");
2119 if (strcmp(s1: A->getValue(), s2: "native") == 0)
2120 CmdArgs.push_back(Elt: Args.MakeArgString(Str: llvm::sys::getHostCPUName()));
2121 else
2122 CmdArgs.push_back(Elt: A->getValue());
2123 }
2124
2125 bool HasBackchain =
2126 Args.hasFlag(Pos: options::OPT_mbackchain, Neg: options::OPT_mno_backchain, Default: false);
2127 bool HasPackedStack = Args.hasFlag(Pos: options::OPT_mpacked_stack,
2128 Neg: options::OPT_mno_packed_stack, Default: false);
2129 systemz::FloatABI FloatABI =
2130 systemz::getSystemZFloatABI(D: getToolChain().getDriver(), Args);
2131 bool HasSoftFloat = (FloatABI == systemz::FloatABI::Soft);
2132 if (HasBackchain && HasPackedStack && !HasSoftFloat) {
2133 const Driver &D = getToolChain().getDriver();
2134 D.Diag(DiagID: diag::err_drv_unsupported_opt)
2135 << "-mpacked-stack -mbackchain -mhard-float";
2136 }
2137 if (HasBackchain)
2138 CmdArgs.push_back(Elt: "-mbackchain");
2139 if (HasPackedStack)
2140 CmdArgs.push_back(Elt: "-mpacked-stack");
2141 if (HasSoftFloat) {
2142 // Floating point operations and argument passing are soft.
2143 CmdArgs.push_back(Elt: "-msoft-float");
2144 CmdArgs.push_back(Elt: "-mfloat-abi");
2145 CmdArgs.push_back(Elt: "soft");
2146 }
2147}
2148
2149void Clang::AddX86TargetArgs(const ArgList &Args,
2150 ArgStringList &CmdArgs) const {
2151 const Driver &D = getToolChain().getDriver();
2152 addX86AlignBranchArgs(D, Args, CmdArgs, /*IsLTO=*/false);
2153
2154 if (!Args.hasFlag(Pos: options::OPT_mred_zone, Neg: options::OPT_mno_red_zone, Default: true) ||
2155 Args.hasArg(Ids: options::OPT_mkernel) ||
2156 Args.hasArg(Ids: options::OPT_fapple_kext))
2157 CmdArgs.push_back(Elt: "-disable-red-zone");
2158
2159 if (!Args.hasFlag(Pos: options::OPT_mtls_direct_seg_refs,
2160 Neg: options::OPT_mno_tls_direct_seg_refs, Default: true))
2161 CmdArgs.push_back(Elt: "-mno-tls-direct-seg-refs");
2162
2163 // Default to avoid implicit floating-point for kernel/kext code, but allow
2164 // that to be overridden with -mno-soft-float.
2165 bool NoImplicitFloat = (Args.hasArg(Ids: options::OPT_mkernel) ||
2166 Args.hasArg(Ids: options::OPT_fapple_kext));
2167 if (Arg *A = Args.getLastArg(
2168 Ids: options::OPT_msoft_float, Ids: options::OPT_mno_soft_float,
2169 Ids: options::OPT_mimplicit_float, Ids: options::OPT_mno_implicit_float)) {
2170 const Option &O = A->getOption();
2171 NoImplicitFloat = (O.matches(ID: options::OPT_mno_implicit_float) ||
2172 O.matches(ID: options::OPT_msoft_float));
2173 }
2174 if (NoImplicitFloat)
2175 CmdArgs.push_back(Elt: "-no-implicit-float");
2176
2177 if (Arg *A = Args.getLastArg(Ids: options::OPT_masm_EQ)) {
2178 StringRef Value = A->getValue();
2179 if (Value == "intel" || Value == "att") {
2180 CmdArgs.push_back(Elt: "-mllvm");
2181 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-x86-asm-syntax=" + Value));
2182 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-inline-asm=" + Value));
2183 } else {
2184 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
2185 << A->getSpelling() << Value;
2186 }
2187 } else if (D.IsCLMode()) {
2188 CmdArgs.push_back(Elt: "-mllvm");
2189 CmdArgs.push_back(Elt: "-x86-asm-syntax=intel");
2190 }
2191
2192 if (Arg *A = Args.getLastArg(Ids: options::OPT_mskip_rax_setup,
2193 Ids: options::OPT_mno_skip_rax_setup))
2194 if (A->getOption().matches(ID: options::OPT_mskip_rax_setup))
2195 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mskip-rax-setup"));
2196
2197 // Set flags to support MCU ABI.
2198 if (Args.hasFlag(Pos: options::OPT_miamcu, Neg: options::OPT_mno_iamcu, Default: false)) {
2199 CmdArgs.push_back(Elt: "-mfloat-abi");
2200 CmdArgs.push_back(Elt: "soft");
2201 CmdArgs.push_back(Elt: "-mstack-alignment=4");
2202 }
2203
2204 // Handle -mtune.
2205
2206 // Default to "generic" unless -march is present or targetting the PS4/PS5.
2207 std::string TuneCPU;
2208 if (!Args.hasArg(Ids: clang::driver::options::OPT_march_EQ) &&
2209 !getToolChain().getTriple().isPS())
2210 TuneCPU = "generic";
2211
2212 // Override based on -mtune.
2213 if (const Arg *A = Args.getLastArg(Ids: clang::driver::options::OPT_mtune_EQ)) {
2214 StringRef Name = A->getValue();
2215
2216 if (Name == "native") {
2217 Name = llvm::sys::getHostCPUName();
2218 if (!Name.empty())
2219 TuneCPU = std::string(Name);
2220 } else
2221 TuneCPU = std::string(Name);
2222 }
2223
2224 if (!TuneCPU.empty()) {
2225 CmdArgs.push_back(Elt: "-tune-cpu");
2226 CmdArgs.push_back(Elt: Args.MakeArgString(Str: TuneCPU));
2227 }
2228}
2229
2230void Clang::AddHexagonTargetArgs(const ArgList &Args,
2231 ArgStringList &CmdArgs) const {
2232 CmdArgs.push_back(Elt: "-mqdsp6-compat");
2233 CmdArgs.push_back(Elt: "-Wreturn-type");
2234
2235 if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) {
2236 CmdArgs.push_back(Elt: "-mllvm");
2237 CmdArgs.push_back(
2238 Elt: Args.MakeArgString(Str: "-hexagon-small-data-threshold=" + Twine(*G)));
2239 }
2240
2241 if (!Args.hasArg(Ids: options::OPT_fno_short_enums))
2242 CmdArgs.push_back(Elt: "-fshort-enums");
2243 if (Args.getLastArg(Ids: options::OPT_mieee_rnd_near)) {
2244 CmdArgs.push_back(Elt: "-mllvm");
2245 CmdArgs.push_back(Elt: "-enable-hexagon-ieee-rnd-near");
2246 }
2247 CmdArgs.push_back(Elt: "-mllvm");
2248 CmdArgs.push_back(Elt: "-machine-sink-split=0");
2249}
2250
2251void Clang::AddLanaiTargetArgs(const ArgList &Args,
2252 ArgStringList &CmdArgs) const {
2253 if (Arg *A = Args.getLastArg(Ids: options::OPT_mcpu_EQ)) {
2254 StringRef CPUName = A->getValue();
2255
2256 CmdArgs.push_back(Elt: "-target-cpu");
2257 CmdArgs.push_back(Elt: Args.MakeArgString(Str: CPUName));
2258 }
2259 if (Arg *A = Args.getLastArg(Ids: options::OPT_mregparm_EQ)) {
2260 StringRef Value = A->getValue();
2261 // Only support mregparm=4 to support old usage. Report error for all other
2262 // cases.
2263 int Mregparm;
2264 if (Value.getAsInteger(Radix: 10, Result&: Mregparm)) {
2265 if (Mregparm != 4) {
2266 getToolChain().getDriver().Diag(
2267 DiagID: diag::err_drv_unsupported_option_argument)
2268 << A->getSpelling() << Value;
2269 }
2270 }
2271 }
2272}
2273
2274void Clang::AddWebAssemblyTargetArgs(const ArgList &Args,
2275 ArgStringList &CmdArgs) const {
2276 // Default to "hidden" visibility.
2277 if (!Args.hasArg(Ids: options::OPT_fvisibility_EQ,
2278 Ids: options::OPT_fvisibility_ms_compat))
2279 CmdArgs.push_back(Elt: "-fvisibility=hidden");
2280}
2281
2282void Clang::AddVETargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const {
2283 // Floating point operations and argument passing are hard.
2284 CmdArgs.push_back(Elt: "-mfloat-abi");
2285 CmdArgs.push_back(Elt: "hard");
2286}
2287
2288void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename,
2289 StringRef Target, const InputInfo &Output,
2290 const InputInfo &Input, const ArgList &Args) const {
2291 // If this is a dry run, do not create the compilation database file.
2292 if (C.getArgs().hasArg(Ids: options::OPT__HASH_HASH_HASH))
2293 return;
2294
2295 using llvm::yaml::escape;
2296 const Driver &D = getToolChain().getDriver();
2297
2298 if (!CompilationDatabase) {
2299 std::error_code EC;
2300 auto File = std::make_unique<llvm::raw_fd_ostream>(
2301 args&: Filename, args&: EC,
2302 args: llvm::sys::fs::OF_TextWithCRLF | llvm::sys::fs::OF_Append);
2303 if (EC) {
2304 D.Diag(DiagID: clang::diag::err_drv_compilationdatabase) << Filename
2305 << EC.message();
2306 return;
2307 }
2308 CompilationDatabase = std::move(File);
2309 }
2310 auto &CDB = *CompilationDatabase;
2311 auto CWD = D.getVFS().getCurrentWorkingDirectory();
2312 if (!CWD)
2313 CWD = ".";
2314 CDB << "{ \"directory\": \"" << escape(Input: *CWD) << "\"";
2315 CDB << ", \"file\": \"" << escape(Input: Input.getFilename()) << "\"";
2316 if (Output.isFilename())
2317 CDB << ", \"output\": \"" << escape(Input: Output.getFilename()) << "\"";
2318 CDB << ", \"arguments\": [\"" << escape(Input: D.ClangExecutable) << "\"";
2319 SmallString<128> Buf;
2320 Buf = "-x";
2321 Buf += types::getTypeName(Id: Input.getType());
2322 CDB << ", \"" << escape(Input: Buf) << "\"";
2323 if (!D.SysRoot.empty() && !Args.hasArg(Ids: options::OPT__sysroot_EQ)) {
2324 Buf = "--sysroot=";
2325 Buf += D.SysRoot;
2326 CDB << ", \"" << escape(Input: Buf) << "\"";
2327 }
2328 CDB << ", \"" << escape(Input: Input.getFilename()) << "\"";
2329 if (Output.isFilename())
2330 CDB << ", \"-o\", \"" << escape(Input: Output.getFilename()) << "\"";
2331 for (auto &A: Args) {
2332 auto &O = A->getOption();
2333 // Skip language selection, which is positional.
2334 if (O.getID() == options::OPT_x)
2335 continue;
2336 // Skip writing dependency output and the compilation database itself.
2337 if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group)
2338 continue;
2339 if (O.getID() == options::OPT_gen_cdb_fragment_path)
2340 continue;
2341 // Skip inputs.
2342 if (O.getKind() == Option::InputClass)
2343 continue;
2344 // Skip output.
2345 if (O.getID() == options::OPT_o)
2346 continue;
2347 // All other arguments are quoted and appended.
2348 ArgStringList ASL;
2349 A->render(Args, Output&: ASL);
2350 for (auto &it: ASL)
2351 CDB << ", \"" << escape(Input: it) << "\"";
2352 }
2353 Buf = "--target=";
2354 Buf += Target;
2355 CDB << ", \"" << escape(Input: Buf) << "\"]},\n";
2356}
2357
2358void Clang::DumpCompilationDatabaseFragmentToDir(
2359 StringRef Dir, Compilation &C, StringRef Target, const InputInfo &Output,
2360 const InputInfo &Input, const llvm::opt::ArgList &Args) const {
2361 // If this is a dry run, do not create the compilation database file.
2362 if (C.getArgs().hasArg(Ids: options::OPT__HASH_HASH_HASH))
2363 return;
2364
2365 if (CompilationDatabase)
2366 DumpCompilationDatabase(C, Filename: "", Target, Output, Input, Args);
2367
2368 SmallString<256> Path = Dir;
2369 const auto &Driver = C.getDriver();
2370 Driver.getVFS().makeAbsolute(Path);
2371 auto Err = llvm::sys::fs::create_directory(path: Path, /*IgnoreExisting=*/true);
2372 if (Err) {
2373 Driver.Diag(DiagID: diag::err_drv_compilationdatabase) << Dir << Err.message();
2374 return;
2375 }
2376
2377 llvm::sys::path::append(
2378 path&: Path,
2379 a: Twine(llvm::sys::path::filename(path: Input.getFilename())) + ".%%%%.json");
2380 int FD;
2381 SmallString<256> TempPath;
2382 Err = llvm::sys::fs::createUniqueFile(Model: Path, ResultFD&: FD, ResultPath&: TempPath,
2383 Flags: llvm::sys::fs::OF_Text);
2384 if (Err) {
2385 Driver.Diag(DiagID: diag::err_drv_compilationdatabase) << Path << Err.message();
2386 return;
2387 }
2388 CompilationDatabase =
2389 std::make_unique<llvm::raw_fd_ostream>(args&: FD, /*shouldClose=*/args: true);
2390 DumpCompilationDatabase(C, Filename: "", Target, Output, Input, Args);
2391}
2392
2393static bool CheckARMImplicitITArg(StringRef Value) {
2394 return Value == "always" || Value == "never" || Value == "arm" ||
2395 Value == "thumb";
2396}
2397
2398static void AddARMImplicitITArgs(const ArgList &Args, ArgStringList &CmdArgs,
2399 StringRef Value) {
2400 CmdArgs.push_back(Elt: "-mllvm");
2401 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-arm-implicit-it=" + Value));
2402}
2403
2404static void CollectArgsForIntegratedAssembler(Compilation &C,
2405 const ArgList &Args,
2406 ArgStringList &CmdArgs,
2407 const Driver &D) {
2408 // Default to -mno-relax-all.
2409 //
2410 // Note: RISC-V requires an indirect jump for offsets larger than 1MiB. This
2411 // cannot be done by assembler branch relaxation as it needs a free temporary
2412 // register. Because of this, branch relaxation is handled by a MachineIR pass
2413 // before the assembler. Forcing assembler branch relaxation for -O0 makes the
2414 // MachineIR branch relaxation inaccurate and it will miss cases where an
2415 // indirect branch is necessary.
2416 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_mrelax_all,
2417 Neg: options::OPT_mno_relax_all);
2418
2419 // Only default to -mincremental-linker-compatible if we think we are
2420 // targeting the MSVC linker.
2421 bool DefaultIncrementalLinkerCompatible =
2422 C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment();
2423 if (Args.hasFlag(Pos: options::OPT_mincremental_linker_compatible,
2424 Neg: options::OPT_mno_incremental_linker_compatible,
2425 Default: DefaultIncrementalLinkerCompatible))
2426 CmdArgs.push_back(Elt: "-mincremental-linker-compatible");
2427
2428 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_femit_dwarf_unwind_EQ);
2429
2430 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_femit_compact_unwind_non_canonical,
2431 Neg: options::OPT_fno_emit_compact_unwind_non_canonical);
2432
2433 // If you add more args here, also add them to the block below that
2434 // starts with "// If CollectArgsForIntegratedAssembler() isn't called below".
2435
2436 // When passing -I arguments to the assembler we sometimes need to
2437 // unconditionally take the next argument. For example, when parsing
2438 // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the
2439 // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo'
2440 // arg after parsing the '-I' arg.
2441 bool TakeNextArg = false;
2442
2443 const llvm::Triple &Triple = C.getDefaultToolChain().getTriple();
2444 bool IsELF = Triple.isOSBinFormatELF();
2445 bool Crel = false, ExperimentalCrel = false;
2446 bool ImplicitMapSyms = false;
2447 bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations();
2448 bool UseNoExecStack = false;
2449 bool Msa = false;
2450 const char *MipsTargetFeature = nullptr;
2451 llvm::SmallVector<const char *> SparcTargetFeatures;
2452 StringRef ImplicitIt;
2453 for (const Arg *A :
2454 Args.filtered(Ids: options::OPT_Wa_COMMA, Ids: options::OPT_Xassembler,
2455 Ids: options::OPT_mimplicit_it_EQ)) {
2456 A->claim();
2457
2458 if (A->getOption().getID() == options::OPT_mimplicit_it_EQ) {
2459 switch (C.getDefaultToolChain().getArch()) {
2460 case llvm::Triple::arm:
2461 case llvm::Triple::armeb:
2462 case llvm::Triple::thumb:
2463 case llvm::Triple::thumbeb:
2464 // Only store the value; the last value set takes effect.
2465 ImplicitIt = A->getValue();
2466 if (!CheckARMImplicitITArg(Value: ImplicitIt))
2467 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
2468 << A->getSpelling() << ImplicitIt;
2469 continue;
2470 default:
2471 break;
2472 }
2473 }
2474
2475 for (StringRef Value : A->getValues()) {
2476 if (TakeNextArg) {
2477 CmdArgs.push_back(Elt: Value.data());
2478 TakeNextArg = false;
2479 continue;
2480 }
2481
2482 if (C.getDefaultToolChain().getTriple().isOSBinFormatCOFF() &&
2483 Value == "-mbig-obj")
2484 continue; // LLVM handles bigobj automatically
2485
2486 auto Equal = Value.split(Separator: '=');
2487 auto checkArg = [&](bool ValidTarget,
2488 std::initializer_list<const char *> Set) {
2489 if (!ValidTarget) {
2490 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
2491 << (Twine("-Wa,") + Equal.first + "=").str()
2492 << Triple.getTriple();
2493 } else if (!llvm::is_contained(Set, Element: Equal.second)) {
2494 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
2495 << (Twine("-Wa,") + Equal.first + "=").str() << Equal.second;
2496 }
2497 };
2498 switch (C.getDefaultToolChain().getArch()) {
2499 default:
2500 break;
2501 case llvm::Triple::x86:
2502 case llvm::Triple::x86_64:
2503 if (Equal.first == "-mrelax-relocations" ||
2504 Equal.first == "--mrelax-relocations") {
2505 UseRelaxRelocations = Equal.second == "yes";
2506 checkArg(IsELF, {"yes", "no"});
2507 continue;
2508 }
2509 if (Value == "-msse2avx") {
2510 CmdArgs.push_back(Elt: "-msse2avx");
2511 continue;
2512 }
2513 break;
2514 case llvm::Triple::wasm32:
2515 case llvm::Triple::wasm64:
2516 if (Value == "--no-type-check") {
2517 CmdArgs.push_back(Elt: "-mno-type-check");
2518 continue;
2519 }
2520 break;
2521 case llvm::Triple::thumb:
2522 case llvm::Triple::thumbeb:
2523 case llvm::Triple::arm:
2524 case llvm::Triple::armeb:
2525 if (Equal.first == "-mimplicit-it") {
2526 // Only store the value; the last value set takes effect.
2527 ImplicitIt = Equal.second;
2528 checkArg(true, {"always", "never", "arm", "thumb"});
2529 continue;
2530 }
2531 if (Value == "-mthumb")
2532 // -mthumb has already been processed in ComputeLLVMTriple()
2533 // recognize but skip over here.
2534 continue;
2535 break;
2536 case llvm::Triple::aarch64:
2537 case llvm::Triple::aarch64_be:
2538 case llvm::Triple::aarch64_32:
2539 if (Equal.first == "-mmapsyms") {
2540 ImplicitMapSyms = Equal.second == "implicit";
2541 checkArg(IsELF, {"default", "implicit"});
2542 continue;
2543 }
2544 break;
2545 case llvm::Triple::mips:
2546 case llvm::Triple::mipsel:
2547 case llvm::Triple::mips64:
2548 case llvm::Triple::mips64el:
2549 if (Value == "--trap") {
2550 CmdArgs.push_back(Elt: "-target-feature");
2551 CmdArgs.push_back(Elt: "+use-tcc-in-div");
2552 continue;
2553 }
2554 if (Value == "--break") {
2555 CmdArgs.push_back(Elt: "-target-feature");
2556 CmdArgs.push_back(Elt: "-use-tcc-in-div");
2557 continue;
2558 }
2559 if (Value.starts_with(Prefix: "-msoft-float")) {
2560 CmdArgs.push_back(Elt: "-target-feature");
2561 CmdArgs.push_back(Elt: "+soft-float");
2562 continue;
2563 }
2564 if (Value.starts_with(Prefix: "-mhard-float")) {
2565 CmdArgs.push_back(Elt: "-target-feature");
2566 CmdArgs.push_back(Elt: "-soft-float");
2567 continue;
2568 }
2569 if (Value == "-mmsa") {
2570 Msa = true;
2571 continue;
2572 }
2573 if (Value == "-mno-msa") {
2574 Msa = false;
2575 continue;
2576 }
2577 MipsTargetFeature = llvm::StringSwitch<const char *>(Value)
2578 .Case(S: "-mips1", Value: "+mips1")
2579 .Case(S: "-mips2", Value: "+mips2")
2580 .Case(S: "-mips3", Value: "+mips3")
2581 .Case(S: "-mips4", Value: "+mips4")
2582 .Case(S: "-mips5", Value: "+mips5")
2583 .Case(S: "-mips32", Value: "+mips32")
2584 .Case(S: "-mips32r2", Value: "+mips32r2")
2585 .Case(S: "-mips32r3", Value: "+mips32r3")
2586 .Case(S: "-mips32r5", Value: "+mips32r5")
2587 .Case(S: "-mips32r6", Value: "+mips32r6")
2588 .Case(S: "-mips64", Value: "+mips64")
2589 .Case(S: "-mips64r2", Value: "+mips64r2")
2590 .Case(S: "-mips64r3", Value: "+mips64r3")
2591 .Case(S: "-mips64r5", Value: "+mips64r5")
2592 .Case(S: "-mips64r6", Value: "+mips64r6")
2593 .Default(Value: nullptr);
2594 if (MipsTargetFeature)
2595 continue;
2596 break;
2597
2598 case llvm::Triple::sparc:
2599 case llvm::Triple::sparcel:
2600 case llvm::Triple::sparcv9:
2601 if (Value == "--undeclared-regs") {
2602 // LLVM already allows undeclared use of G registers, so this option
2603 // becomes a no-op. This solely exists for GNU compatibility.
2604 // TODO implement --no-undeclared-regs
2605 continue;
2606 }
2607 SparcTargetFeatures =
2608 llvm::StringSwitch<llvm::SmallVector<const char *>>(Value)
2609 .Case(S: "-Av8", Value: {"-v8plus"})
2610 .Case(S: "-Av8plus", Value: {"+v8plus", "+v9"})
2611 .Case(S: "-Av8plusa", Value: {"+v8plus", "+v9", "+vis"})
2612 .Case(S: "-Av8plusb", Value: {"+v8plus", "+v9", "+vis", "+vis2"})
2613 .Case(S: "-Av8plusd", Value: {"+v8plus", "+v9", "+vis", "+vis2", "+vis3"})
2614 .Case(S: "-Av9", Value: {"+v9"})
2615 .Case(S: "-Av9a", Value: {"+v9", "+vis"})
2616 .Case(S: "-Av9b", Value: {"+v9", "+vis", "+vis2"})
2617 .Case(S: "-Av9d", Value: {"+v9", "+vis", "+vis2", "+vis3"})
2618 .Default(Value: {});
2619 if (!SparcTargetFeatures.empty())
2620 continue;
2621 break;
2622 }
2623
2624 if (Value == "-force_cpusubtype_ALL") {
2625 // Do nothing, this is the default and we don't support anything else.
2626 } else if (Value == "-L") {
2627 CmdArgs.push_back(Elt: "-msave-temp-labels");
2628 } else if (Value == "--fatal-warnings") {
2629 CmdArgs.push_back(Elt: "-massembler-fatal-warnings");
2630 } else if (Value == "--no-warn" || Value == "-W") {
2631 CmdArgs.push_back(Elt: "-massembler-no-warn");
2632 } else if (Value == "--noexecstack") {
2633 UseNoExecStack = true;
2634 } else if (Value.starts_with(Prefix: "-compress-debug-sections") ||
2635 Value.starts_with(Prefix: "--compress-debug-sections") ||
2636 Value == "-nocompress-debug-sections" ||
2637 Value == "--nocompress-debug-sections") {
2638 CmdArgs.push_back(Elt: Value.data());
2639 } else if (Value == "--crel") {
2640 Crel = true;
2641 } else if (Value == "--no-crel") {
2642 Crel = false;
2643 } else if (Value == "--allow-experimental-crel") {
2644 ExperimentalCrel = true;
2645 } else if (Value.starts_with(Prefix: "-I")) {
2646 CmdArgs.push_back(Elt: Value.data());
2647 // We need to consume the next argument if the current arg is a plain
2648 // -I. The next arg will be the include directory.
2649 if (Value == "-I")
2650 TakeNextArg = true;
2651 } else if (Value.starts_with(Prefix: "-gdwarf-")) {
2652 // "-gdwarf-N" options are not cc1as options.
2653 unsigned DwarfVersion = DwarfVersionNum(ArgValue: Value);
2654 if (DwarfVersion == 0) { // Send it onward, and let cc1as complain.
2655 CmdArgs.push_back(Elt: Value.data());
2656 } else {
2657 RenderDebugEnablingArgs(Args, CmdArgs,
2658 DebugInfoKind: llvm::codegenoptions::DebugInfoConstructor,
2659 DwarfVersion, DebuggerTuning: llvm::DebuggerKind::Default);
2660 }
2661 } else if (Value.starts_with(Prefix: "-mcpu") || Value.starts_with(Prefix: "-mfpu") ||
2662 Value.starts_with(Prefix: "-mhwdiv") || Value.starts_with(Prefix: "-march")) {
2663 // Do nothing, we'll validate it later.
2664 } else if (Value == "-defsym" || Value == "--defsym") {
2665 if (A->getNumValues() != 2) {
2666 D.Diag(DiagID: diag::err_drv_defsym_invalid_format) << Value;
2667 break;
2668 }
2669 const char *S = A->getValue(N: 1);
2670 auto Pair = StringRef(S).split(Separator: '=');
2671 auto Sym = Pair.first;
2672 auto SVal = Pair.second;
2673
2674 if (Sym.empty() || SVal.empty()) {
2675 D.Diag(DiagID: diag::err_drv_defsym_invalid_format) << S;
2676 break;
2677 }
2678 int64_t IVal;
2679 if (SVal.getAsInteger(Radix: 0, Result&: IVal)) {
2680 D.Diag(DiagID: diag::err_drv_defsym_invalid_symval) << SVal;
2681 break;
2682 }
2683 CmdArgs.push_back(Elt: "--defsym");
2684 TakeNextArg = true;
2685 } else if (Value == "-fdebug-compilation-dir") {
2686 CmdArgs.push_back(Elt: "-fdebug-compilation-dir");
2687 TakeNextArg = true;
2688 } else if (Value.consume_front(Prefix: "-fdebug-compilation-dir=")) {
2689 // The flag is a -Wa / -Xassembler argument and Options doesn't
2690 // parse the argument, so this isn't automatically aliased to
2691 // -fdebug-compilation-dir (without '=') here.
2692 CmdArgs.push_back(Elt: "-fdebug-compilation-dir");
2693 CmdArgs.push_back(Elt: Value.data());
2694 } else if (Value == "--version") {
2695 D.PrintVersion(C, OS&: llvm::outs());
2696 } else {
2697 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
2698 << A->getSpelling() << Value;
2699 }
2700 }
2701 }
2702 if (ImplicitIt.size())
2703 AddARMImplicitITArgs(Args, CmdArgs, Value: ImplicitIt);
2704 if (Crel) {
2705 if (!ExperimentalCrel)
2706 D.Diag(DiagID: diag::err_drv_experimental_crel);
2707 if (Triple.isOSBinFormatELF() && !Triple.isMIPS()) {
2708 CmdArgs.push_back(Elt: "--crel");
2709 } else {
2710 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
2711 << "-Wa,--crel" << D.getTargetTriple();
2712 }
2713 }
2714 if (ImplicitMapSyms)
2715 CmdArgs.push_back(Elt: "-mmapsyms=implicit");
2716 if (Msa)
2717 CmdArgs.push_back(Elt: "-mmsa");
2718 if (!UseRelaxRelocations)
2719 CmdArgs.push_back(Elt: "-mrelax-relocations=no");
2720 if (UseNoExecStack)
2721 CmdArgs.push_back(Elt: "-mnoexecstack");
2722 if (MipsTargetFeature != nullptr) {
2723 CmdArgs.push_back(Elt: "-target-feature");
2724 CmdArgs.push_back(Elt: MipsTargetFeature);
2725 }
2726
2727 // Those OSes default to enabling VIS on 64-bit SPARC.
2728 // See also the corresponding code for external assemblers in
2729 // sparc::getSparcAsmModeForCPU().
2730 bool IsSparcV9ATarget =
2731 (C.getDefaultToolChain().getArch() == llvm::Triple::sparcv9) &&
2732 (Triple.isOSLinux() || Triple.isOSFreeBSD() || Triple.isOSOpenBSD());
2733 if (IsSparcV9ATarget && SparcTargetFeatures.empty()) {
2734 CmdArgs.push_back(Elt: "-target-feature");
2735 CmdArgs.push_back(Elt: "+vis");
2736 }
2737 for (const char *Feature : SparcTargetFeatures) {
2738 CmdArgs.push_back(Elt: "-target-feature");
2739 CmdArgs.push_back(Elt: Feature);
2740 }
2741
2742 // forward -fembed-bitcode to assmebler
2743 if (C.getDriver().embedBitcodeEnabled() ||
2744 C.getDriver().embedBitcodeMarkerOnly())
2745 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fembed_bitcode_EQ);
2746
2747 if (const char *AsSecureLogFile = getenv(name: "AS_SECURE_LOG_FILE")) {
2748 CmdArgs.push_back(Elt: "-as-secure-log-file");
2749 CmdArgs.push_back(Elt: Args.MakeArgString(Str: AsSecureLogFile));
2750 }
2751}
2752
2753static std::string ComplexRangeKindToStr(LangOptions::ComplexRangeKind Range) {
2754 switch (Range) {
2755 case LangOptions::ComplexRangeKind::CX_Full:
2756 return "full";
2757 break;
2758 case LangOptions::ComplexRangeKind::CX_Basic:
2759 return "basic";
2760 break;
2761 case LangOptions::ComplexRangeKind::CX_Improved:
2762 return "improved";
2763 break;
2764 case LangOptions::ComplexRangeKind::CX_Promoted:
2765 return "promoted";
2766 break;
2767 default:
2768 return "";
2769 }
2770}
2771
2772static std::string ComplexArithmeticStr(LangOptions::ComplexRangeKind Range) {
2773 return (Range == LangOptions::ComplexRangeKind::CX_None)
2774 ? ""
2775 : "-fcomplex-arithmetic=" + ComplexRangeKindToStr(Range);
2776}
2777
2778static void EmitComplexRangeDiag(const Driver &D, std::string str1,
2779 std::string str2) {
2780 if (str1 != str2 && !str2.empty() && !str1.empty()) {
2781 D.Diag(DiagID: clang::diag::warn_drv_overriding_option) << str1 << str2;
2782 }
2783}
2784
2785static std::string
2786RenderComplexRangeOption(LangOptions::ComplexRangeKind Range) {
2787 std::string ComplexRangeStr = ComplexRangeKindToStr(Range);
2788 if (!ComplexRangeStr.empty())
2789 return "-complex-range=" + ComplexRangeStr;
2790 return ComplexRangeStr;
2791}
2792
2793static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
2794 bool OFastEnabled, const ArgList &Args,
2795 ArgStringList &CmdArgs,
2796 const JobAction &JA) {
2797 // List of veclibs which when used with -fveclib imply -fno-math-errno.
2798 constexpr std::array VecLibImpliesNoMathErrno{llvm::StringLiteral("ArmPL"),
2799 llvm::StringLiteral("SLEEF")};
2800 bool NoMathErrnoWasImpliedByVecLib = false;
2801 const Arg *VecLibArg = nullptr;
2802 // Track the arg (if any) that enabled errno after -fveclib for diagnostics.
2803 const Arg *ArgThatEnabledMathErrnoAfterVecLib = nullptr;
2804
2805 // Handle various floating point optimization flags, mapping them to the
2806 // appropriate LLVM code generation flags. This is complicated by several
2807 // "umbrella" flags, so we do this by stepping through the flags incrementally
2808 // adjusting what we think is enabled/disabled, then at the end setting the
2809 // LLVM flags based on the final state.
2810 bool HonorINFs = true;
2811 bool HonorNaNs = true;
2812 bool ApproxFunc = false;
2813 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
2814 bool MathErrno = TC.IsMathErrnoDefault();
2815 bool AssociativeMath = false;
2816 bool ReciprocalMath = false;
2817 bool SignedZeros = true;
2818 bool TrappingMath = false; // Implemented via -ffp-exception-behavior
2819 bool TrappingMathPresent = false; // Is trapping-math in args, and not
2820 // overriden by ffp-exception-behavior?
2821 bool RoundingFPMath = false;
2822 // -ffp-model values: strict, fast, precise
2823 StringRef FPModel = "";
2824 // -ffp-exception-behavior options: strict, maytrap, ignore
2825 StringRef FPExceptionBehavior = "";
2826 // -ffp-eval-method options: double, extended, source
2827 StringRef FPEvalMethod = "";
2828 llvm::DenormalMode DenormalFPMath =
2829 TC.getDefaultDenormalModeForType(DriverArgs: Args, JA);
2830 llvm::DenormalMode DenormalFP32Math =
2831 TC.getDefaultDenormalModeForType(DriverArgs: Args, JA, FPType: &llvm::APFloat::IEEEsingle());
2832
2833 // CUDA and HIP don't rely on the frontend to pass an ffp-contract option.
2834 // If one wasn't given by the user, don't pass it here.
2835 StringRef FPContract;
2836 StringRef LastSeenFfpContractOption;
2837 StringRef LastFpContractOverrideOption;
2838 bool SeenUnsafeMathModeOption = false;
2839 if (!JA.isDeviceOffloading(OKind: Action::OFK_Cuda) &&
2840 !JA.isOffloading(OKind: Action::OFK_HIP))
2841 FPContract = "on";
2842 bool StrictFPModel = false;
2843 StringRef Float16ExcessPrecision = "";
2844 StringRef BFloat16ExcessPrecision = "";
2845 LangOptions::ComplexRangeKind Range = LangOptions::ComplexRangeKind::CX_None;
2846 std::string ComplexRangeStr;
2847 std::string GccRangeComplexOption;
2848 std::string LastComplexRangeOption;
2849
2850 auto setComplexRange = [&](LangOptions::ComplexRangeKind NewRange) {
2851 // Warn if user expects to perform full implementation of complex
2852 // multiplication or division in the presence of nnan or ninf flags.
2853 if (Range != NewRange)
2854 EmitComplexRangeDiag(D,
2855 str1: !GccRangeComplexOption.empty()
2856 ? GccRangeComplexOption
2857 : ComplexArithmeticStr(Range),
2858 str2: ComplexArithmeticStr(Range: NewRange));
2859 Range = NewRange;
2860 };
2861
2862 // Lambda to set fast-math options. This is also used by -ffp-model=fast
2863 auto applyFastMath = [&](bool Aggressive) {
2864 if (Aggressive) {
2865 HonorINFs = false;
2866 HonorNaNs = false;
2867 setComplexRange(LangOptions::ComplexRangeKind::CX_Basic);
2868 } else {
2869 HonorINFs = true;
2870 HonorNaNs = true;
2871 setComplexRange(LangOptions::ComplexRangeKind::CX_Promoted);
2872 }
2873 MathErrno = false;
2874 AssociativeMath = true;
2875 ReciprocalMath = true;
2876 ApproxFunc = true;
2877 SignedZeros = false;
2878 TrappingMath = false;
2879 RoundingFPMath = false;
2880 FPExceptionBehavior = "";
2881 FPContract = "fast";
2882 SeenUnsafeMathModeOption = true;
2883 };
2884
2885 // Lambda to consolidate common handling for fp-contract
2886 auto restoreFPContractState = [&]() {
2887 // CUDA and HIP don't rely on the frontend to pass an ffp-contract option.
2888 // For other targets, if the state has been changed by one of the
2889 // unsafe-math umbrella options a subsequent -fno-fast-math or
2890 // -fno-unsafe-math-optimizations option reverts to the last value seen for
2891 // the -ffp-contract option or "on" if we have not seen the -ffp-contract
2892 // option. If we have not seen an unsafe-math option or -ffp-contract,
2893 // we leave the FPContract state unchanged.
2894 if (!JA.isDeviceOffloading(OKind: Action::OFK_Cuda) &&
2895 !JA.isOffloading(OKind: Action::OFK_HIP)) {
2896 if (LastSeenFfpContractOption != "")
2897 FPContract = LastSeenFfpContractOption;
2898 else if (SeenUnsafeMathModeOption)
2899 FPContract = "on";
2900 }
2901 // In this case, we're reverting to the last explicit fp-contract option
2902 // or the platform default
2903 LastFpContractOverrideOption = "";
2904 };
2905
2906 if (const Arg *A = Args.getLastArg(Ids: options::OPT_flimited_precision_EQ)) {
2907 CmdArgs.push_back(Elt: "-mlimit-float-precision");
2908 CmdArgs.push_back(Elt: A->getValue());
2909 }
2910
2911 for (const Arg *A : Args) {
2912 auto CheckMathErrnoForVecLib =
2913 llvm::make_scope_exit(F: [&, MathErrnoBeforeArg = MathErrno] {
2914 if (NoMathErrnoWasImpliedByVecLib && !MathErrnoBeforeArg && MathErrno)
2915 ArgThatEnabledMathErrnoAfterVecLib = A;
2916 });
2917
2918 switch (A->getOption().getID()) {
2919 // If this isn't an FP option skip the claim below
2920 default: continue;
2921
2922 case options::OPT_fcx_limited_range:
2923 if (GccRangeComplexOption.empty()) {
2924 if (Range != LangOptions::ComplexRangeKind::CX_Basic)
2925 EmitComplexRangeDiag(D, str1: RenderComplexRangeOption(Range),
2926 str2: "-fcx-limited-range");
2927 } else {
2928 if (GccRangeComplexOption != "-fno-cx-limited-range")
2929 EmitComplexRangeDiag(D, str1: GccRangeComplexOption, str2: "-fcx-limited-range");
2930 }
2931 GccRangeComplexOption = "-fcx-limited-range";
2932 LastComplexRangeOption = A->getSpelling();
2933 Range = LangOptions::ComplexRangeKind::CX_Basic;
2934 break;
2935 case options::OPT_fno_cx_limited_range:
2936 if (GccRangeComplexOption.empty()) {
2937 EmitComplexRangeDiag(D, str1: RenderComplexRangeOption(Range),
2938 str2: "-fno-cx-limited-range");
2939 } else {
2940 if (GccRangeComplexOption != "-fcx-limited-range" &&
2941 GccRangeComplexOption != "-fno-cx-fortran-rules")
2942 EmitComplexRangeDiag(D, str1: GccRangeComplexOption,
2943 str2: "-fno-cx-limited-range");
2944 }
2945 GccRangeComplexOption = "-fno-cx-limited-range";
2946 LastComplexRangeOption = A->getSpelling();
2947 Range = LangOptions::ComplexRangeKind::CX_Full;
2948 break;
2949 case options::OPT_fcx_fortran_rules:
2950 if (GccRangeComplexOption.empty())
2951 EmitComplexRangeDiag(D, str1: RenderComplexRangeOption(Range),
2952 str2: "-fcx-fortran-rules");
2953 else
2954 EmitComplexRangeDiag(D, str1: GccRangeComplexOption, str2: "-fcx-fortran-rules");
2955 GccRangeComplexOption = "-fcx-fortran-rules";
2956 LastComplexRangeOption = A->getSpelling();
2957 Range = LangOptions::ComplexRangeKind::CX_Improved;
2958 break;
2959 case options::OPT_fno_cx_fortran_rules:
2960 if (GccRangeComplexOption.empty()) {
2961 EmitComplexRangeDiag(D, str1: RenderComplexRangeOption(Range),
2962 str2: "-fno-cx-fortran-rules");
2963 } else {
2964 if (GccRangeComplexOption != "-fno-cx-limited-range")
2965 EmitComplexRangeDiag(D, str1: GccRangeComplexOption,
2966 str2: "-fno-cx-fortran-rules");
2967 }
2968 GccRangeComplexOption = "-fno-cx-fortran-rules";
2969 LastComplexRangeOption = A->getSpelling();
2970 Range = LangOptions::ComplexRangeKind::CX_Full;
2971 break;
2972 case options::OPT_fcomplex_arithmetic_EQ: {
2973 LangOptions::ComplexRangeKind RangeVal;
2974 StringRef Val = A->getValue();
2975 if (Val == "full")
2976 RangeVal = LangOptions::ComplexRangeKind::CX_Full;
2977 else if (Val == "improved")
2978 RangeVal = LangOptions::ComplexRangeKind::CX_Improved;
2979 else if (Val == "promoted")
2980 RangeVal = LangOptions::ComplexRangeKind::CX_Promoted;
2981 else if (Val == "basic")
2982 RangeVal = LangOptions::ComplexRangeKind::CX_Basic;
2983 else {
2984 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
2985 << A->getSpelling() << Val;
2986 break;
2987 }
2988 if (!GccRangeComplexOption.empty()) {
2989 if (GccRangeComplexOption != "-fcx-limited-range") {
2990 if (GccRangeComplexOption != "-fcx-fortran-rules") {
2991 if (RangeVal != LangOptions::ComplexRangeKind::CX_Improved)
2992 EmitComplexRangeDiag(D, str1: GccRangeComplexOption,
2993 str2: ComplexArithmeticStr(Range: RangeVal));
2994 } else {
2995 EmitComplexRangeDiag(D, str1: GccRangeComplexOption,
2996 str2: ComplexArithmeticStr(Range: RangeVal));
2997 }
2998 } else {
2999 if (RangeVal != LangOptions::ComplexRangeKind::CX_Basic)
3000 EmitComplexRangeDiag(D, str1: GccRangeComplexOption,
3001 str2: ComplexArithmeticStr(Range: RangeVal));
3002 }
3003 }
3004 LastComplexRangeOption =
3005 Args.MakeArgString(Str: A->getSpelling() + A->getValue());
3006 Range = RangeVal;
3007 break;
3008 }
3009 case options::OPT_ffp_model_EQ: {
3010 // If -ffp-model= is seen, reset to fno-fast-math
3011 HonorINFs = true;
3012 HonorNaNs = true;
3013 ApproxFunc = false;
3014 // Turning *off* -ffast-math restores the toolchain default.
3015 MathErrno = TC.IsMathErrnoDefault();
3016 AssociativeMath = false;
3017 ReciprocalMath = false;
3018 SignedZeros = true;
3019
3020 StringRef Val = A->getValue();
3021 if (OFastEnabled && Val != "aggressive") {
3022 // Only -ffp-model=aggressive is compatible with -OFast, ignore.
3023 D.Diag(DiagID: clang::diag::warn_drv_overriding_option)
3024 << Args.MakeArgString(Str: "-ffp-model=" + Val) << "-Ofast";
3025 break;
3026 }
3027 StrictFPModel = false;
3028 if (!FPModel.empty() && FPModel != Val)
3029 D.Diag(DiagID: clang::diag::warn_drv_overriding_option)
3030 << Args.MakeArgString(Str: "-ffp-model=" + FPModel)
3031 << Args.MakeArgString(Str: "-ffp-model=" + Val);
3032 if (Val == "fast") {
3033 FPModel = Val;
3034 applyFastMath(false);
3035 // applyFastMath sets fp-contract="fast"
3036 LastFpContractOverrideOption = "-ffp-model=fast";
3037 } else if (Val == "aggressive") {
3038 FPModel = Val;
3039 applyFastMath(true);
3040 // applyFastMath sets fp-contract="fast"
3041 LastFpContractOverrideOption = "-ffp-model=aggressive";
3042 } else if (Val == "precise") {
3043 FPModel = Val;
3044 FPContract = "on";
3045 LastFpContractOverrideOption = "-ffp-model=precise";
3046 setComplexRange(LangOptions::ComplexRangeKind::CX_Full);
3047 } else if (Val == "strict") {
3048 StrictFPModel = true;
3049 FPExceptionBehavior = "strict";
3050 FPModel = Val;
3051 FPContract = "off";
3052 LastFpContractOverrideOption = "-ffp-model=strict";
3053 TrappingMath = true;
3054 RoundingFPMath = true;
3055 setComplexRange(LangOptions::ComplexRangeKind::CX_Full);
3056 } else
3057 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
3058 << A->getSpelling() << Val;
3059 LastComplexRangeOption = A->getSpelling();
3060 break;
3061 }
3062
3063 // Options controlling individual features
3064 case options::OPT_fhonor_infinities: HonorINFs = true; break;
3065 case options::OPT_fno_honor_infinities: HonorINFs = false; break;
3066 case options::OPT_fhonor_nans: HonorNaNs = true; break;
3067 case options::OPT_fno_honor_nans: HonorNaNs = false; break;
3068 case options::OPT_fapprox_func: ApproxFunc = true; break;
3069 case options::OPT_fno_approx_func: ApproxFunc = false; break;
3070 case options::OPT_fmath_errno: MathErrno = true; break;
3071 case options::OPT_fno_math_errno: MathErrno = false; break;
3072 case options::OPT_fassociative_math: AssociativeMath = true; break;
3073 case options::OPT_fno_associative_math: AssociativeMath = false; break;
3074 case options::OPT_freciprocal_math: ReciprocalMath = true; break;
3075 case options::OPT_fno_reciprocal_math: ReciprocalMath = false; break;
3076 case options::OPT_fsigned_zeros: SignedZeros = true; break;
3077 case options::OPT_fno_signed_zeros: SignedZeros = false; break;
3078 case options::OPT_ftrapping_math:
3079 if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
3080 FPExceptionBehavior != "strict")
3081 // Warn that previous value of option is overridden.
3082 D.Diag(DiagID: clang::diag::warn_drv_overriding_option)
3083 << Args.MakeArgString(Str: "-ffp-exception-behavior=" +
3084 FPExceptionBehavior)
3085 << "-ftrapping-math";
3086 TrappingMath = true;
3087 TrappingMathPresent = true;
3088 FPExceptionBehavior = "strict";
3089 break;
3090 case options::OPT_fveclib:
3091 VecLibArg = A;
3092 NoMathErrnoWasImpliedByVecLib =
3093 llvm::is_contained(Range: VecLibImpliesNoMathErrno, Element: A->getValue());
3094 if (NoMathErrnoWasImpliedByVecLib)
3095 MathErrno = false;
3096 break;
3097 case options::OPT_fno_trapping_math:
3098 if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
3099 FPExceptionBehavior != "ignore")
3100 // Warn that previous value of option is overridden.
3101 D.Diag(DiagID: clang::diag::warn_drv_overriding_option)
3102 << Args.MakeArgString(Str: "-ffp-exception-behavior=" +
3103 FPExceptionBehavior)
3104 << "-fno-trapping-math";
3105 TrappingMath = false;
3106 TrappingMathPresent = true;
3107 FPExceptionBehavior = "ignore";
3108 break;
3109
3110 case options::OPT_frounding_math:
3111 RoundingFPMath = true;
3112 break;
3113
3114 case options::OPT_fno_rounding_math:
3115 RoundingFPMath = false;
3116 break;
3117
3118 case options::OPT_fdenormal_fp_math_EQ:
3119 DenormalFPMath = llvm::parseDenormalFPAttribute(Str: A->getValue());
3120 DenormalFP32Math = DenormalFPMath;
3121 if (!DenormalFPMath.isValid()) {
3122 D.Diag(DiagID: diag::err_drv_invalid_value)
3123 << A->getAsString(Args) << A->getValue();
3124 }
3125 break;
3126
3127 case options::OPT_fdenormal_fp_math_f32_EQ:
3128 DenormalFP32Math = llvm::parseDenormalFPAttribute(Str: A->getValue());
3129 if (!DenormalFP32Math.isValid()) {
3130 D.Diag(DiagID: diag::err_drv_invalid_value)
3131 << A->getAsString(Args) << A->getValue();
3132 }
3133 break;
3134
3135 // Validate and pass through -ffp-contract option.
3136 case options::OPT_ffp_contract: {
3137 StringRef Val = A->getValue();
3138 if (Val == "fast" || Val == "on" || Val == "off" ||
3139 Val == "fast-honor-pragmas") {
3140 if (Val != FPContract && LastFpContractOverrideOption != "") {
3141 D.Diag(DiagID: clang::diag::warn_drv_overriding_option)
3142 << LastFpContractOverrideOption
3143 << Args.MakeArgString(Str: "-ffp-contract=" + Val);
3144 }
3145
3146 FPContract = Val;
3147 LastSeenFfpContractOption = Val;
3148 LastFpContractOverrideOption = "";
3149 } else
3150 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
3151 << A->getSpelling() << Val;
3152 break;
3153 }
3154
3155 // Validate and pass through -ffp-exception-behavior option.
3156 case options::OPT_ffp_exception_behavior_EQ: {
3157 StringRef Val = A->getValue();
3158 if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
3159 FPExceptionBehavior != Val)
3160 // Warn that previous value of option is overridden.
3161 D.Diag(DiagID: clang::diag::warn_drv_overriding_option)
3162 << Args.MakeArgString(Str: "-ffp-exception-behavior=" +
3163 FPExceptionBehavior)
3164 << Args.MakeArgString(Str: "-ffp-exception-behavior=" + Val);
3165 TrappingMath = TrappingMathPresent = false;
3166 if (Val == "ignore" || Val == "maytrap")
3167 FPExceptionBehavior = Val;
3168 else if (Val == "strict") {
3169 FPExceptionBehavior = Val;
3170 TrappingMath = TrappingMathPresent = true;
3171 } else
3172 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
3173 << A->getSpelling() << Val;
3174 break;
3175 }
3176
3177 // Validate and pass through -ffp-eval-method option.
3178 case options::OPT_ffp_eval_method_EQ: {
3179 StringRef Val = A->getValue();
3180 if (Val == "double" || Val == "extended" || Val == "source")
3181 FPEvalMethod = Val;
3182 else
3183 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
3184 << A->getSpelling() << Val;
3185 break;
3186 }
3187
3188 case options::OPT_fexcess_precision_EQ: {
3189 StringRef Val = A->getValue();
3190 const llvm::Triple::ArchType Arch = TC.getArch();
3191 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
3192 if (Val == "standard" || Val == "fast")
3193 Float16ExcessPrecision = Val;
3194 // To make it GCC compatible, allow the value of "16" which
3195 // means disable excess precision, the same meaning than clang's
3196 // equivalent value "none".
3197 else if (Val == "16")
3198 Float16ExcessPrecision = "none";
3199 else
3200 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
3201 << A->getSpelling() << Val;
3202 } else {
3203 if (!(Val == "standard" || Val == "fast"))
3204 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
3205 << A->getSpelling() << Val;
3206 }
3207 BFloat16ExcessPrecision = Float16ExcessPrecision;
3208 break;
3209 }
3210 case options::OPT_ffinite_math_only:
3211 HonorINFs = false;
3212 HonorNaNs = false;
3213 break;
3214 case options::OPT_fno_finite_math_only:
3215 HonorINFs = true;
3216 HonorNaNs = true;
3217 break;
3218
3219 case options::OPT_funsafe_math_optimizations:
3220 AssociativeMath = true;
3221 ReciprocalMath = true;
3222 SignedZeros = false;
3223 ApproxFunc = true;
3224 TrappingMath = false;
3225 FPExceptionBehavior = "";
3226 FPContract = "fast";
3227 LastFpContractOverrideOption = "-funsafe-math-optimizations";
3228 SeenUnsafeMathModeOption = true;
3229 break;
3230 case options::OPT_fno_unsafe_math_optimizations:
3231 AssociativeMath = false;
3232 ReciprocalMath = false;
3233 SignedZeros = true;
3234 ApproxFunc = false;
3235 restoreFPContractState();
3236 break;
3237
3238 case options::OPT_Ofast:
3239 // If -Ofast is the optimization level, then -ffast-math should be enabled
3240 if (!OFastEnabled)
3241 continue;
3242 [[fallthrough]];
3243 case options::OPT_ffast_math:
3244 applyFastMath(true);
3245 LastComplexRangeOption = A->getSpelling();
3246 if (A->getOption().getID() == options::OPT_Ofast)
3247 LastFpContractOverrideOption = "-Ofast";
3248 else
3249 LastFpContractOverrideOption = "-ffast-math";
3250 break;
3251 case options::OPT_fno_fast_math:
3252 HonorINFs = true;
3253 HonorNaNs = true;
3254 // Turning on -ffast-math (with either flag) removes the need for
3255 // MathErrno. However, turning *off* -ffast-math merely restores the
3256 // toolchain default (which may be false).
3257 MathErrno = TC.IsMathErrnoDefault();
3258 AssociativeMath = false;
3259 ReciprocalMath = false;
3260 ApproxFunc = false;
3261 SignedZeros = true;
3262 restoreFPContractState();
3263 // If the last specified option related to complex range is not
3264 // -ffast-math or -ffp-model=, emit warning.
3265 if (LastComplexRangeOption != "-ffast-math" &&
3266 LastComplexRangeOption != "-ffp-model=" &&
3267 Range != LangOptions::ComplexRangeKind::CX_Full)
3268 EmitComplexRangeDiag(D, str1: LastComplexRangeOption, str2: "-fno-fast-math");
3269 Range = LangOptions::ComplexRangeKind::CX_None;
3270 LastComplexRangeOption = "";
3271 GccRangeComplexOption = "";
3272 LastFpContractOverrideOption = "";
3273 break;
3274 } // End switch (A->getOption().getID())
3275
3276 // The StrictFPModel local variable is needed to report warnings
3277 // in the way we intend. If -ffp-model=strict has been used, we
3278 // want to report a warning for the next option encountered that
3279 // takes us out of the settings described by fp-model=strict, but
3280 // we don't want to continue issuing warnings for other conflicting
3281 // options after that.
3282 if (StrictFPModel) {
3283 // If -ffp-model=strict has been specified on command line but
3284 // subsequent options conflict then emit warning diagnostic.
3285 if (HonorINFs && HonorNaNs && !AssociativeMath && !ReciprocalMath &&
3286 SignedZeros && TrappingMath && RoundingFPMath && !ApproxFunc &&
3287 FPContract == "off")
3288 // OK: Current Arg doesn't conflict with -ffp-model=strict
3289 ;
3290 else {
3291 StrictFPModel = false;
3292 FPModel = "";
3293 // The warning for -ffp-contract would have been reported by the
3294 // OPT_ffp_contract_EQ handler above. A special check here is needed
3295 // to avoid duplicating the warning.
3296 auto RHS = (A->getNumValues() == 0)
3297 ? A->getSpelling()
3298 : Args.MakeArgString(Str: A->getSpelling() + A->getValue());
3299 if (A->getSpelling() != "-ffp-contract=") {
3300 if (RHS != "-ffp-model=strict")
3301 D.Diag(DiagID: clang::diag::warn_drv_overriding_option)
3302 << "-ffp-model=strict" << RHS;
3303 }
3304 }
3305 }
3306
3307 // If we handled this option claim it
3308 A->claim();
3309 }
3310
3311 if (!HonorINFs)
3312 CmdArgs.push_back(Elt: "-menable-no-infs");
3313
3314 if (!HonorNaNs)
3315 CmdArgs.push_back(Elt: "-menable-no-nans");
3316
3317 if (ApproxFunc)
3318 CmdArgs.push_back(Elt: "-fapprox-func");
3319
3320 if (MathErrno) {
3321 CmdArgs.push_back(Elt: "-fmath-errno");
3322 if (NoMathErrnoWasImpliedByVecLib)
3323 D.Diag(DiagID: clang::diag::warn_drv_math_errno_enabled_after_veclib)
3324 << ArgThatEnabledMathErrnoAfterVecLib->getAsString(Args)
3325 << VecLibArg->getAsString(Args);
3326 }
3327
3328 if (AssociativeMath && ReciprocalMath && !SignedZeros && ApproxFunc &&
3329 !TrappingMath)
3330 CmdArgs.push_back(Elt: "-funsafe-math-optimizations");
3331
3332 if (!SignedZeros)
3333 CmdArgs.push_back(Elt: "-fno-signed-zeros");
3334
3335 if (AssociativeMath && !SignedZeros && !TrappingMath)
3336 CmdArgs.push_back(Elt: "-mreassociate");
3337
3338 if (ReciprocalMath)
3339 CmdArgs.push_back(Elt: "-freciprocal-math");
3340
3341 if (TrappingMath) {
3342 // FP Exception Behavior is also set to strict
3343 assert(FPExceptionBehavior == "strict");
3344 }
3345
3346 // The default is IEEE.
3347 if (DenormalFPMath != llvm::DenormalMode::getIEEE()) {
3348 llvm::SmallString<64> DenormFlag;
3349 llvm::raw_svector_ostream ArgStr(DenormFlag);
3350 ArgStr << "-fdenormal-fp-math=" << DenormalFPMath;
3351 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ArgStr.str()));
3352 }
3353
3354 // Add f32 specific denormal mode flag if it's different.
3355 if (DenormalFP32Math != DenormalFPMath) {
3356 llvm::SmallString<64> DenormFlag;
3357 llvm::raw_svector_ostream ArgStr(DenormFlag);
3358 ArgStr << "-fdenormal-fp-math-f32=" << DenormalFP32Math;
3359 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ArgStr.str()));
3360 }
3361
3362 if (!FPContract.empty())
3363 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-ffp-contract=" + FPContract));
3364
3365 if (RoundingFPMath)
3366 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-frounding-math"));
3367 else
3368 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fno-rounding-math"));
3369
3370 if (!FPExceptionBehavior.empty())
3371 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-ffp-exception-behavior=" +
3372 FPExceptionBehavior));
3373
3374 if (!FPEvalMethod.empty())
3375 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-ffp-eval-method=" + FPEvalMethod));
3376
3377 if (!Float16ExcessPrecision.empty())
3378 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-ffloat16-excess-precision=" +
3379 Float16ExcessPrecision));
3380 if (!BFloat16ExcessPrecision.empty())
3381 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fbfloat16-excess-precision=" +
3382 BFloat16ExcessPrecision));
3383
3384 StringRef Recip = parseMRecipOption(Diags&: D.getDiags(), Args);
3385 if (!Recip.empty())
3386 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mrecip=" + Recip));
3387
3388 // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for the
3389 // individual features enabled by -ffast-math instead of the option itself as
3390 // that's consistent with gcc's behaviour.
3391 if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath && ApproxFunc &&
3392 ReciprocalMath && !SignedZeros && !TrappingMath && !RoundingFPMath)
3393 CmdArgs.push_back(Elt: "-ffast-math");
3394
3395 // Handle __FINITE_MATH_ONLY__ similarly.
3396 // The -ffinite-math-only is added to CmdArgs when !HonorINFs && !HonorNaNs.
3397 // Otherwise process the Xclang arguments to determine if -menable-no-infs and
3398 // -menable-no-nans are set by the user.
3399 bool shouldAddFiniteMathOnly = false;
3400 if (!HonorINFs && !HonorNaNs) {
3401 shouldAddFiniteMathOnly = true;
3402 } else {
3403 bool InfValues = true;
3404 bool NanValues = true;
3405 for (const auto *Arg : Args.filtered(Ids: options::OPT_Xclang)) {
3406 StringRef ArgValue = Arg->getValue();
3407 if (ArgValue == "-menable-no-nans")
3408 NanValues = false;
3409 else if (ArgValue == "-menable-no-infs")
3410 InfValues = false;
3411 }
3412 if (!NanValues && !InfValues)
3413 shouldAddFiniteMathOnly = true;
3414 }
3415 if (shouldAddFiniteMathOnly) {
3416 CmdArgs.push_back(Elt: "-ffinite-math-only");
3417 }
3418 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mfpmath_EQ)) {
3419 CmdArgs.push_back(Elt: "-mfpmath");
3420 CmdArgs.push_back(Elt: A->getValue());
3421 }
3422
3423 // Disable a codegen optimization for floating-point casts.
3424 if (Args.hasFlag(Pos: options::OPT_fno_strict_float_cast_overflow,
3425 Neg: options::OPT_fstrict_float_cast_overflow, Default: false))
3426 CmdArgs.push_back(Elt: "-fno-strict-float-cast-overflow");
3427
3428 if (Range != LangOptions::ComplexRangeKind::CX_None)
3429 ComplexRangeStr = RenderComplexRangeOption(Range);
3430 if (!ComplexRangeStr.empty()) {
3431 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ComplexRangeStr));
3432 if (Args.hasArg(Ids: options::OPT_fcomplex_arithmetic_EQ))
3433 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fcomplex-arithmetic=" +
3434 ComplexRangeKindToStr(Range)));
3435 }
3436 if (Args.hasArg(Ids: options::OPT_fcx_limited_range))
3437 CmdArgs.push_back(Elt: "-fcx-limited-range");
3438 if (Args.hasArg(Ids: options::OPT_fcx_fortran_rules))
3439 CmdArgs.push_back(Elt: "-fcx-fortran-rules");
3440 if (Args.hasArg(Ids: options::OPT_fno_cx_limited_range))
3441 CmdArgs.push_back(Elt: "-fno-cx-limited-range");
3442 if (Args.hasArg(Ids: options::OPT_fno_cx_fortran_rules))
3443 CmdArgs.push_back(Elt: "-fno-cx-fortran-rules");
3444}
3445
3446static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs,
3447 const llvm::Triple &Triple,
3448 const InputInfo &Input) {
3449 // Add default argument set.
3450 if (!Args.hasArg(Ids: options::OPT__analyzer_no_default_checks)) {
3451 CmdArgs.push_back(Elt: "-analyzer-checker=core");
3452 CmdArgs.push_back(Elt: "-analyzer-checker=apiModeling");
3453
3454 if (!Triple.isWindowsMSVCEnvironment()) {
3455 CmdArgs.push_back(Elt: "-analyzer-checker=unix");
3456 } else {
3457 // Enable "unix" checkers that also work on Windows.
3458 CmdArgs.push_back(Elt: "-analyzer-checker=unix.API");
3459 CmdArgs.push_back(Elt: "-analyzer-checker=unix.Malloc");
3460 CmdArgs.push_back(Elt: "-analyzer-checker=unix.MallocSizeof");
3461 CmdArgs.push_back(Elt: "-analyzer-checker=unix.MismatchedDeallocator");
3462 CmdArgs.push_back(Elt: "-analyzer-checker=unix.cstring.BadSizeArg");
3463 CmdArgs.push_back(Elt: "-analyzer-checker=unix.cstring.NullArg");
3464 }
3465
3466 // Disable some unix checkers for PS4/PS5.
3467 if (Triple.isPS()) {
3468 CmdArgs.push_back(Elt: "-analyzer-disable-checker=unix.API");
3469 CmdArgs.push_back(Elt: "-analyzer-disable-checker=unix.Vfork");
3470 }
3471
3472 if (Triple.isOSDarwin()) {
3473 CmdArgs.push_back(Elt: "-analyzer-checker=osx");
3474 CmdArgs.push_back(
3475 Elt: "-analyzer-checker=security.insecureAPI.decodeValueOfObjCType");
3476 }
3477 else if (Triple.isOSFuchsia())
3478 CmdArgs.push_back(Elt: "-analyzer-checker=fuchsia");
3479
3480 CmdArgs.push_back(Elt: "-analyzer-checker=deadcode");
3481
3482 if (types::isCXX(Id: Input.getType()))
3483 CmdArgs.push_back(Elt: "-analyzer-checker=cplusplus");
3484
3485 if (!Triple.isPS()) {
3486 CmdArgs.push_back(Elt: "-analyzer-checker=security.insecureAPI.UncheckedReturn");
3487 CmdArgs.push_back(Elt: "-analyzer-checker=security.insecureAPI.getpw");
3488 CmdArgs.push_back(Elt: "-analyzer-checker=security.insecureAPI.gets");
3489 CmdArgs.push_back(Elt: "-analyzer-checker=security.insecureAPI.mktemp");
3490 CmdArgs.push_back(Elt: "-analyzer-checker=security.insecureAPI.mkstemp");
3491 CmdArgs.push_back(Elt: "-analyzer-checker=security.insecureAPI.vfork");
3492 }
3493
3494 // Default nullability checks.
3495 CmdArgs.push_back(Elt: "-analyzer-checker=nullability.NullPassedToNonnull");
3496 CmdArgs.push_back(Elt: "-analyzer-checker=nullability.NullReturnedFromNonnull");
3497 }
3498
3499 // Set the output format. The default is plist, for (lame) historical reasons.
3500 CmdArgs.push_back(Elt: "-analyzer-output");
3501 if (Arg *A = Args.getLastArg(Ids: options::OPT__analyzer_output))
3502 CmdArgs.push_back(Elt: A->getValue());
3503 else
3504 CmdArgs.push_back(Elt: "plist");
3505
3506 // Disable the presentation of standard compiler warnings when using
3507 // --analyze. We only want to show static analyzer diagnostics or frontend
3508 // errors.
3509 CmdArgs.push_back(Elt: "-w");
3510
3511 // Add -Xanalyzer arguments when running as analyzer.
3512 Args.AddAllArgValues(Output&: CmdArgs, Id0: options::OPT_Xanalyzer);
3513}
3514
3515static bool isValidSymbolName(StringRef S) {
3516 if (S.empty())
3517 return false;
3518
3519 if (std::isdigit(S[0]))
3520 return false;
3521
3522 return llvm::all_of(Range&: S, P: [](char C) { return std::isalnum(C) || C == '_'; });
3523}
3524
3525static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
3526 const ArgList &Args, ArgStringList &CmdArgs,
3527 bool KernelOrKext) {
3528 const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple();
3529
3530 // NVPTX doesn't support stack protectors; from the compiler's perspective, it
3531 // doesn't even have a stack!
3532 if (EffectiveTriple.isNVPTX())
3533 return;
3534
3535 // -stack-protector=0 is default.
3536 LangOptions::StackProtectorMode StackProtectorLevel = LangOptions::SSPOff;
3537 LangOptions::StackProtectorMode DefaultStackProtectorLevel =
3538 TC.GetDefaultStackProtectorLevel(KernelOrKext);
3539
3540 if (Arg *A = Args.getLastArg(Ids: options::OPT_fno_stack_protector,
3541 Ids: options::OPT_fstack_protector_all,
3542 Ids: options::OPT_fstack_protector_strong,
3543 Ids: options::OPT_fstack_protector)) {
3544 if (A->getOption().matches(ID: options::OPT_fstack_protector))
3545 StackProtectorLevel =
3546 std::max<>(a: LangOptions::SSPOn, b: DefaultStackProtectorLevel);
3547 else if (A->getOption().matches(ID: options::OPT_fstack_protector_strong))
3548 StackProtectorLevel = LangOptions::SSPStrong;
3549 else if (A->getOption().matches(ID: options::OPT_fstack_protector_all))
3550 StackProtectorLevel = LangOptions::SSPReq;
3551
3552 if (EffectiveTriple.isBPF() && StackProtectorLevel != LangOptions::SSPOff) {
3553 D.Diag(DiagID: diag::warn_drv_unsupported_option_for_target)
3554 << A->getSpelling() << EffectiveTriple.getTriple();
3555 StackProtectorLevel = DefaultStackProtectorLevel;
3556 }
3557 } else {
3558 StackProtectorLevel = DefaultStackProtectorLevel;
3559 }
3560
3561 if (StackProtectorLevel) {
3562 CmdArgs.push_back(Elt: "-stack-protector");
3563 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(StackProtectorLevel)));
3564 }
3565
3566 // --param ssp-buffer-size=
3567 for (const Arg *A : Args.filtered(Ids: options::OPT__param)) {
3568 StringRef Str(A->getValue());
3569 if (Str.consume_front(Prefix: "ssp-buffer-size=")) {
3570 if (StackProtectorLevel) {
3571 CmdArgs.push_back(Elt: "-stack-protector-buffer-size");
3572 // FIXME: Verify the argument is a valid integer.
3573 CmdArgs.push_back(Elt: Args.MakeArgString(Str));
3574 }
3575 A->claim();
3576 }
3577 }
3578
3579 const std::string &TripleStr = EffectiveTriple.getTriple();
3580 if (Arg *A = Args.getLastArg(Ids: options::OPT_mstack_protector_guard_EQ)) {
3581 StringRef Value = A->getValue();
3582 if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
3583 !EffectiveTriple.isARM() && !EffectiveTriple.isThumb() &&
3584 !EffectiveTriple.isRISCV() && !EffectiveTriple.isPPC())
3585 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
3586 << A->getAsString(Args) << TripleStr;
3587 if ((EffectiveTriple.isX86() || EffectiveTriple.isARM() ||
3588 EffectiveTriple.isThumb()) &&
3589 Value != "tls" && Value != "global") {
3590 D.Diag(DiagID: diag::err_drv_invalid_value_with_suggestion)
3591 << A->getOption().getName() << Value << "tls global";
3592 return;
3593 }
3594 if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) &&
3595 Value == "tls") {
3596 if (!Args.hasArg(Ids: options::OPT_mstack_protector_guard_offset_EQ)) {
3597 D.Diag(DiagID: diag::err_drv_ssp_missing_offset_argument)
3598 << A->getAsString(Args);
3599 return;
3600 }
3601 // Check whether the target subarch supports the hardware TLS register
3602 if (!arm::isHardTPSupported(Triple: EffectiveTriple)) {
3603 D.Diag(DiagID: diag::err_target_unsupported_tp_hard)
3604 << EffectiveTriple.getArchName();
3605 return;
3606 }
3607 // Check whether the user asked for something other than -mtp=cp15
3608 if (Arg *A = Args.getLastArg(Ids: options::OPT_mtp_mode_EQ)) {
3609 StringRef Value = A->getValue();
3610 if (Value != "cp15") {
3611 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
3612 << A->getAsString(Args) << "-mstack-protector-guard=tls";
3613 return;
3614 }
3615 }
3616 CmdArgs.push_back(Elt: "-target-feature");
3617 CmdArgs.push_back(Elt: "+read-tp-tpidruro");
3618 }
3619 if (EffectiveTriple.isAArch64() && Value != "sysreg" && Value != "global") {
3620 D.Diag(DiagID: diag::err_drv_invalid_value_with_suggestion)
3621 << A->getOption().getName() << Value << "sysreg global";
3622 return;
3623 }
3624 if (EffectiveTriple.isRISCV() || EffectiveTriple.isPPC()) {
3625 if (Value != "tls" && Value != "global") {
3626 D.Diag(DiagID: diag::err_drv_invalid_value_with_suggestion)
3627 << A->getOption().getName() << Value << "tls global";
3628 return;
3629 }
3630 if (Value == "tls") {
3631 if (!Args.hasArg(Ids: options::OPT_mstack_protector_guard_offset_EQ)) {
3632 D.Diag(DiagID: diag::err_drv_ssp_missing_offset_argument)
3633 << A->getAsString(Args);
3634 return;
3635 }
3636 }
3637 }
3638 A->render(Args, Output&: CmdArgs);
3639 }
3640
3641 if (Arg *A = Args.getLastArg(Ids: options::OPT_mstack_protector_guard_offset_EQ)) {
3642 StringRef Value = A->getValue();
3643 if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
3644 !EffectiveTriple.isARM() && !EffectiveTriple.isThumb() &&
3645 !EffectiveTriple.isRISCV() && !EffectiveTriple.isPPC())
3646 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
3647 << A->getAsString(Args) << TripleStr;
3648 int Offset;
3649 if (Value.getAsInteger(Radix: 10, Result&: Offset)) {
3650 D.Diag(DiagID: diag::err_drv_invalid_value) << A->getOption().getName() << Value;
3651 return;
3652 }
3653 if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) &&
3654 (Offset < 0 || Offset > 0xfffff)) {
3655 D.Diag(DiagID: diag::err_drv_invalid_int_value)
3656 << A->getOption().getName() << Value;
3657 return;
3658 }
3659 A->render(Args, Output&: CmdArgs);
3660 }
3661
3662 if (Arg *A = Args.getLastArg(Ids: options::OPT_mstack_protector_guard_reg_EQ)) {
3663 StringRef Value = A->getValue();
3664 if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
3665 !EffectiveTriple.isRISCV() && !EffectiveTriple.isPPC())
3666 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
3667 << A->getAsString(Args) << TripleStr;
3668 if (EffectiveTriple.isX86() && (Value != "fs" && Value != "gs")) {
3669 D.Diag(DiagID: diag::err_drv_invalid_value_with_suggestion)
3670 << A->getOption().getName() << Value << "fs gs";
3671 return;
3672 }
3673 if (EffectiveTriple.isAArch64() && Value != "sp_el0") {
3674 D.Diag(DiagID: diag::err_drv_invalid_value) << A->getOption().getName() << Value;
3675 return;
3676 }
3677 if (EffectiveTriple.isRISCV() && Value != "tp") {
3678 D.Diag(DiagID: diag::err_drv_invalid_value_with_suggestion)
3679 << A->getOption().getName() << Value << "tp";
3680 return;
3681 }
3682 if (EffectiveTriple.isPPC64() && Value != "r13") {
3683 D.Diag(DiagID: diag::err_drv_invalid_value_with_suggestion)
3684 << A->getOption().getName() << Value << "r13";
3685 return;
3686 }
3687 if (EffectiveTriple.isPPC32() && Value != "r2") {
3688 D.Diag(DiagID: diag::err_drv_invalid_value_with_suggestion)
3689 << A->getOption().getName() << Value << "r2";
3690 return;
3691 }
3692 A->render(Args, Output&: CmdArgs);
3693 }
3694
3695 if (Arg *A = Args.getLastArg(Ids: options::OPT_mstack_protector_guard_symbol_EQ)) {
3696 StringRef Value = A->getValue();
3697 if (!isValidSymbolName(S: Value)) {
3698 D.Diag(DiagID: diag::err_drv_argument_only_allowed_with)
3699 << A->getOption().getName() << "legal symbol name";
3700 return;
3701 }
3702 A->render(Args, Output&: CmdArgs);
3703 }
3704}
3705
3706static void RenderSCPOptions(const ToolChain &TC, const ArgList &Args,
3707 ArgStringList &CmdArgs) {
3708 const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple();
3709
3710 if (!EffectiveTriple.isOSFreeBSD() && !EffectiveTriple.isOSLinux() &&
3711 !EffectiveTriple.isOSFuchsia())
3712 return;
3713
3714 if (!EffectiveTriple.isX86() && !EffectiveTriple.isSystemZ() &&
3715 !EffectiveTriple.isPPC64() && !EffectiveTriple.isAArch64() &&
3716 !EffectiveTriple.isRISCV())
3717 return;
3718
3719 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fstack_clash_protection,
3720 Neg: options::OPT_fno_stack_clash_protection);
3721}
3722
3723static void RenderTrivialAutoVarInitOptions(const Driver &D,
3724 const ToolChain &TC,
3725 const ArgList &Args,
3726 ArgStringList &CmdArgs) {
3727 auto DefaultTrivialAutoVarInit = TC.GetDefaultTrivialAutoVarInit();
3728 StringRef TrivialAutoVarInit = "";
3729
3730 for (const Arg *A : Args) {
3731 switch (A->getOption().getID()) {
3732 default:
3733 continue;
3734 case options::OPT_ftrivial_auto_var_init: {
3735 A->claim();
3736 StringRef Val = A->getValue();
3737 if (Val == "uninitialized" || Val == "zero" || Val == "pattern")
3738 TrivialAutoVarInit = Val;
3739 else
3740 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
3741 << A->getSpelling() << Val;
3742 break;
3743 }
3744 }
3745 }
3746
3747 if (TrivialAutoVarInit.empty())
3748 switch (DefaultTrivialAutoVarInit) {
3749 case LangOptions::TrivialAutoVarInitKind::Uninitialized:
3750 break;
3751 case LangOptions::TrivialAutoVarInitKind::Pattern:
3752 TrivialAutoVarInit = "pattern";
3753 break;
3754 case LangOptions::TrivialAutoVarInitKind::Zero:
3755 TrivialAutoVarInit = "zero";
3756 break;
3757 }
3758
3759 if (!TrivialAutoVarInit.empty()) {
3760 CmdArgs.push_back(
3761 Elt: Args.MakeArgString(Str: "-ftrivial-auto-var-init=" + TrivialAutoVarInit));
3762 }
3763
3764 if (Arg *A =
3765 Args.getLastArg(Ids: options::OPT_ftrivial_auto_var_init_stop_after)) {
3766 if (!Args.hasArg(Ids: options::OPT_ftrivial_auto_var_init) ||
3767 StringRef(
3768 Args.getLastArg(Ids: options::OPT_ftrivial_auto_var_init)->getValue()) ==
3769 "uninitialized")
3770 D.Diag(DiagID: diag::err_drv_trivial_auto_var_init_stop_after_missing_dependency);
3771 A->claim();
3772 StringRef Val = A->getValue();
3773 if (std::stoi(str: Val.str()) <= 0)
3774 D.Diag(DiagID: diag::err_drv_trivial_auto_var_init_stop_after_invalid_value);
3775 CmdArgs.push_back(
3776 Elt: Args.MakeArgString(Str: "-ftrivial-auto-var-init-stop-after=" + Val));
3777 }
3778
3779 if (Arg *A = Args.getLastArg(Ids: options::OPT_ftrivial_auto_var_init_max_size)) {
3780 if (!Args.hasArg(Ids: options::OPT_ftrivial_auto_var_init) ||
3781 StringRef(
3782 Args.getLastArg(Ids: options::OPT_ftrivial_auto_var_init)->getValue()) ==
3783 "uninitialized")
3784 D.Diag(DiagID: diag::err_drv_trivial_auto_var_init_max_size_missing_dependency);
3785 A->claim();
3786 StringRef Val = A->getValue();
3787 if (std::stoi(str: Val.str()) <= 0)
3788 D.Diag(DiagID: diag::err_drv_trivial_auto_var_init_max_size_invalid_value);
3789 CmdArgs.push_back(
3790 Elt: Args.MakeArgString(Str: "-ftrivial-auto-var-init-max-size=" + Val));
3791 }
3792}
3793
3794static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs,
3795 types::ID InputType) {
3796 // cl-denorms-are-zero is not forwarded. It is translated into a generic flag
3797 // for denormal flushing handling based on the target.
3798 const unsigned ForwardedArguments[] = {
3799 options::OPT_cl_opt_disable,
3800 options::OPT_cl_strict_aliasing,
3801 options::OPT_cl_single_precision_constant,
3802 options::OPT_cl_finite_math_only,
3803 options::OPT_cl_kernel_arg_info,
3804 options::OPT_cl_unsafe_math_optimizations,
3805 options::OPT_cl_fast_relaxed_math,
3806 options::OPT_cl_mad_enable,
3807 options::OPT_cl_no_signed_zeros,
3808 options::OPT_cl_fp32_correctly_rounded_divide_sqrt,
3809 options::OPT_cl_uniform_work_group_size
3810 };
3811
3812 if (Arg *A = Args.getLastArg(Ids: options::OPT_cl_std_EQ)) {
3813 std::string CLStdStr = std::string("-cl-std=") + A->getValue();
3814 CmdArgs.push_back(Elt: Args.MakeArgString(Str: CLStdStr));
3815 } else if (Arg *A = Args.getLastArg(Ids: options::OPT_cl_ext_EQ)) {
3816 std::string CLExtStr = std::string("-cl-ext=") + A->getValue();
3817 CmdArgs.push_back(Elt: Args.MakeArgString(Str: CLExtStr));
3818 }
3819
3820 if (Args.hasArg(Ids: options::OPT_cl_finite_math_only)) {
3821 CmdArgs.push_back(Elt: "-menable-no-infs");
3822 CmdArgs.push_back(Elt: "-menable-no-nans");
3823 }
3824
3825 for (const auto &Arg : ForwardedArguments)
3826 if (const auto *A = Args.getLastArg(Ids: Arg))
3827 CmdArgs.push_back(Elt: Args.MakeArgString(Str: A->getOption().getPrefixedName()));
3828
3829 // Only add the default headers if we are compiling OpenCL sources.
3830 if ((types::isOpenCL(Id: InputType) ||
3831 (Args.hasArg(Ids: options::OPT_cl_std_EQ) && types::isSrcFile(Id: InputType))) &&
3832 !Args.hasArg(Ids: options::OPT_cl_no_stdinc)) {
3833 CmdArgs.push_back(Elt: "-finclude-default-header");
3834 CmdArgs.push_back(Elt: "-fdeclare-opencl-builtins");
3835 }
3836}
3837
3838static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs,
3839 types::ID InputType) {
3840 const unsigned ForwardedArguments[] = {
3841 options::OPT_dxil_validator_version,
3842 options::OPT_res_may_alias,
3843 options::OPT_D,
3844 options::OPT_I,
3845 options::OPT_O,
3846 options::OPT_emit_llvm,
3847 options::OPT_emit_obj,
3848 options::OPT_disable_llvm_passes,
3849 options::OPT_fnative_half_type,
3850 options::OPT_hlsl_entrypoint,
3851 options::OPT_fdx_rootsignature_version};
3852 if (!types::isHLSL(Id: InputType))
3853 return;
3854 for (const auto &Arg : ForwardedArguments)
3855 if (const auto *A = Args.getLastArg(Ids: Arg))
3856 A->renderAsInput(Args, Output&: CmdArgs);
3857 // Add the default headers if dxc_no_stdinc is not set.
3858 if (!Args.hasArg(Ids: options::OPT_dxc_no_stdinc) &&
3859 !Args.hasArg(Ids: options::OPT_nostdinc))
3860 CmdArgs.push_back(Elt: "-finclude-default-header");
3861}
3862
3863static void RenderOpenACCOptions(const Driver &D, const ArgList &Args,
3864 ArgStringList &CmdArgs, types::ID InputType) {
3865 if (!Args.hasArg(Ids: options::OPT_fopenacc))
3866 return;
3867
3868 CmdArgs.push_back(Elt: "-fopenacc");
3869
3870 if (Arg *A = Args.getLastArg(Ids: options::OPT_openacc_macro_override)) {
3871 StringRef Value = A->getValue();
3872 int Version;
3873 if (!Value.getAsInteger(Radix: 10, Result&: Version))
3874 A->renderAsInput(Args, Output&: CmdArgs);
3875 else
3876 D.Diag(DiagID: diag::err_drv_clang_unsupported) << Value;
3877 }
3878}
3879
3880static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T,
3881 const ArgList &Args, ArgStringList &CmdArgs) {
3882 // -fbuiltin is default unless -mkernel is used.
3883 bool UseBuiltins =
3884 Args.hasFlag(Pos: options::OPT_fbuiltin, Neg: options::OPT_fno_builtin,
3885 Default: !Args.hasArg(Ids: options::OPT_mkernel));
3886 if (!UseBuiltins)
3887 CmdArgs.push_back(Elt: "-fno-builtin");
3888
3889 // -ffreestanding implies -fno-builtin.
3890 if (Args.hasArg(Ids: options::OPT_ffreestanding))
3891 UseBuiltins = false;
3892
3893 // Process the -fno-builtin-* options.
3894 for (const Arg *A : Args.filtered(Ids: options::OPT_fno_builtin_)) {
3895 A->claim();
3896
3897 // If -fno-builtin is specified, then there's no need to pass the option to
3898 // the frontend.
3899 if (UseBuiltins)
3900 A->render(Args, Output&: CmdArgs);
3901 }
3902}
3903
3904bool Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) {
3905 if (const char *Str = std::getenv(name: "CLANG_MODULE_CACHE_PATH")) {
3906 Twine Path{Str};
3907 Path.toVector(Out&: Result);
3908 return Path.getSingleStringRef() != "";
3909 }
3910 if (llvm::sys::path::cache_directory(result&: Result)) {
3911 llvm::sys::path::append(path&: Result, a: "clang");
3912 llvm::sys::path::append(path&: Result, a: "ModuleCache");
3913 return true;
3914 }
3915 return false;
3916}
3917
3918llvm::SmallString<256>
3919clang::driver::tools::getCXX20NamedModuleOutputPath(const ArgList &Args,
3920 const char *BaseInput) {
3921 if (Arg *ModuleOutputEQ = Args.getLastArg(Ids: options::OPT_fmodule_output_EQ))
3922 return StringRef(ModuleOutputEQ->getValue());
3923
3924 SmallString<256> OutputPath;
3925 if (Arg *FinalOutput = Args.getLastArg(Ids: options::OPT_o);
3926 FinalOutput && Args.hasArg(Ids: options::OPT_c))
3927 OutputPath = FinalOutput->getValue();
3928 else
3929 OutputPath = BaseInput;
3930
3931 const char *Extension = types::getTypeTempSuffix(Id: types::TY_ModuleFile);
3932 llvm::sys::path::replace_extension(path&: OutputPath, extension: Extension);
3933 return OutputPath;
3934}
3935
3936static bool RenderModulesOptions(Compilation &C, const Driver &D,
3937 const ArgList &Args, const InputInfo &Input,
3938 const InputInfo &Output, bool HaveStd20,
3939 ArgStringList &CmdArgs) {
3940 bool IsCXX = types::isCXX(Id: Input.getType());
3941 bool HaveStdCXXModules = IsCXX && HaveStd20;
3942 bool HaveModules = HaveStdCXXModules;
3943
3944 // -fmodules enables the use of precompiled modules (off by default).
3945 // Users can pass -fno-cxx-modules to turn off modules support for
3946 // C++/Objective-C++ programs.
3947 bool HaveClangModules = false;
3948 if (Args.hasFlag(Pos: options::OPT_fmodules, Neg: options::OPT_fno_modules, Default: false)) {
3949 bool AllowedInCXX = Args.hasFlag(Pos: options::OPT_fcxx_modules,
3950 Neg: options::OPT_fno_cxx_modules, Default: true);
3951 if (AllowedInCXX || !IsCXX) {
3952 CmdArgs.push_back(Elt: "-fmodules");
3953 HaveClangModules = true;
3954 }
3955 }
3956
3957 HaveModules |= HaveClangModules;
3958
3959 // -fmodule-maps enables implicit reading of module map files. By default,
3960 // this is enabled if we are using Clang's flavor of precompiled modules.
3961 if (Args.hasFlag(Pos: options::OPT_fimplicit_module_maps,
3962 Neg: options::OPT_fno_implicit_module_maps, Default: HaveClangModules))
3963 CmdArgs.push_back(Elt: "-fimplicit-module-maps");
3964
3965 // -fmodules-decluse checks that modules used are declared so (off by default)
3966 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fmodules_decluse,
3967 Neg: options::OPT_fno_modules_decluse);
3968
3969 // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that
3970 // all #included headers are part of modules.
3971 if (Args.hasFlag(Pos: options::OPT_fmodules_strict_decluse,
3972 Neg: options::OPT_fno_modules_strict_decluse, Default: false))
3973 CmdArgs.push_back(Elt: "-fmodules-strict-decluse");
3974
3975 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fmodulemap_allow_subdirectory_search,
3976 Neg: options::OPT_fno_modulemap_allow_subdirectory_search);
3977
3978 // -fno-implicit-modules turns off implicitly compiling modules on demand.
3979 bool ImplicitModules = false;
3980 if (!Args.hasFlag(Pos: options::OPT_fimplicit_modules,
3981 Neg: options::OPT_fno_implicit_modules, Default: HaveClangModules)) {
3982 if (HaveModules)
3983 CmdArgs.push_back(Elt: "-fno-implicit-modules");
3984 } else if (HaveModules) {
3985 ImplicitModules = true;
3986 // -fmodule-cache-path specifies where our implicitly-built module files
3987 // should be written.
3988 SmallString<128> Path;
3989 if (Arg *A = Args.getLastArg(Ids: options::OPT_fmodules_cache_path))
3990 Path = A->getValue();
3991
3992 bool HasPath = true;
3993 if (C.isForDiagnostics()) {
3994 // When generating crash reports, we want to emit the modules along with
3995 // the reproduction sources, so we ignore any provided module path.
3996 Path = Output.getFilename();
3997 llvm::sys::path::replace_extension(path&: Path, extension: ".cache");
3998 llvm::sys::path::append(path&: Path, a: "modules");
3999 } else if (Path.empty()) {
4000 // No module path was provided: use the default.
4001 HasPath = Driver::getDefaultModuleCachePath(Result&: Path);
4002 }
4003
4004 // `HasPath` will only be false if getDefaultModuleCachePath() fails.
4005 // That being said, that failure is unlikely and not caching is harmless.
4006 if (HasPath) {
4007 const char Arg[] = "-fmodules-cache-path=";
4008 Path.insert(I: Path.begin(), From: Arg, To: Arg + strlen(s: Arg));
4009 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Path));
4010 }
4011 }
4012
4013 if (HaveModules) {
4014 if (Args.hasFlag(Pos: options::OPT_fprebuilt_implicit_modules,
4015 Neg: options::OPT_fno_prebuilt_implicit_modules, Default: false))
4016 CmdArgs.push_back(Elt: "-fprebuilt-implicit-modules");
4017 if (Args.hasFlag(Pos: options::OPT_fmodules_validate_input_files_content,
4018 Neg: options::OPT_fno_modules_validate_input_files_content,
4019 Default: false))
4020 CmdArgs.push_back(Elt: "-fvalidate-ast-input-files-content");
4021 }
4022
4023 // -fmodule-name specifies the module that is currently being built (or
4024 // used for header checking by -fmodule-maps).
4025 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fmodule_name_EQ);
4026
4027 // -fmodule-map-file can be used to specify files containing module
4028 // definitions.
4029 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fmodule_map_file);
4030
4031 // -fbuiltin-module-map can be used to load the clang
4032 // builtin headers modulemap file.
4033 if (Args.hasArg(Ids: options::OPT_fbuiltin_module_map)) {
4034 SmallString<128> BuiltinModuleMap(D.ResourceDir);
4035 llvm::sys::path::append(path&: BuiltinModuleMap, a: "include");
4036 llvm::sys::path::append(path&: BuiltinModuleMap, a: "module.modulemap");
4037 if (llvm::sys::fs::exists(Path: BuiltinModuleMap))
4038 CmdArgs.push_back(
4039 Elt: Args.MakeArgString(Str: "-fmodule-map-file=" + BuiltinModuleMap));
4040 }
4041
4042 // The -fmodule-file=<name>=<file> form specifies the mapping of module
4043 // names to precompiled module files (the module is loaded only if used).
4044 // The -fmodule-file=<file> form can be used to unconditionally load
4045 // precompiled module files (whether used or not).
4046 if (HaveModules || Input.getType() == clang::driver::types::TY_ModuleFile) {
4047 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fmodule_file);
4048
4049 // -fprebuilt-module-path specifies where to load the prebuilt module files.
4050 for (const Arg *A : Args.filtered(Ids: options::OPT_fprebuilt_module_path)) {
4051 CmdArgs.push_back(Elt: Args.MakeArgString(
4052 Str: std::string("-fprebuilt-module-path=") + A->getValue()));
4053 A->claim();
4054 }
4055 } else
4056 Args.ClaimAllArgs(Id0: options::OPT_fmodule_file);
4057
4058 // When building modules and generating crashdumps, we need to dump a module
4059 // dependency VFS alongside the output.
4060 if (HaveClangModules && C.isForDiagnostics()) {
4061 SmallString<128> VFSDir(Output.getFilename());
4062 llvm::sys::path::replace_extension(path&: VFSDir, extension: ".cache");
4063 // Add the cache directory as a temp so the crash diagnostics pick it up.
4064 C.addTempFile(Name: Args.MakeArgString(Str: VFSDir));
4065
4066 llvm::sys::path::append(path&: VFSDir, a: "vfs");
4067 CmdArgs.push_back(Elt: "-module-dependency-dir");
4068 CmdArgs.push_back(Elt: Args.MakeArgString(Str: VFSDir));
4069 }
4070
4071 if (HaveClangModules)
4072 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fmodules_user_build_path);
4073
4074 // Pass through all -fmodules-ignore-macro arguments.
4075 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fmodules_ignore_macro);
4076 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fmodules_prune_interval);
4077 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fmodules_prune_after);
4078
4079 if (HaveClangModules) {
4080 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fbuild_session_timestamp);
4081
4082 if (Arg *A = Args.getLastArg(Ids: options::OPT_fbuild_session_file)) {
4083 if (Args.hasArg(Ids: options::OPT_fbuild_session_timestamp))
4084 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
4085 << A->getAsString(Args) << "-fbuild-session-timestamp";
4086
4087 llvm::sys::fs::file_status Status;
4088 if (llvm::sys::fs::status(path: A->getValue(), result&: Status))
4089 D.Diag(DiagID: diag::err_drv_no_such_file) << A->getValue();
4090 CmdArgs.push_back(Elt: Args.MakeArgString(
4091 Str: "-fbuild-session-timestamp=" +
4092 Twine((uint64_t)std::chrono::duration_cast<std::chrono::seconds>(
4093 d: Status.getLastModificationTime().time_since_epoch())
4094 .count())));
4095 }
4096
4097 if (Args.getLastArg(
4098 Ids: options::OPT_fmodules_validate_once_per_build_session)) {
4099 if (!Args.getLastArg(Ids: options::OPT_fbuild_session_timestamp,
4100 Ids: options::OPT_fbuild_session_file))
4101 D.Diag(DiagID: diag::err_drv_modules_validate_once_requires_timestamp);
4102
4103 Args.AddLastArg(Output&: CmdArgs,
4104 Ids: options::OPT_fmodules_validate_once_per_build_session);
4105 }
4106
4107 if (Args.hasFlag(Pos: options::OPT_fmodules_validate_system_headers,
4108 Neg: options::OPT_fno_modules_validate_system_headers,
4109 Default: ImplicitModules))
4110 CmdArgs.push_back(Elt: "-fmodules-validate-system-headers");
4111
4112 Args.AddLastArg(Output&: CmdArgs,
4113 Ids: options::OPT_fmodules_disable_diagnostic_validation);
4114 } else {
4115 Args.ClaimAllArgs(Id0: options::OPT_fbuild_session_timestamp);
4116 Args.ClaimAllArgs(Id0: options::OPT_fbuild_session_file);
4117 Args.ClaimAllArgs(Id0: options::OPT_fmodules_validate_once_per_build_session);
4118 Args.ClaimAllArgs(Id0: options::OPT_fmodules_validate_system_headers);
4119 Args.ClaimAllArgs(Id0: options::OPT_fno_modules_validate_system_headers);
4120 Args.ClaimAllArgs(Id0: options::OPT_fmodules_disable_diagnostic_validation);
4121 }
4122
4123 // FIXME: We provisionally don't check ODR violations for decls in the global
4124 // module fragment.
4125 CmdArgs.push_back(Elt: "-fskip-odr-check-in-gmf");
4126
4127 if (Args.hasArg(Ids: options::OPT_modules_reduced_bmi) &&
4128 (Input.getType() == driver::types::TY_CXXModule ||
4129 Input.getType() == driver::types::TY_PP_CXXModule)) {
4130 CmdArgs.push_back(Elt: "-fmodules-reduced-bmi");
4131
4132 if (Args.hasArg(Ids: options::OPT_fmodule_output_EQ))
4133 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fmodule_output_EQ);
4134 else {
4135 if (Args.hasArg(Ids: options::OPT__precompile) &&
4136 (!Args.hasArg(Ids: options::OPT_o) ||
4137 Args.getLastArg(Ids: options::OPT_o)->getValue() ==
4138 getCXX20NamedModuleOutputPath(Args, BaseInput: Input.getBaseInput()))) {
4139 D.Diag(DiagID: diag::err_drv_reduced_module_output_overrided);
4140 }
4141
4142 CmdArgs.push_back(Elt: Args.MakeArgString(
4143 Str: "-fmodule-output=" +
4144 getCXX20NamedModuleOutputPath(Args, BaseInput: Input.getBaseInput())));
4145 }
4146 }
4147
4148 // Noop if we see '-fmodules-reduced-bmi' with other translation
4149 // units than module units. This is more user friendly to allow end uers to
4150 // enable this feature without asking for help from build systems.
4151 Args.ClaimAllArgs(Id0: options::OPT_modules_reduced_bmi);
4152
4153 // We need to include the case the input file is a module file here.
4154 // Since the default compilation model for C++ module interface unit will
4155 // create temporary module file and compile the temporary module file
4156 // to get the object file. Then the `-fmodule-output` flag will be
4157 // brought to the second compilation process. So we have to claim it for
4158 // the case too.
4159 if (Input.getType() == driver::types::TY_CXXModule ||
4160 Input.getType() == driver::types::TY_PP_CXXModule ||
4161 Input.getType() == driver::types::TY_ModuleFile) {
4162 Args.ClaimAllArgs(Id0: options::OPT_fmodule_output);
4163 Args.ClaimAllArgs(Id0: options::OPT_fmodule_output_EQ);
4164 }
4165
4166 if (Args.hasArg(Ids: options::OPT_fmodules_embed_all_files))
4167 CmdArgs.push_back(Elt: "-fmodules-embed-all-files");
4168
4169 return HaveModules;
4170}
4171
4172static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T,
4173 ArgStringList &CmdArgs) {
4174 // -fsigned-char is default.
4175 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fsigned_char,
4176 Ids: options::OPT_fno_signed_char,
4177 Ids: options::OPT_funsigned_char,
4178 Ids: options::OPT_fno_unsigned_char)) {
4179 if (A->getOption().matches(ID: options::OPT_funsigned_char) ||
4180 A->getOption().matches(ID: options::OPT_fno_signed_char)) {
4181 CmdArgs.push_back(Elt: "-fno-signed-char");
4182 }
4183 } else if (!isSignedCharDefault(Triple: T)) {
4184 CmdArgs.push_back(Elt: "-fno-signed-char");
4185 }
4186
4187 // The default depends on the language standard.
4188 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fchar8__t, Ids: options::OPT_fno_char8__t);
4189
4190 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fshort_wchar,
4191 Ids: options::OPT_fno_short_wchar)) {
4192 if (A->getOption().matches(ID: options::OPT_fshort_wchar)) {
4193 CmdArgs.push_back(Elt: "-fwchar-type=short");
4194 CmdArgs.push_back(Elt: "-fno-signed-wchar");
4195 } else {
4196 bool IsARM = T.isARM() || T.isThumb() || T.isAArch64();
4197 CmdArgs.push_back(Elt: "-fwchar-type=int");
4198 if (T.isOSzOS() ||
4199 (IsARM && !(T.isOSWindows() || T.isOSNetBSD() || T.isOSOpenBSD())))
4200 CmdArgs.push_back(Elt: "-fno-signed-wchar");
4201 else
4202 CmdArgs.push_back(Elt: "-fsigned-wchar");
4203 }
4204 } else if (T.isOSzOS())
4205 CmdArgs.push_back(Elt: "-fno-signed-wchar");
4206}
4207
4208static void RenderObjCOptions(const ToolChain &TC, const Driver &D,
4209 const llvm::Triple &T, const ArgList &Args,
4210 ObjCRuntime &Runtime, bool InferCovariantReturns,
4211 const InputInfo &Input, ArgStringList &CmdArgs) {
4212 const llvm::Triple::ArchType Arch = TC.getArch();
4213
4214 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and legacy
4215 // is the default. Except for deployment target of 10.5, next runtime is
4216 // always legacy dispatch and -fno-objc-legacy-dispatch gets ignored silently.
4217 if (Runtime.isNonFragile()) {
4218 if (!Args.hasFlag(Pos: options::OPT_fobjc_legacy_dispatch,
4219 Neg: options::OPT_fno_objc_legacy_dispatch,
4220 Default: Runtime.isLegacyDispatchDefaultForArch(Arch))) {
4221 if (TC.UseObjCMixedDispatch())
4222 CmdArgs.push_back(Elt: "-fobjc-dispatch-method=mixed");
4223 else
4224 CmdArgs.push_back(Elt: "-fobjc-dispatch-method=non-legacy");
4225 }
4226 }
4227
4228 // When ObjectiveC legacy runtime is in effect on MacOSX, turn on the option
4229 // to do Array/Dictionary subscripting by default.
4230 if (Arch == llvm::Triple::x86 && T.isMacOSX() &&
4231 Runtime.getKind() == ObjCRuntime::FragileMacOSX && Runtime.isNeXTFamily())
4232 CmdArgs.push_back(Elt: "-fobjc-subscripting-legacy-runtime");
4233
4234 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
4235 // NOTE: This logic is duplicated in ToolChains.cpp.
4236 if (isObjCAutoRefCount(Args)) {
4237 TC.CheckObjCARC();
4238
4239 CmdArgs.push_back(Elt: "-fobjc-arc");
4240
4241 // FIXME: It seems like this entire block, and several around it should be
4242 // wrapped in isObjC, but for now we just use it here as this is where it
4243 // was being used previously.
4244 if (types::isCXX(Id: Input.getType()) && types::isObjC(Id: Input.getType())) {
4245 if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
4246 CmdArgs.push_back(Elt: "-fobjc-arc-cxxlib=libc++");
4247 else
4248 CmdArgs.push_back(Elt: "-fobjc-arc-cxxlib=libstdc++");
4249 }
4250
4251 // Allow the user to enable full exceptions code emission.
4252 // We default off for Objective-C, on for Objective-C++.
4253 if (Args.hasFlag(Pos: options::OPT_fobjc_arc_exceptions,
4254 Neg: options::OPT_fno_objc_arc_exceptions,
4255 /*Default=*/types::isCXX(Id: Input.getType())))
4256 CmdArgs.push_back(Elt: "-fobjc-arc-exceptions");
4257 }
4258
4259 // Silence warning for full exception code emission options when explicitly
4260 // set to use no ARC.
4261 if (Args.hasArg(Ids: options::OPT_fno_objc_arc)) {
4262 Args.ClaimAllArgs(Id0: options::OPT_fobjc_arc_exceptions);
4263 Args.ClaimAllArgs(Id0: options::OPT_fno_objc_arc_exceptions);
4264 }
4265
4266 // Allow the user to control whether messages can be converted to runtime
4267 // functions.
4268 if (types::isObjC(Id: Input.getType())) {
4269 auto *Arg = Args.getLastArg(
4270 Ids: options::OPT_fobjc_convert_messages_to_runtime_calls,
4271 Ids: options::OPT_fno_objc_convert_messages_to_runtime_calls);
4272 if (Arg &&
4273 Arg->getOption().matches(
4274 ID: options::OPT_fno_objc_convert_messages_to_runtime_calls))
4275 CmdArgs.push_back(Elt: "-fno-objc-convert-messages-to-runtime-calls");
4276 }
4277
4278 // -fobjc-infer-related-result-type is the default, except in the Objective-C
4279 // rewriter.
4280 if (InferCovariantReturns)
4281 CmdArgs.push_back(Elt: "-fno-objc-infer-related-result-type");
4282
4283 // Pass down -fobjc-weak or -fno-objc-weak if present.
4284 if (types::isObjC(Id: Input.getType())) {
4285 auto WeakArg =
4286 Args.getLastArg(Ids: options::OPT_fobjc_weak, Ids: options::OPT_fno_objc_weak);
4287 if (!WeakArg) {
4288 // nothing to do
4289 } else if (!Runtime.allowsWeak()) {
4290 if (WeakArg->getOption().matches(ID: options::OPT_fobjc_weak))
4291 D.Diag(DiagID: diag::err_objc_weak_unsupported);
4292 } else {
4293 WeakArg->render(Args, Output&: CmdArgs);
4294 }
4295 }
4296
4297 if (Args.hasArg(Ids: options::OPT_fobjc_disable_direct_methods_for_testing))
4298 CmdArgs.push_back(Elt: "-fobjc-disable-direct-methods-for-testing");
4299}
4300
4301static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args,
4302 ArgStringList &CmdArgs) {
4303 bool CaretDefault = true;
4304 bool ColumnDefault = true;
4305
4306 if (const Arg *A = Args.getLastArg(Ids: options::OPT__SLASH_diagnostics_classic,
4307 Ids: options::OPT__SLASH_diagnostics_column,
4308 Ids: options::OPT__SLASH_diagnostics_caret)) {
4309 switch (A->getOption().getID()) {
4310 case options::OPT__SLASH_diagnostics_caret:
4311 CaretDefault = true;
4312 ColumnDefault = true;
4313 break;
4314 case options::OPT__SLASH_diagnostics_column:
4315 CaretDefault = false;
4316 ColumnDefault = true;
4317 break;
4318 case options::OPT__SLASH_diagnostics_classic:
4319 CaretDefault = false;
4320 ColumnDefault = false;
4321 break;
4322 }
4323 }
4324
4325 // -fcaret-diagnostics is default.
4326 if (!Args.hasFlag(Pos: options::OPT_fcaret_diagnostics,
4327 Neg: options::OPT_fno_caret_diagnostics, Default: CaretDefault))
4328 CmdArgs.push_back(Elt: "-fno-caret-diagnostics");
4329
4330 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fdiagnostics_fixit_info,
4331 Neg: options::OPT_fno_diagnostics_fixit_info);
4332 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fdiagnostics_show_option,
4333 Neg: options::OPT_fno_diagnostics_show_option);
4334
4335 if (const Arg *A =
4336 Args.getLastArg(Ids: options::OPT_fdiagnostics_show_category_EQ)) {
4337 CmdArgs.push_back(Elt: "-fdiagnostics-show-category");
4338 CmdArgs.push_back(Elt: A->getValue());
4339 }
4340
4341 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fdiagnostics_show_hotness,
4342 Neg: options::OPT_fno_diagnostics_show_hotness);
4343
4344 if (const Arg *A =
4345 Args.getLastArg(Ids: options::OPT_fdiagnostics_hotness_threshold_EQ)) {
4346 std::string Opt =
4347 std::string("-fdiagnostics-hotness-threshold=") + A->getValue();
4348 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Opt));
4349 }
4350
4351 if (const Arg *A =
4352 Args.getLastArg(Ids: options::OPT_fdiagnostics_misexpect_tolerance_EQ)) {
4353 std::string Opt =
4354 std::string("-fdiagnostics-misexpect-tolerance=") + A->getValue();
4355 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Opt));
4356 }
4357
4358 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fdiagnostics_format_EQ)) {
4359 CmdArgs.push_back(Elt: "-fdiagnostics-format");
4360 CmdArgs.push_back(Elt: A->getValue());
4361 if (StringRef(A->getValue()) == "sarif" ||
4362 StringRef(A->getValue()) == "SARIF")
4363 D.Diag(DiagID: diag::warn_drv_sarif_format_unstable);
4364 }
4365
4366 if (const Arg *A = Args.getLastArg(
4367 Ids: options::OPT_fdiagnostics_show_note_include_stack,
4368 Ids: options::OPT_fno_diagnostics_show_note_include_stack)) {
4369 const Option &O = A->getOption();
4370 if (O.matches(ID: options::OPT_fdiagnostics_show_note_include_stack))
4371 CmdArgs.push_back(Elt: "-fdiagnostics-show-note-include-stack");
4372 else
4373 CmdArgs.push_back(Elt: "-fno-diagnostics-show-note-include-stack");
4374 }
4375
4376 handleColorDiagnosticsArgs(D, Args, CmdArgs);
4377
4378 if (Args.hasArg(Ids: options::OPT_fansi_escape_codes))
4379 CmdArgs.push_back(Elt: "-fansi-escape-codes");
4380
4381 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fshow_source_location,
4382 Neg: options::OPT_fno_show_source_location);
4383
4384 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fdiagnostics_show_line_numbers,
4385 Neg: options::OPT_fno_diagnostics_show_line_numbers);
4386
4387 if (Args.hasArg(Ids: options::OPT_fdiagnostics_absolute_paths))
4388 CmdArgs.push_back(Elt: "-fdiagnostics-absolute-paths");
4389
4390 if (!Args.hasFlag(Pos: options::OPT_fshow_column, Neg: options::OPT_fno_show_column,
4391 Default: ColumnDefault))
4392 CmdArgs.push_back(Elt: "-fno-show-column");
4393
4394 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fspell_checking,
4395 Neg: options::OPT_fno_spell_checking);
4396
4397 Args.addLastArg(Output&: CmdArgs, Ids: options::OPT_warning_suppression_mappings_EQ);
4398}
4399
4400DwarfFissionKind tools::getDebugFissionKind(const Driver &D,
4401 const ArgList &Args, Arg *&Arg) {
4402 Arg = Args.getLastArg(Ids: options::OPT_gsplit_dwarf, Ids: options::OPT_gsplit_dwarf_EQ,
4403 Ids: options::OPT_gno_split_dwarf);
4404 if (!Arg || Arg->getOption().matches(ID: options::OPT_gno_split_dwarf))
4405 return DwarfFissionKind::None;
4406
4407 if (Arg->getOption().matches(ID: options::OPT_gsplit_dwarf))
4408 return DwarfFissionKind::Split;
4409
4410 StringRef Value = Arg->getValue();
4411 if (Value == "split")
4412 return DwarfFissionKind::Split;
4413 if (Value == "single")
4414 return DwarfFissionKind::Single;
4415
4416 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
4417 << Arg->getSpelling() << Arg->getValue();
4418 return DwarfFissionKind::None;
4419}
4420
4421static void renderDwarfFormat(const Driver &D, const llvm::Triple &T,
4422 const ArgList &Args, ArgStringList &CmdArgs,
4423 unsigned DwarfVersion) {
4424 auto *DwarfFormatArg =
4425 Args.getLastArg(Ids: options::OPT_gdwarf64, Ids: options::OPT_gdwarf32);
4426 if (!DwarfFormatArg)
4427 return;
4428
4429 if (DwarfFormatArg->getOption().matches(ID: options::OPT_gdwarf64)) {
4430 if (DwarfVersion < 3)
4431 D.Diag(DiagID: diag::err_drv_argument_only_allowed_with)
4432 << DwarfFormatArg->getAsString(Args) << "DWARFv3 or greater";
4433 else if (!T.isArch64Bit())
4434 D.Diag(DiagID: diag::err_drv_argument_only_allowed_with)
4435 << DwarfFormatArg->getAsString(Args) << "64 bit architecture";
4436 else if (!T.isOSBinFormatELF())
4437 D.Diag(DiagID: diag::err_drv_argument_only_allowed_with)
4438 << DwarfFormatArg->getAsString(Args) << "ELF platforms";
4439 }
4440
4441 DwarfFormatArg->render(Args, Output&: CmdArgs);
4442}
4443
4444static void
4445renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T,
4446 const ArgList &Args, bool IRInput, ArgStringList &CmdArgs,
4447 const InputInfo &Output,
4448 llvm::codegenoptions::DebugInfoKind &DebugInfoKind,
4449 DwarfFissionKind &DwarfFission) {
4450 if (Args.hasFlag(Pos: options::OPT_fdebug_info_for_profiling,
4451 Neg: options::OPT_fno_debug_info_for_profiling, Default: false) &&
4452 checkDebugInfoOption(
4453 A: Args.getLastArg(Ids: options::OPT_fdebug_info_for_profiling), Args, D, TC))
4454 CmdArgs.push_back(Elt: "-fdebug-info-for-profiling");
4455
4456 // The 'g' groups options involve a somewhat intricate sequence of decisions
4457 // about what to pass from the driver to the frontend, but by the time they
4458 // reach cc1 they've been factored into three well-defined orthogonal choices:
4459 // * what level of debug info to generate
4460 // * what dwarf version to write
4461 // * what debugger tuning to use
4462 // This avoids having to monkey around further in cc1 other than to disable
4463 // codeview if not running in a Windows environment. Perhaps even that
4464 // decision should be made in the driver as well though.
4465 llvm::DebuggerKind DebuggerTuning = TC.getDefaultDebuggerTuning();
4466
4467 bool SplitDWARFInlining =
4468 Args.hasFlag(Pos: options::OPT_fsplit_dwarf_inlining,
4469 Neg: options::OPT_fno_split_dwarf_inlining, Default: false);
4470
4471 // Normally -gsplit-dwarf is only useful with -gN. For IR input, Clang does
4472 // object file generation and no IR generation, -gN should not be needed. So
4473 // allow -gsplit-dwarf with either -gN or IR input.
4474 if (IRInput || Args.hasArg(Ids: options::OPT_g_Group)) {
4475 Arg *SplitDWARFArg;
4476 DwarfFission = getDebugFissionKind(D, Args, Arg&: SplitDWARFArg);
4477 if (DwarfFission != DwarfFissionKind::None &&
4478 !checkDebugInfoOption(A: SplitDWARFArg, Args, D, TC)) {
4479 DwarfFission = DwarfFissionKind::None;
4480 SplitDWARFInlining = false;
4481 }
4482 }
4483 if (const Arg *A = Args.getLastArg(Ids: options::OPT_g_Group)) {
4484 DebugInfoKind = llvm::codegenoptions::DebugInfoConstructor;
4485
4486 // If the last option explicitly specified a debug-info level, use it.
4487 if (checkDebugInfoOption(A, Args, D, TC) &&
4488 A->getOption().matches(ID: options::OPT_gN_Group)) {
4489 DebugInfoKind = debugLevelToInfoKind(A: *A);
4490 // For -g0 or -gline-tables-only, drop -gsplit-dwarf. This gets a bit more
4491 // complicated if you've disabled inline info in the skeleton CUs
4492 // (SplitDWARFInlining) - then there's value in composing split-dwarf and
4493 // line-tables-only, so let those compose naturally in that case.
4494 if (DebugInfoKind == llvm::codegenoptions::NoDebugInfo ||
4495 DebugInfoKind == llvm::codegenoptions::DebugDirectivesOnly ||
4496 (DebugInfoKind == llvm::codegenoptions::DebugLineTablesOnly &&
4497 SplitDWARFInlining))
4498 DwarfFission = DwarfFissionKind::None;
4499 }
4500 }
4501
4502 // If a debugger tuning argument appeared, remember it.
4503 bool HasDebuggerTuning = false;
4504 if (const Arg *A =
4505 Args.getLastArg(Ids: options::OPT_gTune_Group, Ids: options::OPT_ggdbN_Group)) {
4506 HasDebuggerTuning = true;
4507 if (checkDebugInfoOption(A, Args, D, TC)) {
4508 if (A->getOption().matches(ID: options::OPT_glldb))
4509 DebuggerTuning = llvm::DebuggerKind::LLDB;
4510 else if (A->getOption().matches(ID: options::OPT_gsce))
4511 DebuggerTuning = llvm::DebuggerKind::SCE;
4512 else if (A->getOption().matches(ID: options::OPT_gdbx))
4513 DebuggerTuning = llvm::DebuggerKind::DBX;
4514 else
4515 DebuggerTuning = llvm::DebuggerKind::GDB;
4516 }
4517 }
4518
4519 // If a -gdwarf argument appeared, remember it.
4520 bool EmitDwarf = false;
4521 if (const Arg *A = getDwarfNArg(Args))
4522 EmitDwarf = checkDebugInfoOption(A, Args, D, TC);
4523
4524 bool EmitCodeView = false;
4525 if (const Arg *A = Args.getLastArg(Ids: options::OPT_gcodeview))
4526 EmitCodeView = checkDebugInfoOption(A, Args, D, TC);
4527
4528 // If the user asked for debug info but did not explicitly specify -gcodeview
4529 // or -gdwarf, ask the toolchain for the default format.
4530 if (!EmitCodeView && !EmitDwarf &&
4531 DebugInfoKind != llvm::codegenoptions::NoDebugInfo) {
4532 switch (TC.getDefaultDebugFormat()) {
4533 case llvm::codegenoptions::DIF_CodeView:
4534 EmitCodeView = true;
4535 break;
4536 case llvm::codegenoptions::DIF_DWARF:
4537 EmitDwarf = true;
4538 break;
4539 }
4540 }
4541
4542 unsigned RequestedDWARFVersion = 0; // DWARF version requested by the user
4543 unsigned EffectiveDWARFVersion = 0; // DWARF version TC can generate. It may
4544 // be lower than what the user wanted.
4545 if (EmitDwarf) {
4546 RequestedDWARFVersion = getDwarfVersion(TC, Args);
4547 // Clamp effective DWARF version to the max supported by the toolchain.
4548 EffectiveDWARFVersion =
4549 std::min(a: RequestedDWARFVersion, b: TC.getMaxDwarfVersion());
4550 } else {
4551 Args.ClaimAllArgs(Id0: options::OPT_fdebug_default_version);
4552 }
4553
4554 // -gline-directives-only supported only for the DWARF debug info.
4555 if (RequestedDWARFVersion == 0 &&
4556 DebugInfoKind == llvm::codegenoptions::DebugDirectivesOnly)
4557 DebugInfoKind = llvm::codegenoptions::NoDebugInfo;
4558
4559 // strict DWARF is set to false by default. But for DBX, we need it to be set
4560 // as true by default.
4561 if (const Arg *A = Args.getLastArg(Ids: options::OPT_gstrict_dwarf))
4562 (void)checkDebugInfoOption(A, Args, D, TC);
4563 if (Args.hasFlag(Pos: options::OPT_gstrict_dwarf, Neg: options::OPT_gno_strict_dwarf,
4564 Default: DebuggerTuning == llvm::DebuggerKind::DBX))
4565 CmdArgs.push_back(Elt: "-gstrict-dwarf");
4566
4567 // And we handle flag -grecord-gcc-switches later with DWARFDebugFlags.
4568 Args.ClaimAllArgs(Id0: options::OPT_g_flags_Group);
4569
4570 // Column info is included by default for everything except SCE and
4571 // CodeView if not use sampling PGO. Clang doesn't track end columns, just
4572 // starting columns, which, in theory, is fine for CodeView (and PDB). In
4573 // practice, however, the Microsoft debuggers don't handle missing end columns
4574 // well, and the AIX debugger DBX also doesn't handle the columns well, so
4575 // it's better not to include any column info.
4576 if (const Arg *A = Args.getLastArg(Ids: options::OPT_gcolumn_info))
4577 (void)checkDebugInfoOption(A, Args, D, TC);
4578 if (!Args.hasFlag(Pos: options::OPT_gcolumn_info, Neg: options::OPT_gno_column_info,
4579 Default: !(EmitCodeView && !getLastProfileSampleUseArg(Args)) &&
4580 (DebuggerTuning != llvm::DebuggerKind::SCE &&
4581 DebuggerTuning != llvm::DebuggerKind::DBX)))
4582 CmdArgs.push_back(Elt: "-gno-column-info");
4583
4584 // FIXME: Move backend command line options to the module.
4585 if (Args.hasFlag(Pos: options::OPT_gmodules, Neg: options::OPT_gno_modules, Default: false)) {
4586 // If -gline-tables-only or -gline-directives-only is the last option it
4587 // wins.
4588 if (checkDebugInfoOption(A: Args.getLastArg(Ids: options::OPT_gmodules), Args, D,
4589 TC)) {
4590 if (DebugInfoKind != llvm::codegenoptions::DebugLineTablesOnly &&
4591 DebugInfoKind != llvm::codegenoptions::DebugDirectivesOnly) {
4592 DebugInfoKind = llvm::codegenoptions::DebugInfoConstructor;
4593 CmdArgs.push_back(Elt: "-dwarf-ext-refs");
4594 CmdArgs.push_back(Elt: "-fmodule-format=obj");
4595 }
4596 }
4597 }
4598
4599 if (T.isOSBinFormatELF() && SplitDWARFInlining)
4600 CmdArgs.push_back(Elt: "-fsplit-dwarf-inlining");
4601
4602 // After we've dealt with all combinations of things that could
4603 // make DebugInfoKind be other than None or DebugLineTablesOnly,
4604 // figure out if we need to "upgrade" it to standalone debug info.
4605 // We parse these two '-f' options whether or not they will be used,
4606 // to claim them even if you wrote "-fstandalone-debug -gline-tables-only"
4607 bool NeedFullDebug = Args.hasFlag(
4608 Pos: options::OPT_fstandalone_debug, Neg: options::OPT_fno_standalone_debug,
4609 Default: DebuggerTuning == llvm::DebuggerKind::LLDB ||
4610 TC.GetDefaultStandaloneDebug());
4611 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fstandalone_debug))
4612 (void)checkDebugInfoOption(A, Args, D, TC);
4613
4614 if (DebugInfoKind == llvm::codegenoptions::LimitedDebugInfo ||
4615 DebugInfoKind == llvm::codegenoptions::DebugInfoConstructor) {
4616 if (Args.hasFlag(Pos: options::OPT_fno_eliminate_unused_debug_types,
4617 Neg: options::OPT_feliminate_unused_debug_types, Default: false))
4618 DebugInfoKind = llvm::codegenoptions::UnusedTypeInfo;
4619 else if (NeedFullDebug)
4620 DebugInfoKind = llvm::codegenoptions::FullDebugInfo;
4621 }
4622
4623 if (Args.hasFlag(Pos: options::OPT_gembed_source, Neg: options::OPT_gno_embed_source,
4624 Default: false)) {
4625 // Source embedding is a vendor extension to DWARF v5. By now we have
4626 // checked if a DWARF version was stated explicitly, and have otherwise
4627 // fallen back to the target default, so if this is still not at least 5
4628 // we emit an error.
4629 const Arg *A = Args.getLastArg(Ids: options::OPT_gembed_source);
4630 if (RequestedDWARFVersion < 5)
4631 D.Diag(DiagID: diag::err_drv_argument_only_allowed_with)
4632 << A->getAsString(Args) << "-gdwarf-5";
4633 else if (EffectiveDWARFVersion < 5)
4634 // The toolchain has reduced allowed dwarf version, so we can't enable
4635 // -gembed-source.
4636 D.Diag(DiagID: diag::warn_drv_dwarf_version_limited_by_target)
4637 << A->getAsString(Args) << TC.getTripleString() << 5
4638 << EffectiveDWARFVersion;
4639 else if (checkDebugInfoOption(A, Args, D, TC))
4640 CmdArgs.push_back(Elt: "-gembed-source");
4641 }
4642
4643 if (Args.hasFlag(Pos: options::OPT_gkey_instructions,
4644 Neg: options::OPT_gno_key_instructions, Default: false))
4645 CmdArgs.push_back(Elt: "-gkey-instructions");
4646
4647 if (EmitCodeView) {
4648 CmdArgs.push_back(Elt: "-gcodeview");
4649
4650 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_gcodeview_ghash,
4651 Neg: options::OPT_gno_codeview_ghash);
4652
4653 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_gcodeview_command_line,
4654 Neg: options::OPT_gno_codeview_command_line);
4655 }
4656
4657 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_ginline_line_tables,
4658 Neg: options::OPT_gno_inline_line_tables);
4659
4660 // When emitting remarks, we need at least debug lines in the output.
4661 if (willEmitRemarks(Args) &&
4662 DebugInfoKind <= llvm::codegenoptions::DebugDirectivesOnly)
4663 DebugInfoKind = llvm::codegenoptions::DebugLineTablesOnly;
4664
4665 // Adjust the debug info kind for the given toolchain.
4666 TC.adjustDebugInfoKind(DebugInfoKind, Args);
4667
4668 // On AIX, the debugger tuning option can be omitted if it is not explicitly
4669 // set.
4670 RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion: EffectiveDWARFVersion,
4671 DebuggerTuning: T.isOSAIX() && !HasDebuggerTuning
4672 ? llvm::DebuggerKind::Default
4673 : DebuggerTuning);
4674
4675 // -fdebug-macro turns on macro debug info generation.
4676 if (Args.hasFlag(Pos: options::OPT_fdebug_macro, Neg: options::OPT_fno_debug_macro,
4677 Default: false))
4678 if (checkDebugInfoOption(A: Args.getLastArg(Ids: options::OPT_fdebug_macro), Args,
4679 D, TC))
4680 CmdArgs.push_back(Elt: "-debug-info-macro");
4681
4682 // -ggnu-pubnames turns on gnu style pubnames in the backend.
4683 const auto *PubnamesArg =
4684 Args.getLastArg(Ids: options::OPT_ggnu_pubnames, Ids: options::OPT_gno_gnu_pubnames,
4685 Ids: options::OPT_gpubnames, Ids: options::OPT_gno_pubnames);
4686 if (DwarfFission != DwarfFissionKind::None ||
4687 (PubnamesArg && checkDebugInfoOption(A: PubnamesArg, Args, D, TC))) {
4688 const bool OptionSet =
4689 (PubnamesArg &&
4690 (PubnamesArg->getOption().matches(ID: options::OPT_gpubnames) ||
4691 PubnamesArg->getOption().matches(ID: options::OPT_ggnu_pubnames)));
4692 if ((DebuggerTuning != llvm::DebuggerKind::LLDB || OptionSet) &&
4693 (!PubnamesArg ||
4694 (!PubnamesArg->getOption().matches(ID: options::OPT_gno_gnu_pubnames) &&
4695 !PubnamesArg->getOption().matches(ID: options::OPT_gno_pubnames))))
4696 CmdArgs.push_back(Elt: PubnamesArg && PubnamesArg->getOption().matches(
4697 ID: options::OPT_gpubnames)
4698 ? "-gpubnames"
4699 : "-ggnu-pubnames");
4700 }
4701 const auto *SimpleTemplateNamesArg =
4702 Args.getLastArg(Ids: options::OPT_gsimple_template_names,
4703 Ids: options::OPT_gno_simple_template_names);
4704 bool ForwardTemplateParams = DebuggerTuning == llvm::DebuggerKind::SCE;
4705 if (SimpleTemplateNamesArg &&
4706 checkDebugInfoOption(A: SimpleTemplateNamesArg, Args, D, TC)) {
4707 const auto &Opt = SimpleTemplateNamesArg->getOption();
4708 if (Opt.matches(ID: options::OPT_gsimple_template_names)) {
4709 ForwardTemplateParams = true;
4710 CmdArgs.push_back(Elt: "-gsimple-template-names=simple");
4711 }
4712 }
4713
4714 // Emit DW_TAG_template_alias for template aliases? True by default for SCE.
4715 bool UseDebugTemplateAlias =
4716 DebuggerTuning == llvm::DebuggerKind::SCE && RequestedDWARFVersion >= 4;
4717 if (const auto *DebugTemplateAlias = Args.getLastArg(
4718 Ids: options::OPT_gtemplate_alias, Ids: options::OPT_gno_template_alias)) {
4719 // DW_TAG_template_alias is only supported from DWARFv5 but if a user
4720 // asks for it we should let them have it (if the target supports it).
4721 if (checkDebugInfoOption(A: DebugTemplateAlias, Args, D, TC)) {
4722 const auto &Opt = DebugTemplateAlias->getOption();
4723 UseDebugTemplateAlias = Opt.matches(ID: options::OPT_gtemplate_alias);
4724 }
4725 }
4726 if (UseDebugTemplateAlias)
4727 CmdArgs.push_back(Elt: "-gtemplate-alias");
4728
4729 if (const Arg *A = Args.getLastArg(Ids: options::OPT_gsrc_hash_EQ)) {
4730 StringRef v = A->getValue();
4731 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-gsrc-hash=" + v));
4732 }
4733
4734 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fdebug_ranges_base_address,
4735 Neg: options::OPT_fno_debug_ranges_base_address);
4736
4737 // -gdwarf-aranges turns on the emission of the aranges section in the
4738 // backend.
4739 if (const Arg *A = Args.getLastArg(Ids: options::OPT_gdwarf_aranges);
4740 A && checkDebugInfoOption(A, Args, D, TC)) {
4741 CmdArgs.push_back(Elt: "-mllvm");
4742 CmdArgs.push_back(Elt: "-generate-arange-section");
4743 }
4744
4745 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fforce_dwarf_frame,
4746 Neg: options::OPT_fno_force_dwarf_frame);
4747
4748 bool EnableTypeUnits = false;
4749 if (Args.hasFlag(Pos: options::OPT_fdebug_types_section,
4750 Neg: options::OPT_fno_debug_types_section, Default: false)) {
4751 if (!(T.isOSBinFormatELF() || T.isOSBinFormatWasm())) {
4752 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
4753 << Args.getLastArg(Ids: options::OPT_fdebug_types_section)
4754 ->getAsString(Args)
4755 << T.getTriple();
4756 } else if (checkDebugInfoOption(
4757 A: Args.getLastArg(Ids: options::OPT_fdebug_types_section), Args, D,
4758 TC)) {
4759 EnableTypeUnits = true;
4760 CmdArgs.push_back(Elt: "-mllvm");
4761 CmdArgs.push_back(Elt: "-generate-type-units");
4762 }
4763 }
4764
4765 if (const Arg *A =
4766 Args.getLastArg(Ids: options::OPT_gomit_unreferenced_methods,
4767 Ids: options::OPT_gno_omit_unreferenced_methods))
4768 (void)checkDebugInfoOption(A, Args, D, TC);
4769 if (Args.hasFlag(Pos: options::OPT_gomit_unreferenced_methods,
4770 Neg: options::OPT_gno_omit_unreferenced_methods, Default: false) &&
4771 (DebugInfoKind == llvm::codegenoptions::DebugInfoConstructor ||
4772 DebugInfoKind == llvm::codegenoptions::LimitedDebugInfo) &&
4773 !EnableTypeUnits) {
4774 CmdArgs.push_back(Elt: "-gomit-unreferenced-methods");
4775 }
4776
4777 // To avoid join/split of directory+filename, the integrated assembler prefers
4778 // the directory form of .file on all DWARF versions. GNU as doesn't allow the
4779 // form before DWARF v5.
4780 if (!Args.hasFlag(Pos: options::OPT_fdwarf_directory_asm,
4781 Neg: options::OPT_fno_dwarf_directory_asm,
4782 Default: TC.useIntegratedAs() || EffectiveDWARFVersion >= 5))
4783 CmdArgs.push_back(Elt: "-fno-dwarf-directory-asm");
4784
4785 // Decide how to render forward declarations of template instantiations.
4786 // SCE wants full descriptions, others just get them in the name.
4787 if (ForwardTemplateParams)
4788 CmdArgs.push_back(Elt: "-debug-forward-template-params");
4789
4790 // Do we need to explicitly import anonymous namespaces into the parent
4791 // scope?
4792 if (DebuggerTuning == llvm::DebuggerKind::SCE)
4793 CmdArgs.push_back(Elt: "-dwarf-explicit-import");
4794
4795 renderDwarfFormat(D, T, Args, CmdArgs, DwarfVersion: EffectiveDWARFVersion);
4796 RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC);
4797
4798 // This controls whether or not we perform JustMyCode instrumentation.
4799 if (Args.hasFlag(Pos: options::OPT_fjmc, Neg: options::OPT_fno_jmc, Default: false)) {
4800 if (TC.getTriple().isOSBinFormatELF() ||
4801 TC.getTriple().isWindowsMSVCEnvironment()) {
4802 if (DebugInfoKind >= llvm::codegenoptions::DebugInfoConstructor)
4803 CmdArgs.push_back(Elt: "-fjmc");
4804 else if (D.IsCLMode())
4805 D.Diag(DiagID: clang::diag::warn_drv_jmc_requires_debuginfo) << "/JMC"
4806 << "'/Zi', '/Z7'";
4807 else
4808 D.Diag(DiagID: clang::diag::warn_drv_jmc_requires_debuginfo) << "-fjmc"
4809 << "-g";
4810 } else {
4811 D.Diag(DiagID: clang::diag::warn_drv_fjmc_for_elf_only);
4812 }
4813 }
4814
4815 // Add in -fdebug-compilation-dir if necessary.
4816 const char *DebugCompilationDir =
4817 addDebugCompDirArg(Args, CmdArgs, VFS: D.getVFS());
4818
4819 addDebugPrefixMapArg(D, TC, Args, CmdArgs);
4820
4821 // Add the output path to the object file for CodeView debug infos.
4822 if (EmitCodeView && Output.isFilename())
4823 addDebugObjectName(Args, CmdArgs, DebugCompilationDir,
4824 OutputFileName: Output.getFilename());
4825}
4826
4827static void ProcessVSRuntimeLibrary(const ToolChain &TC, const ArgList &Args,
4828 ArgStringList &CmdArgs) {
4829 unsigned RTOptionID = options::OPT__SLASH_MT;
4830
4831 if (Args.hasArg(Ids: options::OPT__SLASH_LDd))
4832 // The /LDd option implies /MTd. The dependent lib part can be overridden,
4833 // but defining _DEBUG is sticky.
4834 RTOptionID = options::OPT__SLASH_MTd;
4835
4836 if (Arg *A = Args.getLastArg(Ids: options::OPT__SLASH_M_Group))
4837 RTOptionID = A->getOption().getID();
4838
4839 if (Arg *A = Args.getLastArg(Ids: options::OPT_fms_runtime_lib_EQ)) {
4840 RTOptionID = llvm::StringSwitch<unsigned>(A->getValue())
4841 .Case(S: "static", Value: options::OPT__SLASH_MT)
4842 .Case(S: "static_dbg", Value: options::OPT__SLASH_MTd)
4843 .Case(S: "dll", Value: options::OPT__SLASH_MD)
4844 .Case(S: "dll_dbg", Value: options::OPT__SLASH_MDd)
4845 .Default(Value: options::OPT__SLASH_MT);
4846 }
4847
4848 StringRef FlagForCRT;
4849 switch (RTOptionID) {
4850 case options::OPT__SLASH_MD:
4851 if (Args.hasArg(Ids: options::OPT__SLASH_LDd))
4852 CmdArgs.push_back(Elt: "-D_DEBUG");
4853 CmdArgs.push_back(Elt: "-D_MT");
4854 CmdArgs.push_back(Elt: "-D_DLL");
4855 FlagForCRT = "--dependent-lib=msvcrt";
4856 break;
4857 case options::OPT__SLASH_MDd:
4858 CmdArgs.push_back(Elt: "-D_DEBUG");
4859 CmdArgs.push_back(Elt: "-D_MT");
4860 CmdArgs.push_back(Elt: "-D_DLL");
4861 FlagForCRT = "--dependent-lib=msvcrtd";
4862 break;
4863 case options::OPT__SLASH_MT:
4864 if (Args.hasArg(Ids: options::OPT__SLASH_LDd))
4865 CmdArgs.push_back(Elt: "-D_DEBUG");
4866 CmdArgs.push_back(Elt: "-D_MT");
4867 CmdArgs.push_back(Elt: "-flto-visibility-public-std");
4868 FlagForCRT = "--dependent-lib=libcmt";
4869 break;
4870 case options::OPT__SLASH_MTd:
4871 CmdArgs.push_back(Elt: "-D_DEBUG");
4872 CmdArgs.push_back(Elt: "-D_MT");
4873 CmdArgs.push_back(Elt: "-flto-visibility-public-std");
4874 FlagForCRT = "--dependent-lib=libcmtd";
4875 break;
4876 default:
4877 llvm_unreachable("Unexpected option ID.");
4878 }
4879
4880 if (Args.hasArg(Ids: options::OPT_fms_omit_default_lib)) {
4881 CmdArgs.push_back(Elt: "-D_VC_NODEFAULTLIB");
4882 } else {
4883 CmdArgs.push_back(Elt: FlagForCRT.data());
4884
4885 // This provides POSIX compatibility (maps 'open' to '_open'), which most
4886 // users want. The /Za flag to cl.exe turns this off, but it's not
4887 // implemented in clang.
4888 CmdArgs.push_back(Elt: "--dependent-lib=oldnames");
4889 }
4890
4891 // All Arm64EC object files implicitly add softintrin.lib. This is necessary
4892 // even if the file doesn't actually refer to any of the routines because
4893 // the CRT itself has incomplete dependency markings.
4894 if (TC.getTriple().isWindowsArm64EC())
4895 CmdArgs.push_back(Elt: "--dependent-lib=softintrin");
4896}
4897
4898void Clang::ConstructJob(Compilation &C, const JobAction &JA,
4899 const InputInfo &Output, const InputInfoList &Inputs,
4900 const ArgList &Args, const char *LinkingOutput) const {
4901 const auto &TC = getToolChain();
4902 const llvm::Triple &RawTriple = TC.getTriple();
4903 const llvm::Triple &Triple = TC.getEffectiveTriple();
4904 const std::string &TripleStr = Triple.getTriple();
4905
4906 bool KernelOrKext =
4907 Args.hasArg(Ids: options::OPT_mkernel, Ids: options::OPT_fapple_kext);
4908 const Driver &D = TC.getDriver();
4909 ArgStringList CmdArgs;
4910
4911 assert(Inputs.size() >= 1 && "Must have at least one input.");
4912 // CUDA/HIP compilation may have multiple inputs (source file + results of
4913 // device-side compilations). OpenMP device jobs also take the host IR as a
4914 // second input. Module precompilation accepts a list of header files to
4915 // include as part of the module. API extraction accepts a list of header
4916 // files whose API information is emitted in the output. All other jobs are
4917 // expected to have exactly one input. SYCL compilation only expects a
4918 // single input.
4919 bool IsCuda = JA.isOffloading(OKind: Action::OFK_Cuda);
4920 bool IsCudaDevice = JA.isDeviceOffloading(OKind: Action::OFK_Cuda);
4921 bool IsHIP = JA.isOffloading(OKind: Action::OFK_HIP);
4922 bool IsHIPDevice = JA.isDeviceOffloading(OKind: Action::OFK_HIP);
4923 bool IsSYCL = JA.isOffloading(OKind: Action::OFK_SYCL);
4924 bool IsSYCLDevice = JA.isDeviceOffloading(OKind: Action::OFK_SYCL);
4925 bool IsOpenMPDevice = JA.isDeviceOffloading(OKind: Action::OFK_OpenMP);
4926 bool IsExtractAPI = isa<ExtractAPIJobAction>(Val: JA);
4927 bool IsDeviceOffloadAction = !(JA.isDeviceOffloading(OKind: Action::OFK_None) ||
4928 JA.isDeviceOffloading(OKind: Action::OFK_Host));
4929 bool IsHostOffloadingAction =
4930 JA.isHostOffloading(OKind: Action::OFK_OpenMP) ||
4931 JA.isHostOffloading(OKind: Action::OFK_SYCL) ||
4932 (JA.isHostOffloading(OKind: C.getActiveOffloadKinds()) &&
4933 Args.hasFlag(Pos: options::OPT_offload_new_driver,
4934 Neg: options::OPT_no_offload_new_driver,
4935 Default: C.isOffloadingHostKind(Kind: Action::OFK_Cuda)));
4936
4937 bool IsRDCMode =
4938 Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, Default: false);
4939
4940 auto LTOMode = IsDeviceOffloadAction ? D.getOffloadLTOMode() : D.getLTOMode();
4941 bool IsUsingLTO = LTOMode != LTOK_None;
4942
4943 // Extract API doesn't have a main input file, so invent a fake one as a
4944 // placeholder.
4945 InputInfo ExtractAPIPlaceholderInput(Inputs[0].getType(), "extract-api",
4946 "extract-api");
4947
4948 const InputInfo &Input =
4949 IsExtractAPI ? ExtractAPIPlaceholderInput : Inputs[0];
4950
4951 InputInfoList ExtractAPIInputs;
4952 InputInfoList HostOffloadingInputs;
4953 const InputInfo *CudaDeviceInput = nullptr;
4954 const InputInfo *OpenMPDeviceInput = nullptr;
4955 for (const InputInfo &I : Inputs) {
4956 if (&I == &Input || I.getType() == types::TY_Nothing) {
4957 // This is the primary input or contains nothing.
4958 } else if (IsExtractAPI) {
4959 auto ExpectedInputType = ExtractAPIPlaceholderInput.getType();
4960 if (I.getType() != ExpectedInputType) {
4961 D.Diag(DiagID: diag::err_drv_extract_api_wrong_kind)
4962 << I.getFilename() << types::getTypeName(Id: I.getType())
4963 << types::getTypeName(Id: ExpectedInputType);
4964 }
4965 ExtractAPIInputs.push_back(Elt: I);
4966 } else if (IsHostOffloadingAction) {
4967 HostOffloadingInputs.push_back(Elt: I);
4968 } else if ((IsCuda || IsHIP) && !CudaDeviceInput) {
4969 CudaDeviceInput = &I;
4970 } else if (IsOpenMPDevice && !OpenMPDeviceInput) {
4971 OpenMPDeviceInput = &I;
4972 } else {
4973 llvm_unreachable("unexpectedly given multiple inputs");
4974 }
4975 }
4976
4977 const llvm::Triple *AuxTriple =
4978 (IsCuda || IsHIP) ? TC.getAuxTriple() : nullptr;
4979 bool IsWindowsMSVC = RawTriple.isWindowsMSVCEnvironment();
4980 bool IsUEFI = RawTriple.isUEFI();
4981 bool IsIAMCU = RawTriple.isOSIAMCU();
4982
4983 // Adjust IsWindowsXYZ for CUDA/HIP/SYCL compilations. Even when compiling in
4984 // device mode (i.e., getToolchain().getTriple() is NVPTX/AMDGCN, not
4985 // Windows), we need to pass Windows-specific flags to cc1.
4986 if (IsCuda || IsHIP || IsSYCL)
4987 IsWindowsMSVC |= AuxTriple && AuxTriple->isWindowsMSVCEnvironment();
4988
4989 // C++ is not supported for IAMCU.
4990 if (IsIAMCU && types::isCXX(Id: Input.getType()))
4991 D.Diag(DiagID: diag::err_drv_clang_unsupported) << "C++ for IAMCU";
4992
4993 // Invoke ourselves in -cc1 mode.
4994 //
4995 // FIXME: Implement custom jobs for internal actions.
4996 CmdArgs.push_back(Elt: "-cc1");
4997
4998 // Add the "effective" target triple.
4999 CmdArgs.push_back(Elt: "-triple");
5000 CmdArgs.push_back(Elt: Args.MakeArgString(Str: TripleStr));
5001
5002 if (const Arg *MJ = Args.getLastArg(Ids: options::OPT_MJ)) {
5003 DumpCompilationDatabase(C, Filename: MJ->getValue(), Target: TripleStr, Output, Input, Args);
5004 Args.ClaimAllArgs(Id0: options::OPT_MJ);
5005 } else if (const Arg *GenCDBFragment =
5006 Args.getLastArg(Ids: options::OPT_gen_cdb_fragment_path)) {
5007 DumpCompilationDatabaseFragmentToDir(Dir: GenCDBFragment->getValue(), C,
5008 Target: TripleStr, Output, Input, Args);
5009 Args.ClaimAllArgs(Id0: options::OPT_gen_cdb_fragment_path);
5010 }
5011
5012 if (IsCuda || IsHIP) {
5013 // We have to pass the triple of the host if compiling for a CUDA/HIP device
5014 // and vice-versa.
5015 std::string NormalizedTriple;
5016 if (JA.isDeviceOffloading(OKind: Action::OFK_Cuda) ||
5017 JA.isDeviceOffloading(OKind: Action::OFK_HIP))
5018 NormalizedTriple = C.getSingleOffloadToolChain<Action::OFK_Host>()
5019 ->getTriple()
5020 .normalize();
5021 else {
5022 // Host-side compilation.
5023 NormalizedTriple =
5024 (IsCuda ? C.getSingleOffloadToolChain<Action::OFK_Cuda>()
5025 : C.getSingleOffloadToolChain<Action::OFK_HIP>())
5026 ->getTriple()
5027 .normalize();
5028 if (IsCuda) {
5029 // We need to figure out which CUDA version we're compiling for, as that
5030 // determines how we load and launch GPU kernels.
5031 auto *CTC = static_cast<const toolchains::CudaToolChain *>(
5032 C.getSingleOffloadToolChain<Action::OFK_Cuda>());
5033 assert(CTC && "Expected valid CUDA Toolchain.");
5034 if (CTC && CTC->CudaInstallation.version() != CudaVersion::UNKNOWN)
5035 CmdArgs.push_back(Elt: Args.MakeArgString(
5036 Str: Twine("-target-sdk-version=") +
5037 CudaVersionToString(V: CTC->CudaInstallation.version())));
5038 // Unsized function arguments used for variadics were introduced in
5039 // CUDA-9.0. We still do not support generating code that actually uses
5040 // variadic arguments yet, but we do need to allow parsing them as
5041 // recent CUDA headers rely on that.
5042 // https://github.com/llvm/llvm-project/issues/58410
5043 if (CTC->CudaInstallation.version() >= CudaVersion::CUDA_90)
5044 CmdArgs.push_back(Elt: "-fcuda-allow-variadic-functions");
5045 }
5046 }
5047 CmdArgs.push_back(Elt: "-aux-triple");
5048 CmdArgs.push_back(Elt: Args.MakeArgString(Str: NormalizedTriple));
5049
5050 if (JA.isDeviceOffloading(OKind: Action::OFK_HIP) &&
5051 (getToolChain().getTriple().isAMDGPU() ||
5052 (getToolChain().getTriple().isSPIRV() &&
5053 getToolChain().getTriple().getVendor() == llvm::Triple::AMD))) {
5054 // Device side compilation printf
5055 if (Args.getLastArg(Ids: options::OPT_mprintf_kind_EQ)) {
5056 CmdArgs.push_back(Elt: Args.MakeArgString(
5057 Str: "-mprintf-kind=" +
5058 Args.getLastArgValue(Id: options::OPT_mprintf_kind_EQ)));
5059 // Force compiler error on invalid conversion specifiers
5060 CmdArgs.push_back(
5061 Elt: Args.MakeArgString(Str: "-Werror=format-invalid-specifier"));
5062 }
5063 }
5064 }
5065
5066 // Optimization level for CodeGen.
5067 if (const Arg *A = Args.getLastArg(Ids: options::OPT_O_Group)) {
5068 if (A->getOption().matches(ID: options::OPT_O4)) {
5069 CmdArgs.push_back(Elt: "-O3");
5070 D.Diag(DiagID: diag::warn_O4_is_O3);
5071 } else {
5072 A->render(Args, Output&: CmdArgs);
5073 }
5074 }
5075
5076 // Unconditionally claim the printf option now to avoid unused diagnostic.
5077 if (const Arg *PF = Args.getLastArg(Ids: options::OPT_mprintf_kind_EQ))
5078 PF->claim();
5079
5080 if (IsSYCL) {
5081 if (IsSYCLDevice) {
5082 // Host triple is needed when doing SYCL device compilations.
5083 llvm::Triple AuxT = C.getDefaultToolChain().getTriple();
5084 std::string NormalizedTriple = AuxT.normalize();
5085 CmdArgs.push_back(Elt: "-aux-triple");
5086 CmdArgs.push_back(Elt: Args.MakeArgString(Str: NormalizedTriple));
5087
5088 // We want to compile sycl kernels.
5089 CmdArgs.push_back(Elt: "-fsycl-is-device");
5090
5091 // Set O2 optimization level by default
5092 if (!Args.getLastArg(Ids: options::OPT_O_Group))
5093 CmdArgs.push_back(Elt: "-O2");
5094 } else {
5095 // Add any options that are needed specific to SYCL offload while
5096 // performing the host side compilation.
5097
5098 // Let the front-end host compilation flow know about SYCL offload
5099 // compilation.
5100 CmdArgs.push_back(Elt: "-fsycl-is-host");
5101 }
5102
5103 // Set options for both host and device.
5104 Arg *SYCLStdArg = Args.getLastArg(Ids: options::OPT_sycl_std_EQ);
5105 if (SYCLStdArg) {
5106 SYCLStdArg->render(Args, Output&: CmdArgs);
5107 } else {
5108 // Ensure the default version in SYCL mode is 2020.
5109 CmdArgs.push_back(Elt: "-sycl-std=2020");
5110 }
5111 }
5112
5113 if (Args.hasArg(Ids: options::OPT_fclangir))
5114 CmdArgs.push_back(Elt: "-fclangir");
5115
5116 if (IsOpenMPDevice) {
5117 // We have to pass the triple of the host if compiling for an OpenMP device.
5118 std::string NormalizedTriple =
5119 C.getSingleOffloadToolChain<Action::OFK_Host>()
5120 ->getTriple()
5121 .normalize();
5122 CmdArgs.push_back(Elt: "-aux-triple");
5123 CmdArgs.push_back(Elt: Args.MakeArgString(Str: NormalizedTriple));
5124 }
5125
5126 if (Triple.isOSWindows() && (Triple.getArch() == llvm::Triple::arm ||
5127 Triple.getArch() == llvm::Triple::thumb)) {
5128 unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 4 : 6;
5129 unsigned Version = 0;
5130 bool Failure =
5131 Triple.getArchName().substr(Start: Offset).consumeInteger(Radix: 10, Result&: Version);
5132 if (Failure || Version < 7)
5133 D.Diag(DiagID: diag::err_target_unsupported_arch) << Triple.getArchName()
5134 << TripleStr;
5135 }
5136
5137 // Push all default warning arguments that are specific to
5138 // the given target. These come before user provided warning options
5139 // are provided.
5140 TC.addClangWarningOptions(CC1Args&: CmdArgs);
5141
5142 // FIXME: Subclass ToolChain for SPIR and move this to addClangWarningOptions.
5143 if (Triple.isSPIR() || Triple.isSPIRV())
5144 CmdArgs.push_back(Elt: "-Wspir-compat");
5145
5146 // Select the appropriate action.
5147 RewriteKind rewriteKind = RK_None;
5148
5149 bool UnifiedLTO = false;
5150 if (IsUsingLTO) {
5151 UnifiedLTO = Args.hasFlag(Pos: options::OPT_funified_lto,
5152 Neg: options::OPT_fno_unified_lto, Default: Triple.isPS());
5153 if (UnifiedLTO)
5154 CmdArgs.push_back(Elt: "-funified-lto");
5155 }
5156
5157 // If CollectArgsForIntegratedAssembler() isn't called below, claim the args
5158 // it claims when not running an assembler. Otherwise, clang would emit
5159 // "argument unused" warnings for assembler flags when e.g. adding "-E" to
5160 // flags while debugging something. That'd be somewhat inconvenient, and it's
5161 // also inconsistent with most other flags -- we don't warn on
5162 // -ffunction-sections not being used in -E mode either for example, even
5163 // though it's not really used either.
5164 if (!isa<AssembleJobAction>(Val: JA)) {
5165 // The args claimed here should match the args used in
5166 // CollectArgsForIntegratedAssembler().
5167 if (TC.useIntegratedAs()) {
5168 Args.ClaimAllArgs(Id0: options::OPT_mrelax_all);
5169 Args.ClaimAllArgs(Id0: options::OPT_mno_relax_all);
5170 Args.ClaimAllArgs(Id0: options::OPT_mincremental_linker_compatible);
5171 Args.ClaimAllArgs(Id0: options::OPT_mno_incremental_linker_compatible);
5172 switch (C.getDefaultToolChain().getArch()) {
5173 case llvm::Triple::arm:
5174 case llvm::Triple::armeb:
5175 case llvm::Triple::thumb:
5176 case llvm::Triple::thumbeb:
5177 Args.ClaimAllArgs(Id0: options::OPT_mimplicit_it_EQ);
5178 break;
5179 default:
5180 break;
5181 }
5182 }
5183 Args.ClaimAllArgs(Id0: options::OPT_Wa_COMMA);
5184 Args.ClaimAllArgs(Id0: options::OPT_Xassembler);
5185 Args.ClaimAllArgs(Id0: options::OPT_femit_dwarf_unwind_EQ);
5186 }
5187
5188 if (isa<AnalyzeJobAction>(Val: JA)) {
5189 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
5190 CmdArgs.push_back(Elt: "-analyze");
5191 } else if (isa<PreprocessJobAction>(Val: JA)) {
5192 if (Output.getType() == types::TY_Dependencies)
5193 CmdArgs.push_back(Elt: "-Eonly");
5194 else {
5195 CmdArgs.push_back(Elt: "-E");
5196 if (Args.hasArg(Ids: options::OPT_rewrite_objc) &&
5197 !Args.hasArg(Ids: options::OPT_g_Group))
5198 CmdArgs.push_back(Elt: "-P");
5199 else if (JA.getType() == types::TY_PP_CXXHeaderUnit)
5200 CmdArgs.push_back(Elt: "-fdirectives-only");
5201 }
5202 } else if (isa<AssembleJobAction>(Val: JA)) {
5203 CmdArgs.push_back(Elt: "-emit-obj");
5204
5205 CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
5206
5207 // Also ignore explicit -force_cpusubtype_ALL option.
5208 (void)Args.hasArg(Ids: options::OPT_force__cpusubtype__ALL);
5209 } else if (isa<PrecompileJobAction>(Val: JA)) {
5210 if (JA.getType() == types::TY_Nothing)
5211 CmdArgs.push_back(Elt: "-fsyntax-only");
5212 else if (JA.getType() == types::TY_ModuleFile)
5213 CmdArgs.push_back(Elt: "-emit-module-interface");
5214 else if (JA.getType() == types::TY_HeaderUnit)
5215 CmdArgs.push_back(Elt: "-emit-header-unit");
5216 else if (!Args.hasArg(Ids: options::OPT_ignore_pch))
5217 CmdArgs.push_back(Elt: "-emit-pch");
5218 } else if (isa<VerifyPCHJobAction>(Val: JA)) {
5219 CmdArgs.push_back(Elt: "-verify-pch");
5220 } else if (isa<ExtractAPIJobAction>(Val: JA)) {
5221 assert(JA.getType() == types::TY_API_INFO &&
5222 "Extract API actions must generate a API information.");
5223 CmdArgs.push_back(Elt: "-extract-api");
5224
5225 if (Arg *PrettySGFArg = Args.getLastArg(Ids: options::OPT_emit_pretty_sgf))
5226 PrettySGFArg->render(Args, Output&: CmdArgs);
5227
5228 Arg *SymbolGraphDirArg = Args.getLastArg(Ids: options::OPT_symbol_graph_dir_EQ);
5229
5230 if (Arg *ProductNameArg = Args.getLastArg(Ids: options::OPT_product_name_EQ))
5231 ProductNameArg->render(Args, Output&: CmdArgs);
5232 if (Arg *ExtractAPIIgnoresFileArg =
5233 Args.getLastArg(Ids: options::OPT_extract_api_ignores_EQ))
5234 ExtractAPIIgnoresFileArg->render(Args, Output&: CmdArgs);
5235 if (Arg *EmitExtensionSymbolGraphs =
5236 Args.getLastArg(Ids: options::OPT_emit_extension_symbol_graphs)) {
5237 if (!SymbolGraphDirArg)
5238 D.Diag(DiagID: diag::err_drv_missing_symbol_graph_dir);
5239
5240 EmitExtensionSymbolGraphs->render(Args, Output&: CmdArgs);
5241 }
5242 if (SymbolGraphDirArg)
5243 SymbolGraphDirArg->render(Args, Output&: CmdArgs);
5244 } else {
5245 assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) &&
5246 "Invalid action for clang tool.");
5247 if (JA.getType() == types::TY_Nothing) {
5248 CmdArgs.push_back(Elt: "-fsyntax-only");
5249 } else if (JA.getType() == types::TY_LLVM_IR ||
5250 JA.getType() == types::TY_LTO_IR) {
5251 CmdArgs.push_back(Elt: "-emit-llvm");
5252 } else if (JA.getType() == types::TY_LLVM_BC ||
5253 JA.getType() == types::TY_LTO_BC) {
5254 // Emit textual llvm IR for AMDGPU offloading for -emit-llvm -S
5255 if (Triple.isAMDGCN() && IsOpenMPDevice && Args.hasArg(Ids: options::OPT_S) &&
5256 Args.hasArg(Ids: options::OPT_emit_llvm)) {
5257 CmdArgs.push_back(Elt: "-emit-llvm");
5258 } else {
5259 CmdArgs.push_back(Elt: "-emit-llvm-bc");
5260 }
5261 } else if (JA.getType() == types::TY_IFS ||
5262 JA.getType() == types::TY_IFS_CPP) {
5263 StringRef ArgStr =
5264 Args.hasArg(Ids: options::OPT_interface_stub_version_EQ)
5265 ? Args.getLastArgValue(Id: options::OPT_interface_stub_version_EQ)
5266 : "ifs-v1";
5267 CmdArgs.push_back(Elt: "-emit-interface-stubs");
5268 CmdArgs.push_back(
5269 Elt: Args.MakeArgString(Str: Twine("-interface-stub-version=") + ArgStr.str()));
5270 } else if (JA.getType() == types::TY_PP_Asm) {
5271 CmdArgs.push_back(Elt: "-S");
5272 } else if (JA.getType() == types::TY_AST) {
5273 if (!Args.hasArg(Ids: options::OPT_ignore_pch))
5274 CmdArgs.push_back(Elt: "-emit-pch");
5275 } else if (JA.getType() == types::TY_ModuleFile) {
5276 CmdArgs.push_back(Elt: "-module-file-info");
5277 } else if (JA.getType() == types::TY_RewrittenObjC) {
5278 CmdArgs.push_back(Elt: "-rewrite-objc");
5279 rewriteKind = RK_NonFragile;
5280 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
5281 CmdArgs.push_back(Elt: "-rewrite-objc");
5282 rewriteKind = RK_Fragile;
5283 } else if (JA.getType() == types::TY_CIR) {
5284 CmdArgs.push_back(Elt: "-emit-cir");
5285 } else {
5286 assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!");
5287 }
5288
5289 // Preserve use-list order by default when emitting bitcode, so that
5290 // loading the bitcode up in 'opt' or 'llc' and running passes gives the
5291 // same result as running passes here. For LTO, we don't need to preserve
5292 // the use-list order, since serialization to bitcode is part of the flow.
5293 if (JA.getType() == types::TY_LLVM_BC)
5294 CmdArgs.push_back(Elt: "-emit-llvm-uselists");
5295
5296 if (IsUsingLTO) {
5297 if (IsDeviceOffloadAction && !JA.isDeviceOffloading(OKind: Action::OFK_OpenMP) &&
5298 !Args.hasFlag(Pos: options::OPT_offload_new_driver,
5299 Neg: options::OPT_no_offload_new_driver,
5300 Default: C.isOffloadingHostKind(Kind: Action::OFK_Cuda)) &&
5301 !Triple.isAMDGPU()) {
5302 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5303 << Args.getLastArg(Ids: options::OPT_foffload_lto,
5304 Ids: options::OPT_foffload_lto_EQ)
5305 ->getAsString(Args)
5306 << Triple.getTriple();
5307 } else if (Triple.isNVPTX() && !IsRDCMode &&
5308 JA.isDeviceOffloading(OKind: Action::OFK_Cuda)) {
5309 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_language_mode)
5310 << Args.getLastArg(Ids: options::OPT_foffload_lto,
5311 Ids: options::OPT_foffload_lto_EQ)
5312 ->getAsString(Args)
5313 << "-fno-gpu-rdc";
5314 } else {
5315 assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin);
5316 CmdArgs.push_back(Elt: Args.MakeArgString(
5317 Str: Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full")));
5318 // PS4 uses the legacy LTO API, which does not support some of the
5319 // features enabled by -flto-unit.
5320 if (!RawTriple.isPS4() ||
5321 (D.getLTOMode() == LTOK_Full) || !UnifiedLTO)
5322 CmdArgs.push_back(Elt: "-flto-unit");
5323 }
5324 }
5325 }
5326
5327 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_dumpdir);
5328
5329 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fthinlto_index_EQ)) {
5330 if (!types::isLLVMIR(Id: Input.getType()))
5331 D.Diag(DiagID: diag::err_drv_arg_requires_bitcode_input) << A->getAsString(Args);
5332 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fthinlto_index_EQ);
5333 }
5334
5335 if (Triple.isPPC())
5336 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_mregnames,
5337 Neg: options::OPT_mno_regnames);
5338
5339 if (Args.getLastArg(Ids: options::OPT_fthin_link_bitcode_EQ))
5340 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fthin_link_bitcode_EQ);
5341
5342 if (Args.getLastArg(Ids: options::OPT_save_temps_EQ))
5343 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_save_temps_EQ);
5344
5345 auto *MemProfArg = Args.getLastArg(Ids: options::OPT_fmemory_profile,
5346 Ids: options::OPT_fmemory_profile_EQ,
5347 Ids: options::OPT_fno_memory_profile);
5348 if (MemProfArg &&
5349 !MemProfArg->getOption().matches(ID: options::OPT_fno_memory_profile))
5350 MemProfArg->render(Args, Output&: CmdArgs);
5351
5352 if (auto *MemProfUseArg =
5353 Args.getLastArg(Ids: options::OPT_fmemory_profile_use_EQ)) {
5354 if (MemProfArg)
5355 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
5356 << MemProfUseArg->getAsString(Args) << MemProfArg->getAsString(Args);
5357 if (auto *PGOInstrArg = Args.getLastArg(Ids: options::OPT_fprofile_generate,
5358 Ids: options::OPT_fprofile_generate_EQ))
5359 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
5360 << MemProfUseArg->getAsString(Args) << PGOInstrArg->getAsString(Args);
5361 MemProfUseArg->render(Args, Output&: CmdArgs);
5362 }
5363
5364 // Embed-bitcode option.
5365 // Only white-listed flags below are allowed to be embedded.
5366 if (C.getDriver().embedBitcodeInObject() && !IsUsingLTO &&
5367 (isa<BackendJobAction>(Val: JA) || isa<AssembleJobAction>(Val: JA))) {
5368 // Add flags implied by -fembed-bitcode.
5369 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fembed_bitcode_EQ);
5370 // Disable all llvm IR level optimizations.
5371 CmdArgs.push_back(Elt: "-disable-llvm-passes");
5372
5373 // Render target options.
5374 TC.addClangTargetOptions(DriverArgs: Args, CC1Args&: CmdArgs, DeviceOffloadKind: JA.getOffloadingDeviceKind());
5375
5376 // reject options that shouldn't be supported in bitcode
5377 // also reject kernel/kext
5378 static const constexpr unsigned kBitcodeOptionIgnorelist[] = {
5379 options::OPT_mkernel,
5380 options::OPT_fapple_kext,
5381 options::OPT_ffunction_sections,
5382 options::OPT_fno_function_sections,
5383 options::OPT_fdata_sections,
5384 options::OPT_fno_data_sections,
5385 options::OPT_fbasic_block_sections_EQ,
5386 options::OPT_funique_internal_linkage_names,
5387 options::OPT_fno_unique_internal_linkage_names,
5388 options::OPT_funique_section_names,
5389 options::OPT_fno_unique_section_names,
5390 options::OPT_funique_basic_block_section_names,
5391 options::OPT_fno_unique_basic_block_section_names,
5392 options::OPT_mrestrict_it,
5393 options::OPT_mno_restrict_it,
5394 options::OPT_mstackrealign,
5395 options::OPT_mno_stackrealign,
5396 options::OPT_mstack_alignment,
5397 options::OPT_mcmodel_EQ,
5398 options::OPT_mlong_calls,
5399 options::OPT_mno_long_calls,
5400 options::OPT_ggnu_pubnames,
5401 options::OPT_gdwarf_aranges,
5402 options::OPT_fdebug_types_section,
5403 options::OPT_fno_debug_types_section,
5404 options::OPT_fdwarf_directory_asm,
5405 options::OPT_fno_dwarf_directory_asm,
5406 options::OPT_mrelax_all,
5407 options::OPT_mno_relax_all,
5408 options::OPT_ftrap_function_EQ,
5409 options::OPT_ffixed_r9,
5410 options::OPT_mfix_cortex_a53_835769,
5411 options::OPT_mno_fix_cortex_a53_835769,
5412 options::OPT_ffixed_x18,
5413 options::OPT_mglobal_merge,
5414 options::OPT_mno_global_merge,
5415 options::OPT_mred_zone,
5416 options::OPT_mno_red_zone,
5417 options::OPT_Wa_COMMA,
5418 options::OPT_Xassembler,
5419 options::OPT_mllvm,
5420 options::OPT_mmlir,
5421 };
5422 for (const auto &A : Args)
5423 if (llvm::is_contained(Range: kBitcodeOptionIgnorelist, Element: A->getOption().getID()))
5424 D.Diag(DiagID: diag::err_drv_unsupported_embed_bitcode) << A->getSpelling();
5425
5426 // Render the CodeGen options that need to be passed.
5427 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_foptimize_sibling_calls,
5428 Neg: options::OPT_fno_optimize_sibling_calls);
5429
5430 RenderFloatingPointOptions(TC, D, OFastEnabled: isOptimizationLevelFast(Args), Args,
5431 CmdArgs, JA);
5432
5433 // Render ABI arguments
5434 switch (TC.getArch()) {
5435 default: break;
5436 case llvm::Triple::arm:
5437 case llvm::Triple::armeb:
5438 case llvm::Triple::thumbeb:
5439 RenderARMABI(D, Triple, Args, CmdArgs);
5440 break;
5441 case llvm::Triple::aarch64:
5442 case llvm::Triple::aarch64_32:
5443 case llvm::Triple::aarch64_be:
5444 RenderAArch64ABI(Triple, Args, CmdArgs);
5445 break;
5446 }
5447
5448 // Input/Output file.
5449 if (Output.getType() == types::TY_Dependencies) {
5450 // Handled with other dependency code.
5451 } else if (Output.isFilename()) {
5452 CmdArgs.push_back(Elt: "-o");
5453 CmdArgs.push_back(Elt: Output.getFilename());
5454 } else {
5455 assert(Output.isNothing() && "Input output.");
5456 }
5457
5458 for (const auto &II : Inputs) {
5459 addDashXForInput(Args, Input: II, CmdArgs);
5460 if (II.isFilename())
5461 CmdArgs.push_back(Elt: II.getFilename());
5462 else
5463 II.getInputArg().renderAsInput(Args, Output&: CmdArgs);
5464 }
5465
5466 C.addCommand(C: std::make_unique<Command>(
5467 args: JA, args: *this, args: ResponseFileSupport::AtFileUTF8(), args: D.getClangProgramPath(),
5468 args&: CmdArgs, args: Inputs, args: Output, args: D.getPrependArg()));
5469 return;
5470 }
5471
5472 if (C.getDriver().embedBitcodeMarkerOnly() && !IsUsingLTO)
5473 CmdArgs.push_back(Elt: "-fembed-bitcode=marker");
5474
5475 // We normally speed up the clang process a bit by skipping destructors at
5476 // exit, but when we're generating diagnostics we can rely on some of the
5477 // cleanup.
5478 if (!C.isForDiagnostics())
5479 CmdArgs.push_back(Elt: "-disable-free");
5480 CmdArgs.push_back(Elt: "-clear-ast-before-backend");
5481
5482#ifdef NDEBUG
5483 const bool IsAssertBuild = false;
5484#else
5485 const bool IsAssertBuild = true;
5486#endif
5487
5488 // Disable the verification pass in asserts builds unless otherwise specified.
5489 if (Args.hasFlag(Pos: options::OPT_fno_verify_intermediate_code,
5490 Neg: options::OPT_fverify_intermediate_code, Default: !IsAssertBuild)) {
5491 CmdArgs.push_back(Elt: "-disable-llvm-verifier");
5492 }
5493
5494 // Discard value names in assert builds unless otherwise specified.
5495 if (Args.hasFlag(Pos: options::OPT_fdiscard_value_names,
5496 Neg: options::OPT_fno_discard_value_names, Default: !IsAssertBuild)) {
5497 if (Args.hasArg(Ids: options::OPT_fdiscard_value_names) &&
5498 llvm::any_of(Range: Inputs, P: [](const clang::driver::InputInfo &II) {
5499 return types::isLLVMIR(Id: II.getType());
5500 })) {
5501 D.Diag(DiagID: diag::warn_ignoring_fdiscard_for_bitcode);
5502 }
5503 CmdArgs.push_back(Elt: "-discard-value-names");
5504 }
5505
5506 // Set the main file name, so that debug info works even with
5507 // -save-temps.
5508 CmdArgs.push_back(Elt: "-main-file-name");
5509 CmdArgs.push_back(Elt: getBaseInputName(Args, Input));
5510
5511 // Some flags which affect the language (via preprocessor
5512 // defines).
5513 if (Args.hasArg(Ids: options::OPT_static))
5514 CmdArgs.push_back(Elt: "-static-define");
5515
5516 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_static_libclosure);
5517
5518 if (Args.hasArg(Ids: options::OPT_municode))
5519 CmdArgs.push_back(Elt: "-DUNICODE");
5520
5521 if (isa<AnalyzeJobAction>(Val: JA))
5522 RenderAnalyzerOptions(Args, CmdArgs, Triple, Input);
5523
5524 if (isa<AnalyzeJobAction>(Val: JA) ||
5525 (isa<PreprocessJobAction>(Val: JA) && Args.hasArg(Ids: options::OPT__analyze)))
5526 CmdArgs.push_back(Elt: "-setup-static-analyzer");
5527
5528 // Enable compatilibily mode to avoid analyzer-config related errors.
5529 // Since we can't access frontend flags through hasArg, let's manually iterate
5530 // through them.
5531 bool FoundAnalyzerConfig = false;
5532 for (auto *Arg : Args.filtered(Ids: options::OPT_Xclang))
5533 if (StringRef(Arg->getValue()) == "-analyzer-config") {
5534 FoundAnalyzerConfig = true;
5535 break;
5536 }
5537 if (!FoundAnalyzerConfig)
5538 for (auto *Arg : Args.filtered(Ids: options::OPT_Xanalyzer))
5539 if (StringRef(Arg->getValue()) == "-analyzer-config") {
5540 FoundAnalyzerConfig = true;
5541 break;
5542 }
5543 if (FoundAnalyzerConfig)
5544 CmdArgs.push_back(Elt: "-analyzer-config-compatibility-mode=true");
5545
5546 CheckCodeGenerationOptions(D, Args);
5547
5548 unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args);
5549 assert(FunctionAlignment <= 31 && "function alignment will be truncated!");
5550 if (FunctionAlignment) {
5551 CmdArgs.push_back(Elt: "-function-alignment");
5552 CmdArgs.push_back(Elt: Args.MakeArgString(Str: std::to_string(val: FunctionAlignment)));
5553 }
5554
5555 // We support -falign-loops=N where N is a power of 2. GCC supports more
5556 // forms.
5557 if (const Arg *A = Args.getLastArg(Ids: options::OPT_falign_loops_EQ)) {
5558 unsigned Value = 0;
5559 if (StringRef(A->getValue()).getAsInteger(Radix: 10, Result&: Value) || Value > 65536)
5560 TC.getDriver().Diag(DiagID: diag::err_drv_invalid_int_value)
5561 << A->getAsString(Args) << A->getValue();
5562 else if (Value & (Value - 1))
5563 TC.getDriver().Diag(DiagID: diag::err_drv_alignment_not_power_of_two)
5564 << A->getAsString(Args) << A->getValue();
5565 // Treat =0 as unspecified (use the target preference).
5566 if (Value)
5567 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-falign-loops=" +
5568 Twine(std::min(a: Value, b: 65536u))));
5569 }
5570
5571 if (Triple.isOSzOS()) {
5572 // On z/OS some of the system header feature macros need to
5573 // be defined to enable most cross platform projects to build
5574 // successfully. Ths include the libc++ library. A
5575 // complicating factor is that users can define these
5576 // macros to the same or different values. We need to add
5577 // the definition for these macros to the compilation command
5578 // if the user hasn't already defined them.
5579
5580 auto findMacroDefinition = [&](const std::string &Macro) {
5581 auto MacroDefs = Args.getAllArgValues(Id: options::OPT_D);
5582 return llvm::any_of(Range&: MacroDefs, P: [&](const std::string &M) {
5583 return M == Macro || M.find(str: Macro + '=') != std::string::npos;
5584 });
5585 };
5586
5587 // _UNIX03_WITHDRAWN is required for libcxx & porting.
5588 if (!findMacroDefinition("_UNIX03_WITHDRAWN"))
5589 CmdArgs.push_back(Elt: "-D_UNIX03_WITHDRAWN");
5590 // _OPEN_DEFAULT is required for XL compat
5591 if (!findMacroDefinition("_OPEN_DEFAULT"))
5592 CmdArgs.push_back(Elt: "-D_OPEN_DEFAULT");
5593 if (D.CCCIsCXX() || types::isCXX(Id: Input.getType())) {
5594 // _XOPEN_SOURCE=600 is required for libcxx.
5595 if (!findMacroDefinition("_XOPEN_SOURCE"))
5596 CmdArgs.push_back(Elt: "-D_XOPEN_SOURCE=600");
5597 }
5598 }
5599
5600 llvm::Reloc::Model RelocationModel;
5601 unsigned PICLevel;
5602 bool IsPIE;
5603 std::tie(args&: RelocationModel, args&: PICLevel, args&: IsPIE) = ParsePICArgs(ToolChain: TC, Args);
5604 Arg *LastPICDataRelArg =
5605 Args.getLastArg(Ids: options::OPT_mno_pic_data_is_text_relative,
5606 Ids: options::OPT_mpic_data_is_text_relative);
5607 bool NoPICDataIsTextRelative = false;
5608 if (LastPICDataRelArg) {
5609 if (LastPICDataRelArg->getOption().matches(
5610 ID: options::OPT_mno_pic_data_is_text_relative)) {
5611 NoPICDataIsTextRelative = true;
5612 if (!PICLevel)
5613 D.Diag(DiagID: diag::err_drv_argument_only_allowed_with)
5614 << "-mno-pic-data-is-text-relative"
5615 << "-fpic/-fpie";
5616 }
5617 if (!Triple.isSystemZ())
5618 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5619 << (NoPICDataIsTextRelative ? "-mno-pic-data-is-text-relative"
5620 : "-mpic-data-is-text-relative")
5621 << RawTriple.str();
5622 }
5623
5624 bool IsROPI = RelocationModel == llvm::Reloc::ROPI ||
5625 RelocationModel == llvm::Reloc::ROPI_RWPI;
5626 bool IsRWPI = RelocationModel == llvm::Reloc::RWPI ||
5627 RelocationModel == llvm::Reloc::ROPI_RWPI;
5628
5629 if (Args.hasArg(Ids: options::OPT_mcmse) &&
5630 !Args.hasArg(Ids: options::OPT_fallow_unsupported)) {
5631 if (IsROPI)
5632 D.Diag(DiagID: diag::err_cmse_pi_are_incompatible) << IsROPI;
5633 if (IsRWPI)
5634 D.Diag(DiagID: diag::err_cmse_pi_are_incompatible) << !IsRWPI;
5635 }
5636
5637 if (IsROPI && types::isCXX(Id: Input.getType()) &&
5638 !Args.hasArg(Ids: options::OPT_fallow_unsupported))
5639 D.Diag(DiagID: diag::err_drv_ropi_incompatible_with_cxx);
5640
5641 const char *RMName = RelocationModelName(Model: RelocationModel);
5642 if (RMName) {
5643 CmdArgs.push_back(Elt: "-mrelocation-model");
5644 CmdArgs.push_back(Elt: RMName);
5645 }
5646 if (PICLevel > 0) {
5647 CmdArgs.push_back(Elt: "-pic-level");
5648 CmdArgs.push_back(Elt: PICLevel == 1 ? "1" : "2");
5649 if (IsPIE)
5650 CmdArgs.push_back(Elt: "-pic-is-pie");
5651 if (NoPICDataIsTextRelative)
5652 CmdArgs.push_back(Elt: "-mcmodel=medium");
5653 }
5654
5655 if (RelocationModel == llvm::Reloc::ROPI ||
5656 RelocationModel == llvm::Reloc::ROPI_RWPI)
5657 CmdArgs.push_back(Elt: "-fropi");
5658 if (RelocationModel == llvm::Reloc::RWPI ||
5659 RelocationModel == llvm::Reloc::ROPI_RWPI)
5660 CmdArgs.push_back(Elt: "-frwpi");
5661
5662 if (Arg *A = Args.getLastArg(Ids: options::OPT_meabi)) {
5663 CmdArgs.push_back(Elt: "-meabi");
5664 CmdArgs.push_back(Elt: A->getValue());
5665 }
5666
5667 // -fsemantic-interposition is forwarded to CC1: set the
5668 // "SemanticInterposition" metadata to 1 (make some linkages interposable) and
5669 // make default visibility external linkage definitions dso_preemptable.
5670 //
5671 // -fno-semantic-interposition: if the target supports .Lfoo$local local
5672 // aliases (make default visibility external linkage definitions dso_local).
5673 // This is the CC1 default for ELF to match COFF/Mach-O.
5674 //
5675 // Otherwise use Clang's traditional behavior: like
5676 // -fno-semantic-interposition but local aliases are not used. So references
5677 // can be interposed if not optimized out.
5678 if (Triple.isOSBinFormatELF()) {
5679 Arg *A = Args.getLastArg(Ids: options::OPT_fsemantic_interposition,
5680 Ids: options::OPT_fno_semantic_interposition);
5681 if (RelocationModel != llvm::Reloc::Static && !IsPIE) {
5682 // The supported targets need to call AsmPrinter::getSymbolPreferLocal.
5683 bool SupportsLocalAlias =
5684 Triple.isAArch64() || Triple.isRISCV() || Triple.isX86();
5685 if (!A)
5686 CmdArgs.push_back(Elt: "-fhalf-no-semantic-interposition");
5687 else if (A->getOption().matches(ID: options::OPT_fsemantic_interposition))
5688 A->render(Args, Output&: CmdArgs);
5689 else if (!SupportsLocalAlias)
5690 CmdArgs.push_back(Elt: "-fhalf-no-semantic-interposition");
5691 }
5692 }
5693
5694 {
5695 std::string Model;
5696 if (Arg *A = Args.getLastArg(Ids: options::OPT_mthread_model)) {
5697 if (!TC.isThreadModelSupported(Model: A->getValue()))
5698 D.Diag(DiagID: diag::err_drv_invalid_thread_model_for_target)
5699 << A->getValue() << A->getAsString(Args);
5700 Model = A->getValue();
5701 } else
5702 Model = TC.getThreadModel();
5703 if (Model != "posix") {
5704 CmdArgs.push_back(Elt: "-mthread-model");
5705 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Model));
5706 }
5707 }
5708
5709 if (Arg *A = Args.getLastArg(Ids: options::OPT_fveclib)) {
5710 StringRef Name = A->getValue();
5711 if (Name == "SVML") {
5712 if (Triple.getArch() != llvm::Triple::x86 &&
5713 Triple.getArch() != llvm::Triple::x86_64)
5714 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5715 << Name << Triple.getArchName();
5716 } else if (Name == "AMDLIBM") {
5717 if (Triple.getArch() != llvm::Triple::x86 &&
5718 Triple.getArch() != llvm::Triple::x86_64)
5719 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5720 << Name << Triple.getArchName();
5721 } else if (Name == "libmvec") {
5722 if (Triple.getArch() != llvm::Triple::x86 &&
5723 Triple.getArch() != llvm::Triple::x86_64 &&
5724 Triple.getArch() != llvm::Triple::aarch64 &&
5725 Triple.getArch() != llvm::Triple::aarch64_be)
5726 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5727 << Name << Triple.getArchName();
5728 } else if (Name == "SLEEF" || Name == "ArmPL") {
5729 if (Triple.getArch() != llvm::Triple::aarch64 &&
5730 Triple.getArch() != llvm::Triple::aarch64_be &&
5731 Triple.getArch() != llvm::Triple::riscv64)
5732 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5733 << Name << Triple.getArchName();
5734 }
5735 A->render(Args, Output&: CmdArgs);
5736 }
5737
5738 if (Args.hasFlag(Pos: options::OPT_fmerge_all_constants,
5739 Neg: options::OPT_fno_merge_all_constants, Default: false))
5740 CmdArgs.push_back(Elt: "-fmerge-all-constants");
5741
5742 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fdelete_null_pointer_checks,
5743 Neg: options::OPT_fno_delete_null_pointer_checks);
5744
5745 // LLVM Code Generator Options.
5746
5747 if (Arg *A = Args.getLastArg(Ids: options::OPT_mabi_EQ_quadword_atomics)) {
5748 if (!Triple.isOSAIX() || Triple.isPPC32())
5749 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5750 << A->getSpelling() << RawTriple.str();
5751 CmdArgs.push_back(Elt: "-mabi=quadword-atomics");
5752 }
5753
5754 if (Arg *A = Args.getLastArg(Ids: options::OPT_mlong_double_128)) {
5755 // Emit the unsupported option error until the Clang's library integration
5756 // support for 128-bit long double is available for AIX.
5757 if (Triple.isOSAIX())
5758 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5759 << A->getSpelling() << RawTriple.str();
5760 }
5761
5762 if (Arg *A = Args.getLastArg(Ids: options::OPT_Wframe_larger_than_EQ)) {
5763 StringRef V = A->getValue(), V1 = V;
5764 unsigned Size;
5765 if (V1.consumeInteger(Radix: 10, Result&: Size) || !V1.empty())
5766 D.Diag(DiagID: diag::err_drv_invalid_argument_to_option)
5767 << V << A->getOption().getName();
5768 else
5769 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fwarn-stack-size=" + V));
5770 }
5771
5772 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fjump_tables,
5773 Neg: options::OPT_fno_jump_tables);
5774 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fprofile_sample_accurate,
5775 Neg: options::OPT_fno_profile_sample_accurate);
5776 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fpreserve_as_comments,
5777 Neg: options::OPT_fno_preserve_as_comments);
5778
5779 if (Arg *A = Args.getLastArg(Ids: options::OPT_mregparm_EQ)) {
5780 CmdArgs.push_back(Elt: "-mregparm");
5781 CmdArgs.push_back(Elt: A->getValue());
5782 }
5783
5784 if (Arg *A = Args.getLastArg(Ids: options::OPT_maix_struct_return,
5785 Ids: options::OPT_msvr4_struct_return)) {
5786 if (!TC.getTriple().isPPC32()) {
5787 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5788 << A->getSpelling() << RawTriple.str();
5789 } else if (A->getOption().matches(ID: options::OPT_maix_struct_return)) {
5790 CmdArgs.push_back(Elt: "-maix-struct-return");
5791 } else {
5792 assert(A->getOption().matches(options::OPT_msvr4_struct_return));
5793 CmdArgs.push_back(Elt: "-msvr4-struct-return");
5794 }
5795 }
5796
5797 if (Arg *A = Args.getLastArg(Ids: options::OPT_fpcc_struct_return,
5798 Ids: options::OPT_freg_struct_return)) {
5799 if (TC.getArch() != llvm::Triple::x86) {
5800 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5801 << A->getSpelling() << RawTriple.str();
5802 } else if (A->getOption().matches(ID: options::OPT_fpcc_struct_return)) {
5803 CmdArgs.push_back(Elt: "-fpcc-struct-return");
5804 } else {
5805 assert(A->getOption().matches(options::OPT_freg_struct_return));
5806 CmdArgs.push_back(Elt: "-freg-struct-return");
5807 }
5808 }
5809
5810 if (Args.hasFlag(Pos: options::OPT_mrtd, Neg: options::OPT_mno_rtd, Default: false)) {
5811 if (Triple.getArch() == llvm::Triple::m68k)
5812 CmdArgs.push_back(Elt: "-fdefault-calling-conv=rtdcall");
5813 else
5814 CmdArgs.push_back(Elt: "-fdefault-calling-conv=stdcall");
5815 }
5816
5817 if (Args.hasArg(Ids: options::OPT_fenable_matrix)) {
5818 // enable-matrix is needed by both the LangOpts and by LLVM.
5819 CmdArgs.push_back(Elt: "-fenable-matrix");
5820 CmdArgs.push_back(Elt: "-mllvm");
5821 CmdArgs.push_back(Elt: "-enable-matrix");
5822 }
5823
5824 CodeGenOptions::FramePointerKind FPKeepKind =
5825 getFramePointerKind(Args, Triple: RawTriple);
5826 const char *FPKeepKindStr = nullptr;
5827 switch (FPKeepKind) {
5828 case CodeGenOptions::FramePointerKind::None:
5829 FPKeepKindStr = "-mframe-pointer=none";
5830 break;
5831 case CodeGenOptions::FramePointerKind::Reserved:
5832 FPKeepKindStr = "-mframe-pointer=reserved";
5833 break;
5834 case CodeGenOptions::FramePointerKind::NonLeaf:
5835 FPKeepKindStr = "-mframe-pointer=non-leaf";
5836 break;
5837 case CodeGenOptions::FramePointerKind::All:
5838 FPKeepKindStr = "-mframe-pointer=all";
5839 break;
5840 }
5841 assert(FPKeepKindStr && "unknown FramePointerKind");
5842 CmdArgs.push_back(Elt: FPKeepKindStr);
5843
5844 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fzero_initialized_in_bss,
5845 Neg: options::OPT_fno_zero_initialized_in_bss);
5846
5847 bool OFastEnabled = isOptimizationLevelFast(Args);
5848 if (OFastEnabled)
5849 D.Diag(DiagID: diag::warn_drv_deprecated_arg_ofast);
5850 // If -Ofast is the optimization level, then -fstrict-aliasing should be
5851 // enabled. This alias option is being used to simplify the hasFlag logic.
5852 OptSpecifier StrictAliasingAliasOption =
5853 OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing;
5854 // We turn strict aliasing off by default if we're Windows MSVC since MSVC
5855 // doesn't do any TBAA.
5856 if (!Args.hasFlag(Pos: options::OPT_fstrict_aliasing, PosAlias: StrictAliasingAliasOption,
5857 Neg: options::OPT_fno_strict_aliasing,
5858 Default: !IsWindowsMSVC && !IsUEFI))
5859 CmdArgs.push_back(Elt: "-relaxed-aliasing");
5860 if (Args.hasFlag(Pos: options::OPT_fno_pointer_tbaa, Neg: options::OPT_fpointer_tbaa,
5861 Default: false))
5862 CmdArgs.push_back(Elt: "-no-pointer-tbaa");
5863 if (!Args.hasFlag(Pos: options::OPT_fstruct_path_tbaa,
5864 Neg: options::OPT_fno_struct_path_tbaa, Default: true))
5865 CmdArgs.push_back(Elt: "-no-struct-path-tbaa");
5866 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fstrict_enums,
5867 Neg: options::OPT_fno_strict_enums);
5868 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fstrict_return,
5869 Neg: options::OPT_fno_strict_return);
5870 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fallow_editor_placeholders,
5871 Neg: options::OPT_fno_allow_editor_placeholders);
5872 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fstrict_vtable_pointers,
5873 Neg: options::OPT_fno_strict_vtable_pointers);
5874 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fforce_emit_vtables,
5875 Neg: options::OPT_fno_force_emit_vtables);
5876 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_foptimize_sibling_calls,
5877 Neg: options::OPT_fno_optimize_sibling_calls);
5878 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fescaping_block_tail_calls,
5879 Neg: options::OPT_fno_escaping_block_tail_calls);
5880
5881 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ffine_grained_bitfield_accesses,
5882 Ids: options::OPT_fno_fine_grained_bitfield_accesses);
5883
5884 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fexperimental_relative_cxx_abi_vtables,
5885 Ids: options::OPT_fno_experimental_relative_cxx_abi_vtables);
5886
5887 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fexperimental_omit_vtable_rtti,
5888 Ids: options::OPT_fno_experimental_omit_vtable_rtti);
5889
5890 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fdisable_block_signature_string,
5891 Ids: options::OPT_fno_disable_block_signature_string);
5892
5893 // Handle segmented stacks.
5894 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fsplit_stack,
5895 Neg: options::OPT_fno_split_stack);
5896
5897 // -fprotect-parens=0 is default.
5898 if (Args.hasFlag(Pos: options::OPT_fprotect_parens,
5899 Neg: options::OPT_fno_protect_parens, Default: false))
5900 CmdArgs.push_back(Elt: "-fprotect-parens");
5901
5902 RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs, JA);
5903
5904 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fatomic_remote_memory,
5905 Neg: options::OPT_fno_atomic_remote_memory);
5906 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fatomic_fine_grained_memory,
5907 Neg: options::OPT_fno_atomic_fine_grained_memory);
5908 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fatomic_ignore_denormal_mode,
5909 Neg: options::OPT_fno_atomic_ignore_denormal_mode);
5910
5911 if (Arg *A = Args.getLastArg(Ids: options::OPT_fextend_args_EQ)) {
5912 const llvm::Triple::ArchType Arch = TC.getArch();
5913 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
5914 StringRef V = A->getValue();
5915 if (V == "64")
5916 CmdArgs.push_back(Elt: "-fextend-arguments=64");
5917 else if (V != "32")
5918 D.Diag(DiagID: diag::err_drv_invalid_argument_to_option)
5919 << A->getValue() << A->getOption().getName();
5920 } else
5921 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5922 << A->getOption().getName() << TripleStr;
5923 }
5924
5925 if (Arg *A = Args.getLastArg(Ids: options::OPT_mdouble_EQ)) {
5926 if (TC.getArch() == llvm::Triple::avr)
5927 A->render(Args, Output&: CmdArgs);
5928 else
5929 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5930 << A->getAsString(Args) << TripleStr;
5931 }
5932
5933 if (Arg *A = Args.getLastArg(Ids: options::OPT_LongDouble_Group)) {
5934 if (TC.getTriple().isX86())
5935 A->render(Args, Output&: CmdArgs);
5936 else if (TC.getTriple().isPPC() &&
5937 (A->getOption().getID() != options::OPT_mlong_double_80))
5938 A->render(Args, Output&: CmdArgs);
5939 else
5940 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5941 << A->getAsString(Args) << TripleStr;
5942 }
5943
5944 // Decide whether to use verbose asm. Verbose assembly is the default on
5945 // toolchains which have the integrated assembler on by default.
5946 bool IsIntegratedAssemblerDefault = TC.IsIntegratedAssemblerDefault();
5947 if (!Args.hasFlag(Pos: options::OPT_fverbose_asm, Neg: options::OPT_fno_verbose_asm,
5948 Default: IsIntegratedAssemblerDefault))
5949 CmdArgs.push_back(Elt: "-fno-verbose-asm");
5950
5951 // Parse 'none' or '$major.$minor'. Disallow -fbinutils-version=0 because we
5952 // use that to indicate the MC default in the backend.
5953 if (Arg *A = Args.getLastArg(Ids: options::OPT_fbinutils_version_EQ)) {
5954 StringRef V = A->getValue();
5955 unsigned Num;
5956 if (V == "none")
5957 A->render(Args, Output&: CmdArgs);
5958 else if (!V.consumeInteger(Radix: 10, Result&: Num) && Num > 0 &&
5959 (V.empty() || (V.consume_front(Prefix: ".") &&
5960 !V.consumeInteger(Radix: 10, Result&: Num) && V.empty())))
5961 A->render(Args, Output&: CmdArgs);
5962 else
5963 D.Diag(DiagID: diag::err_drv_invalid_argument_to_option)
5964 << A->getValue() << A->getOption().getName();
5965 }
5966
5967 // If toolchain choose to use MCAsmParser for inline asm don't pass the
5968 // option to disable integrated-as explicitly.
5969 if (!TC.useIntegratedAs() && !TC.parseInlineAsmUsingAsmParser())
5970 CmdArgs.push_back(Elt: "-no-integrated-as");
5971
5972 if (Args.hasArg(Ids: options::OPT_fdebug_pass_structure)) {
5973 CmdArgs.push_back(Elt: "-mdebug-pass");
5974 CmdArgs.push_back(Elt: "Structure");
5975 }
5976 if (Args.hasArg(Ids: options::OPT_fdebug_pass_arguments)) {
5977 CmdArgs.push_back(Elt: "-mdebug-pass");
5978 CmdArgs.push_back(Elt: "Arguments");
5979 }
5980
5981 // Enable -mconstructor-aliases except on darwin, where we have to work around
5982 // a linker bug (see https://openradar.appspot.com/7198997), and CUDA device
5983 // code, where aliases aren't supported.
5984 if (!RawTriple.isOSDarwin() && !RawTriple.isNVPTX())
5985 CmdArgs.push_back(Elt: "-mconstructor-aliases");
5986
5987 // Darwin's kernel doesn't support guard variables; just die if we
5988 // try to use them.
5989 if (KernelOrKext && RawTriple.isOSDarwin())
5990 CmdArgs.push_back(Elt: "-fforbid-guard-variables");
5991
5992 if (Args.hasFlag(Pos: options::OPT_mms_bitfields, Neg: options::OPT_mno_ms_bitfields,
5993 Default: Triple.isWindowsGNUEnvironment())) {
5994 CmdArgs.push_back(Elt: "-mms-bitfields");
5995 }
5996
5997 if (Triple.isWindowsGNUEnvironment()) {
5998 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fauto_import,
5999 Neg: options::OPT_fno_auto_import);
6000 }
6001
6002 if (Args.hasFlag(Pos: options::OPT_fms_volatile, Neg: options::OPT_fno_ms_volatile,
6003 Default: Triple.isX86() && IsWindowsMSVC))
6004 CmdArgs.push_back(Elt: "-fms-volatile");
6005
6006 // Non-PIC code defaults to -fdirect-access-external-data while PIC code
6007 // defaults to -fno-direct-access-external-data. Pass the option if different
6008 // from the default.
6009 if (Arg *A = Args.getLastArg(Ids: options::OPT_fdirect_access_external_data,
6010 Ids: options::OPT_fno_direct_access_external_data)) {
6011 if (A->getOption().matches(ID: options::OPT_fdirect_access_external_data) !=
6012 (PICLevel == 0))
6013 A->render(Args, Output&: CmdArgs);
6014 } else if (PICLevel == 0 && Triple.isLoongArch()) {
6015 // Some targets default to -fno-direct-access-external-data even for
6016 // -fno-pic.
6017 CmdArgs.push_back(Elt: "-fno-direct-access-external-data");
6018 }
6019
6020 if (Triple.isOSBinFormatELF() && (Triple.isAArch64() || Triple.isX86()))
6021 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fplt, Neg: options::OPT_fno_plt);
6022
6023 // -fhosted is default.
6024 // TODO: Audit uses of KernelOrKext and see where it'd be more appropriate to
6025 // use Freestanding.
6026 bool Freestanding =
6027 Args.hasFlag(Pos: options::OPT_ffreestanding, Neg: options::OPT_fhosted, Default: false) ||
6028 KernelOrKext;
6029 if (Freestanding)
6030 CmdArgs.push_back(Elt: "-ffreestanding");
6031
6032 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fno_knr_functions);
6033
6034 // This is a coarse approximation of what llvm-gcc actually does, both
6035 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
6036 // complicated ways.
6037 auto SanitizeArgs = TC.getSanitizerArgs(JobArgs: Args);
6038 Args.AddLastArg(Output&: CmdArgs,
6039 Ids: options::OPT_fallow_runtime_check_skip_hot_cutoff_EQ);
6040 bool IsAsyncUnwindTablesDefault =
6041 TC.getDefaultUnwindTableLevel(Args) == ToolChain::UnwindTableLevel::Asynchronous;
6042 bool IsSyncUnwindTablesDefault =
6043 TC.getDefaultUnwindTableLevel(Args) == ToolChain::UnwindTableLevel::Synchronous;
6044
6045 bool AsyncUnwindTables = Args.hasFlag(
6046 Pos: options::OPT_fasynchronous_unwind_tables,
6047 Neg: options::OPT_fno_asynchronous_unwind_tables,
6048 Default: (IsAsyncUnwindTablesDefault || SanitizeArgs.needsUnwindTables()) &&
6049 !Freestanding);
6050 bool UnwindTables =
6051 Args.hasFlag(Pos: options::OPT_funwind_tables, Neg: options::OPT_fno_unwind_tables,
6052 Default: IsSyncUnwindTablesDefault && !Freestanding);
6053 if (AsyncUnwindTables)
6054 CmdArgs.push_back(Elt: "-funwind-tables=2");
6055 else if (UnwindTables)
6056 CmdArgs.push_back(Elt: "-funwind-tables=1");
6057
6058 // Prepare `-aux-target-cpu` and `-aux-target-feature` unless
6059 // `--gpu-use-aux-triple-only` is specified.
6060 if (!Args.getLastArg(Ids: options::OPT_gpu_use_aux_triple_only) &&
6061 (IsCudaDevice || IsHIPDevice || IsSYCLDevice)) {
6062 const ArgList &HostArgs =
6063 C.getArgsForToolChain(TC: nullptr, BoundArch: StringRef(), DeviceOffloadKind: Action::OFK_None);
6064 std::string HostCPU =
6065 getCPUName(D, Args: HostArgs, T: *TC.getAuxTriple(), /*FromAs*/ false);
6066 if (!HostCPU.empty()) {
6067 CmdArgs.push_back(Elt: "-aux-target-cpu");
6068 CmdArgs.push_back(Elt: Args.MakeArgString(Str: HostCPU));
6069 }
6070 getTargetFeatures(D, Triple: *TC.getAuxTriple(), Args: HostArgs, CmdArgs,
6071 /*ForAS*/ false, /*IsAux*/ true);
6072 }
6073
6074 TC.addClangTargetOptions(DriverArgs: Args, CC1Args&: CmdArgs, DeviceOffloadKind: JA.getOffloadingDeviceKind());
6075
6076 addMCModel(D, Args, Triple, RelocationModel, CmdArgs);
6077
6078 if (Arg *A = Args.getLastArg(Ids: options::OPT_mtls_size_EQ)) {
6079 StringRef Value = A->getValue();
6080 unsigned TLSSize = 0;
6081 Value.getAsInteger(Radix: 10, Result&: TLSSize);
6082 if (!Triple.isAArch64() || !Triple.isOSBinFormatELF())
6083 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
6084 << A->getOption().getName() << TripleStr;
6085 if (TLSSize != 12 && TLSSize != 24 && TLSSize != 32 && TLSSize != 48)
6086 D.Diag(DiagID: diag::err_drv_invalid_int_value)
6087 << A->getOption().getName() << Value;
6088 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_mtls_size_EQ);
6089 }
6090
6091 if (isTLSDESCEnabled(TC, Args))
6092 CmdArgs.push_back(Elt: "-enable-tlsdesc");
6093
6094 // Add the target cpu
6095 std::string CPU = getCPUName(D, Args, T: Triple, /*FromAs*/ false);
6096 if (!CPU.empty()) {
6097 CmdArgs.push_back(Elt: "-target-cpu");
6098 CmdArgs.push_back(Elt: Args.MakeArgString(Str: CPU));
6099 }
6100
6101 RenderTargetOptions(EffectiveTriple: Triple, Args, KernelOrKext, CmdArgs);
6102
6103 // Add clang-cl arguments.
6104 types::ID InputType = Input.getType();
6105 if (D.IsCLMode())
6106 AddClangCLArgs(Args, InputType, CmdArgs);
6107
6108 llvm::codegenoptions::DebugInfoKind DebugInfoKind =
6109 llvm::codegenoptions::NoDebugInfo;
6110 DwarfFissionKind DwarfFission = DwarfFissionKind::None;
6111 renderDebugOptions(TC, D, T: RawTriple, Args, IRInput: types::isLLVMIR(Id: InputType),
6112 CmdArgs, Output, DebugInfoKind, DwarfFission);
6113
6114 // Add the split debug info name to the command lines here so we
6115 // can propagate it to the backend.
6116 bool SplitDWARF = (DwarfFission != DwarfFissionKind::None) &&
6117 (TC.getTriple().isOSBinFormatELF() ||
6118 TC.getTriple().isOSBinFormatWasm() ||
6119 TC.getTriple().isOSBinFormatCOFF()) &&
6120 (isa<AssembleJobAction>(Val: JA) || isa<CompileJobAction>(Val: JA) ||
6121 isa<BackendJobAction>(Val: JA));
6122 if (SplitDWARF) {
6123 const char *SplitDWARFOut = SplitDebugName(JA, Args, Input, Output);
6124 CmdArgs.push_back(Elt: "-split-dwarf-file");
6125 CmdArgs.push_back(Elt: SplitDWARFOut);
6126 if (DwarfFission == DwarfFissionKind::Split) {
6127 CmdArgs.push_back(Elt: "-split-dwarf-output");
6128 CmdArgs.push_back(Elt: SplitDWARFOut);
6129 }
6130 }
6131
6132 // Pass the linker version in use.
6133 if (Arg *A = Args.getLastArg(Ids: options::OPT_mlinker_version_EQ)) {
6134 CmdArgs.push_back(Elt: "-target-linker-version");
6135 CmdArgs.push_back(Elt: A->getValue());
6136 }
6137
6138 // Explicitly error on some things we know we don't support and can't just
6139 // ignore.
6140 if (!Args.hasArg(Ids: options::OPT_fallow_unsupported)) {
6141 Arg *Unsupported;
6142 if (types::isCXX(Id: InputType) && RawTriple.isOSDarwin() &&
6143 TC.getArch() == llvm::Triple::x86) {
6144 if ((Unsupported = Args.getLastArg(Ids: options::OPT_fapple_kext)) ||
6145 (Unsupported = Args.getLastArg(Ids: options::OPT_mkernel)))
6146 D.Diag(DiagID: diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
6147 << Unsupported->getOption().getName();
6148 }
6149 // The faltivec option has been superseded by the maltivec option.
6150 if ((Unsupported = Args.getLastArg(Ids: options::OPT_faltivec)))
6151 D.Diag(DiagID: diag::err_drv_clang_unsupported_opt_faltivec)
6152 << Unsupported->getOption().getName()
6153 << "please use -maltivec and include altivec.h explicitly";
6154 if ((Unsupported = Args.getLastArg(Ids: options::OPT_fno_altivec)))
6155 D.Diag(DiagID: diag::err_drv_clang_unsupported_opt_faltivec)
6156 << Unsupported->getOption().getName() << "please use -mno-altivec";
6157 }
6158
6159 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_v);
6160
6161 if (Args.getLastArg(Ids: options::OPT_H)) {
6162 CmdArgs.push_back(Elt: "-H");
6163 CmdArgs.push_back(Elt: "-sys-header-deps");
6164 }
6165 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fshow_skipped_includes);
6166
6167 if (D.CCPrintHeadersFormat && !D.CCGenDiagnostics) {
6168 CmdArgs.push_back(Elt: "-header-include-file");
6169 CmdArgs.push_back(Elt: !D.CCPrintHeadersFilename.empty()
6170 ? D.CCPrintHeadersFilename.c_str()
6171 : "-");
6172 CmdArgs.push_back(Elt: "-sys-header-deps");
6173 CmdArgs.push_back(Elt: Args.MakeArgString(
6174 Str: "-header-include-format=" +
6175 std::string(headerIncludeFormatKindToString(K: D.CCPrintHeadersFormat))));
6176 CmdArgs.push_back(
6177 Elt: Args.MakeArgString(Str: "-header-include-filtering=" +
6178 std::string(headerIncludeFilteringKindToString(
6179 K: D.CCPrintHeadersFiltering))));
6180 }
6181 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_P);
6182 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_print_ivar_layout);
6183
6184 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
6185 CmdArgs.push_back(Elt: "-diagnostic-log-file");
6186 CmdArgs.push_back(Elt: !D.CCLogDiagnosticsFilename.empty()
6187 ? D.CCLogDiagnosticsFilename.c_str()
6188 : "-");
6189 }
6190
6191 // Give the gen diagnostics more chances to succeed, by avoiding intentional
6192 // crashes.
6193 if (D.CCGenDiagnostics)
6194 CmdArgs.push_back(Elt: "-disable-pragma-debug-crash");
6195
6196 // Allow backend to put its diagnostic files in the same place as frontend
6197 // crash diagnostics files.
6198 if (Args.hasArg(Ids: options::OPT_fcrash_diagnostics_dir)) {
6199 StringRef Dir = Args.getLastArgValue(Id: options::OPT_fcrash_diagnostics_dir);
6200 CmdArgs.push_back(Elt: "-mllvm");
6201 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-crash-diagnostics-dir=" + Dir));
6202 }
6203
6204 bool UseSeparateSections = isUseSeparateSections(Triple);
6205
6206 if (Args.hasFlag(Pos: options::OPT_ffunction_sections,
6207 Neg: options::OPT_fno_function_sections, Default: UseSeparateSections)) {
6208 CmdArgs.push_back(Elt: "-ffunction-sections");
6209 }
6210
6211 if (Arg *A = Args.getLastArg(Ids: options::OPT_fbasic_block_address_map,
6212 Ids: options::OPT_fno_basic_block_address_map)) {
6213 if ((Triple.isX86() || Triple.isAArch64()) && Triple.isOSBinFormatELF()) {
6214 if (A->getOption().matches(ID: options::OPT_fbasic_block_address_map))
6215 A->render(Args, Output&: CmdArgs);
6216 } else {
6217 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
6218 << A->getAsString(Args) << TripleStr;
6219 }
6220 }
6221
6222 if (Arg *A = Args.getLastArg(Ids: options::OPT_fbasic_block_sections_EQ)) {
6223 StringRef Val = A->getValue();
6224 if (Val == "labels") {
6225 D.Diag(DiagID: diag::warn_drv_deprecated_arg)
6226 << A->getAsString(Args) << /*hasReplacement=*/true
6227 << "-fbasic-block-address-map";
6228 CmdArgs.push_back(Elt: "-fbasic-block-address-map");
6229 } else if (Triple.isX86() && Triple.isOSBinFormatELF()) {
6230 if (Val != "all" && Val != "none" && !Val.starts_with(Prefix: "list="))
6231 D.Diag(DiagID: diag::err_drv_invalid_value)
6232 << A->getAsString(Args) << A->getValue();
6233 else
6234 A->render(Args, Output&: CmdArgs);
6235 } else if (Triple.isAArch64() && Triple.isOSBinFormatELF()) {
6236 // "all" is not supported on AArch64 since branch relaxation creates new
6237 // basic blocks for some cross-section branches.
6238 if (Val != "labels" && Val != "none" && !Val.starts_with(Prefix: "list="))
6239 D.Diag(DiagID: diag::err_drv_invalid_value)
6240 << A->getAsString(Args) << A->getValue();
6241 else
6242 A->render(Args, Output&: CmdArgs);
6243 } else if (Triple.isNVPTX()) {
6244 // Do not pass the option to the GPU compilation. We still want it enabled
6245 // for the host-side compilation, so seeing it here is not an error.
6246 } else if (Val != "none") {
6247 // =none is allowed everywhere. It's useful for overriding the option
6248 // and is the same as not specifying the option.
6249 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
6250 << A->getAsString(Args) << TripleStr;
6251 }
6252 }
6253
6254 bool HasDefaultDataSections = Triple.isOSBinFormatXCOFF();
6255 if (Args.hasFlag(Pos: options::OPT_fdata_sections, Neg: options::OPT_fno_data_sections,
6256 Default: UseSeparateSections || HasDefaultDataSections)) {
6257 CmdArgs.push_back(Elt: "-fdata-sections");
6258 }
6259
6260 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_funique_section_names,
6261 Neg: options::OPT_fno_unique_section_names);
6262 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fseparate_named_sections,
6263 Neg: options::OPT_fno_separate_named_sections);
6264 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_funique_internal_linkage_names,
6265 Neg: options::OPT_fno_unique_internal_linkage_names);
6266 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_funique_basic_block_section_names,
6267 Neg: options::OPT_fno_unique_basic_block_section_names);
6268
6269 if (Arg *A = Args.getLastArg(Ids: options::OPT_fsplit_machine_functions,
6270 Ids: options::OPT_fno_split_machine_functions)) {
6271 if (!A->getOption().matches(ID: options::OPT_fno_split_machine_functions)) {
6272 // This codegen pass is only available on x86 and AArch64 ELF targets.
6273 if ((Triple.isX86() || Triple.isAArch64()) && Triple.isOSBinFormatELF())
6274 A->render(Args, Output&: CmdArgs);
6275 else
6276 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
6277 << A->getAsString(Args) << TripleStr;
6278 }
6279 }
6280
6281 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_finstrument_functions,
6282 Ids: options::OPT_finstrument_functions_after_inlining,
6283 Ids: options::OPT_finstrument_function_entry_bare);
6284 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fconvergent_functions,
6285 Ids: options::OPT_fno_convergent_functions);
6286
6287 // NVPTX doesn't support PGO or coverage
6288 if (!Triple.isNVPTX())
6289 addPGOAndCoverageFlags(TC, C, JA, Output, Args, SanArgs&: SanitizeArgs, CmdArgs);
6290
6291 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fclang_abi_compat_EQ);
6292
6293 if (getLastProfileSampleUseArg(Args) &&
6294 Args.hasFlag(Pos: options::OPT_fsample_profile_use_profi,
6295 Neg: options::OPT_fno_sample_profile_use_profi, Default: false)) {
6296 CmdArgs.push_back(Elt: "-mllvm");
6297 CmdArgs.push_back(Elt: "-sample-profile-use-profi");
6298 }
6299
6300 // Add runtime flag for PS4/PS5 when PGO, coverage, or sanitizers are enabled.
6301 if (RawTriple.isPS() &&
6302 !Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) {
6303 PScpu::addProfileRTArgs(TC, Args, CmdArgs);
6304 PScpu::addSanitizerArgs(TC, Args, CmdArgs);
6305 }
6306
6307 // Pass options for controlling the default header search paths.
6308 if (Args.hasArg(Ids: options::OPT_nostdinc)) {
6309 CmdArgs.push_back(Elt: "-nostdsysteminc");
6310 CmdArgs.push_back(Elt: "-nobuiltininc");
6311 } else {
6312 if (Args.hasArg(Ids: options::OPT_nostdlibinc))
6313 CmdArgs.push_back(Elt: "-nostdsysteminc");
6314 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_nostdincxx);
6315 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_nobuiltininc);
6316 }
6317
6318 // Pass the path to compiler resource files.
6319 CmdArgs.push_back(Elt: "-resource-dir");
6320 CmdArgs.push_back(Elt: D.ResourceDir.c_str());
6321
6322 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_working_directory);
6323
6324 // Add preprocessing options like -I, -D, etc. if we are using the
6325 // preprocessor.
6326 //
6327 // FIXME: Support -fpreprocessed
6328 if (types::getPreprocessedType(Id: InputType) != types::TY_INVALID)
6329 AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
6330
6331 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
6332 // that "The compiler can only warn and ignore the option if not recognized".
6333 // When building with ccache, it will pass -D options to clang even on
6334 // preprocessed inputs and configure concludes that -fPIC is not supported.
6335 Args.ClaimAllArgs(Id0: options::OPT_D);
6336
6337 // Warn about ignored options to clang.
6338 for (const Arg *A :
6339 Args.filtered(Ids: options::OPT_clang_ignored_gcc_optimization_f_Group)) {
6340 D.Diag(DiagID: diag::warn_ignored_gcc_optimization) << A->getAsString(Args);
6341 A->claim();
6342 }
6343
6344 for (const Arg *A :
6345 Args.filtered(Ids: options::OPT_clang_ignored_legacy_options_Group)) {
6346 D.Diag(DiagID: diag::warn_ignored_clang_option) << A->getAsString(Args);
6347 A->claim();
6348 }
6349
6350 claimNoWarnArgs(Args);
6351
6352 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_R_Group);
6353
6354 for (const Arg *A :
6355 Args.filtered(Ids: options::OPT_W_Group, Ids: options::OPT__SLASH_wd)) {
6356 A->claim();
6357 if (A->getOption().getID() == options::OPT__SLASH_wd) {
6358 unsigned WarningNumber;
6359 if (StringRef(A->getValue()).getAsInteger(Radix: 10, Result&: WarningNumber)) {
6360 D.Diag(DiagID: diag::err_drv_invalid_int_value)
6361 << A->getAsString(Args) << A->getValue();
6362 continue;
6363 }
6364
6365 if (auto Group = diagGroupFromCLWarningID(WarningNumber)) {
6366 CmdArgs.push_back(Elt: Args.MakeArgString(
6367 Str: "-Wno-" + DiagnosticIDs::getWarningOptionForGroup(*Group)));
6368 }
6369 continue;
6370 }
6371 A->render(Args, Output&: CmdArgs);
6372 }
6373
6374 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_Wsystem_headers_in_module_EQ);
6375
6376 if (Args.hasFlag(Pos: options::OPT_pedantic, Neg: options::OPT_no_pedantic, Default: false))
6377 CmdArgs.push_back(Elt: "-pedantic");
6378 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_pedantic_errors);
6379 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_w);
6380
6381 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_ffixed_point,
6382 Neg: options::OPT_fno_fixed_point);
6383
6384 if (Arg *A = Args.getLastArg(Ids: options::OPT_fcxx_abi_EQ))
6385 A->render(Args, Output&: CmdArgs);
6386
6387 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fexperimental_relative_cxx_abi_vtables,
6388 Ids: options::OPT_fno_experimental_relative_cxx_abi_vtables);
6389
6390 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fexperimental_omit_vtable_rtti,
6391 Ids: options::OPT_fno_experimental_omit_vtable_rtti);
6392
6393 if (Arg *A = Args.getLastArg(Ids: options::OPT_ffuchsia_api_level_EQ))
6394 A->render(Args, Output&: CmdArgs);
6395
6396 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
6397 // (-ansi is equivalent to -std=c89 or -std=c++98).
6398 //
6399 // If a std is supplied, only add -trigraphs if it follows the
6400 // option.
6401 bool ImplyVCPPCVer = false;
6402 bool ImplyVCPPCXXVer = false;
6403 const Arg *Std = Args.getLastArg(Ids: options::OPT_std_EQ, Ids: options::OPT_ansi);
6404 if (Std) {
6405 if (Std->getOption().matches(ID: options::OPT_ansi))
6406 if (types::isCXX(Id: InputType))
6407 CmdArgs.push_back(Elt: "-std=c++98");
6408 else
6409 CmdArgs.push_back(Elt: "-std=c89");
6410 else
6411 Std->render(Args, Output&: CmdArgs);
6412
6413 // If -f(no-)trigraphs appears after the language standard flag, honor it.
6414 if (Arg *A = Args.getLastArg(Ids: options::OPT_std_EQ, Ids: options::OPT_ansi,
6415 Ids: options::OPT_ftrigraphs,
6416 Ids: options::OPT_fno_trigraphs))
6417 if (A != Std)
6418 A->render(Args, Output&: CmdArgs);
6419 } else {
6420 // Honor -std-default.
6421 //
6422 // FIXME: Clang doesn't correctly handle -std= when the input language
6423 // doesn't match. For the time being just ignore this for C++ inputs;
6424 // eventually we want to do all the standard defaulting here instead of
6425 // splitting it between the driver and clang -cc1.
6426 if (!types::isCXX(Id: InputType)) {
6427 if (!Args.hasArg(Ids: options::OPT__SLASH_std)) {
6428 Args.AddAllArgsTranslated(Output&: CmdArgs, Id0: options::OPT_std_default_EQ, Translation: "-std=",
6429 /*Joined=*/true);
6430 } else
6431 ImplyVCPPCVer = true;
6432 }
6433 else if (IsWindowsMSVC)
6434 ImplyVCPPCXXVer = true;
6435
6436 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ftrigraphs,
6437 Ids: options::OPT_fno_trigraphs);
6438 }
6439
6440 // GCC's behavior for -Wwrite-strings is a bit strange:
6441 // * In C, this "warning flag" changes the types of string literals from
6442 // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning
6443 // for the discarded qualifier.
6444 // * In C++, this is just a normal warning flag.
6445 //
6446 // Implementing this warning correctly in C is hard, so we follow GCC's
6447 // behavior for now. FIXME: Directly diagnose uses of a string literal as
6448 // a non-const char* in C, rather than using this crude hack.
6449 if (!types::isCXX(Id: InputType)) {
6450 // FIXME: This should behave just like a warning flag, and thus should also
6451 // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on.
6452 Arg *WriteStrings =
6453 Args.getLastArg(Ids: options::OPT_Wwrite_strings,
6454 Ids: options::OPT_Wno_write_strings, Ids: options::OPT_w);
6455 if (WriteStrings &&
6456 WriteStrings->getOption().matches(ID: options::OPT_Wwrite_strings))
6457 CmdArgs.push_back(Elt: "-fconst-strings");
6458 }
6459
6460 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
6461 // during C++ compilation, which it is by default. GCC keeps this define even
6462 // in the presence of '-w', match this behavior bug-for-bug.
6463 if (types::isCXX(Id: InputType) &&
6464 Args.hasFlag(Pos: options::OPT_Wdeprecated, Neg: options::OPT_Wno_deprecated,
6465 Default: true)) {
6466 CmdArgs.push_back(Elt: "-fdeprecated-macro");
6467 }
6468
6469 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
6470 if (Arg *Asm = Args.getLastArg(Ids: options::OPT_fasm, Ids: options::OPT_fno_asm)) {
6471 if (Asm->getOption().matches(ID: options::OPT_fasm))
6472 CmdArgs.push_back(Elt: "-fgnu-keywords");
6473 else
6474 CmdArgs.push_back(Elt: "-fno-gnu-keywords");
6475 }
6476
6477 if (!ShouldEnableAutolink(Args, TC, JA))
6478 CmdArgs.push_back(Elt: "-fno-autolink");
6479
6480 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ftemplate_depth_EQ);
6481 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_foperator_arrow_depth_EQ);
6482 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fconstexpr_depth_EQ);
6483 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fconstexpr_steps_EQ);
6484
6485 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fexperimental_library);
6486
6487 if (Args.hasArg(Ids: options::OPT_fexperimental_new_constant_interpreter))
6488 CmdArgs.push_back(Elt: "-fexperimental-new-constant-interpreter");
6489
6490 if (Arg *A = Args.getLastArg(Ids: options::OPT_fbracket_depth_EQ)) {
6491 CmdArgs.push_back(Elt: "-fbracket-depth");
6492 CmdArgs.push_back(Elt: A->getValue());
6493 }
6494
6495 if (Arg *A = Args.getLastArg(Ids: options::OPT_Wlarge_by_value_copy_EQ,
6496 Ids: options::OPT_Wlarge_by_value_copy_def)) {
6497 if (A->getNumValues()) {
6498 StringRef bytes = A->getValue();
6499 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-Wlarge-by-value-copy=" + bytes));
6500 } else
6501 CmdArgs.push_back(Elt: "-Wlarge-by-value-copy=64"); // default value
6502 }
6503
6504 if (Args.hasArg(Ids: options::OPT_relocatable_pch))
6505 CmdArgs.push_back(Elt: "-relocatable-pch");
6506
6507 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fcf_runtime_abi_EQ)) {
6508 static const char *kCFABIs[] = {
6509 "standalone", "objc", "swift", "swift-5.0", "swift-4.2", "swift-4.1",
6510 };
6511
6512 if (!llvm::is_contained(Range&: kCFABIs, Element: StringRef(A->getValue())))
6513 D.Diag(DiagID: diag::err_drv_invalid_cf_runtime_abi) << A->getValue();
6514 else
6515 A->render(Args, Output&: CmdArgs);
6516 }
6517
6518 if (Arg *A = Args.getLastArg(Ids: options::OPT_fconstant_string_class_EQ)) {
6519 CmdArgs.push_back(Elt: "-fconstant-string-class");
6520 CmdArgs.push_back(Elt: A->getValue());
6521 }
6522
6523 if (Arg *A = Args.getLastArg(Ids: options::OPT_ftabstop_EQ)) {
6524 CmdArgs.push_back(Elt: "-ftabstop");
6525 CmdArgs.push_back(Elt: A->getValue());
6526 }
6527
6528 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fstack_size_section,
6529 Neg: options::OPT_fno_stack_size_section);
6530
6531 if (Args.hasArg(Ids: options::OPT_fstack_usage)) {
6532 CmdArgs.push_back(Elt: "-stack-usage-file");
6533
6534 if (Arg *OutputOpt = Args.getLastArg(Ids: options::OPT_o)) {
6535 SmallString<128> OutputFilename(OutputOpt->getValue());
6536 llvm::sys::path::replace_extension(path&: OutputFilename, extension: "su");
6537 CmdArgs.push_back(Elt: Args.MakeArgString(Str: OutputFilename));
6538 } else
6539 CmdArgs.push_back(
6540 Elt: Args.MakeArgString(Str: Twine(getBaseInputStem(Args, Inputs)) + ".su"));
6541 }
6542
6543 CmdArgs.push_back(Elt: "-ferror-limit");
6544 if (Arg *A = Args.getLastArg(Ids: options::OPT_ferror_limit_EQ))
6545 CmdArgs.push_back(Elt: A->getValue());
6546 else
6547 CmdArgs.push_back(Elt: "19");
6548
6549 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fconstexpr_backtrace_limit_EQ);
6550 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fmacro_backtrace_limit_EQ);
6551 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ftemplate_backtrace_limit_EQ);
6552 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fspell_checking_limit_EQ);
6553 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fcaret_diagnostics_max_lines_EQ);
6554
6555 // Pass -fmessage-length=.
6556 unsigned MessageLength = 0;
6557 if (Arg *A = Args.getLastArg(Ids: options::OPT_fmessage_length_EQ)) {
6558 StringRef V(A->getValue());
6559 if (V.getAsInteger(Radix: 0, Result&: MessageLength))
6560 D.Diag(DiagID: diag::err_drv_invalid_argument_to_option)
6561 << V << A->getOption().getName();
6562 } else {
6563 // If -fmessage-length=N was not specified, determine whether this is a
6564 // terminal and, if so, implicitly define -fmessage-length appropriately.
6565 MessageLength = llvm::sys::Process::StandardErrColumns();
6566 }
6567 if (MessageLength != 0)
6568 CmdArgs.push_back(
6569 Elt: Args.MakeArgString(Str: "-fmessage-length=" + Twine(MessageLength)));
6570
6571 if (Arg *A = Args.getLastArg(Ids: options::OPT_frandomize_layout_seed_EQ))
6572 CmdArgs.push_back(
6573 Elt: Args.MakeArgString(Str: "-frandomize-layout-seed=" + Twine(A->getValue(N: 0))));
6574
6575 if (Arg *A = Args.getLastArg(Ids: options::OPT_frandomize_layout_seed_file_EQ))
6576 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-frandomize-layout-seed-file=" +
6577 Twine(A->getValue(N: 0))));
6578
6579 // -fvisibility= and -fvisibility-ms-compat are of a piece.
6580 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fvisibility_EQ,
6581 Ids: options::OPT_fvisibility_ms_compat)) {
6582 if (A->getOption().matches(ID: options::OPT_fvisibility_EQ)) {
6583 A->render(Args, Output&: CmdArgs);
6584 } else {
6585 assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
6586 CmdArgs.push_back(Elt: "-fvisibility=hidden");
6587 CmdArgs.push_back(Elt: "-ftype-visibility=default");
6588 }
6589 } else if (IsOpenMPDevice) {
6590 // When compiling for the OpenMP device we want protected visibility by
6591 // default. This prevents the device from accidentally preempting code on
6592 // the host, makes the system more robust, and improves performance.
6593 CmdArgs.push_back(Elt: "-fvisibility=protected");
6594 }
6595
6596 // PS4/PS5 process these options in addClangTargetOptions.
6597 if (!RawTriple.isPS()) {
6598 if (const Arg *A =
6599 Args.getLastArg(Ids: options::OPT_fvisibility_from_dllstorageclass,
6600 Ids: options::OPT_fno_visibility_from_dllstorageclass)) {
6601 if (A->getOption().matches(
6602 ID: options::OPT_fvisibility_from_dllstorageclass)) {
6603 CmdArgs.push_back(Elt: "-fvisibility-from-dllstorageclass");
6604 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fvisibility_dllexport_EQ);
6605 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fvisibility_nodllstorageclass_EQ);
6606 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fvisibility_externs_dllimport_EQ);
6607 Args.AddLastArg(Output&: CmdArgs,
6608 Ids: options::OPT_fvisibility_externs_nodllstorageclass_EQ);
6609 }
6610 }
6611 }
6612
6613 if (Args.hasFlag(Pos: options::OPT_fvisibility_inlines_hidden,
6614 Neg: options::OPT_fno_visibility_inlines_hidden, Default: false))
6615 CmdArgs.push_back(Elt: "-fvisibility-inlines-hidden");
6616
6617 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fvisibility_inlines_hidden_static_local_var,
6618 Ids: options::OPT_fno_visibility_inlines_hidden_static_local_var);
6619
6620 // -fvisibility-global-new-delete-hidden is a deprecated spelling of
6621 // -fvisibility-global-new-delete=force-hidden.
6622 if (const Arg *A =
6623 Args.getLastArg(Ids: options::OPT_fvisibility_global_new_delete_hidden)) {
6624 D.Diag(DiagID: diag::warn_drv_deprecated_arg)
6625 << A->getAsString(Args) << /*hasReplacement=*/true
6626 << "-fvisibility-global-new-delete=force-hidden";
6627 }
6628
6629 if (const Arg *A =
6630 Args.getLastArg(Ids: options::OPT_fvisibility_global_new_delete_EQ,
6631 Ids: options::OPT_fvisibility_global_new_delete_hidden)) {
6632 if (A->getOption().matches(ID: options::OPT_fvisibility_global_new_delete_EQ)) {
6633 A->render(Args, Output&: CmdArgs);
6634 } else {
6635 assert(A->getOption().matches(
6636 options::OPT_fvisibility_global_new_delete_hidden));
6637 CmdArgs.push_back(Elt: "-fvisibility-global-new-delete=force-hidden");
6638 }
6639 }
6640
6641 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ftlsmodel_EQ);
6642
6643 if (Args.hasFlag(Pos: options::OPT_fnew_infallible,
6644 Neg: options::OPT_fno_new_infallible, Default: false))
6645 CmdArgs.push_back(Elt: "-fnew-infallible");
6646
6647 if (Args.hasFlag(Pos: options::OPT_fno_operator_names,
6648 Neg: options::OPT_foperator_names, Default: false))
6649 CmdArgs.push_back(Elt: "-fno-operator-names");
6650
6651 // Forward -f (flag) options which we can pass directly.
6652 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_femit_all_decls);
6653 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fheinous_gnu_extensions);
6654 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fdigraphs, Ids: options::OPT_fno_digraphs);
6655 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fzero_call_used_regs_EQ);
6656 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fraw_string_literals,
6657 Ids: options::OPT_fno_raw_string_literals);
6658
6659 if (Args.hasFlag(Pos: options::OPT_femulated_tls, Neg: options::OPT_fno_emulated_tls,
6660 Default: Triple.hasDefaultEmulatedTLS()))
6661 CmdArgs.push_back(Elt: "-femulated-tls");
6662
6663 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fcheck_new,
6664 Neg: options::OPT_fno_check_new);
6665
6666 if (Arg *A = Args.getLastArg(Ids: options::OPT_fzero_call_used_regs_EQ)) {
6667 // FIXME: There's no reason for this to be restricted to X86. The backend
6668 // code needs to be changed to include the appropriate function calls
6669 // automatically.
6670 if (!Triple.isX86() && !Triple.isAArch64())
6671 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
6672 << A->getAsString(Args) << TripleStr;
6673 }
6674
6675 // AltiVec-like language extensions aren't relevant for assembling.
6676 if (!isa<PreprocessJobAction>(Val: JA) || Output.getType() != types::TY_PP_Asm)
6677 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fzvector);
6678
6679 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fdiagnostics_show_template_tree);
6680 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fno_elide_type);
6681
6682 // Forward flags for OpenMP. We don't do this if the current action is an
6683 // device offloading action other than OpenMP.
6684 if (Args.hasFlag(Pos: options::OPT_fopenmp, PosAlias: options::OPT_fopenmp_EQ,
6685 Neg: options::OPT_fno_openmp, Default: false) &&
6686 !Args.hasFlag(Pos: options::OPT_foffload_via_llvm,
6687 Neg: options::OPT_fno_offload_via_llvm, Default: false) &&
6688 (JA.isDeviceOffloading(OKind: Action::OFK_None) ||
6689 JA.isDeviceOffloading(OKind: Action::OFK_OpenMP))) {
6690 switch (D.getOpenMPRuntime(Args)) {
6691 case Driver::OMPRT_OMP:
6692 case Driver::OMPRT_IOMP5:
6693 // Clang can generate useful OpenMP code for these two runtime libraries.
6694 CmdArgs.push_back(Elt: "-fopenmp");
6695
6696 // If no option regarding the use of TLS in OpenMP codegeneration is
6697 // given, decide a default based on the target. Otherwise rely on the
6698 // options and pass the right information to the frontend.
6699 if (!Args.hasFlag(Pos: options::OPT_fopenmp_use_tls,
6700 Neg: options::OPT_fnoopenmp_use_tls, /*Default=*/true))
6701 CmdArgs.push_back(Elt: "-fnoopenmp-use-tls");
6702 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fopenmp_simd,
6703 Ids: options::OPT_fno_openmp_simd);
6704 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fopenmp_enable_irbuilder);
6705 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fopenmp_version_EQ);
6706 if (!Args.hasFlag(Pos: options::OPT_fopenmp_extensions,
6707 Neg: options::OPT_fno_openmp_extensions, /*Default=*/true))
6708 CmdArgs.push_back(Elt: "-fno-openmp-extensions");
6709 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fopenmp_cuda_number_of_sm_EQ);
6710 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fopenmp_cuda_blocks_per_sm_EQ);
6711 Args.AddAllArgs(Output&: CmdArgs,
6712 Id0: options::OPT_fopenmp_cuda_teams_reduction_recs_num_EQ);
6713 if (Args.hasFlag(Pos: options::OPT_fopenmp_optimistic_collapse,
6714 Neg: options::OPT_fno_openmp_optimistic_collapse,
6715 /*Default=*/false))
6716 CmdArgs.push_back(Elt: "-fopenmp-optimistic-collapse");
6717
6718 // When in OpenMP offloading mode with NVPTX target, forward
6719 // cuda-mode flag
6720 if (Args.hasFlag(Pos: options::OPT_fopenmp_cuda_mode,
6721 Neg: options::OPT_fno_openmp_cuda_mode, /*Default=*/false))
6722 CmdArgs.push_back(Elt: "-fopenmp-cuda-mode");
6723
6724 // When in OpenMP offloading mode, enable debugging on the device.
6725 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fopenmp_target_debug_EQ);
6726 if (Args.hasFlag(Pos: options::OPT_fopenmp_target_debug,
6727 Neg: options::OPT_fno_openmp_target_debug, /*Default=*/false))
6728 CmdArgs.push_back(Elt: "-fopenmp-target-debug");
6729
6730 // When in OpenMP offloading mode, forward assumptions information about
6731 // thread and team counts in the device.
6732 if (Args.hasFlag(Pos: options::OPT_fopenmp_assume_teams_oversubscription,
6733 Neg: options::OPT_fno_openmp_assume_teams_oversubscription,
6734 /*Default=*/false))
6735 CmdArgs.push_back(Elt: "-fopenmp-assume-teams-oversubscription");
6736 if (Args.hasFlag(Pos: options::OPT_fopenmp_assume_threads_oversubscription,
6737 Neg: options::OPT_fno_openmp_assume_threads_oversubscription,
6738 /*Default=*/false))
6739 CmdArgs.push_back(Elt: "-fopenmp-assume-threads-oversubscription");
6740 if (Args.hasArg(Ids: options::OPT_fopenmp_assume_no_thread_state))
6741 CmdArgs.push_back(Elt: "-fopenmp-assume-no-thread-state");
6742 if (Args.hasArg(Ids: options::OPT_fopenmp_assume_no_nested_parallelism))
6743 CmdArgs.push_back(Elt: "-fopenmp-assume-no-nested-parallelism");
6744 if (Args.hasArg(Ids: options::OPT_fopenmp_offload_mandatory))
6745 CmdArgs.push_back(Elt: "-fopenmp-offload-mandatory");
6746 if (Args.hasArg(Ids: options::OPT_fopenmp_force_usm))
6747 CmdArgs.push_back(Elt: "-fopenmp-force-usm");
6748 break;
6749 default:
6750 // By default, if Clang doesn't know how to generate useful OpenMP code
6751 // for a specific runtime library, we just don't pass the '-fopenmp' flag
6752 // down to the actual compilation.
6753 // FIXME: It would be better to have a mode which *only* omits IR
6754 // generation based on the OpenMP support so that we get consistent
6755 // semantic analysis, etc.
6756 break;
6757 }
6758 } else {
6759 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fopenmp_simd,
6760 Ids: options::OPT_fno_openmp_simd);
6761 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fopenmp_version_EQ);
6762 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fopenmp_extensions,
6763 Neg: options::OPT_fno_openmp_extensions);
6764 }
6765 // Forward the offload runtime change to code generation, liboffload implies
6766 // new driver. Otherwise, check if we should forward the new driver to change
6767 // offloading code generation.
6768 if (Args.hasFlag(Pos: options::OPT_foffload_via_llvm,
6769 Neg: options::OPT_fno_offload_via_llvm, Default: false)) {
6770 CmdArgs.append(IL: {"--offload-new-driver", "-foffload-via-llvm"});
6771 } else if (Args.hasFlag(Pos: options::OPT_offload_new_driver,
6772 Neg: options::OPT_no_offload_new_driver,
6773 Default: C.isOffloadingHostKind(Kind: Action::OFK_Cuda))) {
6774 CmdArgs.push_back(Elt: "--offload-new-driver");
6775 }
6776
6777 const XRayArgs &XRay = TC.getXRayArgs(Args);
6778 XRay.addArgs(TC, Args, CmdArgs, InputType);
6779
6780 for (const auto &Filename :
6781 Args.getAllArgValues(Id: options::OPT_fprofile_list_EQ)) {
6782 if (D.getVFS().exists(Path: Filename))
6783 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fprofile-list=" + Filename));
6784 else
6785 D.Diag(DiagID: clang::diag::err_drv_no_such_file) << Filename;
6786 }
6787
6788 if (Arg *A = Args.getLastArg(Ids: options::OPT_fpatchable_function_entry_EQ)) {
6789 StringRef S0 = A->getValue(), S = S0;
6790 unsigned Size, Offset = 0;
6791 if (!Triple.isAArch64() && !Triple.isLoongArch() && !Triple.isRISCV() &&
6792 !Triple.isX86() &&
6793 !(!Triple.isOSAIX() && (Triple.getArch() == llvm::Triple::ppc ||
6794 Triple.getArch() == llvm::Triple::ppc64)))
6795 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
6796 << A->getAsString(Args) << TripleStr;
6797 else if (S.consumeInteger(Radix: 10, Result&: Size) ||
6798 (!S.empty() &&
6799 (!S.consume_front(Prefix: ",") || S.consumeInteger(Radix: 10, Result&: Offset))) ||
6800 (!S.empty() && (!S.consume_front(Prefix: ",") || S.empty())))
6801 D.Diag(DiagID: diag::err_drv_invalid_argument_to_option)
6802 << S0 << A->getOption().getName();
6803 else if (Size < Offset)
6804 D.Diag(DiagID: diag::err_drv_unsupported_fpatchable_function_entry_argument);
6805 else {
6806 CmdArgs.push_back(Elt: Args.MakeArgString(Str: A->getSpelling() + Twine(Size)));
6807 CmdArgs.push_back(Elt: Args.MakeArgString(
6808 Str: "-fpatchable-function-entry-offset=" + Twine(Offset)));
6809 if (!S.empty())
6810 CmdArgs.push_back(
6811 Elt: Args.MakeArgString(Str: "-fpatchable-function-entry-section=" + S));
6812 }
6813 }
6814
6815 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fms_hotpatch);
6816
6817 if (Args.hasArg(Ids: options::OPT_fms_secure_hotpatch_functions_file))
6818 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fms_secure_hotpatch_functions_file);
6819
6820 for (const auto &A :
6821 Args.getAllArgValues(Id: options::OPT_fms_secure_hotpatch_functions_list))
6822 CmdArgs.push_back(
6823 Elt: Args.MakeArgString(Str: "-fms-secure-hotpatch-functions-list=" + Twine(A)));
6824
6825 if (TC.SupportsProfiling()) {
6826 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_pg);
6827
6828 llvm::Triple::ArchType Arch = TC.getArch();
6829 if (Arg *A = Args.getLastArg(Ids: options::OPT_mfentry)) {
6830 if (Arch == llvm::Triple::systemz || TC.getTriple().isX86())
6831 A->render(Args, Output&: CmdArgs);
6832 else
6833 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
6834 << A->getAsString(Args) << TripleStr;
6835 }
6836 if (Arg *A = Args.getLastArg(Ids: options::OPT_mnop_mcount)) {
6837 if (Arch == llvm::Triple::systemz)
6838 A->render(Args, Output&: CmdArgs);
6839 else
6840 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
6841 << A->getAsString(Args) << TripleStr;
6842 }
6843 if (Arg *A = Args.getLastArg(Ids: options::OPT_mrecord_mcount)) {
6844 if (Arch == llvm::Triple::systemz)
6845 A->render(Args, Output&: CmdArgs);
6846 else
6847 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
6848 << A->getAsString(Args) << TripleStr;
6849 }
6850 }
6851
6852 if (Arg *A = Args.getLastArgNoClaim(Ids: options::OPT_pg)) {
6853 if (TC.getTriple().isOSzOS()) {
6854 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
6855 << A->getAsString(Args) << TripleStr;
6856 }
6857 }
6858 if (Arg *A = Args.getLastArgNoClaim(Ids: options::OPT_p)) {
6859 if (!(TC.getTriple().isOSAIX() || TC.getTriple().isOSOpenBSD())) {
6860 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
6861 << A->getAsString(Args) << TripleStr;
6862 }
6863 }
6864 if (Arg *A = Args.getLastArgNoClaim(Ids: options::OPT_p, Ids: options::OPT_pg)) {
6865 if (A->getOption().matches(ID: options::OPT_p)) {
6866 A->claim();
6867 if (TC.getTriple().isOSAIX() && !Args.hasArgNoClaim(Ids: options::OPT_pg))
6868 CmdArgs.push_back(Elt: "-pg");
6869 }
6870 }
6871
6872 // Reject AIX-specific link options on other targets.
6873 if (!TC.getTriple().isOSAIX()) {
6874 for (const Arg *A : Args.filtered(Ids: options::OPT_b, Ids: options::OPT_K,
6875 Ids: options::OPT_mxcoff_build_id_EQ)) {
6876 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
6877 << A->getSpelling() << TripleStr;
6878 }
6879 }
6880
6881 if (Args.getLastArg(Ids: options::OPT_fapple_kext) ||
6882 (Args.hasArg(Ids: options::OPT_mkernel) && types::isCXX(Id: InputType)))
6883 CmdArgs.push_back(Elt: "-fapple-kext");
6884
6885 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_altivec_src_compat);
6886 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_flax_vector_conversions_EQ);
6887 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fobjc_sender_dependent_dispatch);
6888 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fdiagnostics_print_source_range_info);
6889 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fdiagnostics_parseable_fixits);
6890 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ftime_report);
6891 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ftime_report_EQ);
6892 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ftime_report_json);
6893 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ftrapv);
6894 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_malign_double);
6895 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fno_temp_file);
6896
6897 if (const char *Name = C.getTimeTraceFile(JA: &JA)) {
6898 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-ftime-trace=" + Twine(Name)));
6899 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ftime_trace_granularity_EQ);
6900 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ftime_trace_verbose);
6901 }
6902
6903 if (Arg *A = Args.getLastArg(Ids: options::OPT_ftrapv_handler_EQ)) {
6904 CmdArgs.push_back(Elt: "-ftrapv-handler");
6905 CmdArgs.push_back(Elt: A->getValue());
6906 }
6907
6908 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ftrap_function_EQ);
6909
6910 // Handle -f[no-]wrapv and -f[no-]strict-overflow, which are used by both
6911 // clang and flang.
6912 renderCommonIntegerOverflowOptions(Args, CmdArgs);
6913
6914 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_ffinite_loops,
6915 Ids: options::OPT_fno_finite_loops);
6916
6917 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fwritable_strings);
6918 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_funroll_loops,
6919 Ids: options::OPT_fno_unroll_loops);
6920 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_floop_interchange,
6921 Ids: options::OPT_fno_loop_interchange);
6922
6923 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fstrict_flex_arrays_EQ);
6924
6925 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_pthread);
6926
6927 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_mspeculative_load_hardening,
6928 Neg: options::OPT_mno_speculative_load_hardening);
6929
6930 RenderSSPOptions(D, TC, Args, CmdArgs, KernelOrKext);
6931 RenderSCPOptions(TC, Args, CmdArgs);
6932 RenderTrivialAutoVarInitOptions(D, TC, Args, CmdArgs);
6933
6934 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fswift_async_fp_EQ);
6935
6936 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_mstackrealign,
6937 Neg: options::OPT_mno_stackrealign);
6938
6939 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mstack_alignment)) {
6940 StringRef Value = A->getValue();
6941 int64_t Alignment = 0;
6942 if (Value.getAsInteger(Radix: 10, Result&: Alignment) || Alignment < 0)
6943 D.Diag(DiagID: diag::err_drv_invalid_argument_to_option)
6944 << Value << A->getOption().getName();
6945 else if (Alignment & (Alignment - 1))
6946 D.Diag(DiagID: diag::err_drv_alignment_not_power_of_two)
6947 << A->getAsString(Args) << Value;
6948 else
6949 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mstack-alignment=" + Value));
6950 }
6951
6952 if (Args.hasArg(Ids: options::OPT_mstack_probe_size)) {
6953 StringRef Size = Args.getLastArgValue(Id: options::OPT_mstack_probe_size);
6954
6955 if (!Size.empty())
6956 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mstack-probe-size=" + Size));
6957 else
6958 CmdArgs.push_back(Elt: "-mstack-probe-size=0");
6959 }
6960
6961 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_mstack_arg_probe,
6962 Neg: options::OPT_mno_stack_arg_probe);
6963
6964 if (Arg *A = Args.getLastArg(Ids: options::OPT_mrestrict_it,
6965 Ids: options::OPT_mno_restrict_it)) {
6966 if (A->getOption().matches(ID: options::OPT_mrestrict_it)) {
6967 CmdArgs.push_back(Elt: "-mllvm");
6968 CmdArgs.push_back(Elt: "-arm-restrict-it");
6969 } else {
6970 CmdArgs.push_back(Elt: "-mllvm");
6971 CmdArgs.push_back(Elt: "-arm-default-it");
6972 }
6973 }
6974
6975 // Forward -cl options to -cc1
6976 RenderOpenCLOptions(Args, CmdArgs, InputType);
6977
6978 // Forward hlsl options to -cc1
6979 RenderHLSLOptions(Args, CmdArgs, InputType);
6980
6981 // Forward OpenACC options to -cc1
6982 RenderOpenACCOptions(D, Args, CmdArgs, InputType);
6983
6984 if (IsHIP) {
6985 if (Args.hasFlag(Pos: options::OPT_fhip_new_launch_api,
6986 Neg: options::OPT_fno_hip_new_launch_api, Default: true))
6987 CmdArgs.push_back(Elt: "-fhip-new-launch-api");
6988 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fgpu_allow_device_init,
6989 Neg: options::OPT_fno_gpu_allow_device_init);
6990 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_hipstdpar);
6991 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_hipstdpar_interpose_alloc);
6992 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fhip_kernel_arg_name,
6993 Neg: options::OPT_fno_hip_kernel_arg_name);
6994 }
6995
6996 if (IsCuda || IsHIP) {
6997 if (IsRDCMode)
6998 CmdArgs.push_back(Elt: "-fgpu-rdc");
6999 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fgpu_defer_diag,
7000 Neg: options::OPT_fno_gpu_defer_diag);
7001 if (Args.hasFlag(Pos: options::OPT_fgpu_exclude_wrong_side_overloads,
7002 Neg: options::OPT_fno_gpu_exclude_wrong_side_overloads,
7003 Default: false)) {
7004 CmdArgs.push_back(Elt: "-fgpu-exclude-wrong-side-overloads");
7005 CmdArgs.push_back(Elt: "-fgpu-defer-diag");
7006 }
7007 }
7008
7009 // Forward --no-offloadlib to -cc1.
7010 if (!Args.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib, Default: true))
7011 CmdArgs.push_back(Elt: "--no-offloadlib");
7012
7013 if (Arg *A = Args.getLastArg(Ids: options::OPT_fcf_protection_EQ)) {
7014 CmdArgs.push_back(
7015 Elt: Args.MakeArgString(Str: Twine("-fcf-protection=") + A->getValue()));
7016
7017 if (Arg *SA = Args.getLastArg(Ids: options::OPT_mcf_branch_label_scheme_EQ))
7018 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-mcf-branch-label-scheme=") +
7019 SA->getValue()));
7020 } else if (Triple.isOSOpenBSD() && Triple.getArch() == llvm::Triple::x86_64) {
7021 // Emit IBT endbr64 instructions by default
7022 CmdArgs.push_back(Elt: "-fcf-protection=branch");
7023 // jump-table can generate indirect jumps, which are not permitted
7024 CmdArgs.push_back(Elt: "-fno-jump-tables");
7025 }
7026
7027 if (Arg *A = Args.getLastArg(Ids: options::OPT_mfunction_return_EQ))
7028 CmdArgs.push_back(
7029 Elt: Args.MakeArgString(Str: Twine("-mfunction-return=") + A->getValue()));
7030
7031 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_mindirect_branch_cs_prefix);
7032
7033 // Forward -f options with positive and negative forms; we translate these by
7034 // hand. Do not propagate PGO options to the GPU-side compilations as the
7035 // profile info is for the host-side compilation only.
7036 if (!(IsCudaDevice || IsHIPDevice)) {
7037 if (Arg *A = getLastProfileSampleUseArg(Args)) {
7038 auto *PGOArg = Args.getLastArg(
7039 Ids: options::OPT_fprofile_generate, Ids: options::OPT_fprofile_generate_EQ,
7040 Ids: options::OPT_fcs_profile_generate,
7041 Ids: options::OPT_fcs_profile_generate_EQ, Ids: options::OPT_fprofile_use,
7042 Ids: options::OPT_fprofile_use_EQ);
7043 if (PGOArg)
7044 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
7045 << "SampleUse with PGO options";
7046
7047 StringRef fname = A->getValue();
7048 if (!llvm::sys::fs::exists(Path: fname))
7049 D.Diag(DiagID: diag::err_drv_no_such_file) << fname;
7050 else
7051 A->render(Args, Output&: CmdArgs);
7052 }
7053 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fprofile_remapping_file_EQ);
7054
7055 if (Args.hasFlag(Pos: options::OPT_fpseudo_probe_for_profiling,
7056 Neg: options::OPT_fno_pseudo_probe_for_profiling, Default: false)) {
7057 CmdArgs.push_back(Elt: "-fpseudo-probe-for-profiling");
7058 // Enforce -funique-internal-linkage-names if it's not explicitly turned
7059 // off.
7060 if (Args.hasFlag(Pos: options::OPT_funique_internal_linkage_names,
7061 Neg: options::OPT_fno_unique_internal_linkage_names, Default: true))
7062 CmdArgs.push_back(Elt: "-funique-internal-linkage-names");
7063 }
7064 }
7065 RenderBuiltinOptions(TC, T: RawTriple, Args, CmdArgs);
7066
7067 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fassume_sane_operator_new,
7068 Neg: options::OPT_fno_assume_sane_operator_new);
7069
7070 if (Args.hasFlag(Pos: options::OPT_fapinotes, Neg: options::OPT_fno_apinotes, Default: false))
7071 CmdArgs.push_back(Elt: "-fapinotes");
7072 if (Args.hasFlag(Pos: options::OPT_fapinotes_modules,
7073 Neg: options::OPT_fno_apinotes_modules, Default: false))
7074 CmdArgs.push_back(Elt: "-fapinotes-modules");
7075 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fapinotes_swift_version);
7076
7077 // -fblocks=0 is default.
7078 if (Args.hasFlag(Pos: options::OPT_fblocks, Neg: options::OPT_fno_blocks,
7079 Default: TC.IsBlocksDefault()) ||
7080 (Args.hasArg(Ids: options::OPT_fgnu_runtime) &&
7081 Args.hasArg(Ids: options::OPT_fobjc_nonfragile_abi) &&
7082 !Args.hasArg(Ids: options::OPT_fno_blocks))) {
7083 CmdArgs.push_back(Elt: "-fblocks");
7084
7085 if (!Args.hasArg(Ids: options::OPT_fgnu_runtime) && !TC.hasBlocksRuntime())
7086 CmdArgs.push_back(Elt: "-fblocks-runtime-optional");
7087 }
7088
7089 // -fencode-extended-block-signature=1 is default.
7090 if (TC.IsEncodeExtendedBlockSignatureDefault())
7091 CmdArgs.push_back(Elt: "-fencode-extended-block-signature");
7092
7093 if (Args.hasFlag(Pos: options::OPT_fcoro_aligned_allocation,
7094 Neg: options::OPT_fno_coro_aligned_allocation, Default: false) &&
7095 types::isCXX(Id: InputType))
7096 CmdArgs.push_back(Elt: "-fcoro-aligned-allocation");
7097
7098 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fdouble_square_bracket_attributes,
7099 Ids: options::OPT_fno_double_square_bracket_attributes);
7100
7101 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_faccess_control,
7102 Neg: options::OPT_fno_access_control);
7103 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_felide_constructors,
7104 Neg: options::OPT_fno_elide_constructors);
7105
7106 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
7107
7108 if (KernelOrKext || (types::isCXX(Id: InputType) &&
7109 (RTTIMode == ToolChain::RM_Disabled)))
7110 CmdArgs.push_back(Elt: "-fno-rtti");
7111
7112 // -fshort-enums=0 is default for all architectures except Hexagon and z/OS.
7113 if (Args.hasFlag(Pos: options::OPT_fshort_enums, Neg: options::OPT_fno_short_enums,
7114 Default: TC.getArch() == llvm::Triple::hexagon || Triple.isOSzOS()))
7115 CmdArgs.push_back(Elt: "-fshort-enums");
7116
7117 RenderCharacterOptions(Args, T: AuxTriple ? *AuxTriple : RawTriple, CmdArgs);
7118
7119 // -fuse-cxa-atexit is default.
7120 if (!Args.hasFlag(
7121 Pos: options::OPT_fuse_cxa_atexit, Neg: options::OPT_fno_use_cxa_atexit,
7122 Default: !RawTriple.isOSAIX() &&
7123 (!RawTriple.isOSWindows() ||
7124 RawTriple.isWindowsCygwinEnvironment()) &&
7125 ((RawTriple.getVendor() != llvm::Triple::MipsTechnologies) ||
7126 RawTriple.hasEnvironment())) ||
7127 KernelOrKext)
7128 CmdArgs.push_back(Elt: "-fno-use-cxa-atexit");
7129
7130 if (Args.hasFlag(Pos: options::OPT_fregister_global_dtors_with_atexit,
7131 Neg: options::OPT_fno_register_global_dtors_with_atexit,
7132 Default: RawTriple.isOSDarwin() && !KernelOrKext))
7133 CmdArgs.push_back(Elt: "-fregister-global-dtors-with-atexit");
7134
7135 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fuse_line_directives,
7136 Neg: options::OPT_fno_use_line_directives);
7137
7138 // -fno-minimize-whitespace is default.
7139 if (Args.hasFlag(Pos: options::OPT_fminimize_whitespace,
7140 Neg: options::OPT_fno_minimize_whitespace, Default: false)) {
7141 types::ID InputType = Inputs[0].getType();
7142 if (!isDerivedFromC(Id: InputType))
7143 D.Diag(DiagID: diag::err_drv_opt_unsupported_input_type)
7144 << "-fminimize-whitespace" << types::getTypeName(Id: InputType);
7145 CmdArgs.push_back(Elt: "-fminimize-whitespace");
7146 }
7147
7148 // -fno-keep-system-includes is default.
7149 if (Args.hasFlag(Pos: options::OPT_fkeep_system_includes,
7150 Neg: options::OPT_fno_keep_system_includes, Default: false)) {
7151 types::ID InputType = Inputs[0].getType();
7152 if (!isDerivedFromC(Id: InputType))
7153 D.Diag(DiagID: diag::err_drv_opt_unsupported_input_type)
7154 << "-fkeep-system-includes" << types::getTypeName(Id: InputType);
7155 CmdArgs.push_back(Elt: "-fkeep-system-includes");
7156 }
7157
7158 // -fms-extensions=0 is default.
7159 if (Args.hasFlag(Pos: options::OPT_fms_extensions, Neg: options::OPT_fno_ms_extensions,
7160 Default: IsWindowsMSVC || IsUEFI))
7161 CmdArgs.push_back(Elt: "-fms-extensions");
7162
7163 // -fms-compatibility=0 is default.
7164 bool IsMSVCCompat = Args.hasFlag(
7165 Pos: options::OPT_fms_compatibility, Neg: options::OPT_fno_ms_compatibility,
7166 Default: (IsWindowsMSVC && Args.hasFlag(Pos: options::OPT_fms_extensions,
7167 Neg: options::OPT_fno_ms_extensions, Default: true)));
7168 if (IsMSVCCompat) {
7169 CmdArgs.push_back(Elt: "-fms-compatibility");
7170 if (!types::isCXX(Id: Input.getType()) &&
7171 Args.hasArg(Ids: options::OPT_fms_define_stdc))
7172 CmdArgs.push_back(Elt: "-fms-define-stdc");
7173 }
7174
7175 if (Triple.isWindowsMSVCEnvironment() && !D.IsCLMode() &&
7176 Args.hasArg(Ids: options::OPT_fms_runtime_lib_EQ))
7177 ProcessVSRuntimeLibrary(TC: getToolChain(), Args, CmdArgs);
7178
7179 // Handle -fgcc-version, if present.
7180 VersionTuple GNUCVer;
7181 if (Arg *A = Args.getLastArg(Ids: options::OPT_fgnuc_version_EQ)) {
7182 // Check that the version has 1 to 3 components and the minor and patch
7183 // versions fit in two decimal digits.
7184 StringRef Val = A->getValue();
7185 Val = Val.empty() ? "0" : Val; // Treat "" as 0 or disable.
7186 bool Invalid = GNUCVer.tryParse(string: Val);
7187 unsigned Minor = GNUCVer.getMinor().value_or(u: 0);
7188 unsigned Patch = GNUCVer.getSubminor().value_or(u: 0);
7189 if (Invalid || GNUCVer.getBuild() || Minor >= 100 || Patch >= 100) {
7190 D.Diag(DiagID: diag::err_drv_invalid_value)
7191 << A->getAsString(Args) << A->getValue();
7192 }
7193 } else if (!IsMSVCCompat) {
7194 // Imitate GCC 4.2.1 by default if -fms-compatibility is not in effect.
7195 GNUCVer = VersionTuple(4, 2, 1);
7196 }
7197 if (!GNUCVer.empty()) {
7198 CmdArgs.push_back(
7199 Elt: Args.MakeArgString(Str: "-fgnuc-version=" + GNUCVer.getAsString()));
7200 }
7201
7202 VersionTuple MSVT = TC.computeMSVCVersion(D: &D, Args);
7203 if (!MSVT.empty())
7204 CmdArgs.push_back(
7205 Elt: Args.MakeArgString(Str: "-fms-compatibility-version=" + MSVT.getAsString()));
7206
7207 bool IsMSVC2015Compatible = MSVT.getMajor() >= 19;
7208 if (ImplyVCPPCVer) {
7209 StringRef LanguageStandard;
7210 if (const Arg *StdArg = Args.getLastArg(Ids: options::OPT__SLASH_std)) {
7211 Std = StdArg;
7212 LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue())
7213 .Case(S: "c11", Value: "-std=c11")
7214 .Case(S: "c17", Value: "-std=c17")
7215 .Default(Value: "");
7216 if (LanguageStandard.empty())
7217 D.Diag(DiagID: clang::diag::warn_drv_unused_argument)
7218 << StdArg->getAsString(Args);
7219 }
7220 CmdArgs.push_back(Elt: LanguageStandard.data());
7221 }
7222 if (ImplyVCPPCXXVer) {
7223 StringRef LanguageStandard;
7224 if (const Arg *StdArg = Args.getLastArg(Ids: options::OPT__SLASH_std)) {
7225 Std = StdArg;
7226 LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue())
7227 .Case(S: "c++14", Value: "-std=c++14")
7228 .Case(S: "c++17", Value: "-std=c++17")
7229 .Case(S: "c++20", Value: "-std=c++20")
7230 // TODO add c++23 and c++26 when MSVC supports it.
7231 .Case(S: "c++23preview", Value: "-std=c++23")
7232 .Case(S: "c++latest", Value: "-std=c++26")
7233 .Default(Value: "");
7234 if (LanguageStandard.empty())
7235 D.Diag(DiagID: clang::diag::warn_drv_unused_argument)
7236 << StdArg->getAsString(Args);
7237 }
7238
7239 if (LanguageStandard.empty()) {
7240 if (IsMSVC2015Compatible)
7241 LanguageStandard = "-std=c++14";
7242 else
7243 LanguageStandard = "-std=c++11";
7244 }
7245
7246 CmdArgs.push_back(Elt: LanguageStandard.data());
7247 }
7248
7249 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fborland_extensions,
7250 Neg: options::OPT_fno_borland_extensions);
7251
7252 // -fno-declspec is default, except for PS4/PS5.
7253 if (Args.hasFlag(Pos: options::OPT_fdeclspec, Neg: options::OPT_fno_declspec,
7254 Default: RawTriple.isPS()))
7255 CmdArgs.push_back(Elt: "-fdeclspec");
7256 else if (Args.hasArg(Ids: options::OPT_fno_declspec))
7257 CmdArgs.push_back(Elt: "-fno-declspec"); // Explicitly disabling __declspec.
7258
7259 // -fthreadsafe-static is default, except for MSVC compatibility versions less
7260 // than 19.
7261 if (!Args.hasFlag(Pos: options::OPT_fthreadsafe_statics,
7262 Neg: options::OPT_fno_threadsafe_statics,
7263 Default: !types::isOpenCL(Id: InputType) &&
7264 (!IsWindowsMSVC || IsMSVC2015Compatible)))
7265 CmdArgs.push_back(Elt: "-fno-threadsafe-statics");
7266
7267 if (!Args.hasFlag(Pos: options::OPT_fms_tls_guards, Neg: options::OPT_fno_ms_tls_guards,
7268 Default: true))
7269 CmdArgs.push_back(Elt: "-fno-ms-tls-guards");
7270
7271 // Add -fno-assumptions, if it was specified.
7272 if (!Args.hasFlag(Pos: options::OPT_fassumptions, Neg: options::OPT_fno_assumptions,
7273 Default: true))
7274 CmdArgs.push_back(Elt: "-fno-assumptions");
7275
7276 // -fgnu-keywords default varies depending on language; only pass if
7277 // specified.
7278 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fgnu_keywords,
7279 Ids: options::OPT_fno_gnu_keywords);
7280
7281 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fgnu89_inline,
7282 Neg: options::OPT_fno_gnu89_inline);
7283
7284 const Arg *InlineArg = Args.getLastArg(Ids: options::OPT_finline_functions,
7285 Ids: options::OPT_finline_hint_functions,
7286 Ids: options::OPT_fno_inline_functions);
7287 if (Arg *A = Args.getLastArg(Ids: options::OPT_finline, Ids: options::OPT_fno_inline)) {
7288 if (A->getOption().matches(ID: options::OPT_fno_inline))
7289 A->render(Args, Output&: CmdArgs);
7290 } else if (InlineArg) {
7291 InlineArg->render(Args, Output&: CmdArgs);
7292 }
7293
7294 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_finline_max_stacksize_EQ);
7295
7296 // FIXME: Find a better way to determine whether we are in C++20.
7297 bool HaveCxx20 =
7298 Std &&
7299 (Std->containsValue(Value: "c++2a") || Std->containsValue(Value: "gnu++2a") ||
7300 Std->containsValue(Value: "c++20") || Std->containsValue(Value: "gnu++20") ||
7301 Std->containsValue(Value: "c++2b") || Std->containsValue(Value: "gnu++2b") ||
7302 Std->containsValue(Value: "c++23") || Std->containsValue(Value: "gnu++23") ||
7303 Std->containsValue(Value: "c++2c") || Std->containsValue(Value: "gnu++2c") ||
7304 Std->containsValue(Value: "c++26") || Std->containsValue(Value: "gnu++26") ||
7305 Std->containsValue(Value: "c++latest") || Std->containsValue(Value: "gnu++latest"));
7306 bool HaveModules =
7307 RenderModulesOptions(C, D, Args, Input, Output, HaveStd20: HaveCxx20, CmdArgs);
7308
7309 // -fdelayed-template-parsing is default when targeting MSVC.
7310 // Many old Windows SDK versions require this to parse.
7311 //
7312 // According to
7313 // https://learn.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=msvc-170,
7314 // MSVC actually defaults to -fno-delayed-template-parsing (/Zc:twoPhase-
7315 // with MSVC CLI) if using C++20. So we match the behavior with MSVC here to
7316 // not enable -fdelayed-template-parsing by default after C++20.
7317 //
7318 // FIXME: Given -fdelayed-template-parsing is a source of bugs, we should be
7319 // able to disable this by default at some point.
7320 if (Args.hasFlag(Pos: options::OPT_fdelayed_template_parsing,
7321 Neg: options::OPT_fno_delayed_template_parsing,
7322 Default: IsWindowsMSVC && !HaveCxx20)) {
7323 if (HaveCxx20)
7324 D.Diag(DiagID: clang::diag::warn_drv_delayed_template_parsing_after_cxx20);
7325
7326 CmdArgs.push_back(Elt: "-fdelayed-template-parsing");
7327 }
7328
7329 if (Args.hasFlag(Pos: options::OPT_fpch_validate_input_files_content,
7330 Neg: options::OPT_fno_pch_validate_input_files_content, Default: false))
7331 CmdArgs.push_back(Elt: "-fvalidate-ast-input-files-content");
7332 if (Args.hasFlag(Pos: options::OPT_fpch_instantiate_templates,
7333 Neg: options::OPT_fno_pch_instantiate_templates, Default: false))
7334 CmdArgs.push_back(Elt: "-fpch-instantiate-templates");
7335 if (Args.hasFlag(Pos: options::OPT_fpch_codegen, Neg: options::OPT_fno_pch_codegen,
7336 Default: false))
7337 CmdArgs.push_back(Elt: "-fmodules-codegen");
7338 if (Args.hasFlag(Pos: options::OPT_fpch_debuginfo, Neg: options::OPT_fno_pch_debuginfo,
7339 Default: false))
7340 CmdArgs.push_back(Elt: "-fmodules-debuginfo");
7341
7342 ObjCRuntime Runtime = AddObjCRuntimeArgs(args: Args, inputs: Inputs, cmdArgs&: CmdArgs, rewrite: rewriteKind);
7343 RenderObjCOptions(TC, D, T: RawTriple, Args, Runtime, InferCovariantReturns: rewriteKind != RK_None,
7344 Input, CmdArgs);
7345
7346 if (types::isObjC(Id: Input.getType()) &&
7347 Args.hasFlag(Pos: options::OPT_fobjc_encode_cxx_class_template_spec,
7348 Neg: options::OPT_fno_objc_encode_cxx_class_template_spec,
7349 Default: !Runtime.isNeXTFamily()))
7350 CmdArgs.push_back(Elt: "-fobjc-encode-cxx-class-template-spec");
7351
7352 if (Args.hasFlag(Pos: options::OPT_fapplication_extension,
7353 Neg: options::OPT_fno_application_extension, Default: false))
7354 CmdArgs.push_back(Elt: "-fapplication-extension");
7355
7356 // Handle GCC-style exception args.
7357 bool EH = false;
7358 if (!C.getDriver().IsCLMode())
7359 EH = addExceptionArgs(Args, InputType, TC, KernelOrKext, objcRuntime: Runtime, CmdArgs);
7360
7361 // Handle exception personalities
7362 Arg *A = Args.getLastArg(
7363 Ids: options::OPT_fsjlj_exceptions, Ids: options::OPT_fseh_exceptions,
7364 Ids: options::OPT_fdwarf_exceptions, Ids: options::OPT_fwasm_exceptions);
7365 if (A) {
7366 const Option &Opt = A->getOption();
7367 if (Opt.matches(ID: options::OPT_fsjlj_exceptions))
7368 CmdArgs.push_back(Elt: "-exception-model=sjlj");
7369 if (Opt.matches(ID: options::OPT_fseh_exceptions))
7370 CmdArgs.push_back(Elt: "-exception-model=seh");
7371 if (Opt.matches(ID: options::OPT_fdwarf_exceptions))
7372 CmdArgs.push_back(Elt: "-exception-model=dwarf");
7373 if (Opt.matches(ID: options::OPT_fwasm_exceptions))
7374 CmdArgs.push_back(Elt: "-exception-model=wasm");
7375 } else {
7376 switch (TC.GetExceptionModel(Args)) {
7377 default:
7378 break;
7379 case llvm::ExceptionHandling::DwarfCFI:
7380 CmdArgs.push_back(Elt: "-exception-model=dwarf");
7381 break;
7382 case llvm::ExceptionHandling::SjLj:
7383 CmdArgs.push_back(Elt: "-exception-model=sjlj");
7384 break;
7385 case llvm::ExceptionHandling::WinEH:
7386 CmdArgs.push_back(Elt: "-exception-model=seh");
7387 break;
7388 }
7389 }
7390
7391 // Unwind v2 (epilog) information for x64 Windows.
7392 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_winx64_eh_unwindv2);
7393
7394 // C++ "sane" operator new.
7395 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fassume_sane_operator_new,
7396 Neg: options::OPT_fno_assume_sane_operator_new);
7397
7398 // -fassume-unique-vtables is on by default.
7399 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fassume_unique_vtables,
7400 Neg: options::OPT_fno_assume_unique_vtables);
7401
7402 // -fsized-deallocation is on by default in C++14 onwards and otherwise off
7403 // by default.
7404 Args.addLastArg(Output&: CmdArgs, Ids: options::OPT_fsized_deallocation,
7405 Ids: options::OPT_fno_sized_deallocation);
7406
7407 // -faligned-allocation is on by default in C++17 onwards and otherwise off
7408 // by default.
7409 if (Arg *A = Args.getLastArg(Ids: options::OPT_faligned_allocation,
7410 Ids: options::OPT_fno_aligned_allocation,
7411 Ids: options::OPT_faligned_new_EQ)) {
7412 if (A->getOption().matches(ID: options::OPT_fno_aligned_allocation))
7413 CmdArgs.push_back(Elt: "-fno-aligned-allocation");
7414 else
7415 CmdArgs.push_back(Elt: "-faligned-allocation");
7416 }
7417
7418 // The default new alignment can be specified using a dedicated option or via
7419 // a GCC-compatible option that also turns on aligned allocation.
7420 if (Arg *A = Args.getLastArg(Ids: options::OPT_fnew_alignment_EQ,
7421 Ids: options::OPT_faligned_new_EQ))
7422 CmdArgs.push_back(
7423 Elt: Args.MakeArgString(Str: Twine("-fnew-alignment=") + A->getValue()));
7424
7425 // -fconstant-cfstrings is default, and may be subject to argument translation
7426 // on Darwin.
7427 if (!Args.hasFlag(Pos: options::OPT_fconstant_cfstrings,
7428 Neg: options::OPT_fno_constant_cfstrings, Default: true) ||
7429 !Args.hasFlag(Pos: options::OPT_mconstant_cfstrings,
7430 Neg: options::OPT_mno_constant_cfstrings, Default: true))
7431 CmdArgs.push_back(Elt: "-fno-constant-cfstrings");
7432
7433 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fpascal_strings,
7434 Neg: options::OPT_fno_pascal_strings);
7435
7436 // Honor -fpack-struct= and -fpack-struct, if given. Note that
7437 // -fno-pack-struct doesn't apply to -fpack-struct=.
7438 if (Arg *A = Args.getLastArg(Ids: options::OPT_fpack_struct_EQ)) {
7439 std::string PackStructStr = "-fpack-struct=";
7440 PackStructStr += A->getValue();
7441 CmdArgs.push_back(Elt: Args.MakeArgString(Str: PackStructStr));
7442 } else if (Args.hasFlag(Pos: options::OPT_fpack_struct,
7443 Neg: options::OPT_fno_pack_struct, Default: false)) {
7444 CmdArgs.push_back(Elt: "-fpack-struct=1");
7445 }
7446
7447 // Handle -fmax-type-align=N and -fno-type-align
7448 bool SkipMaxTypeAlign = Args.hasArg(Ids: options::OPT_fno_max_type_align);
7449 if (Arg *A = Args.getLastArg(Ids: options::OPT_fmax_type_align_EQ)) {
7450 if (!SkipMaxTypeAlign) {
7451 std::string MaxTypeAlignStr = "-fmax-type-align=";
7452 MaxTypeAlignStr += A->getValue();
7453 CmdArgs.push_back(Elt: Args.MakeArgString(Str: MaxTypeAlignStr));
7454 }
7455 } else if (RawTriple.isOSDarwin()) {
7456 if (!SkipMaxTypeAlign) {
7457 std::string MaxTypeAlignStr = "-fmax-type-align=16";
7458 CmdArgs.push_back(Elt: Args.MakeArgString(Str: MaxTypeAlignStr));
7459 }
7460 }
7461
7462 if (!Args.hasFlag(Pos: options::OPT_Qy, Neg: options::OPT_Qn, Default: true))
7463 CmdArgs.push_back(Elt: "-Qn");
7464
7465 // -fno-common is the default, set -fcommon only when that flag is set.
7466 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fcommon, Neg: options::OPT_fno_common);
7467
7468 // -fsigned-bitfields is default, and clang doesn't yet support
7469 // -funsigned-bitfields.
7470 if (!Args.hasFlag(Pos: options::OPT_fsigned_bitfields,
7471 Neg: options::OPT_funsigned_bitfields, Default: true))
7472 D.Diag(DiagID: diag::warn_drv_clang_unsupported)
7473 << Args.getLastArg(Ids: options::OPT_funsigned_bitfields)->getAsString(Args);
7474
7475 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
7476 if (!Args.hasFlag(Pos: options::OPT_ffor_scope, Neg: options::OPT_fno_for_scope, Default: true))
7477 D.Diag(DiagID: diag::err_drv_clang_unsupported)
7478 << Args.getLastArg(Ids: options::OPT_fno_for_scope)->getAsString(Args);
7479
7480 // -finput_charset=UTF-8 is default. Reject others
7481 if (Arg *inputCharset = Args.getLastArg(Ids: options::OPT_finput_charset_EQ)) {
7482 StringRef value = inputCharset->getValue();
7483 if (!value.equals_insensitive(RHS: "utf-8"))
7484 D.Diag(DiagID: diag::err_drv_invalid_value) << inputCharset->getAsString(Args)
7485 << value;
7486 }
7487
7488 // -fexec_charset=UTF-8 is default. Reject others
7489 if (Arg *execCharset = Args.getLastArg(Ids: options::OPT_fexec_charset_EQ)) {
7490 StringRef value = execCharset->getValue();
7491 if (!value.equals_insensitive(RHS: "utf-8"))
7492 D.Diag(DiagID: diag::err_drv_invalid_value) << execCharset->getAsString(Args)
7493 << value;
7494 }
7495
7496 RenderDiagnosticsOptions(D, Args, CmdArgs);
7497
7498 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fasm_blocks,
7499 Neg: options::OPT_fno_asm_blocks);
7500
7501 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_fgnu_inline_asm,
7502 Neg: options::OPT_fno_gnu_inline_asm);
7503
7504 handleVectorizeLoopsArgs(Args, CmdArgs);
7505 handleVectorizeSLPArgs(Args, CmdArgs);
7506
7507 StringRef VecWidth = parseMPreferVectorWidthOption(Diags&: D.getDiags(), Args);
7508 if (!VecWidth.empty())
7509 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mprefer-vector-width=" + VecWidth));
7510
7511 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fshow_overloads_EQ);
7512 Args.AddLastArg(Output&: CmdArgs,
7513 Ids: options::OPT_fsanitize_undefined_strip_path_components_EQ);
7514
7515 // -fdollars-in-identifiers default varies depending on platform and
7516 // language; only pass if specified.
7517 if (Arg *A = Args.getLastArg(Ids: options::OPT_fdollars_in_identifiers,
7518 Ids: options::OPT_fno_dollars_in_identifiers)) {
7519 if (A->getOption().matches(ID: options::OPT_fdollars_in_identifiers))
7520 CmdArgs.push_back(Elt: "-fdollars-in-identifiers");
7521 else
7522 CmdArgs.push_back(Elt: "-fno-dollars-in-identifiers");
7523 }
7524
7525 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fapple_pragma_pack,
7526 Neg: options::OPT_fno_apple_pragma_pack);
7527
7528 // Remarks can be enabled with any of the `-f.*optimization-record.*` flags.
7529 if (willEmitRemarks(Args) && checkRemarksOptions(D, Args, Triple))
7530 renderRemarksOptions(Args, CmdArgs, Triple, Input, Output, JA);
7531
7532 bool RewriteImports = Args.hasFlag(Pos: options::OPT_frewrite_imports,
7533 Neg: options::OPT_fno_rewrite_imports, Default: false);
7534 if (RewriteImports)
7535 CmdArgs.push_back(Elt: "-frewrite-imports");
7536
7537 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fdirectives_only,
7538 Neg: options::OPT_fno_directives_only);
7539
7540 // Enable rewrite includes if the user's asked for it or if we're generating
7541 // diagnostics.
7542 // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be
7543 // nice to enable this when doing a crashdump for modules as well.
7544 if (Args.hasFlag(Pos: options::OPT_frewrite_includes,
7545 Neg: options::OPT_fno_rewrite_includes, Default: false) ||
7546 (C.isForDiagnostics() && !HaveModules))
7547 CmdArgs.push_back(Elt: "-frewrite-includes");
7548
7549 if (Args.hasFlag(Pos: options::OPT_fzos_extensions,
7550 Neg: options::OPT_fno_zos_extensions, Default: false))
7551 CmdArgs.push_back(Elt: "-fzos-extensions");
7552 else if (Args.hasArg(Ids: options::OPT_fno_zos_extensions))
7553 CmdArgs.push_back(Elt: "-fno-zos-extensions");
7554
7555 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
7556 if (Arg *A = Args.getLastArg(Ids: options::OPT_traditional,
7557 Ids: options::OPT_traditional_cpp)) {
7558 if (isa<PreprocessJobAction>(Val: JA))
7559 CmdArgs.push_back(Elt: "-traditional-cpp");
7560 else
7561 D.Diag(DiagID: diag::err_drv_clang_unsupported) << A->getAsString(Args);
7562 }
7563
7564 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_dM);
7565 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_dD);
7566 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_dI);
7567
7568 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fmax_tokens_EQ);
7569
7570 // Handle serialized diagnostics.
7571 if (Arg *A = Args.getLastArg(Ids: options::OPT__serialize_diags)) {
7572 CmdArgs.push_back(Elt: "-serialize-diagnostic-file");
7573 CmdArgs.push_back(Elt: Args.MakeArgString(Str: A->getValue()));
7574 }
7575
7576 if (Args.hasArg(Ids: options::OPT_fretain_comments_from_system_headers))
7577 CmdArgs.push_back(Elt: "-fretain-comments-from-system-headers");
7578
7579 if (Arg *A = Args.getLastArg(Ids: options::OPT_fextend_variable_liveness_EQ)) {
7580 A->render(Args, Output&: CmdArgs);
7581 } else if (Arg *A = Args.getLastArg(Ids: options::OPT_O_Group);
7582 A && A->containsValue(Value: "g")) {
7583 // Set -fextend-variable-liveness=all by default at -Og.
7584 CmdArgs.push_back(Elt: "-fextend-variable-liveness=all");
7585 }
7586
7587 // Forward -fcomment-block-commands to -cc1.
7588 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fcomment_block_commands);
7589 // Forward -fparse-all-comments to -cc1.
7590 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fparse_all_comments);
7591
7592 // Turn -fplugin=name.so into -load name.so
7593 for (const Arg *A : Args.filtered(Ids: options::OPT_fplugin_EQ)) {
7594 CmdArgs.push_back(Elt: "-load");
7595 CmdArgs.push_back(Elt: A->getValue());
7596 A->claim();
7597 }
7598
7599 // Turn -fplugin-arg-pluginname-key=value into
7600 // -plugin-arg-pluginname key=value
7601 // GCC has an actual plugin_argument struct with key/value pairs that it
7602 // passes to its plugins, but we don't, so just pass it on as-is.
7603 //
7604 // The syntax for -fplugin-arg- is ambiguous if both plugin name and
7605 // argument key are allowed to contain dashes. GCC therefore only
7606 // allows dashes in the key. We do the same.
7607 for (const Arg *A : Args.filtered(Ids: options::OPT_fplugin_arg)) {
7608 auto ArgValue = StringRef(A->getValue());
7609 auto FirstDashIndex = ArgValue.find(C: '-');
7610 StringRef PluginName = ArgValue.substr(Start: 0, N: FirstDashIndex);
7611 StringRef Arg = ArgValue.substr(Start: FirstDashIndex + 1);
7612
7613 A->claim();
7614 if (FirstDashIndex == StringRef::npos || Arg.empty()) {
7615 if (PluginName.empty()) {
7616 D.Diag(DiagID: diag::warn_drv_missing_plugin_name) << A->getAsString(Args);
7617 } else {
7618 D.Diag(DiagID: diag::warn_drv_missing_plugin_arg)
7619 << PluginName << A->getAsString(Args);
7620 }
7621 continue;
7622 }
7623
7624 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-plugin-arg-") + PluginName));
7625 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Arg));
7626 }
7627
7628 // Forward -fpass-plugin=name.so to -cc1.
7629 for (const Arg *A : Args.filtered(Ids: options::OPT_fpass_plugin_EQ)) {
7630 CmdArgs.push_back(
7631 Elt: Args.MakeArgString(Str: Twine("-fpass-plugin=") + A->getValue()));
7632 A->claim();
7633 }
7634
7635 // Forward --vfsoverlay to -cc1.
7636 for (const Arg *A : Args.filtered(Ids: options::OPT_vfsoverlay)) {
7637 CmdArgs.push_back(Elt: "--vfsoverlay");
7638 CmdArgs.push_back(Elt: A->getValue());
7639 A->claim();
7640 }
7641
7642 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fsafe_buffer_usage_suggestions,
7643 Neg: options::OPT_fno_safe_buffer_usage_suggestions);
7644
7645 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fexperimental_late_parse_attributes,
7646 Neg: options::OPT_fno_experimental_late_parse_attributes);
7647
7648 if (Args.hasFlag(Pos: options::OPT_funique_source_file_names,
7649 Neg: options::OPT_fno_unique_source_file_names, Default: false)) {
7650 if (Arg *A = Args.getLastArg(Ids: options::OPT_unique_source_file_identifier_EQ))
7651 A->render(Args, Output&: CmdArgs);
7652 else
7653 CmdArgs.push_back(Elt: Args.MakeArgString(
7654 Str: Twine("-funique-source-file-identifier=") + Input.getBaseInput()));
7655 }
7656
7657 // Setup statistics file output.
7658 SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D);
7659 if (!StatsFile.empty()) {
7660 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-stats-file=") + StatsFile));
7661 if (D.CCPrintInternalStats)
7662 CmdArgs.push_back(Elt: "-stats-file-append");
7663 }
7664
7665 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
7666 // parser.
7667 for (auto Arg : Args.filtered(Ids: options::OPT_Xclang)) {
7668 Arg->claim();
7669 // -finclude-default-header flag is for preprocessor,
7670 // do not pass it to other cc1 commands when save-temps is enabled
7671 if (C.getDriver().isSaveTempsEnabled() &&
7672 !isa<PreprocessJobAction>(Val: JA)) {
7673 if (StringRef(Arg->getValue()) == "-finclude-default-header")
7674 continue;
7675 }
7676 CmdArgs.push_back(Elt: Arg->getValue());
7677 }
7678 for (const Arg *A : Args.filtered(Ids: options::OPT_mllvm)) {
7679 A->claim();
7680
7681 // We translate this by hand to the -cc1 argument, since nightly test uses
7682 // it and developers have been trained to spell it with -mllvm. Both
7683 // spellings are now deprecated and should be removed.
7684 if (StringRef(A->getValue(N: 0)) == "-disable-llvm-optzns") {
7685 CmdArgs.push_back(Elt: "-disable-llvm-optzns");
7686 } else {
7687 A->render(Args, Output&: CmdArgs);
7688 }
7689 }
7690
7691 // This needs to run after -Xclang argument forwarding to pick up the target
7692 // features enabled through -Xclang -target-feature flags.
7693 SanitizeArgs.addArgs(TC, Args, CmdArgs, InputType);
7694
7695#if CLANG_ENABLE_CIR
7696 // Forward -mmlir arguments to to the MLIR option parser.
7697 for (const Arg *A : Args.filtered(options::OPT_mmlir)) {
7698 A->claim();
7699 A->render(Args, CmdArgs);
7700 }
7701#endif // CLANG_ENABLE_CIR
7702
7703 // With -save-temps, we want to save the unoptimized bitcode output from the
7704 // CompileJobAction, use -disable-llvm-passes to get pristine IR generated
7705 // by the frontend.
7706 // When -fembed-bitcode is enabled, optimized bitcode is emitted because it
7707 // has slightly different breakdown between stages.
7708 // FIXME: -fembed-bitcode -save-temps will save optimized bitcode instead of
7709 // pristine IR generated by the frontend. Ideally, a new compile action should
7710 // be added so both IR can be captured.
7711 if ((C.getDriver().isSaveTempsEnabled() ||
7712 JA.isHostOffloading(OKind: Action::OFK_OpenMP)) &&
7713 !(C.getDriver().embedBitcodeInObject() && !IsUsingLTO) &&
7714 isa<CompileJobAction>(Val: JA))
7715 CmdArgs.push_back(Elt: "-disable-llvm-passes");
7716
7717 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_undef);
7718
7719 const char *Exec = D.getClangProgramPath();
7720
7721 // Optionally embed the -cc1 level arguments into the debug info or a
7722 // section, for build analysis.
7723 // Also record command line arguments into the debug info if
7724 // -grecord-gcc-switches options is set on.
7725 // By default, -gno-record-gcc-switches is set on and no recording.
7726 auto GRecordSwitches = false;
7727 auto FRecordSwitches = false;
7728 if (shouldRecordCommandLine(TC, Args, FRecordCommandLine&: FRecordSwitches, GRecordCommandLine&: GRecordSwitches)) {
7729 auto FlagsArgString = renderEscapedCommandLine(TC, Args);
7730 if (TC.UseDwarfDebugFlags() || GRecordSwitches) {
7731 CmdArgs.push_back(Elt: "-dwarf-debug-flags");
7732 CmdArgs.push_back(Elt: FlagsArgString);
7733 }
7734 if (FRecordSwitches) {
7735 CmdArgs.push_back(Elt: "-record-command-line");
7736 CmdArgs.push_back(Elt: FlagsArgString);
7737 }
7738 }
7739
7740 // Host-side offloading compilation receives all device-side outputs. Include
7741 // them in the host compilation depending on the target. If the host inputs
7742 // are not empty we use the new-driver scheme, otherwise use the old scheme.
7743 if ((IsCuda || IsHIP) && CudaDeviceInput) {
7744 CmdArgs.push_back(Elt: "-fcuda-include-gpubinary");
7745 CmdArgs.push_back(Elt: CudaDeviceInput->getFilename());
7746 } else if (!HostOffloadingInputs.empty()) {
7747 if (IsCuda && !IsRDCMode) {
7748 assert(HostOffloadingInputs.size() == 1 && "Only one input expected");
7749 CmdArgs.push_back(Elt: "-fcuda-include-gpubinary");
7750 CmdArgs.push_back(Elt: HostOffloadingInputs.front().getFilename());
7751 } else {
7752 for (const InputInfo Input : HostOffloadingInputs)
7753 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-fembed-offload-object=" +
7754 TC.getInputFilename(Input)));
7755 }
7756 }
7757
7758 if (IsCuda) {
7759 if (Args.hasFlag(Pos: options::OPT_fcuda_short_ptr,
7760 Neg: options::OPT_fno_cuda_short_ptr, Default: false))
7761 CmdArgs.push_back(Elt: "-fcuda-short-ptr");
7762 }
7763
7764 if (IsCuda || IsHIP) {
7765 // Determine the original source input.
7766 const Action *SourceAction = &JA;
7767 while (SourceAction->getKind() != Action::InputClass) {
7768 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
7769 SourceAction = SourceAction->getInputs()[0];
7770 }
7771 auto CUID = cast<InputAction>(Val: SourceAction)->getId();
7772 if (!CUID.empty())
7773 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-cuid=") + Twine(CUID)));
7774
7775 // -ffast-math turns on -fgpu-approx-transcendentals implicitly, but will
7776 // be overriden by -fno-gpu-approx-transcendentals.
7777 bool UseApproxTranscendentals = Args.hasFlag(
7778 Pos: options::OPT_ffast_math, Neg: options::OPT_fno_fast_math, Default: false);
7779 if (Args.hasFlag(Pos: options::OPT_fgpu_approx_transcendentals,
7780 Neg: options::OPT_fno_gpu_approx_transcendentals,
7781 Default: UseApproxTranscendentals))
7782 CmdArgs.push_back(Elt: "-fgpu-approx-transcendentals");
7783 } else {
7784 Args.claimAllArgs(Ids: options::OPT_fgpu_approx_transcendentals,
7785 Ids: options::OPT_fno_gpu_approx_transcendentals);
7786 }
7787
7788 if (IsHIP) {
7789 CmdArgs.push_back(Elt: "-fcuda-allow-variadic-functions");
7790 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_fgpu_default_stream_EQ);
7791 }
7792
7793 Args.AddAllArgs(Output&: CmdArgs,
7794 Id0: options::OPT_fsanitize_undefined_ignore_overflow_pattern_EQ);
7795
7796 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_foffload_uniform_block,
7797 Ids: options::OPT_fno_offload_uniform_block);
7798
7799 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_foffload_implicit_host_device_templates,
7800 Ids: options::OPT_fno_offload_implicit_host_device_templates);
7801
7802 if (IsCudaDevice || IsHIPDevice) {
7803 StringRef InlineThresh =
7804 Args.getLastArgValue(Id: options::OPT_fgpu_inline_threshold_EQ);
7805 if (!InlineThresh.empty()) {
7806 std::string ArgStr =
7807 std::string("-inline-threshold=") + InlineThresh.str();
7808 CmdArgs.append(IL: {"-mllvm", Args.MakeArgStringRef(Str: ArgStr)});
7809 }
7810 }
7811
7812 if (IsHIPDevice)
7813 Args.addOptOutFlag(Output&: CmdArgs,
7814 Pos: options::OPT_fhip_fp32_correctly_rounded_divide_sqrt,
7815 Neg: options::OPT_fno_hip_fp32_correctly_rounded_divide_sqrt);
7816
7817 // OpenMP offloading device jobs take the argument -fopenmp-host-ir-file-path
7818 // to specify the result of the compile phase on the host, so the meaningful
7819 // device declarations can be identified. Also, -fopenmp-is-target-device is
7820 // passed along to tell the frontend that it is generating code for a device,
7821 // so that only the relevant declarations are emitted.
7822 if (IsOpenMPDevice) {
7823 CmdArgs.push_back(Elt: "-fopenmp-is-target-device");
7824 // If we are offloading cuda/hip via llvm, it's also "cuda device code".
7825 if (Args.hasArg(Ids: options::OPT_foffload_via_llvm))
7826 CmdArgs.push_back(Elt: "-fcuda-is-device");
7827
7828 if (OpenMPDeviceInput) {
7829 CmdArgs.push_back(Elt: "-fopenmp-host-ir-file-path");
7830 CmdArgs.push_back(Elt: Args.MakeArgString(Str: OpenMPDeviceInput->getFilename()));
7831 }
7832 }
7833
7834 if (Triple.isAMDGPU()) {
7835 handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs);
7836
7837 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_munsafe_fp_atomics,
7838 Neg: options::OPT_mno_unsafe_fp_atomics);
7839 Args.addOptOutFlag(Output&: CmdArgs, Pos: options::OPT_mamdgpu_ieee,
7840 Neg: options::OPT_mno_amdgpu_ieee);
7841 }
7842
7843 addOpenMPHostOffloadingArgs(C, JA, Args, CmdArgs);
7844
7845 bool VirtualFunctionElimination =
7846 Args.hasFlag(Pos: options::OPT_fvirtual_function_elimination,
7847 Neg: options::OPT_fno_virtual_function_elimination, Default: false);
7848 if (VirtualFunctionElimination) {
7849 // VFE requires full LTO (currently, this might be relaxed to allow ThinLTO
7850 // in the future).
7851 if (LTOMode != LTOK_Full)
7852 D.Diag(DiagID: diag::err_drv_argument_only_allowed_with)
7853 << "-fvirtual-function-elimination"
7854 << "-flto=full";
7855
7856 CmdArgs.push_back(Elt: "-fvirtual-function-elimination");
7857 }
7858
7859 // VFE requires whole-program-vtables, and enables it by default.
7860 bool WholeProgramVTables = Args.hasFlag(
7861 Pos: options::OPT_fwhole_program_vtables,
7862 Neg: options::OPT_fno_whole_program_vtables, Default: VirtualFunctionElimination);
7863 if (VirtualFunctionElimination && !WholeProgramVTables) {
7864 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
7865 << "-fno-whole-program-vtables"
7866 << "-fvirtual-function-elimination";
7867 }
7868
7869 if (WholeProgramVTables) {
7870 // PS4 uses the legacy LTO API, which does not support this feature in
7871 // ThinLTO mode.
7872 bool IsPS4 = getToolChain().getTriple().isPS4();
7873
7874 // Check if we passed LTO options but they were suppressed because this is a
7875 // device offloading action, or we passed device offload LTO options which
7876 // were suppressed because this is not the device offload action.
7877 // Check if we are using PS4 in regular LTO mode.
7878 // Otherwise, issue an error.
7879
7880 auto OtherLTOMode =
7881 IsDeviceOffloadAction ? D.getLTOMode() : D.getOffloadLTOMode();
7882 auto OtherIsUsingLTO = OtherLTOMode != LTOK_None;
7883
7884 if ((!IsUsingLTO && !OtherIsUsingLTO) ||
7885 (IsPS4 && !UnifiedLTO && (D.getLTOMode() != LTOK_Full)))
7886 D.Diag(DiagID: diag::err_drv_argument_only_allowed_with)
7887 << "-fwhole-program-vtables"
7888 << ((IsPS4 && !UnifiedLTO) ? "-flto=full" : "-flto");
7889
7890 // Propagate -fwhole-program-vtables if this is an LTO compile.
7891 if (IsUsingLTO)
7892 CmdArgs.push_back(Elt: "-fwhole-program-vtables");
7893 }
7894
7895 bool DefaultsSplitLTOUnit =
7896 ((WholeProgramVTables || SanitizeArgs.needsLTO()) &&
7897 (LTOMode == LTOK_Full || TC.canSplitThinLTOUnit())) ||
7898 (!Triple.isPS4() && UnifiedLTO);
7899 bool SplitLTOUnit =
7900 Args.hasFlag(Pos: options::OPT_fsplit_lto_unit,
7901 Neg: options::OPT_fno_split_lto_unit, Default: DefaultsSplitLTOUnit);
7902 if (SanitizeArgs.needsLTO() && !SplitLTOUnit)
7903 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with) << "-fno-split-lto-unit"
7904 << "-fsanitize=cfi";
7905 if (SplitLTOUnit)
7906 CmdArgs.push_back(Elt: "-fsplit-lto-unit");
7907
7908 if (Arg *A = Args.getLastArg(Ids: options::OPT_ffat_lto_objects,
7909 Ids: options::OPT_fno_fat_lto_objects)) {
7910 if (IsUsingLTO && A->getOption().matches(ID: options::OPT_ffat_lto_objects)) {
7911 assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin);
7912 if (!Triple.isOSBinFormatELF()) {
7913 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
7914 << A->getAsString(Args) << TC.getTripleString();
7915 }
7916 CmdArgs.push_back(Elt: Args.MakeArgString(
7917 Str: Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full")));
7918 CmdArgs.push_back(Elt: "-flto-unit");
7919 CmdArgs.push_back(Elt: "-ffat-lto-objects");
7920 A->render(Args, Output&: CmdArgs);
7921 }
7922 }
7923
7924 if (Arg *A = Args.getLastArg(Ids: options::OPT_fglobal_isel,
7925 Ids: options::OPT_fno_global_isel)) {
7926 CmdArgs.push_back(Elt: "-mllvm");
7927 if (A->getOption().matches(ID: options::OPT_fglobal_isel)) {
7928 CmdArgs.push_back(Elt: "-global-isel=1");
7929
7930 // GISel is on by default on AArch64 -O0, so don't bother adding
7931 // the fallback remarks for it. Other combinations will add a warning of
7932 // some kind.
7933 bool IsArchSupported = Triple.getArch() == llvm::Triple::aarch64;
7934 bool IsOptLevelSupported = false;
7935
7936 Arg *A = Args.getLastArg(Ids: options::OPT_O_Group);
7937 if (Triple.getArch() == llvm::Triple::aarch64) {
7938 if (!A || A->getOption().matches(ID: options::OPT_O0))
7939 IsOptLevelSupported = true;
7940 }
7941 if (!IsArchSupported || !IsOptLevelSupported) {
7942 CmdArgs.push_back(Elt: "-mllvm");
7943 CmdArgs.push_back(Elt: "-global-isel-abort=2");
7944
7945 if (!IsArchSupported)
7946 D.Diag(DiagID: diag::warn_drv_global_isel_incomplete) << Triple.getArchName();
7947 else
7948 D.Diag(DiagID: diag::warn_drv_global_isel_incomplete_opt);
7949 }
7950 } else {
7951 CmdArgs.push_back(Elt: "-global-isel=0");
7952 }
7953 }
7954
7955 if (Arg *A = Args.getLastArg(Ids: options::OPT_fforce_enable_int128,
7956 Ids: options::OPT_fno_force_enable_int128)) {
7957 if (A->getOption().matches(ID: options::OPT_fforce_enable_int128))
7958 CmdArgs.push_back(Elt: "-fforce-enable-int128");
7959 }
7960
7961 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fkeep_static_consts,
7962 Neg: options::OPT_fno_keep_static_consts);
7963 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fkeep_persistent_storage_variables,
7964 Neg: options::OPT_fno_keep_persistent_storage_variables);
7965 Args.addOptInFlag(Output&: CmdArgs, Pos: options::OPT_fcomplete_member_pointers,
7966 Neg: options::OPT_fno_complete_member_pointers);
7967 if (Arg *A = Args.getLastArg(Ids: options::OPT_cxx_static_destructors_EQ))
7968 A->render(Args, Output&: CmdArgs);
7969
7970 addMachineOutlinerArgs(D, Args, CmdArgs, Triple, /*IsLTO=*/false);
7971
7972 addOutlineAtomicsArgs(D, TC: getToolChain(), Args, CmdArgs, Triple);
7973
7974 if (Triple.isAArch64() &&
7975 (Args.hasArg(Ids: options::OPT_mno_fmv) ||
7976 (Triple.isAndroid() && Triple.isAndroidVersionLT(Major: 23)) ||
7977 getToolChain().GetRuntimeLibType(Args) != ToolChain::RLT_CompilerRT)) {
7978 // Disable Function Multiversioning on AArch64 target.
7979 CmdArgs.push_back(Elt: "-target-feature");
7980 CmdArgs.push_back(Elt: "-fmv");
7981 }
7982
7983 if (Args.hasFlag(Pos: options::OPT_faddrsig, Neg: options::OPT_fno_addrsig,
7984 Default: (TC.getTriple().isOSBinFormatELF() ||
7985 TC.getTriple().isOSBinFormatCOFF()) &&
7986 !TC.getTriple().isPS4() && !TC.getTriple().isVE() &&
7987 !TC.getTriple().isOSNetBSD() &&
7988 !Distro(D.getVFS(), TC.getTriple()).IsGentoo() &&
7989 !TC.getTriple().isAndroid() && TC.useIntegratedAs()))
7990 CmdArgs.push_back(Elt: "-faddrsig");
7991
7992 if ((Triple.isOSBinFormatELF() || Triple.isOSBinFormatMachO()) &&
7993 (EH || UnwindTables || AsyncUnwindTables ||
7994 DebugInfoKind != llvm::codegenoptions::NoDebugInfo))
7995 CmdArgs.push_back(Elt: "-D__GCC_HAVE_DWARF2_CFI_ASM=1");
7996
7997 if (Arg *A = Args.getLastArg(Ids: options::OPT_fsymbol_partition_EQ)) {
7998 std::string Str = A->getAsString(Args);
7999 if (!TC.getTriple().isOSBinFormatELF())
8000 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
8001 << Str << TC.getTripleString();
8002 CmdArgs.push_back(Elt: Args.MakeArgString(Str));
8003 }
8004
8005 // Add the "-o out -x type src.c" flags last. This is done primarily to make
8006 // the -cc1 command easier to edit when reproducing compiler crashes.
8007 if (Output.getType() == types::TY_Dependencies) {
8008 // Handled with other dependency code.
8009 } else if (Output.isFilename()) {
8010 if (Output.getType() == clang::driver::types::TY_IFS_CPP ||
8011 Output.getType() == clang::driver::types::TY_IFS) {
8012 SmallString<128> OutputFilename(Output.getFilename());
8013 llvm::sys::path::replace_extension(path&: OutputFilename, extension: "ifs");
8014 CmdArgs.push_back(Elt: "-o");
8015 CmdArgs.push_back(Elt: Args.MakeArgString(Str: OutputFilename));
8016 } else {
8017 CmdArgs.push_back(Elt: "-o");
8018 CmdArgs.push_back(Elt: Output.getFilename());
8019 }
8020 } else {
8021 assert(Output.isNothing() && "Invalid output.");
8022 }
8023
8024 addDashXForInput(Args, Input, CmdArgs);
8025
8026 ArrayRef<InputInfo> FrontendInputs = Input;
8027 if (IsExtractAPI)
8028 FrontendInputs = ExtractAPIInputs;
8029 else if (Input.isNothing())
8030 FrontendInputs = {};
8031
8032 for (const InputInfo &Input : FrontendInputs) {
8033 if (Input.isFilename())
8034 CmdArgs.push_back(Elt: Input.getFilename());
8035 else
8036 Input.getInputArg().renderAsInput(Args, Output&: CmdArgs);
8037 }
8038
8039 if (D.CC1Main && !D.CCGenDiagnostics) {
8040 // Invoke the CC1 directly in this process
8041 C.addCommand(C: std::make_unique<CC1Command>(
8042 args: JA, args: *this, args: ResponseFileSupport::AtFileUTF8(), args&: Exec, args&: CmdArgs, args: Inputs,
8043 args: Output, args: D.getPrependArg()));
8044 } else {
8045 C.addCommand(C: std::make_unique<Command>(
8046 args: JA, args: *this, args: ResponseFileSupport::AtFileUTF8(), args&: Exec, args&: CmdArgs, args: Inputs,
8047 args: Output, args: D.getPrependArg()));
8048 }
8049
8050 // Make the compile command echo its inputs for /showFilenames.
8051 if (Output.getType() == types::TY_Object &&
8052 Args.hasFlag(Pos: options::OPT__SLASH_showFilenames,
8053 Neg: options::OPT__SLASH_showFilenames_, Default: false)) {
8054 C.getJobs().getJobs().back()->PrintInputFilenames = true;
8055 }
8056
8057 if (Arg *A = Args.getLastArg(Ids: options::OPT_pg))
8058 if (FPKeepKind == CodeGenOptions::FramePointerKind::None &&
8059 !Args.hasArg(Ids: options::OPT_mfentry))
8060 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with) << "-fomit-frame-pointer"
8061 << A->getAsString(Args);
8062
8063 // Claim some arguments which clang supports automatically.
8064
8065 // -fpch-preprocess is used with gcc to add a special marker in the output to
8066 // include the PCH file.
8067 Args.ClaimAllArgs(Id0: options::OPT_fpch_preprocess);
8068
8069 // Claim some arguments which clang doesn't support, but we don't
8070 // care to warn the user about.
8071 Args.ClaimAllArgs(Id0: options::OPT_clang_ignored_f_Group);
8072 Args.ClaimAllArgs(Id0: options::OPT_clang_ignored_m_Group);
8073
8074 // Disable warnings for clang -E -emit-llvm foo.c
8075 Args.ClaimAllArgs(Id0: options::OPT_emit_llvm);
8076}
8077
8078Clang::Clang(const ToolChain &TC, bool HasIntegratedBackend)
8079 // CAUTION! The first constructor argument ("clang") is not arbitrary,
8080 // as it is for other tools. Some operations on a Tool actually test
8081 // whether that tool is Clang based on the Tool's Name as a string.
8082 : Tool("clang", "clang frontend", TC), HasBackend(HasIntegratedBackend) {}
8083
8084Clang::~Clang() {}
8085
8086/// Add options related to the Objective-C runtime/ABI.
8087///
8088/// Returns true if the runtime is non-fragile.
8089ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
8090 const InputInfoList &inputs,
8091 ArgStringList &cmdArgs,
8092 RewriteKind rewriteKind) const {
8093 // Look for the controlling runtime option.
8094 Arg *runtimeArg =
8095 args.getLastArg(Ids: options::OPT_fnext_runtime, Ids: options::OPT_fgnu_runtime,
8096 Ids: options::OPT_fobjc_runtime_EQ);
8097
8098 // Just forward -fobjc-runtime= to the frontend. This supercedes
8099 // options about fragility.
8100 if (runtimeArg &&
8101 runtimeArg->getOption().matches(ID: options::OPT_fobjc_runtime_EQ)) {
8102 ObjCRuntime runtime;
8103 StringRef value = runtimeArg->getValue();
8104 if (runtime.tryParse(input: value)) {
8105 getToolChain().getDriver().Diag(DiagID: diag::err_drv_unknown_objc_runtime)
8106 << value;
8107 }
8108 if ((runtime.getKind() == ObjCRuntime::GNUstep) &&
8109 (runtime.getVersion() >= VersionTuple(2, 0)))
8110 if (!getToolChain().getTriple().isOSBinFormatELF() &&
8111 !getToolChain().getTriple().isOSBinFormatCOFF()) {
8112 getToolChain().getDriver().Diag(
8113 DiagID: diag::err_drv_gnustep_objc_runtime_incompatible_binary)
8114 << runtime.getVersion().getMajor();
8115 }
8116
8117 runtimeArg->render(Args: args, Output&: cmdArgs);
8118 return runtime;
8119 }
8120
8121 // Otherwise, we'll need the ABI "version". Version numbers are
8122 // slightly confusing for historical reasons:
8123 // 1 - Traditional "fragile" ABI
8124 // 2 - Non-fragile ABI, version 1
8125 // 3 - Non-fragile ABI, version 2
8126 unsigned objcABIVersion = 1;
8127 // If -fobjc-abi-version= is present, use that to set the version.
8128 if (Arg *abiArg = args.getLastArg(Ids: options::OPT_fobjc_abi_version_EQ)) {
8129 StringRef value = abiArg->getValue();
8130 if (value == "1")
8131 objcABIVersion = 1;
8132 else if (value == "2")
8133 objcABIVersion = 2;
8134 else if (value == "3")
8135 objcABIVersion = 3;
8136 else
8137 getToolChain().getDriver().Diag(DiagID: diag::err_drv_clang_unsupported) << value;
8138 } else {
8139 // Otherwise, determine if we are using the non-fragile ABI.
8140 bool nonFragileABIIsDefault =
8141 (rewriteKind == RK_NonFragile ||
8142 (rewriteKind == RK_None &&
8143 getToolChain().IsObjCNonFragileABIDefault()));
8144 if (args.hasFlag(Pos: options::OPT_fobjc_nonfragile_abi,
8145 Neg: options::OPT_fno_objc_nonfragile_abi,
8146 Default: nonFragileABIIsDefault)) {
8147// Determine the non-fragile ABI version to use.
8148#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
8149 unsigned nonFragileABIVersion = 1;
8150#else
8151 unsigned nonFragileABIVersion = 2;
8152#endif
8153
8154 if (Arg *abiArg =
8155 args.getLastArg(Ids: options::OPT_fobjc_nonfragile_abi_version_EQ)) {
8156 StringRef value = abiArg->getValue();
8157 if (value == "1")
8158 nonFragileABIVersion = 1;
8159 else if (value == "2")
8160 nonFragileABIVersion = 2;
8161 else
8162 getToolChain().getDriver().Diag(DiagID: diag::err_drv_clang_unsupported)
8163 << value;
8164 }
8165
8166 objcABIVersion = 1 + nonFragileABIVersion;
8167 } else {
8168 objcABIVersion = 1;
8169 }
8170 }
8171
8172 // We don't actually care about the ABI version other than whether
8173 // it's non-fragile.
8174 bool isNonFragile = objcABIVersion != 1;
8175
8176 // If we have no runtime argument, ask the toolchain for its default runtime.
8177 // However, the rewriter only really supports the Mac runtime, so assume that.
8178 ObjCRuntime runtime;
8179 if (!runtimeArg) {
8180 switch (rewriteKind) {
8181 case RK_None:
8182 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
8183 break;
8184 case RK_Fragile:
8185 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
8186 break;
8187 case RK_NonFragile:
8188 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
8189 break;
8190 }
8191
8192 // -fnext-runtime
8193 } else if (runtimeArg->getOption().matches(ID: options::OPT_fnext_runtime)) {
8194 // On Darwin, make this use the default behavior for the toolchain.
8195 if (getToolChain().getTriple().isOSDarwin()) {
8196 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
8197
8198 // Otherwise, build for a generic macosx port.
8199 } else {
8200 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
8201 }
8202
8203 // -fgnu-runtime
8204 } else {
8205 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
8206 // Legacy behaviour is to target the gnustep runtime if we are in
8207 // non-fragile mode or the GCC runtime in fragile mode.
8208 if (isNonFragile)
8209 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(2, 0));
8210 else
8211 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
8212 }
8213
8214 if (llvm::any_of(Range: inputs, P: [](const InputInfo &input) {
8215 return types::isObjC(Id: input.getType());
8216 }))
8217 cmdArgs.push_back(
8218 Elt: args.MakeArgString(Str: "-fobjc-runtime=" + runtime.getAsString()));
8219 return runtime;
8220}
8221
8222static bool maybeConsumeDash(const std::string &EH, size_t &I) {
8223 bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-');
8224 I += HaveDash;
8225 return !HaveDash;
8226}
8227
8228namespace {
8229struct EHFlags {
8230 bool Synch = false;
8231 bool Asynch = false;
8232 bool NoUnwindC = false;
8233};
8234} // end anonymous namespace
8235
8236/// /EH controls whether to run destructor cleanups when exceptions are
8237/// thrown. There are three modifiers:
8238/// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions.
8239/// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions.
8240/// The 'a' modifier is unimplemented and fundamentally hard in LLVM IR.
8241/// - c: Assume that extern "C" functions are implicitly nounwind.
8242/// The default is /EHs-c-, meaning cleanups are disabled.
8243static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args,
8244 bool isWindowsMSVC) {
8245 EHFlags EH;
8246
8247 std::vector<std::string> EHArgs =
8248 Args.getAllArgValues(Id: options::OPT__SLASH_EH);
8249 for (const auto &EHVal : EHArgs) {
8250 for (size_t I = 0, E = EHVal.size(); I != E; ++I) {
8251 switch (EHVal[I]) {
8252 case 'a':
8253 EH.Asynch = maybeConsumeDash(EH: EHVal, I);
8254 if (EH.Asynch) {
8255 // Async exceptions are Windows MSVC only.
8256 if (!isWindowsMSVC) {
8257 EH.Asynch = false;
8258 D.Diag(DiagID: clang::diag::warn_drv_unused_argument) << "/EHa" << EHVal;
8259 continue;
8260 }
8261 EH.Synch = false;
8262 }
8263 continue;
8264 case 'c':
8265 EH.NoUnwindC = maybeConsumeDash(EH: EHVal, I);
8266 continue;
8267 case 's':
8268 EH.Synch = maybeConsumeDash(EH: EHVal, I);
8269 if (EH.Synch)
8270 EH.Asynch = false;
8271 continue;
8272 default:
8273 break;
8274 }
8275 D.Diag(DiagID: clang::diag::err_drv_invalid_value) << "/EH" << EHVal;
8276 break;
8277 }
8278 }
8279 // The /GX, /GX- flags are only processed if there are not /EH flags.
8280 // The default is that /GX is not specified.
8281 if (EHArgs.empty() &&
8282 Args.hasFlag(Pos: options::OPT__SLASH_GX, Neg: options::OPT__SLASH_GX_,
8283 /*Default=*/false)) {
8284 EH.Synch = true;
8285 EH.NoUnwindC = true;
8286 }
8287
8288 if (Args.hasArg(Ids: options::OPT__SLASH_kernel)) {
8289 EH.Synch = false;
8290 EH.NoUnwindC = false;
8291 EH.Asynch = false;
8292 }
8293
8294 return EH;
8295}
8296
8297void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
8298 ArgStringList &CmdArgs) const {
8299 bool isNVPTX = getToolChain().getTriple().isNVPTX();
8300
8301 ProcessVSRuntimeLibrary(TC: getToolChain(), Args, CmdArgs);
8302
8303 if (Arg *ShowIncludes =
8304 Args.getLastArg(Ids: options::OPT__SLASH_showIncludes,
8305 Ids: options::OPT__SLASH_showIncludes_user)) {
8306 CmdArgs.push_back(Elt: "--show-includes");
8307 if (ShowIncludes->getOption().matches(ID: options::OPT__SLASH_showIncludes))
8308 CmdArgs.push_back(Elt: "-sys-header-deps");
8309 }
8310
8311 // This controls whether or not we emit RTTI data for polymorphic types.
8312 if (Args.hasFlag(Pos: options::OPT__SLASH_GR_, Neg: options::OPT__SLASH_GR,
8313 /*Default=*/false))
8314 CmdArgs.push_back(Elt: "-fno-rtti-data");
8315
8316 // This controls whether or not we emit stack-protector instrumentation.
8317 // In MSVC, Buffer Security Check (/GS) is on by default.
8318 if (!isNVPTX && Args.hasFlag(Pos: options::OPT__SLASH_GS, Neg: options::OPT__SLASH_GS_,
8319 /*Default=*/true)) {
8320 CmdArgs.push_back(Elt: "-stack-protector");
8321 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(LangOptions::SSPStrong)));
8322 }
8323
8324 const Driver &D = getToolChain().getDriver();
8325
8326 bool IsWindowsMSVC = getToolChain().getTriple().isWindowsMSVCEnvironment();
8327 EHFlags EH = parseClangCLEHFlags(D, Args, isWindowsMSVC: IsWindowsMSVC);
8328 if (!isNVPTX && (EH.Synch || EH.Asynch)) {
8329 if (types::isCXX(Id: InputType))
8330 CmdArgs.push_back(Elt: "-fcxx-exceptions");
8331 CmdArgs.push_back(Elt: "-fexceptions");
8332 if (EH.Asynch)
8333 CmdArgs.push_back(Elt: "-fasync-exceptions");
8334 }
8335 if (types::isCXX(Id: InputType) && EH.Synch && EH.NoUnwindC)
8336 CmdArgs.push_back(Elt: "-fexternc-nounwind");
8337
8338 // /EP should expand to -E -P.
8339 if (Args.hasArg(Ids: options::OPT__SLASH_EP)) {
8340 CmdArgs.push_back(Elt: "-E");
8341 CmdArgs.push_back(Elt: "-P");
8342 }
8343
8344 if (Args.hasFlag(Pos: options::OPT__SLASH_Zc_dllexportInlines_,
8345 Neg: options::OPT__SLASH_Zc_dllexportInlines,
8346 Default: false)) {
8347 CmdArgs.push_back(Elt: "-fno-dllexport-inlines");
8348 }
8349
8350 if (Args.hasFlag(Pos: options::OPT__SLASH_Zc_wchar_t_,
8351 Neg: options::OPT__SLASH_Zc_wchar_t, Default: false)) {
8352 CmdArgs.push_back(Elt: "-fno-wchar");
8353 }
8354
8355 if (Args.hasArg(Ids: options::OPT__SLASH_kernel)) {
8356 llvm::Triple::ArchType Arch = getToolChain().getArch();
8357 std::vector<std::string> Values =
8358 Args.getAllArgValues(Id: options::OPT__SLASH_arch);
8359 if (!Values.empty()) {
8360 llvm::SmallSet<std::string, 4> SupportedArches;
8361 if (Arch == llvm::Triple::x86)
8362 SupportedArches.insert(V: "IA32");
8363
8364 for (auto &V : Values)
8365 if (!SupportedArches.contains(V))
8366 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
8367 << std::string("/arch:").append(str: V) << "/kernel";
8368 }
8369
8370 CmdArgs.push_back(Elt: "-fno-rtti");
8371 if (Args.hasFlag(Pos: options::OPT__SLASH_GR, Neg: options::OPT__SLASH_GR_, Default: false))
8372 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with) << "/GR"
8373 << "/kernel";
8374 }
8375
8376 Arg *MostGeneralArg = Args.getLastArg(Ids: options::OPT__SLASH_vmg);
8377 Arg *BestCaseArg = Args.getLastArg(Ids: options::OPT__SLASH_vmb);
8378 if (MostGeneralArg && BestCaseArg)
8379 D.Diag(DiagID: clang::diag::err_drv_argument_not_allowed_with)
8380 << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args);
8381
8382 if (MostGeneralArg) {
8383 Arg *SingleArg = Args.getLastArg(Ids: options::OPT__SLASH_vms);
8384 Arg *MultipleArg = Args.getLastArg(Ids: options::OPT__SLASH_vmm);
8385 Arg *VirtualArg = Args.getLastArg(Ids: options::OPT__SLASH_vmv);
8386
8387 Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg;
8388 Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg;
8389 if (FirstConflict && SecondConflict && FirstConflict != SecondConflict)
8390 D.Diag(DiagID: clang::diag::err_drv_argument_not_allowed_with)
8391 << FirstConflict->getAsString(Args)
8392 << SecondConflict->getAsString(Args);
8393
8394 if (SingleArg)
8395 CmdArgs.push_back(Elt: "-fms-memptr-rep=single");
8396 else if (MultipleArg)
8397 CmdArgs.push_back(Elt: "-fms-memptr-rep=multiple");
8398 else
8399 CmdArgs.push_back(Elt: "-fms-memptr-rep=virtual");
8400 }
8401
8402 if (Args.hasArg(Ids: options::OPT_regcall4))
8403 CmdArgs.push_back(Elt: "-regcall4");
8404
8405 // Parse the default calling convention options.
8406 if (Arg *CCArg =
8407 Args.getLastArg(Ids: options::OPT__SLASH_Gd, Ids: options::OPT__SLASH_Gr,
8408 Ids: options::OPT__SLASH_Gz, Ids: options::OPT__SLASH_Gv,
8409 Ids: options::OPT__SLASH_Gregcall)) {
8410 unsigned DCCOptId = CCArg->getOption().getID();
8411 const char *DCCFlag = nullptr;
8412 bool ArchSupported = !isNVPTX;
8413 llvm::Triple::ArchType Arch = getToolChain().getArch();
8414 switch (DCCOptId) {
8415 case options::OPT__SLASH_Gd:
8416 DCCFlag = "-fdefault-calling-conv=cdecl";
8417 break;
8418 case options::OPT__SLASH_Gr:
8419 ArchSupported = Arch == llvm::Triple::x86;
8420 DCCFlag = "-fdefault-calling-conv=fastcall";
8421 break;
8422 case options::OPT__SLASH_Gz:
8423 ArchSupported = Arch == llvm::Triple::x86;
8424 DCCFlag = "-fdefault-calling-conv=stdcall";
8425 break;
8426 case options::OPT__SLASH_Gv:
8427 ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64;
8428 DCCFlag = "-fdefault-calling-conv=vectorcall";
8429 break;
8430 case options::OPT__SLASH_Gregcall:
8431 ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64;
8432 DCCFlag = "-fdefault-calling-conv=regcall";
8433 break;
8434 }
8435
8436 // MSVC doesn't warn if /Gr or /Gz is used on x64, so we don't either.
8437 if (ArchSupported && DCCFlag)
8438 CmdArgs.push_back(Elt: DCCFlag);
8439 }
8440
8441 if (Args.hasArg(Ids: options::OPT__SLASH_Gregcall4))
8442 CmdArgs.push_back(Elt: "-regcall4");
8443
8444 Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_vtordisp_mode_EQ);
8445
8446 if (!Args.hasArg(Ids: options::OPT_fdiagnostics_format_EQ)) {
8447 CmdArgs.push_back(Elt: "-fdiagnostics-format");
8448 CmdArgs.push_back(Elt: "msvc");
8449 }
8450
8451 if (Args.hasArg(Ids: options::OPT__SLASH_kernel))
8452 CmdArgs.push_back(Elt: "-fms-kernel");
8453
8454 // Unwind v2 (epilog) information for x64 Windows.
8455 if (Args.hasArg(Ids: options::OPT__SLASH_d2epilogunwindrequirev2))
8456 CmdArgs.push_back(Elt: "-fwinx64-eh-unwindv2=required");
8457 else if (Args.hasArg(Ids: options::OPT__SLASH_d2epilogunwind))
8458 CmdArgs.push_back(Elt: "-fwinx64-eh-unwindv2=best-effort");
8459
8460 for (const Arg *A : Args.filtered(Ids: options::OPT__SLASH_guard)) {
8461 StringRef GuardArgs = A->getValue();
8462 // The only valid options are "cf", "cf,nochecks", "cf-", "ehcont" and
8463 // "ehcont-".
8464 if (GuardArgs.equals_insensitive(RHS: "cf")) {
8465 // Emit CFG instrumentation and the table of address-taken functions.
8466 CmdArgs.push_back(Elt: "-cfguard");
8467 } else if (GuardArgs.equals_insensitive(RHS: "cf,nochecks")) {
8468 // Emit only the table of address-taken functions.
8469 CmdArgs.push_back(Elt: "-cfguard-no-checks");
8470 } else if (GuardArgs.equals_insensitive(RHS: "ehcont")) {
8471 // Emit EH continuation table.
8472 CmdArgs.push_back(Elt: "-ehcontguard");
8473 } else if (GuardArgs.equals_insensitive(RHS: "cf-") ||
8474 GuardArgs.equals_insensitive(RHS: "ehcont-")) {
8475 // Do nothing, but we might want to emit a security warning in future.
8476 } else {
8477 D.Diag(DiagID: diag::err_drv_invalid_value) << A->getSpelling() << GuardArgs;
8478 }
8479 A->claim();
8480 }
8481
8482 for (const auto &FuncOverride :
8483 Args.getAllArgValues(Id: options::OPT__SLASH_funcoverride)) {
8484 CmdArgs.push_back(Elt: Args.MakeArgString(
8485 Str: Twine("-loader-replaceable-function=") + FuncOverride));
8486 }
8487}
8488
8489const char *Clang::getBaseInputName(const ArgList &Args,
8490 const InputInfo &Input) {
8491 return Args.MakeArgString(Str: llvm::sys::path::filename(path: Input.getBaseInput()));
8492}
8493
8494const char *Clang::getBaseInputStem(const ArgList &Args,
8495 const InputInfoList &Inputs) {
8496 const char *Str = getBaseInputName(Args, Input: Inputs[0]);
8497
8498 if (const char *End = strrchr(s: Str, c: '.'))
8499 return Args.MakeArgString(Str: std::string(Str, End));
8500
8501 return Str;
8502}
8503
8504const char *Clang::getDependencyFileName(const ArgList &Args,
8505 const InputInfoList &Inputs) {
8506 // FIXME: Think about this more.
8507
8508 if (Arg *OutputOpt = Args.getLastArg(Ids: options::OPT_o)) {
8509 SmallString<128> OutputFilename(OutputOpt->getValue());
8510 llvm::sys::path::replace_extension(path&: OutputFilename, extension: llvm::Twine('d'));
8511 return Args.MakeArgString(Str: OutputFilename);
8512 }
8513
8514 return Args.MakeArgString(Str: Twine(getBaseInputStem(Args, Inputs)) + ".d");
8515}
8516
8517// Begin ClangAs
8518
8519void ClangAs::AddMIPSTargetArgs(const ArgList &Args,
8520 ArgStringList &CmdArgs) const {
8521 StringRef CPUName;
8522 StringRef ABIName;
8523 const llvm::Triple &Triple = getToolChain().getTriple();
8524 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
8525
8526 CmdArgs.push_back(Elt: "-target-abi");
8527 CmdArgs.push_back(Elt: ABIName.data());
8528}
8529
8530void ClangAs::AddX86TargetArgs(const ArgList &Args,
8531 ArgStringList &CmdArgs) const {
8532 addX86AlignBranchArgs(D: getToolChain().getDriver(), Args, CmdArgs,
8533 /*IsLTO=*/false);
8534
8535 if (Arg *A = Args.getLastArg(Ids: options::OPT_masm_EQ)) {
8536 StringRef Value = A->getValue();
8537 if (Value == "intel" || Value == "att") {
8538 CmdArgs.push_back(Elt: "-mllvm");
8539 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-x86-asm-syntax=" + Value));
8540 } else {
8541 getToolChain().getDriver().Diag(DiagID: diag::err_drv_unsupported_option_argument)
8542 << A->getSpelling() << Value;
8543 }
8544 }
8545}
8546
8547void ClangAs::AddLoongArchTargetArgs(const ArgList &Args,
8548 ArgStringList &CmdArgs) const {
8549 CmdArgs.push_back(Elt: "-target-abi");
8550 CmdArgs.push_back(Elt: loongarch::getLoongArchABI(D: getToolChain().getDriver(), Args,
8551 Triple: getToolChain().getTriple())
8552 .data());
8553}
8554
8555void ClangAs::AddRISCVTargetArgs(const ArgList &Args,
8556 ArgStringList &CmdArgs) const {
8557 const llvm::Triple &Triple = getToolChain().getTriple();
8558 StringRef ABIName = riscv::getRISCVABI(Args, Triple);
8559
8560 CmdArgs.push_back(Elt: "-target-abi");
8561 CmdArgs.push_back(Elt: ABIName.data());
8562
8563 if (Args.hasFlag(Pos: options::OPT_mdefault_build_attributes,
8564 Neg: options::OPT_mno_default_build_attributes, Default: true)) {
8565 CmdArgs.push_back(Elt: "-mllvm");
8566 CmdArgs.push_back(Elt: "-riscv-add-build-attributes");
8567 }
8568}
8569
8570void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
8571 const InputInfo &Output, const InputInfoList &Inputs,
8572 const ArgList &Args,
8573 const char *LinkingOutput) const {
8574 ArgStringList CmdArgs;
8575
8576 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
8577 const InputInfo &Input = Inputs[0];
8578
8579 const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
8580 const std::string &TripleStr = Triple.getTriple();
8581 const auto &D = getToolChain().getDriver();
8582
8583 // Don't warn about "clang -w -c foo.s"
8584 Args.ClaimAllArgs(Id0: options::OPT_w);
8585 // and "clang -emit-llvm -c foo.s"
8586 Args.ClaimAllArgs(Id0: options::OPT_emit_llvm);
8587
8588 claimNoWarnArgs(Args);
8589
8590 // Invoke ourselves in -cc1as mode.
8591 //
8592 // FIXME: Implement custom jobs for internal actions.
8593 CmdArgs.push_back(Elt: "-cc1as");
8594
8595 // Add the "effective" target triple.
8596 CmdArgs.push_back(Elt: "-triple");
8597 CmdArgs.push_back(Elt: Args.MakeArgString(Str: TripleStr));
8598
8599 getToolChain().addClangCC1ASTargetOptions(Args, CC1ASArgs&: CmdArgs);
8600
8601 // Set the output mode, we currently only expect to be used as a real
8602 // assembler.
8603 CmdArgs.push_back(Elt: "-filetype");
8604 CmdArgs.push_back(Elt: "obj");
8605
8606 // Set the main file name, so that debug info works even with
8607 // -save-temps or preprocessed assembly.
8608 CmdArgs.push_back(Elt: "-main-file-name");
8609 CmdArgs.push_back(Elt: Clang::getBaseInputName(Args, Input));
8610
8611 // Add the target cpu
8612 std::string CPU = getCPUName(D, Args, T: Triple, /*FromAs*/ true);
8613 if (!CPU.empty()) {
8614 CmdArgs.push_back(Elt: "-target-cpu");
8615 CmdArgs.push_back(Elt: Args.MakeArgString(Str: CPU));
8616 }
8617
8618 // Add the target features
8619 getTargetFeatures(D, Triple, Args, CmdArgs, ForAS: true);
8620
8621 // Ignore explicit -force_cpusubtype_ALL option.
8622 (void)Args.hasArg(Ids: options::OPT_force__cpusubtype__ALL);
8623
8624 // Pass along any -I options so we get proper .include search paths.
8625 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_I_Group);
8626
8627 // Pass along any --embed-dir or similar options so we get proper embed paths.
8628 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_embed_dir_EQ);
8629
8630 // Determine the original source input.
8631 auto FindSource = [](const Action *S) -> const Action * {
8632 while (S->getKind() != Action::InputClass) {
8633 assert(!S->getInputs().empty() && "unexpected root action!");
8634 S = S->getInputs()[0];
8635 }
8636 return S;
8637 };
8638 const Action *SourceAction = FindSource(&JA);
8639
8640 // Forward -g and handle debug info related flags, assuming we are dealing
8641 // with an actual assembly file.
8642 bool WantDebug = false;
8643 Args.ClaimAllArgs(Id0: options::OPT_g_Group);
8644 if (Arg *A = Args.getLastArg(Ids: options::OPT_g_Group))
8645 WantDebug = !A->getOption().matches(ID: options::OPT_g0) &&
8646 !A->getOption().matches(ID: options::OPT_ggdb0);
8647
8648 // If a -gdwarf argument appeared, remember it.
8649 bool EmitDwarf = false;
8650 if (const Arg *A = getDwarfNArg(Args))
8651 EmitDwarf = checkDebugInfoOption(A, Args, D, TC: getToolChain());
8652
8653 bool EmitCodeView = false;
8654 if (const Arg *A = Args.getLastArg(Ids: options::OPT_gcodeview))
8655 EmitCodeView = checkDebugInfoOption(A, Args, D, TC: getToolChain());
8656
8657 // If the user asked for debug info but did not explicitly specify -gcodeview
8658 // or -gdwarf, ask the toolchain for the default format.
8659 if (!EmitCodeView && !EmitDwarf && WantDebug) {
8660 switch (getToolChain().getDefaultDebugFormat()) {
8661 case llvm::codegenoptions::DIF_CodeView:
8662 EmitCodeView = true;
8663 break;
8664 case llvm::codegenoptions::DIF_DWARF:
8665 EmitDwarf = true;
8666 break;
8667 }
8668 }
8669
8670 // If the arguments don't imply DWARF, don't emit any debug info here.
8671 if (!EmitDwarf)
8672 WantDebug = false;
8673
8674 llvm::codegenoptions::DebugInfoKind DebugInfoKind =
8675 llvm::codegenoptions::NoDebugInfo;
8676
8677 // Add the -fdebug-compilation-dir flag if needed.
8678 const char *DebugCompilationDir =
8679 addDebugCompDirArg(Args, CmdArgs, VFS: C.getDriver().getVFS());
8680
8681 if (SourceAction->getType() == types::TY_Asm ||
8682 SourceAction->getType() == types::TY_PP_Asm) {
8683 // You might think that it would be ok to set DebugInfoKind outside of
8684 // the guard for source type, however there is a test which asserts
8685 // that some assembler invocation receives no -debug-info-kind,
8686 // and it's not clear whether that test is just overly restrictive.
8687 DebugInfoKind = (WantDebug ? llvm::codegenoptions::DebugInfoConstructor
8688 : llvm::codegenoptions::NoDebugInfo);
8689
8690 addDebugPrefixMapArg(D: getToolChain().getDriver(), TC: getToolChain(), Args,
8691 CmdArgs);
8692
8693 // Set the AT_producer to the clang version when using the integrated
8694 // assembler on assembly source files.
8695 CmdArgs.push_back(Elt: "-dwarf-debug-producer");
8696 CmdArgs.push_back(Elt: Args.MakeArgString(Str: getClangFullVersion()));
8697
8698 // And pass along -I options
8699 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_I);
8700 }
8701 const unsigned DwarfVersion = getDwarfVersion(TC: getToolChain(), Args);
8702 RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion,
8703 DebuggerTuning: llvm::DebuggerKind::Default);
8704 renderDwarfFormat(D, T: Triple, Args, CmdArgs, DwarfVersion);
8705 RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC: getToolChain());
8706
8707 // Handle -fPIC et al -- the relocation-model affects the assembler
8708 // for some targets.
8709 llvm::Reloc::Model RelocationModel;
8710 unsigned PICLevel;
8711 bool IsPIE;
8712 std::tie(args&: RelocationModel, args&: PICLevel, args&: IsPIE) =
8713 ParsePICArgs(ToolChain: getToolChain(), Args);
8714
8715 const char *RMName = RelocationModelName(Model: RelocationModel);
8716 if (RMName) {
8717 CmdArgs.push_back(Elt: "-mrelocation-model");
8718 CmdArgs.push_back(Elt: RMName);
8719 }
8720
8721 // Optionally embed the -cc1as level arguments into the debug info, for build
8722 // analysis.
8723 if (getToolChain().UseDwarfDebugFlags()) {
8724 ArgStringList OriginalArgs;
8725 for (const auto &Arg : Args)
8726 Arg->render(Args, Output&: OriginalArgs);
8727
8728 SmallString<256> Flags;
8729 const char *Exec = getToolChain().getDriver().getClangProgramPath();
8730 escapeSpacesAndBackslashes(Arg: Exec, Res&: Flags);
8731 for (const char *OriginalArg : OriginalArgs) {
8732 SmallString<128> EscapedArg;
8733 escapeSpacesAndBackslashes(Arg: OriginalArg, Res&: EscapedArg);
8734 Flags += " ";
8735 Flags += EscapedArg;
8736 }
8737 CmdArgs.push_back(Elt: "-dwarf-debug-flags");
8738 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Flags));
8739 }
8740
8741 // FIXME: Add -static support, once we have it.
8742
8743 // Add target specific flags.
8744 switch (getToolChain().getArch()) {
8745 default:
8746 break;
8747
8748 case llvm::Triple::mips:
8749 case llvm::Triple::mipsel:
8750 case llvm::Triple::mips64:
8751 case llvm::Triple::mips64el:
8752 AddMIPSTargetArgs(Args, CmdArgs);
8753 break;
8754
8755 case llvm::Triple::x86:
8756 case llvm::Triple::x86_64:
8757 AddX86TargetArgs(Args, CmdArgs);
8758 break;
8759
8760 case llvm::Triple::arm:
8761 case llvm::Triple::armeb:
8762 case llvm::Triple::thumb:
8763 case llvm::Triple::thumbeb:
8764 // This isn't in AddARMTargetArgs because we want to do this for assembly
8765 // only, not C/C++.
8766 if (Args.hasFlag(Pos: options::OPT_mdefault_build_attributes,
8767 Neg: options::OPT_mno_default_build_attributes, Default: true)) {
8768 CmdArgs.push_back(Elt: "-mllvm");
8769 CmdArgs.push_back(Elt: "-arm-add-build-attributes");
8770 }
8771 break;
8772
8773 case llvm::Triple::aarch64:
8774 case llvm::Triple::aarch64_32:
8775 case llvm::Triple::aarch64_be:
8776 if (Args.hasArg(Ids: options::OPT_mmark_bti_property)) {
8777 CmdArgs.push_back(Elt: "-mllvm");
8778 CmdArgs.push_back(Elt: "-aarch64-mark-bti-property");
8779 }
8780 break;
8781
8782 case llvm::Triple::loongarch32:
8783 case llvm::Triple::loongarch64:
8784 AddLoongArchTargetArgs(Args, CmdArgs);
8785 break;
8786
8787 case llvm::Triple::riscv32:
8788 case llvm::Triple::riscv64:
8789 AddRISCVTargetArgs(Args, CmdArgs);
8790 break;
8791
8792 case llvm::Triple::hexagon:
8793 if (Args.hasFlag(Pos: options::OPT_mdefault_build_attributes,
8794 Neg: options::OPT_mno_default_build_attributes, Default: true)) {
8795 CmdArgs.push_back(Elt: "-mllvm");
8796 CmdArgs.push_back(Elt: "-hexagon-add-build-attributes");
8797 }
8798 break;
8799 }
8800
8801 // Consume all the warning flags. Usually this would be handled more
8802 // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as
8803 // doesn't handle that so rather than warning about unused flags that are
8804 // actually used, we'll lie by omission instead.
8805 // FIXME: Stop lying and consume only the appropriate driver flags
8806 Args.ClaimAllArgs(Id0: options::OPT_W_Group);
8807
8808 CollectArgsForIntegratedAssembler(C, Args, CmdArgs,
8809 D: getToolChain().getDriver());
8810
8811 // Forward -Xclangas arguments to -cc1as
8812 for (auto Arg : Args.filtered(Ids: options::OPT_Xclangas)) {
8813 Arg->claim();
8814 CmdArgs.push_back(Elt: Arg->getValue());
8815 }
8816
8817 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_mllvm);
8818
8819 if (DebugInfoKind > llvm::codegenoptions::NoDebugInfo && Output.isFilename())
8820 addDebugObjectName(Args, CmdArgs, DebugCompilationDir,
8821 OutputFileName: Output.getFilename());
8822
8823 // Fixup any previous commands that use -object-file-name because when we
8824 // generated them, the final .obj name wasn't yet known.
8825 for (Command &J : C.getJobs()) {
8826 if (SourceAction != FindSource(&J.getSource()))
8827 continue;
8828 auto &JArgs = J.getArguments();
8829 for (unsigned I = 0; I < JArgs.size(); ++I) {
8830 if (StringRef(JArgs[I]).starts_with(Prefix: "-object-file-name=") &&
8831 Output.isFilename()) {
8832 ArgStringList NewArgs(JArgs.begin(), JArgs.begin() + I);
8833 addDebugObjectName(Args, CmdArgs&: NewArgs, DebugCompilationDir,
8834 OutputFileName: Output.getFilename());
8835 NewArgs.append(in_start: JArgs.begin() + I + 1, in_end: JArgs.end());
8836 J.replaceArguments(List: NewArgs);
8837 break;
8838 }
8839 }
8840 }
8841
8842 assert(Output.isFilename() && "Unexpected lipo output.");
8843 CmdArgs.push_back(Elt: "-o");
8844 CmdArgs.push_back(Elt: Output.getFilename());
8845
8846 const llvm::Triple &T = getToolChain().getTriple();
8847 Arg *A;
8848 if (getDebugFissionKind(D, Args, Arg&: A) == DwarfFissionKind::Split &&
8849 T.isOSBinFormatELF()) {
8850 CmdArgs.push_back(Elt: "-split-dwarf-output");
8851 CmdArgs.push_back(Elt: SplitDebugName(JA, Args, Input, Output));
8852 }
8853
8854 if (Triple.isAMDGPU())
8855 handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs, /*IsCC1As=*/true);
8856
8857 assert(Input.isFilename() && "Invalid input.");
8858 CmdArgs.push_back(Elt: Input.getFilename());
8859
8860 const char *Exec = getToolChain().getDriver().getClangProgramPath();
8861 if (D.CC1Main && !D.CCGenDiagnostics) {
8862 // Invoke cc1as directly in this process.
8863 C.addCommand(C: std::make_unique<CC1Command>(
8864 args: JA, args: *this, args: ResponseFileSupport::AtFileUTF8(), args&: Exec, args&: CmdArgs, args: Inputs,
8865 args: Output, args: D.getPrependArg()));
8866 } else {
8867 C.addCommand(C: std::make_unique<Command>(
8868 args: JA, args: *this, args: ResponseFileSupport::AtFileUTF8(), args&: Exec, args&: CmdArgs, args: Inputs,
8869 args: Output, args: D.getPrependArg()));
8870 }
8871}
8872
8873// Begin OffloadBundler
8874void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA,
8875 const InputInfo &Output,
8876 const InputInfoList &Inputs,
8877 const llvm::opt::ArgList &TCArgs,
8878 const char *LinkingOutput) const {
8879 // The version with only one output is expected to refer to a bundling job.
8880 assert(isa<OffloadBundlingJobAction>(JA) && "Expecting bundling job!");
8881
8882 // The bundling command looks like this:
8883 // clang-offload-bundler -type=bc
8884 // -targets=host-triple,openmp-triple1,openmp-triple2
8885 // -output=output_file
8886 // -input=unbundle_file_host
8887 // -input=unbundle_file_tgt1
8888 // -input=unbundle_file_tgt2
8889
8890 ArgStringList CmdArgs;
8891
8892 // Get the type.
8893 CmdArgs.push_back(Elt: TCArgs.MakeArgString(
8894 Str: Twine("-type=") + types::getTypeTempSuffix(Id: Output.getType())));
8895
8896 assert(JA.getInputs().size() == Inputs.size() &&
8897 "Not have inputs for all dependence actions??");
8898
8899 // Get the targets.
8900 SmallString<128> Triples;
8901 Triples += "-targets=";
8902 for (unsigned I = 0; I < Inputs.size(); ++I) {
8903 if (I)
8904 Triples += ',';
8905
8906 // Find ToolChain for this input.
8907 Action::OffloadKind CurKind = Action::OFK_Host;
8908 const ToolChain *CurTC = &getToolChain();
8909 const Action *CurDep = JA.getInputs()[I];
8910
8911 if (const auto *OA = dyn_cast<OffloadAction>(Val: CurDep)) {
8912 CurTC = nullptr;
8913 OA->doOnEachDependence(Work: [&](Action *A, const ToolChain *TC, const char *) {
8914 assert(CurTC == nullptr && "Expected one dependence!");
8915 CurKind = A->getOffloadingDeviceKind();
8916 CurTC = TC;
8917 });
8918 }
8919 Triples += Action::GetOffloadKindName(Kind: CurKind);
8920 Triples += '-';
8921 Triples +=
8922 CurTC->getTriple().normalize(Form: llvm::Triple::CanonicalForm::FOUR_IDENT);
8923 if ((CurKind == Action::OFK_HIP || CurKind == Action::OFK_Cuda) &&
8924 !StringRef(CurDep->getOffloadingArch()).empty()) {
8925 Triples += '-';
8926 Triples += CurDep->getOffloadingArch();
8927 }
8928
8929 // TODO: Replace parsing of -march flag. Can be done by storing GPUArch
8930 // with each toolchain.
8931 StringRef GPUArchName;
8932 if (CurKind == Action::OFK_OpenMP) {
8933 // Extract GPUArch from -march argument in TC argument list.
8934 for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) {
8935 auto ArchStr = StringRef(TCArgs.getArgString(Index: ArgIndex));
8936 auto Arch = ArchStr.starts_with_insensitive(Prefix: "-march=");
8937 if (Arch) {
8938 GPUArchName = ArchStr.substr(Start: 7);
8939 Triples += "-";
8940 break;
8941 }
8942 }
8943 Triples += GPUArchName.str();
8944 }
8945 }
8946 CmdArgs.push_back(Elt: TCArgs.MakeArgString(Str: Triples));
8947
8948 // Get bundled file command.
8949 CmdArgs.push_back(
8950 Elt: TCArgs.MakeArgString(Str: Twine("-output=") + Output.getFilename()));
8951
8952 // Get unbundled files command.
8953 for (unsigned I = 0; I < Inputs.size(); ++I) {
8954 SmallString<128> UB;
8955 UB += "-input=";
8956
8957 // Find ToolChain for this input.
8958 const ToolChain *CurTC = &getToolChain();
8959 if (const auto *OA = dyn_cast<OffloadAction>(Val: JA.getInputs()[I])) {
8960 CurTC = nullptr;
8961 OA->doOnEachDependence(Work: [&](Action *, const ToolChain *TC, const char *) {
8962 assert(CurTC == nullptr && "Expected one dependence!");
8963 CurTC = TC;
8964 });
8965 UB += C.addTempFile(
8966 Name: C.getArgs().MakeArgString(Str: CurTC->getInputFilename(Input: Inputs[I])));
8967 } else {
8968 UB += CurTC->getInputFilename(Input: Inputs[I]);
8969 }
8970 CmdArgs.push_back(Elt: TCArgs.MakeArgString(Str: UB));
8971 }
8972 addOffloadCompressArgs(TCArgs, CmdArgs);
8973 // All the inputs are encoded as commands.
8974 C.addCommand(C: std::make_unique<Command>(
8975 args: JA, args: *this, args: ResponseFileSupport::None(),
8976 args: TCArgs.MakeArgString(Str: getToolChain().GetProgramPath(Name: getShortName())),
8977 args&: CmdArgs, args: ArrayRef<InputInfo>(), args: Output));
8978}
8979
8980void OffloadBundler::ConstructJobMultipleOutputs(
8981 Compilation &C, const JobAction &JA, const InputInfoList &Outputs,
8982 const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs,
8983 const char *LinkingOutput) const {
8984 // The version with multiple outputs is expected to refer to a unbundling job.
8985 auto &UA = cast<OffloadUnbundlingJobAction>(Val: JA);
8986
8987 // The unbundling command looks like this:
8988 // clang-offload-bundler -type=bc
8989 // -targets=host-triple,openmp-triple1,openmp-triple2
8990 // -input=input_file
8991 // -output=unbundle_file_host
8992 // -output=unbundle_file_tgt1
8993 // -output=unbundle_file_tgt2
8994 // -unbundle
8995
8996 ArgStringList CmdArgs;
8997
8998 assert(Inputs.size() == 1 && "Expecting to unbundle a single file!");
8999 InputInfo Input = Inputs.front();
9000
9001 // Get the type.
9002 CmdArgs.push_back(Elt: TCArgs.MakeArgString(
9003 Str: Twine("-type=") + types::getTypeTempSuffix(Id: Input.getType())));
9004
9005 // Get the targets.
9006 SmallString<128> Triples;
9007 Triples += "-targets=";
9008 auto DepInfo = UA.getDependentActionsInfo();
9009 for (unsigned I = 0; I < DepInfo.size(); ++I) {
9010 if (I)
9011 Triples += ',';
9012
9013 auto &Dep = DepInfo[I];
9014 Triples += Action::GetOffloadKindName(Kind: Dep.DependentOffloadKind);
9015 Triples += '-';
9016 Triples += Dep.DependentToolChain->getTriple().normalize(
9017 Form: llvm::Triple::CanonicalForm::FOUR_IDENT);
9018 if ((Dep.DependentOffloadKind == Action::OFK_HIP ||
9019 Dep.DependentOffloadKind == Action::OFK_Cuda) &&
9020 !Dep.DependentBoundArch.empty()) {
9021 Triples += '-';
9022 Triples += Dep.DependentBoundArch;
9023 }
9024 // TODO: Replace parsing of -march flag. Can be done by storing GPUArch
9025 // with each toolchain.
9026 StringRef GPUArchName;
9027 if (Dep.DependentOffloadKind == Action::OFK_OpenMP) {
9028 // Extract GPUArch from -march argument in TC argument list.
9029 for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) {
9030 StringRef ArchStr = StringRef(TCArgs.getArgString(Index: ArgIndex));
9031 auto Arch = ArchStr.starts_with_insensitive(Prefix: "-march=");
9032 if (Arch) {
9033 GPUArchName = ArchStr.substr(Start: 7);
9034 Triples += "-";
9035 break;
9036 }
9037 }
9038 Triples += GPUArchName.str();
9039 }
9040 }
9041
9042 CmdArgs.push_back(Elt: TCArgs.MakeArgString(Str: Triples));
9043
9044 // Get bundled file command.
9045 CmdArgs.push_back(
9046 Elt: TCArgs.MakeArgString(Str: Twine("-input=") + Input.getFilename()));
9047
9048 // Get unbundled files command.
9049 for (unsigned I = 0; I < Outputs.size(); ++I) {
9050 SmallString<128> UB;
9051 UB += "-output=";
9052 UB += DepInfo[I].DependentToolChain->getInputFilename(Input: Outputs[I]);
9053 CmdArgs.push_back(Elt: TCArgs.MakeArgString(Str: UB));
9054 }
9055 CmdArgs.push_back(Elt: "-unbundle");
9056 CmdArgs.push_back(Elt: "-allow-missing-bundles");
9057 if (TCArgs.hasArg(Ids: options::OPT_v))
9058 CmdArgs.push_back(Elt: "-verbose");
9059
9060 // All the inputs are encoded as commands.
9061 C.addCommand(C: std::make_unique<Command>(
9062 args: JA, args: *this, args: ResponseFileSupport::None(),
9063 args: TCArgs.MakeArgString(Str: getToolChain().GetProgramPath(Name: getShortName())),
9064 args&: CmdArgs, args: ArrayRef<InputInfo>(), args: Outputs));
9065}
9066
9067void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
9068 const InputInfo &Output,
9069 const InputInfoList &Inputs,
9070 const llvm::opt::ArgList &Args,
9071 const char *LinkingOutput) const {
9072 ArgStringList CmdArgs;
9073
9074 // Add the output file name.
9075 assert(Output.isFilename() && "Invalid output.");
9076 CmdArgs.push_back(Elt: "-o");
9077 CmdArgs.push_back(Elt: Output.getFilename());
9078
9079 // Create the inputs to bundle the needed metadata.
9080 for (const InputInfo &Input : Inputs) {
9081 const Action *OffloadAction = Input.getAction();
9082 const ToolChain *TC = OffloadAction->getOffloadingToolChain();
9083 const ArgList &TCArgs =
9084 C.getArgsForToolChain(TC, BoundArch: OffloadAction->getOffloadingArch(),
9085 DeviceOffloadKind: OffloadAction->getOffloadingDeviceKind());
9086 StringRef File = C.getArgs().MakeArgString(Str: TC->getInputFilename(Input));
9087 StringRef Arch = OffloadAction->getOffloadingArch()
9088 ? OffloadAction->getOffloadingArch()
9089 : TCArgs.getLastArgValue(Id: options::OPT_march_EQ);
9090 StringRef Kind =
9091 Action::GetOffloadKindName(Kind: OffloadAction->getOffloadingDeviceKind());
9092
9093 ArgStringList Features;
9094 SmallVector<StringRef> FeatureArgs;
9095 getTargetFeatures(D: TC->getDriver(), Triple: TC->getTriple(), Args: TCArgs, CmdArgs&: Features,
9096 ForAS: false);
9097 llvm::copy_if(Range&: Features, Out: std::back_inserter(x&: FeatureArgs),
9098 P: [](StringRef Arg) { return !Arg.starts_with(Prefix: "-target"); });
9099
9100 // TODO: We need to pass in the full target-id and handle it properly in the
9101 // linker wrapper.
9102 SmallVector<std::string> Parts{
9103 "file=" + File.str(),
9104 "triple=" + TC->getTripleString(),
9105 "arch=" + (Arch.empty() ? "generic" : Arch.str()),
9106 "kind=" + Kind.str(),
9107 };
9108
9109 if (TC->getDriver().isUsingOffloadLTO())
9110 for (StringRef Feature : FeatureArgs)
9111 Parts.emplace_back(Args: "feature=" + Feature.str());
9112
9113 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--image=" + llvm::join(R&: Parts, Separator: ",")));
9114 }
9115
9116 C.addCommand(C: std::make_unique<Command>(
9117 args: JA, args: *this, args: ResponseFileSupport::None(),
9118 args: Args.MakeArgString(Str: getToolChain().GetProgramPath(Name: getShortName())),
9119 args&: CmdArgs, args: Inputs, args: Output));
9120}
9121
9122void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
9123 const InputInfo &Output,
9124 const InputInfoList &Inputs,
9125 const ArgList &Args,
9126 const char *LinkingOutput) const {
9127 using namespace options;
9128
9129 // A list of permitted options that will be forwarded to the embedded device
9130 // compilation job.
9131 const llvm::DenseSet<unsigned> CompilerOptions{
9132 OPT_v,
9133 OPT_cuda_path_EQ,
9134 OPT_rocm_path_EQ,
9135 OPT_O_Group,
9136 OPT_g_Group,
9137 OPT_g_flags_Group,
9138 OPT_R_value_Group,
9139 OPT_R_Group,
9140 OPT_Xcuda_ptxas,
9141 OPT_ftime_report,
9142 OPT_ftime_trace,
9143 OPT_ftime_trace_EQ,
9144 OPT_ftime_trace_granularity_EQ,
9145 OPT_ftime_trace_verbose,
9146 OPT_opt_record_file,
9147 OPT_opt_record_format,
9148 OPT_opt_record_passes,
9149 OPT_fsave_optimization_record,
9150 OPT_fsave_optimization_record_EQ,
9151 OPT_fno_save_optimization_record,
9152 OPT_foptimization_record_file_EQ,
9153 OPT_foptimization_record_passes_EQ,
9154 OPT_save_temps,
9155 OPT_save_temps_EQ,
9156 OPT_mcode_object_version_EQ,
9157 OPT_load,
9158 OPT_fno_lto,
9159 OPT_flto,
9160 OPT_flto_partitions_EQ,
9161 OPT_flto_EQ};
9162 const llvm::DenseSet<unsigned> LinkerOptions{OPT_mllvm, OPT_Zlinker_input};
9163 auto ShouldForward = [&](const llvm::DenseSet<unsigned> &Set, Arg *A) {
9164 return Set.contains(V: A->getOption().getID()) ||
9165 (A->getOption().getGroup().isValid() &&
9166 Set.contains(V: A->getOption().getGroup().getID()));
9167 };
9168
9169 ArgStringList CmdArgs;
9170 for (Action::OffloadKind Kind : {Action::OFK_Cuda, Action::OFK_OpenMP,
9171 Action::OFK_HIP, Action::OFK_SYCL}) {
9172 auto TCRange = C.getOffloadToolChains(Kind);
9173 for (auto &I : llvm::make_range(p: TCRange)) {
9174 const ToolChain *TC = I.second;
9175
9176 // We do not use a bound architecture here so options passed only to a
9177 // specific architecture via -Xarch_<cpu> will not be forwarded.
9178 ArgStringList CompilerArgs;
9179 ArgStringList LinkerArgs;
9180 for (Arg *A : C.getArgsForToolChain(TC, /*BoundArch=*/"", DeviceOffloadKind: Kind)) {
9181 if (A->getOption().matches(ID: OPT_Zlinker_input))
9182 LinkerArgs.emplace_back(Args: A->getValue());
9183 else if (ShouldForward(CompilerOptions, A))
9184 A->render(Args, Output&: CompilerArgs);
9185 else if (ShouldForward(LinkerOptions, A))
9186 A->render(Args, Output&: LinkerArgs);
9187 }
9188
9189 // If this is OpenMP the device linker will need `-lompdevice`.
9190 if (Kind == Action::OFK_OpenMP && !Args.hasArg(Ids: OPT_no_offloadlib) &&
9191 (TC->getTriple().isAMDGPU() || TC->getTriple().isNVPTX()))
9192 LinkerArgs.emplace_back(Args: "-lompdevice");
9193
9194 // Forward all of these to the appropriate toolchain.
9195 for (StringRef Arg : CompilerArgs)
9196 CmdArgs.push_back(Elt: Args.MakeArgString(
9197 Str: "--device-compiler=" + TC->getTripleString() + "=" + Arg));
9198 for (StringRef Arg : LinkerArgs)
9199 CmdArgs.push_back(Elt: Args.MakeArgString(
9200 Str: "--device-linker=" + TC->getTripleString() + "=" + Arg));
9201
9202 // Forward the LTO mode relying on the Driver's parsing.
9203 if (C.getDriver().getOffloadLTOMode() == LTOK_Full)
9204 CmdArgs.push_back(Elt: Args.MakeArgString(
9205 Str: "--device-compiler=" + TC->getTripleString() + "=-flto=full"));
9206 else if (C.getDriver().getOffloadLTOMode() == LTOK_Thin) {
9207 CmdArgs.push_back(Elt: Args.MakeArgString(
9208 Str: "--device-compiler=" + TC->getTripleString() + "=-flto=thin"));
9209 if (TC->getTriple().isAMDGPU()) {
9210 CmdArgs.push_back(
9211 Elt: Args.MakeArgString(Str: "--device-linker=" + TC->getTripleString() +
9212 "=-plugin-opt=-force-import-all"));
9213 CmdArgs.push_back(
9214 Elt: Args.MakeArgString(Str: "--device-linker=" + TC->getTripleString() +
9215 "=-plugin-opt=-avail-extern-to-local"));
9216 CmdArgs.push_back(Elt: Args.MakeArgString(
9217 Str: "--device-linker=" + TC->getTripleString() +
9218 "=-plugin-opt=-avail-extern-gv-in-addrspace-to-local=3"));
9219 if (Kind == Action::OFK_OpenMP) {
9220 CmdArgs.push_back(
9221 Elt: Args.MakeArgString(Str: "--device-linker=" + TC->getTripleString() +
9222 "=-plugin-opt=-amdgpu-internalize-symbols"));
9223 }
9224 }
9225 }
9226 }
9227 }
9228
9229 CmdArgs.push_back(
9230 Elt: Args.MakeArgString(Str: "--host-triple=" + getToolChain().getTripleString()));
9231 if (Args.hasArg(Ids: options::OPT_v))
9232 CmdArgs.push_back(Elt: "--wrapper-verbose");
9233 if (Arg *A = Args.getLastArg(Ids: options::OPT_cuda_path_EQ))
9234 CmdArgs.push_back(
9235 Elt: Args.MakeArgString(Str: Twine("--cuda-path=") + A->getValue()));
9236
9237 // Construct the link job so we can wrap around it.
9238 Linker->ConstructJob(C, JA, Output, Inputs, TCArgs: Args, LinkingOutput);
9239 const auto &LinkCommand = C.getJobs().getJobs().back();
9240
9241 // Forward -Xoffload-linker<-triple> arguments to the device link job.
9242 for (Arg *A : Args.filtered(Ids: options::OPT_Xoffload_linker)) {
9243 StringRef Val = A->getValue(N: 0);
9244 if (Val.empty())
9245 CmdArgs.push_back(
9246 Elt: Args.MakeArgString(Str: Twine("--device-linker=") + A->getValue(N: 1)));
9247 else
9248 CmdArgs.push_back(Elt: Args.MakeArgString(
9249 Str: "--device-linker=" +
9250 ToolChain::getOpenMPTriple(TripleStr: Val.drop_front()).getTriple() + "=" +
9251 A->getValue(N: 1)));
9252 }
9253 Args.ClaimAllArgs(Id0: options::OPT_Xoffload_linker);
9254
9255 // Embed bitcode instead of an object in JIT mode.
9256 if (Args.hasFlag(Pos: options::OPT_fopenmp_target_jit,
9257 Neg: options::OPT_fno_openmp_target_jit, Default: false))
9258 CmdArgs.push_back(Elt: "--embed-bitcode");
9259
9260 // Save temporary files created by the linker wrapper.
9261 if (Args.hasArg(Ids: options::OPT_save_temps_EQ) ||
9262 Args.hasArg(Ids: options::OPT_save_temps))
9263 CmdArgs.push_back(Elt: "--save-temps");
9264
9265 // Pass in the C library for GPUs if present and not disabled.
9266 if (Args.hasFlag(Pos: options::OPT_offloadlib, Neg: OPT_no_offloadlib, Default: true) &&
9267 !Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_r,
9268 Ids: options::OPT_nodefaultlibs, Ids: options::OPT_nolibc,
9269 Ids: options::OPT_nogpulibc)) {
9270 forAllAssociatedToolChains(C, JA, RegularToolChain: getToolChain(), Work: [&](const ToolChain &TC) {
9271 // The device C library is only available for NVPTX and AMDGPU targets
9272 // currently.
9273 if (!TC.getTriple().isNVPTX() && !TC.getTriple().isAMDGPU())
9274 return;
9275 bool HasLibC = TC.getStdlibIncludePath().has_value();
9276 if (HasLibC) {
9277 CmdArgs.push_back(Elt: Args.MakeArgString(
9278 Str: "--device-linker=" + TC.getTripleString() + "=" + "-lc"));
9279 CmdArgs.push_back(Elt: Args.MakeArgString(
9280 Str: "--device-linker=" + TC.getTripleString() + "=" + "-lm"));
9281 }
9282 auto HasCompilerRT = getToolChain().getVFS().exists(
9283 Path: TC.getCompilerRT(Args, Component: "builtins", Type: ToolChain::FT_Static));
9284 if (HasCompilerRT)
9285 CmdArgs.push_back(
9286 Elt: Args.MakeArgString(Str: "--device-linker=" + TC.getTripleString() + "=" +
9287 "-lclang_rt.builtins"));
9288 bool HasFlangRT = HasCompilerRT && C.getDriver().IsFlangMode();
9289 if (HasFlangRT)
9290 CmdArgs.push_back(
9291 Elt: Args.MakeArgString(Str: "--device-linker=" + TC.getTripleString() + "=" +
9292 "-lflang_rt.runtime"));
9293 });
9294 }
9295
9296 // Add the linker arguments to be forwarded by the wrapper.
9297 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("--linker-path=") +
9298 LinkCommand->getExecutable()));
9299
9300 // We use action type to differentiate two use cases of the linker wrapper.
9301 // TY_Image for normal linker wrapper work.
9302 // TY_Object for HIP fno-gpu-rdc embedding device binary in a relocatable
9303 // object.
9304 assert(JA.getType() == types::TY_Object || JA.getType() == types::TY_Image);
9305 if (JA.getType() == types::TY_Object) {
9306 CmdArgs.append(IL: {"-o", Output.getFilename()});
9307 for (auto Input : Inputs)
9308 CmdArgs.push_back(Elt: Input.getFilename());
9309 CmdArgs.push_back(Elt: "-r");
9310 } else
9311 for (const char *LinkArg : LinkCommand->getArguments())
9312 CmdArgs.push_back(Elt: LinkArg);
9313
9314 addOffloadCompressArgs(TCArgs: Args, CmdArgs);
9315
9316 if (Arg *A = Args.getLastArg(Ids: options::OPT_offload_jobs_EQ)) {
9317 int NumThreads;
9318 if (StringRef(A->getValue()).getAsInteger(Radix: 10, Result&: NumThreads) ||
9319 NumThreads <= 0)
9320 C.getDriver().Diag(DiagID: diag::err_drv_invalid_int_value)
9321 << A->getAsString(Args) << A->getValue();
9322 else
9323 CmdArgs.push_back(
9324 Elt: Args.MakeArgString(Str: "--wrapper-jobs=" + Twine(NumThreads)));
9325 }
9326
9327 const char *Exec =
9328 Args.MakeArgString(Str: getToolChain().GetProgramPath(Name: "clang-linker-wrapper"));
9329
9330 // Replace the executable and arguments of the link job with the
9331 // wrapper.
9332 LinkCommand->replaceExecutable(Exe: Exec);
9333 LinkCommand->replaceArguments(List: CmdArgs);
9334}
9335