blob: 4f475b121f62e080ab2211190f4bba865f9e60fc [file] [log] [blame]
Fangrui Song524b3c12019-03-01 06:49:51 +00001//===-- Clang.cpp - Clang+LLVM ToolChain Implementations --------*- C++ -*-===//
David L. Jonesf561aba2017-03-08 01:02:16 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David L. Jonesf561aba2017-03-08 01:02:16 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "Clang.h"
10#include "Arch/AArch64.h"
11#include "Arch/ARM.h"
12#include "Arch/Mips.h"
13#include "Arch/PPC.h"
Alex Bradbury71f45452018-01-11 13:36:56 +000014#include "Arch/RISCV.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000015#include "Arch/Sparc.h"
16#include "Arch/SystemZ.h"
17#include "Arch/X86.h"
Konstantin Zhuravlyov8914a6d2017-11-10 19:09:57 +000018#include "AMDGPU.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000019#include "CommonArgs.h"
20#include "Hexagon.h"
Anton Korobeynikov93165d62019-01-15 19:44:05 +000021#include "MSP430.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000022#include "InputInfo.h"
23#include "PS4CPU.h"
24#include "clang/Basic/CharInfo.h"
Yuanfang Chenff22ec32019-07-20 22:50:50 +000025#include "clang/Basic/CodeGenOptions.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000026#include "clang/Basic/LangOptions.h"
27#include "clang/Basic/ObjCRuntime.h"
28#include "clang/Basic/Version.h"
Michal Gornydae01c32018-12-23 15:07:26 +000029#include "clang/Driver/Distro.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000030#include "clang/Driver/DriverDiagnostic.h"
31#include "clang/Driver/Options.h"
32#include "clang/Driver/SanitizerArgs.h"
Dean Michael Berris835832d2017-03-30 00:29:36 +000033#include "clang/Driver/XRayArgs.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000034#include "llvm/ADT/StringExtras.h"
Nico Weberd637c052018-04-30 13:52:15 +000035#include "llvm/Config/llvm-config.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000036#include "llvm/Option/ArgList.h"
37#include "llvm/Support/CodeGen.h"
38#include "llvm/Support/Compression.h"
39#include "llvm/Support/FileSystem.h"
40#include "llvm/Support/Path.h"
41#include "llvm/Support/Process.h"
Eric Christopher53b2cb72017-06-30 00:03:56 +000042#include "llvm/Support/TargetParser.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000043#include "llvm/Support/YAMLParser.h"
44
45#ifdef LLVM_ON_UNIX
46#include <unistd.h> // For getuid().
47#endif
48
49using namespace clang::driver;
50using namespace clang::driver::tools;
51using namespace clang;
52using namespace llvm::opt;
53
54static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
55 if (Arg *A =
56 Args.getLastArg(clang::driver::options::OPT_C, options::OPT_CC)) {
57 if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_P) &&
58 !Args.hasArg(options::OPT__SLASH_EP) && !D.CCCIsCPP()) {
59 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
60 << A->getBaseArg().getAsString(Args)
61 << (D.IsCLMode() ? "/E, /P or /EP" : "-E");
62 }
63 }
64}
65
66static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
67 // In gcc, only ARM checks this, but it seems reasonable to check universally.
68 if (Args.hasArg(options::OPT_static))
69 if (const Arg *A =
70 Args.getLastArg(options::OPT_dynamic, options::OPT_mdynamic_no_pic))
71 D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args)
72 << "-static";
73}
74
75// Add backslashes to escape spaces and other backslashes.
76// This is used for the space-separated argument list specified with
77// the -dwarf-debug-flags option.
78static void EscapeSpacesAndBackslashes(const char *Arg,
79 SmallVectorImpl<char> &Res) {
80 for (; *Arg; ++Arg) {
81 switch (*Arg) {
82 default:
83 break;
84 case ' ':
85 case '\\':
86 Res.push_back('\\');
87 break;
88 }
89 Res.push_back(*Arg);
90 }
91}
92
93// Quote target names for inclusion in GNU Make dependency files.
94// Only the characters '$', '#', ' ', '\t' are quoted.
95static void QuoteTarget(StringRef Target, SmallVectorImpl<char> &Res) {
96 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
97 switch (Target[i]) {
98 case ' ':
99 case '\t':
100 // Escape the preceding backslashes
101 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
102 Res.push_back('\\');
103
104 // Escape the space/tab
105 Res.push_back('\\');
106 break;
107 case '$':
108 Res.push_back('$');
109 break;
110 case '#':
111 Res.push_back('\\');
112 break;
113 default:
114 break;
115 }
116
117 Res.push_back(Target[i]);
118 }
119}
120
121/// Apply \a Work on the current tool chain \a RegularToolChain and any other
122/// offloading tool chain that is associated with the current action \a JA.
123static void
124forAllAssociatedToolChains(Compilation &C, const JobAction &JA,
125 const ToolChain &RegularToolChain,
126 llvm::function_ref<void(const ToolChain &)> Work) {
127 // Apply Work on the current/regular tool chain.
128 Work(RegularToolChain);
129
130 // Apply Work on all the offloading tool chains associated with the current
131 // action.
132 if (JA.isHostOffloading(Action::OFK_Cuda))
133 Work(*C.getSingleOffloadToolChain<Action::OFK_Cuda>());
134 else if (JA.isDeviceOffloading(Action::OFK_Cuda))
135 Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
Yaxun Liu398612b2018-05-08 21:02:12 +0000136 else if (JA.isHostOffloading(Action::OFK_HIP))
137 Work(*C.getSingleOffloadToolChain<Action::OFK_HIP>());
138 else if (JA.isDeviceOffloading(Action::OFK_HIP))
139 Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
David L. Jonesf561aba2017-03-08 01:02:16 +0000140
Gheorghe-Teodor Bercea59d7b772017-06-29 15:49:03 +0000141 if (JA.isHostOffloading(Action::OFK_OpenMP)) {
142 auto TCs = C.getOffloadToolChains<Action::OFK_OpenMP>();
143 for (auto II = TCs.first, IE = TCs.second; II != IE; ++II)
144 Work(*II->second);
145 } else if (JA.isDeviceOffloading(Action::OFK_OpenMP))
146 Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
147
David L. Jonesf561aba2017-03-08 01:02:16 +0000148 //
149 // TODO: Add support for other offloading programming models here.
150 //
151}
152
153/// This is a helper function for validating the optional refinement step
154/// parameter in reciprocal argument strings. Return false if there is an error
155/// parsing the refinement step. Otherwise, return true and set the Position
156/// of the refinement step in the input string.
157static bool getRefinementStep(StringRef In, const Driver &D,
158 const Arg &A, size_t &Position) {
159 const char RefinementStepToken = ':';
160 Position = In.find(RefinementStepToken);
161 if (Position != StringRef::npos) {
162 StringRef Option = A.getOption().getName();
163 StringRef RefStep = In.substr(Position + 1);
164 // Allow exactly one numeric character for the additional refinement
165 // step parameter. This is reasonable for all currently-supported
166 // operations and architectures because we would expect that a larger value
167 // of refinement steps would cause the estimate "optimization" to
168 // under-perform the native operation. Also, if the estimate does not
169 // converge quickly, it probably will not ever converge, so further
170 // refinement steps will not produce a better answer.
171 if (RefStep.size() != 1) {
172 D.Diag(diag::err_drv_invalid_value) << Option << RefStep;
173 return false;
174 }
175 char RefStepChar = RefStep[0];
176 if (RefStepChar < '0' || RefStepChar > '9') {
177 D.Diag(diag::err_drv_invalid_value) << Option << RefStep;
178 return false;
179 }
180 }
181 return true;
182}
183
184/// The -mrecip flag requires processing of many optional parameters.
185static void ParseMRecip(const Driver &D, const ArgList &Args,
186 ArgStringList &OutStrings) {
187 StringRef DisabledPrefixIn = "!";
188 StringRef DisabledPrefixOut = "!";
189 StringRef EnabledPrefixOut = "";
190 StringRef Out = "-mrecip=";
191
192 Arg *A = Args.getLastArg(options::OPT_mrecip, options::OPT_mrecip_EQ);
193 if (!A)
194 return;
195
196 unsigned NumOptions = A->getNumValues();
197 if (NumOptions == 0) {
198 // No option is the same as "all".
199 OutStrings.push_back(Args.MakeArgString(Out + "all"));
200 return;
201 }
202
203 // Pass through "all", "none", or "default" with an optional refinement step.
204 if (NumOptions == 1) {
205 StringRef Val = A->getValue(0);
206 size_t RefStepLoc;
207 if (!getRefinementStep(Val, D, *A, RefStepLoc))
208 return;
209 StringRef ValBase = Val.slice(0, RefStepLoc);
210 if (ValBase == "all" || ValBase == "none" || ValBase == "default") {
211 OutStrings.push_back(Args.MakeArgString(Out + Val));
212 return;
213 }
214 }
215
216 // Each reciprocal type may be enabled or disabled individually.
217 // Check each input value for validity, concatenate them all back together,
218 // and pass through.
219
220 llvm::StringMap<bool> OptionStrings;
221 OptionStrings.insert(std::make_pair("divd", false));
222 OptionStrings.insert(std::make_pair("divf", false));
223 OptionStrings.insert(std::make_pair("vec-divd", false));
224 OptionStrings.insert(std::make_pair("vec-divf", false));
225 OptionStrings.insert(std::make_pair("sqrtd", false));
226 OptionStrings.insert(std::make_pair("sqrtf", false));
227 OptionStrings.insert(std::make_pair("vec-sqrtd", false));
228 OptionStrings.insert(std::make_pair("vec-sqrtf", false));
229
230 for (unsigned i = 0; i != NumOptions; ++i) {
231 StringRef Val = A->getValue(i);
232
233 bool IsDisabled = Val.startswith(DisabledPrefixIn);
234 // Ignore the disablement token for string matching.
235 if (IsDisabled)
236 Val = Val.substr(1);
237
238 size_t RefStep;
239 if (!getRefinementStep(Val, D, *A, RefStep))
240 return;
241
242 StringRef ValBase = Val.slice(0, RefStep);
243 llvm::StringMap<bool>::iterator OptionIter = OptionStrings.find(ValBase);
244 if (OptionIter == OptionStrings.end()) {
245 // Try again specifying float suffix.
246 OptionIter = OptionStrings.find(ValBase.str() + 'f');
247 if (OptionIter == OptionStrings.end()) {
248 // The input name did not match any known option string.
249 D.Diag(diag::err_drv_unknown_argument) << Val;
250 return;
251 }
252 // The option was specified without a float or double suffix.
253 // Make sure that the double entry was not already specified.
254 // The float entry will be checked below.
255 if (OptionStrings[ValBase.str() + 'd']) {
256 D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val;
257 return;
258 }
259 }
260
261 if (OptionIter->second == true) {
262 // Duplicate option specified.
263 D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val;
264 return;
265 }
266
267 // Mark the matched option as found. Do not allow duplicate specifiers.
268 OptionIter->second = true;
269
270 // If the precision was not specified, also mark the double entry as found.
271 if (ValBase.back() != 'f' && ValBase.back() != 'd')
272 OptionStrings[ValBase.str() + 'd'] = true;
273
274 // Build the output string.
275 StringRef Prefix = IsDisabled ? DisabledPrefixOut : EnabledPrefixOut;
276 Out = Args.MakeArgString(Out + Prefix + Val);
277 if (i != NumOptions - 1)
278 Out = Args.MakeArgString(Out + ",");
279 }
280
281 OutStrings.push_back(Args.MakeArgString(Out));
282}
283
Craig Topper9a724aa2017-12-11 21:09:19 +0000284/// The -mprefer-vector-width option accepts either a positive integer
285/// or the string "none".
286static void ParseMPreferVectorWidth(const Driver &D, const ArgList &Args,
287 ArgStringList &CmdArgs) {
288 Arg *A = Args.getLastArg(options::OPT_mprefer_vector_width_EQ);
289 if (!A)
290 return;
291
292 StringRef Value = A->getValue();
293 if (Value == "none") {
294 CmdArgs.push_back("-mprefer-vector-width=none");
295 } else {
296 unsigned Width;
297 if (Value.getAsInteger(10, Width)) {
298 D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value;
299 return;
300 }
301 CmdArgs.push_back(Args.MakeArgString("-mprefer-vector-width=" + Value));
302 }
303}
304
David L. Jonesf561aba2017-03-08 01:02:16 +0000305static void getWebAssemblyTargetFeatures(const ArgList &Args,
306 std::vector<StringRef> &Features) {
307 handleTargetFeaturesGroup(Args, Features, options::OPT_m_wasm_Features_Group);
308}
309
David L. Jonesf561aba2017-03-08 01:02:16 +0000310static void getTargetFeatures(const ToolChain &TC, const llvm::Triple &Triple,
311 const ArgList &Args, ArgStringList &CmdArgs,
312 bool ForAS) {
313 const Driver &D = TC.getDriver();
314 std::vector<StringRef> Features;
315 switch (Triple.getArch()) {
316 default:
317 break;
318 case llvm::Triple::mips:
319 case llvm::Triple::mipsel:
320 case llvm::Triple::mips64:
321 case llvm::Triple::mips64el:
322 mips::getMIPSTargetFeatures(D, Triple, Args, Features);
323 break;
324
325 case llvm::Triple::arm:
326 case llvm::Triple::armeb:
327 case llvm::Triple::thumb:
328 case llvm::Triple::thumbeb:
329 arm::getARMTargetFeatures(TC, Triple, Args, CmdArgs, Features, ForAS);
330 break;
331
332 case llvm::Triple::ppc:
333 case llvm::Triple::ppc64:
334 case llvm::Triple::ppc64le:
335 ppc::getPPCTargetFeatures(D, Triple, Args, Features);
336 break;
Alex Bradbury71f45452018-01-11 13:36:56 +0000337 case llvm::Triple::riscv32:
338 case llvm::Triple::riscv64:
339 riscv::getRISCVTargetFeatures(D, Args, Features);
340 break;
David L. Jonesf561aba2017-03-08 01:02:16 +0000341 case llvm::Triple::systemz:
342 systemz::getSystemZTargetFeatures(Args, Features);
343 break;
344 case llvm::Triple::aarch64:
345 case llvm::Triple::aarch64_be:
Alex Lorenz9b20a992018-12-17 19:30:46 +0000346 aarch64::getAArch64TargetFeatures(D, Triple, Args, Features);
David L. Jonesf561aba2017-03-08 01:02:16 +0000347 break;
348 case llvm::Triple::x86:
349 case llvm::Triple::x86_64:
350 x86::getX86TargetFeatures(D, Triple, Args, Features);
351 break;
352 case llvm::Triple::hexagon:
Sumanth Gundapaneni57098f52017-10-18 18:10:13 +0000353 hexagon::getHexagonTargetFeatures(D, Args, Features);
David L. Jonesf561aba2017-03-08 01:02:16 +0000354 break;
355 case llvm::Triple::wasm32:
356 case llvm::Triple::wasm64:
357 getWebAssemblyTargetFeatures(Args, Features);
358 break;
359 case llvm::Triple::sparc:
360 case llvm::Triple::sparcel:
361 case llvm::Triple::sparcv9:
362 sparc::getSparcTargetFeatures(D, Args, Features);
363 break;
364 case llvm::Triple::r600:
365 case llvm::Triple::amdgcn:
Konstantin Zhuravlyov8914a6d2017-11-10 19:09:57 +0000366 amdgpu::getAMDGPUTargetFeatures(D, Args, Features);
David L. Jonesf561aba2017-03-08 01:02:16 +0000367 break;
Anton Korobeynikov93165d62019-01-15 19:44:05 +0000368 case llvm::Triple::msp430:
369 msp430::getMSP430TargetFeatures(D, Args, Features);
David L. Jonesf561aba2017-03-08 01:02:16 +0000370 }
371
372 // Find the last of each feature.
373 llvm::StringMap<unsigned> LastOpt;
374 for (unsigned I = 0, N = Features.size(); I < N; ++I) {
375 StringRef Name = Features[I];
376 assert(Name[0] == '-' || Name[0] == '+');
377 LastOpt[Name.drop_front(1)] = I;
378 }
379
380 for (unsigned I = 0, N = Features.size(); I < N; ++I) {
381 // If this feature was overridden, ignore it.
382 StringRef Name = Features[I];
383 llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name.drop_front(1));
384 assert(LastI != LastOpt.end());
385 unsigned Last = LastI->second;
386 if (Last != I)
387 continue;
388
389 CmdArgs.push_back("-target-feature");
390 CmdArgs.push_back(Name.data());
391 }
392}
393
394static bool
395shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
396 const llvm::Triple &Triple) {
397 // We use the zero-cost exception tables for Objective-C if the non-fragile
398 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
399 // later.
400 if (runtime.isNonFragile())
401 return true;
402
403 if (!Triple.isMacOSX())
404 return false;
405
406 return (!Triple.isMacOSXVersionLT(10, 5) &&
407 (Triple.getArch() == llvm::Triple::x86_64 ||
408 Triple.getArch() == llvm::Triple::arm));
409}
410
411/// Adds exception related arguments to the driver command arguments. There's a
412/// master flag, -fexceptions and also language specific flags to enable/disable
413/// C++ and Objective-C exceptions. This makes it possible to for example
414/// disable C++ exceptions but enable Objective-C exceptions.
415static void addExceptionArgs(const ArgList &Args, types::ID InputType,
416 const ToolChain &TC, bool KernelOrKext,
417 const ObjCRuntime &objcRuntime,
418 ArgStringList &CmdArgs) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000419 const llvm::Triple &Triple = TC.getTriple();
420
421 if (KernelOrKext) {
422 // -mkernel and -fapple-kext imply no exceptions, so claim exception related
423 // arguments now to avoid warnings about unused arguments.
424 Args.ClaimAllArgs(options::OPT_fexceptions);
425 Args.ClaimAllArgs(options::OPT_fno_exceptions);
426 Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
427 Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
428 Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
429 Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
430 return;
431 }
432
433 // See if the user explicitly enabled exceptions.
434 bool EH = Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
435 false);
436
437 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
438 // is not necessarily sensible, but follows GCC.
439 if (types::isObjC(InputType) &&
440 Args.hasFlag(options::OPT_fobjc_exceptions,
441 options::OPT_fno_objc_exceptions, true)) {
442 CmdArgs.push_back("-fobjc-exceptions");
443
444 EH |= shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
445 }
446
447 if (types::isCXX(InputType)) {
448 // Disable C++ EH by default on XCore and PS4.
449 bool CXXExceptionsEnabled =
450 Triple.getArch() != llvm::Triple::xcore && !Triple.isPS4CPU();
451 Arg *ExceptionArg = Args.getLastArg(
452 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
453 options::OPT_fexceptions, options::OPT_fno_exceptions);
454 if (ExceptionArg)
455 CXXExceptionsEnabled =
456 ExceptionArg->getOption().matches(options::OPT_fcxx_exceptions) ||
457 ExceptionArg->getOption().matches(options::OPT_fexceptions);
458
459 if (CXXExceptionsEnabled) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000460 CmdArgs.push_back("-fcxx-exceptions");
461
462 EH = true;
463 }
464 }
465
466 if (EH)
467 CmdArgs.push_back("-fexceptions");
468}
469
470static bool ShouldDisableAutolink(const ArgList &Args, const ToolChain &TC) {
471 bool Default = true;
472 if (TC.getTriple().isOSDarwin()) {
473 // The native darwin assembler doesn't support the linker_option directives,
474 // so we disable them if we think the .s file will be passed to it.
475 Default = TC.useIntegratedAs();
476 }
477 return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
478 Default);
479}
480
481static bool ShouldDisableDwarfDirectory(const ArgList &Args,
482 const ToolChain &TC) {
483 bool UseDwarfDirectory =
484 Args.hasFlag(options::OPT_fdwarf_directory_asm,
485 options::OPT_fno_dwarf_directory_asm, TC.useIntegratedAs());
486 return !UseDwarfDirectory;
487}
488
489// Convert an arg of the form "-gN" or "-ggdbN" or one of their aliases
490// to the corresponding DebugInfoKind.
491static codegenoptions::DebugInfoKind DebugLevelToInfoKind(const Arg &A) {
492 assert(A.getOption().matches(options::OPT_gN_Group) &&
493 "Not a -g option that specifies a debug-info level");
494 if (A.getOption().matches(options::OPT_g0) ||
495 A.getOption().matches(options::OPT_ggdb0))
496 return codegenoptions::NoDebugInfo;
497 if (A.getOption().matches(options::OPT_gline_tables_only) ||
498 A.getOption().matches(options::OPT_ggdb1))
499 return codegenoptions::DebugLineTablesOnly;
Alexey Bataev80e1b5e2018-08-31 13:56:14 +0000500 if (A.getOption().matches(options::OPT_gline_directives_only))
501 return codegenoptions::DebugDirectivesOnly;
David L. Jonesf561aba2017-03-08 01:02:16 +0000502 return codegenoptions::LimitedDebugInfo;
503}
504
505static bool mustUseNonLeafFramePointerForTarget(const llvm::Triple &Triple) {
506 switch (Triple.getArch()){
507 default:
508 return false;
509 case llvm::Triple::arm:
510 case llvm::Triple::thumb:
511 // ARM Darwin targets require a frame pointer to be always present to aid
512 // offline debugging via backtraces.
513 return Triple.isOSDarwin();
514 }
515}
516
517static bool useFramePointerForTargetByDefault(const ArgList &Args,
518 const llvm::Triple &Triple) {
Fangrui Songdc039662019-07-12 02:01:51 +0000519 if (Args.hasArg(options::OPT_pg))
520 return true;
521
David L. Jonesf561aba2017-03-08 01:02:16 +0000522 switch (Triple.getArch()) {
523 case llvm::Triple::xcore:
524 case llvm::Triple::wasm32:
525 case llvm::Triple::wasm64:
Anton Korobeynikovf1f897c2019-02-05 20:15:03 +0000526 case llvm::Triple::msp430:
David L. Jonesf561aba2017-03-08 01:02:16 +0000527 // XCore never wants frame pointers, regardless of OS.
528 // WebAssembly never wants frame pointers.
529 return false;
Fangrui Song8c0b58f2019-07-12 02:14:08 +0000530 case llvm::Triple::ppc:
531 case llvm::Triple::ppc64:
532 case llvm::Triple::ppc64le:
Mandeep Singh Grang0c5300a2018-04-12 19:31:37 +0000533 case llvm::Triple::riscv32:
534 case llvm::Triple::riscv64:
535 return !areOptimizationsEnabled(Args);
David L. Jonesf561aba2017-03-08 01:02:16 +0000536 default:
537 break;
538 }
539
Michal Gorny5a409d02018-12-20 13:09:30 +0000540 if (Triple.isOSNetBSD()) {
Joerg Sonnenberger2ad82102018-07-17 12:38:57 +0000541 return !areOptimizationsEnabled(Args);
542 }
543
Kristina Brooks77a4adc2018-11-29 03:49:14 +0000544 if (Triple.isOSLinux() || Triple.getOS() == llvm::Triple::CloudABI ||
545 Triple.isOSHurd()) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000546 switch (Triple.getArch()) {
547 // Don't use a frame pointer on linux if optimizing for certain targets.
548 case llvm::Triple::mips64:
549 case llvm::Triple::mips64el:
550 case llvm::Triple::mips:
551 case llvm::Triple::mipsel:
David L. Jonesf561aba2017-03-08 01:02:16 +0000552 case llvm::Triple::systemz:
553 case llvm::Triple::x86:
554 case llvm::Triple::x86_64:
555 return !areOptimizationsEnabled(Args);
556 default:
557 return true;
558 }
559 }
560
561 if (Triple.isOSWindows()) {
562 switch (Triple.getArch()) {
563 case llvm::Triple::x86:
564 return !areOptimizationsEnabled(Args);
565 case llvm::Triple::x86_64:
566 return Triple.isOSBinFormatMachO();
567 case llvm::Triple::arm:
568 case llvm::Triple::thumb:
569 // Windows on ARM builds with FPO disabled to aid fast stack walking
570 return true;
571 default:
572 // All other supported Windows ISAs use xdata unwind information, so frame
573 // pointers are not generally useful.
574 return false;
575 }
576 }
577
578 return true;
579}
580
Yuanfang Chenff22ec32019-07-20 22:50:50 +0000581static CodeGenOptions::FramePointerKind
582getFramePointerKind(const ArgList &Args, const llvm::Triple &Triple) {
Nico Weber2346b922019-08-13 17:37:09 +0000583 // We have 4 states:
584 //
585 // 00) leaf retained, non-leaf retained
586 // 01) leaf retained, non-leaf omitted (this is invalid)
587 // 10) leaf omitted, non-leaf retained
588 // (what -momit-leaf-frame-pointer was designed for)
589 // 11) leaf omitted, non-leaf omitted
590 //
591 // "omit" options taking precedence over "no-omit" options is the only way
592 // to make 3 valid states representable
Fangrui Songdc039662019-07-12 02:01:51 +0000593 Arg *A = Args.getLastArg(options::OPT_fomit_frame_pointer,
594 options::OPT_fno_omit_frame_pointer);
595 bool OmitFP = A && A->getOption().matches(options::OPT_fomit_frame_pointer);
596 bool NoOmitFP =
597 A && A->getOption().matches(options::OPT_fno_omit_frame_pointer);
Nico Weber2346b922019-08-13 17:37:09 +0000598 bool KeepLeaf =
599 Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
600 options::OPT_mno_omit_leaf_frame_pointer, Triple.isPS4CPU());
Fangrui Songdc039662019-07-12 02:01:51 +0000601 if (NoOmitFP || mustUseNonLeafFramePointerForTarget(Triple) ||
602 (!OmitFP && useFramePointerForTargetByDefault(Args, Triple))) {
Nico Weber2346b922019-08-13 17:37:09 +0000603 if (KeepLeaf)
Yuanfang Chenff22ec32019-07-20 22:50:50 +0000604 return CodeGenOptions::FramePointerKind::NonLeaf;
605 return CodeGenOptions::FramePointerKind::All;
Fangrui Songdc039662019-07-12 02:01:51 +0000606 }
Yuanfang Chenff22ec32019-07-20 22:50:50 +0000607 return CodeGenOptions::FramePointerKind::None;
David L. Jonesf561aba2017-03-08 01:02:16 +0000608}
609
610/// Add a CC1 option to specify the debug compilation directory.
Michael J. Spencer7e48b402019-05-28 22:21:47 +0000611static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs,
612 const llvm::vfs::FileSystem &VFS) {
Nico Weber37b75332019-06-17 12:10:40 +0000613 if (Arg *A = Args.getLastArg(options::OPT_fdebug_compilation_dir)) {
614 CmdArgs.push_back("-fdebug-compilation-dir");
615 CmdArgs.push_back(A->getValue());
616 } else if (llvm::ErrorOr<std::string> CWD =
617 VFS.getCurrentWorkingDirectory()) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000618 CmdArgs.push_back("-fdebug-compilation-dir");
Michael J. Spencer7e48b402019-05-28 22:21:47 +0000619 CmdArgs.push_back(Args.MakeArgString(*CWD));
David L. Jonesf561aba2017-03-08 01:02:16 +0000620 }
621}
622
Paul Robinson9b292b42018-07-10 15:15:24 +0000623/// Add a CC1 and CC1AS option to specify the debug file path prefix map.
624static void addDebugPrefixMapArg(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs) {
625 for (const Arg *A : Args.filtered(options::OPT_fdebug_prefix_map_EQ)) {
626 StringRef Map = A->getValue();
627 if (Map.find('=') == StringRef::npos)
628 D.Diag(diag::err_drv_invalid_argument_to_fdebug_prefix_map) << Map;
629 else
630 CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map));
631 A->claim();
632 }
633}
634
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000635/// Vectorize at all optimization levels greater than 1 except for -Oz.
Nico Weber37b75332019-06-17 12:10:40 +0000636/// For -Oz the loop vectorizer is disabled, while the slp vectorizer is
637/// enabled.
David L. Jonesf561aba2017-03-08 01:02:16 +0000638static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) {
639 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
640 if (A->getOption().matches(options::OPT_O4) ||
641 A->getOption().matches(options::OPT_Ofast))
642 return true;
643
644 if (A->getOption().matches(options::OPT_O0))
645 return false;
646
647 assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
648
649 // Vectorize -Os.
650 StringRef S(A->getValue());
651 if (S == "s")
652 return true;
653
654 // Don't vectorize -Oz, unless it's the slp vectorizer.
655 if (S == "z")
656 return isSlpVec;
657
658 unsigned OptLevel = 0;
659 if (S.getAsInteger(10, OptLevel))
660 return false;
661
662 return OptLevel > 1;
663 }
664
665 return false;
666}
667
668/// Add -x lang to \p CmdArgs for \p Input.
669static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
670 ArgStringList &CmdArgs) {
671 // When using -verify-pch, we don't want to provide the type
672 // 'precompiled-header' if it was inferred from the file extension
673 if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH)
674 return;
675
676 CmdArgs.push_back("-x");
677 if (Args.hasArg(options::OPT_rewrite_objc))
678 CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
Richard Smith34e485f2017-04-18 21:55:37 +0000679 else {
680 // Map the driver type to the frontend type. This is mostly an identity
681 // mapping, except that the distinction between module interface units
682 // and other source files does not exist at the frontend layer.
683 const char *ClangType;
684 switch (Input.getType()) {
685 case types::TY_CXXModule:
686 ClangType = "c++";
687 break;
688 case types::TY_PP_CXXModule:
689 ClangType = "c++-cpp-output";
690 break;
691 default:
692 ClangType = types::getTypeName(Input.getType());
693 break;
694 }
695 CmdArgs.push_back(ClangType);
696 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000697}
698
699static void appendUserToPath(SmallVectorImpl<char> &Result) {
700#ifdef LLVM_ON_UNIX
701 const char *Username = getenv("LOGNAME");
702#else
703 const char *Username = getenv("USERNAME");
704#endif
705 if (Username) {
706 // Validate that LoginName can be used in a path, and get its length.
707 size_t Len = 0;
708 for (const char *P = Username; *P; ++P, ++Len) {
709 if (!clang::isAlphanumeric(*P) && *P != '_') {
710 Username = nullptr;
711 break;
712 }
713 }
714
715 if (Username && Len > 0) {
716 Result.append(Username, Username + Len);
717 return;
718 }
719 }
720
721// Fallback to user id.
722#ifdef LLVM_ON_UNIX
723 std::string UID = llvm::utostr(getuid());
724#else
725 // FIXME: Windows seems to have an 'SID' that might work.
726 std::string UID = "9999";
727#endif
728 Result.append(UID.begin(), UID.end());
729}
730
Russell Gallop7a9ccf82019-05-14 14:01:40 +0000731static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
732 const Driver &D, const InputInfo &Output,
733 const ArgList &Args,
David L. Jonesf561aba2017-03-08 01:02:16 +0000734 ArgStringList &CmdArgs) {
735
736 auto *PGOGenerateArg = Args.getLastArg(options::OPT_fprofile_generate,
737 options::OPT_fprofile_generate_EQ,
738 options::OPT_fno_profile_generate);
739 if (PGOGenerateArg &&
740 PGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate))
741 PGOGenerateArg = nullptr;
742
Rong Xua4a09b22019-03-04 20:21:31 +0000743 auto *CSPGOGenerateArg = Args.getLastArg(options::OPT_fcs_profile_generate,
744 options::OPT_fcs_profile_generate_EQ,
745 options::OPT_fno_profile_generate);
746 if (CSPGOGenerateArg &&
747 CSPGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate))
748 CSPGOGenerateArg = nullptr;
749
David L. Jonesf561aba2017-03-08 01:02:16 +0000750 auto *ProfileGenerateArg = Args.getLastArg(
751 options::OPT_fprofile_instr_generate,
752 options::OPT_fprofile_instr_generate_EQ,
753 options::OPT_fno_profile_instr_generate);
754 if (ProfileGenerateArg &&
755 ProfileGenerateArg->getOption().matches(
756 options::OPT_fno_profile_instr_generate))
757 ProfileGenerateArg = nullptr;
758
759 if (PGOGenerateArg && ProfileGenerateArg)
760 D.Diag(diag::err_drv_argument_not_allowed_with)
761 << PGOGenerateArg->getSpelling() << ProfileGenerateArg->getSpelling();
762
763 auto *ProfileUseArg = getLastProfileUseArg(Args);
764
765 if (PGOGenerateArg && ProfileUseArg)
766 D.Diag(diag::err_drv_argument_not_allowed_with)
767 << ProfileUseArg->getSpelling() << PGOGenerateArg->getSpelling();
768
769 if (ProfileGenerateArg && ProfileUseArg)
770 D.Diag(diag::err_drv_argument_not_allowed_with)
771 << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling();
772
Rong Xua4a09b22019-03-04 20:21:31 +0000773 if (CSPGOGenerateArg && PGOGenerateArg)
774 D.Diag(diag::err_drv_argument_not_allowed_with)
775 << CSPGOGenerateArg->getSpelling() << PGOGenerateArg->getSpelling();
776
David L. Jonesf561aba2017-03-08 01:02:16 +0000777 if (ProfileGenerateArg) {
778 if (ProfileGenerateArg->getOption().matches(
779 options::OPT_fprofile_instr_generate_EQ))
780 CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-instrument-path=") +
781 ProfileGenerateArg->getValue()));
782 // The default is to use Clang Instrumentation.
783 CmdArgs.push_back("-fprofile-instrument=clang");
Russell Gallop7a9ccf82019-05-14 14:01:40 +0000784 if (TC.getTriple().isWindowsMSVCEnvironment()) {
785 // Add dependent lib for clang_rt.profile
786 CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
787 TC.getCompilerRT(Args, "profile")));
788 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000789 }
790
Rong Xua4a09b22019-03-04 20:21:31 +0000791 Arg *PGOGenArg = nullptr;
David L. Jonesf561aba2017-03-08 01:02:16 +0000792 if (PGOGenerateArg) {
Rong Xua4a09b22019-03-04 20:21:31 +0000793 assert(!CSPGOGenerateArg);
794 PGOGenArg = PGOGenerateArg;
David L. Jonesf561aba2017-03-08 01:02:16 +0000795 CmdArgs.push_back("-fprofile-instrument=llvm");
Rong Xua4a09b22019-03-04 20:21:31 +0000796 }
797 if (CSPGOGenerateArg) {
798 assert(!PGOGenerateArg);
799 PGOGenArg = CSPGOGenerateArg;
800 CmdArgs.push_back("-fprofile-instrument=csllvm");
801 }
802 if (PGOGenArg) {
Russell Gallop72fea1d2019-05-22 10:06:49 +0000803 if (TC.getTriple().isWindowsMSVCEnvironment()) {
804 CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
805 TC.getCompilerRT(Args, "profile")));
806 }
Rong Xua4a09b22019-03-04 20:21:31 +0000807 if (PGOGenArg->getOption().matches(
808 PGOGenerateArg ? options::OPT_fprofile_generate_EQ
809 : options::OPT_fcs_profile_generate_EQ)) {
810 SmallString<128> Path(PGOGenArg->getValue());
David L. Jonesf561aba2017-03-08 01:02:16 +0000811 llvm::sys::path::append(Path, "default_%m.profraw");
812 CmdArgs.push_back(
813 Args.MakeArgString(Twine("-fprofile-instrument-path=") + Path));
814 }
815 }
816
817 if (ProfileUseArg) {
818 if (ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ))
819 CmdArgs.push_back(Args.MakeArgString(
820 Twine("-fprofile-instrument-use-path=") + ProfileUseArg->getValue()));
821 else if ((ProfileUseArg->getOption().matches(
822 options::OPT_fprofile_use_EQ) ||
823 ProfileUseArg->getOption().matches(
824 options::OPT_fprofile_instr_use))) {
825 SmallString<128> Path(
826 ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue());
827 if (Path.empty() || llvm::sys::fs::is_directory(Path))
828 llvm::sys::path::append(Path, "default.profdata");
829 CmdArgs.push_back(
830 Args.MakeArgString(Twine("-fprofile-instrument-use-path=") + Path));
831 }
832 }
833
834 if (Args.hasArg(options::OPT_ftest_coverage) ||
835 Args.hasArg(options::OPT_coverage))
836 CmdArgs.push_back("-femit-coverage-notes");
837 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
838 false) ||
839 Args.hasArg(options::OPT_coverage))
840 CmdArgs.push_back("-femit-coverage-data");
841
842 if (Args.hasFlag(options::OPT_fcoverage_mapping,
Vedant Kumar99b31292017-06-28 01:56:07 +0000843 options::OPT_fno_coverage_mapping, false)) {
844 if (!ProfileGenerateArg)
845 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
846 << "-fcoverage-mapping"
847 << "-fprofile-instr-generate";
David L. Jonesf561aba2017-03-08 01:02:16 +0000848
David L. Jonesf561aba2017-03-08 01:02:16 +0000849 CmdArgs.push_back("-fcoverage-mapping");
Vedant Kumar99b31292017-06-28 01:56:07 +0000850 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000851
Calixte Denizetf4bf6712018-11-17 19:41:39 +0000852 if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) {
853 auto *Arg = Args.getLastArg(options::OPT_fprofile_exclude_files_EQ);
854 if (!Args.hasArg(options::OPT_coverage))
855 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
856 << "-fprofile-exclude-files="
857 << "--coverage";
858
859 StringRef v = Arg->getValue();
860 CmdArgs.push_back(
861 Args.MakeArgString(Twine("-fprofile-exclude-files=" + v)));
862 }
863
864 if (Args.hasArg(options::OPT_fprofile_filter_files_EQ)) {
865 auto *Arg = Args.getLastArg(options::OPT_fprofile_filter_files_EQ);
866 if (!Args.hasArg(options::OPT_coverage))
867 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
868 << "-fprofile-filter-files="
869 << "--coverage";
870
871 StringRef v = Arg->getValue();
872 CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-filter-files=" + v)));
873 }
874
David L. Jonesf561aba2017-03-08 01:02:16 +0000875 if (C.getArgs().hasArg(options::OPT_c) ||
876 C.getArgs().hasArg(options::OPT_S)) {
877 if (Output.isFilename()) {
878 CmdArgs.push_back("-coverage-notes-file");
879 SmallString<128> OutputFilename;
880 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
881 OutputFilename = FinalOutput->getValue();
882 else
883 OutputFilename = llvm::sys::path::filename(Output.getBaseInput());
884 SmallString<128> CoverageFilename = OutputFilename;
Michael J. Spencer7e48b402019-05-28 22:21:47 +0000885 if (llvm::sys::path::is_relative(CoverageFilename))
886 (void)D.getVFS().makeAbsolute(CoverageFilename);
David L. Jonesf561aba2017-03-08 01:02:16 +0000887 llvm::sys::path::replace_extension(CoverageFilename, "gcno");
888 CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
889
890 // Leave -fprofile-dir= an unused argument unless .gcda emission is
891 // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider
892 // the flag used. There is no -fno-profile-dir, so the user has no
893 // targeted way to suppress the warning.
894 if (Args.hasArg(options::OPT_fprofile_arcs) ||
895 Args.hasArg(options::OPT_coverage)) {
896 CmdArgs.push_back("-coverage-data-file");
897 if (Arg *FProfileDir = Args.getLastArg(options::OPT_fprofile_dir)) {
898 CoverageFilename = FProfileDir->getValue();
899 llvm::sys::path::append(CoverageFilename, OutputFilename);
900 }
901 llvm::sys::path::replace_extension(CoverageFilename, "gcda");
902 CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
903 }
904 }
905 }
906}
907
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000908/// Check whether the given input tree contains any compilation actions.
David L. Jonesf561aba2017-03-08 01:02:16 +0000909static bool ContainsCompileAction(const Action *A) {
910 if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A))
911 return true;
912
913 for (const auto &AI : A->inputs())
914 if (ContainsCompileAction(AI))
915 return true;
916
917 return false;
918}
919
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000920/// Check if -relax-all should be passed to the internal assembler.
David L. Jonesf561aba2017-03-08 01:02:16 +0000921/// This is done by default when compiling non-assembler source with -O0.
922static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
923 bool RelaxDefault = true;
924
925 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
926 RelaxDefault = A->getOption().matches(options::OPT_O0);
927
928 if (RelaxDefault) {
929 RelaxDefault = false;
930 for (const auto &Act : C.getActions()) {
931 if (ContainsCompileAction(Act)) {
932 RelaxDefault = true;
933 break;
934 }
935 }
936 }
937
938 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
939 RelaxDefault);
940}
941
942// Extract the integer N from a string spelled "-dwarf-N", returning 0
943// on mismatch. The StringRef input (rather than an Arg) allows
944// for use by the "-Xassembler" option parser.
945static unsigned DwarfVersionNum(StringRef ArgValue) {
946 return llvm::StringSwitch<unsigned>(ArgValue)
947 .Case("-gdwarf-2", 2)
948 .Case("-gdwarf-3", 3)
949 .Case("-gdwarf-4", 4)
950 .Case("-gdwarf-5", 5)
951 .Default(0);
952}
953
954static void RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs,
955 codegenoptions::DebugInfoKind DebugInfoKind,
956 unsigned DwarfVersion,
957 llvm::DebuggerKind DebuggerTuning) {
958 switch (DebugInfoKind) {
Alexey Bataev80e1b5e2018-08-31 13:56:14 +0000959 case codegenoptions::DebugDirectivesOnly:
960 CmdArgs.push_back("-debug-info-kind=line-directives-only");
961 break;
David L. Jonesf561aba2017-03-08 01:02:16 +0000962 case codegenoptions::DebugLineTablesOnly:
963 CmdArgs.push_back("-debug-info-kind=line-tables-only");
964 break;
965 case codegenoptions::LimitedDebugInfo:
966 CmdArgs.push_back("-debug-info-kind=limited");
967 break;
968 case codegenoptions::FullDebugInfo:
969 CmdArgs.push_back("-debug-info-kind=standalone");
970 break;
971 default:
972 break;
973 }
974 if (DwarfVersion > 0)
975 CmdArgs.push_back(
976 Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion)));
977 switch (DebuggerTuning) {
978 case llvm::DebuggerKind::GDB:
979 CmdArgs.push_back("-debugger-tuning=gdb");
980 break;
981 case llvm::DebuggerKind::LLDB:
982 CmdArgs.push_back("-debugger-tuning=lldb");
983 break;
984 case llvm::DebuggerKind::SCE:
985 CmdArgs.push_back("-debugger-tuning=sce");
986 break;
987 default:
988 break;
989 }
990}
991
Alexey Bataevb83b4e42018-07-27 19:45:14 +0000992static bool checkDebugInfoOption(const Arg *A, const ArgList &Args,
993 const Driver &D, const ToolChain &TC) {
994 assert(A && "Expected non-nullptr argument.");
995 if (TC.supportsDebugInfoOption(A))
996 return true;
997 D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target)
998 << A->getAsString(Args) << TC.getTripleString();
999 return false;
1000}
1001
Saleem Abdulrasoold064e912017-06-23 15:34:16 +00001002static void RenderDebugInfoCompressionArgs(const ArgList &Args,
1003 ArgStringList &CmdArgs,
Alexey Bataevb83b4e42018-07-27 19:45:14 +00001004 const Driver &D,
1005 const ToolChain &TC) {
Saleem Abdulrasoold064e912017-06-23 15:34:16 +00001006 const Arg *A = Args.getLastArg(options::OPT_gz, options::OPT_gz_EQ);
1007 if (!A)
1008 return;
Alexey Bataevb83b4e42018-07-27 19:45:14 +00001009 if (checkDebugInfoOption(A, Args, D, TC)) {
1010 if (A->getOption().getID() == options::OPT_gz) {
1011 if (llvm::zlib::isAvailable())
Fangrui Songbaabc872019-05-11 01:14:50 +00001012 CmdArgs.push_back("--compress-debug-sections");
Alexey Bataevb83b4e42018-07-27 19:45:14 +00001013 else
1014 D.Diag(diag::warn_debug_compression_unavailable);
1015 return;
Saleem Abdulrasoold064e912017-06-23 15:34:16 +00001016 }
Alexey Bataevb83b4e42018-07-27 19:45:14 +00001017
1018 StringRef Value = A->getValue();
1019 if (Value == "none") {
Fangrui Songbaabc872019-05-11 01:14:50 +00001020 CmdArgs.push_back("--compress-debug-sections=none");
Alexey Bataevb83b4e42018-07-27 19:45:14 +00001021 } else if (Value == "zlib" || Value == "zlib-gnu") {
1022 if (llvm::zlib::isAvailable()) {
1023 CmdArgs.push_back(
Fangrui Songbaabc872019-05-11 01:14:50 +00001024 Args.MakeArgString("--compress-debug-sections=" + Twine(Value)));
Alexey Bataevb83b4e42018-07-27 19:45:14 +00001025 } else {
1026 D.Diag(diag::warn_debug_compression_unavailable);
1027 }
1028 } else {
1029 D.Diag(diag::err_drv_unsupported_option_argument)
1030 << A->getOption().getName() << Value;
1031 }
Saleem Abdulrasoold064e912017-06-23 15:34:16 +00001032 }
1033}
1034
David L. Jonesf561aba2017-03-08 01:02:16 +00001035static const char *RelocationModelName(llvm::Reloc::Model Model) {
1036 switch (Model) {
1037 case llvm::Reloc::Static:
1038 return "static";
1039 case llvm::Reloc::PIC_:
1040 return "pic";
1041 case llvm::Reloc::DynamicNoPIC:
1042 return "dynamic-no-pic";
1043 case llvm::Reloc::ROPI:
1044 return "ropi";
1045 case llvm::Reloc::RWPI:
1046 return "rwpi";
1047 case llvm::Reloc::ROPI_RWPI:
1048 return "ropi-rwpi";
1049 }
1050 llvm_unreachable("Unknown Reloc::Model kind");
1051}
1052
1053void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
1054 const Driver &D, const ArgList &Args,
1055 ArgStringList &CmdArgs,
1056 const InputInfo &Output,
1057 const InputInfoList &Inputs) const {
1058 Arg *A;
1059 const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU();
1060
1061 CheckPreprocessingOptions(D, Args);
1062
1063 Args.AddLastArg(CmdArgs, options::OPT_C);
1064 Args.AddLastArg(CmdArgs, options::OPT_CC);
1065
1066 // Handle dependency file generation.
1067 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
1068 (A = Args.getLastArg(options::OPT_MD)) ||
1069 (A = Args.getLastArg(options::OPT_MMD))) {
1070 // Determine the output location.
1071 const char *DepFile;
1072 if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
1073 DepFile = MF->getValue();
1074 C.addFailureResultFile(DepFile, &JA);
1075 } else if (Output.getType() == types::TY_Dependencies) {
1076 DepFile = Output.getFilename();
1077 } else if (A->getOption().matches(options::OPT_M) ||
1078 A->getOption().matches(options::OPT_MM)) {
1079 DepFile = "-";
1080 } else {
1081 DepFile = getDependencyFileName(Args, Inputs);
1082 C.addFailureResultFile(DepFile, &JA);
1083 }
1084 CmdArgs.push_back("-dependency-file");
1085 CmdArgs.push_back(DepFile);
1086
1087 // Add a default target if one wasn't specified.
1088 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
1089 const char *DepTarget;
1090
1091 // If user provided -o, that is the dependency target, except
1092 // when we are only generating a dependency file.
1093 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
1094 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
1095 DepTarget = OutputOpt->getValue();
1096 } else {
1097 // Otherwise derive from the base input.
1098 //
1099 // FIXME: This should use the computed output file location.
1100 SmallString<128> P(Inputs[0].getBaseInput());
1101 llvm::sys::path::replace_extension(P, "o");
1102 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
1103 }
1104
Yuka Takahashicdb53482017-06-16 16:01:13 +00001105 if (!A->getOption().matches(options::OPT_MD) && !A->getOption().matches(options::OPT_MMD)) {
1106 CmdArgs.push_back("-w");
1107 }
David L. Jonesf561aba2017-03-08 01:02:16 +00001108 CmdArgs.push_back("-MT");
1109 SmallString<128> Quoted;
1110 QuoteTarget(DepTarget, Quoted);
1111 CmdArgs.push_back(Args.MakeArgString(Quoted));
1112 }
1113
1114 if (A->getOption().matches(options::OPT_M) ||
1115 A->getOption().matches(options::OPT_MD))
1116 CmdArgs.push_back("-sys-header-deps");
1117 if ((isa<PrecompileJobAction>(JA) &&
1118 !Args.hasArg(options::OPT_fno_module_file_deps)) ||
1119 Args.hasArg(options::OPT_fmodule_file_deps))
1120 CmdArgs.push_back("-module-file-deps");
1121 }
1122
1123 if (Args.hasArg(options::OPT_MG)) {
1124 if (!A || A->getOption().matches(options::OPT_MD) ||
1125 A->getOption().matches(options::OPT_MMD))
1126 D.Diag(diag::err_drv_mg_requires_m_or_mm);
1127 CmdArgs.push_back("-MG");
1128 }
1129
1130 Args.AddLastArg(CmdArgs, options::OPT_MP);
1131 Args.AddLastArg(CmdArgs, options::OPT_MV);
1132
1133 // Convert all -MQ <target> args to -MT <quoted target>
1134 for (const Arg *A : Args.filtered(options::OPT_MT, options::OPT_MQ)) {
1135 A->claim();
1136
1137 if (A->getOption().matches(options::OPT_MQ)) {
1138 CmdArgs.push_back("-MT");
1139 SmallString<128> Quoted;
1140 QuoteTarget(A->getValue(), Quoted);
1141 CmdArgs.push_back(Args.MakeArgString(Quoted));
1142
1143 // -MT flag - no change
1144 } else {
1145 A->render(Args, CmdArgs);
1146 }
1147 }
1148
1149 // Add offload include arguments specific for CUDA. This must happen before
1150 // we -I or -include anything else, because we must pick up the CUDA headers
1151 // from the particular CUDA installation, rather than from e.g.
1152 // /usr/local/include.
1153 if (JA.isOffloading(Action::OFK_Cuda))
1154 getToolChain().AddCudaIncludeArgs(Args, CmdArgs);
1155
Gheorghe-Teodor Berceae62c6932019-05-08 15:52:33 +00001156 // If we are offloading to a target via OpenMP we need to include the
1157 // openmp_wrappers folder which contains alternative system headers.
1158 if (JA.isDeviceOffloading(Action::OFK_OpenMP) &&
1159 getToolChain().getTriple().isNVPTX()){
1160 if (!Args.hasArg(options::OPT_nobuiltininc)) {
1161 // Add openmp_wrappers/* to our system include path. This lets us wrap
1162 // standard library headers.
1163 SmallString<128> P(D.ResourceDir);
1164 llvm::sys::path::append(P, "include");
1165 llvm::sys::path::append(P, "openmp_wrappers");
1166 CmdArgs.push_back("-internal-isystem");
1167 CmdArgs.push_back(Args.MakeArgString(P));
1168 }
1169
1170 CmdArgs.push_back("-include");
Gheorghe-Teodor Bercea94695712019-05-13 22:11:44 +00001171 CmdArgs.push_back("__clang_openmp_math_declares.h");
Gheorghe-Teodor Berceae62c6932019-05-08 15:52:33 +00001172 }
1173
David L. Jonesf561aba2017-03-08 01:02:16 +00001174 // Add -i* options, and automatically translate to
1175 // -include-pch/-include-pth for transparent PCH support. It's
1176 // wonky, but we include looking for .gch so we can support seamless
1177 // replacement into a build system already set up to be generating
1178 // .gch files.
Erich Keane76675de2018-07-05 17:22:13 +00001179
1180 if (getToolChain().getDriver().IsCLMode()) {
David L. Jonesf561aba2017-03-08 01:02:16 +00001181 const Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc);
1182 const Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu);
Erich Keane76675de2018-07-05 17:22:13 +00001183 if (YcArg && JA.getKind() >= Action::PrecompileJobClass &&
1184 JA.getKind() <= Action::AssembleJobClass) {
1185 CmdArgs.push_back(Args.MakeArgString("-building-pch-with-obj"));
David L. Jonesf561aba2017-03-08 01:02:16 +00001186 }
Erich Keane76675de2018-07-05 17:22:13 +00001187 if (YcArg || YuArg) {
1188 StringRef ThroughHeader = YcArg ? YcArg->getValue() : YuArg->getValue();
1189 if (!isa<PrecompileJobAction>(JA)) {
1190 CmdArgs.push_back("-include-pch");
Mike Rice58df1af2018-09-11 17:10:44 +00001191 CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath(
1192 C, !ThroughHeader.empty()
1193 ? ThroughHeader
1194 : llvm::sys::path::filename(Inputs[0].getBaseInput()))));
Erich Keane76675de2018-07-05 17:22:13 +00001195 }
Mike Rice58df1af2018-09-11 17:10:44 +00001196
1197 if (ThroughHeader.empty()) {
1198 CmdArgs.push_back(Args.MakeArgString(
1199 Twine("-pch-through-hdrstop-") + (YcArg ? "create" : "use")));
1200 } else {
1201 CmdArgs.push_back(
1202 Args.MakeArgString(Twine("-pch-through-header=") + ThroughHeader));
1203 }
Erich Keane76675de2018-07-05 17:22:13 +00001204 }
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001205 }
David L. Jonesf561aba2017-03-08 01:02:16 +00001206
1207 bool RenderedImplicitInclude = false;
David L. Jonesf561aba2017-03-08 01:02:16 +00001208 for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) {
Erich Keane76675de2018-07-05 17:22:13 +00001209 if (A->getOption().matches(options::OPT_include)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00001210 // Handling of gcc-style gch precompiled headers.
1211 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
1212 RenderedImplicitInclude = true;
1213
David L. Jonesf561aba2017-03-08 01:02:16 +00001214 bool FoundPCH = false;
1215 SmallString<128> P(A->getValue());
1216 // We want the files to have a name like foo.h.pch. Add a dummy extension
1217 // so that replace_extension does the right thing.
1218 P += ".dummy";
Erich Keane0a6b5b62018-12-04 14:34:09 +00001219 llvm::sys::path::replace_extension(P, "pch");
1220 if (llvm::sys::fs::exists(P))
1221 FoundPCH = true;
David L. Jonesf561aba2017-03-08 01:02:16 +00001222
1223 if (!FoundPCH) {
David L. Jonesf561aba2017-03-08 01:02:16 +00001224 llvm::sys::path::replace_extension(P, "gch");
1225 if (llvm::sys::fs::exists(P)) {
Erich Keane0a6b5b62018-12-04 14:34:09 +00001226 FoundPCH = true;
David L. Jonesf561aba2017-03-08 01:02:16 +00001227 }
1228 }
1229
Erich Keane0a6b5b62018-12-04 14:34:09 +00001230 if (FoundPCH) {
David L. Jonesf561aba2017-03-08 01:02:16 +00001231 if (IsFirstImplicitInclude) {
1232 A->claim();
Erich Keane0a6b5b62018-12-04 14:34:09 +00001233 CmdArgs.push_back("-include-pch");
David L. Jonesf561aba2017-03-08 01:02:16 +00001234 CmdArgs.push_back(Args.MakeArgString(P));
1235 continue;
1236 } else {
1237 // Ignore the PCH if not first on command line and emit warning.
1238 D.Diag(diag::warn_drv_pch_not_first_include) << P
1239 << A->getAsString(Args);
1240 }
1241 }
1242 } else if (A->getOption().matches(options::OPT_isystem_after)) {
1243 // Handling of paths which must come late. These entries are handled by
1244 // the toolchain itself after the resource dir is inserted in the right
1245 // search order.
1246 // Do not claim the argument so that the use of the argument does not
1247 // silently go unnoticed on toolchains which do not honour the option.
1248 continue;
Shoaib Meenaib50e8c52019-08-06 06:48:43 +00001249 } else if (A->getOption().matches(options::OPT_stdlibxx_isystem)) {
1250 // Translated to -internal-isystem by the driver, no need to pass to cc1.
1251 continue;
David L. Jonesf561aba2017-03-08 01:02:16 +00001252 }
1253
1254 // Not translated, render as usual.
1255 A->claim();
1256 A->render(Args, CmdArgs);
1257 }
1258
1259 Args.AddAllArgs(CmdArgs,
1260 {options::OPT_D, options::OPT_U, options::OPT_I_Group,
1261 options::OPT_F, options::OPT_index_header_map});
1262
1263 // Add -Wp, and -Xpreprocessor if using the preprocessor.
1264
1265 // FIXME: There is a very unfortunate problem here, some troubled
1266 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
1267 // really support that we would have to parse and then translate
1268 // those options. :(
1269 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
1270 options::OPT_Xpreprocessor);
1271
1272 // -I- is a deprecated GCC feature, reject it.
1273 if (Arg *A = Args.getLastArg(options::OPT_I_))
1274 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
1275
1276 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
1277 // -isysroot to the CC1 invocation.
1278 StringRef sysroot = C.getSysRoot();
1279 if (sysroot != "") {
1280 if (!Args.hasArg(options::OPT_isysroot)) {
1281 CmdArgs.push_back("-isysroot");
1282 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
1283 }
1284 }
1285
1286 // Parse additional include paths from environment variables.
1287 // FIXME: We should probably sink the logic for handling these from the
1288 // frontend into the driver. It will allow deleting 4 otherwise unused flags.
1289 // CPATH - included following the user specified includes (but prior to
1290 // builtin and standard includes).
1291 addDirectoryList(Args, CmdArgs, "-I", "CPATH");
1292 // C_INCLUDE_PATH - system includes enabled when compiling C.
1293 addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
1294 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
1295 addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
1296 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
1297 addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
1298 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
1299 addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
1300
1301 // While adding the include arguments, we also attempt to retrieve the
1302 // arguments of related offloading toolchains or arguments that are specific
1303 // of an offloading programming model.
1304
1305 // Add C++ include arguments, if needed.
Shoaib Meenaib50e8c52019-08-06 06:48:43 +00001306 if (types::isCXX(Inputs[0].getType())) {
1307 bool HasStdlibxxIsystem = Args.hasArg(options::OPT_stdlibxx_isystem);
1308 forAllAssociatedToolChains(
1309 C, JA, getToolChain(),
1310 [&Args, &CmdArgs, HasStdlibxxIsystem](const ToolChain &TC) {
1311 HasStdlibxxIsystem ? TC.AddClangCXXStdlibIsystemArgs(Args, CmdArgs)
1312 : TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
1313 });
1314 }
David L. Jonesf561aba2017-03-08 01:02:16 +00001315
1316 // Add system include arguments for all targets but IAMCU.
1317 if (!IsIAMCU)
1318 forAllAssociatedToolChains(C, JA, getToolChain(),
1319 [&Args, &CmdArgs](const ToolChain &TC) {
1320 TC.AddClangSystemIncludeArgs(Args, CmdArgs);
1321 });
1322 else {
1323 // For IAMCU add special include arguments.
1324 getToolChain().AddIAMCUIncludeArgs(Args, CmdArgs);
1325 }
1326}
1327
1328// FIXME: Move to target hook.
1329static bool isSignedCharDefault(const llvm::Triple &Triple) {
1330 switch (Triple.getArch()) {
1331 default:
1332 return true;
1333
1334 case llvm::Triple::aarch64:
1335 case llvm::Triple::aarch64_be:
1336 case llvm::Triple::arm:
1337 case llvm::Triple::armeb:
1338 case llvm::Triple::thumb:
1339 case llvm::Triple::thumbeb:
1340 if (Triple.isOSDarwin() || Triple.isOSWindows())
1341 return true;
1342 return false;
1343
1344 case llvm::Triple::ppc:
1345 case llvm::Triple::ppc64:
1346 if (Triple.isOSDarwin())
1347 return true;
1348 return false;
1349
1350 case llvm::Triple::hexagon:
1351 case llvm::Triple::ppc64le:
Alex Bradbury71f45452018-01-11 13:36:56 +00001352 case llvm::Triple::riscv32:
1353 case llvm::Triple::riscv64:
David L. Jonesf561aba2017-03-08 01:02:16 +00001354 case llvm::Triple::systemz:
1355 case llvm::Triple::xcore:
1356 return false;
1357 }
1358}
1359
1360static bool isNoCommonDefault(const llvm::Triple &Triple) {
1361 switch (Triple.getArch()) {
1362 default:
Petr Hosekbf45ece2018-02-23 20:10:14 +00001363 if (Triple.isOSFuchsia())
1364 return true;
David L. Jonesf561aba2017-03-08 01:02:16 +00001365 return false;
1366
1367 case llvm::Triple::xcore:
1368 case llvm::Triple::wasm32:
1369 case llvm::Triple::wasm64:
1370 return true;
1371 }
1372}
1373
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00001374namespace {
1375void RenderARMABI(const llvm::Triple &Triple, const ArgList &Args,
1376 ArgStringList &CmdArgs) {
David L. Jonesf561aba2017-03-08 01:02:16 +00001377 // Select the ABI to use.
1378 // FIXME: Support -meabi.
1379 // FIXME: Parts of this are duplicated in the backend, unify this somehow.
1380 const char *ABIName = nullptr;
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00001381 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00001382 ABIName = A->getValue();
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00001383 } else {
Daniel Jasperd27538a2017-06-30 08:02:37 +00001384 std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false);
Eric Christopher53b2cb72017-06-30 00:03:56 +00001385 ABIName = llvm::ARM::computeDefaultTargetABI(Triple, CPU).data();
David L. Jonesf561aba2017-03-08 01:02:16 +00001386 }
Eric Christopher53b2cb72017-06-30 00:03:56 +00001387
David L. Jonesf561aba2017-03-08 01:02:16 +00001388 CmdArgs.push_back("-target-abi");
1389 CmdArgs.push_back(ABIName);
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00001390}
1391}
1392
1393void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args,
1394 ArgStringList &CmdArgs, bool KernelOrKext) const {
1395 RenderARMABI(Triple, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00001396
1397 // Determine floating point ABI from the options & target defaults.
1398 arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args);
1399 if (ABI == arm::FloatABI::Soft) {
1400 // Floating point operations and argument passing are soft.
1401 // FIXME: This changes CPP defines, we need -target-soft-float.
1402 CmdArgs.push_back("-msoft-float");
1403 CmdArgs.push_back("-mfloat-abi");
1404 CmdArgs.push_back("soft");
1405 } else if (ABI == arm::FloatABI::SoftFP) {
1406 // Floating point operations are hard, but argument passing is soft.
1407 CmdArgs.push_back("-mfloat-abi");
1408 CmdArgs.push_back("soft");
1409 } else {
1410 // Floating point operations and argument passing are hard.
1411 assert(ABI == arm::FloatABI::Hard && "Invalid float abi!");
1412 CmdArgs.push_back("-mfloat-abi");
1413 CmdArgs.push_back("hard");
1414 }
1415
1416 // Forward the -mglobal-merge option for explicit control over the pass.
1417 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
1418 options::OPT_mno_global_merge)) {
Eli Friedman01d349b2018-04-12 22:21:36 +00001419 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00001420 if (A->getOption().matches(options::OPT_mno_global_merge))
1421 CmdArgs.push_back("-arm-global-merge=false");
1422 else
1423 CmdArgs.push_back("-arm-global-merge=true");
1424 }
1425
1426 if (!Args.hasFlag(options::OPT_mimplicit_float,
1427 options::OPT_mno_implicit_float, true))
1428 CmdArgs.push_back("-no-implicit-float");
Javed Absar603a2ba2019-05-21 14:21:26 +00001429
1430 if (Args.getLastArg(options::OPT_mcmse))
1431 CmdArgs.push_back("-mcmse");
David L. Jonesf561aba2017-03-08 01:02:16 +00001432}
1433
Saleem Abdulrasool6c3ed7b2017-09-03 04:47:00 +00001434void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple,
1435 const ArgList &Args, bool KernelOrKext,
1436 ArgStringList &CmdArgs) const {
1437 const ToolChain &TC = getToolChain();
1438
1439 // Add the target features
1440 getTargetFeatures(TC, EffectiveTriple, Args, CmdArgs, false);
1441
1442 // Add target specific flags.
1443 switch (TC.getArch()) {
1444 default:
1445 break;
1446
1447 case llvm::Triple::arm:
1448 case llvm::Triple::armeb:
1449 case llvm::Triple::thumb:
1450 case llvm::Triple::thumbeb:
1451 // Use the effective triple, which takes into account the deployment target.
1452 AddARMTargetArgs(EffectiveTriple, Args, CmdArgs, KernelOrKext);
1453 CmdArgs.push_back("-fallow-half-arguments-and-returns");
1454 break;
1455
1456 case llvm::Triple::aarch64:
1457 case llvm::Triple::aarch64_be:
1458 AddAArch64TargetArgs(Args, CmdArgs);
1459 CmdArgs.push_back("-fallow-half-arguments-and-returns");
1460 break;
1461
1462 case llvm::Triple::mips:
1463 case llvm::Triple::mipsel:
1464 case llvm::Triple::mips64:
1465 case llvm::Triple::mips64el:
1466 AddMIPSTargetArgs(Args, CmdArgs);
1467 break;
1468
1469 case llvm::Triple::ppc:
1470 case llvm::Triple::ppc64:
1471 case llvm::Triple::ppc64le:
1472 AddPPCTargetArgs(Args, CmdArgs);
1473 break;
1474
Alex Bradbury71f45452018-01-11 13:36:56 +00001475 case llvm::Triple::riscv32:
1476 case llvm::Triple::riscv64:
1477 AddRISCVTargetArgs(Args, CmdArgs);
1478 break;
1479
Saleem Abdulrasool6c3ed7b2017-09-03 04:47:00 +00001480 case llvm::Triple::sparc:
1481 case llvm::Triple::sparcel:
1482 case llvm::Triple::sparcv9:
1483 AddSparcTargetArgs(Args, CmdArgs);
1484 break;
1485
1486 case llvm::Triple::systemz:
1487 AddSystemZTargetArgs(Args, CmdArgs);
1488 break;
1489
1490 case llvm::Triple::x86:
1491 case llvm::Triple::x86_64:
1492 AddX86TargetArgs(Args, CmdArgs);
1493 break;
1494
1495 case llvm::Triple::lanai:
1496 AddLanaiTargetArgs(Args, CmdArgs);
1497 break;
1498
1499 case llvm::Triple::hexagon:
1500 AddHexagonTargetArgs(Args, CmdArgs);
1501 break;
1502
1503 case llvm::Triple::wasm32:
1504 case llvm::Triple::wasm64:
1505 AddWebAssemblyTargetArgs(Args, CmdArgs);
1506 break;
1507 }
1508}
1509
Luke Cheesemana8a24aa2018-10-25 15:23:49 +00001510// Parse -mbranch-protection=<protection>[+<protection>]* where
1511// <protection> ::= standard | none | [bti,pac-ret[+b-key,+leaf]*]
1512// Returns a triple of (return address signing Scope, signing key, require
1513// landing pads)
1514static std::tuple<StringRef, StringRef, bool>
1515ParseAArch64BranchProtection(const Driver &D, const ArgList &Args,
1516 const Arg *A) {
1517 StringRef Scope = "none";
1518 StringRef Key = "a_key";
1519 bool IndirectBranches = false;
1520
1521 StringRef Value = A->getValue();
1522 // This maps onto -mbranch-protection=<scope>+<key>
1523
1524 if (Value.equals("standard")) {
1525 Scope = "non-leaf";
1526 Key = "a_key";
1527 IndirectBranches = true;
1528
1529 } else if (!Value.equals("none")) {
1530 SmallVector<StringRef, 4> BranchProtection;
1531 StringRef(A->getValue()).split(BranchProtection, '+');
1532
1533 auto Protection = BranchProtection.begin();
1534 while (Protection != BranchProtection.end()) {
1535 if (Protection->equals("bti"))
1536 IndirectBranches = true;
1537 else if (Protection->equals("pac-ret")) {
1538 Scope = "non-leaf";
1539 while (++Protection != BranchProtection.end()) {
1540 // Inner loop as "leaf" and "b-key" options must only appear attached
1541 // to pac-ret.
1542 if (Protection->equals("leaf"))
1543 Scope = "all";
1544 else if (Protection->equals("b-key"))
1545 Key = "b_key";
1546 else
1547 break;
1548 }
1549 Protection--;
1550 } else
1551 D.Diag(diag::err_invalid_branch_protection)
1552 << *Protection << A->getAsString(Args);
1553 Protection++;
1554 }
1555 }
1556
1557 return std::make_tuple(Scope, Key, IndirectBranches);
1558}
1559
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00001560namespace {
1561void RenderAArch64ABI(const llvm::Triple &Triple, const ArgList &Args,
1562 ArgStringList &CmdArgs) {
1563 const char *ABIName = nullptr;
1564 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
1565 ABIName = A->getValue();
1566 else if (Triple.isOSDarwin())
1567 ABIName = "darwinpcs";
1568 else
1569 ABIName = "aapcs";
1570
1571 CmdArgs.push_back("-target-abi");
1572 CmdArgs.push_back(ABIName);
1573}
1574}
1575
David L. Jonesf561aba2017-03-08 01:02:16 +00001576void Clang::AddAArch64TargetArgs(const ArgList &Args,
1577 ArgStringList &CmdArgs) const {
1578 const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
1579
1580 if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
1581 Args.hasArg(options::OPT_mkernel) ||
1582 Args.hasArg(options::OPT_fapple_kext))
1583 CmdArgs.push_back("-disable-red-zone");
1584
1585 if (!Args.hasFlag(options::OPT_mimplicit_float,
1586 options::OPT_mno_implicit_float, true))
1587 CmdArgs.push_back("-no-implicit-float");
1588
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00001589 RenderAArch64ABI(Triple, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00001590
1591 if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a53_835769,
1592 options::OPT_mno_fix_cortex_a53_835769)) {
Eli Friedman01d349b2018-04-12 22:21:36 +00001593 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00001594 if (A->getOption().matches(options::OPT_mfix_cortex_a53_835769))
1595 CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
1596 else
1597 CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=0");
1598 } else if (Triple.isAndroid()) {
1599 // Enabled A53 errata (835769) workaround by default on android
Eli Friedman01d349b2018-04-12 22:21:36 +00001600 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00001601 CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
1602 }
1603
1604 // Forward the -mglobal-merge option for explicit control over the pass.
1605 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
1606 options::OPT_mno_global_merge)) {
Eli Friedman01d349b2018-04-12 22:21:36 +00001607 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00001608 if (A->getOption().matches(options::OPT_mno_global_merge))
1609 CmdArgs.push_back("-aarch64-enable-global-merge=false");
1610 else
1611 CmdArgs.push_back("-aarch64-enable-global-merge=true");
1612 }
Luke Cheeseman0ac44c12018-08-17 12:55:05 +00001613
Luke Cheesemana8a24aa2018-10-25 15:23:49 +00001614 // Enable/disable return address signing and indirect branch targets.
1615 if (Arg *A = Args.getLastArg(options::OPT_msign_return_address_EQ,
1616 options::OPT_mbranch_protection_EQ)) {
1617
1618 const Driver &D = getToolChain().getDriver();
1619
1620 StringRef Scope, Key;
1621 bool IndirectBranches;
1622
1623 if (A->getOption().matches(options::OPT_msign_return_address_EQ)) {
1624 Scope = A->getValue();
1625 if (!Scope.equals("none") && !Scope.equals("non-leaf") &&
1626 !Scope.equals("all"))
1627 D.Diag(diag::err_invalid_branch_protection)
1628 << Scope << A->getAsString(Args);
1629 Key = "a_key";
1630 IndirectBranches = false;
1631 } else
1632 std::tie(Scope, Key, IndirectBranches) =
1633 ParseAArch64BranchProtection(D, Args, A);
1634
Luke Cheeseman0ac44c12018-08-17 12:55:05 +00001635 CmdArgs.push_back(
Luke Cheesemana8a24aa2018-10-25 15:23:49 +00001636 Args.MakeArgString(Twine("-msign-return-address=") + Scope));
1637 CmdArgs.push_back(
1638 Args.MakeArgString(Twine("-msign-return-address-key=") + Key));
1639 if (IndirectBranches)
1640 CmdArgs.push_back("-mbranch-target-enforce");
Luke Cheeseman0ac44c12018-08-17 12:55:05 +00001641 }
David L. Jonesf561aba2017-03-08 01:02:16 +00001642}
1643
1644void Clang::AddMIPSTargetArgs(const ArgList &Args,
1645 ArgStringList &CmdArgs) const {
1646 const Driver &D = getToolChain().getDriver();
1647 StringRef CPUName;
1648 StringRef ABIName;
1649 const llvm::Triple &Triple = getToolChain().getTriple();
1650 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
1651
1652 CmdArgs.push_back("-target-abi");
1653 CmdArgs.push_back(ABIName.data());
1654
1655 mips::FloatABI ABI = mips::getMipsFloatABI(D, Args);
1656 if (ABI == mips::FloatABI::Soft) {
1657 // Floating point operations and argument passing are soft.
1658 CmdArgs.push_back("-msoft-float");
1659 CmdArgs.push_back("-mfloat-abi");
1660 CmdArgs.push_back("soft");
1661 } else {
1662 // Floating point operations and argument passing are hard.
1663 assert(ABI == mips::FloatABI::Hard && "Invalid float abi!");
1664 CmdArgs.push_back("-mfloat-abi");
1665 CmdArgs.push_back("hard");
1666 }
1667
1668 if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) {
1669 if (A->getOption().matches(options::OPT_mxgot)) {
1670 CmdArgs.push_back("-mllvm");
1671 CmdArgs.push_back("-mxgot");
1672 }
1673 }
1674
1675 if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1,
1676 options::OPT_mno_ldc1_sdc1)) {
1677 if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) {
1678 CmdArgs.push_back("-mllvm");
1679 CmdArgs.push_back("-mno-ldc1-sdc1");
1680 }
1681 }
1682
1683 if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division,
1684 options::OPT_mno_check_zero_division)) {
1685 if (A->getOption().matches(options::OPT_mno_check_zero_division)) {
1686 CmdArgs.push_back("-mllvm");
1687 CmdArgs.push_back("-mno-check-zero-division");
1688 }
1689 }
1690
1691 if (Arg *A = Args.getLastArg(options::OPT_G)) {
1692 StringRef v = A->getValue();
1693 CmdArgs.push_back("-mllvm");
1694 CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
1695 A->claim();
1696 }
1697
Simon Dardis31636a12017-07-20 14:04:12 +00001698 Arg *GPOpt = Args.getLastArg(options::OPT_mgpopt, options::OPT_mno_gpopt);
1699 Arg *ABICalls =
1700 Args.getLastArg(options::OPT_mabicalls, options::OPT_mno_abicalls);
1701
1702 // -mabicalls is the default for many MIPS environments, even with -fno-pic.
1703 // -mgpopt is the default for static, -fno-pic environments but these two
1704 // options conflict. We want to be certain that -mno-abicalls -mgpopt is
1705 // the only case where -mllvm -mgpopt is passed.
1706 // NOTE: We need a warning here or in the backend to warn when -mgpopt is
1707 // passed explicitly when compiling something with -mabicalls
1708 // (implictly) in affect. Currently the warning is in the backend.
Simon Dardisad9d05d2017-08-11 15:01:34 +00001709 //
1710 // When the ABI in use is N64, we also need to determine the PIC mode that
1711 // is in use, as -fno-pic for N64 implies -mno-abicalls.
Simon Dardis31636a12017-07-20 14:04:12 +00001712 bool NoABICalls =
1713 ABICalls && ABICalls->getOption().matches(options::OPT_mno_abicalls);
Simon Dardisad9d05d2017-08-11 15:01:34 +00001714
1715 llvm::Reloc::Model RelocationModel;
1716 unsigned PICLevel;
1717 bool IsPIE;
1718 std::tie(RelocationModel, PICLevel, IsPIE) =
1719 ParsePICArgs(getToolChain(), Args);
1720
1721 NoABICalls = NoABICalls ||
1722 (RelocationModel == llvm::Reloc::Static && ABIName == "n64");
1723
Simon Dardis31636a12017-07-20 14:04:12 +00001724 bool WantGPOpt = GPOpt && GPOpt->getOption().matches(options::OPT_mgpopt);
1725 // We quietly ignore -mno-gpopt as the backend defaults to -mno-gpopt.
1726 if (NoABICalls && (!GPOpt || WantGPOpt)) {
1727 CmdArgs.push_back("-mllvm");
1728 CmdArgs.push_back("-mgpopt");
Simon Dardis9f1d5d82017-07-20 22:23:21 +00001729
1730 Arg *LocalSData = Args.getLastArg(options::OPT_mlocal_sdata,
1731 options::OPT_mno_local_sdata);
Simon Dardis7d318782017-07-24 14:02:09 +00001732 Arg *ExternSData = Args.getLastArg(options::OPT_mextern_sdata,
Simon Dardiseeed0002017-08-03 13:04:29 +00001733 options::OPT_mno_extern_sdata);
1734 Arg *EmbeddedData = Args.getLastArg(options::OPT_membedded_data,
1735 options::OPT_mno_embedded_data);
Simon Dardis9f1d5d82017-07-20 22:23:21 +00001736 if (LocalSData) {
1737 CmdArgs.push_back("-mllvm");
1738 if (LocalSData->getOption().matches(options::OPT_mlocal_sdata)) {
1739 CmdArgs.push_back("-mlocal-sdata=1");
1740 } else {
1741 CmdArgs.push_back("-mlocal-sdata=0");
1742 }
1743 LocalSData->claim();
1744 }
1745
Simon Dardis7d318782017-07-24 14:02:09 +00001746 if (ExternSData) {
1747 CmdArgs.push_back("-mllvm");
1748 if (ExternSData->getOption().matches(options::OPT_mextern_sdata)) {
1749 CmdArgs.push_back("-mextern-sdata=1");
1750 } else {
1751 CmdArgs.push_back("-mextern-sdata=0");
1752 }
1753 ExternSData->claim();
1754 }
Simon Dardiseeed0002017-08-03 13:04:29 +00001755
1756 if (EmbeddedData) {
1757 CmdArgs.push_back("-mllvm");
1758 if (EmbeddedData->getOption().matches(options::OPT_membedded_data)) {
1759 CmdArgs.push_back("-membedded-data=1");
1760 } else {
1761 CmdArgs.push_back("-membedded-data=0");
1762 }
1763 EmbeddedData->claim();
1764 }
1765
Simon Dardis31636a12017-07-20 14:04:12 +00001766 } else if ((!ABICalls || (!NoABICalls && ABICalls)) && WantGPOpt)
1767 D.Diag(diag::warn_drv_unsupported_gpopt) << (ABICalls ? 0 : 1);
1768
1769 if (GPOpt)
1770 GPOpt->claim();
1771
David L. Jonesf561aba2017-03-08 01:02:16 +00001772 if (Arg *A = Args.getLastArg(options::OPT_mcompact_branches_EQ)) {
1773 StringRef Val = StringRef(A->getValue());
1774 if (mips::hasCompactBranches(CPUName)) {
1775 if (Val == "never" || Val == "always" || Val == "optimal") {
1776 CmdArgs.push_back("-mllvm");
1777 CmdArgs.push_back(Args.MakeArgString("-mips-compact-branches=" + Val));
1778 } else
1779 D.Diag(diag::err_drv_unsupported_option_argument)
1780 << A->getOption().getName() << Val;
1781 } else
1782 D.Diag(diag::warn_target_unsupported_compact_branches) << CPUName;
1783 }
Vladimir Stefanovic99113a02019-01-18 19:54:51 +00001784
1785 if (Arg *A = Args.getLastArg(options::OPT_mrelax_pic_calls,
1786 options::OPT_mno_relax_pic_calls)) {
1787 if (A->getOption().matches(options::OPT_mno_relax_pic_calls)) {
1788 CmdArgs.push_back("-mllvm");
1789 CmdArgs.push_back("-mips-jalr-reloc=0");
1790 }
1791 }
David L. Jonesf561aba2017-03-08 01:02:16 +00001792}
1793
1794void Clang::AddPPCTargetArgs(const ArgList &Args,
1795 ArgStringList &CmdArgs) const {
1796 // Select the ABI to use.
1797 const char *ABIName = nullptr;
1798 if (getToolChain().getTriple().isOSLinux())
1799 switch (getToolChain().getArch()) {
1800 case llvm::Triple::ppc64: {
1801 // When targeting a processor that supports QPX, or if QPX is
1802 // specifically enabled, default to using the ABI that supports QPX (so
1803 // long as it is not specifically disabled).
1804 bool HasQPX = false;
1805 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
1806 HasQPX = A->getValue() == StringRef("a2q");
1807 HasQPX = Args.hasFlag(options::OPT_mqpx, options::OPT_mno_qpx, HasQPX);
1808 if (HasQPX) {
1809 ABIName = "elfv1-qpx";
1810 break;
1811 }
1812
1813 ABIName = "elfv1";
1814 break;
1815 }
1816 case llvm::Triple::ppc64le:
1817 ABIName = "elfv2";
1818 break;
1819 default:
1820 break;
1821 }
1822
Fangrui Song6bd02a42019-07-15 07:25:11 +00001823 bool IEEELongDouble = false;
1824 for (const Arg *A : Args.filtered(options::OPT_mabi_EQ)) {
1825 StringRef V = A->getValue();
1826 if (V == "ieeelongdouble")
1827 IEEELongDouble = true;
1828 else if (V == "ibmlongdouble")
1829 IEEELongDouble = false;
1830 else if (V != "altivec")
1831 // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore
1832 // the option if given as we don't have backend support for any targets
1833 // that don't use the altivec abi.
David L. Jonesf561aba2017-03-08 01:02:16 +00001834 ABIName = A->getValue();
Fangrui Song6bd02a42019-07-15 07:25:11 +00001835 }
1836 if (IEEELongDouble)
1837 CmdArgs.push_back("-mabi=ieeelongdouble");
David L. Jonesf561aba2017-03-08 01:02:16 +00001838
1839 ppc::FloatABI FloatABI =
1840 ppc::getPPCFloatABI(getToolChain().getDriver(), Args);
1841
1842 if (FloatABI == ppc::FloatABI::Soft) {
1843 // Floating point operations and argument passing are soft.
1844 CmdArgs.push_back("-msoft-float");
1845 CmdArgs.push_back("-mfloat-abi");
1846 CmdArgs.push_back("soft");
1847 } else {
1848 // Floating point operations and argument passing are hard.
1849 assert(FloatABI == ppc::FloatABI::Hard && "Invalid float abi!");
1850 CmdArgs.push_back("-mfloat-abi");
1851 CmdArgs.push_back("hard");
1852 }
1853
1854 if (ABIName) {
1855 CmdArgs.push_back("-target-abi");
1856 CmdArgs.push_back(ABIName);
1857 }
1858}
1859
Alex Bradbury71f45452018-01-11 13:36:56 +00001860void Clang::AddRISCVTargetArgs(const ArgList &Args,
1861 ArgStringList &CmdArgs) const {
Alex Bradbury71f45452018-01-11 13:36:56 +00001862 const llvm::Triple &Triple = getToolChain().getTriple();
Roger Ferrer Ibanez371bdc92019-08-07 07:08:00 +00001863 StringRef ABIName = riscv::getRISCVABI(Args, Triple);
Alex Bradbury71f45452018-01-11 13:36:56 +00001864
1865 CmdArgs.push_back("-target-abi");
Roger Ferrer Ibanez371bdc92019-08-07 07:08:00 +00001866 CmdArgs.push_back(ABIName.data());
Alex Bradbury71f45452018-01-11 13:36:56 +00001867}
1868
David L. Jonesf561aba2017-03-08 01:02:16 +00001869void Clang::AddSparcTargetArgs(const ArgList &Args,
1870 ArgStringList &CmdArgs) const {
1871 sparc::FloatABI FloatABI =
1872 sparc::getSparcFloatABI(getToolChain().getDriver(), Args);
1873
1874 if (FloatABI == sparc::FloatABI::Soft) {
1875 // Floating point operations and argument passing are soft.
1876 CmdArgs.push_back("-msoft-float");
1877 CmdArgs.push_back("-mfloat-abi");
1878 CmdArgs.push_back("soft");
1879 } else {
1880 // Floating point operations and argument passing are hard.
1881 assert(FloatABI == sparc::FloatABI::Hard && "Invalid float abi!");
1882 CmdArgs.push_back("-mfloat-abi");
1883 CmdArgs.push_back("hard");
1884 }
1885}
1886
1887void Clang::AddSystemZTargetArgs(const ArgList &Args,
1888 ArgStringList &CmdArgs) const {
1889 if (Args.hasFlag(options::OPT_mbackchain, options::OPT_mno_backchain, false))
1890 CmdArgs.push_back("-mbackchain");
1891}
1892
1893void Clang::AddX86TargetArgs(const ArgList &Args,
1894 ArgStringList &CmdArgs) const {
1895 if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
1896 Args.hasArg(options::OPT_mkernel) ||
1897 Args.hasArg(options::OPT_fapple_kext))
1898 CmdArgs.push_back("-disable-red-zone");
1899
Kristina Brooks7f569b72018-10-18 14:07:02 +00001900 if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs,
1901 options::OPT_mno_tls_direct_seg_refs, true))
1902 CmdArgs.push_back("-mno-tls-direct-seg-refs");
1903
David L. Jonesf561aba2017-03-08 01:02:16 +00001904 // Default to avoid implicit floating-point for kernel/kext code, but allow
1905 // that to be overridden with -mno-soft-float.
1906 bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
1907 Args.hasArg(options::OPT_fapple_kext));
1908 if (Arg *A = Args.getLastArg(
1909 options::OPT_msoft_float, options::OPT_mno_soft_float,
1910 options::OPT_mimplicit_float, options::OPT_mno_implicit_float)) {
1911 const Option &O = A->getOption();
1912 NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
1913 O.matches(options::OPT_msoft_float));
1914 }
1915 if (NoImplicitFloat)
1916 CmdArgs.push_back("-no-implicit-float");
1917
1918 if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
1919 StringRef Value = A->getValue();
1920 if (Value == "intel" || Value == "att") {
1921 CmdArgs.push_back("-mllvm");
1922 CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
1923 } else {
1924 getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
1925 << A->getOption().getName() << Value;
1926 }
Nico Webere3712cf2018-01-17 13:34:20 +00001927 } else if (getToolChain().getDriver().IsCLMode()) {
1928 CmdArgs.push_back("-mllvm");
1929 CmdArgs.push_back("-x86-asm-syntax=intel");
David L. Jonesf561aba2017-03-08 01:02:16 +00001930 }
1931
1932 // Set flags to support MCU ABI.
1933 if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) {
1934 CmdArgs.push_back("-mfloat-abi");
1935 CmdArgs.push_back("soft");
1936 CmdArgs.push_back("-mstack-alignment=4");
1937 }
1938}
1939
1940void Clang::AddHexagonTargetArgs(const ArgList &Args,
1941 ArgStringList &CmdArgs) const {
1942 CmdArgs.push_back("-mqdsp6-compat");
1943 CmdArgs.push_back("-Wreturn-type");
1944
1945 if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00001946 CmdArgs.push_back("-mllvm");
Benjamin Kramer3a13ed62017-12-28 16:58:54 +00001947 CmdArgs.push_back(Args.MakeArgString("-hexagon-small-data-threshold=" +
1948 Twine(G.getValue())));
David L. Jonesf561aba2017-03-08 01:02:16 +00001949 }
1950
1951 if (!Args.hasArg(options::OPT_fno_short_enums))
1952 CmdArgs.push_back("-fshort-enums");
1953 if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
1954 CmdArgs.push_back("-mllvm");
1955 CmdArgs.push_back("-enable-hexagon-ieee-rnd-near");
1956 }
1957 CmdArgs.push_back("-mllvm");
1958 CmdArgs.push_back("-machine-sink-split=0");
1959}
1960
1961void Clang::AddLanaiTargetArgs(const ArgList &Args,
1962 ArgStringList &CmdArgs) const {
1963 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
1964 StringRef CPUName = A->getValue();
1965
1966 CmdArgs.push_back("-target-cpu");
1967 CmdArgs.push_back(Args.MakeArgString(CPUName));
1968 }
1969 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1970 StringRef Value = A->getValue();
1971 // Only support mregparm=4 to support old usage. Report error for all other
1972 // cases.
1973 int Mregparm;
1974 if (Value.getAsInteger(10, Mregparm)) {
1975 if (Mregparm != 4) {
1976 getToolChain().getDriver().Diag(
1977 diag::err_drv_unsupported_option_argument)
1978 << A->getOption().getName() << Value;
1979 }
1980 }
1981 }
1982}
1983
1984void Clang::AddWebAssemblyTargetArgs(const ArgList &Args,
1985 ArgStringList &CmdArgs) const {
1986 // Default to "hidden" visibility.
1987 if (!Args.hasArg(options::OPT_fvisibility_EQ,
1988 options::OPT_fvisibility_ms_compat)) {
1989 CmdArgs.push_back("-fvisibility");
1990 CmdArgs.push_back("hidden");
1991 }
1992}
1993
1994void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename,
1995 StringRef Target, const InputInfo &Output,
1996 const InputInfo &Input, const ArgList &Args) const {
1997 // If this is a dry run, do not create the compilation database file.
1998 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
1999 return;
2000
2001 using llvm::yaml::escape;
2002 const Driver &D = getToolChain().getDriver();
2003
2004 if (!CompilationDatabase) {
2005 std::error_code EC;
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00002006 auto File = std::make_unique<llvm::raw_fd_ostream>(Filename, EC,
Fangrui Songd9b948b2019-08-05 05:43:48 +00002007 llvm::sys::fs::OF_Text);
David L. Jonesf561aba2017-03-08 01:02:16 +00002008 if (EC) {
2009 D.Diag(clang::diag::err_drv_compilationdatabase) << Filename
2010 << EC.message();
2011 return;
2012 }
2013 CompilationDatabase = std::move(File);
2014 }
2015 auto &CDB = *CompilationDatabase;
2016 SmallString<128> Buf;
2017 if (llvm::sys::fs::current_path(Buf))
2018 Buf = ".";
2019 CDB << "{ \"directory\": \"" << escape(Buf) << "\"";
2020 CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\"";
2021 CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\"";
2022 CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\"";
2023 Buf = "-x";
2024 Buf += types::getTypeName(Input.getType());
2025 CDB << ", \"" << escape(Buf) << "\"";
2026 if (!D.SysRoot.empty() && !Args.hasArg(options::OPT__sysroot_EQ)) {
2027 Buf = "--sysroot=";
2028 Buf += D.SysRoot;
2029 CDB << ", \"" << escape(Buf) << "\"";
2030 }
2031 CDB << ", \"" << escape(Input.getFilename()) << "\"";
2032 for (auto &A: Args) {
2033 auto &O = A->getOption();
2034 // Skip language selection, which is positional.
2035 if (O.getID() == options::OPT_x)
2036 continue;
2037 // Skip writing dependency output and the compilation database itself.
2038 if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group)
2039 continue;
2040 // Skip inputs.
2041 if (O.getKind() == Option::InputClass)
2042 continue;
2043 // All other arguments are quoted and appended.
2044 ArgStringList ASL;
2045 A->render(Args, ASL);
2046 for (auto &it: ASL)
2047 CDB << ", \"" << escape(it) << "\"";
2048 }
2049 Buf = "--target=";
2050 Buf += Target;
2051 CDB << ", \"" << escape(Buf) << "\"]},\n";
2052}
2053
2054static void CollectArgsForIntegratedAssembler(Compilation &C,
2055 const ArgList &Args,
2056 ArgStringList &CmdArgs,
2057 const Driver &D) {
2058 if (UseRelaxAll(C, Args))
2059 CmdArgs.push_back("-mrelax-all");
2060
2061 // Only default to -mincremental-linker-compatible if we think we are
2062 // targeting the MSVC linker.
2063 bool DefaultIncrementalLinkerCompatible =
2064 C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment();
2065 if (Args.hasFlag(options::OPT_mincremental_linker_compatible,
2066 options::OPT_mno_incremental_linker_compatible,
2067 DefaultIncrementalLinkerCompatible))
2068 CmdArgs.push_back("-mincremental-linker-compatible");
2069
2070 switch (C.getDefaultToolChain().getArch()) {
2071 case llvm::Triple::arm:
2072 case llvm::Triple::armeb:
2073 case llvm::Triple::thumb:
2074 case llvm::Triple::thumbeb:
2075 if (Arg *A = Args.getLastArg(options::OPT_mimplicit_it_EQ)) {
2076 StringRef Value = A->getValue();
2077 if (Value == "always" || Value == "never" || Value == "arm" ||
2078 Value == "thumb") {
2079 CmdArgs.push_back("-mllvm");
2080 CmdArgs.push_back(Args.MakeArgString("-arm-implicit-it=" + Value));
2081 } else {
2082 D.Diag(diag::err_drv_unsupported_option_argument)
2083 << A->getOption().getName() << Value;
2084 }
2085 }
2086 break;
2087 default:
2088 break;
2089 }
2090
Nico Weberb28ffd82019-07-27 01:13:00 +00002091 // If you add more args here, also add them to the block below that
2092 // starts with "// If CollectArgsForIntegratedAssembler() isn't called below".
2093
David L. Jonesf561aba2017-03-08 01:02:16 +00002094 // When passing -I arguments to the assembler we sometimes need to
2095 // unconditionally take the next argument. For example, when parsing
2096 // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the
2097 // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo'
2098 // arg after parsing the '-I' arg.
2099 bool TakeNextArg = false;
2100
Petr Hosek5668d832017-11-22 01:38:31 +00002101 bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations();
Dan Albert2715b282019-03-28 18:08:28 +00002102 bool UseNoExecStack = C.getDefaultToolChain().isNoExecStackDefault();
David L. Jonesf561aba2017-03-08 01:02:16 +00002103 const char *MipsTargetFeature = nullptr;
2104 for (const Arg *A :
2105 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
2106 A->claim();
2107
2108 for (StringRef Value : A->getValues()) {
2109 if (TakeNextArg) {
2110 CmdArgs.push_back(Value.data());
2111 TakeNextArg = false;
2112 continue;
2113 }
2114
2115 if (C.getDefaultToolChain().getTriple().isOSBinFormatCOFF() &&
2116 Value == "-mbig-obj")
2117 continue; // LLVM handles bigobj automatically
2118
2119 switch (C.getDefaultToolChain().getArch()) {
2120 default:
2121 break;
Peter Smith3947cb32017-11-20 13:43:55 +00002122 case llvm::Triple::thumb:
2123 case llvm::Triple::thumbeb:
2124 case llvm::Triple::arm:
2125 case llvm::Triple::armeb:
2126 if (Value == "-mthumb")
2127 // -mthumb has already been processed in ComputeLLVMTriple()
2128 // recognize but skip over here.
2129 continue;
Peter Smith931c9fa2017-11-20 13:53:55 +00002130 break;
David L. Jonesf561aba2017-03-08 01:02:16 +00002131 case llvm::Triple::mips:
2132 case llvm::Triple::mipsel:
2133 case llvm::Triple::mips64:
2134 case llvm::Triple::mips64el:
2135 if (Value == "--trap") {
2136 CmdArgs.push_back("-target-feature");
2137 CmdArgs.push_back("+use-tcc-in-div");
2138 continue;
2139 }
2140 if (Value == "--break") {
2141 CmdArgs.push_back("-target-feature");
2142 CmdArgs.push_back("-use-tcc-in-div");
2143 continue;
2144 }
2145 if (Value.startswith("-msoft-float")) {
2146 CmdArgs.push_back("-target-feature");
2147 CmdArgs.push_back("+soft-float");
2148 continue;
2149 }
2150 if (Value.startswith("-mhard-float")) {
2151 CmdArgs.push_back("-target-feature");
2152 CmdArgs.push_back("-soft-float");
2153 continue;
2154 }
2155
2156 MipsTargetFeature = llvm::StringSwitch<const char *>(Value)
2157 .Case("-mips1", "+mips1")
2158 .Case("-mips2", "+mips2")
2159 .Case("-mips3", "+mips3")
2160 .Case("-mips4", "+mips4")
2161 .Case("-mips5", "+mips5")
2162 .Case("-mips32", "+mips32")
2163 .Case("-mips32r2", "+mips32r2")
2164 .Case("-mips32r3", "+mips32r3")
2165 .Case("-mips32r5", "+mips32r5")
2166 .Case("-mips32r6", "+mips32r6")
2167 .Case("-mips64", "+mips64")
2168 .Case("-mips64r2", "+mips64r2")
2169 .Case("-mips64r3", "+mips64r3")
2170 .Case("-mips64r5", "+mips64r5")
2171 .Case("-mips64r6", "+mips64r6")
2172 .Default(nullptr);
2173 if (MipsTargetFeature)
2174 continue;
2175 }
2176
2177 if (Value == "-force_cpusubtype_ALL") {
2178 // Do nothing, this is the default and we don't support anything else.
2179 } else if (Value == "-L") {
2180 CmdArgs.push_back("-msave-temp-labels");
2181 } else if (Value == "--fatal-warnings") {
2182 CmdArgs.push_back("-massembler-fatal-warnings");
Brian Cain7b953b62019-08-08 19:19:20 +00002183 } else if (Value == "--no-warn") {
2184 CmdArgs.push_back("-massembler-no-warn");
David L. Jonesf561aba2017-03-08 01:02:16 +00002185 } else if (Value == "--noexecstack") {
Dan Albert2715b282019-03-28 18:08:28 +00002186 UseNoExecStack = true;
Saleem Abdulrasoold064e912017-06-23 15:34:16 +00002187 } else if (Value.startswith("-compress-debug-sections") ||
2188 Value.startswith("--compress-debug-sections") ||
2189 Value == "-nocompress-debug-sections" ||
David L. Jonesf561aba2017-03-08 01:02:16 +00002190 Value == "--nocompress-debug-sections") {
Saleem Abdulrasoold064e912017-06-23 15:34:16 +00002191 CmdArgs.push_back(Value.data());
David L. Jonesf561aba2017-03-08 01:02:16 +00002192 } else if (Value == "-mrelax-relocations=yes" ||
2193 Value == "--mrelax-relocations=yes") {
2194 UseRelaxRelocations = true;
2195 } else if (Value == "-mrelax-relocations=no" ||
2196 Value == "--mrelax-relocations=no") {
2197 UseRelaxRelocations = false;
2198 } else if (Value.startswith("-I")) {
2199 CmdArgs.push_back(Value.data());
2200 // We need to consume the next argument if the current arg is a plain
2201 // -I. The next arg will be the include directory.
2202 if (Value == "-I")
2203 TakeNextArg = true;
2204 } else if (Value.startswith("-gdwarf-")) {
2205 // "-gdwarf-N" options are not cc1as options.
2206 unsigned DwarfVersion = DwarfVersionNum(Value);
2207 if (DwarfVersion == 0) { // Send it onward, and let cc1as complain.
2208 CmdArgs.push_back(Value.data());
2209 } else {
2210 RenderDebugEnablingArgs(Args, CmdArgs,
2211 codegenoptions::LimitedDebugInfo,
2212 DwarfVersion, llvm::DebuggerKind::Default);
2213 }
2214 } else if (Value.startswith("-mcpu") || Value.startswith("-mfpu") ||
2215 Value.startswith("-mhwdiv") || Value.startswith("-march")) {
2216 // Do nothing, we'll validate it later.
2217 } else if (Value == "-defsym") {
2218 if (A->getNumValues() != 2) {
2219 D.Diag(diag::err_drv_defsym_invalid_format) << Value;
2220 break;
2221 }
2222 const char *S = A->getValue(1);
2223 auto Pair = StringRef(S).split('=');
2224 auto Sym = Pair.first;
2225 auto SVal = Pair.second;
2226
2227 if (Sym.empty() || SVal.empty()) {
2228 D.Diag(diag::err_drv_defsym_invalid_format) << S;
2229 break;
2230 }
2231 int64_t IVal;
2232 if (SVal.getAsInteger(0, IVal)) {
2233 D.Diag(diag::err_drv_defsym_invalid_symval) << SVal;
2234 break;
2235 }
2236 CmdArgs.push_back(Value.data());
2237 TakeNextArg = true;
Nico Weber4c9fa4a2018-12-06 18:50:39 +00002238 } else if (Value == "-fdebug-compilation-dir") {
2239 CmdArgs.push_back("-fdebug-compilation-dir");
2240 TakeNextArg = true;
David L. Jonesf561aba2017-03-08 01:02:16 +00002241 } else {
2242 D.Diag(diag::err_drv_unsupported_option_argument)
2243 << A->getOption().getName() << Value;
2244 }
2245 }
2246 }
David L. Jonesf561aba2017-03-08 01:02:16 +00002247 if (UseRelaxRelocations)
2248 CmdArgs.push_back("--mrelax-relocations");
Dan Albert2715b282019-03-28 18:08:28 +00002249 if (UseNoExecStack)
2250 CmdArgs.push_back("-mnoexecstack");
David L. Jonesf561aba2017-03-08 01:02:16 +00002251 if (MipsTargetFeature != nullptr) {
2252 CmdArgs.push_back("-target-feature");
2253 CmdArgs.push_back(MipsTargetFeature);
2254 }
Steven Wu098742f2018-12-12 17:30:16 +00002255
2256 // forward -fembed-bitcode to assmebler
2257 if (C.getDriver().embedBitcodeEnabled() ||
2258 C.getDriver().embedBitcodeMarkerOnly())
2259 Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ);
David L. Jonesf561aba2017-03-08 01:02:16 +00002260}
2261
Saleem Abdulrasoole6d219d2017-09-01 22:04:24 +00002262static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
2263 bool OFastEnabled, const ArgList &Args,
2264 ArgStringList &CmdArgs) {
2265 // Handle various floating point optimization flags, mapping them to the
2266 // appropriate LLVM code generation flags. This is complicated by several
2267 // "umbrella" flags, so we do this by stepping through the flags incrementally
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002268 // adjusting what we think is enabled/disabled, then at the end setting the
Saleem Abdulrasoole6d219d2017-09-01 22:04:24 +00002269 // LLVM flags based on the final state.
2270 bool HonorINFs = true;
2271 bool HonorNaNs = true;
2272 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
2273 bool MathErrno = TC.IsMathErrnoDefault();
2274 bool AssociativeMath = false;
2275 bool ReciprocalMath = false;
2276 bool SignedZeros = true;
2277 bool TrappingMath = true;
2278 StringRef DenormalFPMath = "";
2279 StringRef FPContract = "";
2280
Saleem Abdulrasool258e4f62018-09-18 21:12:39 +00002281 if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
2282 CmdArgs.push_back("-mlimit-float-precision");
2283 CmdArgs.push_back(A->getValue());
2284 }
2285
Saleem Abdulrasoole6d219d2017-09-01 22:04:24 +00002286 for (const Arg *A : Args) {
2287 switch (A->getOption().getID()) {
2288 // If this isn't an FP option skip the claim below
2289 default: continue;
2290
2291 // Options controlling individual features
2292 case options::OPT_fhonor_infinities: HonorINFs = true; break;
2293 case options::OPT_fno_honor_infinities: HonorINFs = false; break;
2294 case options::OPT_fhonor_nans: HonorNaNs = true; break;
2295 case options::OPT_fno_honor_nans: HonorNaNs = false; break;
2296 case options::OPT_fmath_errno: MathErrno = true; break;
2297 case options::OPT_fno_math_errno: MathErrno = false; break;
2298 case options::OPT_fassociative_math: AssociativeMath = true; break;
2299 case options::OPT_fno_associative_math: AssociativeMath = false; break;
2300 case options::OPT_freciprocal_math: ReciprocalMath = true; break;
2301 case options::OPT_fno_reciprocal_math: ReciprocalMath = false; break;
2302 case options::OPT_fsigned_zeros: SignedZeros = true; break;
2303 case options::OPT_fno_signed_zeros: SignedZeros = false; break;
2304 case options::OPT_ftrapping_math: TrappingMath = true; break;
2305 case options::OPT_fno_trapping_math: TrappingMath = false; break;
2306
2307 case options::OPT_fdenormal_fp_math_EQ:
2308 DenormalFPMath = A->getValue();
2309 break;
2310
2311 // Validate and pass through -fp-contract option.
2312 case options::OPT_ffp_contract: {
2313 StringRef Val = A->getValue();
2314 if (Val == "fast" || Val == "on" || Val == "off")
2315 FPContract = Val;
2316 else
2317 D.Diag(diag::err_drv_unsupported_option_argument)
2318 << A->getOption().getName() << Val;
2319 break;
2320 }
2321
2322 case options::OPT_ffinite_math_only:
2323 HonorINFs = false;
2324 HonorNaNs = false;
2325 break;
2326 case options::OPT_fno_finite_math_only:
2327 HonorINFs = true;
2328 HonorNaNs = true;
2329 break;
2330
2331 case options::OPT_funsafe_math_optimizations:
2332 AssociativeMath = true;
2333 ReciprocalMath = true;
2334 SignedZeros = false;
2335 TrappingMath = false;
2336 break;
2337 case options::OPT_fno_unsafe_math_optimizations:
2338 AssociativeMath = false;
2339 ReciprocalMath = false;
2340 SignedZeros = true;
2341 TrappingMath = true;
2342 // -fno_unsafe_math_optimizations restores default denormal handling
2343 DenormalFPMath = "";
2344 break;
2345
2346 case options::OPT_Ofast:
2347 // If -Ofast is the optimization level, then -ffast-math should be enabled
2348 if (!OFastEnabled)
2349 continue;
2350 LLVM_FALLTHROUGH;
2351 case options::OPT_ffast_math:
2352 HonorINFs = false;
2353 HonorNaNs = false;
2354 MathErrno = false;
2355 AssociativeMath = true;
2356 ReciprocalMath = true;
2357 SignedZeros = false;
2358 TrappingMath = false;
2359 // If fast-math is set then set the fp-contract mode to fast.
2360 FPContract = "fast";
2361 break;
2362 case options::OPT_fno_fast_math:
2363 HonorINFs = true;
2364 HonorNaNs = true;
2365 // Turning on -ffast-math (with either flag) removes the need for
2366 // MathErrno. However, turning *off* -ffast-math merely restores the
2367 // toolchain default (which may be false).
2368 MathErrno = TC.IsMathErrnoDefault();
2369 AssociativeMath = false;
2370 ReciprocalMath = false;
2371 SignedZeros = true;
2372 TrappingMath = true;
2373 // -fno_fast_math restores default denormal and fpcontract handling
2374 DenormalFPMath = "";
2375 FPContract = "";
2376 break;
2377 }
2378
2379 // If we handled this option claim it
2380 A->claim();
2381 }
2382
2383 if (!HonorINFs)
2384 CmdArgs.push_back("-menable-no-infs");
2385
2386 if (!HonorNaNs)
2387 CmdArgs.push_back("-menable-no-nans");
2388
2389 if (MathErrno)
2390 CmdArgs.push_back("-fmath-errno");
2391
2392 if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
2393 !TrappingMath)
2394 CmdArgs.push_back("-menable-unsafe-fp-math");
2395
2396 if (!SignedZeros)
2397 CmdArgs.push_back("-fno-signed-zeros");
2398
Sanjay Patelcb8c0092017-12-16 16:11:17 +00002399 if (AssociativeMath && !SignedZeros && !TrappingMath)
2400 CmdArgs.push_back("-mreassociate");
2401
Saleem Abdulrasoole6d219d2017-09-01 22:04:24 +00002402 if (ReciprocalMath)
2403 CmdArgs.push_back("-freciprocal-math");
2404
2405 if (!TrappingMath)
2406 CmdArgs.push_back("-fno-trapping-math");
2407
2408 if (!DenormalFPMath.empty())
2409 CmdArgs.push_back(
2410 Args.MakeArgString("-fdenormal-fp-math=" + DenormalFPMath));
2411
2412 if (!FPContract.empty())
2413 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract));
2414
2415 ParseMRecip(D, Args, CmdArgs);
2416
2417 // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for the
2418 // individual features enabled by -ffast-math instead of the option itself as
2419 // that's consistent with gcc's behaviour.
2420 if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath &&
2421 ReciprocalMath && !SignedZeros && !TrappingMath)
2422 CmdArgs.push_back("-ffast-math");
2423
2424 // Handle __FINITE_MATH_ONLY__ similarly.
2425 if (!HonorINFs && !HonorNaNs)
2426 CmdArgs.push_back("-ffinite-math-only");
Saleem Abdulrasoolfb302ca2017-09-03 04:46:57 +00002427
2428 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) {
2429 CmdArgs.push_back("-mfpmath");
2430 CmdArgs.push_back(A->getValue());
2431 }
Sanjay Pateld1754762018-04-27 14:22:48 +00002432
2433 // Disable a codegen optimization for floating-point casts.
Sanjay Patelc81450e2018-04-30 18:19:03 +00002434 if (Args.hasFlag(options::OPT_fno_strict_float_cast_overflow,
2435 options::OPT_fstrict_float_cast_overflow, false))
2436 CmdArgs.push_back("-fno-strict-float-cast-overflow");
Saleem Abdulrasoole6d219d2017-09-01 22:04:24 +00002437}
2438
Saleem Abdulrasool24aafa52017-08-30 14:18:08 +00002439static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs,
2440 const llvm::Triple &Triple,
2441 const InputInfo &Input) {
2442 // Enable region store model by default.
2443 CmdArgs.push_back("-analyzer-store=region");
2444
2445 // Treat blocks as analysis entry points.
2446 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
2447
Saleem Abdulrasool24aafa52017-08-30 14:18:08 +00002448 // Add default argument set.
2449 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
2450 CmdArgs.push_back("-analyzer-checker=core");
2451 CmdArgs.push_back("-analyzer-checker=apiModeling");
2452
2453 if (!Triple.isWindowsMSVCEnvironment()) {
2454 CmdArgs.push_back("-analyzer-checker=unix");
2455 } else {
2456 // Enable "unix" checkers that also work on Windows.
2457 CmdArgs.push_back("-analyzer-checker=unix.API");
2458 CmdArgs.push_back("-analyzer-checker=unix.Malloc");
2459 CmdArgs.push_back("-analyzer-checker=unix.MallocSizeof");
2460 CmdArgs.push_back("-analyzer-checker=unix.MismatchedDeallocator");
2461 CmdArgs.push_back("-analyzer-checker=unix.cstring.BadSizeArg");
2462 CmdArgs.push_back("-analyzer-checker=unix.cstring.NullArg");
2463 }
2464
2465 // Disable some unix checkers for PS4.
2466 if (Triple.isPS4CPU()) {
2467 CmdArgs.push_back("-analyzer-disable-checker=unix.API");
2468 CmdArgs.push_back("-analyzer-disable-checker=unix.Vfork");
2469 }
2470
2471 if (Triple.isOSDarwin())
2472 CmdArgs.push_back("-analyzer-checker=osx");
2473
2474 CmdArgs.push_back("-analyzer-checker=deadcode");
2475
2476 if (types::isCXX(Input.getType()))
2477 CmdArgs.push_back("-analyzer-checker=cplusplus");
2478
2479 if (!Triple.isPS4CPU()) {
2480 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn");
2481 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
2482 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
2483 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
2484 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
2485 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
2486 }
2487
2488 // Default nullability checks.
2489 CmdArgs.push_back("-analyzer-checker=nullability.NullPassedToNonnull");
2490 CmdArgs.push_back("-analyzer-checker=nullability.NullReturnedFromNonnull");
2491 }
2492
2493 // Set the output format. The default is plist, for (lame) historical reasons.
2494 CmdArgs.push_back("-analyzer-output");
2495 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
2496 CmdArgs.push_back(A->getValue());
2497 else
2498 CmdArgs.push_back("plist");
2499
2500 // Disable the presentation of standard compiler warnings when using
2501 // --analyze. We only want to show static analyzer diagnostics or frontend
2502 // errors.
2503 CmdArgs.push_back("-w");
2504
2505 // Add -Xanalyzer arguments when running as analyzer.
2506 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
2507}
2508
Saleem Abdulrasoold5ba5452017-08-29 23:59:08 +00002509static void RenderSSPOptions(const ToolChain &TC, const ArgList &Args,
Saleem Abdulrasoolc2320ad2017-09-06 04:56:23 +00002510 ArgStringList &CmdArgs, bool KernelOrKext) {
Saleem Abdulrasoold5ba5452017-08-29 23:59:08 +00002511 const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple();
2512
2513 // NVPTX doesn't support stack protectors; from the compiler's perspective, it
2514 // doesn't even have a stack!
2515 if (EffectiveTriple.isNVPTX())
2516 return;
2517
2518 // -stack-protector=0 is default.
2519 unsigned StackProtectorLevel = 0;
2520 unsigned DefaultStackProtectorLevel =
2521 TC.GetDefaultStackProtectorLevel(KernelOrKext);
2522
2523 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
2524 options::OPT_fstack_protector_all,
2525 options::OPT_fstack_protector_strong,
2526 options::OPT_fstack_protector)) {
2527 if (A->getOption().matches(options::OPT_fstack_protector))
2528 StackProtectorLevel =
2529 std::max<unsigned>(LangOptions::SSPOn, DefaultStackProtectorLevel);
2530 else if (A->getOption().matches(options::OPT_fstack_protector_strong))
2531 StackProtectorLevel = LangOptions::SSPStrong;
2532 else if (A->getOption().matches(options::OPT_fstack_protector_all))
2533 StackProtectorLevel = LangOptions::SSPReq;
2534 } else {
Bruno Cardoso Lopesbad2c4a2017-09-06 00:44:10 +00002535 StackProtectorLevel = DefaultStackProtectorLevel;
Saleem Abdulrasoold5ba5452017-08-29 23:59:08 +00002536 }
2537
2538 if (StackProtectorLevel) {
2539 CmdArgs.push_back("-stack-protector");
2540 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
2541 }
2542
2543 // --param ssp-buffer-size=
2544 for (const Arg *A : Args.filtered(options::OPT__param)) {
2545 StringRef Str(A->getValue());
2546 if (Str.startswith("ssp-buffer-size=")) {
2547 if (StackProtectorLevel) {
2548 CmdArgs.push_back("-stack-protector-buffer-size");
2549 // FIXME: Verify the argument is a valid integer.
2550 CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
2551 }
2552 A->claim();
2553 }
2554 }
2555}
2556
JF Bastien14daa202018-12-18 05:12:21 +00002557static void RenderTrivialAutoVarInitOptions(const Driver &D,
2558 const ToolChain &TC,
2559 const ArgList &Args,
2560 ArgStringList &CmdArgs) {
2561 auto DefaultTrivialAutoVarInit = TC.GetDefaultTrivialAutoVarInit();
2562 StringRef TrivialAutoVarInit = "";
2563
2564 for (const Arg *A : Args) {
2565 switch (A->getOption().getID()) {
2566 default:
2567 continue;
2568 case options::OPT_ftrivial_auto_var_init: {
2569 A->claim();
2570 StringRef Val = A->getValue();
2571 if (Val == "uninitialized" || Val == "zero" || Val == "pattern")
2572 TrivialAutoVarInit = Val;
2573 else
2574 D.Diag(diag::err_drv_unsupported_option_argument)
2575 << A->getOption().getName() << Val;
2576 break;
2577 }
2578 }
2579 }
2580
2581 if (TrivialAutoVarInit.empty())
2582 switch (DefaultTrivialAutoVarInit) {
2583 case LangOptions::TrivialAutoVarInitKind::Uninitialized:
2584 break;
2585 case LangOptions::TrivialAutoVarInitKind::Pattern:
2586 TrivialAutoVarInit = "pattern";
2587 break;
2588 case LangOptions::TrivialAutoVarInitKind::Zero:
2589 TrivialAutoVarInit = "zero";
2590 break;
2591 }
2592
2593 if (!TrivialAutoVarInit.empty()) {
2594 if (TrivialAutoVarInit == "zero" && !Args.hasArg(options::OPT_enable_trivial_var_init_zero))
2595 D.Diag(diag::err_drv_trivial_auto_var_init_zero_disabled);
2596 CmdArgs.push_back(
2597 Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit));
2598 }
2599}
2600
Saleem Abdulrasool68c808f62017-08-29 23:59:07 +00002601static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs) {
2602 const unsigned ForwardedArguments[] = {
2603 options::OPT_cl_opt_disable,
2604 options::OPT_cl_strict_aliasing,
2605 options::OPT_cl_single_precision_constant,
2606 options::OPT_cl_finite_math_only,
2607 options::OPT_cl_kernel_arg_info,
2608 options::OPT_cl_unsafe_math_optimizations,
2609 options::OPT_cl_fast_relaxed_math,
2610 options::OPT_cl_mad_enable,
2611 options::OPT_cl_no_signed_zeros,
2612 options::OPT_cl_denorms_are_zero,
2613 options::OPT_cl_fp32_correctly_rounded_divide_sqrt,
Alexey Sotkin20f65922018-02-22 11:54:14 +00002614 options::OPT_cl_uniform_work_group_size
Saleem Abdulrasool68c808f62017-08-29 23:59:07 +00002615 };
2616
2617 if (Arg *A = Args.getLastArg(options::OPT_cl_std_EQ)) {
2618 std::string CLStdStr = std::string("-cl-std=") + A->getValue();
2619 CmdArgs.push_back(Args.MakeArgString(CLStdStr));
2620 }
2621
2622 for (const auto &Arg : ForwardedArguments)
2623 if (const auto *A = Args.getLastArg(Arg))
2624 CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName()));
2625}
2626
Saleem Abdulrasool0a322c62017-08-31 15:35:01 +00002627static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args,
2628 ArgStringList &CmdArgs) {
2629 bool ARCMTEnabled = false;
2630 if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) {
2631 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
2632 options::OPT_ccc_arcmt_modify,
2633 options::OPT_ccc_arcmt_migrate)) {
2634 ARCMTEnabled = true;
2635 switch (A->getOption().getID()) {
2636 default: llvm_unreachable("missed a case");
2637 case options::OPT_ccc_arcmt_check:
2638 CmdArgs.push_back("-arcmt-check");
2639 break;
2640 case options::OPT_ccc_arcmt_modify:
2641 CmdArgs.push_back("-arcmt-modify");
2642 break;
2643 case options::OPT_ccc_arcmt_migrate:
2644 CmdArgs.push_back("-arcmt-migrate");
2645 CmdArgs.push_back("-mt-migrate-directory");
2646 CmdArgs.push_back(A->getValue());
2647
2648 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
2649 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
2650 break;
2651 }
2652 }
2653 } else {
2654 Args.ClaimAllArgs(options::OPT_ccc_arcmt_check);
2655 Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify);
2656 Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate);
2657 }
2658
2659 if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
2660 if (ARCMTEnabled)
2661 D.Diag(diag::err_drv_argument_not_allowed_with)
2662 << A->getAsString(Args) << "-ccc-arcmt-migrate";
2663
2664 CmdArgs.push_back("-mt-migrate-directory");
2665 CmdArgs.push_back(A->getValue());
2666
2667 if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
2668 options::OPT_objcmt_migrate_subscripting,
2669 options::OPT_objcmt_migrate_property)) {
2670 // None specified, means enable them all.
2671 CmdArgs.push_back("-objcmt-migrate-literals");
2672 CmdArgs.push_back("-objcmt-migrate-subscripting");
2673 CmdArgs.push_back("-objcmt-migrate-property");
2674 } else {
2675 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2676 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
2677 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
2678 }
2679 } else {
2680 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2681 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
2682 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
2683 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all);
2684 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property);
2685 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property);
2686 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property_dot_syntax);
2687 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation);
2688 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype);
2689 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros);
2690 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance);
2691 Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property);
2692 Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property);
2693 Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly);
2694 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init);
2695 Args.AddLastArg(CmdArgs, options::OPT_objcmt_whitelist_dir_path);
2696 }
2697}
2698
Saleem Abdulrasool99f4ead2017-09-01 23:44:01 +00002699static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T,
2700 const ArgList &Args, ArgStringList &CmdArgs) {
2701 // -fbuiltin is default unless -mkernel is used.
2702 bool UseBuiltins =
2703 Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin,
2704 !Args.hasArg(options::OPT_mkernel));
2705 if (!UseBuiltins)
2706 CmdArgs.push_back("-fno-builtin");
2707
2708 // -ffreestanding implies -fno-builtin.
2709 if (Args.hasArg(options::OPT_ffreestanding))
2710 UseBuiltins = false;
2711
2712 // Process the -fno-builtin-* options.
2713 for (const auto &Arg : Args) {
2714 const Option &O = Arg->getOption();
2715 if (!O.matches(options::OPT_fno_builtin_))
2716 continue;
2717
2718 Arg->claim();
2719
2720 // If -fno-builtin is specified, then there's no need to pass the option to
2721 // the frontend.
2722 if (!UseBuiltins)
2723 continue;
2724
2725 StringRef FuncName = Arg->getValue();
2726 CmdArgs.push_back(Args.MakeArgString("-fno-builtin-" + FuncName));
2727 }
2728
2729 // le32-specific flags:
2730 // -fno-math-builtin: clang should not convert math builtins to intrinsics
2731 // by default.
2732 if (TC.getArch() == llvm::Triple::le32)
2733 CmdArgs.push_back("-fno-math-builtin");
Saleem Abdulrasool99f4ead2017-09-01 23:44:01 +00002734}
2735
Adrian Prantl70599032018-02-09 18:43:10 +00002736void Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) {
2737 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false, Result);
2738 llvm::sys::path::append(Result, "org.llvm.clang.");
2739 appendUserToPath(Result);
2740 llvm::sys::path::append(Result, "ModuleCache");
2741}
2742
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00002743static void RenderModulesOptions(Compilation &C, const Driver &D,
2744 const ArgList &Args, const InputInfo &Input,
2745 const InputInfo &Output,
2746 ArgStringList &CmdArgs, bool &HaveModules) {
2747 // -fmodules enables the use of precompiled modules (off by default).
2748 // Users can pass -fno-cxx-modules to turn off modules support for
2749 // C++/Objective-C++ programs.
2750 bool HaveClangModules = false;
2751 if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
2752 bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
2753 options::OPT_fno_cxx_modules, true);
2754 if (AllowedInCXX || !types::isCXX(Input.getType())) {
2755 CmdArgs.push_back("-fmodules");
2756 HaveClangModules = true;
2757 }
2758 }
2759
Richard Smithb1b580e2019-04-14 11:11:37 +00002760 HaveModules |= HaveClangModules;
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00002761 if (Args.hasArg(options::OPT_fmodules_ts)) {
2762 CmdArgs.push_back("-fmodules-ts");
2763 HaveModules = true;
2764 }
2765
2766 // -fmodule-maps enables implicit reading of module map files. By default,
2767 // this is enabled if we are using Clang's flavor of precompiled modules.
2768 if (Args.hasFlag(options::OPT_fimplicit_module_maps,
2769 options::OPT_fno_implicit_module_maps, HaveClangModules))
2770 CmdArgs.push_back("-fimplicit-module-maps");
2771
2772 // -fmodules-decluse checks that modules used are declared so (off by default)
2773 if (Args.hasFlag(options::OPT_fmodules_decluse,
2774 options::OPT_fno_modules_decluse, false))
2775 CmdArgs.push_back("-fmodules-decluse");
2776
2777 // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that
2778 // all #included headers are part of modules.
2779 if (Args.hasFlag(options::OPT_fmodules_strict_decluse,
2780 options::OPT_fno_modules_strict_decluse, false))
2781 CmdArgs.push_back("-fmodules-strict-decluse");
2782
2783 // -fno-implicit-modules turns off implicitly compiling modules on demand.
Bruno Cardoso Lopes89b9fdb2018-04-18 06:07:49 +00002784 bool ImplicitModules = false;
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00002785 if (!Args.hasFlag(options::OPT_fimplicit_modules,
2786 options::OPT_fno_implicit_modules, HaveClangModules)) {
2787 if (HaveModules)
2788 CmdArgs.push_back("-fno-implicit-modules");
2789 } else if (HaveModules) {
Bruno Cardoso Lopes89b9fdb2018-04-18 06:07:49 +00002790 ImplicitModules = true;
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00002791 // -fmodule-cache-path specifies where our implicitly-built module files
2792 // should be written.
2793 SmallString<128> Path;
2794 if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path))
2795 Path = A->getValue();
2796
2797 if (C.isForDiagnostics()) {
2798 // When generating crash reports, we want to emit the modules along with
2799 // the reproduction sources, so we ignore any provided module path.
2800 Path = Output.getFilename();
2801 llvm::sys::path::replace_extension(Path, ".cache");
2802 llvm::sys::path::append(Path, "modules");
2803 } else if (Path.empty()) {
2804 // No module path was provided: use the default.
Adrian Prantl70599032018-02-09 18:43:10 +00002805 Driver::getDefaultModuleCachePath(Path);
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00002806 }
2807
2808 const char Arg[] = "-fmodules-cache-path=";
2809 Path.insert(Path.begin(), Arg, Arg + strlen(Arg));
2810 CmdArgs.push_back(Args.MakeArgString(Path));
2811 }
2812
2813 if (HaveModules) {
2814 // -fprebuilt-module-path specifies where to load the prebuilt module files.
2815 for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) {
2816 CmdArgs.push_back(Args.MakeArgString(
2817 std::string("-fprebuilt-module-path=") + A->getValue()));
2818 A->claim();
2819 }
2820 }
2821
2822 // -fmodule-name specifies the module that is currently being built (or
2823 // used for header checking by -fmodule-maps).
2824 Args.AddLastArg(CmdArgs, options::OPT_fmodule_name_EQ);
2825
2826 // -fmodule-map-file can be used to specify files containing module
2827 // definitions.
2828 Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file);
2829
2830 // -fbuiltin-module-map can be used to load the clang
2831 // builtin headers modulemap file.
2832 if (Args.hasArg(options::OPT_fbuiltin_module_map)) {
2833 SmallString<128> BuiltinModuleMap(D.ResourceDir);
2834 llvm::sys::path::append(BuiltinModuleMap, "include");
2835 llvm::sys::path::append(BuiltinModuleMap, "module.modulemap");
2836 if (llvm::sys::fs::exists(BuiltinModuleMap))
2837 CmdArgs.push_back(
2838 Args.MakeArgString("-fmodule-map-file=" + BuiltinModuleMap));
2839 }
2840
2841 // The -fmodule-file=<name>=<file> form specifies the mapping of module
2842 // names to precompiled module files (the module is loaded only if used).
2843 // The -fmodule-file=<file> form can be used to unconditionally load
2844 // precompiled module files (whether used or not).
2845 if (HaveModules)
2846 Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file);
2847 else
2848 Args.ClaimAllArgs(options::OPT_fmodule_file);
2849
2850 // When building modules and generating crashdumps, we need to dump a module
2851 // dependency VFS alongside the output.
2852 if (HaveClangModules && C.isForDiagnostics()) {
2853 SmallString<128> VFSDir(Output.getFilename());
2854 llvm::sys::path::replace_extension(VFSDir, ".cache");
2855 // Add the cache directory as a temp so the crash diagnostics pick it up.
2856 C.addTempFile(Args.MakeArgString(VFSDir));
2857
2858 llvm::sys::path::append(VFSDir, "vfs");
2859 CmdArgs.push_back("-module-dependency-dir");
2860 CmdArgs.push_back(Args.MakeArgString(VFSDir));
2861 }
2862
2863 if (HaveClangModules)
2864 Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path);
2865
2866 // Pass through all -fmodules-ignore-macro arguments.
2867 Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
2868 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
2869 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
2870
2871 Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp);
2872
2873 if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) {
2874 if (Args.hasArg(options::OPT_fbuild_session_timestamp))
2875 D.Diag(diag::err_drv_argument_not_allowed_with)
2876 << A->getAsString(Args) << "-fbuild-session-timestamp";
2877
2878 llvm::sys::fs::file_status Status;
2879 if (llvm::sys::fs::status(A->getValue(), Status))
2880 D.Diag(diag::err_drv_no_such_file) << A->getValue();
2881 CmdArgs.push_back(
2882 Args.MakeArgString("-fbuild-session-timestamp=" +
2883 Twine((uint64_t)Status.getLastModificationTime()
2884 .time_since_epoch()
2885 .count())));
2886 }
2887
2888 if (Args.getLastArg(options::OPT_fmodules_validate_once_per_build_session)) {
2889 if (!Args.getLastArg(options::OPT_fbuild_session_timestamp,
2890 options::OPT_fbuild_session_file))
2891 D.Diag(diag::err_drv_modules_validate_once_requires_timestamp);
2892
2893 Args.AddLastArg(CmdArgs,
2894 options::OPT_fmodules_validate_once_per_build_session);
2895 }
2896
Bruno Cardoso Lopes89b9fdb2018-04-18 06:07:49 +00002897 if (Args.hasFlag(options::OPT_fmodules_validate_system_headers,
2898 options::OPT_fno_modules_validate_system_headers,
2899 ImplicitModules))
2900 CmdArgs.push_back("-fmodules-validate-system-headers");
2901
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00002902 Args.AddLastArg(CmdArgs, options::OPT_fmodules_disable_diagnostic_validation);
2903}
2904
Saleem Abdulrasool729379a2017-10-06 23:09:55 +00002905static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T,
2906 ArgStringList &CmdArgs) {
2907 // -fsigned-char is default.
2908 if (const Arg *A = Args.getLastArg(options::OPT_fsigned_char,
2909 options::OPT_fno_signed_char,
2910 options::OPT_funsigned_char,
2911 options::OPT_fno_unsigned_char)) {
2912 if (A->getOption().matches(options::OPT_funsigned_char) ||
2913 A->getOption().matches(options::OPT_fno_signed_char)) {
2914 CmdArgs.push_back("-fno-signed-char");
2915 }
2916 } else if (!isSignedCharDefault(T)) {
2917 CmdArgs.push_back("-fno-signed-char");
2918 }
2919
Richard Smith28ddb912018-11-14 21:04:34 +00002920 // The default depends on the language standard.
Nico Weber908b6972019-06-26 17:51:47 +00002921 Args.AddLastArg(CmdArgs, options::OPT_fchar8__t, options::OPT_fno_char8__t);
Richard Smith3a8244d2018-05-01 05:02:45 +00002922
Saleem Abdulrasool729379a2017-10-06 23:09:55 +00002923 if (const Arg *A = Args.getLastArg(options::OPT_fshort_wchar,
2924 options::OPT_fno_short_wchar)) {
2925 if (A->getOption().matches(options::OPT_fshort_wchar)) {
2926 CmdArgs.push_back("-fwchar-type=short");
2927 CmdArgs.push_back("-fno-signed-wchar");
2928 } else {
Saleem Abdulrasool8ba8b022017-10-29 06:01:14 +00002929 bool IsARM = T.isARM() || T.isThumb() || T.isAArch64();
Saleem Abdulrasool729379a2017-10-06 23:09:55 +00002930 CmdArgs.push_back("-fwchar-type=int");
Michal Gorny5a409d02018-12-20 13:09:30 +00002931 if (IsARM && !(T.isOSWindows() || T.isOSNetBSD() ||
2932 T.isOSOpenBSD()))
Saleem Abdulrasool8ba8b022017-10-29 06:01:14 +00002933 CmdArgs.push_back("-fno-signed-wchar");
2934 else
2935 CmdArgs.push_back("-fsigned-wchar");
Saleem Abdulrasool729379a2017-10-06 23:09:55 +00002936 }
2937 }
2938}
2939
Saleem Abdulrasoolb2d4ebb2017-09-01 17:43:59 +00002940static void RenderObjCOptions(const ToolChain &TC, const Driver &D,
2941 const llvm::Triple &T, const ArgList &Args,
2942 ObjCRuntime &Runtime, bool InferCovariantReturns,
2943 const InputInfo &Input, ArgStringList &CmdArgs) {
2944 const llvm::Triple::ArchType Arch = TC.getArch();
2945
2946 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and legacy
2947 // is the default. Except for deployment target of 10.5, next runtime is
2948 // always legacy dispatch and -fno-objc-legacy-dispatch gets ignored silently.
2949 if (Runtime.isNonFragile()) {
2950 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
2951 options::OPT_fno_objc_legacy_dispatch,
2952 Runtime.isLegacyDispatchDefaultForArch(Arch))) {
2953 if (TC.UseObjCMixedDispatch())
2954 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
2955 else
2956 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
2957 }
2958 }
2959
2960 // When ObjectiveC legacy runtime is in effect on MacOSX, turn on the option
2961 // to do Array/Dictionary subscripting by default.
2962 if (Arch == llvm::Triple::x86 && T.isMacOSX() &&
Saleem Abdulrasoolb2d4ebb2017-09-01 17:43:59 +00002963 Runtime.getKind() == ObjCRuntime::FragileMacOSX && Runtime.isNeXTFamily())
2964 CmdArgs.push_back("-fobjc-subscripting-legacy-runtime");
2965
2966 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
2967 // NOTE: This logic is duplicated in ToolChains.cpp.
2968 if (isObjCAutoRefCount(Args)) {
2969 TC.CheckObjCARC();
2970
2971 CmdArgs.push_back("-fobjc-arc");
2972
2973 // FIXME: It seems like this entire block, and several around it should be
2974 // wrapped in isObjC, but for now we just use it here as this is where it
2975 // was being used previously.
2976 if (types::isCXX(Input.getType()) && types::isObjC(Input.getType())) {
2977 if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
2978 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
2979 else
2980 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
2981 }
2982
2983 // Allow the user to enable full exceptions code emission.
2984 // We default off for Objective-C, on for Objective-C++.
2985 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
2986 options::OPT_fno_objc_arc_exceptions,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00002987 /*Default=*/types::isCXX(Input.getType())))
Saleem Abdulrasoolb2d4ebb2017-09-01 17:43:59 +00002988 CmdArgs.push_back("-fobjc-arc-exceptions");
2989 }
2990
2991 // Silence warning for full exception code emission options when explicitly
2992 // set to use no ARC.
2993 if (Args.hasArg(options::OPT_fno_objc_arc)) {
2994 Args.ClaimAllArgs(options::OPT_fobjc_arc_exceptions);
2995 Args.ClaimAllArgs(options::OPT_fno_objc_arc_exceptions);
2996 }
2997
Pete Coopere3886802018-12-08 05:13:50 +00002998 // Allow the user to control whether messages can be converted to runtime
2999 // functions.
3000 if (types::isObjC(Input.getType())) {
3001 auto *Arg = Args.getLastArg(
3002 options::OPT_fobjc_convert_messages_to_runtime_calls,
3003 options::OPT_fno_objc_convert_messages_to_runtime_calls);
3004 if (Arg &&
3005 Arg->getOption().matches(
3006 options::OPT_fno_objc_convert_messages_to_runtime_calls))
3007 CmdArgs.push_back("-fno-objc-convert-messages-to-runtime-calls");
3008 }
3009
Saleem Abdulrasoolb2d4ebb2017-09-01 17:43:59 +00003010 // -fobjc-infer-related-result-type is the default, except in the Objective-C
3011 // rewriter.
3012 if (InferCovariantReturns)
3013 CmdArgs.push_back("-fno-objc-infer-related-result-type");
3014
3015 // Pass down -fobjc-weak or -fno-objc-weak if present.
3016 if (types::isObjC(Input.getType())) {
3017 auto WeakArg =
3018 Args.getLastArg(options::OPT_fobjc_weak, options::OPT_fno_objc_weak);
3019 if (!WeakArg) {
3020 // nothing to do
3021 } else if (!Runtime.allowsWeak()) {
3022 if (WeakArg->getOption().matches(options::OPT_fobjc_weak))
3023 D.Diag(diag::err_objc_weak_unsupported);
3024 } else {
3025 WeakArg->render(Args, CmdArgs);
3026 }
3027 }
3028}
3029
Saleem Abdulrasool75557fa2017-09-01 18:57:34 +00003030static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args,
3031 ArgStringList &CmdArgs) {
3032 bool CaretDefault = true;
3033 bool ColumnDefault = true;
3034
3035 if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diagnostics_classic,
3036 options::OPT__SLASH_diagnostics_column,
3037 options::OPT__SLASH_diagnostics_caret)) {
3038 switch (A->getOption().getID()) {
3039 case options::OPT__SLASH_diagnostics_caret:
3040 CaretDefault = true;
3041 ColumnDefault = true;
3042 break;
3043 case options::OPT__SLASH_diagnostics_column:
3044 CaretDefault = false;
3045 ColumnDefault = true;
3046 break;
3047 case options::OPT__SLASH_diagnostics_classic:
3048 CaretDefault = false;
3049 ColumnDefault = false;
3050 break;
3051 }
3052 }
3053
3054 // -fcaret-diagnostics is default.
3055 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
3056 options::OPT_fno_caret_diagnostics, CaretDefault))
3057 CmdArgs.push_back("-fno-caret-diagnostics");
3058
3059 // -fdiagnostics-fixit-info is default, only pass non-default.
3060 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
3061 options::OPT_fno_diagnostics_fixit_info))
3062 CmdArgs.push_back("-fno-diagnostics-fixit-info");
3063
3064 // Enable -fdiagnostics-show-option by default.
3065 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
3066 options::OPT_fno_diagnostics_show_option))
3067 CmdArgs.push_back("-fdiagnostics-show-option");
3068
3069 if (const Arg *A =
3070 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
3071 CmdArgs.push_back("-fdiagnostics-show-category");
3072 CmdArgs.push_back(A->getValue());
3073 }
3074
3075 if (Args.hasFlag(options::OPT_fdiagnostics_show_hotness,
3076 options::OPT_fno_diagnostics_show_hotness, false))
3077 CmdArgs.push_back("-fdiagnostics-show-hotness");
3078
3079 if (const Arg *A =
3080 Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) {
3081 std::string Opt =
3082 std::string("-fdiagnostics-hotness-threshold=") + A->getValue();
3083 CmdArgs.push_back(Args.MakeArgString(Opt));
3084 }
3085
3086 if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
3087 CmdArgs.push_back("-fdiagnostics-format");
3088 CmdArgs.push_back(A->getValue());
3089 }
3090
3091 if (const Arg *A = Args.getLastArg(
3092 options::OPT_fdiagnostics_show_note_include_stack,
3093 options::OPT_fno_diagnostics_show_note_include_stack)) {
3094 const Option &O = A->getOption();
3095 if (O.matches(options::OPT_fdiagnostics_show_note_include_stack))
3096 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
3097 else
3098 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
3099 }
3100
3101 // Color diagnostics are parsed by the driver directly from argv and later
3102 // re-parsed to construct this job; claim any possible color diagnostic here
3103 // to avoid warn_drv_unused_argument and diagnose bad
3104 // OPT_fdiagnostics_color_EQ values.
3105 for (const Arg *A : Args) {
3106 const Option &O = A->getOption();
3107 if (!O.matches(options::OPT_fcolor_diagnostics) &&
3108 !O.matches(options::OPT_fdiagnostics_color) &&
3109 !O.matches(options::OPT_fno_color_diagnostics) &&
3110 !O.matches(options::OPT_fno_diagnostics_color) &&
3111 !O.matches(options::OPT_fdiagnostics_color_EQ))
3112 continue;
3113
3114 if (O.matches(options::OPT_fdiagnostics_color_EQ)) {
3115 StringRef Value(A->getValue());
3116 if (Value != "always" && Value != "never" && Value != "auto")
3117 D.Diag(diag::err_drv_clang_unsupported)
3118 << ("-fdiagnostics-color=" + Value).str();
3119 }
3120 A->claim();
3121 }
3122
3123 if (D.getDiags().getDiagnosticOptions().ShowColors)
3124 CmdArgs.push_back("-fcolor-diagnostics");
3125
3126 if (Args.hasArg(options::OPT_fansi_escape_codes))
3127 CmdArgs.push_back("-fansi-escape-codes");
3128
3129 if (!Args.hasFlag(options::OPT_fshow_source_location,
3130 options::OPT_fno_show_source_location))
3131 CmdArgs.push_back("-fno-show-source-location");
3132
3133 if (Args.hasArg(options::OPT_fdiagnostics_absolute_paths))
3134 CmdArgs.push_back("-fdiagnostics-absolute-paths");
3135
3136 if (!Args.hasFlag(options::OPT_fshow_column, options::OPT_fno_show_column,
3137 ColumnDefault))
3138 CmdArgs.push_back("-fno-show-column");
3139
3140 if (!Args.hasFlag(options::OPT_fspell_checking,
3141 options::OPT_fno_spell_checking))
3142 CmdArgs.push_back("-fno-spell-checking");
3143}
3144
George Rimar91829ee2018-11-14 09:22:16 +00003145enum class DwarfFissionKind { None, Split, Single };
3146
3147static DwarfFissionKind getDebugFissionKind(const Driver &D,
3148 const ArgList &Args, Arg *&Arg) {
3149 Arg =
3150 Args.getLastArg(options::OPT_gsplit_dwarf, options::OPT_gsplit_dwarf_EQ);
3151 if (!Arg)
3152 return DwarfFissionKind::None;
3153
3154 if (Arg->getOption().matches(options::OPT_gsplit_dwarf))
3155 return DwarfFissionKind::Split;
3156
3157 StringRef Value = Arg->getValue();
3158 if (Value == "split")
3159 return DwarfFissionKind::Split;
3160 if (Value == "single")
3161 return DwarfFissionKind::Single;
3162
3163 D.Diag(diag::err_drv_unsupported_option_argument)
3164 << Arg->getOption().getName() << Arg->getValue();
3165 return DwarfFissionKind::None;
3166}
3167
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003168static void RenderDebugOptions(const ToolChain &TC, const Driver &D,
3169 const llvm::Triple &T, const ArgList &Args,
3170 bool EmitCodeView, bool IsWindowsMSVC,
3171 ArgStringList &CmdArgs,
3172 codegenoptions::DebugInfoKind &DebugInfoKind,
George Rimar91829ee2018-11-14 09:22:16 +00003173 DwarfFissionKind &DwarfFission) {
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003174 if (Args.hasFlag(options::OPT_fdebug_info_for_profiling,
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003175 options::OPT_fno_debug_info_for_profiling, false) &&
3176 checkDebugInfoOption(
3177 Args.getLastArg(options::OPT_fdebug_info_for_profiling), Args, D, TC))
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003178 CmdArgs.push_back("-fdebug-info-for-profiling");
3179
3180 // The 'g' groups options involve a somewhat intricate sequence of decisions
3181 // about what to pass from the driver to the frontend, but by the time they
3182 // reach cc1 they've been factored into three well-defined orthogonal choices:
3183 // * what level of debug info to generate
3184 // * what dwarf version to write
3185 // * what debugger tuning to use
3186 // This avoids having to monkey around further in cc1 other than to disable
3187 // codeview if not running in a Windows environment. Perhaps even that
3188 // decision should be made in the driver as well though.
3189 unsigned DWARFVersion = 0;
3190 llvm::DebuggerKind DebuggerTuning = TC.getDefaultDebuggerTuning();
3191
3192 bool SplitDWARFInlining =
3193 Args.hasFlag(options::OPT_fsplit_dwarf_inlining,
3194 options::OPT_fno_split_dwarf_inlining, true);
3195
3196 Args.ClaimAllArgs(options::OPT_g_Group);
3197
George Rimar91829ee2018-11-14 09:22:16 +00003198 Arg* SplitDWARFArg;
3199 DwarfFission = getDebugFissionKind(D, Args, SplitDWARFArg);
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003200
George Rimar91829ee2018-11-14 09:22:16 +00003201 if (DwarfFission != DwarfFissionKind::None &&
3202 !checkDebugInfoOption(SplitDWARFArg, Args, D, TC)) {
3203 DwarfFission = DwarfFissionKind::None;
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003204 SplitDWARFInlining = false;
3205 }
3206
Fangrui Songe3576b02019-04-17 01:46:27 +00003207 if (const Arg *A =
3208 Args.getLastArg(options::OPT_g_Group, options::OPT_gsplit_dwarf,
3209 options::OPT_gsplit_dwarf_EQ)) {
3210 DebugInfoKind = codegenoptions::LimitedDebugInfo;
3211
3212 // If the last option explicitly specified a debug-info level, use it.
3213 if (checkDebugInfoOption(A, Args, D, TC) &&
3214 A->getOption().matches(options::OPT_gN_Group)) {
3215 DebugInfoKind = DebugLevelToInfoKind(*A);
3216 // For -g0 or -gline-tables-only, drop -gsplit-dwarf. This gets a bit more
3217 // complicated if you've disabled inline info in the skeleton CUs
3218 // (SplitDWARFInlining) - then there's value in composing split-dwarf and
3219 // line-tables-only, so let those compose naturally in that case.
3220 if (DebugInfoKind == codegenoptions::NoDebugInfo ||
3221 DebugInfoKind == codegenoptions::DebugDirectivesOnly ||
3222 (DebugInfoKind == codegenoptions::DebugLineTablesOnly &&
3223 SplitDWARFInlining))
3224 DwarfFission = DwarfFissionKind::None;
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003225 }
3226 }
3227
3228 // If a debugger tuning argument appeared, remember it.
3229 if (const Arg *A =
3230 Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) {
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003231 if (checkDebugInfoOption(A, Args, D, TC)) {
3232 if (A->getOption().matches(options::OPT_glldb))
3233 DebuggerTuning = llvm::DebuggerKind::LLDB;
3234 else if (A->getOption().matches(options::OPT_gsce))
3235 DebuggerTuning = llvm::DebuggerKind::SCE;
3236 else
3237 DebuggerTuning = llvm::DebuggerKind::GDB;
3238 }
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003239 }
3240
3241 // If a -gdwarf argument appeared, remember it.
3242 if (const Arg *A =
3243 Args.getLastArg(options::OPT_gdwarf_2, options::OPT_gdwarf_3,
3244 options::OPT_gdwarf_4, options::OPT_gdwarf_5))
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003245 if (checkDebugInfoOption(A, Args, D, TC))
3246 DWARFVersion = DwarfVersionNum(A->getSpelling());
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003247
Reid Kleckner7b7b1142018-11-14 22:59:27 +00003248 if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) {
3249 if (checkDebugInfoOption(A, Args, D, TC))
3250 EmitCodeView = true;
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003251 }
3252
Reid Kleckner7b7b1142018-11-14 22:59:27 +00003253 // If the user asked for debug info but did not explicitly specify -gcodeview
3254 // or -gdwarf, ask the toolchain for the default format.
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003255 if (!EmitCodeView && DWARFVersion == 0 &&
Reid Kleckner7b7b1142018-11-14 22:59:27 +00003256 DebugInfoKind != codegenoptions::NoDebugInfo) {
3257 switch (TC.getDefaultDebugFormat()) {
3258 case codegenoptions::DIF_CodeView:
3259 EmitCodeView = true;
3260 break;
3261 case codegenoptions::DIF_DWARF:
3262 DWARFVersion = TC.GetDefaultDwarfVersion();
3263 break;
3264 }
3265 }
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003266
Alexey Bataev80e1b5e2018-08-31 13:56:14 +00003267 // -gline-directives-only supported only for the DWARF debug info.
3268 if (DWARFVersion == 0 && DebugInfoKind == codegenoptions::DebugDirectivesOnly)
3269 DebugInfoKind = codegenoptions::NoDebugInfo;
3270
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003271 // We ignore flag -gstrict-dwarf for now.
3272 // And we handle flag -grecord-gcc-switches later with DWARFDebugFlags.
3273 Args.ClaimAllArgs(options::OPT_g_flags_Group);
3274
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003275 // Column info is included by default for everything except SCE and
3276 // CodeView. Clang doesn't track end columns, just starting columns, which,
3277 // in theory, is fine for CodeView (and PDB). In practice, however, the
3278 // Microsoft debuggers don't handle missing end columns well, so it's better
3279 // not to include any column info.
3280 if (const Arg *A = Args.getLastArg(options::OPT_gcolumn_info))
3281 (void)checkDebugInfoOption(A, Args, D, TC);
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003282 if (Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info,
Martin Storsjof9fa17b2018-05-08 20:55:23 +00003283 /*Default=*/!EmitCodeView &&
Paul Robinsona8280812017-09-29 21:25:07 +00003284 DebuggerTuning != llvm::DebuggerKind::SCE))
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003285 CmdArgs.push_back("-dwarf-column-info");
3286
3287 // FIXME: Move backend command line options to the module.
Alexey Bataev80e1b5e2018-08-31 13:56:14 +00003288 // If -gline-tables-only or -gline-directives-only is the last option it wins.
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003289 if (const Arg *A = Args.getLastArg(options::OPT_gmodules))
3290 if (checkDebugInfoOption(A, Args, D, TC)) {
Alexey Bataev80e1b5e2018-08-31 13:56:14 +00003291 if (DebugInfoKind != codegenoptions::DebugLineTablesOnly &&
3292 DebugInfoKind != codegenoptions::DebugDirectivesOnly) {
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003293 DebugInfoKind = codegenoptions::LimitedDebugInfo;
3294 CmdArgs.push_back("-dwarf-ext-refs");
3295 CmdArgs.push_back("-fmodule-format=obj");
3296 }
3297 }
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003298
Aaron Puchertb207bae2019-06-26 21:36:35 +00003299 if (T.isOSBinFormatELF() && !SplitDWARFInlining)
3300 CmdArgs.push_back("-fno-split-dwarf-inlining");
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003301
3302 // After we've dealt with all combinations of things that could
3303 // make DebugInfoKind be other than None or DebugLineTablesOnly,
3304 // figure out if we need to "upgrade" it to standalone debug info.
3305 // We parse these two '-f' options whether or not they will be used,
3306 // to claim them even if you wrote "-fstandalone-debug -gline-tables-only"
David Blaikieb068f922019-04-16 00:16:29 +00003307 bool NeedFullDebug = Args.hasFlag(
3308 options::OPT_fstandalone_debug, options::OPT_fno_standalone_debug,
3309 DebuggerTuning == llvm::DebuggerKind::LLDB ||
3310 TC.GetDefaultStandaloneDebug());
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003311 if (const Arg *A = Args.getLastArg(options::OPT_fstandalone_debug))
3312 (void)checkDebugInfoOption(A, Args, D, TC);
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003313 if (DebugInfoKind == codegenoptions::LimitedDebugInfo && NeedFullDebug)
3314 DebugInfoKind = codegenoptions::FullDebugInfo;
3315
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003316 if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source,
3317 false)) {
Scott Lindera2fbcef2018-02-26 17:32:31 +00003318 // Source embedding is a vendor extension to DWARF v5. By now we have
3319 // checked if a DWARF version was stated explicitly, and have otherwise
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003320 // fallen back to the target default, so if this is still not at least 5
3321 // we emit an error.
3322 const Arg *A = Args.getLastArg(options::OPT_gembed_source);
Scott Lindera2fbcef2018-02-26 17:32:31 +00003323 if (DWARFVersion < 5)
3324 D.Diag(diag::err_drv_argument_only_allowed_with)
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003325 << A->getAsString(Args) << "-gdwarf-5";
3326 else if (checkDebugInfoOption(A, Args, D, TC))
3327 CmdArgs.push_back("-gembed-source");
Scott Lindera2fbcef2018-02-26 17:32:31 +00003328 }
3329
Reid Kleckner75557712018-11-16 18:47:41 +00003330 if (EmitCodeView) {
Reid Kleckner7b7b1142018-11-14 22:59:27 +00003331 CmdArgs.push_back("-gcodeview");
3332
Reid Kleckner75557712018-11-16 18:47:41 +00003333 // Emit codeview type hashes if requested.
3334 if (Args.hasFlag(options::OPT_gcodeview_ghash,
3335 options::OPT_gno_codeview_ghash, false)) {
3336 CmdArgs.push_back("-gcodeview-ghash");
3337 }
3338 }
3339
Alexey Bataevc92fc3c2018-12-12 14:52:27 +00003340 // Adjust the debug info kind for the given toolchain.
3341 TC.adjustDebugInfoKind(DebugInfoKind, Args);
3342
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003343 RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DWARFVersion,
3344 DebuggerTuning);
3345
3346 // -fdebug-macro turns on macro debug info generation.
3347 if (Args.hasFlag(options::OPT_fdebug_macro, options::OPT_fno_debug_macro,
3348 false))
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003349 if (checkDebugInfoOption(Args.getLastArg(options::OPT_fdebug_macro), Args,
3350 D, TC))
3351 CmdArgs.push_back("-debug-info-macro");
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003352
3353 // -ggnu-pubnames turns on gnu style pubnames in the backend.
David Blaikie65864522018-08-20 20:14:08 +00003354 const auto *PubnamesArg =
3355 Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames,
3356 options::OPT_gpubnames, options::OPT_gno_pubnames);
George Rimar91829ee2018-11-14 09:22:16 +00003357 if (DwarfFission != DwarfFissionKind::None ||
3358 DebuggerTuning == llvm::DebuggerKind::LLDB ||
David Blaikie65864522018-08-20 20:14:08 +00003359 (PubnamesArg && checkDebugInfoOption(PubnamesArg, Args, D, TC)))
3360 if (!PubnamesArg ||
3361 (!PubnamesArg->getOption().matches(options::OPT_gno_gnu_pubnames) &&
3362 !PubnamesArg->getOption().matches(options::OPT_gno_pubnames)))
3363 CmdArgs.push_back(PubnamesArg && PubnamesArg->getOption().matches(
3364 options::OPT_gpubnames)
3365 ? "-gpubnames"
3366 : "-ggnu-pubnames");
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003367
David Blaikie27692de2018-11-13 20:08:13 +00003368 if (Args.hasFlag(options::OPT_fdebug_ranges_base_address,
3369 options::OPT_fno_debug_ranges_base_address, false)) {
3370 CmdArgs.push_back("-fdebug-ranges-base-address");
3371 }
3372
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003373 // -gdwarf-aranges turns on the emission of the aranges section in the
3374 // backend.
Paul Robinsona8280812017-09-29 21:25:07 +00003375 // Always enabled for SCE tuning.
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003376 bool NeedAranges = DebuggerTuning == llvm::DebuggerKind::SCE;
3377 if (const Arg *A = Args.getLastArg(options::OPT_gdwarf_aranges))
3378 NeedAranges = checkDebugInfoOption(A, Args, D, TC) || NeedAranges;
3379 if (NeedAranges) {
Eli Friedman01d349b2018-04-12 22:21:36 +00003380 CmdArgs.push_back("-mllvm");
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003381 CmdArgs.push_back("-generate-arange-section");
3382 }
3383
3384 if (Args.hasFlag(options::OPT_fdebug_types_section,
3385 options::OPT_fno_debug_types_section, false)) {
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003386 if (!T.isOSBinFormatELF()) {
Jonas Devlieghere488bd012018-07-23 17:50:15 +00003387 D.Diag(diag::err_drv_unsupported_opt_for_target)
3388 << Args.getLastArg(options::OPT_fdebug_types_section)
3389 ->getAsString(Args)
3390 << T.getTriple();
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003391 } else if (checkDebugInfoOption(
3392 Args.getLastArg(options::OPT_fdebug_types_section), Args, D,
3393 TC)) {
3394 CmdArgs.push_back("-mllvm");
3395 CmdArgs.push_back("-generate-type-units");
3396 }
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003397 }
3398
Paul Robinson1787f812017-09-28 18:37:02 +00003399 // Decide how to render forward declarations of template instantiations.
3400 // SCE wants full descriptions, others just get them in the name.
3401 if (DebuggerTuning == llvm::DebuggerKind::SCE)
3402 CmdArgs.push_back("-debug-forward-template-params");
3403
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003404 // Do we need to explicitly import anonymous namespaces into the parent
3405 // scope?
Paul Robinsona8280812017-09-29 21:25:07 +00003406 if (DebuggerTuning == llvm::DebuggerKind::SCE)
3407 CmdArgs.push_back("-dwarf-explicit-import");
3408
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003409 RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC);
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003410}
3411
David L. Jonesf561aba2017-03-08 01:02:16 +00003412void Clang::ConstructJob(Compilation &C, const JobAction &JA,
3413 const InputInfo &Output, const InputInfoList &Inputs,
3414 const ArgList &Args, const char *LinkingOutput) const {
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003415 const auto &TC = getToolChain();
3416 const llvm::Triple &RawTriple = TC.getTriple();
3417 const llvm::Triple &Triple = TC.getEffectiveTriple();
David L. Jonesf561aba2017-03-08 01:02:16 +00003418 const std::string &TripleStr = Triple.getTriple();
3419
3420 bool KernelOrKext =
3421 Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003422 const Driver &D = TC.getDriver();
David L. Jonesf561aba2017-03-08 01:02:16 +00003423 ArgStringList CmdArgs;
3424
3425 // Check number of inputs for sanity. We need at least one input.
3426 assert(Inputs.size() >= 1 && "Must have at least one input.");
Yaxun Liu398612b2018-05-08 21:02:12 +00003427 // CUDA/HIP compilation may have multiple inputs (source file + results of
David L. Jonesf561aba2017-03-08 01:02:16 +00003428 // device-side compilations). OpenMP device jobs also take the host IR as a
Richard Smithcd35eff2018-09-15 01:21:16 +00003429 // second input. Module precompilation accepts a list of header files to
3430 // include as part of the module. All other jobs are expected to have exactly
3431 // one input.
David L. Jonesf561aba2017-03-08 01:02:16 +00003432 bool IsCuda = JA.isOffloading(Action::OFK_Cuda);
Yaxun Liu398612b2018-05-08 21:02:12 +00003433 bool IsHIP = JA.isOffloading(Action::OFK_HIP);
David L. Jonesf561aba2017-03-08 01:02:16 +00003434 bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP);
Richard Smithcd35eff2018-09-15 01:21:16 +00003435 bool IsHeaderModulePrecompile = isa<HeaderModulePrecompileJobAction>(JA);
3436
3437 // A header module compilation doesn't have a main input file, so invent a
3438 // fake one as a placeholder.
Richard Smithcd35eff2018-09-15 01:21:16 +00003439 const char *ModuleName = [&]{
3440 auto *ModuleNameArg = Args.getLastArg(options::OPT_fmodule_name_EQ);
3441 return ModuleNameArg ? ModuleNameArg->getValue() : "";
3442 }();
Benjamin Kramer5904c412018-11-05 12:46:02 +00003443 InputInfo HeaderModuleInput(Inputs[0].getType(), ModuleName, ModuleName);
Richard Smithcd35eff2018-09-15 01:21:16 +00003444
3445 const InputInfo &Input =
3446 IsHeaderModulePrecompile ? HeaderModuleInput : Inputs[0];
3447
3448 InputInfoList ModuleHeaderInputs;
3449 const InputInfo *CudaDeviceInput = nullptr;
3450 const InputInfo *OpenMPDeviceInput = nullptr;
3451 for (const InputInfo &I : Inputs) {
3452 if (&I == &Input) {
3453 // This is the primary input.
Benjamin Kramer5904c412018-11-05 12:46:02 +00003454 } else if (IsHeaderModulePrecompile &&
Richard Smithcd35eff2018-09-15 01:21:16 +00003455 types::getPrecompiledType(I.getType()) == types::TY_PCH) {
Benjamin Kramer5904c412018-11-05 12:46:02 +00003456 types::ID Expected = HeaderModuleInput.getType();
Richard Smithcd35eff2018-09-15 01:21:16 +00003457 if (I.getType() != Expected) {
3458 D.Diag(diag::err_drv_module_header_wrong_kind)
3459 << I.getFilename() << types::getTypeName(I.getType())
3460 << types::getTypeName(Expected);
3461 }
3462 ModuleHeaderInputs.push_back(I);
3463 } else if ((IsCuda || IsHIP) && !CudaDeviceInput) {
3464 CudaDeviceInput = &I;
3465 } else if (IsOpenMPDevice && !OpenMPDeviceInput) {
3466 OpenMPDeviceInput = &I;
3467 } else {
3468 llvm_unreachable("unexpectedly given multiple inputs");
3469 }
3470 }
David L. Jonesf561aba2017-03-08 01:02:16 +00003471
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003472 const llvm::Triple *AuxTriple = IsCuda ? TC.getAuxTriple() : nullptr;
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003473 bool IsWindowsMSVC = RawTriple.isWindowsMSVCEnvironment();
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003474 bool IsIAMCU = RawTriple.isOSIAMCU();
David L. Jonesf561aba2017-03-08 01:02:16 +00003475
Yaxun Liu398612b2018-05-08 21:02:12 +00003476 // Adjust IsWindowsXYZ for CUDA/HIP compilations. Even when compiling in
3477 // device mode (i.e., getToolchain().getTriple() is NVPTX/AMDGCN, not
3478 // Windows), we need to pass Windows-specific flags to cc1.
Fangrui Songe6e09562019-07-12 13:21:58 +00003479 if (IsCuda || IsHIP)
David L. Jonesf561aba2017-03-08 01:02:16 +00003480 IsWindowsMSVC |= AuxTriple && AuxTriple->isWindowsMSVCEnvironment();
David L. Jonesf561aba2017-03-08 01:02:16 +00003481
3482 // C++ is not supported for IAMCU.
3483 if (IsIAMCU && types::isCXX(Input.getType()))
3484 D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU";
3485
3486 // Invoke ourselves in -cc1 mode.
3487 //
3488 // FIXME: Implement custom jobs for internal actions.
3489 CmdArgs.push_back("-cc1");
3490
3491 // Add the "effective" target triple.
3492 CmdArgs.push_back("-triple");
3493 CmdArgs.push_back(Args.MakeArgString(TripleStr));
3494
3495 if (const Arg *MJ = Args.getLastArg(options::OPT_MJ)) {
3496 DumpCompilationDatabase(C, MJ->getValue(), TripleStr, Output, Input, Args);
3497 Args.ClaimAllArgs(options::OPT_MJ);
3498 }
3499
Yaxun Liu398612b2018-05-08 21:02:12 +00003500 if (IsCuda || IsHIP) {
3501 // We have to pass the triple of the host if compiling for a CUDA/HIP device
3502 // and vice-versa.
David L. Jonesf561aba2017-03-08 01:02:16 +00003503 std::string NormalizedTriple;
Yaxun Liu398612b2018-05-08 21:02:12 +00003504 if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
3505 JA.isDeviceOffloading(Action::OFK_HIP))
David L. Jonesf561aba2017-03-08 01:02:16 +00003506 NormalizedTriple = C.getSingleOffloadToolChain<Action::OFK_Host>()
3507 ->getTriple()
3508 .normalize();
Artem Belevich8fa28a02019-01-31 21:32:24 +00003509 else {
3510 // Host-side compilation.
Yaxun Liu398612b2018-05-08 21:02:12 +00003511 NormalizedTriple =
3512 (IsCuda ? C.getSingleOffloadToolChain<Action::OFK_Cuda>()
3513 : C.getSingleOffloadToolChain<Action::OFK_HIP>())
3514 ->getTriple()
3515 .normalize();
Artem Belevich8fa28a02019-01-31 21:32:24 +00003516 if (IsCuda) {
3517 // We need to figure out which CUDA version we're compiling for, as that
3518 // determines how we load and launch GPU kernels.
3519 auto *CTC = static_cast<const toolchains::CudaToolChain *>(
3520 C.getSingleOffloadToolChain<Action::OFK_Cuda>());
3521 assert(CTC && "Expected valid CUDA Toolchain.");
3522 if (CTC && CTC->CudaInstallation.version() != CudaVersion::UNKNOWN)
3523 CmdArgs.push_back(Args.MakeArgString(
3524 Twine("-target-sdk-version=") +
3525 CudaVersionToString(CTC->CudaInstallation.version())));
3526 }
3527 }
David L. Jonesf561aba2017-03-08 01:02:16 +00003528 CmdArgs.push_back("-aux-triple");
3529 CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
3530 }
3531
Gheorghe-Teodor Bercea59d7b772017-06-29 15:49:03 +00003532 if (IsOpenMPDevice) {
3533 // We have to pass the triple of the host if compiling for an OpenMP device.
3534 std::string NormalizedTriple =
3535 C.getSingleOffloadToolChain<Action::OFK_Host>()
3536 ->getTriple()
3537 .normalize();
3538 CmdArgs.push_back("-aux-triple");
3539 CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
3540 }
3541
David L. Jonesf561aba2017-03-08 01:02:16 +00003542 if (Triple.isOSWindows() && (Triple.getArch() == llvm::Triple::arm ||
3543 Triple.getArch() == llvm::Triple::thumb)) {
3544 unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 4 : 6;
3545 unsigned Version;
3546 Triple.getArchName().substr(Offset).getAsInteger(10, Version);
3547 if (Version < 7)
3548 D.Diag(diag::err_target_unsupported_arch) << Triple.getArchName()
3549 << TripleStr;
3550 }
3551
3552 // Push all default warning arguments that are specific to
3553 // the given target. These come before user provided warning options
3554 // are provided.
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003555 TC.addClangWarningOptions(CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00003556
3557 // Select the appropriate action.
3558 RewriteKind rewriteKind = RK_None;
3559
Nico Weberb28ffd82019-07-27 01:13:00 +00003560 // If CollectArgsForIntegratedAssembler() isn't called below, claim the args
3561 // it claims when not running an assembler. Otherwise, clang would emit
3562 // "argument unused" warnings for assembler flags when e.g. adding "-E" to
3563 // flags while debugging something. That'd be somewhat inconvenient, and it's
3564 // also inconsistent with most other flags -- we don't warn on
3565 // -ffunction-sections not being used in -E mode either for example, even
3566 // though it's not really used either.
3567 if (!isa<AssembleJobAction>(JA)) {
3568 // The args claimed here should match the args used in
3569 // CollectArgsForIntegratedAssembler().
3570 if (TC.useIntegratedAs()) {
3571 Args.ClaimAllArgs(options::OPT_mrelax_all);
3572 Args.ClaimAllArgs(options::OPT_mno_relax_all);
3573 Args.ClaimAllArgs(options::OPT_mincremental_linker_compatible);
3574 Args.ClaimAllArgs(options::OPT_mno_incremental_linker_compatible);
3575 switch (C.getDefaultToolChain().getArch()) {
3576 case llvm::Triple::arm:
3577 case llvm::Triple::armeb:
3578 case llvm::Triple::thumb:
3579 case llvm::Triple::thumbeb:
3580 Args.ClaimAllArgs(options::OPT_mimplicit_it_EQ);
Bjorn Pettersson60c1ee22019-07-27 17:09:08 +00003581 break;
Nico Weberb28ffd82019-07-27 01:13:00 +00003582 default:
3583 break;
3584 }
3585 }
3586 Args.ClaimAllArgs(options::OPT_Wa_COMMA);
3587 Args.ClaimAllArgs(options::OPT_Xassembler);
3588 }
3589
David L. Jonesf561aba2017-03-08 01:02:16 +00003590 if (isa<AnalyzeJobAction>(JA)) {
3591 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
3592 CmdArgs.push_back("-analyze");
3593 } else if (isa<MigrateJobAction>(JA)) {
3594 CmdArgs.push_back("-migrate");
3595 } else if (isa<PreprocessJobAction>(JA)) {
3596 if (Output.getType() == types::TY_Dependencies)
3597 CmdArgs.push_back("-Eonly");
3598 else {
3599 CmdArgs.push_back("-E");
3600 if (Args.hasArg(options::OPT_rewrite_objc) &&
3601 !Args.hasArg(options::OPT_g_Group))
3602 CmdArgs.push_back("-P");
3603 }
3604 } else if (isa<AssembleJobAction>(JA)) {
3605 CmdArgs.push_back("-emit-obj");
3606
3607 CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
3608
3609 // Also ignore explicit -force_cpusubtype_ALL option.
3610 (void)Args.hasArg(options::OPT_force__cpusubtype__ALL);
3611 } else if (isa<PrecompileJobAction>(JA)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00003612 if (JA.getType() == types::TY_Nothing)
3613 CmdArgs.push_back("-fsyntax-only");
3614 else if (JA.getType() == types::TY_ModuleFile)
Richard Smithcd35eff2018-09-15 01:21:16 +00003615 CmdArgs.push_back(IsHeaderModulePrecompile
3616 ? "-emit-header-module"
3617 : "-emit-module-interface");
David L. Jonesf561aba2017-03-08 01:02:16 +00003618 else
Erich Keane0a6b5b62018-12-04 14:34:09 +00003619 CmdArgs.push_back("-emit-pch");
David L. Jonesf561aba2017-03-08 01:02:16 +00003620 } else if (isa<VerifyPCHJobAction>(JA)) {
3621 CmdArgs.push_back("-verify-pch");
3622 } else {
3623 assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) &&
3624 "Invalid action for clang tool.");
3625 if (JA.getType() == types::TY_Nothing) {
3626 CmdArgs.push_back("-fsyntax-only");
3627 } else if (JA.getType() == types::TY_LLVM_IR ||
3628 JA.getType() == types::TY_LTO_IR) {
3629 CmdArgs.push_back("-emit-llvm");
3630 } else if (JA.getType() == types::TY_LLVM_BC ||
3631 JA.getType() == types::TY_LTO_BC) {
3632 CmdArgs.push_back("-emit-llvm-bc");
Puyan Lotfi68f29da2019-06-20 16:59:48 +00003633 } else if (JA.getType() == types::TY_IFS) {
Puyan Lotfi926f4f72019-08-22 23:44:34 +00003634 StringRef ArgStr =
3635 Args.hasArg(options::OPT_iterface_stub_version_EQ)
3636 ? Args.getLastArgValue(options::OPT_iterface_stub_version_EQ)
3637 : "";
Puyan Lotfi68f29da2019-06-20 16:59:48 +00003638 StringRef StubFormat =
Puyan Lotfi926f4f72019-08-22 23:44:34 +00003639 llvm::StringSwitch<StringRef>(ArgStr)
Puyan Lotfid2418452019-08-22 23:29:22 +00003640 .Case("experimental-ifs-v1", "experimental-ifs-v1")
Puyan Lotfi68f29da2019-06-20 16:59:48 +00003641 .Default("");
3642
Puyan Lotfi926f4f72019-08-22 23:44:34 +00003643 if (StubFormat.empty()) {
3644 std::string ErrorMessage =
3645 "Invalid interface stub format: " + ArgStr.str() +
3646 ((ArgStr == "experimental-yaml-elf-v1" ||
3647 ArgStr == "experimental-tapi-elf-v1")
3648 ? " is deprecated."
3649 : ".");
Puyan Lotfi68f29da2019-06-20 16:59:48 +00003650 D.Diag(diag::err_drv_invalid_value)
Puyan Lotfi926f4f72019-08-22 23:44:34 +00003651 << "Must specify a valid interface stub format type, ie: "
3652 "-interface-stub-version=experimental-ifs-v1"
3653 << ErrorMessage;
3654 }
Puyan Lotfi68f29da2019-06-20 16:59:48 +00003655
3656 CmdArgs.push_back("-emit-interface-stubs");
3657 CmdArgs.push_back(
3658 Args.MakeArgString(Twine("-interface-stub-version=") + StubFormat));
David L. Jonesf561aba2017-03-08 01:02:16 +00003659 } else if (JA.getType() == types::TY_PP_Asm) {
3660 CmdArgs.push_back("-S");
3661 } else if (JA.getType() == types::TY_AST) {
3662 CmdArgs.push_back("-emit-pch");
3663 } else if (JA.getType() == types::TY_ModuleFile) {
3664 CmdArgs.push_back("-module-file-info");
3665 } else if (JA.getType() == types::TY_RewrittenObjC) {
3666 CmdArgs.push_back("-rewrite-objc");
3667 rewriteKind = RK_NonFragile;
3668 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
3669 CmdArgs.push_back("-rewrite-objc");
3670 rewriteKind = RK_Fragile;
3671 } else {
3672 assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!");
3673 }
3674
3675 // Preserve use-list order by default when emitting bitcode, so that
3676 // loading the bitcode up in 'opt' or 'llc' and running passes gives the
3677 // same result as running passes here. For LTO, we don't need to preserve
3678 // the use-list order, since serialization to bitcode is part of the flow.
3679 if (JA.getType() == types::TY_LLVM_BC)
3680 CmdArgs.push_back("-emit-llvm-uselists");
3681
Artem Belevichecb178b2018-03-21 22:22:59 +00003682 // Device-side jobs do not support LTO.
3683 bool isDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) ||
3684 JA.isDeviceOffloading(Action::OFK_Host));
3685
3686 if (D.isUsingLTO() && !isDeviceOffloadAction) {
David L. Jonesf561aba2017-03-08 01:02:16 +00003687 Args.AddLastArg(CmdArgs, options::OPT_flto, options::OPT_flto_EQ);
3688
Paul Robinsond23f2a82017-07-13 21:25:47 +00003689 // The Darwin and PS4 linkers currently use the legacy LTO API, which
3690 // does not support LTO unit features (CFI, whole program vtable opt)
3691 // under ThinLTO.
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003692 if (!(RawTriple.isOSDarwin() || RawTriple.isPS4()) ||
David L. Jonesf561aba2017-03-08 01:02:16 +00003693 D.getLTOMode() == LTOK_Full)
3694 CmdArgs.push_back("-flto-unit");
3695 }
3696 }
3697
3698 if (const Arg *A = Args.getLastArg(options::OPT_fthinlto_index_EQ)) {
3699 if (!types::isLLVMIR(Input.getType()))
Bob Haarman79434642019-07-15 20:51:44 +00003700 D.Diag(diag::err_drv_arg_requires_bitcode_input) << A->getAsString(Args);
David L. Jonesf561aba2017-03-08 01:02:16 +00003701 Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ);
3702 }
3703
Teresa Johnson6e5cec22018-04-17 20:21:53 +00003704 if (Args.getLastArg(options::OPT_save_temps_EQ))
Teresa Johnson9e4321c2018-04-17 16:39:25 +00003705 Args.AddLastArg(CmdArgs, options::OPT_save_temps_EQ);
3706
David L. Jonesf561aba2017-03-08 01:02:16 +00003707 // Embed-bitcode option.
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00003708 // Only white-listed flags below are allowed to be embedded.
David L. Jonesf561aba2017-03-08 01:02:16 +00003709 if (C.getDriver().embedBitcodeInObject() && !C.getDriver().isUsingLTO() &&
3710 (isa<BackendJobAction>(JA) || isa<AssembleJobAction>(JA))) {
3711 // Add flags implied by -fembed-bitcode.
3712 Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ);
3713 // Disable all llvm IR level optimizations.
3714 CmdArgs.push_back("-disable-llvm-passes");
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00003715
Fangrui Song2632ebb2019-05-30 02:30:04 +00003716 // Render target options such as -fuse-init-array on modern ELF platforms.
3717 TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind());
3718
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00003719 // reject options that shouldn't be supported in bitcode
3720 // also reject kernel/kext
3721 static const constexpr unsigned kBitcodeOptionBlacklist[] = {
3722 options::OPT_mkernel,
3723 options::OPT_fapple_kext,
3724 options::OPT_ffunction_sections,
3725 options::OPT_fno_function_sections,
3726 options::OPT_fdata_sections,
3727 options::OPT_fno_data_sections,
3728 options::OPT_funique_section_names,
3729 options::OPT_fno_unique_section_names,
3730 options::OPT_mrestrict_it,
3731 options::OPT_mno_restrict_it,
3732 options::OPT_mstackrealign,
3733 options::OPT_mno_stackrealign,
3734 options::OPT_mstack_alignment,
3735 options::OPT_mcmodel_EQ,
3736 options::OPT_mlong_calls,
3737 options::OPT_mno_long_calls,
3738 options::OPT_ggnu_pubnames,
3739 options::OPT_gdwarf_aranges,
3740 options::OPT_fdebug_types_section,
3741 options::OPT_fno_debug_types_section,
3742 options::OPT_fdwarf_directory_asm,
3743 options::OPT_fno_dwarf_directory_asm,
3744 options::OPT_mrelax_all,
3745 options::OPT_mno_relax_all,
3746 options::OPT_ftrap_function_EQ,
3747 options::OPT_ffixed_r9,
3748 options::OPT_mfix_cortex_a53_835769,
3749 options::OPT_mno_fix_cortex_a53_835769,
3750 options::OPT_ffixed_x18,
3751 options::OPT_mglobal_merge,
3752 options::OPT_mno_global_merge,
3753 options::OPT_mred_zone,
3754 options::OPT_mno_red_zone,
3755 options::OPT_Wa_COMMA,
3756 options::OPT_Xassembler,
3757 options::OPT_mllvm,
3758 };
3759 for (const auto &A : Args)
Fangrui Song75e74e02019-03-31 08:48:19 +00003760 if (llvm::find(kBitcodeOptionBlacklist, A->getOption().getID()) !=
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00003761 std::end(kBitcodeOptionBlacklist))
3762 D.Diag(diag::err_drv_unsupported_embed_bitcode) << A->getSpelling();
3763
3764 // Render the CodeGen options that need to be passed.
3765 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
3766 options::OPT_fno_optimize_sibling_calls))
3767 CmdArgs.push_back("-mdisable-tail-calls");
3768
3769 RenderFloatingPointOptions(TC, D, isOptimizationLevelFast(Args), Args,
3770 CmdArgs);
3771
3772 // Render ABI arguments
3773 switch (TC.getArch()) {
3774 default: break;
3775 case llvm::Triple::arm:
3776 case llvm::Triple::armeb:
3777 case llvm::Triple::thumbeb:
3778 RenderARMABI(Triple, Args, CmdArgs);
3779 break;
3780 case llvm::Triple::aarch64:
3781 case llvm::Triple::aarch64_be:
3782 RenderAArch64ABI(Triple, Args, CmdArgs);
3783 break;
3784 }
3785
3786 // Optimization level for CodeGen.
3787 if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) {
3788 if (A->getOption().matches(options::OPT_O4)) {
3789 CmdArgs.push_back("-O3");
3790 D.Diag(diag::warn_O4_is_O3);
3791 } else {
3792 A->render(Args, CmdArgs);
3793 }
3794 }
3795
3796 // Input/Output file.
3797 if (Output.getType() == types::TY_Dependencies) {
3798 // Handled with other dependency code.
3799 } else if (Output.isFilename()) {
3800 CmdArgs.push_back("-o");
3801 CmdArgs.push_back(Output.getFilename());
3802 } else {
3803 assert(Output.isNothing() && "Input output.");
3804 }
3805
3806 for (const auto &II : Inputs) {
3807 addDashXForInput(Args, II, CmdArgs);
3808 if (II.isFilename())
Martin Storsjob547ef22018-10-26 08:33:29 +00003809 CmdArgs.push_back(II.getFilename());
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00003810 else
3811 II.getInputArg().renderAsInput(Args, CmdArgs);
3812 }
3813
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00003814 C.addCommand(std::make_unique<Command>(JA, *this, D.getClangProgramPath(),
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00003815 CmdArgs, Inputs));
3816 return;
David L. Jonesf561aba2017-03-08 01:02:16 +00003817 }
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00003818
David L. Jonesf561aba2017-03-08 01:02:16 +00003819 if (C.getDriver().embedBitcodeMarkerOnly() && !C.getDriver().isUsingLTO())
3820 CmdArgs.push_back("-fembed-bitcode=marker");
3821
3822 // We normally speed up the clang process a bit by skipping destructors at
3823 // exit, but when we're generating diagnostics we can rely on some of the
3824 // cleanup.
3825 if (!C.isForDiagnostics())
3826 CmdArgs.push_back("-disable-free");
3827
David L. Jonesf561aba2017-03-08 01:02:16 +00003828#ifdef NDEBUG
Eric Fiselier123c7492018-02-07 18:36:51 +00003829 const bool IsAssertBuild = false;
3830#else
3831 const bool IsAssertBuild = true;
David L. Jonesf561aba2017-03-08 01:02:16 +00003832#endif
3833
Eric Fiselier123c7492018-02-07 18:36:51 +00003834 // Disable the verification pass in -asserts builds.
3835 if (!IsAssertBuild)
Eric Fiseliercca7ddd2018-02-07 19:17:03 +00003836 CmdArgs.push_back("-disable-llvm-verifier");
Eric Fiselier123c7492018-02-07 18:36:51 +00003837
3838 // Discard value names in assert builds unless otherwise specified.
Eric Fiseliera06ca4b2018-02-14 20:56:52 +00003839 if (Args.hasFlag(options::OPT_fdiscard_value_names,
3840 options::OPT_fno_discard_value_names, !IsAssertBuild))
Eric Fiselier123c7492018-02-07 18:36:51 +00003841 CmdArgs.push_back("-discard-value-names");
3842
David L. Jonesf561aba2017-03-08 01:02:16 +00003843 // Set the main file name, so that debug info works even with
3844 // -save-temps.
3845 CmdArgs.push_back("-main-file-name");
3846 CmdArgs.push_back(getBaseInputName(Args, Input));
3847
3848 // Some flags which affect the language (via preprocessor
3849 // defines).
3850 if (Args.hasArg(options::OPT_static))
3851 CmdArgs.push_back("-static-define");
3852
Martin Storsjo434ef832018-08-06 19:48:44 +00003853 if (Args.hasArg(options::OPT_municode))
3854 CmdArgs.push_back("-DUNICODE");
3855
Saleem Abdulrasool24aafa52017-08-30 14:18:08 +00003856 if (isa<AnalyzeJobAction>(JA))
3857 RenderAnalyzerOptions(Args, CmdArgs, Triple, Input);
David L. Jonesf561aba2017-03-08 01:02:16 +00003858
George Karpenkov6d45b1f2018-12-21 00:26:19 +00003859 // Enable compatilibily mode to avoid analyzer-config related errors.
3860 // Since we can't access frontend flags through hasArg, let's manually iterate
3861 // through them.
Artem Dergachev0ec95c82018-12-21 01:11:21 +00003862 bool FoundAnalyzerConfig = false;
George Karpenkov6d45b1f2018-12-21 00:26:19 +00003863 for (auto Arg : Args.filtered(options::OPT_Xclang))
Artem Dergachev0ec95c82018-12-21 01:11:21 +00003864 if (StringRef(Arg->getValue()) == "-analyzer-config") {
3865 FoundAnalyzerConfig = true;
3866 break;
3867 }
3868 if (!FoundAnalyzerConfig)
3869 for (auto Arg : Args.filtered(options::OPT_Xanalyzer))
3870 if (StringRef(Arg->getValue()) == "-analyzer-config") {
3871 FoundAnalyzerConfig = true;
3872 break;
3873 }
3874 if (FoundAnalyzerConfig)
3875 CmdArgs.push_back("-analyzer-config-compatibility-mode=true");
George Karpenkov6d45b1f2018-12-21 00:26:19 +00003876
David L. Jonesf561aba2017-03-08 01:02:16 +00003877 CheckCodeGenerationOptions(D, Args);
3878
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003879 unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args);
Saleem Abdulrasool3fe5b7a2018-04-19 23:14:57 +00003880 assert(FunctionAlignment <= 31 && "function alignment will be truncated!");
3881 if (FunctionAlignment) {
3882 CmdArgs.push_back("-function-alignment");
3883 CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment)));
3884 }
3885
David L. Jonesf561aba2017-03-08 01:02:16 +00003886 llvm::Reloc::Model RelocationModel;
3887 unsigned PICLevel;
3888 bool IsPIE;
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003889 std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(TC, Args);
David L. Jonesf561aba2017-03-08 01:02:16 +00003890
3891 const char *RMName = RelocationModelName(RelocationModel);
3892
3893 if ((RelocationModel == llvm::Reloc::ROPI ||
3894 RelocationModel == llvm::Reloc::ROPI_RWPI) &&
3895 types::isCXX(Input.getType()) &&
3896 !Args.hasArg(options::OPT_fallow_unsupported))
3897 D.Diag(diag::err_drv_ropi_incompatible_with_cxx);
3898
3899 if (RMName) {
3900 CmdArgs.push_back("-mrelocation-model");
3901 CmdArgs.push_back(RMName);
3902 }
3903 if (PICLevel > 0) {
3904 CmdArgs.push_back("-pic-level");
3905 CmdArgs.push_back(PICLevel == 1 ? "1" : "2");
3906 if (IsPIE)
3907 CmdArgs.push_back("-pic-is-pie");
3908 }
3909
Oliver Stannarde3c8ce82019-02-18 12:39:47 +00003910 if (RelocationModel == llvm::Reloc::ROPI ||
3911 RelocationModel == llvm::Reloc::ROPI_RWPI)
3912 CmdArgs.push_back("-fropi");
3913 if (RelocationModel == llvm::Reloc::RWPI ||
3914 RelocationModel == llvm::Reloc::ROPI_RWPI)
3915 CmdArgs.push_back("-frwpi");
3916
David L. Jonesf561aba2017-03-08 01:02:16 +00003917 if (Arg *A = Args.getLastArg(options::OPT_meabi)) {
3918 CmdArgs.push_back("-meabi");
3919 CmdArgs.push_back(A->getValue());
3920 }
3921
3922 CmdArgs.push_back("-mthread-model");
Jonathan Roelofs6fbb9e02017-09-07 22:01:25 +00003923 if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) {
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003924 if (!TC.isThreadModelSupported(A->getValue()))
Jonathan Roelofs6fbb9e02017-09-07 22:01:25 +00003925 D.Diag(diag::err_drv_invalid_thread_model_for_target)
3926 << A->getValue() << A->getAsString(Args);
David L. Jonesf561aba2017-03-08 01:02:16 +00003927 CmdArgs.push_back(A->getValue());
Jonathan Roelofs6fbb9e02017-09-07 22:01:25 +00003928 }
David L. Jonesf561aba2017-03-08 01:02:16 +00003929 else
Thomas Livelyf3b4f992019-02-28 18:39:08 +00003930 CmdArgs.push_back(Args.MakeArgString(TC.getThreadModel()));
David L. Jonesf561aba2017-03-08 01:02:16 +00003931
3932 Args.AddLastArg(CmdArgs, options::OPT_fveclib);
3933
Manoj Gupta4b3eefa2018-04-05 15:29:52 +00003934 if (Args.hasFlag(options::OPT_fmerge_all_constants,
3935 options::OPT_fno_merge_all_constants, false))
3936 CmdArgs.push_back("-fmerge-all-constants");
David L. Jonesf561aba2017-03-08 01:02:16 +00003937
Manoj Guptada08f6a2018-07-19 00:44:52 +00003938 if (Args.hasFlag(options::OPT_fno_delete_null_pointer_checks,
3939 options::OPT_fdelete_null_pointer_checks, false))
3940 CmdArgs.push_back("-fno-delete-null-pointer-checks");
3941
David L. Jonesf561aba2017-03-08 01:02:16 +00003942 // LLVM Code Generator Options.
3943
3944 if (Args.hasArg(options::OPT_frewrite_map_file) ||
3945 Args.hasArg(options::OPT_frewrite_map_file_EQ)) {
3946 for (const Arg *A : Args.filtered(options::OPT_frewrite_map_file,
3947 options::OPT_frewrite_map_file_EQ)) {
3948 StringRef Map = A->getValue();
3949 if (!llvm::sys::fs::exists(Map)) {
3950 D.Diag(diag::err_drv_no_such_file) << Map;
3951 } else {
3952 CmdArgs.push_back("-frewrite-map-file");
3953 CmdArgs.push_back(A->getValue());
3954 A->claim();
3955 }
3956 }
3957 }
3958
3959 if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) {
3960 StringRef v = A->getValue();
3961 CmdArgs.push_back("-mllvm");
3962 CmdArgs.push_back(Args.MakeArgString("-warn-stack-size=" + v));
3963 A->claim();
3964 }
3965
3966 if (!Args.hasFlag(options::OPT_fjump_tables, options::OPT_fno_jump_tables,
3967 true))
3968 CmdArgs.push_back("-fno-jump-tables");
3969
Dehao Chen5e97f232017-08-24 21:37:33 +00003970 if (Args.hasFlag(options::OPT_fprofile_sample_accurate,
3971 options::OPT_fno_profile_sample_accurate, false))
3972 CmdArgs.push_back("-fprofile-sample-accurate");
3973
David L. Jonesf561aba2017-03-08 01:02:16 +00003974 if (!Args.hasFlag(options::OPT_fpreserve_as_comments,
3975 options::OPT_fno_preserve_as_comments, true))
3976 CmdArgs.push_back("-fno-preserve-as-comments");
3977
3978 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
3979 CmdArgs.push_back("-mregparm");
3980 CmdArgs.push_back(A->getValue());
3981 }
3982
3983 if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return,
3984 options::OPT_freg_struct_return)) {
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003985 if (TC.getArch() != llvm::Triple::x86) {
David L. Jonesf561aba2017-03-08 01:02:16 +00003986 D.Diag(diag::err_drv_unsupported_opt_for_target)
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003987 << A->getSpelling() << RawTriple.str();
David L. Jonesf561aba2017-03-08 01:02:16 +00003988 } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) {
3989 CmdArgs.push_back("-fpcc-struct-return");
3990 } else {
3991 assert(A->getOption().matches(options::OPT_freg_struct_return));
3992 CmdArgs.push_back("-freg-struct-return");
3993 }
3994 }
3995
3996 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
3997 CmdArgs.push_back("-fdefault-calling-conv=stdcall");
3998
Yuanfang Chenff22ec32019-07-20 22:50:50 +00003999 CodeGenOptions::FramePointerKind FPKeepKind =
4000 getFramePointerKind(Args, RawTriple);
4001 const char *FPKeepKindStr = nullptr;
4002 switch (FPKeepKind) {
4003 case CodeGenOptions::FramePointerKind::None:
4004 FPKeepKindStr = "-mframe-pointer=none";
4005 break;
4006 case CodeGenOptions::FramePointerKind::NonLeaf:
4007 FPKeepKindStr = "-mframe-pointer=non-leaf";
4008 break;
4009 case CodeGenOptions::FramePointerKind::All:
4010 FPKeepKindStr = "-mframe-pointer=all";
4011 break;
Fangrui Songdc039662019-07-12 02:01:51 +00004012 }
Yuanfang Chenff22ec32019-07-20 22:50:50 +00004013 assert(FPKeepKindStr && "unknown FramePointerKind");
4014 CmdArgs.push_back(FPKeepKindStr);
4015
David L. Jonesf561aba2017-03-08 01:02:16 +00004016 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
4017 options::OPT_fno_zero_initialized_in_bss))
4018 CmdArgs.push_back("-mno-zero-initialized-in-bss");
4019
4020 bool OFastEnabled = isOptimizationLevelFast(Args);
4021 // If -Ofast is the optimization level, then -fstrict-aliasing should be
4022 // enabled. This alias option is being used to simplify the hasFlag logic.
4023 OptSpecifier StrictAliasingAliasOption =
4024 OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing;
4025 // We turn strict aliasing off by default if we're in CL mode, since MSVC
4026 // doesn't do any TBAA.
Saleem Abdulrasool33d41382017-08-29 23:59:06 +00004027 bool TBAAOnByDefault = !D.IsCLMode();
David L. Jonesf561aba2017-03-08 01:02:16 +00004028 if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
4029 options::OPT_fno_strict_aliasing, TBAAOnByDefault))
4030 CmdArgs.push_back("-relaxed-aliasing");
4031 if (!Args.hasFlag(options::OPT_fstruct_path_tbaa,
4032 options::OPT_fno_struct_path_tbaa))
4033 CmdArgs.push_back("-no-struct-path-tbaa");
4034 if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
4035 false))
4036 CmdArgs.push_back("-fstrict-enums");
4037 if (!Args.hasFlag(options::OPT_fstrict_return, options::OPT_fno_strict_return,
4038 true))
4039 CmdArgs.push_back("-fno-strict-return");
Alex Lorenz1be800c52017-04-19 08:58:56 +00004040 if (Args.hasFlag(options::OPT_fallow_editor_placeholders,
4041 options::OPT_fno_allow_editor_placeholders, false))
4042 CmdArgs.push_back("-fallow-editor-placeholders");
David L. Jonesf561aba2017-03-08 01:02:16 +00004043 if (Args.hasFlag(options::OPT_fstrict_vtable_pointers,
4044 options::OPT_fno_strict_vtable_pointers,
4045 false))
4046 CmdArgs.push_back("-fstrict-vtable-pointers");
Piotr Padlewskie368de32018-06-13 13:55:42 +00004047 if (Args.hasFlag(options::OPT_fforce_emit_vtables,
4048 options::OPT_fno_force_emit_vtables,
4049 false))
4050 CmdArgs.push_back("-fforce-emit-vtables");
David L. Jonesf561aba2017-03-08 01:02:16 +00004051 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
4052 options::OPT_fno_optimize_sibling_calls))
4053 CmdArgs.push_back("-mdisable-tail-calls");
Akira Hatanaka627586b2018-03-02 01:53:15 +00004054 if (Args.hasFlag(options::OPT_fno_escaping_block_tail_calls,
Akira Hatanaka9f9d7662018-03-10 05:55:21 +00004055 options::OPT_fescaping_block_tail_calls, false))
Akira Hatanaka627586b2018-03-02 01:53:15 +00004056 CmdArgs.push_back("-fno-escaping-block-tail-calls");
David L. Jonesf561aba2017-03-08 01:02:16 +00004057
Wei Mi9b3d6272017-10-16 16:50:27 +00004058 Args.AddLastArg(CmdArgs, options::OPT_ffine_grained_bitfield_accesses,
4059 options::OPT_fno_fine_grained_bitfield_accesses);
4060
David L. Jonesf561aba2017-03-08 01:02:16 +00004061 // Handle segmented stacks.
4062 if (Args.hasArg(options::OPT_fsplit_stack))
4063 CmdArgs.push_back("-split-stacks");
4064
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004065 RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004066
Troy A. Johnsonc0d70bc2019-08-17 04:20:24 +00004067 if (Arg *A = Args.getLastArg(options::OPT_LongDouble_Group)) {
Fangrui Song11cb39c2019-07-09 00:27:43 +00004068 if (TC.getArch() == llvm::Triple::x86 ||
Troy A. Johnsonc0d70bc2019-08-17 04:20:24 +00004069 TC.getArch() == llvm::Triple::x86_64)
4070 A->render(Args, CmdArgs);
4071 else if ((TC.getArch() == llvm::Triple::ppc || TC.getTriple().isPPC64()) &&
4072 (A->getOption().getID() != options::OPT_mlong_double_80))
Fangrui Songc46d78d2019-07-12 02:32:15 +00004073 A->render(Args, CmdArgs);
4074 else
Fangrui Song11cb39c2019-07-09 00:27:43 +00004075 D.Diag(diag::err_drv_unsupported_opt_for_target)
4076 << A->getAsString(Args) << TripleStr;
Fangrui Song11cb39c2019-07-09 00:27:43 +00004077 }
4078
David L. Jonesf561aba2017-03-08 01:02:16 +00004079 // Decide whether to use verbose asm. Verbose assembly is the default on
4080 // toolchains which have the integrated assembler on by default.
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004081 bool IsIntegratedAssemblerDefault = TC.IsIntegratedAssemblerDefault();
David L. Jonesf561aba2017-03-08 01:02:16 +00004082 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Erich Keanee30b71f2019-08-26 17:00:13 +00004083 IsIntegratedAssemblerDefault))
David L. Jonesf561aba2017-03-08 01:02:16 +00004084 CmdArgs.push_back("-masm-verbose");
4085
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004086 if (!TC.useIntegratedAs())
David L. Jonesf561aba2017-03-08 01:02:16 +00004087 CmdArgs.push_back("-no-integrated-as");
4088
4089 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
4090 CmdArgs.push_back("-mdebug-pass");
4091 CmdArgs.push_back("Structure");
4092 }
4093 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
4094 CmdArgs.push_back("-mdebug-pass");
4095 CmdArgs.push_back("Arguments");
4096 }
4097
4098 // Enable -mconstructor-aliases except on darwin, where we have to work around
4099 // a linker bug (see <rdar://problem/7651567>), and CUDA device code, where
4100 // aliases aren't supported.
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00004101 if (!RawTriple.isOSDarwin() && !RawTriple.isNVPTX())
David L. Jonesf561aba2017-03-08 01:02:16 +00004102 CmdArgs.push_back("-mconstructor-aliases");
4103
4104 // Darwin's kernel doesn't support guard variables; just die if we
4105 // try to use them.
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00004106 if (KernelOrKext && RawTriple.isOSDarwin())
David L. Jonesf561aba2017-03-08 01:02:16 +00004107 CmdArgs.push_back("-fforbid-guard-variables");
4108
4109 if (Args.hasFlag(options::OPT_mms_bitfields, options::OPT_mno_ms_bitfields,
4110 false)) {
4111 CmdArgs.push_back("-mms-bitfields");
4112 }
4113
4114 if (Args.hasFlag(options::OPT_mpie_copy_relocations,
4115 options::OPT_mno_pie_copy_relocations,
4116 false)) {
4117 CmdArgs.push_back("-mpie-copy-relocations");
4118 }
4119
Sriraman Tallam5c651482017-11-07 19:37:51 +00004120 if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) {
4121 CmdArgs.push_back("-fno-plt");
4122 }
4123
Vedant Kumardf502592017-09-12 22:51:53 +00004124 // -fhosted is default.
4125 // TODO: Audit uses of KernelOrKext and see where it'd be more appropriate to
4126 // use Freestanding.
4127 bool Freestanding =
4128 Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
4129 KernelOrKext;
4130 if (Freestanding)
4131 CmdArgs.push_back("-ffreestanding");
4132
David L. Jonesf561aba2017-03-08 01:02:16 +00004133 // This is a coarse approximation of what llvm-gcc actually does, both
4134 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
4135 // complicated ways.
4136 bool AsynchronousUnwindTables =
4137 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
4138 options::OPT_fno_asynchronous_unwind_tables,
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004139 (TC.IsUnwindTablesDefault(Args) ||
4140 TC.getSanitizerArgs().needsUnwindTables()) &&
Vedant Kumardf502592017-09-12 22:51:53 +00004141 !Freestanding);
David L. Jonesf561aba2017-03-08 01:02:16 +00004142 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
4143 AsynchronousUnwindTables))
4144 CmdArgs.push_back("-munwind-tables");
4145
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004146 TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind());
David L. Jonesf561aba2017-03-08 01:02:16 +00004147
David L. Jonesf561aba2017-03-08 01:02:16 +00004148 // FIXME: Handle -mtune=.
4149 (void)Args.hasArg(options::OPT_mtune_EQ);
4150
4151 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
4152 CmdArgs.push_back("-mcode-model");
4153 CmdArgs.push_back(A->getValue());
4154 }
4155
4156 // Add the target cpu
4157 std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false);
4158 if (!CPU.empty()) {
4159 CmdArgs.push_back("-target-cpu");
4160 CmdArgs.push_back(Args.MakeArgString(CPU));
4161 }
4162
Saleem Abdulrasool6c3ed7b2017-09-03 04:47:00 +00004163 RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004164
David L. Jonesf561aba2017-03-08 01:02:16 +00004165 // These two are potentially updated by AddClangCLArgs.
4166 codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo;
4167 bool EmitCodeView = false;
4168
4169 // Add clang-cl arguments.
4170 types::ID InputType = Input.getType();
Saleem Abdulrasool33d41382017-08-29 23:59:06 +00004171 if (D.IsCLMode())
David L. Jonesf561aba2017-03-08 01:02:16 +00004172 AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView);
4173
George Rimar91829ee2018-11-14 09:22:16 +00004174 DwarfFissionKind DwarfFission;
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004175 RenderDebugOptions(TC, D, RawTriple, Args, EmitCodeView, IsWindowsMSVC,
George Rimar91829ee2018-11-14 09:22:16 +00004176 CmdArgs, DebugInfoKind, DwarfFission);
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00004177
4178 // Add the split debug info name to the command lines here so we
4179 // can propagate it to the backend.
George Rimar91829ee2018-11-14 09:22:16 +00004180 bool SplitDWARF = (DwarfFission != DwarfFissionKind::None) &&
Fangrui Songee957e02019-03-28 08:24:00 +00004181 TC.getTriple().isOSBinFormatELF() &&
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00004182 (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) ||
4183 isa<BackendJobAction>(JA));
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00004184 if (SplitDWARF) {
Aaron Pucherte1dc4952019-06-15 15:38:51 +00004185 const char *SplitDWARFOut = SplitDebugName(Args, Input, Output);
4186 CmdArgs.push_back("-split-dwarf-file");
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00004187 CmdArgs.push_back(SplitDWARFOut);
Aaron Pucherte1dc4952019-06-15 15:38:51 +00004188 if (DwarfFission == DwarfFissionKind::Split) {
4189 CmdArgs.push_back("-split-dwarf-output");
4190 CmdArgs.push_back(SplitDWARFOut);
4191 }
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00004192 }
4193
David L. Jonesf561aba2017-03-08 01:02:16 +00004194 // Pass the linker version in use.
4195 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
4196 CmdArgs.push_back("-target-linker-version");
4197 CmdArgs.push_back(A->getValue());
4198 }
4199
David L. Jonesf561aba2017-03-08 01:02:16 +00004200 // Explicitly error on some things we know we don't support and can't just
4201 // ignore.
4202 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
4203 Arg *Unsupported;
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00004204 if (types::isCXX(InputType) && RawTriple.isOSDarwin() &&
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004205 TC.getArch() == llvm::Triple::x86) {
David L. Jonesf561aba2017-03-08 01:02:16 +00004206 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
4207 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
4208 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
4209 << Unsupported->getOption().getName();
4210 }
Eric Christopher758aad72017-03-21 22:06:18 +00004211 // The faltivec option has been superseded by the maltivec option.
4212 if ((Unsupported = Args.getLastArg(options::OPT_faltivec)))
4213 D.Diag(diag::err_drv_clang_unsupported_opt_faltivec)
4214 << Unsupported->getOption().getName()
4215 << "please use -maltivec and include altivec.h explicitly";
4216 if ((Unsupported = Args.getLastArg(options::OPT_fno_altivec)))
4217 D.Diag(diag::err_drv_clang_unsupported_opt_faltivec)
4218 << Unsupported->getOption().getName() << "please use -mno-altivec";
David L. Jonesf561aba2017-03-08 01:02:16 +00004219 }
4220
4221 Args.AddAllArgs(CmdArgs, options::OPT_v);
4222 Args.AddLastArg(CmdArgs, options::OPT_H);
4223 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
4224 CmdArgs.push_back("-header-include-file");
4225 CmdArgs.push_back(D.CCPrintHeadersFilename ? D.CCPrintHeadersFilename
4226 : "-");
4227 }
4228 Args.AddLastArg(CmdArgs, options::OPT_P);
4229 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
4230
4231 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
4232 CmdArgs.push_back("-diagnostic-log-file");
4233 CmdArgs.push_back(D.CCLogDiagnosticsFilename ? D.CCLogDiagnosticsFilename
4234 : "-");
4235 }
4236
David L. Jonesf561aba2017-03-08 01:02:16 +00004237 bool UseSeparateSections = isUseSeparateSections(Triple);
4238
4239 if (Args.hasFlag(options::OPT_ffunction_sections,
4240 options::OPT_fno_function_sections, UseSeparateSections)) {
4241 CmdArgs.push_back("-ffunction-sections");
4242 }
4243
4244 if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections,
4245 UseSeparateSections)) {
4246 CmdArgs.push_back("-fdata-sections");
4247 }
4248
4249 if (!Args.hasFlag(options::OPT_funique_section_names,
4250 options::OPT_fno_unique_section_names, true))
4251 CmdArgs.push_back("-fno-unique-section-names");
4252
Nico Weber908b6972019-06-26 17:51:47 +00004253 Args.AddLastArg(CmdArgs, options::OPT_finstrument_functions,
4254 options::OPT_finstrument_functions_after_inlining,
4255 options::OPT_finstrument_function_entry_bare);
David L. Jonesf561aba2017-03-08 01:02:16 +00004256
Artem Belevichc30bcad2018-01-24 17:41:02 +00004257 // NVPTX doesn't support PGO or coverage. There's no runtime support for
4258 // sampling, overhead of call arc collection is way too high and there's no
4259 // way to collect the output.
4260 if (!Triple.isNVPTX())
Russell Gallop7a9ccf82019-05-14 14:01:40 +00004261 addPGOAndCoverageFlags(TC, C, D, Output, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004262
Nico Weber908b6972019-06-26 17:51:47 +00004263 Args.AddLastArg(CmdArgs, options::OPT_fclang_abi_compat_EQ);
Richard Smithf667ad52017-08-26 01:04:35 +00004264
Pierre Gousseau1abf9432018-06-06 14:04:15 +00004265 // Add runtime flag for PS4 when PGO, coverage, or sanitizers are enabled.
Pierre Gousseau53b5cfb2018-12-18 17:03:35 +00004266 if (RawTriple.isPS4CPU() &&
4267 !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004268 PS4cpu::addProfileRTArgs(TC, Args, CmdArgs);
4269 PS4cpu::addSanitizerArgs(TC, CmdArgs);
Pierre Gousseau1abf9432018-06-06 14:04:15 +00004270 }
David L. Jonesf561aba2017-03-08 01:02:16 +00004271
4272 // Pass options for controlling the default header search paths.
4273 if (Args.hasArg(options::OPT_nostdinc)) {
4274 CmdArgs.push_back("-nostdsysteminc");
4275 CmdArgs.push_back("-nobuiltininc");
4276 } else {
4277 if (Args.hasArg(options::OPT_nostdlibinc))
4278 CmdArgs.push_back("-nostdsysteminc");
4279 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
4280 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
4281 }
4282
4283 // Pass the path to compiler resource files.
4284 CmdArgs.push_back("-resource-dir");
4285 CmdArgs.push_back(D.ResourceDir.c_str());
4286
4287 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
4288
Saleem Abdulrasool0a322c62017-08-31 15:35:01 +00004289 RenderARCMigrateToolOptions(D, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004290
4291 // Add preprocessing options like -I, -D, etc. if we are using the
4292 // preprocessor.
4293 //
4294 // FIXME: Support -fpreprocessed
4295 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
4296 AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
4297
4298 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
4299 // that "The compiler can only warn and ignore the option if not recognized".
4300 // When building with ccache, it will pass -D options to clang even on
4301 // preprocessed inputs and configure concludes that -fPIC is not supported.
4302 Args.ClaimAllArgs(options::OPT_D);
4303
4304 // Manually translate -O4 to -O3; let clang reject others.
4305 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
4306 if (A->getOption().matches(options::OPT_O4)) {
4307 CmdArgs.push_back("-O3");
4308 D.Diag(diag::warn_O4_is_O3);
4309 } else {
4310 A->render(Args, CmdArgs);
4311 }
4312 }
4313
4314 // Warn about ignored options to clang.
4315 for (const Arg *A :
4316 Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) {
4317 D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args);
4318 A->claim();
4319 }
4320
Joerg Sonnenbergerc9199682017-07-01 21:36:21 +00004321 for (const Arg *A :
4322 Args.filtered(options::OPT_clang_ignored_legacy_options_Group)) {
4323 D.Diag(diag::warn_ignored_clang_option) << A->getAsString(Args);
4324 A->claim();
4325 }
4326
David L. Jonesf561aba2017-03-08 01:02:16 +00004327 claimNoWarnArgs(Args);
4328
4329 Args.AddAllArgs(CmdArgs, options::OPT_R_Group);
4330
4331 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
4332 if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
4333 CmdArgs.push_back("-pedantic");
4334 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
4335 Args.AddLastArg(CmdArgs, options::OPT_w);
4336
Leonard Chanf921d852018-06-04 16:07:52 +00004337 // Fixed point flags
4338 if (Args.hasFlag(options::OPT_ffixed_point, options::OPT_fno_fixed_point,
4339 /*Default=*/false))
4340 Args.AddLastArg(CmdArgs, options::OPT_ffixed_point);
4341
David L. Jonesf561aba2017-03-08 01:02:16 +00004342 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
4343 // (-ansi is equivalent to -std=c89 or -std=c++98).
4344 //
4345 // If a std is supplied, only add -trigraphs if it follows the
4346 // option.
4347 bool ImplyVCPPCXXVer = false;
Richard Smithb1b580e2019-04-14 11:11:37 +00004348 const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi);
4349 if (Std) {
David L. Jonesf561aba2017-03-08 01:02:16 +00004350 if (Std->getOption().matches(options::OPT_ansi))
4351 if (types::isCXX(InputType))
4352 CmdArgs.push_back("-std=c++98");
4353 else
4354 CmdArgs.push_back("-std=c89");
4355 else
4356 Std->render(Args, CmdArgs);
4357
4358 // If -f(no-)trigraphs appears after the language standard flag, honor it.
4359 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
4360 options::OPT_ftrigraphs,
4361 options::OPT_fno_trigraphs))
4362 if (A != Std)
4363 A->render(Args, CmdArgs);
4364 } else {
4365 // Honor -std-default.
4366 //
4367 // FIXME: Clang doesn't correctly handle -std= when the input language
4368 // doesn't match. For the time being just ignore this for C++ inputs;
4369 // eventually we want to do all the standard defaulting here instead of
4370 // splitting it between the driver and clang -cc1.
4371 if (!types::isCXX(InputType))
4372 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=",
4373 /*Joined=*/true);
4374 else if (IsWindowsMSVC)
4375 ImplyVCPPCXXVer = true;
4376
4377 Args.AddLastArg(CmdArgs, options::OPT_ftrigraphs,
4378 options::OPT_fno_trigraphs);
4379 }
4380
4381 // GCC's behavior for -Wwrite-strings is a bit strange:
4382 // * In C, this "warning flag" changes the types of string literals from
4383 // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning
4384 // for the discarded qualifier.
4385 // * In C++, this is just a normal warning flag.
4386 //
4387 // Implementing this warning correctly in C is hard, so we follow GCC's
4388 // behavior for now. FIXME: Directly diagnose uses of a string literal as
4389 // a non-const char* in C, rather than using this crude hack.
4390 if (!types::isCXX(InputType)) {
4391 // FIXME: This should behave just like a warning flag, and thus should also
4392 // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on.
4393 Arg *WriteStrings =
4394 Args.getLastArg(options::OPT_Wwrite_strings,
4395 options::OPT_Wno_write_strings, options::OPT_w);
4396 if (WriteStrings &&
4397 WriteStrings->getOption().matches(options::OPT_Wwrite_strings))
4398 CmdArgs.push_back("-fconst-strings");
4399 }
4400
4401 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
4402 // during C++ compilation, which it is by default. GCC keeps this define even
4403 // in the presence of '-w', match this behavior bug-for-bug.
4404 if (types::isCXX(InputType) &&
4405 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
4406 true)) {
4407 CmdArgs.push_back("-fdeprecated-macro");
4408 }
4409
4410 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
4411 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
4412 if (Asm->getOption().matches(options::OPT_fasm))
4413 CmdArgs.push_back("-fgnu-keywords");
4414 else
4415 CmdArgs.push_back("-fno-gnu-keywords");
4416 }
4417
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004418 if (ShouldDisableDwarfDirectory(Args, TC))
David L. Jonesf561aba2017-03-08 01:02:16 +00004419 CmdArgs.push_back("-fno-dwarf-directory-asm");
4420
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004421 if (ShouldDisableAutolink(Args, TC))
David L. Jonesf561aba2017-03-08 01:02:16 +00004422 CmdArgs.push_back("-fno-autolink");
4423
4424 // Add in -fdebug-compilation-dir if necessary.
Michael J. Spencer7e48b402019-05-28 22:21:47 +00004425 addDebugCompDirArg(Args, CmdArgs, D.getVFS());
David L. Jonesf561aba2017-03-08 01:02:16 +00004426
Paul Robinson9b292b42018-07-10 15:15:24 +00004427 addDebugPrefixMapArg(D, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004428
4429 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
4430 options::OPT_ftemplate_depth_EQ)) {
4431 CmdArgs.push_back("-ftemplate-depth");
4432 CmdArgs.push_back(A->getValue());
4433 }
4434
4435 if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) {
4436 CmdArgs.push_back("-foperator-arrow-depth");
4437 CmdArgs.push_back(A->getValue());
4438 }
4439
4440 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
4441 CmdArgs.push_back("-fconstexpr-depth");
4442 CmdArgs.push_back(A->getValue());
4443 }
4444
4445 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) {
4446 CmdArgs.push_back("-fconstexpr-steps");
4447 CmdArgs.push_back(A->getValue());
4448 }
4449
4450 if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
4451 CmdArgs.push_back("-fbracket-depth");
4452 CmdArgs.push_back(A->getValue());
4453 }
4454
4455 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
4456 options::OPT_Wlarge_by_value_copy_def)) {
4457 if (A->getNumValues()) {
4458 StringRef bytes = A->getValue();
4459 CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
4460 } else
4461 CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
4462 }
4463
4464 if (Args.hasArg(options::OPT_relocatable_pch))
4465 CmdArgs.push_back("-relocatable-pch");
4466
Saleem Abdulrasool81a650e2018-10-24 23:28:28 +00004467 if (const Arg *A = Args.getLastArg(options::OPT_fcf_runtime_abi_EQ)) {
4468 static const char *kCFABIs[] = {
4469 "standalone", "objc", "swift", "swift-5.0", "swift-4.2", "swift-4.1",
4470 };
4471
4472 if (find(kCFABIs, StringRef(A->getValue())) == std::end(kCFABIs))
4473 D.Diag(diag::err_drv_invalid_cf_runtime_abi) << A->getValue();
4474 else
4475 A->render(Args, CmdArgs);
4476 }
4477
David L. Jonesf561aba2017-03-08 01:02:16 +00004478 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
4479 CmdArgs.push_back("-fconstant-string-class");
4480 CmdArgs.push_back(A->getValue());
4481 }
4482
4483 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
4484 CmdArgs.push_back("-ftabstop");
4485 CmdArgs.push_back(A->getValue());
4486 }
4487
Sean Eveson5110d4f2018-01-08 13:42:26 +00004488 if (Args.hasFlag(options::OPT_fstack_size_section,
4489 options::OPT_fno_stack_size_section, RawTriple.isPS4()))
4490 CmdArgs.push_back("-fstack-size-section");
4491
David L. Jonesf561aba2017-03-08 01:02:16 +00004492 CmdArgs.push_back("-ferror-limit");
4493 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
4494 CmdArgs.push_back(A->getValue());
4495 else
4496 CmdArgs.push_back("19");
4497
4498 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
4499 CmdArgs.push_back("-fmacro-backtrace-limit");
4500 CmdArgs.push_back(A->getValue());
4501 }
4502
4503 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
4504 CmdArgs.push_back("-ftemplate-backtrace-limit");
4505 CmdArgs.push_back(A->getValue());
4506 }
4507
4508 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
4509 CmdArgs.push_back("-fconstexpr-backtrace-limit");
4510 CmdArgs.push_back(A->getValue());
4511 }
4512
4513 if (Arg *A = Args.getLastArg(options::OPT_fspell_checking_limit_EQ)) {
4514 CmdArgs.push_back("-fspell-checking-limit");
4515 CmdArgs.push_back(A->getValue());
4516 }
4517
4518 // Pass -fmessage-length=.
4519 CmdArgs.push_back("-fmessage-length");
4520 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
4521 CmdArgs.push_back(A->getValue());
4522 } else {
4523 // If -fmessage-length=N was not specified, determine whether this is a
4524 // terminal and, if so, implicitly define -fmessage-length appropriately.
4525 unsigned N = llvm::sys::Process::StandardErrColumns();
4526 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
4527 }
4528
4529 // -fvisibility= and -fvisibility-ms-compat are of a piece.
4530 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
4531 options::OPT_fvisibility_ms_compat)) {
4532 if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
4533 CmdArgs.push_back("-fvisibility");
4534 CmdArgs.push_back(A->getValue());
4535 } else {
4536 assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
4537 CmdArgs.push_back("-fvisibility");
4538 CmdArgs.push_back("hidden");
4539 CmdArgs.push_back("-ftype-visibility");
4540 CmdArgs.push_back("default");
4541 }
4542 }
4543
4544 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Petr Hosek821b38f2018-12-04 03:25:25 +00004545 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden);
David L. Jonesf561aba2017-03-08 01:02:16 +00004546
4547 Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
4548
David L. Jonesf561aba2017-03-08 01:02:16 +00004549 // Forward -f (flag) options which we can pass directly.
4550 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
4551 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Jacob Bandes-Storch33f3e632018-07-17 04:56:22 +00004552 Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004553 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
Chih-Hung Hsiehca552b82018-03-01 22:26:19 +00004554 Args.AddLastArg(CmdArgs, options::OPT_femulated_tls,
4555 options::OPT_fno_emulated_tls);
Elizabeth Andrews6593df22018-08-22 19:05:19 +00004556 Args.AddLastArg(CmdArgs, options::OPT_fkeep_static_consts);
Chih-Hung Hsiehca552b82018-03-01 22:26:19 +00004557
David L. Jonesf561aba2017-03-08 01:02:16 +00004558 // AltiVec-like language extensions aren't relevant for assembling.
Eric Christopher758aad72017-03-21 22:06:18 +00004559 if (!isa<PreprocessJobAction>(JA) || Output.getType() != types::TY_PP_Asm)
David L. Jonesf561aba2017-03-08 01:02:16 +00004560 Args.AddLastArg(CmdArgs, options::OPT_fzvector);
Eric Christopher758aad72017-03-21 22:06:18 +00004561
David L. Jonesf561aba2017-03-08 01:02:16 +00004562 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
4563 Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
4564
4565 // Forward flags for OpenMP. We don't do this if the current action is an
4566 // device offloading action other than OpenMP.
4567 if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
4568 options::OPT_fno_openmp, false) &&
4569 (JA.isDeviceOffloading(Action::OFK_None) ||
4570 JA.isDeviceOffloading(Action::OFK_OpenMP))) {
Saleem Abdulrasool33d41382017-08-29 23:59:06 +00004571 switch (D.getOpenMPRuntime(Args)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00004572 case Driver::OMPRT_OMP:
4573 case Driver::OMPRT_IOMP5:
4574 // Clang can generate useful OpenMP code for these two runtime libraries.
4575 CmdArgs.push_back("-fopenmp");
4576
4577 // If no option regarding the use of TLS in OpenMP codegeneration is
4578 // given, decide a default based on the target. Otherwise rely on the
4579 // options and pass the right information to the frontend.
4580 if (!Args.hasFlag(options::OPT_fopenmp_use_tls,
4581 options::OPT_fnoopenmp_use_tls, /*Default=*/true))
4582 CmdArgs.push_back("-fnoopenmp-use-tls");
Alexey Bataev66f95772018-05-21 16:40:32 +00004583 Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd,
4584 options::OPT_fno_openmp_simd);
David L. Jonesf561aba2017-03-08 01:02:16 +00004585 Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ);
Alexey Bataeve4090182018-11-02 14:54:07 +00004586 Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_number_of_sm_EQ);
4587 Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_blocks_per_sm_EQ);
Alexey Bataev8061acd2019-02-20 16:36:22 +00004588 Args.AddAllArgs(CmdArgs,
4589 options::OPT_fopenmp_cuda_teams_reduction_recs_num_EQ);
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00004590 if (Args.hasFlag(options::OPT_fopenmp_optimistic_collapse,
4591 options::OPT_fno_openmp_optimistic_collapse,
4592 /*Default=*/false))
4593 CmdArgs.push_back("-fopenmp-optimistic-collapse");
Carlo Bertolli79712092018-02-28 20:48:35 +00004594
4595 // When in OpenMP offloading mode with NVPTX target, forward
4596 // cuda-mode flag
Alexey Bataev80a9a612018-08-30 14:45:24 +00004597 if (Args.hasFlag(options::OPT_fopenmp_cuda_mode,
4598 options::OPT_fno_openmp_cuda_mode, /*Default=*/false))
4599 CmdArgs.push_back("-fopenmp-cuda-mode");
4600
4601 // When in OpenMP offloading mode with NVPTX target, check if full runtime
4602 // is required.
4603 if (Args.hasFlag(options::OPT_fopenmp_cuda_force_full_runtime,
4604 options::OPT_fno_openmp_cuda_force_full_runtime,
4605 /*Default=*/false))
4606 CmdArgs.push_back("-fopenmp-cuda-force-full-runtime");
David L. Jonesf561aba2017-03-08 01:02:16 +00004607 break;
4608 default:
4609 // By default, if Clang doesn't know how to generate useful OpenMP code
4610 // for a specific runtime library, we just don't pass the '-fopenmp' flag
4611 // down to the actual compilation.
4612 // FIXME: It would be better to have a mode which *only* omits IR
4613 // generation based on the OpenMP support so that we get consistent
4614 // semantic analysis, etc.
4615 break;
4616 }
Alexey Bataeve927ca72017-12-29 17:36:15 +00004617 } else {
4618 Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd,
4619 options::OPT_fno_openmp_simd);
4620 Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ);
David L. Jonesf561aba2017-03-08 01:02:16 +00004621 }
4622
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004623 const SanitizerArgs &Sanitize = TC.getSanitizerArgs();
4624 Sanitize.addArgs(TC, Args, CmdArgs, InputType);
David L. Jonesf561aba2017-03-08 01:02:16 +00004625
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004626 const XRayArgs &XRay = TC.getXRayArgs();
4627 XRay.addArgs(TC, Args, CmdArgs, InputType);
Dean Michael Berris835832d2017-03-30 00:29:36 +00004628
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004629 if (TC.SupportsProfiling())
David L. Jonesf561aba2017-03-08 01:02:16 +00004630 Args.AddLastArg(CmdArgs, options::OPT_pg);
4631
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004632 if (TC.SupportsProfiling())
David L. Jonesf561aba2017-03-08 01:02:16 +00004633 Args.AddLastArg(CmdArgs, options::OPT_mfentry);
4634
4635 // -flax-vector-conversions is default.
4636 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
4637 options::OPT_fno_lax_vector_conversions))
4638 CmdArgs.push_back("-fno-lax-vector-conversions");
4639
4640 if (Args.getLastArg(options::OPT_fapple_kext) ||
4641 (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType)))
4642 CmdArgs.push_back("-fapple-kext");
4643
4644 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
4645 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
4646 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
4647 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
Anton Afanasyevd880de22019-03-30 08:42:48 +00004648 Args.AddLastArg(CmdArgs, options::OPT_ftime_trace);
Anton Afanasyev4fdcabf2019-07-24 14:55:40 +00004649 Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_granularity_EQ);
David L. Jonesf561aba2017-03-08 01:02:16 +00004650 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
Craig Topper3205dbb2019-03-21 20:07:24 +00004651 Args.AddLastArg(CmdArgs, options::OPT_malign_double);
David L. Jonesf561aba2017-03-08 01:02:16 +00004652
4653 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
4654 CmdArgs.push_back("-ftrapv-handler");
4655 CmdArgs.push_back(A->getValue());
4656 }
4657
4658 Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
4659
4660 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
4661 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
4662 if (Arg *A = Args.getLastArg(options::OPT_fwrapv, options::OPT_fno_wrapv)) {
4663 if (A->getOption().matches(options::OPT_fwrapv))
4664 CmdArgs.push_back("-fwrapv");
4665 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
4666 options::OPT_fno_strict_overflow)) {
4667 if (A->getOption().matches(options::OPT_fno_strict_overflow))
4668 CmdArgs.push_back("-fwrapv");
4669 }
4670
4671 if (Arg *A = Args.getLastArg(options::OPT_freroll_loops,
4672 options::OPT_fno_reroll_loops))
4673 if (A->getOption().matches(options::OPT_freroll_loops))
4674 CmdArgs.push_back("-freroll-loops");
4675
4676 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
4677 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops,
4678 options::OPT_fno_unroll_loops);
4679
4680 Args.AddLastArg(CmdArgs, options::OPT_pthread);
4681
Zola Bridgesc8666792018-11-26 18:13:31 +00004682 if (Args.hasFlag(options::OPT_mspeculative_load_hardening, options::OPT_mno_speculative_load_hardening,
4683 false))
4684 CmdArgs.push_back(Args.MakeArgString("-mspeculative-load-hardening"));
Chandler Carruth664aa862018-09-04 12:38:00 +00004685
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004686 RenderSSPOptions(TC, Args, CmdArgs, KernelOrKext);
JF Bastien14daa202018-12-18 05:12:21 +00004687 RenderTrivialAutoVarInitOptions(D, TC, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004688
4689 // Translate -mstackrealign
4690 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
4691 false))
4692 CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
4693
4694 if (Args.hasArg(options::OPT_mstack_alignment)) {
4695 StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
4696 CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
4697 }
4698
4699 if (Args.hasArg(options::OPT_mstack_probe_size)) {
4700 StringRef Size = Args.getLastArgValue(options::OPT_mstack_probe_size);
4701
4702 if (!Size.empty())
4703 CmdArgs.push_back(Args.MakeArgString("-mstack-probe-size=" + Size));
4704 else
4705 CmdArgs.push_back("-mstack-probe-size=0");
4706 }
4707
Hans Wennborgd43f40d2018-02-23 13:47:36 +00004708 if (!Args.hasFlag(options::OPT_mstack_arg_probe,
4709 options::OPT_mno_stack_arg_probe, true))
4710 CmdArgs.push_back(Args.MakeArgString("-mno-stack-arg-probe"));
4711
David L. Jonesf561aba2017-03-08 01:02:16 +00004712 if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it,
4713 options::OPT_mno_restrict_it)) {
4714 if (A->getOption().matches(options::OPT_mrestrict_it)) {
Eli Friedman01d349b2018-04-12 22:21:36 +00004715 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00004716 CmdArgs.push_back("-arm-restrict-it");
4717 } else {
Eli Friedman01d349b2018-04-12 22:21:36 +00004718 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00004719 CmdArgs.push_back("-arm-no-restrict-it");
4720 }
4721 } else if (Triple.isOSWindows() &&
4722 (Triple.getArch() == llvm::Triple::arm ||
4723 Triple.getArch() == llvm::Triple::thumb)) {
4724 // Windows on ARM expects restricted IT blocks
Eli Friedman01d349b2018-04-12 22:21:36 +00004725 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00004726 CmdArgs.push_back("-arm-restrict-it");
4727 }
4728
4729 // Forward -cl options to -cc1
Saleem Abdulrasool68c808f62017-08-29 23:59:07 +00004730 RenderOpenCLOptions(Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004731
Oren Ben Simhon57cc1a52018-01-09 08:53:59 +00004732 if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) {
4733 CmdArgs.push_back(
4734 Args.MakeArgString(Twine("-fcf-protection=") + A->getValue()));
4735 }
4736
David L. Jonesf561aba2017-03-08 01:02:16 +00004737 // Forward -f options with positive and negative forms; we translate
4738 // these by hand.
Dehao Chenea4b78f2017-03-21 21:40:53 +00004739 if (Arg *A = getLastProfileSampleUseArg(Args)) {
Rong Xua4a09b22019-03-04 20:21:31 +00004740 auto *PGOArg = Args.getLastArg(
4741 options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ,
4742 options::OPT_fcs_profile_generate, options::OPT_fcs_profile_generate_EQ,
4743 options::OPT_fprofile_use, options::OPT_fprofile_use_EQ);
4744 if (PGOArg)
4745 D.Diag(diag::err_drv_argument_not_allowed_with)
4746 << "SampleUse with PGO options";
4747
David L. Jonesf561aba2017-03-08 01:02:16 +00004748 StringRef fname = A->getValue();
4749 if (!llvm::sys::fs::exists(fname))
4750 D.Diag(diag::err_drv_no_such_file) << fname;
4751 else
4752 A->render(Args, CmdArgs);
4753 }
Richard Smith8654ae52018-10-10 23:13:35 +00004754 Args.AddLastArg(CmdArgs, options::OPT_fprofile_remapping_file_EQ);
David L. Jonesf561aba2017-03-08 01:02:16 +00004755
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004756 RenderBuiltinOptions(TC, RawTriple, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004757
4758 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
4759 options::OPT_fno_assume_sane_operator_new))
4760 CmdArgs.push_back("-fno-assume-sane-operator-new");
4761
4762 // -fblocks=0 is default.
4763 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004764 TC.IsBlocksDefault()) ||
David L. Jonesf561aba2017-03-08 01:02:16 +00004765 (Args.hasArg(options::OPT_fgnu_runtime) &&
4766 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
4767 !Args.hasArg(options::OPT_fno_blocks))) {
4768 CmdArgs.push_back("-fblocks");
4769
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004770 if (!Args.hasArg(options::OPT_fgnu_runtime) && !TC.hasBlocksRuntime())
David L. Jonesf561aba2017-03-08 01:02:16 +00004771 CmdArgs.push_back("-fblocks-runtime-optional");
4772 }
4773
Saleem Abdulrasoolb2d4ebb2017-09-01 17:43:59 +00004774 // -fencode-extended-block-signature=1 is default.
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004775 if (TC.IsEncodeExtendedBlockSignatureDefault())
Saleem Abdulrasoolb2d4ebb2017-09-01 17:43:59 +00004776 CmdArgs.push_back("-fencode-extended-block-signature");
4777
David L. Jonesf561aba2017-03-08 01:02:16 +00004778 if (Args.hasFlag(options::OPT_fcoroutines_ts, options::OPT_fno_coroutines_ts,
4779 false) &&
4780 types::isCXX(InputType)) {
4781 CmdArgs.push_back("-fcoroutines-ts");
4782 }
4783
Aaron Ballman61736552017-10-21 20:28:58 +00004784 Args.AddLastArg(CmdArgs, options::OPT_fdouble_square_bracket_attributes,
4785 options::OPT_fno_double_square_bracket_attributes);
4786
David L. Jonesf561aba2017-03-08 01:02:16 +00004787 // -faccess-control is default.
4788 if (Args.hasFlag(options::OPT_fno_access_control,
4789 options::OPT_faccess_control, false))
4790 CmdArgs.push_back("-fno-access-control");
4791
4792 // -felide-constructors is the default.
4793 if (Args.hasFlag(options::OPT_fno_elide_constructors,
4794 options::OPT_felide_constructors, false))
4795 CmdArgs.push_back("-fno-elide-constructors");
4796
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004797 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
David L. Jonesf561aba2017-03-08 01:02:16 +00004798
4799 if (KernelOrKext || (types::isCXX(InputType) &&
Sunil Srivastava2ada2492018-05-18 23:32:01 +00004800 (RTTIMode == ToolChain::RM_Disabled)))
David L. Jonesf561aba2017-03-08 01:02:16 +00004801 CmdArgs.push_back("-fno-rtti");
4802
4803 // -fshort-enums=0 is default for all architectures except Hexagon.
4804 if (Args.hasFlag(options::OPT_fshort_enums, options::OPT_fno_short_enums,
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004805 TC.getArch() == llvm::Triple::hexagon))
David L. Jonesf561aba2017-03-08 01:02:16 +00004806 CmdArgs.push_back("-fshort-enums");
4807
Saleem Abdulrasool729379a2017-10-06 23:09:55 +00004808 RenderCharacterOptions(Args, AuxTriple ? *AuxTriple : RawTriple, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004809
4810 // -fuse-cxa-atexit is default.
4811 if (!Args.hasFlag(
4812 options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
Saleem Abdulrasool015bded2017-09-11 20:18:09 +00004813 !RawTriple.isOSWindows() &&
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004814 TC.getArch() != llvm::Triple::xcore &&
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00004815 ((RawTriple.getVendor() != llvm::Triple::MipsTechnologies) ||
4816 RawTriple.hasEnvironment())) ||
David L. Jonesf561aba2017-03-08 01:02:16 +00004817 KernelOrKext)
4818 CmdArgs.push_back("-fno-use-cxa-atexit");
4819
Akira Hatanaka617e2612018-04-17 18:41:52 +00004820 if (Args.hasFlag(options::OPT_fregister_global_dtors_with_atexit,
4821 options::OPT_fno_register_global_dtors_with_atexit,
Akira Hatanaka18db58e2018-04-27 01:42:33 +00004822 RawTriple.isOSDarwin() && !KernelOrKext))
Akira Hatanaka617e2612018-04-17 18:41:52 +00004823 CmdArgs.push_back("-fregister-global-dtors-with-atexit");
4824
David L. Jonesf561aba2017-03-08 01:02:16 +00004825 // -fms-extensions=0 is default.
4826 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
4827 IsWindowsMSVC))
4828 CmdArgs.push_back("-fms-extensions");
4829
Martin Storsjof1f8f4a2018-05-09 09:11:01 +00004830 // -fno-use-line-directives is default.
David L. Jonesf561aba2017-03-08 01:02:16 +00004831 if (Args.hasFlag(options::OPT_fuse_line_directives,
Martin Storsjof1f8f4a2018-05-09 09:11:01 +00004832 options::OPT_fno_use_line_directives, false))
David L. Jonesf561aba2017-03-08 01:02:16 +00004833 CmdArgs.push_back("-fuse-line-directives");
4834
4835 // -fms-compatibility=0 is default.
4836 if (Args.hasFlag(options::OPT_fms_compatibility,
4837 options::OPT_fno_ms_compatibility,
4838 (IsWindowsMSVC &&
4839 Args.hasFlag(options::OPT_fms_extensions,
4840 options::OPT_fno_ms_extensions, true))))
4841 CmdArgs.push_back("-fms-compatibility");
4842
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004843 VersionTuple MSVT = TC.computeMSVCVersion(&D, Args);
David L. Jonesf561aba2017-03-08 01:02:16 +00004844 if (!MSVT.empty())
4845 CmdArgs.push_back(
4846 Args.MakeArgString("-fms-compatibility-version=" + MSVT.getAsString()));
4847
4848 bool IsMSVC2015Compatible = MSVT.getMajor() >= 19;
4849 if (ImplyVCPPCXXVer) {
4850 StringRef LanguageStandard;
4851 if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) {
Richard Smithb1b580e2019-04-14 11:11:37 +00004852 Std = StdArg;
David L. Jonesf561aba2017-03-08 01:02:16 +00004853 LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue())
4854 .Case("c++14", "-std=c++14")
Martell Malonef6f6a9c2017-10-15 17:27:58 +00004855 .Case("c++17", "-std=c++17")
4856 .Case("c++latest", "-std=c++2a")
David L. Jonesf561aba2017-03-08 01:02:16 +00004857 .Default("");
4858 if (LanguageStandard.empty())
4859 D.Diag(clang::diag::warn_drv_unused_argument)
4860 << StdArg->getAsString(Args);
4861 }
4862
4863 if (LanguageStandard.empty()) {
4864 if (IsMSVC2015Compatible)
4865 LanguageStandard = "-std=c++14";
4866 else
4867 LanguageStandard = "-std=c++11";
4868 }
4869
4870 CmdArgs.push_back(LanguageStandard.data());
4871 }
4872
4873 // -fno-borland-extensions is default.
4874 if (Args.hasFlag(options::OPT_fborland_extensions,
4875 options::OPT_fno_borland_extensions, false))
4876 CmdArgs.push_back("-fborland-extensions");
4877
4878 // -fno-declspec is default, except for PS4.
4879 if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec,
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00004880 RawTriple.isPS4()))
David L. Jonesf561aba2017-03-08 01:02:16 +00004881 CmdArgs.push_back("-fdeclspec");
4882 else if (Args.hasArg(options::OPT_fno_declspec))
4883 CmdArgs.push_back("-fno-declspec"); // Explicitly disabling __declspec.
4884
4885 // -fthreadsafe-static is default, except for MSVC compatibility versions less
4886 // than 19.
4887 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
4888 options::OPT_fno_threadsafe_statics,
4889 !IsWindowsMSVC || IsMSVC2015Compatible))
4890 CmdArgs.push_back("-fno-threadsafe-statics");
4891
Hans Wennborg82884532019-08-22 13:15:36 +00004892 // -fno-delayed-template-parsing is default, except when targeting MSVC.
4893 // Many old Windows SDK versions require this to parse.
4894 // FIXME: MSVC introduced /Zc:twoPhase- to disable this behavior in their
4895 // compiler. We should be able to disable this by default at some point.
David L. Jonesf561aba2017-03-08 01:02:16 +00004896 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
Hans Wennborg82884532019-08-22 13:15:36 +00004897 options::OPT_fno_delayed_template_parsing, IsWindowsMSVC))
David L. Jonesf561aba2017-03-08 01:02:16 +00004898 CmdArgs.push_back("-fdelayed-template-parsing");
4899
4900 // -fgnu-keywords default varies depending on language; only pass if
4901 // specified.
Nico Weber908b6972019-06-26 17:51:47 +00004902 Args.AddLastArg(CmdArgs, options::OPT_fgnu_keywords,
4903 options::OPT_fno_gnu_keywords);
David L. Jonesf561aba2017-03-08 01:02:16 +00004904
4905 if (Args.hasFlag(options::OPT_fgnu89_inline, options::OPT_fno_gnu89_inline,
4906 false))
4907 CmdArgs.push_back("-fgnu89-inline");
4908
4909 if (Args.hasArg(options::OPT_fno_inline))
4910 CmdArgs.push_back("-fno-inline");
4911
Nico Weber908b6972019-06-26 17:51:47 +00004912 Args.AddLastArg(CmdArgs, options::OPT_finline_functions,
4913 options::OPT_finline_hint_functions,
4914 options::OPT_fno_inline_functions);
David L. Jonesf561aba2017-03-08 01:02:16 +00004915
Richard Smithb1b580e2019-04-14 11:11:37 +00004916 // FIXME: Find a better way to determine whether the language has modules
4917 // support by default, or just assume that all languages do.
4918 bool HaveModules =
4919 Std && (Std->containsValue("c++2a") || Std->containsValue("c++latest"));
4920 RenderModulesOptions(C, D, Args, Input, Output, CmdArgs, HaveModules);
4921
David L. Jonesf561aba2017-03-08 01:02:16 +00004922 Args.AddLastArg(CmdArgs, options::OPT_fexperimental_new_pass_manager,
4923 options::OPT_fno_experimental_new_pass_manager);
4924
Saleem Abdulrasoolb2d4ebb2017-09-01 17:43:59 +00004925 ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004926 RenderObjCOptions(TC, D, RawTriple, Args, Runtime, rewriteKind != RK_None,
4927 Input, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004928
4929 if (Args.hasFlag(options::OPT_fapplication_extension,
4930 options::OPT_fno_application_extension, false))
4931 CmdArgs.push_back("-fapplication-extension");
4932
4933 // Handle GCC-style exception args.
4934 if (!C.getDriver().IsCLMode())
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004935 addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004936
Martell Malonec950c652017-11-29 07:25:12 +00004937 // Handle exception personalities
4938 Arg *A = Args.getLastArg(options::OPT_fsjlj_exceptions,
4939 options::OPT_fseh_exceptions,
4940 options::OPT_fdwarf_exceptions);
4941 if (A) {
4942 const Option &Opt = A->getOption();
4943 if (Opt.matches(options::OPT_fsjlj_exceptions))
4944 CmdArgs.push_back("-fsjlj-exceptions");
4945 if (Opt.matches(options::OPT_fseh_exceptions))
4946 CmdArgs.push_back("-fseh-exceptions");
4947 if (Opt.matches(options::OPT_fdwarf_exceptions))
4948 CmdArgs.push_back("-fdwarf-exceptions");
4949 } else {
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004950 switch (TC.GetExceptionModel(Args)) {
Reid Kleckner7383b8e2017-11-29 21:36:00 +00004951 default:
4952 break;
4953 case llvm::ExceptionHandling::DwarfCFI:
4954 CmdArgs.push_back("-fdwarf-exceptions");
4955 break;
4956 case llvm::ExceptionHandling::SjLj:
4957 CmdArgs.push_back("-fsjlj-exceptions");
4958 break;
4959 case llvm::ExceptionHandling::WinEH:
4960 CmdArgs.push_back("-fseh-exceptions");
4961 break;
Martell Malonec950c652017-11-29 07:25:12 +00004962 }
4963 }
David L. Jonesf561aba2017-03-08 01:02:16 +00004964
4965 // C++ "sane" operator new.
4966 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
4967 options::OPT_fno_assume_sane_operator_new))
4968 CmdArgs.push_back("-fno-assume-sane-operator-new");
4969
4970 // -frelaxed-template-template-args is off by default, as it is a severe
4971 // breaking change until a corresponding change to template partial ordering
4972 // is provided.
4973 if (Args.hasFlag(options::OPT_frelaxed_template_template_args,
4974 options::OPT_fno_relaxed_template_template_args, false))
4975 CmdArgs.push_back("-frelaxed-template-template-args");
4976
4977 // -fsized-deallocation is off by default, as it is an ABI-breaking change for
4978 // most platforms.
4979 if (Args.hasFlag(options::OPT_fsized_deallocation,
4980 options::OPT_fno_sized_deallocation, false))
4981 CmdArgs.push_back("-fsized-deallocation");
4982
4983 // -faligned-allocation is on by default in C++17 onwards and otherwise off
4984 // by default.
4985 if (Arg *A = Args.getLastArg(options::OPT_faligned_allocation,
4986 options::OPT_fno_aligned_allocation,
4987 options::OPT_faligned_new_EQ)) {
4988 if (A->getOption().matches(options::OPT_fno_aligned_allocation))
4989 CmdArgs.push_back("-fno-aligned-allocation");
4990 else
4991 CmdArgs.push_back("-faligned-allocation");
4992 }
4993
4994 // The default new alignment can be specified using a dedicated option or via
4995 // a GCC-compatible option that also turns on aligned allocation.
4996 if (Arg *A = Args.getLastArg(options::OPT_fnew_alignment_EQ,
4997 options::OPT_faligned_new_EQ))
4998 CmdArgs.push_back(
4999 Args.MakeArgString(Twine("-fnew-alignment=") + A->getValue()));
5000
5001 // -fconstant-cfstrings is default, and may be subject to argument translation
5002 // on Darwin.
5003 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
5004 options::OPT_fno_constant_cfstrings) ||
5005 !Args.hasFlag(options::OPT_mconstant_cfstrings,
5006 options::OPT_mno_constant_cfstrings))
5007 CmdArgs.push_back("-fno-constant-cfstrings");
5008
David L. Jonesf561aba2017-03-08 01:02:16 +00005009 // -fno-pascal-strings is default, only pass non-default.
5010 if (Args.hasFlag(options::OPT_fpascal_strings,
5011 options::OPT_fno_pascal_strings, false))
5012 CmdArgs.push_back("-fpascal-strings");
5013
5014 // Honor -fpack-struct= and -fpack-struct, if given. Note that
5015 // -fno-pack-struct doesn't apply to -fpack-struct=.
5016 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
5017 std::string PackStructStr = "-fpack-struct=";
5018 PackStructStr += A->getValue();
5019 CmdArgs.push_back(Args.MakeArgString(PackStructStr));
5020 } else if (Args.hasFlag(options::OPT_fpack_struct,
5021 options::OPT_fno_pack_struct, false)) {
5022 CmdArgs.push_back("-fpack-struct=1");
5023 }
5024
5025 // Handle -fmax-type-align=N and -fno-type-align
5026 bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align);
5027 if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) {
5028 if (!SkipMaxTypeAlign) {
5029 std::string MaxTypeAlignStr = "-fmax-type-align=";
5030 MaxTypeAlignStr += A->getValue();
5031 CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
5032 }
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00005033 } else if (RawTriple.isOSDarwin()) {
David L. Jonesf561aba2017-03-08 01:02:16 +00005034 if (!SkipMaxTypeAlign) {
5035 std::string MaxTypeAlignStr = "-fmax-type-align=16";
5036 CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
5037 }
5038 }
5039
Mikhail Maltsev4a4e7a32018-04-23 10:08:46 +00005040 if (!Args.hasFlag(options::OPT_Qy, options::OPT_Qn, true))
5041 CmdArgs.push_back("-Qn");
5042
David L. Jonesf561aba2017-03-08 01:02:16 +00005043 // -fcommon is the default unless compiling kernel code or the target says so
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00005044 bool NoCommonDefault = KernelOrKext || isNoCommonDefault(RawTriple);
David L. Jonesf561aba2017-03-08 01:02:16 +00005045 if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common,
5046 !NoCommonDefault))
5047 CmdArgs.push_back("-fno-common");
5048
5049 // -fsigned-bitfields is default, and clang doesn't yet support
5050 // -funsigned-bitfields.
5051 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
5052 options::OPT_funsigned_bitfields))
5053 D.Diag(diag::warn_drv_clang_unsupported)
5054 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
5055
5056 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
5057 if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope))
5058 D.Diag(diag::err_drv_clang_unsupported)
5059 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
5060
5061 // -finput_charset=UTF-8 is default. Reject others
5062 if (Arg *inputCharset = Args.getLastArg(options::OPT_finput_charset_EQ)) {
5063 StringRef value = inputCharset->getValue();
5064 if (!value.equals_lower("utf-8"))
5065 D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args)
5066 << value;
5067 }
5068
5069 // -fexec_charset=UTF-8 is default. Reject others
5070 if (Arg *execCharset = Args.getLastArg(options::OPT_fexec_charset_EQ)) {
5071 StringRef value = execCharset->getValue();
5072 if (!value.equals_lower("utf-8"))
5073 D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args)
5074 << value;
5075 }
5076
Saleem Abdulrasool75557fa2017-09-01 18:57:34 +00005077 RenderDiagnosticsOptions(D, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00005078
5079 // -fno-asm-blocks is default.
5080 if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
5081 false))
5082 CmdArgs.push_back("-fasm-blocks");
5083
5084 // -fgnu-inline-asm is default.
5085 if (!Args.hasFlag(options::OPT_fgnu_inline_asm,
5086 options::OPT_fno_gnu_inline_asm, true))
5087 CmdArgs.push_back("-fno-gnu-inline-asm");
5088
5089 // Enable vectorization per default according to the optimization level
5090 // selected. For optimization levels that want vectorization we use the alias
5091 // option to simplify the hasFlag logic.
5092 bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false);
5093 OptSpecifier VectorizeAliasOption =
5094 EnableVec ? options::OPT_O_Group : options::OPT_fvectorize;
5095 if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
5096 options::OPT_fno_vectorize, EnableVec))
5097 CmdArgs.push_back("-vectorize-loops");
5098
5099 // -fslp-vectorize is enabled based on the optimization level selected.
5100 bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true);
5101 OptSpecifier SLPVectAliasOption =
5102 EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize;
5103 if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption,
5104 options::OPT_fno_slp_vectorize, EnableSLPVec))
5105 CmdArgs.push_back("-vectorize-slp");
5106
Craig Topper9a724aa2017-12-11 21:09:19 +00005107 ParseMPreferVectorWidth(D, Args, CmdArgs);
5108
Nico Weber908b6972019-06-26 17:51:47 +00005109 Args.AddLastArg(CmdArgs, options::OPT_fshow_overloads_EQ);
5110 Args.AddLastArg(CmdArgs,
5111 options::OPT_fsanitize_undefined_strip_path_components_EQ);
David L. Jonesf561aba2017-03-08 01:02:16 +00005112
5113 // -fdollars-in-identifiers default varies depending on platform and
5114 // language; only pass if specified.
5115 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
5116 options::OPT_fno_dollars_in_identifiers)) {
5117 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
5118 CmdArgs.push_back("-fdollars-in-identifiers");
5119 else
5120 CmdArgs.push_back("-fno-dollars-in-identifiers");
5121 }
5122
5123 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
5124 // practical purposes.
5125 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
5126 options::OPT_fno_unit_at_a_time)) {
5127 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
5128 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
5129 }
5130
5131 if (Args.hasFlag(options::OPT_fapple_pragma_pack,
5132 options::OPT_fno_apple_pragma_pack, false))
5133 CmdArgs.push_back("-fapple-pragma-pack");
5134
Francis Visoiu Mistrihdd422362019-03-12 21:22:27 +00005135 // Remarks can be enabled with any of the `-f.*optimization-record.*` flags.
David L. Jonesf561aba2017-03-08 01:02:16 +00005136 if (Args.hasFlag(options::OPT_fsave_optimization_record,
Jonas Devliegherecf73eba2017-12-19 17:16:45 +00005137 options::OPT_foptimization_record_file_EQ,
Francis Visoiu Mistrihdd422362019-03-12 21:22:27 +00005138 options::OPT_fno_save_optimization_record, false) ||
Francis Visoiu Mistrih34667512019-06-17 16:06:00 +00005139 Args.hasFlag(options::OPT_fsave_optimization_record_EQ,
5140 options::OPT_fno_save_optimization_record, false) ||
Francis Visoiu Mistrihdd422362019-03-12 21:22:27 +00005141 Args.hasFlag(options::OPT_foptimization_record_passes_EQ,
David L. Jonesf561aba2017-03-08 01:02:16 +00005142 options::OPT_fno_save_optimization_record, false)) {
5143 CmdArgs.push_back("-opt-record-file");
5144
5145 const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ);
5146 if (A) {
5147 CmdArgs.push_back(A->getValue());
5148 } else {
5149 SmallString<128> F;
Hal Finkel67814df2017-08-16 21:34:27 +00005150
5151 if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) {
5152 if (Arg *FinalOutput = Args.getLastArg(options::OPT_o))
5153 F = FinalOutput->getValue();
5154 }
5155
5156 if (F.empty()) {
David L. Jonesf561aba2017-03-08 01:02:16 +00005157 // Use the input filename.
5158 F = llvm::sys::path::stem(Input.getBaseInput());
5159
5160 // If we're compiling for an offload architecture (i.e. a CUDA device),
5161 // we need to make the file name for the device compilation different
5162 // from the host compilation.
5163 if (!JA.isDeviceOffloading(Action::OFK_None) &&
5164 !JA.isDeviceOffloading(Action::OFK_Host)) {
5165 llvm::sys::path::replace_extension(F, "");
5166 F += Action::GetOffloadingFileNamePrefix(JA.getOffloadingDeviceKind(),
5167 Triple.normalize());
5168 F += "-";
5169 F += JA.getOffloadingArch();
5170 }
5171 }
5172
Francis Visoiu Mistrih36a7a982019-06-17 22:49:38 +00005173 std::string Extension = "opt.";
5174 if (const Arg *A =
5175 Args.getLastArg(options::OPT_fsave_optimization_record_EQ))
5176 Extension += A->getValue();
5177 else
5178 Extension += "yaml";
5179
5180 llvm::sys::path::replace_extension(F, Extension);
David L. Jonesf561aba2017-03-08 01:02:16 +00005181 CmdArgs.push_back(Args.MakeArgString(F));
5182 }
Francis Visoiu Mistrih36a7a982019-06-17 22:49:38 +00005183
Francis Visoiu Mistrihdd422362019-03-12 21:22:27 +00005184 if (const Arg *A =
5185 Args.getLastArg(options::OPT_foptimization_record_passes_EQ)) {
5186 CmdArgs.push_back("-opt-record-passes");
5187 CmdArgs.push_back(A->getValue());
5188 }
Francis Visoiu Mistrih34667512019-06-17 16:06:00 +00005189
5190 if (const Arg *A =
5191 Args.getLastArg(options::OPT_fsave_optimization_record_EQ)) {
5192 CmdArgs.push_back("-opt-record-format");
5193 CmdArgs.push_back(A->getValue());
5194 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005195 }
5196
Richard Smith86a3ef52017-06-09 21:24:02 +00005197 bool RewriteImports = Args.hasFlag(options::OPT_frewrite_imports,
5198 options::OPT_fno_rewrite_imports, false);
5199 if (RewriteImports)
5200 CmdArgs.push_back("-frewrite-imports");
5201
David L. Jonesf561aba2017-03-08 01:02:16 +00005202 // Enable rewrite includes if the user's asked for it or if we're generating
5203 // diagnostics.
5204 // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be
5205 // nice to enable this when doing a crashdump for modules as well.
5206 if (Args.hasFlag(options::OPT_frewrite_includes,
5207 options::OPT_fno_rewrite_includes, false) ||
David Blaikiea99b8e42018-11-15 03:04:19 +00005208 (C.isForDiagnostics() && !HaveModules))
David L. Jonesf561aba2017-03-08 01:02:16 +00005209 CmdArgs.push_back("-frewrite-includes");
5210
5211 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
5212 if (Arg *A = Args.getLastArg(options::OPT_traditional,
5213 options::OPT_traditional_cpp)) {
5214 if (isa<PreprocessJobAction>(JA))
5215 CmdArgs.push_back("-traditional-cpp");
5216 else
5217 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
5218 }
5219
5220 Args.AddLastArg(CmdArgs, options::OPT_dM);
5221 Args.AddLastArg(CmdArgs, options::OPT_dD);
5222
5223 // Handle serialized diagnostics.
5224 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
5225 CmdArgs.push_back("-serialize-diagnostic-file");
5226 CmdArgs.push_back(Args.MakeArgString(A->getValue()));
5227 }
5228
5229 if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
5230 CmdArgs.push_back("-fretain-comments-from-system-headers");
5231
5232 // Forward -fcomment-block-commands to -cc1.
5233 Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
5234 // Forward -fparse-all-comments to -cc1.
5235 Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments);
5236
5237 // Turn -fplugin=name.so into -load name.so
5238 for (const Arg *A : Args.filtered(options::OPT_fplugin_EQ)) {
5239 CmdArgs.push_back("-load");
5240 CmdArgs.push_back(A->getValue());
5241 A->claim();
5242 }
5243
Philip Pfaffee3f105c2019-02-02 23:19:32 +00005244 // Forward -fpass-plugin=name.so to -cc1.
5245 for (const Arg *A : Args.filtered(options::OPT_fpass_plugin_EQ)) {
5246 CmdArgs.push_back(
5247 Args.MakeArgString(Twine("-fpass-plugin=") + A->getValue()));
5248 A->claim();
5249 }
5250
David L. Jonesf561aba2017-03-08 01:02:16 +00005251 // Setup statistics file output.
Florian Hahn2e081d12018-04-20 12:50:10 +00005252 SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D);
5253 if (!StatsFile.empty())
5254 CmdArgs.push_back(Args.MakeArgString(Twine("-stats-file=") + StatsFile));
David L. Jonesf561aba2017-03-08 01:02:16 +00005255
5256 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
5257 // parser.
Guansong Zhang4747cf52017-03-15 20:57:11 +00005258 // -finclude-default-header flag is for preprocessor,
5259 // do not pass it to other cc1 commands when save-temps is enabled
5260 if (C.getDriver().isSaveTempsEnabled() &&
5261 !isa<PreprocessJobAction>(JA)) {
5262 for (auto Arg : Args.filtered(options::OPT_Xclang)) {
5263 Arg->claim();
5264 if (StringRef(Arg->getValue()) != "-finclude-default-header")
5265 CmdArgs.push_back(Arg->getValue());
5266 }
5267 }
5268 else {
5269 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
5270 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005271 for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
5272 A->claim();
5273
5274 // We translate this by hand to the -cc1 argument, since nightly test uses
5275 // it and developers have been trained to spell it with -mllvm. Both
5276 // spellings are now deprecated and should be removed.
5277 if (StringRef(A->getValue(0)) == "-disable-llvm-optzns") {
5278 CmdArgs.push_back("-disable-llvm-optzns");
5279 } else {
5280 A->render(Args, CmdArgs);
5281 }
5282 }
5283
5284 // With -save-temps, we want to save the unoptimized bitcode output from the
5285 // CompileJobAction, use -disable-llvm-passes to get pristine IR generated
5286 // by the frontend.
5287 // When -fembed-bitcode is enabled, optimized bitcode is emitted because it
5288 // has slightly different breakdown between stages.
5289 // FIXME: -fembed-bitcode -save-temps will save optimized bitcode instead of
5290 // pristine IR generated by the frontend. Ideally, a new compile action should
5291 // be added so both IR can be captured.
5292 if (C.getDriver().isSaveTempsEnabled() &&
5293 !(C.getDriver().embedBitcodeInObject() && !C.getDriver().isUsingLTO()) &&
5294 isa<CompileJobAction>(JA))
5295 CmdArgs.push_back("-disable-llvm-passes");
5296
David L. Jonesf561aba2017-03-08 01:02:16 +00005297 Args.AddAllArgs(CmdArgs, options::OPT_undef);
5298
Saleem Abdulrasool33d41382017-08-29 23:59:06 +00005299 const char *Exec = D.getClangProgramPath();
David L. Jonesf561aba2017-03-08 01:02:16 +00005300
Scott Linderde6beb02018-12-14 15:38:15 +00005301 // Optionally embed the -cc1 level arguments into the debug info or a
5302 // section, for build analysis.
Eric Christopherca325172017-03-29 23:34:20 +00005303 // Also record command line arguments into the debug info if
5304 // -grecord-gcc-switches options is set on.
5305 // By default, -gno-record-gcc-switches is set on and no recording.
Scott Linderde6beb02018-12-14 15:38:15 +00005306 auto GRecordSwitches =
5307 Args.hasFlag(options::OPT_grecord_command_line,
5308 options::OPT_gno_record_command_line, false);
5309 auto FRecordSwitches =
5310 Args.hasFlag(options::OPT_frecord_command_line,
5311 options::OPT_fno_record_command_line, false);
5312 if (FRecordSwitches && !Triple.isOSBinFormatELF())
5313 D.Diag(diag::err_drv_unsupported_opt_for_target)
5314 << Args.getLastArg(options::OPT_frecord_command_line)->getAsString(Args)
5315 << TripleStr;
5316 if (TC.UseDwarfDebugFlags() || GRecordSwitches || FRecordSwitches) {
David L. Jonesf561aba2017-03-08 01:02:16 +00005317 ArgStringList OriginalArgs;
5318 for (const auto &Arg : Args)
5319 Arg->render(Args, OriginalArgs);
5320
5321 SmallString<256> Flags;
5322 Flags += Exec;
5323 for (const char *OriginalArg : OriginalArgs) {
5324 SmallString<128> EscapedArg;
5325 EscapeSpacesAndBackslashes(OriginalArg, EscapedArg);
5326 Flags += " ";
5327 Flags += EscapedArg;
5328 }
Scott Linderde6beb02018-12-14 15:38:15 +00005329 auto FlagsArgString = Args.MakeArgString(Flags);
5330 if (TC.UseDwarfDebugFlags() || GRecordSwitches) {
5331 CmdArgs.push_back("-dwarf-debug-flags");
5332 CmdArgs.push_back(FlagsArgString);
5333 }
5334 if (FRecordSwitches) {
5335 CmdArgs.push_back("-record-command-line");
5336 CmdArgs.push_back(FlagsArgString);
5337 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005338 }
5339
Yaxun Liu97670892018-10-02 17:48:54 +00005340 // Host-side cuda compilation receives all device-side outputs in a single
5341 // fatbin as Inputs[1]. Include the binary with -fcuda-include-gpubinary.
5342 if ((IsCuda || IsHIP) && CudaDeviceInput) {
Jonas Hahnfelde7681322018-02-28 17:53:46 +00005343 CmdArgs.push_back("-fcuda-include-gpubinary");
Richard Smithcd35eff2018-09-15 01:21:16 +00005344 CmdArgs.push_back(CudaDeviceInput->getFilename());
Yaxun Liu97670892018-10-02 17:48:54 +00005345 if (Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false))
5346 CmdArgs.push_back("-fgpu-rdc");
5347 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005348
Yaxun Liu97670892018-10-02 17:48:54 +00005349 if (IsCuda) {
Artem Belevich679dafe2018-05-09 23:10:09 +00005350 if (Args.hasFlag(options::OPT_fcuda_short_ptr,
5351 options::OPT_fno_cuda_short_ptr, false))
5352 CmdArgs.push_back("-fcuda-short-ptr");
Jonas Hahnfeld5379c6d2018-02-12 10:46:45 +00005353 }
5354
David L. Jonesf561aba2017-03-08 01:02:16 +00005355 // OpenMP offloading device jobs take the argument -fopenmp-host-ir-file-path
5356 // to specify the result of the compile phase on the host, so the meaningful
5357 // device declarations can be identified. Also, -fopenmp-is-device is passed
5358 // along to tell the frontend that it is generating code for a device, so that
5359 // only the relevant declarations are emitted.
Gheorghe-Teodor Bercea3addb7d2017-06-29 15:59:19 +00005360 if (IsOpenMPDevice) {
David L. Jonesf561aba2017-03-08 01:02:16 +00005361 CmdArgs.push_back("-fopenmp-is-device");
Richard Smithcd35eff2018-09-15 01:21:16 +00005362 if (OpenMPDeviceInput) {
Gheorghe-Teodor Bercea3addb7d2017-06-29 15:59:19 +00005363 CmdArgs.push_back("-fopenmp-host-ir-file-path");
Richard Smithcd35eff2018-09-15 01:21:16 +00005364 CmdArgs.push_back(Args.MakeArgString(OpenMPDeviceInput->getFilename()));
Gheorghe-Teodor Bercea3addb7d2017-06-29 15:59:19 +00005365 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005366 }
5367
5368 // For all the host OpenMP offloading compile jobs we need to pass the targets
5369 // information using -fopenmp-targets= option.
Alexey Bataev77403de2018-07-26 15:17:38 +00005370 if (JA.isHostOffloading(Action::OFK_OpenMP)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00005371 SmallString<128> TargetInfo("-fopenmp-targets=");
5372
5373 Arg *Tgts = Args.getLastArg(options::OPT_fopenmp_targets_EQ);
5374 assert(Tgts && Tgts->getNumValues() &&
5375 "OpenMP offloading has to have targets specified.");
5376 for (unsigned i = 0; i < Tgts->getNumValues(); ++i) {
5377 if (i)
5378 TargetInfo += ',';
5379 // We need to get the string from the triple because it may be not exactly
5380 // the same as the one we get directly from the arguments.
5381 llvm::Triple T(Tgts->getValue(i));
5382 TargetInfo += T.getTriple();
5383 }
5384 CmdArgs.push_back(Args.MakeArgString(TargetInfo.str()));
5385 }
5386
5387 bool WholeProgramVTables =
5388 Args.hasFlag(options::OPT_fwhole_program_vtables,
5389 options::OPT_fno_whole_program_vtables, false);
5390 if (WholeProgramVTables) {
5391 if (!D.isUsingLTO())
5392 D.Diag(diag::err_drv_argument_only_allowed_with)
5393 << "-fwhole-program-vtables"
5394 << "-flto";
5395 CmdArgs.push_back("-fwhole-program-vtables");
5396 }
5397
Teresa Johnson84cecfc2019-01-11 18:32:07 +00005398 bool RequiresSplitLTOUnit = WholeProgramVTables || Sanitize.needsLTO();
5399 bool SplitLTOUnit =
5400 Args.hasFlag(options::OPT_fsplit_lto_unit,
5401 options::OPT_fno_split_lto_unit, RequiresSplitLTOUnit);
5402 if (RequiresSplitLTOUnit && !SplitLTOUnit)
5403 D.Diag(diag::err_drv_argument_not_allowed_with)
5404 << "-fno-split-lto-unit"
5405 << (WholeProgramVTables ? "-fwhole-program-vtables" : "-fsanitize=cfi");
5406 if (SplitLTOUnit)
5407 CmdArgs.push_back("-fsplit-lto-unit");
5408
Amara Emerson4ee9f822018-01-26 00:27:22 +00005409 if (Arg *A = Args.getLastArg(options::OPT_fexperimental_isel,
5410 options::OPT_fno_experimental_isel)) {
5411 CmdArgs.push_back("-mllvm");
5412 if (A->getOption().matches(options::OPT_fexperimental_isel)) {
5413 CmdArgs.push_back("-global-isel=1");
5414
5415 // GISel is on by default on AArch64 -O0, so don't bother adding
5416 // the fallback remarks for it. Other combinations will add a warning of
5417 // some kind.
5418 bool IsArchSupported = Triple.getArch() == llvm::Triple::aarch64;
5419 bool IsOptLevelSupported = false;
5420
5421 Arg *A = Args.getLastArg(options::OPT_O_Group);
5422 if (Triple.getArch() == llvm::Triple::aarch64) {
5423 if (!A || A->getOption().matches(options::OPT_O0))
5424 IsOptLevelSupported = true;
5425 }
5426 if (!IsArchSupported || !IsOptLevelSupported) {
5427 CmdArgs.push_back("-mllvm");
5428 CmdArgs.push_back("-global-isel-abort=2");
5429
5430 if (!IsArchSupported)
5431 D.Diag(diag::warn_drv_experimental_isel_incomplete) << Triple.getArchName();
5432 else
5433 D.Diag(diag::warn_drv_experimental_isel_incomplete_opt);
5434 }
5435 } else {
5436 CmdArgs.push_back("-global-isel=0");
5437 }
5438 }
5439
Manman Ren394d4cc2019-03-04 20:30:30 +00005440 if (Args.hasArg(options::OPT_forder_file_instrumentation)) {
5441 CmdArgs.push_back("-forder-file-instrumentation");
5442 // Enable order file instrumentation when ThinLTO is not on. When ThinLTO is
5443 // on, we need to pass these flags as linker flags and that will be handled
5444 // outside of the compiler.
5445 if (!D.isUsingLTO()) {
5446 CmdArgs.push_back("-mllvm");
5447 CmdArgs.push_back("-enable-order-file-instrumentation");
5448 }
5449 }
5450
Mandeep Singh Grangac24bb52018-02-25 03:58:23 +00005451 if (Arg *A = Args.getLastArg(options::OPT_fforce_enable_int128,
5452 options::OPT_fno_force_enable_int128)) {
5453 if (A->getOption().matches(options::OPT_fforce_enable_int128))
5454 CmdArgs.push_back("-fforce-enable-int128");
5455 }
5456
Peter Collingbourne54d13b42018-05-30 03:40:04 +00005457 if (Args.hasFlag(options::OPT_fcomplete_member_pointers,
5458 options::OPT_fno_complete_member_pointers, false))
5459 CmdArgs.push_back("-fcomplete-member-pointers");
5460
Erik Pilkington5a559e62018-08-21 17:24:06 +00005461 if (!Args.hasFlag(options::OPT_fcxx_static_destructors,
5462 options::OPT_fno_cxx_static_destructors, true))
5463 CmdArgs.push_back("-fno-c++-static-destructors");
5464
Jessica Paquette36a25672018-06-29 18:06:10 +00005465 if (Arg *A = Args.getLastArg(options::OPT_moutline,
5466 options::OPT_mno_outline)) {
5467 if (A->getOption().matches(options::OPT_moutline)) {
5468 // We only support -moutline in AArch64 right now. If we're not compiling
5469 // for AArch64, emit a warning and ignore the flag. Otherwise, add the
5470 // proper mllvm flags.
5471 if (Triple.getArch() != llvm::Triple::aarch64) {
5472 D.Diag(diag::warn_drv_moutline_unsupported_opt) << Triple.getArchName();
5473 } else {
Jessica Paquette36a25672018-06-29 18:06:10 +00005474 CmdArgs.push_back("-mllvm");
Jessica Paquette33648c32018-07-06 22:24:56 +00005475 CmdArgs.push_back("-enable-machine-outliner");
Jessica Paquettea67abc82018-06-26 22:09:48 +00005476 }
Jessica Paquette36a25672018-06-29 18:06:10 +00005477 } else {
5478 // Disable all outlining behaviour.
5479 CmdArgs.push_back("-mllvm");
5480 CmdArgs.push_back("-enable-machine-outliner=never");
Jessica Paquettea67abc82018-06-26 22:09:48 +00005481 }
5482 }
5483
Peter Collingbourne14b468b2018-07-18 00:27:07 +00005484 if (Args.hasFlag(options::OPT_faddrsig, options::OPT_fno_addrsig,
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00005485 (TC.getTriple().isOSBinFormatELF() ||
5486 TC.getTriple().isOSBinFormatCOFF()) &&
Douglas Yung25f04772018-12-19 22:45:26 +00005487 !TC.getTriple().isPS4() &&
Michal Gorny5a409d02018-12-20 13:09:30 +00005488 !TC.getTriple().isOSNetBSD() &&
Michal Gornydae01c32018-12-23 15:07:26 +00005489 !Distro(D.getVFS()).IsGentoo() &&
Dan Albertdd142342019-01-08 22:33:59 +00005490 !TC.getTriple().isAndroid() &&
Michal Gorny5a409d02018-12-20 13:09:30 +00005491 TC.useIntegratedAs()))
Peter Collingbourne14b468b2018-07-18 00:27:07 +00005492 CmdArgs.push_back("-faddrsig");
5493
Peter Collingbournee08e68d2019-06-07 19:10:08 +00005494 if (Arg *A = Args.getLastArg(options::OPT_fsymbol_partition_EQ)) {
5495 std::string Str = A->getAsString(Args);
5496 if (!TC.getTriple().isOSBinFormatELF())
5497 D.Diag(diag::err_drv_unsupported_opt_for_target)
5498 << Str << TC.getTripleString();
5499 CmdArgs.push_back(Args.MakeArgString(Str));
5500 }
5501
Reid Kleckner549ed542019-05-23 18:35:43 +00005502 // Add the "-o out -x type src.c" flags last. This is done primarily to make
5503 // the -cc1 command easier to edit when reproducing compiler crashes.
5504 if (Output.getType() == types::TY_Dependencies) {
5505 // Handled with other dependency code.
5506 } else if (Output.isFilename()) {
5507 CmdArgs.push_back("-o");
5508 CmdArgs.push_back(Output.getFilename());
5509 } else {
5510 assert(Output.isNothing() && "Invalid output.");
5511 }
5512
5513 addDashXForInput(Args, Input, CmdArgs);
5514
5515 ArrayRef<InputInfo> FrontendInputs = Input;
5516 if (IsHeaderModulePrecompile)
5517 FrontendInputs = ModuleHeaderInputs;
5518 else if (Input.isNothing())
5519 FrontendInputs = {};
5520
5521 for (const InputInfo &Input : FrontendInputs) {
5522 if (Input.isFilename())
5523 CmdArgs.push_back(Input.getFilename());
5524 else
5525 Input.getInputArg().renderAsInput(Args, CmdArgs);
5526 }
5527
David L. Jonesf561aba2017-03-08 01:02:16 +00005528 // Finally add the compile command to the compilation.
5529 if (Args.hasArg(options::OPT__SLASH_fallback) &&
5530 Output.getType() == types::TY_Object &&
5531 (InputType == types::TY_C || InputType == types::TY_CXX)) {
5532 auto CLCommand =
5533 getCLFallback()->GetCommand(C, JA, Output, Inputs, Args, LinkingOutput);
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00005534 C.addCommand(std::make_unique<FallbackCommand>(
David L. Jonesf561aba2017-03-08 01:02:16 +00005535 JA, *this, Exec, CmdArgs, Inputs, std::move(CLCommand)));
5536 } else if (Args.hasArg(options::OPT__SLASH_fallback) &&
5537 isa<PrecompileJobAction>(JA)) {
5538 // In /fallback builds, run the main compilation even if the pch generation
5539 // fails, so that the main compilation's fallback to cl.exe runs.
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00005540 C.addCommand(std::make_unique<ForceSuccessCommand>(JA, *this, Exec,
David L. Jonesf561aba2017-03-08 01:02:16 +00005541 CmdArgs, Inputs));
5542 } else {
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00005543 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
David L. Jonesf561aba2017-03-08 01:02:16 +00005544 }
5545
Hans Wennborg2fe01042018-10-13 19:13:14 +00005546 // Make the compile command echo its inputs for /showFilenames.
5547 if (Output.getType() == types::TY_Object &&
5548 Args.hasFlag(options::OPT__SLASH_showFilenames,
5549 options::OPT__SLASH_showFilenames_, false)) {
5550 C.getJobs().getJobs().back()->setPrintInputFilenames(true);
5551 }
5552
David L. Jonesf561aba2017-03-08 01:02:16 +00005553 if (Arg *A = Args.getLastArg(options::OPT_pg))
Yuanfang Chenff22ec32019-07-20 22:50:50 +00005554 if (FPKeepKind == CodeGenOptions::FramePointerKind::None)
David L. Jonesf561aba2017-03-08 01:02:16 +00005555 D.Diag(diag::err_drv_argument_not_allowed_with) << "-fomit-frame-pointer"
5556 << A->getAsString(Args);
5557
5558 // Claim some arguments which clang supports automatically.
5559
5560 // -fpch-preprocess is used with gcc to add a special marker in the output to
Erich Keane0a6b5b62018-12-04 14:34:09 +00005561 // include the PCH file.
David L. Jonesf561aba2017-03-08 01:02:16 +00005562 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
5563
5564 // Claim some arguments which clang doesn't support, but we don't
5565 // care to warn the user about.
5566 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
5567 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
5568
5569 // Disable warnings for clang -E -emit-llvm foo.c
5570 Args.ClaimAllArgs(options::OPT_emit_llvm);
5571}
5572
5573Clang::Clang(const ToolChain &TC)
5574 // CAUTION! The first constructor argument ("clang") is not arbitrary,
5575 // as it is for other tools. Some operations on a Tool actually test
5576 // whether that tool is Clang based on the Tool's Name as a string.
5577 : Tool("clang", "clang frontend", TC, RF_Full) {}
5578
5579Clang::~Clang() {}
5580
5581/// Add options related to the Objective-C runtime/ABI.
5582///
5583/// Returns true if the runtime is non-fragile.
5584ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
5585 ArgStringList &cmdArgs,
5586 RewriteKind rewriteKind) const {
5587 // Look for the controlling runtime option.
5588 Arg *runtimeArg =
5589 args.getLastArg(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
5590 options::OPT_fobjc_runtime_EQ);
5591
5592 // Just forward -fobjc-runtime= to the frontend. This supercedes
5593 // options about fragility.
5594 if (runtimeArg &&
5595 runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
5596 ObjCRuntime runtime;
5597 StringRef value = runtimeArg->getValue();
5598 if (runtime.tryParse(value)) {
5599 getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
5600 << value;
5601 }
David Chisnall404bbcb2018-05-22 10:13:06 +00005602 if ((runtime.getKind() == ObjCRuntime::GNUstep) &&
5603 (runtime.getVersion() >= VersionTuple(2, 0)))
David Chisnallef16ea72018-09-04 10:07:27 +00005604 if (!getToolChain().getTriple().isOSBinFormatELF() &&
5605 !getToolChain().getTriple().isOSBinFormatCOFF()) {
David Chisnall404bbcb2018-05-22 10:13:06 +00005606 getToolChain().getDriver().Diag(
5607 diag::err_drv_gnustep_objc_runtime_incompatible_binary)
5608 << runtime.getVersion().getMajor();
5609 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005610
5611 runtimeArg->render(args, cmdArgs);
5612 return runtime;
5613 }
5614
5615 // Otherwise, we'll need the ABI "version". Version numbers are
5616 // slightly confusing for historical reasons:
5617 // 1 - Traditional "fragile" ABI
5618 // 2 - Non-fragile ABI, version 1
5619 // 3 - Non-fragile ABI, version 2
5620 unsigned objcABIVersion = 1;
5621 // If -fobjc-abi-version= is present, use that to set the version.
5622 if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
5623 StringRef value = abiArg->getValue();
5624 if (value == "1")
5625 objcABIVersion = 1;
5626 else if (value == "2")
5627 objcABIVersion = 2;
5628 else if (value == "3")
5629 objcABIVersion = 3;
5630 else
5631 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) << value;
5632 } else {
5633 // Otherwise, determine if we are using the non-fragile ABI.
5634 bool nonFragileABIIsDefault =
5635 (rewriteKind == RK_NonFragile ||
5636 (rewriteKind == RK_None &&
5637 getToolChain().IsObjCNonFragileABIDefault()));
5638 if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
5639 options::OPT_fno_objc_nonfragile_abi,
5640 nonFragileABIIsDefault)) {
5641// Determine the non-fragile ABI version to use.
5642#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
5643 unsigned nonFragileABIVersion = 1;
5644#else
5645 unsigned nonFragileABIVersion = 2;
5646#endif
5647
5648 if (Arg *abiArg =
5649 args.getLastArg(options::OPT_fobjc_nonfragile_abi_version_EQ)) {
5650 StringRef value = abiArg->getValue();
5651 if (value == "1")
5652 nonFragileABIVersion = 1;
5653 else if (value == "2")
5654 nonFragileABIVersion = 2;
5655 else
5656 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
5657 << value;
5658 }
5659
5660 objcABIVersion = 1 + nonFragileABIVersion;
5661 } else {
5662 objcABIVersion = 1;
5663 }
5664 }
5665
5666 // We don't actually care about the ABI version other than whether
5667 // it's non-fragile.
5668 bool isNonFragile = objcABIVersion != 1;
5669
5670 // If we have no runtime argument, ask the toolchain for its default runtime.
5671 // However, the rewriter only really supports the Mac runtime, so assume that.
5672 ObjCRuntime runtime;
5673 if (!runtimeArg) {
5674 switch (rewriteKind) {
5675 case RK_None:
5676 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
5677 break;
5678 case RK_Fragile:
5679 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
5680 break;
5681 case RK_NonFragile:
5682 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
5683 break;
5684 }
5685
5686 // -fnext-runtime
5687 } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
5688 // On Darwin, make this use the default behavior for the toolchain.
5689 if (getToolChain().getTriple().isOSDarwin()) {
5690 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
5691
5692 // Otherwise, build for a generic macosx port.
5693 } else {
5694 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
5695 }
5696
5697 // -fgnu-runtime
5698 } else {
5699 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
5700 // Legacy behaviour is to target the gnustep runtime if we are in
5701 // non-fragile mode or the GCC runtime in fragile mode.
5702 if (isNonFragile)
David Chisnall404bbcb2018-05-22 10:13:06 +00005703 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(2, 0));
David L. Jonesf561aba2017-03-08 01:02:16 +00005704 else
5705 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
5706 }
5707
5708 cmdArgs.push_back(
5709 args.MakeArgString("-fobjc-runtime=" + runtime.getAsString()));
5710 return runtime;
5711}
5712
5713static bool maybeConsumeDash(const std::string &EH, size_t &I) {
5714 bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-');
5715 I += HaveDash;
5716 return !HaveDash;
5717}
5718
5719namespace {
5720struct EHFlags {
5721 bool Synch = false;
5722 bool Asynch = false;
5723 bool NoUnwindC = false;
5724};
5725} // end anonymous namespace
5726
5727/// /EH controls whether to run destructor cleanups when exceptions are
5728/// thrown. There are three modifiers:
5729/// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions.
5730/// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions.
5731/// The 'a' modifier is unimplemented and fundamentally hard in LLVM IR.
5732/// - c: Assume that extern "C" functions are implicitly nounwind.
5733/// The default is /EHs-c-, meaning cleanups are disabled.
5734static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) {
5735 EHFlags EH;
5736
5737 std::vector<std::string> EHArgs =
5738 Args.getAllArgValues(options::OPT__SLASH_EH);
5739 for (auto EHVal : EHArgs) {
5740 for (size_t I = 0, E = EHVal.size(); I != E; ++I) {
5741 switch (EHVal[I]) {
5742 case 'a':
5743 EH.Asynch = maybeConsumeDash(EHVal, I);
5744 if (EH.Asynch)
5745 EH.Synch = false;
5746 continue;
5747 case 'c':
5748 EH.NoUnwindC = maybeConsumeDash(EHVal, I);
5749 continue;
5750 case 's':
5751 EH.Synch = maybeConsumeDash(EHVal, I);
5752 if (EH.Synch)
5753 EH.Asynch = false;
5754 continue;
5755 default:
5756 break;
5757 }
5758 D.Diag(clang::diag::err_drv_invalid_value) << "/EH" << EHVal;
5759 break;
5760 }
5761 }
5762 // The /GX, /GX- flags are only processed if there are not /EH flags.
5763 // The default is that /GX is not specified.
5764 if (EHArgs.empty() &&
5765 Args.hasFlag(options::OPT__SLASH_GX, options::OPT__SLASH_GX_,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00005766 /*Default=*/false)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00005767 EH.Synch = true;
5768 EH.NoUnwindC = true;
5769 }
5770
5771 return EH;
5772}
5773
5774void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
5775 ArgStringList &CmdArgs,
5776 codegenoptions::DebugInfoKind *DebugInfoKind,
5777 bool *EmitCodeView) const {
5778 unsigned RTOptionID = options::OPT__SLASH_MT;
5779
5780 if (Args.hasArg(options::OPT__SLASH_LDd))
5781 // The /LDd option implies /MTd. The dependent lib part can be overridden,
5782 // but defining _DEBUG is sticky.
5783 RTOptionID = options::OPT__SLASH_MTd;
5784
5785 if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group))
5786 RTOptionID = A->getOption().getID();
5787
5788 StringRef FlagForCRT;
5789 switch (RTOptionID) {
5790 case options::OPT__SLASH_MD:
5791 if (Args.hasArg(options::OPT__SLASH_LDd))
5792 CmdArgs.push_back("-D_DEBUG");
5793 CmdArgs.push_back("-D_MT");
5794 CmdArgs.push_back("-D_DLL");
5795 FlagForCRT = "--dependent-lib=msvcrt";
5796 break;
5797 case options::OPT__SLASH_MDd:
5798 CmdArgs.push_back("-D_DEBUG");
5799 CmdArgs.push_back("-D_MT");
5800 CmdArgs.push_back("-D_DLL");
5801 FlagForCRT = "--dependent-lib=msvcrtd";
5802 break;
5803 case options::OPT__SLASH_MT:
5804 if (Args.hasArg(options::OPT__SLASH_LDd))
5805 CmdArgs.push_back("-D_DEBUG");
5806 CmdArgs.push_back("-D_MT");
5807 CmdArgs.push_back("-flto-visibility-public-std");
5808 FlagForCRT = "--dependent-lib=libcmt";
5809 break;
5810 case options::OPT__SLASH_MTd:
5811 CmdArgs.push_back("-D_DEBUG");
5812 CmdArgs.push_back("-D_MT");
5813 CmdArgs.push_back("-flto-visibility-public-std");
5814 FlagForCRT = "--dependent-lib=libcmtd";
5815 break;
5816 default:
5817 llvm_unreachable("Unexpected option ID.");
5818 }
5819
5820 if (Args.hasArg(options::OPT__SLASH_Zl)) {
5821 CmdArgs.push_back("-D_VC_NODEFAULTLIB");
5822 } else {
5823 CmdArgs.push_back(FlagForCRT.data());
5824
5825 // This provides POSIX compatibility (maps 'open' to '_open'), which most
5826 // users want. The /Za flag to cl.exe turns this off, but it's not
5827 // implemented in clang.
5828 CmdArgs.push_back("--dependent-lib=oldnames");
5829 }
5830
Nico Weber908b6972019-06-26 17:51:47 +00005831 Args.AddLastArg(CmdArgs, options::OPT_show_includes);
David L. Jonesf561aba2017-03-08 01:02:16 +00005832
5833 // This controls whether or not we emit RTTI data for polymorphic types.
5834 if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00005835 /*Default=*/false))
David L. Jonesf561aba2017-03-08 01:02:16 +00005836 CmdArgs.push_back("-fno-rtti-data");
5837
5838 // This controls whether or not we emit stack-protector instrumentation.
5839 // In MSVC, Buffer Security Check (/GS) is on by default.
5840 if (Args.hasFlag(options::OPT__SLASH_GS, options::OPT__SLASH_GS_,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00005841 /*Default=*/true)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00005842 CmdArgs.push_back("-stack-protector");
5843 CmdArgs.push_back(Args.MakeArgString(Twine(LangOptions::SSPStrong)));
5844 }
5845
5846 // Emit CodeView if -Z7, -Zd, or -gline-tables-only are present.
5847 if (Arg *DebugInfoArg =
5848 Args.getLastArg(options::OPT__SLASH_Z7, options::OPT__SLASH_Zd,
5849 options::OPT_gline_tables_only)) {
5850 *EmitCodeView = true;
5851 if (DebugInfoArg->getOption().matches(options::OPT__SLASH_Z7))
5852 *DebugInfoKind = codegenoptions::LimitedDebugInfo;
5853 else
5854 *DebugInfoKind = codegenoptions::DebugLineTablesOnly;
David L. Jonesf561aba2017-03-08 01:02:16 +00005855 } else {
5856 *EmitCodeView = false;
5857 }
5858
5859 const Driver &D = getToolChain().getDriver();
5860 EHFlags EH = parseClangCLEHFlags(D, Args);
5861 if (EH.Synch || EH.Asynch) {
5862 if (types::isCXX(InputType))
5863 CmdArgs.push_back("-fcxx-exceptions");
5864 CmdArgs.push_back("-fexceptions");
5865 }
5866 if (types::isCXX(InputType) && EH.Synch && EH.NoUnwindC)
5867 CmdArgs.push_back("-fexternc-nounwind");
5868
5869 // /EP should expand to -E -P.
5870 if (Args.hasArg(options::OPT__SLASH_EP)) {
5871 CmdArgs.push_back("-E");
5872 CmdArgs.push_back("-P");
5873 }
5874
5875 unsigned VolatileOptionID;
5876 if (getToolChain().getArch() == llvm::Triple::x86_64 ||
5877 getToolChain().getArch() == llvm::Triple::x86)
5878 VolatileOptionID = options::OPT__SLASH_volatile_ms;
5879 else
5880 VolatileOptionID = options::OPT__SLASH_volatile_iso;
5881
5882 if (Arg *A = Args.getLastArg(options::OPT__SLASH_volatile_Group))
5883 VolatileOptionID = A->getOption().getID();
5884
5885 if (VolatileOptionID == options::OPT__SLASH_volatile_ms)
5886 CmdArgs.push_back("-fms-volatile");
5887
Takuto Ikuta302c6432018-11-03 06:45:00 +00005888 if (Args.hasFlag(options::OPT__SLASH_Zc_dllexportInlines_,
5889 options::OPT__SLASH_Zc_dllexportInlines,
Takuto Ikuta245d9472018-11-13 04:14:09 +00005890 false)) {
5891 if (Args.hasArg(options::OPT__SLASH_fallback)) {
5892 D.Diag(clang::diag::err_drv_dllexport_inlines_and_fallback);
5893 } else {
Takuto Ikuta302c6432018-11-03 06:45:00 +00005894 CmdArgs.push_back("-fno-dllexport-inlines");
Takuto Ikuta245d9472018-11-13 04:14:09 +00005895 }
5896 }
Takuto Ikuta302c6432018-11-03 06:45:00 +00005897
David L. Jonesf561aba2017-03-08 01:02:16 +00005898 Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg);
5899 Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb);
5900 if (MostGeneralArg && BestCaseArg)
5901 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
5902 << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args);
5903
5904 if (MostGeneralArg) {
5905 Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms);
5906 Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm);
5907 Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv);
5908
5909 Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg;
5910 Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg;
5911 if (FirstConflict && SecondConflict && FirstConflict != SecondConflict)
5912 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
5913 << FirstConflict->getAsString(Args)
5914 << SecondConflict->getAsString(Args);
5915
5916 if (SingleArg)
5917 CmdArgs.push_back("-fms-memptr-rep=single");
5918 else if (MultipleArg)
5919 CmdArgs.push_back("-fms-memptr-rep=multiple");
5920 else
5921 CmdArgs.push_back("-fms-memptr-rep=virtual");
5922 }
5923
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005924 // Parse the default calling convention options.
5925 if (Arg *CCArg =
5926 Args.getLastArg(options::OPT__SLASH_Gd, options::OPT__SLASH_Gr,
Erich Keanea957ffb2017-11-02 21:08:00 +00005927 options::OPT__SLASH_Gz, options::OPT__SLASH_Gv,
5928 options::OPT__SLASH_Gregcall)) {
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005929 unsigned DCCOptId = CCArg->getOption().getID();
5930 const char *DCCFlag = nullptr;
5931 bool ArchSupported = true;
5932 llvm::Triple::ArchType Arch = getToolChain().getArch();
5933 switch (DCCOptId) {
5934 case options::OPT__SLASH_Gd:
Reid Kleckner6344f102017-05-31 15:50:35 +00005935 DCCFlag = "-fdefault-calling-conv=cdecl";
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005936 break;
5937 case options::OPT__SLASH_Gr:
5938 ArchSupported = Arch == llvm::Triple::x86;
Reid Kleckner6344f102017-05-31 15:50:35 +00005939 DCCFlag = "-fdefault-calling-conv=fastcall";
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005940 break;
5941 case options::OPT__SLASH_Gz:
5942 ArchSupported = Arch == llvm::Triple::x86;
Reid Kleckner6344f102017-05-31 15:50:35 +00005943 DCCFlag = "-fdefault-calling-conv=stdcall";
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005944 break;
5945 case options::OPT__SLASH_Gv:
5946 ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64;
Reid Kleckner6344f102017-05-31 15:50:35 +00005947 DCCFlag = "-fdefault-calling-conv=vectorcall";
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005948 break;
Erich Keanea957ffb2017-11-02 21:08:00 +00005949 case options::OPT__SLASH_Gregcall:
5950 ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64;
5951 DCCFlag = "-fdefault-calling-conv=regcall";
5952 break;
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005953 }
5954
5955 // MSVC doesn't warn if /Gr or /Gz is used on x64, so we don't either.
5956 if (ArchSupported && DCCFlag)
5957 CmdArgs.push_back(DCCFlag);
5958 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005959
Nico Weber908b6972019-06-26 17:51:47 +00005960 Args.AddLastArg(CmdArgs, options::OPT_vtordisp_mode_EQ);
David L. Jonesf561aba2017-03-08 01:02:16 +00005961
5962 if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) {
5963 CmdArgs.push_back("-fdiagnostics-format");
5964 if (Args.hasArg(options::OPT__SLASH_fallback))
5965 CmdArgs.push_back("msvc-fallback");
5966 else
5967 CmdArgs.push_back("msvc");
5968 }
Adrian McCarthydb2736d2018-01-09 23:49:30 +00005969
Hans Wennborga912e3e2018-08-10 09:49:21 +00005970 if (Arg *A = Args.getLastArg(options::OPT__SLASH_guard)) {
5971 SmallVector<StringRef, 1> SplitArgs;
5972 StringRef(A->getValue()).split(SplitArgs, ",");
5973 bool Instrument = false;
5974 bool NoChecks = false;
5975 for (StringRef Arg : SplitArgs) {
5976 if (Arg.equals_lower("cf"))
5977 Instrument = true;
5978 else if (Arg.equals_lower("cf-"))
5979 Instrument = false;
5980 else if (Arg.equals_lower("nochecks"))
5981 NoChecks = true;
5982 else if (Arg.equals_lower("nochecks-"))
5983 NoChecks = false;
5984 else
5985 D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << Arg;
5986 }
5987 // Currently there's no support emitting CFG instrumentation; the flag only
5988 // emits the table of address-taken functions.
5989 if (Instrument || NoChecks)
5990 CmdArgs.push_back("-cfguard");
5991 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005992}
5993
5994visualstudio::Compiler *Clang::getCLFallback() const {
5995 if (!CLFallback)
5996 CLFallback.reset(new visualstudio::Compiler(getToolChain()));
5997 return CLFallback.get();
5998}
5999
6000
6001const char *Clang::getBaseInputName(const ArgList &Args,
6002 const InputInfo &Input) {
6003 return Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput()));
6004}
6005
6006const char *Clang::getBaseInputStem(const ArgList &Args,
6007 const InputInfoList &Inputs) {
6008 const char *Str = getBaseInputName(Args, Inputs[0]);
6009
6010 if (const char *End = strrchr(Str, '.'))
6011 return Args.MakeArgString(std::string(Str, End));
6012
6013 return Str;
6014}
6015
6016const char *Clang::getDependencyFileName(const ArgList &Args,
6017 const InputInfoList &Inputs) {
6018 // FIXME: Think about this more.
6019 std::string Res;
6020
6021 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
6022 std::string Str(OutputOpt->getValue());
6023 Res = Str.substr(0, Str.rfind('.'));
6024 } else {
6025 Res = getBaseInputStem(Args, Inputs);
6026 }
6027 return Args.MakeArgString(Res + ".d");
6028}
6029
6030// Begin ClangAs
6031
6032void ClangAs::AddMIPSTargetArgs(const ArgList &Args,
6033 ArgStringList &CmdArgs) const {
6034 StringRef CPUName;
6035 StringRef ABIName;
6036 const llvm::Triple &Triple = getToolChain().getTriple();
6037 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
6038
6039 CmdArgs.push_back("-target-abi");
6040 CmdArgs.push_back(ABIName.data());
6041}
6042
6043void ClangAs::AddX86TargetArgs(const ArgList &Args,
6044 ArgStringList &CmdArgs) const {
6045 if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
6046 StringRef Value = A->getValue();
6047 if (Value == "intel" || Value == "att") {
6048 CmdArgs.push_back("-mllvm");
6049 CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
6050 } else {
6051 getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
6052 << A->getOption().getName() << Value;
6053 }
6054 }
6055}
6056
Roger Ferrer Ibaneze41a74e2019-03-26 08:01:18 +00006057void ClangAs::AddRISCVTargetArgs(const ArgList &Args,
6058 ArgStringList &CmdArgs) const {
6059 const llvm::Triple &Triple = getToolChain().getTriple();
6060 StringRef ABIName = riscv::getRISCVABI(Args, Triple);
6061
6062 CmdArgs.push_back("-target-abi");
6063 CmdArgs.push_back(ABIName.data());
6064}
6065
David L. Jonesf561aba2017-03-08 01:02:16 +00006066void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
6067 const InputInfo &Output, const InputInfoList &Inputs,
6068 const ArgList &Args,
6069 const char *LinkingOutput) const {
6070 ArgStringList CmdArgs;
6071
6072 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
6073 const InputInfo &Input = Inputs[0];
6074
Martin Storsjob547ef22018-10-26 08:33:29 +00006075 const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
David L. Jonesf561aba2017-03-08 01:02:16 +00006076 const std::string &TripleStr = Triple.getTriple();
Martin Storsjob547ef22018-10-26 08:33:29 +00006077 const auto &D = getToolChain().getDriver();
David L. Jonesf561aba2017-03-08 01:02:16 +00006078
6079 // Don't warn about "clang -w -c foo.s"
6080 Args.ClaimAllArgs(options::OPT_w);
6081 // and "clang -emit-llvm -c foo.s"
6082 Args.ClaimAllArgs(options::OPT_emit_llvm);
6083
6084 claimNoWarnArgs(Args);
6085
6086 // Invoke ourselves in -cc1as mode.
6087 //
6088 // FIXME: Implement custom jobs for internal actions.
6089 CmdArgs.push_back("-cc1as");
6090
6091 // Add the "effective" target triple.
6092 CmdArgs.push_back("-triple");
6093 CmdArgs.push_back(Args.MakeArgString(TripleStr));
6094
6095 // Set the output mode, we currently only expect to be used as a real
6096 // assembler.
6097 CmdArgs.push_back("-filetype");
6098 CmdArgs.push_back("obj");
6099
6100 // Set the main file name, so that debug info works even with
6101 // -save-temps or preprocessed assembly.
6102 CmdArgs.push_back("-main-file-name");
6103 CmdArgs.push_back(Clang::getBaseInputName(Args, Input));
6104
6105 // Add the target cpu
6106 std::string CPU = getCPUName(Args, Triple, /*FromAs*/ true);
6107 if (!CPU.empty()) {
6108 CmdArgs.push_back("-target-cpu");
6109 CmdArgs.push_back(Args.MakeArgString(CPU));
6110 }
6111
6112 // Add the target features
6113 getTargetFeatures(getToolChain(), Triple, Args, CmdArgs, true);
6114
6115 // Ignore explicit -force_cpusubtype_ALL option.
6116 (void)Args.hasArg(options::OPT_force__cpusubtype__ALL);
6117
6118 // Pass along any -I options so we get proper .include search paths.
6119 Args.AddAllArgs(CmdArgs, options::OPT_I_Group);
6120
6121 // Determine the original source input.
6122 const Action *SourceAction = &JA;
6123 while (SourceAction->getKind() != Action::InputClass) {
6124 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
6125 SourceAction = SourceAction->getInputs()[0];
6126 }
6127
6128 // Forward -g and handle debug info related flags, assuming we are dealing
6129 // with an actual assembly file.
6130 bool WantDebug = false;
6131 unsigned DwarfVersion = 0;
6132 Args.ClaimAllArgs(options::OPT_g_Group);
6133 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
6134 WantDebug = !A->getOption().matches(options::OPT_g0) &&
6135 !A->getOption().matches(options::OPT_ggdb0);
6136 if (WantDebug)
6137 DwarfVersion = DwarfVersionNum(A->getSpelling());
6138 }
6139 if (DwarfVersion == 0)
6140 DwarfVersion = getToolChain().GetDefaultDwarfVersion();
6141
6142 codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo;
6143
6144 if (SourceAction->getType() == types::TY_Asm ||
6145 SourceAction->getType() == types::TY_PP_Asm) {
6146 // You might think that it would be ok to set DebugInfoKind outside of
6147 // the guard for source type, however there is a test which asserts
6148 // that some assembler invocation receives no -debug-info-kind,
6149 // and it's not clear whether that test is just overly restrictive.
6150 DebugInfoKind = (WantDebug ? codegenoptions::LimitedDebugInfo
6151 : codegenoptions::NoDebugInfo);
6152 // Add the -fdebug-compilation-dir flag if needed.
Michael J. Spencer7e48b402019-05-28 22:21:47 +00006153 addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS());
David L. Jonesf561aba2017-03-08 01:02:16 +00006154
Paul Robinson9b292b42018-07-10 15:15:24 +00006155 addDebugPrefixMapArg(getToolChain().getDriver(), Args, CmdArgs);
6156
David L. Jonesf561aba2017-03-08 01:02:16 +00006157 // Set the AT_producer to the clang version when using the integrated
6158 // assembler on assembly source files.
6159 CmdArgs.push_back("-dwarf-debug-producer");
6160 CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
6161
6162 // And pass along -I options
6163 Args.AddAllArgs(CmdArgs, options::OPT_I);
6164 }
6165 RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion,
6166 llvm::DebuggerKind::Default);
Alexey Bataevb83b4e42018-07-27 19:45:14 +00006167 RenderDebugInfoCompressionArgs(Args, CmdArgs, D, getToolChain());
Saleem Abdulrasoold064e912017-06-23 15:34:16 +00006168
David L. Jonesf561aba2017-03-08 01:02:16 +00006169
6170 // Handle -fPIC et al -- the relocation-model affects the assembler
6171 // for some targets.
6172 llvm::Reloc::Model RelocationModel;
6173 unsigned PICLevel;
6174 bool IsPIE;
6175 std::tie(RelocationModel, PICLevel, IsPIE) =
6176 ParsePICArgs(getToolChain(), Args);
6177
6178 const char *RMName = RelocationModelName(RelocationModel);
6179 if (RMName) {
6180 CmdArgs.push_back("-mrelocation-model");
6181 CmdArgs.push_back(RMName);
6182 }
6183
6184 // Optionally embed the -cc1as level arguments into the debug info, for build
6185 // analysis.
6186 if (getToolChain().UseDwarfDebugFlags()) {
6187 ArgStringList OriginalArgs;
6188 for (const auto &Arg : Args)
6189 Arg->render(Args, OriginalArgs);
6190
6191 SmallString<256> Flags;
6192 const char *Exec = getToolChain().getDriver().getClangProgramPath();
6193 Flags += Exec;
6194 for (const char *OriginalArg : OriginalArgs) {
6195 SmallString<128> EscapedArg;
6196 EscapeSpacesAndBackslashes(OriginalArg, EscapedArg);
6197 Flags += " ";
6198 Flags += EscapedArg;
6199 }
6200 CmdArgs.push_back("-dwarf-debug-flags");
6201 CmdArgs.push_back(Args.MakeArgString(Flags));
6202 }
6203
6204 // FIXME: Add -static support, once we have it.
6205
6206 // Add target specific flags.
6207 switch (getToolChain().getArch()) {
6208 default:
6209 break;
6210
6211 case llvm::Triple::mips:
6212 case llvm::Triple::mipsel:
6213 case llvm::Triple::mips64:
6214 case llvm::Triple::mips64el:
6215 AddMIPSTargetArgs(Args, CmdArgs);
6216 break;
6217
6218 case llvm::Triple::x86:
6219 case llvm::Triple::x86_64:
6220 AddX86TargetArgs(Args, CmdArgs);
6221 break;
Oliver Stannard692dc542017-04-18 13:21:05 +00006222
6223 case llvm::Triple::arm:
6224 case llvm::Triple::armeb:
6225 case llvm::Triple::thumb:
6226 case llvm::Triple::thumbeb:
6227 // This isn't in AddARMTargetArgs because we want to do this for assembly
6228 // only, not C/C++.
6229 if (Args.hasFlag(options::OPT_mdefault_build_attributes,
6230 options::OPT_mno_default_build_attributes, true)) {
6231 CmdArgs.push_back("-mllvm");
6232 CmdArgs.push_back("-arm-add-build-attributes");
6233 }
6234 break;
Roger Ferrer Ibaneze41a74e2019-03-26 08:01:18 +00006235
6236 case llvm::Triple::riscv32:
6237 case llvm::Triple::riscv64:
6238 AddRISCVTargetArgs(Args, CmdArgs);
6239 break;
David L. Jonesf561aba2017-03-08 01:02:16 +00006240 }
6241
6242 // Consume all the warning flags. Usually this would be handled more
6243 // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as
6244 // doesn't handle that so rather than warning about unused flags that are
6245 // actually used, we'll lie by omission instead.
6246 // FIXME: Stop lying and consume only the appropriate driver flags
6247 Args.ClaimAllArgs(options::OPT_W_Group);
6248
6249 CollectArgsForIntegratedAssembler(C, Args, CmdArgs,
6250 getToolChain().getDriver());
6251
6252 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
6253
6254 assert(Output.isFilename() && "Unexpected lipo output.");
6255 CmdArgs.push_back("-o");
Martin Storsjob547ef22018-10-26 08:33:29 +00006256 CmdArgs.push_back(Output.getFilename());
David L. Jonesf561aba2017-03-08 01:02:16 +00006257
Petr Hosekd3265352018-10-15 21:30:32 +00006258 const llvm::Triple &T = getToolChain().getTriple();
George Rimar91829ee2018-11-14 09:22:16 +00006259 Arg *A;
Fangrui Songee957e02019-03-28 08:24:00 +00006260 if (getDebugFissionKind(D, Args, A) == DwarfFissionKind::Split &&
6261 T.isOSBinFormatELF()) {
Aaron Puchert922759a2019-06-15 14:07:43 +00006262 CmdArgs.push_back("-split-dwarf-output");
George Rimar36d71da2019-03-27 11:00:03 +00006263 CmdArgs.push_back(SplitDebugName(Args, Input, Output));
Peter Collingbourne91d02842018-05-22 18:52:37 +00006264 }
6265
David L. Jonesf561aba2017-03-08 01:02:16 +00006266 assert(Input.isFilename() && "Invalid input.");
Martin Storsjob547ef22018-10-26 08:33:29 +00006267 CmdArgs.push_back(Input.getFilename());
David L. Jonesf561aba2017-03-08 01:02:16 +00006268
6269 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00006270 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
David L. Jonesf561aba2017-03-08 01:02:16 +00006271}
6272
6273// Begin OffloadBundler
6274
6275void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA,
6276 const InputInfo &Output,
6277 const InputInfoList &Inputs,
6278 const llvm::opt::ArgList &TCArgs,
6279 const char *LinkingOutput) const {
6280 // The version with only one output is expected to refer to a bundling job.
6281 assert(isa<OffloadBundlingJobAction>(JA) && "Expecting bundling job!");
6282
6283 // The bundling command looks like this:
6284 // clang-offload-bundler -type=bc
6285 // -targets=host-triple,openmp-triple1,openmp-triple2
6286 // -outputs=input_file
6287 // -inputs=unbundle_file_host,unbundle_file_tgt1,unbundle_file_tgt2"
6288
6289 ArgStringList CmdArgs;
6290
6291 // Get the type.
6292 CmdArgs.push_back(TCArgs.MakeArgString(
6293 Twine("-type=") + types::getTypeTempSuffix(Output.getType())));
6294
6295 assert(JA.getInputs().size() == Inputs.size() &&
6296 "Not have inputs for all dependence actions??");
6297
6298 // Get the targets.
6299 SmallString<128> Triples;
6300 Triples += "-targets=";
6301 for (unsigned I = 0; I < Inputs.size(); ++I) {
6302 if (I)
6303 Triples += ',';
6304
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +00006305 // Find ToolChain for this input.
David L. Jonesf561aba2017-03-08 01:02:16 +00006306 Action::OffloadKind CurKind = Action::OFK_Host;
6307 const ToolChain *CurTC = &getToolChain();
6308 const Action *CurDep = JA.getInputs()[I];
6309
6310 if (const auto *OA = dyn_cast<OffloadAction>(CurDep)) {
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +00006311 CurTC = nullptr;
David L. Jonesf561aba2017-03-08 01:02:16 +00006312 OA->doOnEachDependence([&](Action *A, const ToolChain *TC, const char *) {
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +00006313 assert(CurTC == nullptr && "Expected one dependence!");
David L. Jonesf561aba2017-03-08 01:02:16 +00006314 CurKind = A->getOffloadingDeviceKind();
6315 CurTC = TC;
6316 });
6317 }
6318 Triples += Action::GetOffloadKindName(CurKind);
6319 Triples += '-';
6320 Triples += CurTC->getTriple().normalize();
Yaxun Liu609f7522018-05-11 19:02:18 +00006321 if (CurKind == Action::OFK_HIP && CurDep->getOffloadingArch()) {
6322 Triples += '-';
6323 Triples += CurDep->getOffloadingArch();
6324 }
David L. Jonesf561aba2017-03-08 01:02:16 +00006325 }
6326 CmdArgs.push_back(TCArgs.MakeArgString(Triples));
6327
6328 // Get bundled file command.
6329 CmdArgs.push_back(
6330 TCArgs.MakeArgString(Twine("-outputs=") + Output.getFilename()));
6331
6332 // Get unbundled files command.
6333 SmallString<128> UB;
6334 UB += "-inputs=";
6335 for (unsigned I = 0; I < Inputs.size(); ++I) {
6336 if (I)
6337 UB += ',';
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +00006338
6339 // Find ToolChain for this input.
6340 const ToolChain *CurTC = &getToolChain();
6341 if (const auto *OA = dyn_cast<OffloadAction>(JA.getInputs()[I])) {
6342 CurTC = nullptr;
6343 OA->doOnEachDependence([&](Action *, const ToolChain *TC, const char *) {
6344 assert(CurTC == nullptr && "Expected one dependence!");
6345 CurTC = TC;
6346 });
6347 }
6348 UB += CurTC->getInputFilename(Inputs[I]);
David L. Jonesf561aba2017-03-08 01:02:16 +00006349 }
6350 CmdArgs.push_back(TCArgs.MakeArgString(UB));
6351
6352 // All the inputs are encoded as commands.
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00006353 C.addCommand(std::make_unique<Command>(
David L. Jonesf561aba2017-03-08 01:02:16 +00006354 JA, *this,
6355 TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
6356 CmdArgs, None));
6357}
6358
6359void OffloadBundler::ConstructJobMultipleOutputs(
6360 Compilation &C, const JobAction &JA, const InputInfoList &Outputs,
6361 const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs,
6362 const char *LinkingOutput) const {
6363 // The version with multiple outputs is expected to refer to a unbundling job.
6364 auto &UA = cast<OffloadUnbundlingJobAction>(JA);
6365
6366 // The unbundling command looks like this:
6367 // clang-offload-bundler -type=bc
6368 // -targets=host-triple,openmp-triple1,openmp-triple2
6369 // -inputs=input_file
6370 // -outputs=unbundle_file_host,unbundle_file_tgt1,unbundle_file_tgt2"
6371 // -unbundle
6372
6373 ArgStringList CmdArgs;
6374
6375 assert(Inputs.size() == 1 && "Expecting to unbundle a single file!");
6376 InputInfo Input = Inputs.front();
6377
6378 // Get the type.
6379 CmdArgs.push_back(TCArgs.MakeArgString(
6380 Twine("-type=") + types::getTypeTempSuffix(Input.getType())));
6381
6382 // Get the targets.
6383 SmallString<128> Triples;
6384 Triples += "-targets=";
6385 auto DepInfo = UA.getDependentActionsInfo();
6386 for (unsigned I = 0; I < DepInfo.size(); ++I) {
6387 if (I)
6388 Triples += ',';
6389
6390 auto &Dep = DepInfo[I];
6391 Triples += Action::GetOffloadKindName(Dep.DependentOffloadKind);
6392 Triples += '-';
6393 Triples += Dep.DependentToolChain->getTriple().normalize();
Yaxun Liu609f7522018-05-11 19:02:18 +00006394 if (Dep.DependentOffloadKind == Action::OFK_HIP &&
6395 !Dep.DependentBoundArch.empty()) {
6396 Triples += '-';
6397 Triples += Dep.DependentBoundArch;
6398 }
David L. Jonesf561aba2017-03-08 01:02:16 +00006399 }
6400
6401 CmdArgs.push_back(TCArgs.MakeArgString(Triples));
6402
6403 // Get bundled file command.
6404 CmdArgs.push_back(
6405 TCArgs.MakeArgString(Twine("-inputs=") + Input.getFilename()));
6406
6407 // Get unbundled files command.
6408 SmallString<128> UB;
6409 UB += "-outputs=";
6410 for (unsigned I = 0; I < Outputs.size(); ++I) {
6411 if (I)
6412 UB += ',';
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +00006413 UB += DepInfo[I].DependentToolChain->getInputFilename(Outputs[I]);
David L. Jonesf561aba2017-03-08 01:02:16 +00006414 }
6415 CmdArgs.push_back(TCArgs.MakeArgString(UB));
6416 CmdArgs.push_back("-unbundle");
6417
6418 // All the inputs are encoded as commands.
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00006419 C.addCommand(std::make_unique<Command>(
David L. Jonesf561aba2017-03-08 01:02:16 +00006420 JA, *this,
6421 TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
6422 CmdArgs, None));
6423}