blob: 127f80696220d22bec89d1aac783a7e22f84d818 [file] [log] [blame]
David L. Jonesf561aba2017-03-08 01:02:16 +00001//===--- LLVM.cpp - Clang+LLVM ToolChain Implementations --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Clang.h"
11#include "Arch/AArch64.h"
12#include "Arch/ARM.h"
13#include "Arch/Mips.h"
14#include "Arch/PPC.h"
Alex Bradbury71f45452018-01-11 13:36:56 +000015#include "Arch/RISCV.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000016#include "Arch/Sparc.h"
17#include "Arch/SystemZ.h"
18#include "Arch/X86.h"
Konstantin Zhuravlyov8914a6d2017-11-10 19:09:57 +000019#include "AMDGPU.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000020#include "CommonArgs.h"
21#include "Hexagon.h"
22#include "InputInfo.h"
23#include "PS4CPU.h"
24#include "clang/Basic/CharInfo.h"
25#include "clang/Basic/LangOptions.h"
26#include "clang/Basic/ObjCRuntime.h"
27#include "clang/Basic/Version.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000028#include "clang/Driver/DriverDiagnostic.h"
29#include "clang/Driver/Options.h"
30#include "clang/Driver/SanitizerArgs.h"
Dean Michael Berris835832d2017-03-30 00:29:36 +000031#include "clang/Driver/XRayArgs.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000032#include "llvm/ADT/StringExtras.h"
Nico Weberd637c052018-04-30 13:52:15 +000033#include "llvm/Config/llvm-config.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000034#include "llvm/Option/ArgList.h"
35#include "llvm/Support/CodeGen.h"
36#include "llvm/Support/Compression.h"
37#include "llvm/Support/FileSystem.h"
38#include "llvm/Support/Path.h"
39#include "llvm/Support/Process.h"
Eric Christopher53b2cb72017-06-30 00:03:56 +000040#include "llvm/Support/TargetParser.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000041#include "llvm/Support/YAMLParser.h"
42
43#ifdef LLVM_ON_UNIX
44#include <unistd.h> // For getuid().
45#endif
46
47using namespace clang::driver;
48using namespace clang::driver::tools;
49using namespace clang;
50using namespace llvm::opt;
51
52static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
53 if (Arg *A =
54 Args.getLastArg(clang::driver::options::OPT_C, options::OPT_CC)) {
55 if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_P) &&
56 !Args.hasArg(options::OPT__SLASH_EP) && !D.CCCIsCPP()) {
57 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
58 << A->getBaseArg().getAsString(Args)
59 << (D.IsCLMode() ? "/E, /P or /EP" : "-E");
60 }
61 }
62}
63
64static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
65 // In gcc, only ARM checks this, but it seems reasonable to check universally.
66 if (Args.hasArg(options::OPT_static))
67 if (const Arg *A =
68 Args.getLastArg(options::OPT_dynamic, options::OPT_mdynamic_no_pic))
69 D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args)
70 << "-static";
71}
72
73// Add backslashes to escape spaces and other backslashes.
74// This is used for the space-separated argument list specified with
75// the -dwarf-debug-flags option.
76static void EscapeSpacesAndBackslashes(const char *Arg,
77 SmallVectorImpl<char> &Res) {
78 for (; *Arg; ++Arg) {
79 switch (*Arg) {
80 default:
81 break;
82 case ' ':
83 case '\\':
84 Res.push_back('\\');
85 break;
86 }
87 Res.push_back(*Arg);
88 }
89}
90
91// Quote target names for inclusion in GNU Make dependency files.
92// Only the characters '$', '#', ' ', '\t' are quoted.
93static void QuoteTarget(StringRef Target, SmallVectorImpl<char> &Res) {
94 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
95 switch (Target[i]) {
96 case ' ':
97 case '\t':
98 // Escape the preceding backslashes
99 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
100 Res.push_back('\\');
101
102 // Escape the space/tab
103 Res.push_back('\\');
104 break;
105 case '$':
106 Res.push_back('$');
107 break;
108 case '#':
109 Res.push_back('\\');
110 break;
111 default:
112 break;
113 }
114
115 Res.push_back(Target[i]);
116 }
117}
118
119/// Apply \a Work on the current tool chain \a RegularToolChain and any other
120/// offloading tool chain that is associated with the current action \a JA.
121static void
122forAllAssociatedToolChains(Compilation &C, const JobAction &JA,
123 const ToolChain &RegularToolChain,
124 llvm::function_ref<void(const ToolChain &)> Work) {
125 // Apply Work on the current/regular tool chain.
126 Work(RegularToolChain);
127
128 // Apply Work on all the offloading tool chains associated with the current
129 // action.
130 if (JA.isHostOffloading(Action::OFK_Cuda))
131 Work(*C.getSingleOffloadToolChain<Action::OFK_Cuda>());
132 else if (JA.isDeviceOffloading(Action::OFK_Cuda))
133 Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
Yaxun Liu398612b2018-05-08 21:02:12 +0000134 else if (JA.isHostOffloading(Action::OFK_HIP))
135 Work(*C.getSingleOffloadToolChain<Action::OFK_HIP>());
136 else if (JA.isDeviceOffloading(Action::OFK_HIP))
137 Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
David L. Jonesf561aba2017-03-08 01:02:16 +0000138
Gheorghe-Teodor Bercea59d7b772017-06-29 15:49:03 +0000139 if (JA.isHostOffloading(Action::OFK_OpenMP)) {
140 auto TCs = C.getOffloadToolChains<Action::OFK_OpenMP>();
141 for (auto II = TCs.first, IE = TCs.second; II != IE; ++II)
142 Work(*II->second);
143 } else if (JA.isDeviceOffloading(Action::OFK_OpenMP))
144 Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
145
David L. Jonesf561aba2017-03-08 01:02:16 +0000146 //
147 // TODO: Add support for other offloading programming models here.
148 //
149}
150
151/// This is a helper function for validating the optional refinement step
152/// parameter in reciprocal argument strings. Return false if there is an error
153/// parsing the refinement step. Otherwise, return true and set the Position
154/// of the refinement step in the input string.
155static bool getRefinementStep(StringRef In, const Driver &D,
156 const Arg &A, size_t &Position) {
157 const char RefinementStepToken = ':';
158 Position = In.find(RefinementStepToken);
159 if (Position != StringRef::npos) {
160 StringRef Option = A.getOption().getName();
161 StringRef RefStep = In.substr(Position + 1);
162 // Allow exactly one numeric character for the additional refinement
163 // step parameter. This is reasonable for all currently-supported
164 // operations and architectures because we would expect that a larger value
165 // of refinement steps would cause the estimate "optimization" to
166 // under-perform the native operation. Also, if the estimate does not
167 // converge quickly, it probably will not ever converge, so further
168 // refinement steps will not produce a better answer.
169 if (RefStep.size() != 1) {
170 D.Diag(diag::err_drv_invalid_value) << Option << RefStep;
171 return false;
172 }
173 char RefStepChar = RefStep[0];
174 if (RefStepChar < '0' || RefStepChar > '9') {
175 D.Diag(diag::err_drv_invalid_value) << Option << RefStep;
176 return false;
177 }
178 }
179 return true;
180}
181
182/// The -mrecip flag requires processing of many optional parameters.
183static void ParseMRecip(const Driver &D, const ArgList &Args,
184 ArgStringList &OutStrings) {
185 StringRef DisabledPrefixIn = "!";
186 StringRef DisabledPrefixOut = "!";
187 StringRef EnabledPrefixOut = "";
188 StringRef Out = "-mrecip=";
189
190 Arg *A = Args.getLastArg(options::OPT_mrecip, options::OPT_mrecip_EQ);
191 if (!A)
192 return;
193
194 unsigned NumOptions = A->getNumValues();
195 if (NumOptions == 0) {
196 // No option is the same as "all".
197 OutStrings.push_back(Args.MakeArgString(Out + "all"));
198 return;
199 }
200
201 // Pass through "all", "none", or "default" with an optional refinement step.
202 if (NumOptions == 1) {
203 StringRef Val = A->getValue(0);
204 size_t RefStepLoc;
205 if (!getRefinementStep(Val, D, *A, RefStepLoc))
206 return;
207 StringRef ValBase = Val.slice(0, RefStepLoc);
208 if (ValBase == "all" || ValBase == "none" || ValBase == "default") {
209 OutStrings.push_back(Args.MakeArgString(Out + Val));
210 return;
211 }
212 }
213
214 // Each reciprocal type may be enabled or disabled individually.
215 // Check each input value for validity, concatenate them all back together,
216 // and pass through.
217
218 llvm::StringMap<bool> OptionStrings;
219 OptionStrings.insert(std::make_pair("divd", false));
220 OptionStrings.insert(std::make_pair("divf", false));
221 OptionStrings.insert(std::make_pair("vec-divd", false));
222 OptionStrings.insert(std::make_pair("vec-divf", false));
223 OptionStrings.insert(std::make_pair("sqrtd", false));
224 OptionStrings.insert(std::make_pair("sqrtf", false));
225 OptionStrings.insert(std::make_pair("vec-sqrtd", false));
226 OptionStrings.insert(std::make_pair("vec-sqrtf", false));
227
228 for (unsigned i = 0; i != NumOptions; ++i) {
229 StringRef Val = A->getValue(i);
230
231 bool IsDisabled = Val.startswith(DisabledPrefixIn);
232 // Ignore the disablement token for string matching.
233 if (IsDisabled)
234 Val = Val.substr(1);
235
236 size_t RefStep;
237 if (!getRefinementStep(Val, D, *A, RefStep))
238 return;
239
240 StringRef ValBase = Val.slice(0, RefStep);
241 llvm::StringMap<bool>::iterator OptionIter = OptionStrings.find(ValBase);
242 if (OptionIter == OptionStrings.end()) {
243 // Try again specifying float suffix.
244 OptionIter = OptionStrings.find(ValBase.str() + 'f');
245 if (OptionIter == OptionStrings.end()) {
246 // The input name did not match any known option string.
247 D.Diag(diag::err_drv_unknown_argument) << Val;
248 return;
249 }
250 // The option was specified without a float or double suffix.
251 // Make sure that the double entry was not already specified.
252 // The float entry will be checked below.
253 if (OptionStrings[ValBase.str() + 'd']) {
254 D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val;
255 return;
256 }
257 }
258
259 if (OptionIter->second == true) {
260 // Duplicate option specified.
261 D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val;
262 return;
263 }
264
265 // Mark the matched option as found. Do not allow duplicate specifiers.
266 OptionIter->second = true;
267
268 // If the precision was not specified, also mark the double entry as found.
269 if (ValBase.back() != 'f' && ValBase.back() != 'd')
270 OptionStrings[ValBase.str() + 'd'] = true;
271
272 // Build the output string.
273 StringRef Prefix = IsDisabled ? DisabledPrefixOut : EnabledPrefixOut;
274 Out = Args.MakeArgString(Out + Prefix + Val);
275 if (i != NumOptions - 1)
276 Out = Args.MakeArgString(Out + ",");
277 }
278
279 OutStrings.push_back(Args.MakeArgString(Out));
280}
281
Craig Topper9a724aa2017-12-11 21:09:19 +0000282/// The -mprefer-vector-width option accepts either a positive integer
283/// or the string "none".
284static void ParseMPreferVectorWidth(const Driver &D, const ArgList &Args,
285 ArgStringList &CmdArgs) {
286 Arg *A = Args.getLastArg(options::OPT_mprefer_vector_width_EQ);
287 if (!A)
288 return;
289
290 StringRef Value = A->getValue();
291 if (Value == "none") {
292 CmdArgs.push_back("-mprefer-vector-width=none");
293 } else {
294 unsigned Width;
295 if (Value.getAsInteger(10, Width)) {
296 D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value;
297 return;
298 }
299 CmdArgs.push_back(Args.MakeArgString("-mprefer-vector-width=" + Value));
300 }
301}
302
David L. Jonesf561aba2017-03-08 01:02:16 +0000303static void getWebAssemblyTargetFeatures(const ArgList &Args,
304 std::vector<StringRef> &Features) {
305 handleTargetFeaturesGroup(Args, Features, options::OPT_m_wasm_Features_Group);
306}
307
David L. Jonesf561aba2017-03-08 01:02:16 +0000308static void getTargetFeatures(const ToolChain &TC, const llvm::Triple &Triple,
309 const ArgList &Args, ArgStringList &CmdArgs,
310 bool ForAS) {
311 const Driver &D = TC.getDriver();
312 std::vector<StringRef> Features;
313 switch (Triple.getArch()) {
314 default:
315 break;
316 case llvm::Triple::mips:
317 case llvm::Triple::mipsel:
318 case llvm::Triple::mips64:
319 case llvm::Triple::mips64el:
320 mips::getMIPSTargetFeatures(D, Triple, Args, Features);
321 break;
322
323 case llvm::Triple::arm:
324 case llvm::Triple::armeb:
325 case llvm::Triple::thumb:
326 case llvm::Triple::thumbeb:
327 arm::getARMTargetFeatures(TC, Triple, Args, CmdArgs, Features, ForAS);
328 break;
329
330 case llvm::Triple::ppc:
331 case llvm::Triple::ppc64:
332 case llvm::Triple::ppc64le:
333 ppc::getPPCTargetFeatures(D, Triple, Args, Features);
334 break;
Alex Bradbury71f45452018-01-11 13:36:56 +0000335 case llvm::Triple::riscv32:
336 case llvm::Triple::riscv64:
337 riscv::getRISCVTargetFeatures(D, Args, Features);
338 break;
David L. Jonesf561aba2017-03-08 01:02:16 +0000339 case llvm::Triple::systemz:
340 systemz::getSystemZTargetFeatures(Args, Features);
341 break;
342 case llvm::Triple::aarch64:
343 case llvm::Triple::aarch64_be:
344 aarch64::getAArch64TargetFeatures(D, Args, Features);
345 break;
346 case llvm::Triple::x86:
347 case llvm::Triple::x86_64:
348 x86::getX86TargetFeatures(D, Triple, Args, Features);
349 break;
350 case llvm::Triple::hexagon:
Sumanth Gundapaneni57098f52017-10-18 18:10:13 +0000351 hexagon::getHexagonTargetFeatures(D, Args, Features);
David L. Jonesf561aba2017-03-08 01:02:16 +0000352 break;
353 case llvm::Triple::wasm32:
354 case llvm::Triple::wasm64:
355 getWebAssemblyTargetFeatures(Args, Features);
356 break;
357 case llvm::Triple::sparc:
358 case llvm::Triple::sparcel:
359 case llvm::Triple::sparcv9:
360 sparc::getSparcTargetFeatures(D, Args, Features);
361 break;
362 case llvm::Triple::r600:
363 case llvm::Triple::amdgcn:
Konstantin Zhuravlyov8914a6d2017-11-10 19:09:57 +0000364 amdgpu::getAMDGPUTargetFeatures(D, Args, Features);
David L. Jonesf561aba2017-03-08 01:02:16 +0000365 break;
366 }
367
368 // Find the last of each feature.
369 llvm::StringMap<unsigned> LastOpt;
370 for (unsigned I = 0, N = Features.size(); I < N; ++I) {
371 StringRef Name = Features[I];
372 assert(Name[0] == '-' || Name[0] == '+');
373 LastOpt[Name.drop_front(1)] = I;
374 }
375
376 for (unsigned I = 0, N = Features.size(); I < N; ++I) {
377 // If this feature was overridden, ignore it.
378 StringRef Name = Features[I];
379 llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name.drop_front(1));
380 assert(LastI != LastOpt.end());
381 unsigned Last = LastI->second;
382 if (Last != I)
383 continue;
384
385 CmdArgs.push_back("-target-feature");
386 CmdArgs.push_back(Name.data());
387 }
388}
389
390static bool
391shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
392 const llvm::Triple &Triple) {
393 // We use the zero-cost exception tables for Objective-C if the non-fragile
394 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
395 // later.
396 if (runtime.isNonFragile())
397 return true;
398
399 if (!Triple.isMacOSX())
400 return false;
401
402 return (!Triple.isMacOSXVersionLT(10, 5) &&
403 (Triple.getArch() == llvm::Triple::x86_64 ||
404 Triple.getArch() == llvm::Triple::arm));
405}
406
407/// Adds exception related arguments to the driver command arguments. There's a
408/// master flag, -fexceptions and also language specific flags to enable/disable
409/// C++ and Objective-C exceptions. This makes it possible to for example
410/// disable C++ exceptions but enable Objective-C exceptions.
411static void addExceptionArgs(const ArgList &Args, types::ID InputType,
412 const ToolChain &TC, bool KernelOrKext,
413 const ObjCRuntime &objcRuntime,
414 ArgStringList &CmdArgs) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000415 const llvm::Triple &Triple = TC.getTriple();
416
417 if (KernelOrKext) {
418 // -mkernel and -fapple-kext imply no exceptions, so claim exception related
419 // arguments now to avoid warnings about unused arguments.
420 Args.ClaimAllArgs(options::OPT_fexceptions);
421 Args.ClaimAllArgs(options::OPT_fno_exceptions);
422 Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
423 Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
424 Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
425 Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
426 return;
427 }
428
429 // See if the user explicitly enabled exceptions.
430 bool EH = Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
431 false);
432
433 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
434 // is not necessarily sensible, but follows GCC.
435 if (types::isObjC(InputType) &&
436 Args.hasFlag(options::OPT_fobjc_exceptions,
437 options::OPT_fno_objc_exceptions, true)) {
438 CmdArgs.push_back("-fobjc-exceptions");
439
440 EH |= shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
441 }
442
443 if (types::isCXX(InputType)) {
444 // Disable C++ EH by default on XCore and PS4.
445 bool CXXExceptionsEnabled =
446 Triple.getArch() != llvm::Triple::xcore && !Triple.isPS4CPU();
447 Arg *ExceptionArg = Args.getLastArg(
448 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
449 options::OPT_fexceptions, options::OPT_fno_exceptions);
450 if (ExceptionArg)
451 CXXExceptionsEnabled =
452 ExceptionArg->getOption().matches(options::OPT_fcxx_exceptions) ||
453 ExceptionArg->getOption().matches(options::OPT_fexceptions);
454
455 if (CXXExceptionsEnabled) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000456 CmdArgs.push_back("-fcxx-exceptions");
457
458 EH = true;
459 }
460 }
461
462 if (EH)
463 CmdArgs.push_back("-fexceptions");
464}
465
466static bool ShouldDisableAutolink(const ArgList &Args, const ToolChain &TC) {
467 bool Default = true;
468 if (TC.getTriple().isOSDarwin()) {
469 // The native darwin assembler doesn't support the linker_option directives,
470 // so we disable them if we think the .s file will be passed to it.
471 Default = TC.useIntegratedAs();
472 }
473 return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
474 Default);
475}
476
477static bool ShouldDisableDwarfDirectory(const ArgList &Args,
478 const ToolChain &TC) {
479 bool UseDwarfDirectory =
480 Args.hasFlag(options::OPT_fdwarf_directory_asm,
481 options::OPT_fno_dwarf_directory_asm, TC.useIntegratedAs());
482 return !UseDwarfDirectory;
483}
484
485// Convert an arg of the form "-gN" or "-ggdbN" or one of their aliases
486// to the corresponding DebugInfoKind.
487static codegenoptions::DebugInfoKind DebugLevelToInfoKind(const Arg &A) {
488 assert(A.getOption().matches(options::OPT_gN_Group) &&
489 "Not a -g option that specifies a debug-info level");
490 if (A.getOption().matches(options::OPT_g0) ||
491 A.getOption().matches(options::OPT_ggdb0))
492 return codegenoptions::NoDebugInfo;
493 if (A.getOption().matches(options::OPT_gline_tables_only) ||
494 A.getOption().matches(options::OPT_ggdb1))
495 return codegenoptions::DebugLineTablesOnly;
Alexey Bataev80e1b5e2018-08-31 13:56:14 +0000496 if (A.getOption().matches(options::OPT_gline_directives_only))
497 return codegenoptions::DebugDirectivesOnly;
David L. Jonesf561aba2017-03-08 01:02:16 +0000498 return codegenoptions::LimitedDebugInfo;
499}
500
501static bool mustUseNonLeafFramePointerForTarget(const llvm::Triple &Triple) {
502 switch (Triple.getArch()){
503 default:
504 return false;
505 case llvm::Triple::arm:
506 case llvm::Triple::thumb:
507 // ARM Darwin targets require a frame pointer to be always present to aid
508 // offline debugging via backtraces.
509 return Triple.isOSDarwin();
510 }
511}
512
513static bool useFramePointerForTargetByDefault(const ArgList &Args,
514 const llvm::Triple &Triple) {
515 switch (Triple.getArch()) {
516 case llvm::Triple::xcore:
517 case llvm::Triple::wasm32:
518 case llvm::Triple::wasm64:
519 // XCore never wants frame pointers, regardless of OS.
520 // WebAssembly never wants frame pointers.
521 return false;
Mandeep Singh Grang0c5300a2018-04-12 19:31:37 +0000522 case llvm::Triple::riscv32:
523 case llvm::Triple::riscv64:
524 return !areOptimizationsEnabled(Args);
David L. Jonesf561aba2017-03-08 01:02:16 +0000525 default:
526 break;
527 }
528
Joerg Sonnenberger2ad82102018-07-17 12:38:57 +0000529 if (Triple.getOS() == llvm::Triple::NetBSD) {
530 return !areOptimizationsEnabled(Args);
531 }
532
David L. Jonesf561aba2017-03-08 01:02:16 +0000533 if (Triple.isOSLinux() || Triple.getOS() == llvm::Triple::CloudABI) {
534 switch (Triple.getArch()) {
535 // Don't use a frame pointer on linux if optimizing for certain targets.
536 case llvm::Triple::mips64:
537 case llvm::Triple::mips64el:
538 case llvm::Triple::mips:
539 case llvm::Triple::mipsel:
540 case llvm::Triple::ppc:
541 case llvm::Triple::ppc64:
542 case llvm::Triple::ppc64le:
543 case llvm::Triple::systemz:
544 case llvm::Triple::x86:
545 case llvm::Triple::x86_64:
546 return !areOptimizationsEnabled(Args);
547 default:
548 return true;
549 }
550 }
551
552 if (Triple.isOSWindows()) {
553 switch (Triple.getArch()) {
554 case llvm::Triple::x86:
555 return !areOptimizationsEnabled(Args);
556 case llvm::Triple::x86_64:
557 return Triple.isOSBinFormatMachO();
558 case llvm::Triple::arm:
559 case llvm::Triple::thumb:
560 // Windows on ARM builds with FPO disabled to aid fast stack walking
561 return true;
562 default:
563 // All other supported Windows ISAs use xdata unwind information, so frame
564 // pointers are not generally useful.
565 return false;
566 }
567 }
568
569 return true;
570}
571
572static bool shouldUseFramePointer(const ArgList &Args,
573 const llvm::Triple &Triple) {
574 if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
575 options::OPT_fomit_frame_pointer))
576 return A->getOption().matches(options::OPT_fno_omit_frame_pointer) ||
577 mustUseNonLeafFramePointerForTarget(Triple);
578
579 if (Args.hasArg(options::OPT_pg))
580 return true;
581
582 return useFramePointerForTargetByDefault(Args, Triple);
583}
584
585static bool shouldUseLeafFramePointer(const ArgList &Args,
586 const llvm::Triple &Triple) {
587 if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer,
588 options::OPT_momit_leaf_frame_pointer))
589 return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer);
590
591 if (Args.hasArg(options::OPT_pg))
592 return true;
593
594 if (Triple.isPS4CPU())
595 return false;
596
597 return useFramePointerForTargetByDefault(Args, Triple);
598}
599
600/// Add a CC1 option to specify the debug compilation directory.
601static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
602 SmallString<128> cwd;
603 if (!llvm::sys::fs::current_path(cwd)) {
604 CmdArgs.push_back("-fdebug-compilation-dir");
605 CmdArgs.push_back(Args.MakeArgString(cwd));
606 }
607}
608
Paul Robinson9b292b42018-07-10 15:15:24 +0000609/// Add a CC1 and CC1AS option to specify the debug file path prefix map.
610static void addDebugPrefixMapArg(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs) {
611 for (const Arg *A : Args.filtered(options::OPT_fdebug_prefix_map_EQ)) {
612 StringRef Map = A->getValue();
613 if (Map.find('=') == StringRef::npos)
614 D.Diag(diag::err_drv_invalid_argument_to_fdebug_prefix_map) << Map;
615 else
616 CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map));
617 A->claim();
618 }
619}
620
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000621/// Vectorize at all optimization levels greater than 1 except for -Oz.
David L. Jonesf561aba2017-03-08 01:02:16 +0000622/// For -Oz the loop vectorizer is disable, while the slp vectorizer is enabled.
623static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) {
624 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
625 if (A->getOption().matches(options::OPT_O4) ||
626 A->getOption().matches(options::OPT_Ofast))
627 return true;
628
629 if (A->getOption().matches(options::OPT_O0))
630 return false;
631
632 assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
633
634 // Vectorize -Os.
635 StringRef S(A->getValue());
636 if (S == "s")
637 return true;
638
639 // Don't vectorize -Oz, unless it's the slp vectorizer.
640 if (S == "z")
641 return isSlpVec;
642
643 unsigned OptLevel = 0;
644 if (S.getAsInteger(10, OptLevel))
645 return false;
646
647 return OptLevel > 1;
648 }
649
650 return false;
651}
652
653/// Add -x lang to \p CmdArgs for \p Input.
654static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
655 ArgStringList &CmdArgs) {
656 // When using -verify-pch, we don't want to provide the type
657 // 'precompiled-header' if it was inferred from the file extension
658 if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH)
659 return;
660
661 CmdArgs.push_back("-x");
662 if (Args.hasArg(options::OPT_rewrite_objc))
663 CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
Richard Smith34e485f2017-04-18 21:55:37 +0000664 else {
665 // Map the driver type to the frontend type. This is mostly an identity
666 // mapping, except that the distinction between module interface units
667 // and other source files does not exist at the frontend layer.
668 const char *ClangType;
669 switch (Input.getType()) {
670 case types::TY_CXXModule:
671 ClangType = "c++";
672 break;
673 case types::TY_PP_CXXModule:
674 ClangType = "c++-cpp-output";
675 break;
676 default:
677 ClangType = types::getTypeName(Input.getType());
678 break;
679 }
680 CmdArgs.push_back(ClangType);
681 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000682}
683
684static void appendUserToPath(SmallVectorImpl<char> &Result) {
685#ifdef LLVM_ON_UNIX
686 const char *Username = getenv("LOGNAME");
687#else
688 const char *Username = getenv("USERNAME");
689#endif
690 if (Username) {
691 // Validate that LoginName can be used in a path, and get its length.
692 size_t Len = 0;
693 for (const char *P = Username; *P; ++P, ++Len) {
694 if (!clang::isAlphanumeric(*P) && *P != '_') {
695 Username = nullptr;
696 break;
697 }
698 }
699
700 if (Username && Len > 0) {
701 Result.append(Username, Username + Len);
702 return;
703 }
704 }
705
706// Fallback to user id.
707#ifdef LLVM_ON_UNIX
708 std::string UID = llvm::utostr(getuid());
709#else
710 // FIXME: Windows seems to have an 'SID' that might work.
711 std::string UID = "9999";
712#endif
713 Result.append(UID.begin(), UID.end());
714}
715
716static void addPGOAndCoverageFlags(Compilation &C, const Driver &D,
717 const InputInfo &Output, const ArgList &Args,
718 ArgStringList &CmdArgs) {
719
720 auto *PGOGenerateArg = Args.getLastArg(options::OPT_fprofile_generate,
721 options::OPT_fprofile_generate_EQ,
722 options::OPT_fno_profile_generate);
723 if (PGOGenerateArg &&
724 PGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate))
725 PGOGenerateArg = nullptr;
726
727 auto *ProfileGenerateArg = Args.getLastArg(
728 options::OPT_fprofile_instr_generate,
729 options::OPT_fprofile_instr_generate_EQ,
730 options::OPT_fno_profile_instr_generate);
731 if (ProfileGenerateArg &&
732 ProfileGenerateArg->getOption().matches(
733 options::OPT_fno_profile_instr_generate))
734 ProfileGenerateArg = nullptr;
735
736 if (PGOGenerateArg && ProfileGenerateArg)
737 D.Diag(diag::err_drv_argument_not_allowed_with)
738 << PGOGenerateArg->getSpelling() << ProfileGenerateArg->getSpelling();
739
740 auto *ProfileUseArg = getLastProfileUseArg(Args);
741
742 if (PGOGenerateArg && ProfileUseArg)
743 D.Diag(diag::err_drv_argument_not_allowed_with)
744 << ProfileUseArg->getSpelling() << PGOGenerateArg->getSpelling();
745
746 if (ProfileGenerateArg && ProfileUseArg)
747 D.Diag(diag::err_drv_argument_not_allowed_with)
748 << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling();
749
750 if (ProfileGenerateArg) {
751 if (ProfileGenerateArg->getOption().matches(
752 options::OPT_fprofile_instr_generate_EQ))
753 CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-instrument-path=") +
754 ProfileGenerateArg->getValue()));
755 // The default is to use Clang Instrumentation.
756 CmdArgs.push_back("-fprofile-instrument=clang");
757 }
758
759 if (PGOGenerateArg) {
760 CmdArgs.push_back("-fprofile-instrument=llvm");
761 if (PGOGenerateArg->getOption().matches(
762 options::OPT_fprofile_generate_EQ)) {
763 SmallString<128> Path(PGOGenerateArg->getValue());
764 llvm::sys::path::append(Path, "default_%m.profraw");
765 CmdArgs.push_back(
766 Args.MakeArgString(Twine("-fprofile-instrument-path=") + Path));
767 }
768 }
769
770 if (ProfileUseArg) {
771 if (ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ))
772 CmdArgs.push_back(Args.MakeArgString(
773 Twine("-fprofile-instrument-use-path=") + ProfileUseArg->getValue()));
774 else if ((ProfileUseArg->getOption().matches(
775 options::OPT_fprofile_use_EQ) ||
776 ProfileUseArg->getOption().matches(
777 options::OPT_fprofile_instr_use))) {
778 SmallString<128> Path(
779 ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue());
780 if (Path.empty() || llvm::sys::fs::is_directory(Path))
781 llvm::sys::path::append(Path, "default.profdata");
782 CmdArgs.push_back(
783 Args.MakeArgString(Twine("-fprofile-instrument-use-path=") + Path));
784 }
785 }
786
787 if (Args.hasArg(options::OPT_ftest_coverage) ||
788 Args.hasArg(options::OPT_coverage))
789 CmdArgs.push_back("-femit-coverage-notes");
790 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
791 false) ||
792 Args.hasArg(options::OPT_coverage))
793 CmdArgs.push_back("-femit-coverage-data");
794
795 if (Args.hasFlag(options::OPT_fcoverage_mapping,
Vedant Kumar99b31292017-06-28 01:56:07 +0000796 options::OPT_fno_coverage_mapping, false)) {
797 if (!ProfileGenerateArg)
798 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
799 << "-fcoverage-mapping"
800 << "-fprofile-instr-generate";
David L. Jonesf561aba2017-03-08 01:02:16 +0000801
David L. Jonesf561aba2017-03-08 01:02:16 +0000802 CmdArgs.push_back("-fcoverage-mapping");
Vedant Kumar99b31292017-06-28 01:56:07 +0000803 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000804
805 if (C.getArgs().hasArg(options::OPT_c) ||
806 C.getArgs().hasArg(options::OPT_S)) {
807 if (Output.isFilename()) {
808 CmdArgs.push_back("-coverage-notes-file");
809 SmallString<128> OutputFilename;
810 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
811 OutputFilename = FinalOutput->getValue();
812 else
813 OutputFilename = llvm::sys::path::filename(Output.getBaseInput());
814 SmallString<128> CoverageFilename = OutputFilename;
815 if (llvm::sys::path::is_relative(CoverageFilename)) {
816 SmallString<128> Pwd;
817 if (!llvm::sys::fs::current_path(Pwd)) {
818 llvm::sys::path::append(Pwd, CoverageFilename);
819 CoverageFilename.swap(Pwd);
820 }
821 }
822 llvm::sys::path::replace_extension(CoverageFilename, "gcno");
823 CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
824
825 // Leave -fprofile-dir= an unused argument unless .gcda emission is
826 // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider
827 // the flag used. There is no -fno-profile-dir, so the user has no
828 // targeted way to suppress the warning.
829 if (Args.hasArg(options::OPT_fprofile_arcs) ||
830 Args.hasArg(options::OPT_coverage)) {
831 CmdArgs.push_back("-coverage-data-file");
832 if (Arg *FProfileDir = Args.getLastArg(options::OPT_fprofile_dir)) {
833 CoverageFilename = FProfileDir->getValue();
834 llvm::sys::path::append(CoverageFilename, OutputFilename);
835 }
836 llvm::sys::path::replace_extension(CoverageFilename, "gcda");
837 CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
838 }
839 }
840 }
841}
842
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000843/// Check whether the given input tree contains any compilation actions.
David L. Jonesf561aba2017-03-08 01:02:16 +0000844static bool ContainsCompileAction(const Action *A) {
845 if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A))
846 return true;
847
848 for (const auto &AI : A->inputs())
849 if (ContainsCompileAction(AI))
850 return true;
851
852 return false;
853}
854
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000855/// Check if -relax-all should be passed to the internal assembler.
David L. Jonesf561aba2017-03-08 01:02:16 +0000856/// This is done by default when compiling non-assembler source with -O0.
857static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
858 bool RelaxDefault = true;
859
860 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
861 RelaxDefault = A->getOption().matches(options::OPT_O0);
862
863 if (RelaxDefault) {
864 RelaxDefault = false;
865 for (const auto &Act : C.getActions()) {
866 if (ContainsCompileAction(Act)) {
867 RelaxDefault = true;
868 break;
869 }
870 }
871 }
872
873 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
874 RelaxDefault);
875}
876
877// Extract the integer N from a string spelled "-dwarf-N", returning 0
878// on mismatch. The StringRef input (rather than an Arg) allows
879// for use by the "-Xassembler" option parser.
880static unsigned DwarfVersionNum(StringRef ArgValue) {
881 return llvm::StringSwitch<unsigned>(ArgValue)
882 .Case("-gdwarf-2", 2)
883 .Case("-gdwarf-3", 3)
884 .Case("-gdwarf-4", 4)
885 .Case("-gdwarf-5", 5)
886 .Default(0);
887}
888
889static void RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs,
890 codegenoptions::DebugInfoKind DebugInfoKind,
891 unsigned DwarfVersion,
892 llvm::DebuggerKind DebuggerTuning) {
893 switch (DebugInfoKind) {
Alexey Bataev80e1b5e2018-08-31 13:56:14 +0000894 case codegenoptions::DebugDirectivesOnly:
895 CmdArgs.push_back("-debug-info-kind=line-directives-only");
896 break;
David L. Jonesf561aba2017-03-08 01:02:16 +0000897 case codegenoptions::DebugLineTablesOnly:
898 CmdArgs.push_back("-debug-info-kind=line-tables-only");
899 break;
900 case codegenoptions::LimitedDebugInfo:
901 CmdArgs.push_back("-debug-info-kind=limited");
902 break;
903 case codegenoptions::FullDebugInfo:
904 CmdArgs.push_back("-debug-info-kind=standalone");
905 break;
906 default:
907 break;
908 }
909 if (DwarfVersion > 0)
910 CmdArgs.push_back(
911 Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion)));
912 switch (DebuggerTuning) {
913 case llvm::DebuggerKind::GDB:
914 CmdArgs.push_back("-debugger-tuning=gdb");
915 break;
916 case llvm::DebuggerKind::LLDB:
917 CmdArgs.push_back("-debugger-tuning=lldb");
918 break;
919 case llvm::DebuggerKind::SCE:
920 CmdArgs.push_back("-debugger-tuning=sce");
921 break;
922 default:
923 break;
924 }
925}
926
Alexey Bataevb83b4e42018-07-27 19:45:14 +0000927static bool checkDebugInfoOption(const Arg *A, const ArgList &Args,
928 const Driver &D, const ToolChain &TC) {
929 assert(A && "Expected non-nullptr argument.");
930 if (TC.supportsDebugInfoOption(A))
931 return true;
932 D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target)
933 << A->getAsString(Args) << TC.getTripleString();
934 return false;
935}
936
Saleem Abdulrasoold064e912017-06-23 15:34:16 +0000937static void RenderDebugInfoCompressionArgs(const ArgList &Args,
938 ArgStringList &CmdArgs,
Alexey Bataevb83b4e42018-07-27 19:45:14 +0000939 const Driver &D,
940 const ToolChain &TC) {
Saleem Abdulrasoold064e912017-06-23 15:34:16 +0000941 const Arg *A = Args.getLastArg(options::OPT_gz, options::OPT_gz_EQ);
942 if (!A)
943 return;
Alexey Bataevb83b4e42018-07-27 19:45:14 +0000944 if (checkDebugInfoOption(A, Args, D, TC)) {
945 if (A->getOption().getID() == options::OPT_gz) {
946 if (llvm::zlib::isAvailable())
947 CmdArgs.push_back("-compress-debug-sections");
948 else
949 D.Diag(diag::warn_debug_compression_unavailable);
950 return;
Saleem Abdulrasoold064e912017-06-23 15:34:16 +0000951 }
Alexey Bataevb83b4e42018-07-27 19:45:14 +0000952
953 StringRef Value = A->getValue();
954 if (Value == "none") {
955 CmdArgs.push_back("-compress-debug-sections=none");
956 } else if (Value == "zlib" || Value == "zlib-gnu") {
957 if (llvm::zlib::isAvailable()) {
958 CmdArgs.push_back(
959 Args.MakeArgString("-compress-debug-sections=" + Twine(Value)));
960 } else {
961 D.Diag(diag::warn_debug_compression_unavailable);
962 }
963 } else {
964 D.Diag(diag::err_drv_unsupported_option_argument)
965 << A->getOption().getName() << Value;
966 }
Saleem Abdulrasoold064e912017-06-23 15:34:16 +0000967 }
968}
969
David L. Jonesf561aba2017-03-08 01:02:16 +0000970static const char *RelocationModelName(llvm::Reloc::Model Model) {
971 switch (Model) {
972 case llvm::Reloc::Static:
973 return "static";
974 case llvm::Reloc::PIC_:
975 return "pic";
976 case llvm::Reloc::DynamicNoPIC:
977 return "dynamic-no-pic";
978 case llvm::Reloc::ROPI:
979 return "ropi";
980 case llvm::Reloc::RWPI:
981 return "rwpi";
982 case llvm::Reloc::ROPI_RWPI:
983 return "ropi-rwpi";
984 }
985 llvm_unreachable("Unknown Reloc::Model kind");
986}
987
988void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
989 const Driver &D, const ArgList &Args,
990 ArgStringList &CmdArgs,
991 const InputInfo &Output,
992 const InputInfoList &Inputs) const {
993 Arg *A;
994 const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU();
995
996 CheckPreprocessingOptions(D, Args);
997
998 Args.AddLastArg(CmdArgs, options::OPT_C);
999 Args.AddLastArg(CmdArgs, options::OPT_CC);
1000
1001 // Handle dependency file generation.
1002 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
1003 (A = Args.getLastArg(options::OPT_MD)) ||
1004 (A = Args.getLastArg(options::OPT_MMD))) {
1005 // Determine the output location.
1006 const char *DepFile;
1007 if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
1008 DepFile = MF->getValue();
1009 C.addFailureResultFile(DepFile, &JA);
1010 } else if (Output.getType() == types::TY_Dependencies) {
1011 DepFile = Output.getFilename();
1012 } else if (A->getOption().matches(options::OPT_M) ||
1013 A->getOption().matches(options::OPT_MM)) {
1014 DepFile = "-";
1015 } else {
1016 DepFile = getDependencyFileName(Args, Inputs);
1017 C.addFailureResultFile(DepFile, &JA);
1018 }
1019 CmdArgs.push_back("-dependency-file");
1020 CmdArgs.push_back(DepFile);
1021
1022 // Add a default target if one wasn't specified.
1023 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
1024 const char *DepTarget;
1025
1026 // If user provided -o, that is the dependency target, except
1027 // when we are only generating a dependency file.
1028 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
1029 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
1030 DepTarget = OutputOpt->getValue();
1031 } else {
1032 // Otherwise derive from the base input.
1033 //
1034 // FIXME: This should use the computed output file location.
1035 SmallString<128> P(Inputs[0].getBaseInput());
1036 llvm::sys::path::replace_extension(P, "o");
1037 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
1038 }
1039
Yuka Takahashicdb53482017-06-16 16:01:13 +00001040 if (!A->getOption().matches(options::OPT_MD) && !A->getOption().matches(options::OPT_MMD)) {
1041 CmdArgs.push_back("-w");
1042 }
David L. Jonesf561aba2017-03-08 01:02:16 +00001043 CmdArgs.push_back("-MT");
1044 SmallString<128> Quoted;
1045 QuoteTarget(DepTarget, Quoted);
1046 CmdArgs.push_back(Args.MakeArgString(Quoted));
1047 }
1048
1049 if (A->getOption().matches(options::OPT_M) ||
1050 A->getOption().matches(options::OPT_MD))
1051 CmdArgs.push_back("-sys-header-deps");
1052 if ((isa<PrecompileJobAction>(JA) &&
1053 !Args.hasArg(options::OPT_fno_module_file_deps)) ||
1054 Args.hasArg(options::OPT_fmodule_file_deps))
1055 CmdArgs.push_back("-module-file-deps");
1056 }
1057
1058 if (Args.hasArg(options::OPT_MG)) {
1059 if (!A || A->getOption().matches(options::OPT_MD) ||
1060 A->getOption().matches(options::OPT_MMD))
1061 D.Diag(diag::err_drv_mg_requires_m_or_mm);
1062 CmdArgs.push_back("-MG");
1063 }
1064
1065 Args.AddLastArg(CmdArgs, options::OPT_MP);
1066 Args.AddLastArg(CmdArgs, options::OPT_MV);
1067
1068 // Convert all -MQ <target> args to -MT <quoted target>
1069 for (const Arg *A : Args.filtered(options::OPT_MT, options::OPT_MQ)) {
1070 A->claim();
1071
1072 if (A->getOption().matches(options::OPT_MQ)) {
1073 CmdArgs.push_back("-MT");
1074 SmallString<128> Quoted;
1075 QuoteTarget(A->getValue(), Quoted);
1076 CmdArgs.push_back(Args.MakeArgString(Quoted));
1077
1078 // -MT flag - no change
1079 } else {
1080 A->render(Args, CmdArgs);
1081 }
1082 }
1083
1084 // Add offload include arguments specific for CUDA. This must happen before
1085 // we -I or -include anything else, because we must pick up the CUDA headers
1086 // from the particular CUDA installation, rather than from e.g.
1087 // /usr/local/include.
1088 if (JA.isOffloading(Action::OFK_Cuda))
1089 getToolChain().AddCudaIncludeArgs(Args, CmdArgs);
1090
1091 // Add -i* options, and automatically translate to
1092 // -include-pch/-include-pth for transparent PCH support. It's
1093 // wonky, but we include looking for .gch so we can support seamless
1094 // replacement into a build system already set up to be generating
1095 // .gch files.
Erich Keane76675de2018-07-05 17:22:13 +00001096
1097 if (getToolChain().getDriver().IsCLMode()) {
David L. Jonesf561aba2017-03-08 01:02:16 +00001098 const Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc);
1099 const Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu);
Erich Keane76675de2018-07-05 17:22:13 +00001100 if (YcArg && JA.getKind() >= Action::PrecompileJobClass &&
1101 JA.getKind() <= Action::AssembleJobClass) {
1102 CmdArgs.push_back(Args.MakeArgString("-building-pch-with-obj"));
David L. Jonesf561aba2017-03-08 01:02:16 +00001103 }
Erich Keane76675de2018-07-05 17:22:13 +00001104 if (YcArg || YuArg) {
1105 StringRef ThroughHeader = YcArg ? YcArg->getValue() : YuArg->getValue();
1106 if (!isa<PrecompileJobAction>(JA)) {
1107 CmdArgs.push_back("-include-pch");
Mike Rice58df1af2018-09-11 17:10:44 +00001108 CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath(
1109 C, !ThroughHeader.empty()
1110 ? ThroughHeader
1111 : llvm::sys::path::filename(Inputs[0].getBaseInput()))));
Erich Keane76675de2018-07-05 17:22:13 +00001112 }
Mike Rice58df1af2018-09-11 17:10:44 +00001113
1114 if (ThroughHeader.empty()) {
1115 CmdArgs.push_back(Args.MakeArgString(
1116 Twine("-pch-through-hdrstop-") + (YcArg ? "create" : "use")));
1117 } else {
1118 CmdArgs.push_back(
1119 Args.MakeArgString(Twine("-pch-through-header=") + ThroughHeader));
1120 }
Erich Keane76675de2018-07-05 17:22:13 +00001121 }
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001122 }
David L. Jonesf561aba2017-03-08 01:02:16 +00001123
1124 bool RenderedImplicitInclude = false;
David L. Jonesf561aba2017-03-08 01:02:16 +00001125 for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) {
Erich Keane76675de2018-07-05 17:22:13 +00001126 if (A->getOption().matches(options::OPT_include)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00001127 // Handling of gcc-style gch precompiled headers.
1128 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
1129 RenderedImplicitInclude = true;
1130
1131 // Use PCH if the user requested it.
1132 bool UsePCH = D.CCCUsePCH;
1133
1134 bool FoundPTH = false;
1135 bool FoundPCH = false;
1136 SmallString<128> P(A->getValue());
1137 // We want the files to have a name like foo.h.pch. Add a dummy extension
1138 // so that replace_extension does the right thing.
1139 P += ".dummy";
1140 if (UsePCH) {
1141 llvm::sys::path::replace_extension(P, "pch");
1142 if (llvm::sys::fs::exists(P))
1143 FoundPCH = true;
1144 }
1145
1146 if (!FoundPCH) {
1147 llvm::sys::path::replace_extension(P, "pth");
1148 if (llvm::sys::fs::exists(P))
1149 FoundPTH = true;
1150 }
1151
1152 if (!FoundPCH && !FoundPTH) {
1153 llvm::sys::path::replace_extension(P, "gch");
1154 if (llvm::sys::fs::exists(P)) {
1155 FoundPCH = UsePCH;
1156 FoundPTH = !UsePCH;
1157 }
1158 }
1159
1160 if (FoundPCH || FoundPTH) {
1161 if (IsFirstImplicitInclude) {
1162 A->claim();
1163 if (UsePCH)
1164 CmdArgs.push_back("-include-pch");
1165 else
1166 CmdArgs.push_back("-include-pth");
1167 CmdArgs.push_back(Args.MakeArgString(P));
1168 continue;
1169 } else {
1170 // Ignore the PCH if not first on command line and emit warning.
1171 D.Diag(diag::warn_drv_pch_not_first_include) << P
1172 << A->getAsString(Args);
1173 }
1174 }
1175 } else if (A->getOption().matches(options::OPT_isystem_after)) {
1176 // Handling of paths which must come late. These entries are handled by
1177 // the toolchain itself after the resource dir is inserted in the right
1178 // search order.
1179 // Do not claim the argument so that the use of the argument does not
1180 // silently go unnoticed on toolchains which do not honour the option.
1181 continue;
1182 }
1183
1184 // Not translated, render as usual.
1185 A->claim();
1186 A->render(Args, CmdArgs);
1187 }
1188
1189 Args.AddAllArgs(CmdArgs,
1190 {options::OPT_D, options::OPT_U, options::OPT_I_Group,
1191 options::OPT_F, options::OPT_index_header_map});
1192
1193 // Add -Wp, and -Xpreprocessor if using the preprocessor.
1194
1195 // FIXME: There is a very unfortunate problem here, some troubled
1196 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
1197 // really support that we would have to parse and then translate
1198 // those options. :(
1199 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
1200 options::OPT_Xpreprocessor);
1201
1202 // -I- is a deprecated GCC feature, reject it.
1203 if (Arg *A = Args.getLastArg(options::OPT_I_))
1204 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
1205
1206 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
1207 // -isysroot to the CC1 invocation.
1208 StringRef sysroot = C.getSysRoot();
1209 if (sysroot != "") {
1210 if (!Args.hasArg(options::OPT_isysroot)) {
1211 CmdArgs.push_back("-isysroot");
1212 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
1213 }
1214 }
1215
1216 // Parse additional include paths from environment variables.
1217 // FIXME: We should probably sink the logic for handling these from the
1218 // frontend into the driver. It will allow deleting 4 otherwise unused flags.
1219 // CPATH - included following the user specified includes (but prior to
1220 // builtin and standard includes).
1221 addDirectoryList(Args, CmdArgs, "-I", "CPATH");
1222 // C_INCLUDE_PATH - system includes enabled when compiling C.
1223 addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
1224 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
1225 addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
1226 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
1227 addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
1228 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
1229 addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
1230
1231 // While adding the include arguments, we also attempt to retrieve the
1232 // arguments of related offloading toolchains or arguments that are specific
1233 // of an offloading programming model.
1234
1235 // Add C++ include arguments, if needed.
1236 if (types::isCXX(Inputs[0].getType()))
1237 forAllAssociatedToolChains(C, JA, getToolChain(),
1238 [&Args, &CmdArgs](const ToolChain &TC) {
1239 TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
1240 });
1241
1242 // Add system include arguments for all targets but IAMCU.
1243 if (!IsIAMCU)
1244 forAllAssociatedToolChains(C, JA, getToolChain(),
1245 [&Args, &CmdArgs](const ToolChain &TC) {
1246 TC.AddClangSystemIncludeArgs(Args, CmdArgs);
1247 });
1248 else {
1249 // For IAMCU add special include arguments.
1250 getToolChain().AddIAMCUIncludeArgs(Args, CmdArgs);
1251 }
1252}
1253
1254// FIXME: Move to target hook.
1255static bool isSignedCharDefault(const llvm::Triple &Triple) {
1256 switch (Triple.getArch()) {
1257 default:
1258 return true;
1259
1260 case llvm::Triple::aarch64:
1261 case llvm::Triple::aarch64_be:
1262 case llvm::Triple::arm:
1263 case llvm::Triple::armeb:
1264 case llvm::Triple::thumb:
1265 case llvm::Triple::thumbeb:
1266 if (Triple.isOSDarwin() || Triple.isOSWindows())
1267 return true;
1268 return false;
1269
1270 case llvm::Triple::ppc:
1271 case llvm::Triple::ppc64:
1272 if (Triple.isOSDarwin())
1273 return true;
1274 return false;
1275
1276 case llvm::Triple::hexagon:
1277 case llvm::Triple::ppc64le:
Alex Bradbury71f45452018-01-11 13:36:56 +00001278 case llvm::Triple::riscv32:
1279 case llvm::Triple::riscv64:
David L. Jonesf561aba2017-03-08 01:02:16 +00001280 case llvm::Triple::systemz:
1281 case llvm::Triple::xcore:
1282 return false;
1283 }
1284}
1285
1286static bool isNoCommonDefault(const llvm::Triple &Triple) {
1287 switch (Triple.getArch()) {
1288 default:
Petr Hosekbf45ece2018-02-23 20:10:14 +00001289 if (Triple.isOSFuchsia())
1290 return true;
David L. Jonesf561aba2017-03-08 01:02:16 +00001291 return false;
1292
1293 case llvm::Triple::xcore:
1294 case llvm::Triple::wasm32:
1295 case llvm::Triple::wasm64:
1296 return true;
1297 }
1298}
1299
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00001300namespace {
1301void RenderARMABI(const llvm::Triple &Triple, const ArgList &Args,
1302 ArgStringList &CmdArgs) {
David L. Jonesf561aba2017-03-08 01:02:16 +00001303 // Select the ABI to use.
1304 // FIXME: Support -meabi.
1305 // FIXME: Parts of this are duplicated in the backend, unify this somehow.
1306 const char *ABIName = nullptr;
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00001307 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00001308 ABIName = A->getValue();
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00001309 } else {
Daniel Jasperd27538a2017-06-30 08:02:37 +00001310 std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false);
Eric Christopher53b2cb72017-06-30 00:03:56 +00001311 ABIName = llvm::ARM::computeDefaultTargetABI(Triple, CPU).data();
David L. Jonesf561aba2017-03-08 01:02:16 +00001312 }
Eric Christopher53b2cb72017-06-30 00:03:56 +00001313
David L. Jonesf561aba2017-03-08 01:02:16 +00001314 CmdArgs.push_back("-target-abi");
1315 CmdArgs.push_back(ABIName);
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00001316}
1317}
1318
1319void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args,
1320 ArgStringList &CmdArgs, bool KernelOrKext) const {
1321 RenderARMABI(Triple, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00001322
1323 // Determine floating point ABI from the options & target defaults.
1324 arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args);
1325 if (ABI == arm::FloatABI::Soft) {
1326 // Floating point operations and argument passing are soft.
1327 // FIXME: This changes CPP defines, we need -target-soft-float.
1328 CmdArgs.push_back("-msoft-float");
1329 CmdArgs.push_back("-mfloat-abi");
1330 CmdArgs.push_back("soft");
1331 } else if (ABI == arm::FloatABI::SoftFP) {
1332 // Floating point operations are hard, but argument passing is soft.
1333 CmdArgs.push_back("-mfloat-abi");
1334 CmdArgs.push_back("soft");
1335 } else {
1336 // Floating point operations and argument passing are hard.
1337 assert(ABI == arm::FloatABI::Hard && "Invalid float abi!");
1338 CmdArgs.push_back("-mfloat-abi");
1339 CmdArgs.push_back("hard");
1340 }
1341
1342 // Forward the -mglobal-merge option for explicit control over the pass.
1343 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
1344 options::OPT_mno_global_merge)) {
Eli Friedman01d349b2018-04-12 22:21:36 +00001345 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00001346 if (A->getOption().matches(options::OPT_mno_global_merge))
1347 CmdArgs.push_back("-arm-global-merge=false");
1348 else
1349 CmdArgs.push_back("-arm-global-merge=true");
1350 }
1351
1352 if (!Args.hasFlag(options::OPT_mimplicit_float,
1353 options::OPT_mno_implicit_float, true))
1354 CmdArgs.push_back("-no-implicit-float");
1355}
1356
Saleem Abdulrasool6c3ed7b2017-09-03 04:47:00 +00001357void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple,
1358 const ArgList &Args, bool KernelOrKext,
1359 ArgStringList &CmdArgs) const {
1360 const ToolChain &TC = getToolChain();
1361
1362 // Add the target features
1363 getTargetFeatures(TC, EffectiveTriple, Args, CmdArgs, false);
1364
1365 // Add target specific flags.
1366 switch (TC.getArch()) {
1367 default:
1368 break;
1369
1370 case llvm::Triple::arm:
1371 case llvm::Triple::armeb:
1372 case llvm::Triple::thumb:
1373 case llvm::Triple::thumbeb:
1374 // Use the effective triple, which takes into account the deployment target.
1375 AddARMTargetArgs(EffectiveTriple, Args, CmdArgs, KernelOrKext);
1376 CmdArgs.push_back("-fallow-half-arguments-and-returns");
1377 break;
1378
1379 case llvm::Triple::aarch64:
1380 case llvm::Triple::aarch64_be:
1381 AddAArch64TargetArgs(Args, CmdArgs);
1382 CmdArgs.push_back("-fallow-half-arguments-and-returns");
1383 break;
1384
1385 case llvm::Triple::mips:
1386 case llvm::Triple::mipsel:
1387 case llvm::Triple::mips64:
1388 case llvm::Triple::mips64el:
1389 AddMIPSTargetArgs(Args, CmdArgs);
1390 break;
1391
1392 case llvm::Triple::ppc:
1393 case llvm::Triple::ppc64:
1394 case llvm::Triple::ppc64le:
1395 AddPPCTargetArgs(Args, CmdArgs);
1396 break;
1397
Alex Bradbury71f45452018-01-11 13:36:56 +00001398 case llvm::Triple::riscv32:
1399 case llvm::Triple::riscv64:
1400 AddRISCVTargetArgs(Args, CmdArgs);
1401 break;
1402
Saleem Abdulrasool6c3ed7b2017-09-03 04:47:00 +00001403 case llvm::Triple::sparc:
1404 case llvm::Triple::sparcel:
1405 case llvm::Triple::sparcv9:
1406 AddSparcTargetArgs(Args, CmdArgs);
1407 break;
1408
1409 case llvm::Triple::systemz:
1410 AddSystemZTargetArgs(Args, CmdArgs);
1411 break;
1412
1413 case llvm::Triple::x86:
1414 case llvm::Triple::x86_64:
1415 AddX86TargetArgs(Args, CmdArgs);
1416 break;
1417
1418 case llvm::Triple::lanai:
1419 AddLanaiTargetArgs(Args, CmdArgs);
1420 break;
1421
1422 case llvm::Triple::hexagon:
1423 AddHexagonTargetArgs(Args, CmdArgs);
1424 break;
1425
1426 case llvm::Triple::wasm32:
1427 case llvm::Triple::wasm64:
1428 AddWebAssemblyTargetArgs(Args, CmdArgs);
1429 break;
1430 }
1431}
1432
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00001433namespace {
1434void RenderAArch64ABI(const llvm::Triple &Triple, const ArgList &Args,
1435 ArgStringList &CmdArgs) {
1436 const char *ABIName = nullptr;
1437 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
1438 ABIName = A->getValue();
1439 else if (Triple.isOSDarwin())
1440 ABIName = "darwinpcs";
1441 else
1442 ABIName = "aapcs";
1443
1444 CmdArgs.push_back("-target-abi");
1445 CmdArgs.push_back(ABIName);
1446}
1447}
1448
David L. Jonesf561aba2017-03-08 01:02:16 +00001449void Clang::AddAArch64TargetArgs(const ArgList &Args,
1450 ArgStringList &CmdArgs) const {
1451 const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
1452
1453 if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
1454 Args.hasArg(options::OPT_mkernel) ||
1455 Args.hasArg(options::OPT_fapple_kext))
1456 CmdArgs.push_back("-disable-red-zone");
1457
1458 if (!Args.hasFlag(options::OPT_mimplicit_float,
1459 options::OPT_mno_implicit_float, true))
1460 CmdArgs.push_back("-no-implicit-float");
1461
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00001462 RenderAArch64ABI(Triple, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00001463
1464 if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a53_835769,
1465 options::OPT_mno_fix_cortex_a53_835769)) {
Eli Friedman01d349b2018-04-12 22:21:36 +00001466 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00001467 if (A->getOption().matches(options::OPT_mfix_cortex_a53_835769))
1468 CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
1469 else
1470 CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=0");
1471 } else if (Triple.isAndroid()) {
1472 // Enabled A53 errata (835769) workaround by default on android
Eli Friedman01d349b2018-04-12 22:21:36 +00001473 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00001474 CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
1475 }
1476
1477 // Forward the -mglobal-merge option for explicit control over the pass.
1478 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
1479 options::OPT_mno_global_merge)) {
Eli Friedman01d349b2018-04-12 22:21:36 +00001480 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00001481 if (A->getOption().matches(options::OPT_mno_global_merge))
1482 CmdArgs.push_back("-aarch64-enable-global-merge=false");
1483 else
1484 CmdArgs.push_back("-aarch64-enable-global-merge=true");
1485 }
Luke Cheeseman0ac44c12018-08-17 12:55:05 +00001486
1487 if (Arg *A = Args.getLastArg(options::OPT_msign_return_address)) {
1488 CmdArgs.push_back(
1489 Args.MakeArgString(Twine("-msign-return-address=") + A->getValue()));
1490 }
David L. Jonesf561aba2017-03-08 01:02:16 +00001491}
1492
1493void Clang::AddMIPSTargetArgs(const ArgList &Args,
1494 ArgStringList &CmdArgs) const {
1495 const Driver &D = getToolChain().getDriver();
1496 StringRef CPUName;
1497 StringRef ABIName;
1498 const llvm::Triple &Triple = getToolChain().getTriple();
1499 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
1500
1501 CmdArgs.push_back("-target-abi");
1502 CmdArgs.push_back(ABIName.data());
1503
1504 mips::FloatABI ABI = mips::getMipsFloatABI(D, Args);
1505 if (ABI == mips::FloatABI::Soft) {
1506 // Floating point operations and argument passing are soft.
1507 CmdArgs.push_back("-msoft-float");
1508 CmdArgs.push_back("-mfloat-abi");
1509 CmdArgs.push_back("soft");
1510 } else {
1511 // Floating point operations and argument passing are hard.
1512 assert(ABI == mips::FloatABI::Hard && "Invalid float abi!");
1513 CmdArgs.push_back("-mfloat-abi");
1514 CmdArgs.push_back("hard");
1515 }
1516
1517 if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) {
1518 if (A->getOption().matches(options::OPT_mxgot)) {
1519 CmdArgs.push_back("-mllvm");
1520 CmdArgs.push_back("-mxgot");
1521 }
1522 }
1523
1524 if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1,
1525 options::OPT_mno_ldc1_sdc1)) {
1526 if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) {
1527 CmdArgs.push_back("-mllvm");
1528 CmdArgs.push_back("-mno-ldc1-sdc1");
1529 }
1530 }
1531
1532 if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division,
1533 options::OPT_mno_check_zero_division)) {
1534 if (A->getOption().matches(options::OPT_mno_check_zero_division)) {
1535 CmdArgs.push_back("-mllvm");
1536 CmdArgs.push_back("-mno-check-zero-division");
1537 }
1538 }
1539
1540 if (Arg *A = Args.getLastArg(options::OPT_G)) {
1541 StringRef v = A->getValue();
1542 CmdArgs.push_back("-mllvm");
1543 CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
1544 A->claim();
1545 }
1546
Simon Dardis31636a12017-07-20 14:04:12 +00001547 Arg *GPOpt = Args.getLastArg(options::OPT_mgpopt, options::OPT_mno_gpopt);
1548 Arg *ABICalls =
1549 Args.getLastArg(options::OPT_mabicalls, options::OPT_mno_abicalls);
1550
1551 // -mabicalls is the default for many MIPS environments, even with -fno-pic.
1552 // -mgpopt is the default for static, -fno-pic environments but these two
1553 // options conflict. We want to be certain that -mno-abicalls -mgpopt is
1554 // the only case where -mllvm -mgpopt is passed.
1555 // NOTE: We need a warning here or in the backend to warn when -mgpopt is
1556 // passed explicitly when compiling something with -mabicalls
1557 // (implictly) in affect. Currently the warning is in the backend.
Simon Dardisad9d05d2017-08-11 15:01:34 +00001558 //
1559 // When the ABI in use is N64, we also need to determine the PIC mode that
1560 // is in use, as -fno-pic for N64 implies -mno-abicalls.
Simon Dardis31636a12017-07-20 14:04:12 +00001561 bool NoABICalls =
1562 ABICalls && ABICalls->getOption().matches(options::OPT_mno_abicalls);
Simon Dardisad9d05d2017-08-11 15:01:34 +00001563
1564 llvm::Reloc::Model RelocationModel;
1565 unsigned PICLevel;
1566 bool IsPIE;
1567 std::tie(RelocationModel, PICLevel, IsPIE) =
1568 ParsePICArgs(getToolChain(), Args);
1569
1570 NoABICalls = NoABICalls ||
1571 (RelocationModel == llvm::Reloc::Static && ABIName == "n64");
1572
Simon Dardis31636a12017-07-20 14:04:12 +00001573 bool WantGPOpt = GPOpt && GPOpt->getOption().matches(options::OPT_mgpopt);
1574 // We quietly ignore -mno-gpopt as the backend defaults to -mno-gpopt.
1575 if (NoABICalls && (!GPOpt || WantGPOpt)) {
1576 CmdArgs.push_back("-mllvm");
1577 CmdArgs.push_back("-mgpopt");
Simon Dardis9f1d5d82017-07-20 22:23:21 +00001578
1579 Arg *LocalSData = Args.getLastArg(options::OPT_mlocal_sdata,
1580 options::OPT_mno_local_sdata);
Simon Dardis7d318782017-07-24 14:02:09 +00001581 Arg *ExternSData = Args.getLastArg(options::OPT_mextern_sdata,
Simon Dardiseeed0002017-08-03 13:04:29 +00001582 options::OPT_mno_extern_sdata);
1583 Arg *EmbeddedData = Args.getLastArg(options::OPT_membedded_data,
1584 options::OPT_mno_embedded_data);
Simon Dardis9f1d5d82017-07-20 22:23:21 +00001585 if (LocalSData) {
1586 CmdArgs.push_back("-mllvm");
1587 if (LocalSData->getOption().matches(options::OPT_mlocal_sdata)) {
1588 CmdArgs.push_back("-mlocal-sdata=1");
1589 } else {
1590 CmdArgs.push_back("-mlocal-sdata=0");
1591 }
1592 LocalSData->claim();
1593 }
1594
Simon Dardis7d318782017-07-24 14:02:09 +00001595 if (ExternSData) {
1596 CmdArgs.push_back("-mllvm");
1597 if (ExternSData->getOption().matches(options::OPT_mextern_sdata)) {
1598 CmdArgs.push_back("-mextern-sdata=1");
1599 } else {
1600 CmdArgs.push_back("-mextern-sdata=0");
1601 }
1602 ExternSData->claim();
1603 }
Simon Dardiseeed0002017-08-03 13:04:29 +00001604
1605 if (EmbeddedData) {
1606 CmdArgs.push_back("-mllvm");
1607 if (EmbeddedData->getOption().matches(options::OPT_membedded_data)) {
1608 CmdArgs.push_back("-membedded-data=1");
1609 } else {
1610 CmdArgs.push_back("-membedded-data=0");
1611 }
1612 EmbeddedData->claim();
1613 }
1614
Simon Dardis31636a12017-07-20 14:04:12 +00001615 } else if ((!ABICalls || (!NoABICalls && ABICalls)) && WantGPOpt)
1616 D.Diag(diag::warn_drv_unsupported_gpopt) << (ABICalls ? 0 : 1);
1617
1618 if (GPOpt)
1619 GPOpt->claim();
1620
David L. Jonesf561aba2017-03-08 01:02:16 +00001621 if (Arg *A = Args.getLastArg(options::OPT_mcompact_branches_EQ)) {
1622 StringRef Val = StringRef(A->getValue());
1623 if (mips::hasCompactBranches(CPUName)) {
1624 if (Val == "never" || Val == "always" || Val == "optimal") {
1625 CmdArgs.push_back("-mllvm");
1626 CmdArgs.push_back(Args.MakeArgString("-mips-compact-branches=" + Val));
1627 } else
1628 D.Diag(diag::err_drv_unsupported_option_argument)
1629 << A->getOption().getName() << Val;
1630 } else
1631 D.Diag(diag::warn_target_unsupported_compact_branches) << CPUName;
1632 }
1633}
1634
1635void Clang::AddPPCTargetArgs(const ArgList &Args,
1636 ArgStringList &CmdArgs) const {
1637 // Select the ABI to use.
1638 const char *ABIName = nullptr;
1639 if (getToolChain().getTriple().isOSLinux())
1640 switch (getToolChain().getArch()) {
1641 case llvm::Triple::ppc64: {
1642 // When targeting a processor that supports QPX, or if QPX is
1643 // specifically enabled, default to using the ABI that supports QPX (so
1644 // long as it is not specifically disabled).
1645 bool HasQPX = false;
1646 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
1647 HasQPX = A->getValue() == StringRef("a2q");
1648 HasQPX = Args.hasFlag(options::OPT_mqpx, options::OPT_mno_qpx, HasQPX);
1649 if (HasQPX) {
1650 ABIName = "elfv1-qpx";
1651 break;
1652 }
1653
1654 ABIName = "elfv1";
1655 break;
1656 }
1657 case llvm::Triple::ppc64le:
1658 ABIName = "elfv2";
1659 break;
1660 default:
1661 break;
1662 }
1663
1664 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
1665 // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore
1666 // the option if given as we don't have backend support for any targets
1667 // that don't use the altivec abi.
1668 if (StringRef(A->getValue()) != "altivec")
1669 ABIName = A->getValue();
1670
1671 ppc::FloatABI FloatABI =
1672 ppc::getPPCFloatABI(getToolChain().getDriver(), Args);
1673
1674 if (FloatABI == ppc::FloatABI::Soft) {
1675 // Floating point operations and argument passing are soft.
1676 CmdArgs.push_back("-msoft-float");
1677 CmdArgs.push_back("-mfloat-abi");
1678 CmdArgs.push_back("soft");
1679 } else {
1680 // Floating point operations and argument passing are hard.
1681 assert(FloatABI == ppc::FloatABI::Hard && "Invalid float abi!");
1682 CmdArgs.push_back("-mfloat-abi");
1683 CmdArgs.push_back("hard");
1684 }
1685
1686 if (ABIName) {
1687 CmdArgs.push_back("-target-abi");
1688 CmdArgs.push_back(ABIName);
1689 }
1690}
1691
Alex Bradbury71f45452018-01-11 13:36:56 +00001692void Clang::AddRISCVTargetArgs(const ArgList &Args,
1693 ArgStringList &CmdArgs) const {
1694 // FIXME: currently defaults to the soft-float ABIs. Will need to be
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001695 // expanded to select ilp32f, ilp32d, lp64f, lp64d when appropriate.
Alex Bradbury71f45452018-01-11 13:36:56 +00001696 const char *ABIName = nullptr;
1697 const llvm::Triple &Triple = getToolChain().getTriple();
1698 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
1699 ABIName = A->getValue();
1700 else if (Triple.getArch() == llvm::Triple::riscv32)
1701 ABIName = "ilp32";
1702 else if (Triple.getArch() == llvm::Triple::riscv64)
1703 ABIName = "lp64";
1704 else
1705 llvm_unreachable("Unexpected triple!");
1706
1707 CmdArgs.push_back("-target-abi");
1708 CmdArgs.push_back(ABIName);
1709}
1710
David L. Jonesf561aba2017-03-08 01:02:16 +00001711void Clang::AddSparcTargetArgs(const ArgList &Args,
1712 ArgStringList &CmdArgs) const {
1713 sparc::FloatABI FloatABI =
1714 sparc::getSparcFloatABI(getToolChain().getDriver(), Args);
1715
1716 if (FloatABI == sparc::FloatABI::Soft) {
1717 // Floating point operations and argument passing are soft.
1718 CmdArgs.push_back("-msoft-float");
1719 CmdArgs.push_back("-mfloat-abi");
1720 CmdArgs.push_back("soft");
1721 } else {
1722 // Floating point operations and argument passing are hard.
1723 assert(FloatABI == sparc::FloatABI::Hard && "Invalid float abi!");
1724 CmdArgs.push_back("-mfloat-abi");
1725 CmdArgs.push_back("hard");
1726 }
1727}
1728
1729void Clang::AddSystemZTargetArgs(const ArgList &Args,
1730 ArgStringList &CmdArgs) const {
1731 if (Args.hasFlag(options::OPT_mbackchain, options::OPT_mno_backchain, false))
1732 CmdArgs.push_back("-mbackchain");
1733}
1734
1735void Clang::AddX86TargetArgs(const ArgList &Args,
1736 ArgStringList &CmdArgs) const {
1737 if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
1738 Args.hasArg(options::OPT_mkernel) ||
1739 Args.hasArg(options::OPT_fapple_kext))
1740 CmdArgs.push_back("-disable-red-zone");
1741
1742 // Default to avoid implicit floating-point for kernel/kext code, but allow
1743 // that to be overridden with -mno-soft-float.
1744 bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
1745 Args.hasArg(options::OPT_fapple_kext));
1746 if (Arg *A = Args.getLastArg(
1747 options::OPT_msoft_float, options::OPT_mno_soft_float,
1748 options::OPT_mimplicit_float, options::OPT_mno_implicit_float)) {
1749 const Option &O = A->getOption();
1750 NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
1751 O.matches(options::OPT_msoft_float));
1752 }
1753 if (NoImplicitFloat)
1754 CmdArgs.push_back("-no-implicit-float");
1755
1756 if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
1757 StringRef Value = A->getValue();
1758 if (Value == "intel" || Value == "att") {
1759 CmdArgs.push_back("-mllvm");
1760 CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
1761 } else {
1762 getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
1763 << A->getOption().getName() << Value;
1764 }
Nico Webere3712cf2018-01-17 13:34:20 +00001765 } else if (getToolChain().getDriver().IsCLMode()) {
1766 CmdArgs.push_back("-mllvm");
1767 CmdArgs.push_back("-x86-asm-syntax=intel");
David L. Jonesf561aba2017-03-08 01:02:16 +00001768 }
1769
1770 // Set flags to support MCU ABI.
1771 if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) {
1772 CmdArgs.push_back("-mfloat-abi");
1773 CmdArgs.push_back("soft");
1774 CmdArgs.push_back("-mstack-alignment=4");
1775 }
1776}
1777
1778void Clang::AddHexagonTargetArgs(const ArgList &Args,
1779 ArgStringList &CmdArgs) const {
1780 CmdArgs.push_back("-mqdsp6-compat");
1781 CmdArgs.push_back("-Wreturn-type");
1782
1783 if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00001784 CmdArgs.push_back("-mllvm");
Benjamin Kramer3a13ed62017-12-28 16:58:54 +00001785 CmdArgs.push_back(Args.MakeArgString("-hexagon-small-data-threshold=" +
1786 Twine(G.getValue())));
David L. Jonesf561aba2017-03-08 01:02:16 +00001787 }
1788
1789 if (!Args.hasArg(options::OPT_fno_short_enums))
1790 CmdArgs.push_back("-fshort-enums");
1791 if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
1792 CmdArgs.push_back("-mllvm");
1793 CmdArgs.push_back("-enable-hexagon-ieee-rnd-near");
1794 }
1795 CmdArgs.push_back("-mllvm");
1796 CmdArgs.push_back("-machine-sink-split=0");
1797}
1798
1799void Clang::AddLanaiTargetArgs(const ArgList &Args,
1800 ArgStringList &CmdArgs) const {
1801 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
1802 StringRef CPUName = A->getValue();
1803
1804 CmdArgs.push_back("-target-cpu");
1805 CmdArgs.push_back(Args.MakeArgString(CPUName));
1806 }
1807 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1808 StringRef Value = A->getValue();
1809 // Only support mregparm=4 to support old usage. Report error for all other
1810 // cases.
1811 int Mregparm;
1812 if (Value.getAsInteger(10, Mregparm)) {
1813 if (Mregparm != 4) {
1814 getToolChain().getDriver().Diag(
1815 diag::err_drv_unsupported_option_argument)
1816 << A->getOption().getName() << Value;
1817 }
1818 }
1819 }
1820}
1821
1822void Clang::AddWebAssemblyTargetArgs(const ArgList &Args,
1823 ArgStringList &CmdArgs) const {
1824 // Default to "hidden" visibility.
1825 if (!Args.hasArg(options::OPT_fvisibility_EQ,
1826 options::OPT_fvisibility_ms_compat)) {
1827 CmdArgs.push_back("-fvisibility");
1828 CmdArgs.push_back("hidden");
1829 }
1830}
1831
1832void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename,
1833 StringRef Target, const InputInfo &Output,
1834 const InputInfo &Input, const ArgList &Args) const {
1835 // If this is a dry run, do not create the compilation database file.
1836 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
1837 return;
1838
1839 using llvm::yaml::escape;
1840 const Driver &D = getToolChain().getDriver();
1841
1842 if (!CompilationDatabase) {
1843 std::error_code EC;
1844 auto File = llvm::make_unique<llvm::raw_fd_ostream>(Filename, EC, llvm::sys::fs::F_Text);
1845 if (EC) {
1846 D.Diag(clang::diag::err_drv_compilationdatabase) << Filename
1847 << EC.message();
1848 return;
1849 }
1850 CompilationDatabase = std::move(File);
1851 }
1852 auto &CDB = *CompilationDatabase;
1853 SmallString<128> Buf;
1854 if (llvm::sys::fs::current_path(Buf))
1855 Buf = ".";
1856 CDB << "{ \"directory\": \"" << escape(Buf) << "\"";
1857 CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\"";
1858 CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\"";
1859 CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\"";
1860 Buf = "-x";
1861 Buf += types::getTypeName(Input.getType());
1862 CDB << ", \"" << escape(Buf) << "\"";
1863 if (!D.SysRoot.empty() && !Args.hasArg(options::OPT__sysroot_EQ)) {
1864 Buf = "--sysroot=";
1865 Buf += D.SysRoot;
1866 CDB << ", \"" << escape(Buf) << "\"";
1867 }
1868 CDB << ", \"" << escape(Input.getFilename()) << "\"";
1869 for (auto &A: Args) {
1870 auto &O = A->getOption();
1871 // Skip language selection, which is positional.
1872 if (O.getID() == options::OPT_x)
1873 continue;
1874 // Skip writing dependency output and the compilation database itself.
1875 if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group)
1876 continue;
1877 // Skip inputs.
1878 if (O.getKind() == Option::InputClass)
1879 continue;
1880 // All other arguments are quoted and appended.
1881 ArgStringList ASL;
1882 A->render(Args, ASL);
1883 for (auto &it: ASL)
1884 CDB << ", \"" << escape(it) << "\"";
1885 }
1886 Buf = "--target=";
1887 Buf += Target;
1888 CDB << ", \"" << escape(Buf) << "\"]},\n";
1889}
1890
1891static void CollectArgsForIntegratedAssembler(Compilation &C,
1892 const ArgList &Args,
1893 ArgStringList &CmdArgs,
1894 const Driver &D) {
1895 if (UseRelaxAll(C, Args))
1896 CmdArgs.push_back("-mrelax-all");
1897
1898 // Only default to -mincremental-linker-compatible if we think we are
1899 // targeting the MSVC linker.
1900 bool DefaultIncrementalLinkerCompatible =
1901 C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment();
1902 if (Args.hasFlag(options::OPT_mincremental_linker_compatible,
1903 options::OPT_mno_incremental_linker_compatible,
1904 DefaultIncrementalLinkerCompatible))
1905 CmdArgs.push_back("-mincremental-linker-compatible");
1906
1907 switch (C.getDefaultToolChain().getArch()) {
1908 case llvm::Triple::arm:
1909 case llvm::Triple::armeb:
1910 case llvm::Triple::thumb:
1911 case llvm::Triple::thumbeb:
1912 if (Arg *A = Args.getLastArg(options::OPT_mimplicit_it_EQ)) {
1913 StringRef Value = A->getValue();
1914 if (Value == "always" || Value == "never" || Value == "arm" ||
1915 Value == "thumb") {
1916 CmdArgs.push_back("-mllvm");
1917 CmdArgs.push_back(Args.MakeArgString("-arm-implicit-it=" + Value));
1918 } else {
1919 D.Diag(diag::err_drv_unsupported_option_argument)
1920 << A->getOption().getName() << Value;
1921 }
1922 }
1923 break;
1924 default:
1925 break;
1926 }
1927
1928 // When passing -I arguments to the assembler we sometimes need to
1929 // unconditionally take the next argument. For example, when parsing
1930 // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the
1931 // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo'
1932 // arg after parsing the '-I' arg.
1933 bool TakeNextArg = false;
1934
Petr Hosek5668d832017-11-22 01:38:31 +00001935 bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations();
David L. Jonesf561aba2017-03-08 01:02:16 +00001936 const char *MipsTargetFeature = nullptr;
1937 for (const Arg *A :
1938 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
1939 A->claim();
1940
1941 for (StringRef Value : A->getValues()) {
1942 if (TakeNextArg) {
1943 CmdArgs.push_back(Value.data());
1944 TakeNextArg = false;
1945 continue;
1946 }
1947
1948 if (C.getDefaultToolChain().getTriple().isOSBinFormatCOFF() &&
1949 Value == "-mbig-obj")
1950 continue; // LLVM handles bigobj automatically
1951
1952 switch (C.getDefaultToolChain().getArch()) {
1953 default:
1954 break;
Peter Smith3947cb32017-11-20 13:43:55 +00001955 case llvm::Triple::thumb:
1956 case llvm::Triple::thumbeb:
1957 case llvm::Triple::arm:
1958 case llvm::Triple::armeb:
1959 if (Value == "-mthumb")
1960 // -mthumb has already been processed in ComputeLLVMTriple()
1961 // recognize but skip over here.
1962 continue;
Peter Smith931c9fa2017-11-20 13:53:55 +00001963 break;
David L. Jonesf561aba2017-03-08 01:02:16 +00001964 case llvm::Triple::mips:
1965 case llvm::Triple::mipsel:
1966 case llvm::Triple::mips64:
1967 case llvm::Triple::mips64el:
1968 if (Value == "--trap") {
1969 CmdArgs.push_back("-target-feature");
1970 CmdArgs.push_back("+use-tcc-in-div");
1971 continue;
1972 }
1973 if (Value == "--break") {
1974 CmdArgs.push_back("-target-feature");
1975 CmdArgs.push_back("-use-tcc-in-div");
1976 continue;
1977 }
1978 if (Value.startswith("-msoft-float")) {
1979 CmdArgs.push_back("-target-feature");
1980 CmdArgs.push_back("+soft-float");
1981 continue;
1982 }
1983 if (Value.startswith("-mhard-float")) {
1984 CmdArgs.push_back("-target-feature");
1985 CmdArgs.push_back("-soft-float");
1986 continue;
1987 }
1988
1989 MipsTargetFeature = llvm::StringSwitch<const char *>(Value)
1990 .Case("-mips1", "+mips1")
1991 .Case("-mips2", "+mips2")
1992 .Case("-mips3", "+mips3")
1993 .Case("-mips4", "+mips4")
1994 .Case("-mips5", "+mips5")
1995 .Case("-mips32", "+mips32")
1996 .Case("-mips32r2", "+mips32r2")
1997 .Case("-mips32r3", "+mips32r3")
1998 .Case("-mips32r5", "+mips32r5")
1999 .Case("-mips32r6", "+mips32r6")
2000 .Case("-mips64", "+mips64")
2001 .Case("-mips64r2", "+mips64r2")
2002 .Case("-mips64r3", "+mips64r3")
2003 .Case("-mips64r5", "+mips64r5")
2004 .Case("-mips64r6", "+mips64r6")
2005 .Default(nullptr);
2006 if (MipsTargetFeature)
2007 continue;
2008 }
2009
2010 if (Value == "-force_cpusubtype_ALL") {
2011 // Do nothing, this is the default and we don't support anything else.
2012 } else if (Value == "-L") {
2013 CmdArgs.push_back("-msave-temp-labels");
2014 } else if (Value == "--fatal-warnings") {
2015 CmdArgs.push_back("-massembler-fatal-warnings");
2016 } else if (Value == "--noexecstack") {
2017 CmdArgs.push_back("-mnoexecstack");
Saleem Abdulrasoold064e912017-06-23 15:34:16 +00002018 } else if (Value.startswith("-compress-debug-sections") ||
2019 Value.startswith("--compress-debug-sections") ||
2020 Value == "-nocompress-debug-sections" ||
David L. Jonesf561aba2017-03-08 01:02:16 +00002021 Value == "--nocompress-debug-sections") {
Saleem Abdulrasoold064e912017-06-23 15:34:16 +00002022 CmdArgs.push_back(Value.data());
David L. Jonesf561aba2017-03-08 01:02:16 +00002023 } else if (Value == "-mrelax-relocations=yes" ||
2024 Value == "--mrelax-relocations=yes") {
2025 UseRelaxRelocations = true;
2026 } else if (Value == "-mrelax-relocations=no" ||
2027 Value == "--mrelax-relocations=no") {
2028 UseRelaxRelocations = false;
2029 } else if (Value.startswith("-I")) {
2030 CmdArgs.push_back(Value.data());
2031 // We need to consume the next argument if the current arg is a plain
2032 // -I. The next arg will be the include directory.
2033 if (Value == "-I")
2034 TakeNextArg = true;
2035 } else if (Value.startswith("-gdwarf-")) {
2036 // "-gdwarf-N" options are not cc1as options.
2037 unsigned DwarfVersion = DwarfVersionNum(Value);
2038 if (DwarfVersion == 0) { // Send it onward, and let cc1as complain.
2039 CmdArgs.push_back(Value.data());
2040 } else {
2041 RenderDebugEnablingArgs(Args, CmdArgs,
2042 codegenoptions::LimitedDebugInfo,
2043 DwarfVersion, llvm::DebuggerKind::Default);
2044 }
2045 } else if (Value.startswith("-mcpu") || Value.startswith("-mfpu") ||
2046 Value.startswith("-mhwdiv") || Value.startswith("-march")) {
2047 // Do nothing, we'll validate it later.
2048 } else if (Value == "-defsym") {
2049 if (A->getNumValues() != 2) {
2050 D.Diag(diag::err_drv_defsym_invalid_format) << Value;
2051 break;
2052 }
2053 const char *S = A->getValue(1);
2054 auto Pair = StringRef(S).split('=');
2055 auto Sym = Pair.first;
2056 auto SVal = Pair.second;
2057
2058 if (Sym.empty() || SVal.empty()) {
2059 D.Diag(diag::err_drv_defsym_invalid_format) << S;
2060 break;
2061 }
2062 int64_t IVal;
2063 if (SVal.getAsInteger(0, IVal)) {
2064 D.Diag(diag::err_drv_defsym_invalid_symval) << SVal;
2065 break;
2066 }
2067 CmdArgs.push_back(Value.data());
2068 TakeNextArg = true;
2069 } else {
2070 D.Diag(diag::err_drv_unsupported_option_argument)
2071 << A->getOption().getName() << Value;
2072 }
2073 }
2074 }
David L. Jonesf561aba2017-03-08 01:02:16 +00002075 if (UseRelaxRelocations)
2076 CmdArgs.push_back("--mrelax-relocations");
2077 if (MipsTargetFeature != nullptr) {
2078 CmdArgs.push_back("-target-feature");
2079 CmdArgs.push_back(MipsTargetFeature);
2080 }
2081}
2082
Saleem Abdulrasoole6d219d2017-09-01 22:04:24 +00002083static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
2084 bool OFastEnabled, const ArgList &Args,
2085 ArgStringList &CmdArgs) {
2086 // Handle various floating point optimization flags, mapping them to the
2087 // appropriate LLVM code generation flags. This is complicated by several
2088 // "umbrella" flags, so we do this by stepping through the flags incrementally
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002089 // adjusting what we think is enabled/disabled, then at the end setting the
Saleem Abdulrasoole6d219d2017-09-01 22:04:24 +00002090 // LLVM flags based on the final state.
2091 bool HonorINFs = true;
2092 bool HonorNaNs = true;
2093 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
2094 bool MathErrno = TC.IsMathErrnoDefault();
2095 bool AssociativeMath = false;
2096 bool ReciprocalMath = false;
2097 bool SignedZeros = true;
2098 bool TrappingMath = true;
2099 StringRef DenormalFPMath = "";
2100 StringRef FPContract = "";
2101
Saleem Abdulrasool258e4f62018-09-18 21:12:39 +00002102 if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
2103 CmdArgs.push_back("-mlimit-float-precision");
2104 CmdArgs.push_back(A->getValue());
2105 }
2106
Saleem Abdulrasoole6d219d2017-09-01 22:04:24 +00002107 for (const Arg *A : Args) {
2108 switch (A->getOption().getID()) {
2109 // If this isn't an FP option skip the claim below
2110 default: continue;
2111
2112 // Options controlling individual features
2113 case options::OPT_fhonor_infinities: HonorINFs = true; break;
2114 case options::OPT_fno_honor_infinities: HonorINFs = false; break;
2115 case options::OPT_fhonor_nans: HonorNaNs = true; break;
2116 case options::OPT_fno_honor_nans: HonorNaNs = false; break;
2117 case options::OPT_fmath_errno: MathErrno = true; break;
2118 case options::OPT_fno_math_errno: MathErrno = false; break;
2119 case options::OPT_fassociative_math: AssociativeMath = true; break;
2120 case options::OPT_fno_associative_math: AssociativeMath = false; break;
2121 case options::OPT_freciprocal_math: ReciprocalMath = true; break;
2122 case options::OPT_fno_reciprocal_math: ReciprocalMath = false; break;
2123 case options::OPT_fsigned_zeros: SignedZeros = true; break;
2124 case options::OPT_fno_signed_zeros: SignedZeros = false; break;
2125 case options::OPT_ftrapping_math: TrappingMath = true; break;
2126 case options::OPT_fno_trapping_math: TrappingMath = false; break;
2127
2128 case options::OPT_fdenormal_fp_math_EQ:
2129 DenormalFPMath = A->getValue();
2130 break;
2131
2132 // Validate and pass through -fp-contract option.
2133 case options::OPT_ffp_contract: {
2134 StringRef Val = A->getValue();
2135 if (Val == "fast" || Val == "on" || Val == "off")
2136 FPContract = Val;
2137 else
2138 D.Diag(diag::err_drv_unsupported_option_argument)
2139 << A->getOption().getName() << Val;
2140 break;
2141 }
2142
2143 case options::OPT_ffinite_math_only:
2144 HonorINFs = false;
2145 HonorNaNs = false;
2146 break;
2147 case options::OPT_fno_finite_math_only:
2148 HonorINFs = true;
2149 HonorNaNs = true;
2150 break;
2151
2152 case options::OPT_funsafe_math_optimizations:
2153 AssociativeMath = true;
2154 ReciprocalMath = true;
2155 SignedZeros = false;
2156 TrappingMath = false;
2157 break;
2158 case options::OPT_fno_unsafe_math_optimizations:
2159 AssociativeMath = false;
2160 ReciprocalMath = false;
2161 SignedZeros = true;
2162 TrappingMath = true;
2163 // -fno_unsafe_math_optimizations restores default denormal handling
2164 DenormalFPMath = "";
2165 break;
2166
2167 case options::OPT_Ofast:
2168 // If -Ofast is the optimization level, then -ffast-math should be enabled
2169 if (!OFastEnabled)
2170 continue;
2171 LLVM_FALLTHROUGH;
2172 case options::OPT_ffast_math:
2173 HonorINFs = false;
2174 HonorNaNs = false;
2175 MathErrno = false;
2176 AssociativeMath = true;
2177 ReciprocalMath = true;
2178 SignedZeros = false;
2179 TrappingMath = false;
2180 // If fast-math is set then set the fp-contract mode to fast.
2181 FPContract = "fast";
2182 break;
2183 case options::OPT_fno_fast_math:
2184 HonorINFs = true;
2185 HonorNaNs = true;
2186 // Turning on -ffast-math (with either flag) removes the need for
2187 // MathErrno. However, turning *off* -ffast-math merely restores the
2188 // toolchain default (which may be false).
2189 MathErrno = TC.IsMathErrnoDefault();
2190 AssociativeMath = false;
2191 ReciprocalMath = false;
2192 SignedZeros = true;
2193 TrappingMath = true;
2194 // -fno_fast_math restores default denormal and fpcontract handling
2195 DenormalFPMath = "";
2196 FPContract = "";
2197 break;
2198 }
2199
2200 // If we handled this option claim it
2201 A->claim();
2202 }
2203
2204 if (!HonorINFs)
2205 CmdArgs.push_back("-menable-no-infs");
2206
2207 if (!HonorNaNs)
2208 CmdArgs.push_back("-menable-no-nans");
2209
2210 if (MathErrno)
2211 CmdArgs.push_back("-fmath-errno");
2212
2213 if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
2214 !TrappingMath)
2215 CmdArgs.push_back("-menable-unsafe-fp-math");
2216
2217 if (!SignedZeros)
2218 CmdArgs.push_back("-fno-signed-zeros");
2219
Sanjay Patelcb8c0092017-12-16 16:11:17 +00002220 if (AssociativeMath && !SignedZeros && !TrappingMath)
2221 CmdArgs.push_back("-mreassociate");
2222
Saleem Abdulrasoole6d219d2017-09-01 22:04:24 +00002223 if (ReciprocalMath)
2224 CmdArgs.push_back("-freciprocal-math");
2225
2226 if (!TrappingMath)
2227 CmdArgs.push_back("-fno-trapping-math");
2228
2229 if (!DenormalFPMath.empty())
2230 CmdArgs.push_back(
2231 Args.MakeArgString("-fdenormal-fp-math=" + DenormalFPMath));
2232
2233 if (!FPContract.empty())
2234 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract));
2235
2236 ParseMRecip(D, Args, CmdArgs);
2237
2238 // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for the
2239 // individual features enabled by -ffast-math instead of the option itself as
2240 // that's consistent with gcc's behaviour.
2241 if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath &&
2242 ReciprocalMath && !SignedZeros && !TrappingMath)
2243 CmdArgs.push_back("-ffast-math");
2244
2245 // Handle __FINITE_MATH_ONLY__ similarly.
2246 if (!HonorINFs && !HonorNaNs)
2247 CmdArgs.push_back("-ffinite-math-only");
Saleem Abdulrasoolfb302ca2017-09-03 04:46:57 +00002248
2249 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) {
2250 CmdArgs.push_back("-mfpmath");
2251 CmdArgs.push_back(A->getValue());
2252 }
Sanjay Pateld1754762018-04-27 14:22:48 +00002253
2254 // Disable a codegen optimization for floating-point casts.
Sanjay Patelc81450e2018-04-30 18:19:03 +00002255 if (Args.hasFlag(options::OPT_fno_strict_float_cast_overflow,
2256 options::OPT_fstrict_float_cast_overflow, false))
2257 CmdArgs.push_back("-fno-strict-float-cast-overflow");
Saleem Abdulrasoole6d219d2017-09-01 22:04:24 +00002258}
2259
Saleem Abdulrasool24aafa52017-08-30 14:18:08 +00002260static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs,
2261 const llvm::Triple &Triple,
2262 const InputInfo &Input) {
2263 // Enable region store model by default.
2264 CmdArgs.push_back("-analyzer-store=region");
2265
2266 // Treat blocks as analysis entry points.
2267 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
2268
Saleem Abdulrasool24aafa52017-08-30 14:18:08 +00002269 // Add default argument set.
2270 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
2271 CmdArgs.push_back("-analyzer-checker=core");
2272 CmdArgs.push_back("-analyzer-checker=apiModeling");
2273
2274 if (!Triple.isWindowsMSVCEnvironment()) {
2275 CmdArgs.push_back("-analyzer-checker=unix");
2276 } else {
2277 // Enable "unix" checkers that also work on Windows.
2278 CmdArgs.push_back("-analyzer-checker=unix.API");
2279 CmdArgs.push_back("-analyzer-checker=unix.Malloc");
2280 CmdArgs.push_back("-analyzer-checker=unix.MallocSizeof");
2281 CmdArgs.push_back("-analyzer-checker=unix.MismatchedDeallocator");
2282 CmdArgs.push_back("-analyzer-checker=unix.cstring.BadSizeArg");
2283 CmdArgs.push_back("-analyzer-checker=unix.cstring.NullArg");
2284 }
2285
2286 // Disable some unix checkers for PS4.
2287 if (Triple.isPS4CPU()) {
2288 CmdArgs.push_back("-analyzer-disable-checker=unix.API");
2289 CmdArgs.push_back("-analyzer-disable-checker=unix.Vfork");
2290 }
2291
2292 if (Triple.isOSDarwin())
2293 CmdArgs.push_back("-analyzer-checker=osx");
2294
2295 CmdArgs.push_back("-analyzer-checker=deadcode");
2296
2297 if (types::isCXX(Input.getType()))
2298 CmdArgs.push_back("-analyzer-checker=cplusplus");
2299
2300 if (!Triple.isPS4CPU()) {
2301 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn");
2302 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
2303 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
2304 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
2305 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
2306 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
2307 }
2308
2309 // Default nullability checks.
2310 CmdArgs.push_back("-analyzer-checker=nullability.NullPassedToNonnull");
2311 CmdArgs.push_back("-analyzer-checker=nullability.NullReturnedFromNonnull");
2312 }
2313
2314 // Set the output format. The default is plist, for (lame) historical reasons.
2315 CmdArgs.push_back("-analyzer-output");
2316 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
2317 CmdArgs.push_back(A->getValue());
2318 else
2319 CmdArgs.push_back("plist");
2320
2321 // Disable the presentation of standard compiler warnings when using
2322 // --analyze. We only want to show static analyzer diagnostics or frontend
2323 // errors.
2324 CmdArgs.push_back("-w");
2325
2326 // Add -Xanalyzer arguments when running as analyzer.
2327 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
2328}
2329
Saleem Abdulrasoold5ba5452017-08-29 23:59:08 +00002330static void RenderSSPOptions(const ToolChain &TC, const ArgList &Args,
Saleem Abdulrasoolc2320ad2017-09-06 04:56:23 +00002331 ArgStringList &CmdArgs, bool KernelOrKext) {
Saleem Abdulrasoold5ba5452017-08-29 23:59:08 +00002332 const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple();
2333
2334 // NVPTX doesn't support stack protectors; from the compiler's perspective, it
2335 // doesn't even have a stack!
2336 if (EffectiveTriple.isNVPTX())
2337 return;
2338
2339 // -stack-protector=0 is default.
2340 unsigned StackProtectorLevel = 0;
2341 unsigned DefaultStackProtectorLevel =
2342 TC.GetDefaultStackProtectorLevel(KernelOrKext);
2343
2344 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
2345 options::OPT_fstack_protector_all,
2346 options::OPT_fstack_protector_strong,
2347 options::OPT_fstack_protector)) {
2348 if (A->getOption().matches(options::OPT_fstack_protector))
2349 StackProtectorLevel =
2350 std::max<unsigned>(LangOptions::SSPOn, DefaultStackProtectorLevel);
2351 else if (A->getOption().matches(options::OPT_fstack_protector_strong))
2352 StackProtectorLevel = LangOptions::SSPStrong;
2353 else if (A->getOption().matches(options::OPT_fstack_protector_all))
2354 StackProtectorLevel = LangOptions::SSPReq;
2355 } else {
Bruno Cardoso Lopesbad2c4a2017-09-06 00:44:10 +00002356 StackProtectorLevel = DefaultStackProtectorLevel;
Saleem Abdulrasoold5ba5452017-08-29 23:59:08 +00002357 }
2358
2359 if (StackProtectorLevel) {
2360 CmdArgs.push_back("-stack-protector");
2361 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
2362 }
2363
2364 // --param ssp-buffer-size=
2365 for (const Arg *A : Args.filtered(options::OPT__param)) {
2366 StringRef Str(A->getValue());
2367 if (Str.startswith("ssp-buffer-size=")) {
2368 if (StackProtectorLevel) {
2369 CmdArgs.push_back("-stack-protector-buffer-size");
2370 // FIXME: Verify the argument is a valid integer.
2371 CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
2372 }
2373 A->claim();
2374 }
2375 }
2376}
2377
Saleem Abdulrasool68c808f62017-08-29 23:59:07 +00002378static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs) {
2379 const unsigned ForwardedArguments[] = {
2380 options::OPT_cl_opt_disable,
2381 options::OPT_cl_strict_aliasing,
2382 options::OPT_cl_single_precision_constant,
2383 options::OPT_cl_finite_math_only,
2384 options::OPT_cl_kernel_arg_info,
2385 options::OPT_cl_unsafe_math_optimizations,
2386 options::OPT_cl_fast_relaxed_math,
2387 options::OPT_cl_mad_enable,
2388 options::OPT_cl_no_signed_zeros,
2389 options::OPT_cl_denorms_are_zero,
2390 options::OPT_cl_fp32_correctly_rounded_divide_sqrt,
Alexey Sotkin20f65922018-02-22 11:54:14 +00002391 options::OPT_cl_uniform_work_group_size
Saleem Abdulrasool68c808f62017-08-29 23:59:07 +00002392 };
2393
2394 if (Arg *A = Args.getLastArg(options::OPT_cl_std_EQ)) {
2395 std::string CLStdStr = std::string("-cl-std=") + A->getValue();
2396 CmdArgs.push_back(Args.MakeArgString(CLStdStr));
2397 }
2398
2399 for (const auto &Arg : ForwardedArguments)
2400 if (const auto *A = Args.getLastArg(Arg))
2401 CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName()));
2402}
2403
Saleem Abdulrasool0a322c62017-08-31 15:35:01 +00002404static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args,
2405 ArgStringList &CmdArgs) {
2406 bool ARCMTEnabled = false;
2407 if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) {
2408 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
2409 options::OPT_ccc_arcmt_modify,
2410 options::OPT_ccc_arcmt_migrate)) {
2411 ARCMTEnabled = true;
2412 switch (A->getOption().getID()) {
2413 default: llvm_unreachable("missed a case");
2414 case options::OPT_ccc_arcmt_check:
2415 CmdArgs.push_back("-arcmt-check");
2416 break;
2417 case options::OPT_ccc_arcmt_modify:
2418 CmdArgs.push_back("-arcmt-modify");
2419 break;
2420 case options::OPT_ccc_arcmt_migrate:
2421 CmdArgs.push_back("-arcmt-migrate");
2422 CmdArgs.push_back("-mt-migrate-directory");
2423 CmdArgs.push_back(A->getValue());
2424
2425 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
2426 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
2427 break;
2428 }
2429 }
2430 } else {
2431 Args.ClaimAllArgs(options::OPT_ccc_arcmt_check);
2432 Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify);
2433 Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate);
2434 }
2435
2436 if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
2437 if (ARCMTEnabled)
2438 D.Diag(diag::err_drv_argument_not_allowed_with)
2439 << A->getAsString(Args) << "-ccc-arcmt-migrate";
2440
2441 CmdArgs.push_back("-mt-migrate-directory");
2442 CmdArgs.push_back(A->getValue());
2443
2444 if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
2445 options::OPT_objcmt_migrate_subscripting,
2446 options::OPT_objcmt_migrate_property)) {
2447 // None specified, means enable them all.
2448 CmdArgs.push_back("-objcmt-migrate-literals");
2449 CmdArgs.push_back("-objcmt-migrate-subscripting");
2450 CmdArgs.push_back("-objcmt-migrate-property");
2451 } else {
2452 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2453 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
2454 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
2455 }
2456 } else {
2457 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2458 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
2459 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
2460 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all);
2461 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property);
2462 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property);
2463 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property_dot_syntax);
2464 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation);
2465 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype);
2466 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros);
2467 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance);
2468 Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property);
2469 Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property);
2470 Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly);
2471 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init);
2472 Args.AddLastArg(CmdArgs, options::OPT_objcmt_whitelist_dir_path);
2473 }
2474}
2475
Saleem Abdulrasool99f4ead2017-09-01 23:44:01 +00002476static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T,
2477 const ArgList &Args, ArgStringList &CmdArgs) {
2478 // -fbuiltin is default unless -mkernel is used.
2479 bool UseBuiltins =
2480 Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin,
2481 !Args.hasArg(options::OPT_mkernel));
2482 if (!UseBuiltins)
2483 CmdArgs.push_back("-fno-builtin");
2484
2485 // -ffreestanding implies -fno-builtin.
2486 if (Args.hasArg(options::OPT_ffreestanding))
2487 UseBuiltins = false;
2488
2489 // Process the -fno-builtin-* options.
2490 for (const auto &Arg : Args) {
2491 const Option &O = Arg->getOption();
2492 if (!O.matches(options::OPT_fno_builtin_))
2493 continue;
2494
2495 Arg->claim();
2496
2497 // If -fno-builtin is specified, then there's no need to pass the option to
2498 // the frontend.
2499 if (!UseBuiltins)
2500 continue;
2501
2502 StringRef FuncName = Arg->getValue();
2503 CmdArgs.push_back(Args.MakeArgString("-fno-builtin-" + FuncName));
2504 }
2505
2506 // le32-specific flags:
2507 // -fno-math-builtin: clang should not convert math builtins to intrinsics
2508 // by default.
2509 if (TC.getArch() == llvm::Triple::le32)
2510 CmdArgs.push_back("-fno-math-builtin");
Saleem Abdulrasool99f4ead2017-09-01 23:44:01 +00002511}
2512
Adrian Prantl70599032018-02-09 18:43:10 +00002513void Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) {
2514 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false, Result);
2515 llvm::sys::path::append(Result, "org.llvm.clang.");
2516 appendUserToPath(Result);
2517 llvm::sys::path::append(Result, "ModuleCache");
2518}
2519
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00002520static void RenderModulesOptions(Compilation &C, const Driver &D,
2521 const ArgList &Args, const InputInfo &Input,
2522 const InputInfo &Output,
2523 ArgStringList &CmdArgs, bool &HaveModules) {
2524 // -fmodules enables the use of precompiled modules (off by default).
2525 // Users can pass -fno-cxx-modules to turn off modules support for
2526 // C++/Objective-C++ programs.
2527 bool HaveClangModules = false;
2528 if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
2529 bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
2530 options::OPT_fno_cxx_modules, true);
2531 if (AllowedInCXX || !types::isCXX(Input.getType())) {
2532 CmdArgs.push_back("-fmodules");
2533 HaveClangModules = true;
2534 }
2535 }
2536
2537 HaveModules = HaveClangModules;
2538 if (Args.hasArg(options::OPT_fmodules_ts)) {
2539 CmdArgs.push_back("-fmodules-ts");
2540 HaveModules = true;
2541 }
2542
2543 // -fmodule-maps enables implicit reading of module map files. By default,
2544 // this is enabled if we are using Clang's flavor of precompiled modules.
2545 if (Args.hasFlag(options::OPT_fimplicit_module_maps,
2546 options::OPT_fno_implicit_module_maps, HaveClangModules))
2547 CmdArgs.push_back("-fimplicit-module-maps");
2548
2549 // -fmodules-decluse checks that modules used are declared so (off by default)
2550 if (Args.hasFlag(options::OPT_fmodules_decluse,
2551 options::OPT_fno_modules_decluse, false))
2552 CmdArgs.push_back("-fmodules-decluse");
2553
2554 // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that
2555 // all #included headers are part of modules.
2556 if (Args.hasFlag(options::OPT_fmodules_strict_decluse,
2557 options::OPT_fno_modules_strict_decluse, false))
2558 CmdArgs.push_back("-fmodules-strict-decluse");
2559
2560 // -fno-implicit-modules turns off implicitly compiling modules on demand.
Bruno Cardoso Lopes89b9fdb2018-04-18 06:07:49 +00002561 bool ImplicitModules = false;
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00002562 if (!Args.hasFlag(options::OPT_fimplicit_modules,
2563 options::OPT_fno_implicit_modules, HaveClangModules)) {
2564 if (HaveModules)
2565 CmdArgs.push_back("-fno-implicit-modules");
2566 } else if (HaveModules) {
Bruno Cardoso Lopes89b9fdb2018-04-18 06:07:49 +00002567 ImplicitModules = true;
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00002568 // -fmodule-cache-path specifies where our implicitly-built module files
2569 // should be written.
2570 SmallString<128> Path;
2571 if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path))
2572 Path = A->getValue();
2573
2574 if (C.isForDiagnostics()) {
2575 // When generating crash reports, we want to emit the modules along with
2576 // the reproduction sources, so we ignore any provided module path.
2577 Path = Output.getFilename();
2578 llvm::sys::path::replace_extension(Path, ".cache");
2579 llvm::sys::path::append(Path, "modules");
2580 } else if (Path.empty()) {
2581 // No module path was provided: use the default.
Adrian Prantl70599032018-02-09 18:43:10 +00002582 Driver::getDefaultModuleCachePath(Path);
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00002583 }
2584
2585 const char Arg[] = "-fmodules-cache-path=";
2586 Path.insert(Path.begin(), Arg, Arg + strlen(Arg));
2587 CmdArgs.push_back(Args.MakeArgString(Path));
2588 }
2589
2590 if (HaveModules) {
2591 // -fprebuilt-module-path specifies where to load the prebuilt module files.
2592 for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) {
2593 CmdArgs.push_back(Args.MakeArgString(
2594 std::string("-fprebuilt-module-path=") + A->getValue()));
2595 A->claim();
2596 }
2597 }
2598
2599 // -fmodule-name specifies the module that is currently being built (or
2600 // used for header checking by -fmodule-maps).
2601 Args.AddLastArg(CmdArgs, options::OPT_fmodule_name_EQ);
2602
2603 // -fmodule-map-file can be used to specify files containing module
2604 // definitions.
2605 Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file);
2606
2607 // -fbuiltin-module-map can be used to load the clang
2608 // builtin headers modulemap file.
2609 if (Args.hasArg(options::OPT_fbuiltin_module_map)) {
2610 SmallString<128> BuiltinModuleMap(D.ResourceDir);
2611 llvm::sys::path::append(BuiltinModuleMap, "include");
2612 llvm::sys::path::append(BuiltinModuleMap, "module.modulemap");
2613 if (llvm::sys::fs::exists(BuiltinModuleMap))
2614 CmdArgs.push_back(
2615 Args.MakeArgString("-fmodule-map-file=" + BuiltinModuleMap));
2616 }
2617
2618 // The -fmodule-file=<name>=<file> form specifies the mapping of module
2619 // names to precompiled module files (the module is loaded only if used).
2620 // The -fmodule-file=<file> form can be used to unconditionally load
2621 // precompiled module files (whether used or not).
2622 if (HaveModules)
2623 Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file);
2624 else
2625 Args.ClaimAllArgs(options::OPT_fmodule_file);
2626
2627 // When building modules and generating crashdumps, we need to dump a module
2628 // dependency VFS alongside the output.
2629 if (HaveClangModules && C.isForDiagnostics()) {
2630 SmallString<128> VFSDir(Output.getFilename());
2631 llvm::sys::path::replace_extension(VFSDir, ".cache");
2632 // Add the cache directory as a temp so the crash diagnostics pick it up.
2633 C.addTempFile(Args.MakeArgString(VFSDir));
2634
2635 llvm::sys::path::append(VFSDir, "vfs");
2636 CmdArgs.push_back("-module-dependency-dir");
2637 CmdArgs.push_back(Args.MakeArgString(VFSDir));
2638 }
2639
2640 if (HaveClangModules)
2641 Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path);
2642
2643 // Pass through all -fmodules-ignore-macro arguments.
2644 Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
2645 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
2646 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
2647
2648 Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp);
2649
2650 if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) {
2651 if (Args.hasArg(options::OPT_fbuild_session_timestamp))
2652 D.Diag(diag::err_drv_argument_not_allowed_with)
2653 << A->getAsString(Args) << "-fbuild-session-timestamp";
2654
2655 llvm::sys::fs::file_status Status;
2656 if (llvm::sys::fs::status(A->getValue(), Status))
2657 D.Diag(diag::err_drv_no_such_file) << A->getValue();
2658 CmdArgs.push_back(
2659 Args.MakeArgString("-fbuild-session-timestamp=" +
2660 Twine((uint64_t)Status.getLastModificationTime()
2661 .time_since_epoch()
2662 .count())));
2663 }
2664
2665 if (Args.getLastArg(options::OPT_fmodules_validate_once_per_build_session)) {
2666 if (!Args.getLastArg(options::OPT_fbuild_session_timestamp,
2667 options::OPT_fbuild_session_file))
2668 D.Diag(diag::err_drv_modules_validate_once_requires_timestamp);
2669
2670 Args.AddLastArg(CmdArgs,
2671 options::OPT_fmodules_validate_once_per_build_session);
2672 }
2673
Bruno Cardoso Lopes89b9fdb2018-04-18 06:07:49 +00002674 if (Args.hasFlag(options::OPT_fmodules_validate_system_headers,
2675 options::OPT_fno_modules_validate_system_headers,
2676 ImplicitModules))
2677 CmdArgs.push_back("-fmodules-validate-system-headers");
2678
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00002679 Args.AddLastArg(CmdArgs, options::OPT_fmodules_disable_diagnostic_validation);
2680}
2681
Saleem Abdulrasool729379a2017-10-06 23:09:55 +00002682static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T,
2683 ArgStringList &CmdArgs) {
2684 // -fsigned-char is default.
2685 if (const Arg *A = Args.getLastArg(options::OPT_fsigned_char,
2686 options::OPT_fno_signed_char,
2687 options::OPT_funsigned_char,
2688 options::OPT_fno_unsigned_char)) {
2689 if (A->getOption().matches(options::OPT_funsigned_char) ||
2690 A->getOption().matches(options::OPT_fno_signed_char)) {
2691 CmdArgs.push_back("-fno-signed-char");
2692 }
2693 } else if (!isSignedCharDefault(T)) {
2694 CmdArgs.push_back("-fno-signed-char");
2695 }
2696
Richard Smith3a8244d2018-05-01 05:02:45 +00002697 if (Args.hasFlag(options::OPT_fchar8__t, options::OPT_fno_char8__t, false))
2698 CmdArgs.push_back("-fchar8_t");
2699
Saleem Abdulrasool729379a2017-10-06 23:09:55 +00002700 if (const Arg *A = Args.getLastArg(options::OPT_fshort_wchar,
2701 options::OPT_fno_short_wchar)) {
2702 if (A->getOption().matches(options::OPT_fshort_wchar)) {
2703 CmdArgs.push_back("-fwchar-type=short");
2704 CmdArgs.push_back("-fno-signed-wchar");
2705 } else {
Saleem Abdulrasool8ba8b022017-10-29 06:01:14 +00002706 bool IsARM = T.isARM() || T.isThumb() || T.isAArch64();
Saleem Abdulrasool729379a2017-10-06 23:09:55 +00002707 CmdArgs.push_back("-fwchar-type=int");
Saleem Abdulrasool8ba8b022017-10-29 06:01:14 +00002708 if (IsARM && !(T.isOSWindows() || T.getOS() == llvm::Triple::NetBSD ||
2709 T.getOS() == llvm::Triple::OpenBSD))
2710 CmdArgs.push_back("-fno-signed-wchar");
2711 else
2712 CmdArgs.push_back("-fsigned-wchar");
Saleem Abdulrasool729379a2017-10-06 23:09:55 +00002713 }
2714 }
2715}
2716
Saleem Abdulrasoolb2d4ebb2017-09-01 17:43:59 +00002717static void RenderObjCOptions(const ToolChain &TC, const Driver &D,
2718 const llvm::Triple &T, const ArgList &Args,
2719 ObjCRuntime &Runtime, bool InferCovariantReturns,
2720 const InputInfo &Input, ArgStringList &CmdArgs) {
2721 const llvm::Triple::ArchType Arch = TC.getArch();
2722
2723 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and legacy
2724 // is the default. Except for deployment target of 10.5, next runtime is
2725 // always legacy dispatch and -fno-objc-legacy-dispatch gets ignored silently.
2726 if (Runtime.isNonFragile()) {
2727 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
2728 options::OPT_fno_objc_legacy_dispatch,
2729 Runtime.isLegacyDispatchDefaultForArch(Arch))) {
2730 if (TC.UseObjCMixedDispatch())
2731 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
2732 else
2733 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
2734 }
2735 }
2736
2737 // When ObjectiveC legacy runtime is in effect on MacOSX, turn on the option
2738 // to do Array/Dictionary subscripting by default.
2739 if (Arch == llvm::Triple::x86 && T.isMacOSX() &&
2740 !T.isMacOSXVersionLT(10, 7) &&
2741 Runtime.getKind() == ObjCRuntime::FragileMacOSX && Runtime.isNeXTFamily())
2742 CmdArgs.push_back("-fobjc-subscripting-legacy-runtime");
2743
2744 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
2745 // NOTE: This logic is duplicated in ToolChains.cpp.
2746 if (isObjCAutoRefCount(Args)) {
2747 TC.CheckObjCARC();
2748
2749 CmdArgs.push_back("-fobjc-arc");
2750
2751 // FIXME: It seems like this entire block, and several around it should be
2752 // wrapped in isObjC, but for now we just use it here as this is where it
2753 // was being used previously.
2754 if (types::isCXX(Input.getType()) && types::isObjC(Input.getType())) {
2755 if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
2756 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
2757 else
2758 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
2759 }
2760
2761 // Allow the user to enable full exceptions code emission.
2762 // We default off for Objective-C, on for Objective-C++.
2763 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
2764 options::OPT_fno_objc_arc_exceptions,
2765 /*default=*/types::isCXX(Input.getType())))
2766 CmdArgs.push_back("-fobjc-arc-exceptions");
2767 }
2768
2769 // Silence warning for full exception code emission options when explicitly
2770 // set to use no ARC.
2771 if (Args.hasArg(options::OPT_fno_objc_arc)) {
2772 Args.ClaimAllArgs(options::OPT_fobjc_arc_exceptions);
2773 Args.ClaimAllArgs(options::OPT_fno_objc_arc_exceptions);
2774 }
2775
2776 // -fobjc-infer-related-result-type is the default, except in the Objective-C
2777 // rewriter.
2778 if (InferCovariantReturns)
2779 CmdArgs.push_back("-fno-objc-infer-related-result-type");
2780
2781 // Pass down -fobjc-weak or -fno-objc-weak if present.
2782 if (types::isObjC(Input.getType())) {
2783 auto WeakArg =
2784 Args.getLastArg(options::OPT_fobjc_weak, options::OPT_fno_objc_weak);
2785 if (!WeakArg) {
2786 // nothing to do
2787 } else if (!Runtime.allowsWeak()) {
2788 if (WeakArg->getOption().matches(options::OPT_fobjc_weak))
2789 D.Diag(diag::err_objc_weak_unsupported);
2790 } else {
2791 WeakArg->render(Args, CmdArgs);
2792 }
2793 }
2794}
2795
Saleem Abdulrasool75557fa2017-09-01 18:57:34 +00002796static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args,
2797 ArgStringList &CmdArgs) {
2798 bool CaretDefault = true;
2799 bool ColumnDefault = true;
2800
2801 if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diagnostics_classic,
2802 options::OPT__SLASH_diagnostics_column,
2803 options::OPT__SLASH_diagnostics_caret)) {
2804 switch (A->getOption().getID()) {
2805 case options::OPT__SLASH_diagnostics_caret:
2806 CaretDefault = true;
2807 ColumnDefault = true;
2808 break;
2809 case options::OPT__SLASH_diagnostics_column:
2810 CaretDefault = false;
2811 ColumnDefault = true;
2812 break;
2813 case options::OPT__SLASH_diagnostics_classic:
2814 CaretDefault = false;
2815 ColumnDefault = false;
2816 break;
2817 }
2818 }
2819
2820 // -fcaret-diagnostics is default.
2821 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
2822 options::OPT_fno_caret_diagnostics, CaretDefault))
2823 CmdArgs.push_back("-fno-caret-diagnostics");
2824
2825 // -fdiagnostics-fixit-info is default, only pass non-default.
2826 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
2827 options::OPT_fno_diagnostics_fixit_info))
2828 CmdArgs.push_back("-fno-diagnostics-fixit-info");
2829
2830 // Enable -fdiagnostics-show-option by default.
2831 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
2832 options::OPT_fno_diagnostics_show_option))
2833 CmdArgs.push_back("-fdiagnostics-show-option");
2834
2835 if (const Arg *A =
2836 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
2837 CmdArgs.push_back("-fdiagnostics-show-category");
2838 CmdArgs.push_back(A->getValue());
2839 }
2840
2841 if (Args.hasFlag(options::OPT_fdiagnostics_show_hotness,
2842 options::OPT_fno_diagnostics_show_hotness, false))
2843 CmdArgs.push_back("-fdiagnostics-show-hotness");
2844
2845 if (const Arg *A =
2846 Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) {
2847 std::string Opt =
2848 std::string("-fdiagnostics-hotness-threshold=") + A->getValue();
2849 CmdArgs.push_back(Args.MakeArgString(Opt));
2850 }
2851
2852 if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
2853 CmdArgs.push_back("-fdiagnostics-format");
2854 CmdArgs.push_back(A->getValue());
2855 }
2856
2857 if (const Arg *A = Args.getLastArg(
2858 options::OPT_fdiagnostics_show_note_include_stack,
2859 options::OPT_fno_diagnostics_show_note_include_stack)) {
2860 const Option &O = A->getOption();
2861 if (O.matches(options::OPT_fdiagnostics_show_note_include_stack))
2862 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
2863 else
2864 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
2865 }
2866
2867 // Color diagnostics are parsed by the driver directly from argv and later
2868 // re-parsed to construct this job; claim any possible color diagnostic here
2869 // to avoid warn_drv_unused_argument and diagnose bad
2870 // OPT_fdiagnostics_color_EQ values.
2871 for (const Arg *A : Args) {
2872 const Option &O = A->getOption();
2873 if (!O.matches(options::OPT_fcolor_diagnostics) &&
2874 !O.matches(options::OPT_fdiagnostics_color) &&
2875 !O.matches(options::OPT_fno_color_diagnostics) &&
2876 !O.matches(options::OPT_fno_diagnostics_color) &&
2877 !O.matches(options::OPT_fdiagnostics_color_EQ))
2878 continue;
2879
2880 if (O.matches(options::OPT_fdiagnostics_color_EQ)) {
2881 StringRef Value(A->getValue());
2882 if (Value != "always" && Value != "never" && Value != "auto")
2883 D.Diag(diag::err_drv_clang_unsupported)
2884 << ("-fdiagnostics-color=" + Value).str();
2885 }
2886 A->claim();
2887 }
2888
2889 if (D.getDiags().getDiagnosticOptions().ShowColors)
2890 CmdArgs.push_back("-fcolor-diagnostics");
2891
2892 if (Args.hasArg(options::OPT_fansi_escape_codes))
2893 CmdArgs.push_back("-fansi-escape-codes");
2894
2895 if (!Args.hasFlag(options::OPT_fshow_source_location,
2896 options::OPT_fno_show_source_location))
2897 CmdArgs.push_back("-fno-show-source-location");
2898
2899 if (Args.hasArg(options::OPT_fdiagnostics_absolute_paths))
2900 CmdArgs.push_back("-fdiagnostics-absolute-paths");
2901
2902 if (!Args.hasFlag(options::OPT_fshow_column, options::OPT_fno_show_column,
2903 ColumnDefault))
2904 CmdArgs.push_back("-fno-show-column");
2905
2906 if (!Args.hasFlag(options::OPT_fspell_checking,
2907 options::OPT_fno_spell_checking))
2908 CmdArgs.push_back("-fno-spell-checking");
2909}
2910
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00002911static void RenderDebugOptions(const ToolChain &TC, const Driver &D,
2912 const llvm::Triple &T, const ArgList &Args,
2913 bool EmitCodeView, bool IsWindowsMSVC,
2914 ArgStringList &CmdArgs,
2915 codegenoptions::DebugInfoKind &DebugInfoKind,
2916 const Arg *&SplitDWARFArg) {
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00002917 if (Args.hasFlag(options::OPT_fdebug_info_for_profiling,
Alexey Bataevb83b4e42018-07-27 19:45:14 +00002918 options::OPT_fno_debug_info_for_profiling, false) &&
2919 checkDebugInfoOption(
2920 Args.getLastArg(options::OPT_fdebug_info_for_profiling), Args, D, TC))
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00002921 CmdArgs.push_back("-fdebug-info-for-profiling");
2922
2923 // The 'g' groups options involve a somewhat intricate sequence of decisions
2924 // about what to pass from the driver to the frontend, but by the time they
2925 // reach cc1 they've been factored into three well-defined orthogonal choices:
2926 // * what level of debug info to generate
2927 // * what dwarf version to write
2928 // * what debugger tuning to use
2929 // This avoids having to monkey around further in cc1 other than to disable
2930 // codeview if not running in a Windows environment. Perhaps even that
2931 // decision should be made in the driver as well though.
2932 unsigned DWARFVersion = 0;
2933 llvm::DebuggerKind DebuggerTuning = TC.getDefaultDebuggerTuning();
2934
2935 bool SplitDWARFInlining =
2936 Args.hasFlag(options::OPT_fsplit_dwarf_inlining,
2937 options::OPT_fno_split_dwarf_inlining, true);
2938
2939 Args.ClaimAllArgs(options::OPT_g_Group);
2940
2941 SplitDWARFArg = Args.getLastArg(options::OPT_gsplit_dwarf);
2942
Alexey Bataevb83b4e42018-07-27 19:45:14 +00002943 if (SplitDWARFArg && !checkDebugInfoOption(SplitDWARFArg, Args, D, TC)) {
2944 SplitDWARFArg = nullptr;
2945 SplitDWARFInlining = false;
2946 }
2947
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00002948 if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) {
Alexey Bataevb83b4e42018-07-27 19:45:14 +00002949 if (checkDebugInfoOption(A, Args, D, TC)) {
2950 // If the last option explicitly specified a debug-info level, use it.
2951 if (A->getOption().matches(options::OPT_gN_Group)) {
2952 DebugInfoKind = DebugLevelToInfoKind(*A);
2953 // If you say "-gsplit-dwarf -gline-tables-only", -gsplit-dwarf loses.
2954 // But -gsplit-dwarf is not a g_group option, hence we have to check the
2955 // order explicitly. If -gsplit-dwarf wins, we fix DebugInfoKind later.
2956 // This gets a bit more complicated if you've disabled inline info in
2957 // the skeleton CUs (SplitDWARFInlining) - then there's value in
2958 // composing split-dwarf and line-tables-only, so let those compose
2959 // naturally in that case. And if you just turned off debug info,
2960 // (-gsplit-dwarf -g0) - do that.
2961 if (SplitDWARFArg) {
2962 if (A->getIndex() > SplitDWARFArg->getIndex()) {
2963 if (DebugInfoKind == codegenoptions::NoDebugInfo ||
Alexey Bataev80e1b5e2018-08-31 13:56:14 +00002964 DebugInfoKind == codegenoptions::DebugDirectivesOnly ||
Alexey Bataevb83b4e42018-07-27 19:45:14 +00002965 (DebugInfoKind == codegenoptions::DebugLineTablesOnly &&
2966 SplitDWARFInlining))
2967 SplitDWARFArg = nullptr;
2968 } else if (SplitDWARFInlining)
2969 DebugInfoKind = codegenoptions::NoDebugInfo;
2970 }
2971 } else {
2972 // For any other 'g' option, use Limited.
2973 DebugInfoKind = codegenoptions::LimitedDebugInfo;
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00002974 }
2975 } else {
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00002976 DebugInfoKind = codegenoptions::LimitedDebugInfo;
2977 }
2978 }
2979
2980 // If a debugger tuning argument appeared, remember it.
2981 if (const Arg *A =
2982 Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) {
Alexey Bataevb83b4e42018-07-27 19:45:14 +00002983 if (checkDebugInfoOption(A, Args, D, TC)) {
2984 if (A->getOption().matches(options::OPT_glldb))
2985 DebuggerTuning = llvm::DebuggerKind::LLDB;
2986 else if (A->getOption().matches(options::OPT_gsce))
2987 DebuggerTuning = llvm::DebuggerKind::SCE;
2988 else
2989 DebuggerTuning = llvm::DebuggerKind::GDB;
2990 }
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00002991 }
2992
2993 // If a -gdwarf argument appeared, remember it.
2994 if (const Arg *A =
2995 Args.getLastArg(options::OPT_gdwarf_2, options::OPT_gdwarf_3,
2996 options::OPT_gdwarf_4, options::OPT_gdwarf_5))
Alexey Bataevb83b4e42018-07-27 19:45:14 +00002997 if (checkDebugInfoOption(A, Args, D, TC))
2998 DWARFVersion = DwarfVersionNum(A->getSpelling());
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00002999
3000 // Forward -gcodeview. EmitCodeView might have been set by CL-compatibility
3001 // argument parsing.
Reid Kleckner54af3e72018-02-26 22:55:33 +00003002 if (EmitCodeView) {
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003003 if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) {
3004 EmitCodeView = checkDebugInfoOption(A, Args, D, TC);
3005 if (EmitCodeView) {
3006 // DWARFVersion remains at 0 if no explicit choice was made.
3007 CmdArgs.push_back("-gcodeview");
3008 }
3009 }
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003010 }
3011
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003012 if (!EmitCodeView && DWARFVersion == 0 &&
3013 DebugInfoKind != codegenoptions::NoDebugInfo)
3014 DWARFVersion = TC.GetDefaultDwarfVersion();
3015
Alexey Bataev80e1b5e2018-08-31 13:56:14 +00003016 // -gline-directives-only supported only for the DWARF debug info.
3017 if (DWARFVersion == 0 && DebugInfoKind == codegenoptions::DebugDirectivesOnly)
3018 DebugInfoKind = codegenoptions::NoDebugInfo;
3019
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003020 // We ignore flag -gstrict-dwarf for now.
3021 // And we handle flag -grecord-gcc-switches later with DWARFDebugFlags.
3022 Args.ClaimAllArgs(options::OPT_g_flags_Group);
3023
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003024 // Column info is included by default for everything except SCE and
3025 // CodeView. Clang doesn't track end columns, just starting columns, which,
3026 // in theory, is fine for CodeView (and PDB). In practice, however, the
3027 // Microsoft debuggers don't handle missing end columns well, so it's better
3028 // not to include any column info.
3029 if (const Arg *A = Args.getLastArg(options::OPT_gcolumn_info))
3030 (void)checkDebugInfoOption(A, Args, D, TC);
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003031 if (Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info,
Martin Storsjof9fa17b2018-05-08 20:55:23 +00003032 /*Default=*/!EmitCodeView &&
Paul Robinsona8280812017-09-29 21:25:07 +00003033 DebuggerTuning != llvm::DebuggerKind::SCE))
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003034 CmdArgs.push_back("-dwarf-column-info");
3035
3036 // FIXME: Move backend command line options to the module.
Alexey Bataev80e1b5e2018-08-31 13:56:14 +00003037 // If -gline-tables-only or -gline-directives-only is the last option it wins.
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003038 if (const Arg *A = Args.getLastArg(options::OPT_gmodules))
3039 if (checkDebugInfoOption(A, Args, D, TC)) {
Alexey Bataev80e1b5e2018-08-31 13:56:14 +00003040 if (DebugInfoKind != codegenoptions::DebugLineTablesOnly &&
3041 DebugInfoKind != codegenoptions::DebugDirectivesOnly) {
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003042 DebugInfoKind = codegenoptions::LimitedDebugInfo;
3043 CmdArgs.push_back("-dwarf-ext-refs");
3044 CmdArgs.push_back("-fmodule-format=obj");
3045 }
3046 }
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003047
3048 // -gsplit-dwarf should turn on -g and enable the backend dwarf
3049 // splitting and extraction.
3050 // FIXME: Currently only works on Linux.
3051 if (T.isOSLinux()) {
3052 if (!SplitDWARFInlining)
3053 CmdArgs.push_back("-fno-split-dwarf-inlining");
3054
3055 if (SplitDWARFArg) {
3056 if (DebugInfoKind == codegenoptions::NoDebugInfo)
3057 DebugInfoKind = codegenoptions::LimitedDebugInfo;
3058 CmdArgs.push_back("-enable-split-dwarf");
3059 }
3060 }
3061
3062 // After we've dealt with all combinations of things that could
3063 // make DebugInfoKind be other than None or DebugLineTablesOnly,
3064 // figure out if we need to "upgrade" it to standalone debug info.
3065 // We parse these two '-f' options whether or not they will be used,
3066 // to claim them even if you wrote "-fstandalone-debug -gline-tables-only"
3067 bool NeedFullDebug = Args.hasFlag(options::OPT_fstandalone_debug,
3068 options::OPT_fno_standalone_debug,
3069 TC.GetDefaultStandaloneDebug());
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003070 if (const Arg *A = Args.getLastArg(options::OPT_fstandalone_debug))
3071 (void)checkDebugInfoOption(A, Args, D, TC);
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003072 if (DebugInfoKind == codegenoptions::LimitedDebugInfo && NeedFullDebug)
3073 DebugInfoKind = codegenoptions::FullDebugInfo;
3074
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003075 if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source,
3076 false)) {
Scott Lindera2fbcef2018-02-26 17:32:31 +00003077 // Source embedding is a vendor extension to DWARF v5. By now we have
3078 // checked if a DWARF version was stated explicitly, and have otherwise
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003079 // fallen back to the target default, so if this is still not at least 5
3080 // we emit an error.
3081 const Arg *A = Args.getLastArg(options::OPT_gembed_source);
Scott Lindera2fbcef2018-02-26 17:32:31 +00003082 if (DWARFVersion < 5)
3083 D.Diag(diag::err_drv_argument_only_allowed_with)
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003084 << A->getAsString(Args) << "-gdwarf-5";
3085 else if (checkDebugInfoOption(A, Args, D, TC))
3086 CmdArgs.push_back("-gembed-source");
Scott Lindera2fbcef2018-02-26 17:32:31 +00003087 }
3088
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003089 RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DWARFVersion,
3090 DebuggerTuning);
3091
3092 // -fdebug-macro turns on macro debug info generation.
3093 if (Args.hasFlag(options::OPT_fdebug_macro, options::OPT_fno_debug_macro,
3094 false))
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003095 if (checkDebugInfoOption(Args.getLastArg(options::OPT_fdebug_macro), Args,
3096 D, TC))
3097 CmdArgs.push_back("-debug-info-macro");
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003098
3099 // -ggnu-pubnames turns on gnu style pubnames in the backend.
David Blaikie65864522018-08-20 20:14:08 +00003100 const auto *PubnamesArg =
3101 Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames,
3102 options::OPT_gpubnames, options::OPT_gno_pubnames);
Pavel Labathc1e0c342018-09-06 17:01:45 +00003103 if (SplitDWARFArg || DebuggerTuning == llvm::DebuggerKind::LLDB ||
David Blaikie65864522018-08-20 20:14:08 +00003104 (PubnamesArg && checkDebugInfoOption(PubnamesArg, Args, D, TC)))
3105 if (!PubnamesArg ||
3106 (!PubnamesArg->getOption().matches(options::OPT_gno_gnu_pubnames) &&
3107 !PubnamesArg->getOption().matches(options::OPT_gno_pubnames)))
3108 CmdArgs.push_back(PubnamesArg && PubnamesArg->getOption().matches(
3109 options::OPT_gpubnames)
3110 ? "-gpubnames"
3111 : "-ggnu-pubnames");
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003112
3113 // -gdwarf-aranges turns on the emission of the aranges section in the
3114 // backend.
Paul Robinsona8280812017-09-29 21:25:07 +00003115 // Always enabled for SCE tuning.
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003116 bool NeedAranges = DebuggerTuning == llvm::DebuggerKind::SCE;
3117 if (const Arg *A = Args.getLastArg(options::OPT_gdwarf_aranges))
3118 NeedAranges = checkDebugInfoOption(A, Args, D, TC) || NeedAranges;
3119 if (NeedAranges) {
Eli Friedman01d349b2018-04-12 22:21:36 +00003120 CmdArgs.push_back("-mllvm");
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003121 CmdArgs.push_back("-generate-arange-section");
3122 }
3123
3124 if (Args.hasFlag(options::OPT_fdebug_types_section,
3125 options::OPT_fno_debug_types_section, false)) {
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003126 if (!T.isOSBinFormatELF()) {
Jonas Devlieghere488bd012018-07-23 17:50:15 +00003127 D.Diag(diag::err_drv_unsupported_opt_for_target)
3128 << Args.getLastArg(options::OPT_fdebug_types_section)
3129 ->getAsString(Args)
3130 << T.getTriple();
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003131 } else if (checkDebugInfoOption(
3132 Args.getLastArg(options::OPT_fdebug_types_section), Args, D,
3133 TC)) {
3134 CmdArgs.push_back("-mllvm");
3135 CmdArgs.push_back("-generate-type-units");
3136 }
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003137 }
3138
Paul Robinson1787f812017-09-28 18:37:02 +00003139 // Decide how to render forward declarations of template instantiations.
3140 // SCE wants full descriptions, others just get them in the name.
3141 if (DebuggerTuning == llvm::DebuggerKind::SCE)
3142 CmdArgs.push_back("-debug-forward-template-params");
3143
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003144 // Do we need to explicitly import anonymous namespaces into the parent
3145 // scope?
Paul Robinsona8280812017-09-29 21:25:07 +00003146 if (DebuggerTuning == llvm::DebuggerKind::SCE)
3147 CmdArgs.push_back("-dwarf-explicit-import");
3148
Alexey Bataevb83b4e42018-07-27 19:45:14 +00003149 RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC);
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003150}
3151
David L. Jonesf561aba2017-03-08 01:02:16 +00003152void Clang::ConstructJob(Compilation &C, const JobAction &JA,
3153 const InputInfo &Output, const InputInfoList &Inputs,
3154 const ArgList &Args, const char *LinkingOutput) const {
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003155 const auto &TC = getToolChain();
3156 const llvm::Triple &RawTriple = TC.getTriple();
3157 const llvm::Triple &Triple = TC.getEffectiveTriple();
David L. Jonesf561aba2017-03-08 01:02:16 +00003158 const std::string &TripleStr = Triple.getTriple();
3159
3160 bool KernelOrKext =
3161 Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003162 const Driver &D = TC.getDriver();
David L. Jonesf561aba2017-03-08 01:02:16 +00003163 ArgStringList CmdArgs;
3164
3165 // Check number of inputs for sanity. We need at least one input.
3166 assert(Inputs.size() >= 1 && "Must have at least one input.");
Yaxun Liu398612b2018-05-08 21:02:12 +00003167 // CUDA/HIP compilation may have multiple inputs (source file + results of
David L. Jonesf561aba2017-03-08 01:02:16 +00003168 // device-side compilations). OpenMP device jobs also take the host IR as a
Richard Smithcd35eff2018-09-15 01:21:16 +00003169 // second input. Module precompilation accepts a list of header files to
3170 // include as part of the module. All other jobs are expected to have exactly
3171 // one input.
David L. Jonesf561aba2017-03-08 01:02:16 +00003172 bool IsCuda = JA.isOffloading(Action::OFK_Cuda);
Yaxun Liu398612b2018-05-08 21:02:12 +00003173 bool IsHIP = JA.isOffloading(Action::OFK_HIP);
David L. Jonesf561aba2017-03-08 01:02:16 +00003174 bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP);
Richard Smithcd35eff2018-09-15 01:21:16 +00003175 bool IsModulePrecompile =
3176 isa<PrecompileJobAction>(JA) && JA.getType() == types::TY_ModuleFile;
3177 bool IsHeaderModulePrecompile = isa<HeaderModulePrecompileJobAction>(JA);
3178
3179 // A header module compilation doesn't have a main input file, so invent a
3180 // fake one as a placeholder.
3181 // FIXME: Pick the language based on the header file language.
3182 const char *ModuleName = [&]{
3183 auto *ModuleNameArg = Args.getLastArg(options::OPT_fmodule_name_EQ);
3184 return ModuleNameArg ? ModuleNameArg->getValue() : "";
3185 }();
3186 InputInfo HeaderModuleInput(types::TY_CXXModule, ModuleName, ModuleName);
3187
3188 const InputInfo &Input =
3189 IsHeaderModulePrecompile ? HeaderModuleInput : Inputs[0];
3190
3191 InputInfoList ModuleHeaderInputs;
3192 const InputInfo *CudaDeviceInput = nullptr;
3193 const InputInfo *OpenMPDeviceInput = nullptr;
3194 for (const InputInfo &I : Inputs) {
3195 if (&I == &Input) {
3196 // This is the primary input.
3197 } else if (IsModulePrecompile &&
3198 types::getPrecompiledType(I.getType()) == types::TY_PCH) {
3199 types::ID Expected =
3200 types::lookupHeaderTypeForSourceType(Inputs[0].getType());
3201 if (I.getType() != Expected) {
3202 D.Diag(diag::err_drv_module_header_wrong_kind)
3203 << I.getFilename() << types::getTypeName(I.getType())
3204 << types::getTypeName(Expected);
3205 }
3206 ModuleHeaderInputs.push_back(I);
3207 } else if ((IsCuda || IsHIP) && !CudaDeviceInput) {
3208 CudaDeviceInput = &I;
3209 } else if (IsOpenMPDevice && !OpenMPDeviceInput) {
3210 OpenMPDeviceInput = &I;
3211 } else {
3212 llvm_unreachable("unexpectedly given multiple inputs");
3213 }
3214 }
David L. Jonesf561aba2017-03-08 01:02:16 +00003215
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003216 const llvm::Triple *AuxTriple = IsCuda ? TC.getAuxTriple() : nullptr;
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003217 bool IsWindowsGNU = RawTriple.isWindowsGNUEnvironment();
3218 bool IsWindowsCygnus = RawTriple.isWindowsCygwinEnvironment();
3219 bool IsWindowsMSVC = RawTriple.isWindowsMSVCEnvironment();
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003220 bool IsIAMCU = RawTriple.isOSIAMCU();
David L. Jonesf561aba2017-03-08 01:02:16 +00003221
Yaxun Liu398612b2018-05-08 21:02:12 +00003222 // Adjust IsWindowsXYZ for CUDA/HIP compilations. Even when compiling in
3223 // device mode (i.e., getToolchain().getTriple() is NVPTX/AMDGCN, not
3224 // Windows), we need to pass Windows-specific flags to cc1.
3225 if (IsCuda || IsHIP) {
David L. Jonesf561aba2017-03-08 01:02:16 +00003226 IsWindowsMSVC |= AuxTriple && AuxTriple->isWindowsMSVCEnvironment();
3227 IsWindowsGNU |= AuxTriple && AuxTriple->isWindowsGNUEnvironment();
3228 IsWindowsCygnus |= AuxTriple && AuxTriple->isWindowsCygwinEnvironment();
3229 }
3230
3231 // C++ is not supported for IAMCU.
3232 if (IsIAMCU && types::isCXX(Input.getType()))
3233 D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU";
3234
3235 // Invoke ourselves in -cc1 mode.
3236 //
3237 // FIXME: Implement custom jobs for internal actions.
3238 CmdArgs.push_back("-cc1");
3239
3240 // Add the "effective" target triple.
3241 CmdArgs.push_back("-triple");
3242 CmdArgs.push_back(Args.MakeArgString(TripleStr));
3243
3244 if (const Arg *MJ = Args.getLastArg(options::OPT_MJ)) {
3245 DumpCompilationDatabase(C, MJ->getValue(), TripleStr, Output, Input, Args);
3246 Args.ClaimAllArgs(options::OPT_MJ);
3247 }
3248
Yaxun Liu398612b2018-05-08 21:02:12 +00003249 if (IsCuda || IsHIP) {
3250 // We have to pass the triple of the host if compiling for a CUDA/HIP device
3251 // and vice-versa.
David L. Jonesf561aba2017-03-08 01:02:16 +00003252 std::string NormalizedTriple;
Yaxun Liu398612b2018-05-08 21:02:12 +00003253 if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
3254 JA.isDeviceOffloading(Action::OFK_HIP))
David L. Jonesf561aba2017-03-08 01:02:16 +00003255 NormalizedTriple = C.getSingleOffloadToolChain<Action::OFK_Host>()
3256 ->getTriple()
3257 .normalize();
3258 else
Yaxun Liu398612b2018-05-08 21:02:12 +00003259 NormalizedTriple =
3260 (IsCuda ? C.getSingleOffloadToolChain<Action::OFK_Cuda>()
3261 : C.getSingleOffloadToolChain<Action::OFK_HIP>())
3262 ->getTriple()
3263 .normalize();
David L. Jonesf561aba2017-03-08 01:02:16 +00003264
3265 CmdArgs.push_back("-aux-triple");
3266 CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
3267 }
3268
Gheorghe-Teodor Bercea59d7b772017-06-29 15:49:03 +00003269 if (IsOpenMPDevice) {
3270 // We have to pass the triple of the host if compiling for an OpenMP device.
3271 std::string NormalizedTriple =
3272 C.getSingleOffloadToolChain<Action::OFK_Host>()
3273 ->getTriple()
3274 .normalize();
3275 CmdArgs.push_back("-aux-triple");
3276 CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
3277 }
3278
David L. Jonesf561aba2017-03-08 01:02:16 +00003279 if (Triple.isOSWindows() && (Triple.getArch() == llvm::Triple::arm ||
3280 Triple.getArch() == llvm::Triple::thumb)) {
3281 unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 4 : 6;
3282 unsigned Version;
3283 Triple.getArchName().substr(Offset).getAsInteger(10, Version);
3284 if (Version < 7)
3285 D.Diag(diag::err_target_unsupported_arch) << Triple.getArchName()
3286 << TripleStr;
3287 }
3288
3289 // Push all default warning arguments that are specific to
3290 // the given target. These come before user provided warning options
3291 // are provided.
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003292 TC.addClangWarningOptions(CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00003293
3294 // Select the appropriate action.
3295 RewriteKind rewriteKind = RK_None;
3296
3297 if (isa<AnalyzeJobAction>(JA)) {
3298 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
3299 CmdArgs.push_back("-analyze");
3300 } else if (isa<MigrateJobAction>(JA)) {
3301 CmdArgs.push_back("-migrate");
3302 } else if (isa<PreprocessJobAction>(JA)) {
3303 if (Output.getType() == types::TY_Dependencies)
3304 CmdArgs.push_back("-Eonly");
3305 else {
3306 CmdArgs.push_back("-E");
3307 if (Args.hasArg(options::OPT_rewrite_objc) &&
3308 !Args.hasArg(options::OPT_g_Group))
3309 CmdArgs.push_back("-P");
3310 }
3311 } else if (isa<AssembleJobAction>(JA)) {
3312 CmdArgs.push_back("-emit-obj");
3313
3314 CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
3315
3316 // Also ignore explicit -force_cpusubtype_ALL option.
3317 (void)Args.hasArg(options::OPT_force__cpusubtype__ALL);
3318 } else if (isa<PrecompileJobAction>(JA)) {
3319 // Use PCH if the user requested it.
3320 bool UsePCH = D.CCCUsePCH;
3321
3322 if (JA.getType() == types::TY_Nothing)
3323 CmdArgs.push_back("-fsyntax-only");
3324 else if (JA.getType() == types::TY_ModuleFile)
Richard Smithcd35eff2018-09-15 01:21:16 +00003325 CmdArgs.push_back(IsHeaderModulePrecompile
3326 ? "-emit-header-module"
3327 : "-emit-module-interface");
David L. Jonesf561aba2017-03-08 01:02:16 +00003328 else if (UsePCH)
3329 CmdArgs.push_back("-emit-pch");
3330 else
3331 CmdArgs.push_back("-emit-pth");
3332 } else if (isa<VerifyPCHJobAction>(JA)) {
3333 CmdArgs.push_back("-verify-pch");
3334 } else {
3335 assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) &&
3336 "Invalid action for clang tool.");
3337 if (JA.getType() == types::TY_Nothing) {
3338 CmdArgs.push_back("-fsyntax-only");
3339 } else if (JA.getType() == types::TY_LLVM_IR ||
3340 JA.getType() == types::TY_LTO_IR) {
3341 CmdArgs.push_back("-emit-llvm");
3342 } else if (JA.getType() == types::TY_LLVM_BC ||
3343 JA.getType() == types::TY_LTO_BC) {
3344 CmdArgs.push_back("-emit-llvm-bc");
3345 } else if (JA.getType() == types::TY_PP_Asm) {
3346 CmdArgs.push_back("-S");
3347 } else if (JA.getType() == types::TY_AST) {
3348 CmdArgs.push_back("-emit-pch");
3349 } else if (JA.getType() == types::TY_ModuleFile) {
3350 CmdArgs.push_back("-module-file-info");
3351 } else if (JA.getType() == types::TY_RewrittenObjC) {
3352 CmdArgs.push_back("-rewrite-objc");
3353 rewriteKind = RK_NonFragile;
3354 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
3355 CmdArgs.push_back("-rewrite-objc");
3356 rewriteKind = RK_Fragile;
3357 } else {
3358 assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!");
3359 }
3360
3361 // Preserve use-list order by default when emitting bitcode, so that
3362 // loading the bitcode up in 'opt' or 'llc' and running passes gives the
3363 // same result as running passes here. For LTO, we don't need to preserve
3364 // the use-list order, since serialization to bitcode is part of the flow.
3365 if (JA.getType() == types::TY_LLVM_BC)
3366 CmdArgs.push_back("-emit-llvm-uselists");
3367
Artem Belevichecb178b2018-03-21 22:22:59 +00003368 // Device-side jobs do not support LTO.
3369 bool isDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) ||
3370 JA.isDeviceOffloading(Action::OFK_Host));
3371
3372 if (D.isUsingLTO() && !isDeviceOffloadAction) {
David L. Jonesf561aba2017-03-08 01:02:16 +00003373 Args.AddLastArg(CmdArgs, options::OPT_flto, options::OPT_flto_EQ);
3374
Paul Robinsond23f2a82017-07-13 21:25:47 +00003375 // The Darwin and PS4 linkers currently use the legacy LTO API, which
3376 // does not support LTO unit features (CFI, whole program vtable opt)
3377 // under ThinLTO.
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003378 if (!(RawTriple.isOSDarwin() || RawTriple.isPS4()) ||
David L. Jonesf561aba2017-03-08 01:02:16 +00003379 D.getLTOMode() == LTOK_Full)
3380 CmdArgs.push_back("-flto-unit");
3381 }
3382 }
3383
3384 if (const Arg *A = Args.getLastArg(options::OPT_fthinlto_index_EQ)) {
3385 if (!types::isLLVMIR(Input.getType()))
3386 D.Diag(diag::err_drv_argument_only_allowed_with) << A->getAsString(Args)
3387 << "-x ir";
3388 Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ);
3389 }
3390
Teresa Johnson6e5cec22018-04-17 20:21:53 +00003391 if (Args.getLastArg(options::OPT_save_temps_EQ))
Teresa Johnson9e4321c2018-04-17 16:39:25 +00003392 Args.AddLastArg(CmdArgs, options::OPT_save_temps_EQ);
3393
David L. Jonesf561aba2017-03-08 01:02:16 +00003394 // Embed-bitcode option.
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00003395 // Only white-listed flags below are allowed to be embedded.
David L. Jonesf561aba2017-03-08 01:02:16 +00003396 if (C.getDriver().embedBitcodeInObject() && !C.getDriver().isUsingLTO() &&
3397 (isa<BackendJobAction>(JA) || isa<AssembleJobAction>(JA))) {
3398 // Add flags implied by -fembed-bitcode.
3399 Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ);
3400 // Disable all llvm IR level optimizations.
3401 CmdArgs.push_back("-disable-llvm-passes");
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00003402
3403 // reject options that shouldn't be supported in bitcode
3404 // also reject kernel/kext
3405 static const constexpr unsigned kBitcodeOptionBlacklist[] = {
3406 options::OPT_mkernel,
3407 options::OPT_fapple_kext,
3408 options::OPT_ffunction_sections,
3409 options::OPT_fno_function_sections,
3410 options::OPT_fdata_sections,
3411 options::OPT_fno_data_sections,
3412 options::OPT_funique_section_names,
3413 options::OPT_fno_unique_section_names,
3414 options::OPT_mrestrict_it,
3415 options::OPT_mno_restrict_it,
3416 options::OPT_mstackrealign,
3417 options::OPT_mno_stackrealign,
3418 options::OPT_mstack_alignment,
3419 options::OPT_mcmodel_EQ,
3420 options::OPT_mlong_calls,
3421 options::OPT_mno_long_calls,
3422 options::OPT_ggnu_pubnames,
3423 options::OPT_gdwarf_aranges,
3424 options::OPT_fdebug_types_section,
3425 options::OPT_fno_debug_types_section,
3426 options::OPT_fdwarf_directory_asm,
3427 options::OPT_fno_dwarf_directory_asm,
3428 options::OPT_mrelax_all,
3429 options::OPT_mno_relax_all,
3430 options::OPT_ftrap_function_EQ,
3431 options::OPT_ffixed_r9,
3432 options::OPT_mfix_cortex_a53_835769,
3433 options::OPT_mno_fix_cortex_a53_835769,
3434 options::OPT_ffixed_x18,
3435 options::OPT_mglobal_merge,
3436 options::OPT_mno_global_merge,
3437 options::OPT_mred_zone,
3438 options::OPT_mno_red_zone,
3439 options::OPT_Wa_COMMA,
3440 options::OPT_Xassembler,
3441 options::OPT_mllvm,
3442 };
3443 for (const auto &A : Args)
3444 if (std::find(std::begin(kBitcodeOptionBlacklist),
3445 std::end(kBitcodeOptionBlacklist),
3446 A->getOption().getID()) !=
3447 std::end(kBitcodeOptionBlacklist))
3448 D.Diag(diag::err_drv_unsupported_embed_bitcode) << A->getSpelling();
3449
3450 // Render the CodeGen options that need to be passed.
3451 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
3452 options::OPT_fno_optimize_sibling_calls))
3453 CmdArgs.push_back("-mdisable-tail-calls");
3454
3455 RenderFloatingPointOptions(TC, D, isOptimizationLevelFast(Args), Args,
3456 CmdArgs);
3457
3458 // Render ABI arguments
3459 switch (TC.getArch()) {
3460 default: break;
3461 case llvm::Triple::arm:
3462 case llvm::Triple::armeb:
3463 case llvm::Triple::thumbeb:
3464 RenderARMABI(Triple, Args, CmdArgs);
3465 break;
3466 case llvm::Triple::aarch64:
3467 case llvm::Triple::aarch64_be:
3468 RenderAArch64ABI(Triple, Args, CmdArgs);
3469 break;
3470 }
3471
3472 // Optimization level for CodeGen.
3473 if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) {
3474 if (A->getOption().matches(options::OPT_O4)) {
3475 CmdArgs.push_back("-O3");
3476 D.Diag(diag::warn_O4_is_O3);
3477 } else {
3478 A->render(Args, CmdArgs);
3479 }
3480 }
3481
3482 // Input/Output file.
3483 if (Output.getType() == types::TY_Dependencies) {
3484 // Handled with other dependency code.
3485 } else if (Output.isFilename()) {
3486 CmdArgs.push_back("-o");
3487 CmdArgs.push_back(Output.getFilename());
3488 } else {
3489 assert(Output.isNothing() && "Input output.");
3490 }
3491
3492 for (const auto &II : Inputs) {
3493 addDashXForInput(Args, II, CmdArgs);
3494 if (II.isFilename())
3495 CmdArgs.push_back(II.getFilename());
3496 else
3497 II.getInputArg().renderAsInput(Args, CmdArgs);
3498 }
3499
3500 C.addCommand(llvm::make_unique<Command>(JA, *this, D.getClangProgramPath(),
3501 CmdArgs, Inputs));
3502 return;
David L. Jonesf561aba2017-03-08 01:02:16 +00003503 }
Saleem Abdulrasool51313bc2018-09-24 23:50:02 +00003504
David L. Jonesf561aba2017-03-08 01:02:16 +00003505 if (C.getDriver().embedBitcodeMarkerOnly() && !C.getDriver().isUsingLTO())
3506 CmdArgs.push_back("-fembed-bitcode=marker");
3507
3508 // We normally speed up the clang process a bit by skipping destructors at
3509 // exit, but when we're generating diagnostics we can rely on some of the
3510 // cleanup.
3511 if (!C.isForDiagnostics())
3512 CmdArgs.push_back("-disable-free");
3513
David L. Jonesf561aba2017-03-08 01:02:16 +00003514#ifdef NDEBUG
Eric Fiselier123c7492018-02-07 18:36:51 +00003515 const bool IsAssertBuild = false;
3516#else
3517 const bool IsAssertBuild = true;
David L. Jonesf561aba2017-03-08 01:02:16 +00003518#endif
3519
Eric Fiselier123c7492018-02-07 18:36:51 +00003520 // Disable the verification pass in -asserts builds.
3521 if (!IsAssertBuild)
Eric Fiseliercca7ddd2018-02-07 19:17:03 +00003522 CmdArgs.push_back("-disable-llvm-verifier");
Eric Fiselier123c7492018-02-07 18:36:51 +00003523
3524 // Discard value names in assert builds unless otherwise specified.
Eric Fiseliera06ca4b2018-02-14 20:56:52 +00003525 if (Args.hasFlag(options::OPT_fdiscard_value_names,
3526 options::OPT_fno_discard_value_names, !IsAssertBuild))
Eric Fiselier123c7492018-02-07 18:36:51 +00003527 CmdArgs.push_back("-discard-value-names");
3528
David L. Jonesf561aba2017-03-08 01:02:16 +00003529 // Set the main file name, so that debug info works even with
3530 // -save-temps.
3531 CmdArgs.push_back("-main-file-name");
3532 CmdArgs.push_back(getBaseInputName(Args, Input));
3533
3534 // Some flags which affect the language (via preprocessor
3535 // defines).
3536 if (Args.hasArg(options::OPT_static))
3537 CmdArgs.push_back("-static-define");
3538
Martin Storsjo434ef832018-08-06 19:48:44 +00003539 if (Args.hasArg(options::OPT_municode))
3540 CmdArgs.push_back("-DUNICODE");
3541
Saleem Abdulrasool24aafa52017-08-30 14:18:08 +00003542 if (isa<AnalyzeJobAction>(JA))
3543 RenderAnalyzerOptions(Args, CmdArgs, Triple, Input);
David L. Jonesf561aba2017-03-08 01:02:16 +00003544
3545 CheckCodeGenerationOptions(D, Args);
3546
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003547 unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args);
Saleem Abdulrasool3fe5b7a2018-04-19 23:14:57 +00003548 assert(FunctionAlignment <= 31 && "function alignment will be truncated!");
3549 if (FunctionAlignment) {
3550 CmdArgs.push_back("-function-alignment");
3551 CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment)));
3552 }
3553
David L. Jonesf561aba2017-03-08 01:02:16 +00003554 llvm::Reloc::Model RelocationModel;
3555 unsigned PICLevel;
3556 bool IsPIE;
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003557 std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(TC, Args);
David L. Jonesf561aba2017-03-08 01:02:16 +00003558
3559 const char *RMName = RelocationModelName(RelocationModel);
3560
3561 if ((RelocationModel == llvm::Reloc::ROPI ||
3562 RelocationModel == llvm::Reloc::ROPI_RWPI) &&
3563 types::isCXX(Input.getType()) &&
3564 !Args.hasArg(options::OPT_fallow_unsupported))
3565 D.Diag(diag::err_drv_ropi_incompatible_with_cxx);
3566
3567 if (RMName) {
3568 CmdArgs.push_back("-mrelocation-model");
3569 CmdArgs.push_back(RMName);
3570 }
3571 if (PICLevel > 0) {
3572 CmdArgs.push_back("-pic-level");
3573 CmdArgs.push_back(PICLevel == 1 ? "1" : "2");
3574 if (IsPIE)
3575 CmdArgs.push_back("-pic-is-pie");
3576 }
3577
3578 if (Arg *A = Args.getLastArg(options::OPT_meabi)) {
3579 CmdArgs.push_back("-meabi");
3580 CmdArgs.push_back(A->getValue());
3581 }
3582
3583 CmdArgs.push_back("-mthread-model");
Jonathan Roelofs6fbb9e02017-09-07 22:01:25 +00003584 if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) {
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003585 if (!TC.isThreadModelSupported(A->getValue()))
Jonathan Roelofs6fbb9e02017-09-07 22:01:25 +00003586 D.Diag(diag::err_drv_invalid_thread_model_for_target)
3587 << A->getValue() << A->getAsString(Args);
David L. Jonesf561aba2017-03-08 01:02:16 +00003588 CmdArgs.push_back(A->getValue());
Jonathan Roelofs6fbb9e02017-09-07 22:01:25 +00003589 }
David L. Jonesf561aba2017-03-08 01:02:16 +00003590 else
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003591 CmdArgs.push_back(Args.MakeArgString(TC.getThreadModel()));
David L. Jonesf561aba2017-03-08 01:02:16 +00003592
3593 Args.AddLastArg(CmdArgs, options::OPT_fveclib);
3594
Manoj Gupta4b3eefa2018-04-05 15:29:52 +00003595 if (Args.hasFlag(options::OPT_fmerge_all_constants,
3596 options::OPT_fno_merge_all_constants, false))
3597 CmdArgs.push_back("-fmerge-all-constants");
David L. Jonesf561aba2017-03-08 01:02:16 +00003598
Manoj Guptada08f6a2018-07-19 00:44:52 +00003599 if (Args.hasFlag(options::OPT_fno_delete_null_pointer_checks,
3600 options::OPT_fdelete_null_pointer_checks, false))
3601 CmdArgs.push_back("-fno-delete-null-pointer-checks");
3602
David L. Jonesf561aba2017-03-08 01:02:16 +00003603 // LLVM Code Generator Options.
3604
3605 if (Args.hasArg(options::OPT_frewrite_map_file) ||
3606 Args.hasArg(options::OPT_frewrite_map_file_EQ)) {
3607 for (const Arg *A : Args.filtered(options::OPT_frewrite_map_file,
3608 options::OPT_frewrite_map_file_EQ)) {
3609 StringRef Map = A->getValue();
3610 if (!llvm::sys::fs::exists(Map)) {
3611 D.Diag(diag::err_drv_no_such_file) << Map;
3612 } else {
3613 CmdArgs.push_back("-frewrite-map-file");
3614 CmdArgs.push_back(A->getValue());
3615 A->claim();
3616 }
3617 }
3618 }
3619
3620 if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) {
3621 StringRef v = A->getValue();
3622 CmdArgs.push_back("-mllvm");
3623 CmdArgs.push_back(Args.MakeArgString("-warn-stack-size=" + v));
3624 A->claim();
3625 }
3626
3627 if (!Args.hasFlag(options::OPT_fjump_tables, options::OPT_fno_jump_tables,
3628 true))
3629 CmdArgs.push_back("-fno-jump-tables");
3630
Dehao Chen5e97f232017-08-24 21:37:33 +00003631 if (Args.hasFlag(options::OPT_fprofile_sample_accurate,
3632 options::OPT_fno_profile_sample_accurate, false))
3633 CmdArgs.push_back("-fprofile-sample-accurate");
3634
David L. Jonesf561aba2017-03-08 01:02:16 +00003635 if (!Args.hasFlag(options::OPT_fpreserve_as_comments,
3636 options::OPT_fno_preserve_as_comments, true))
3637 CmdArgs.push_back("-fno-preserve-as-comments");
3638
3639 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
3640 CmdArgs.push_back("-mregparm");
3641 CmdArgs.push_back(A->getValue());
3642 }
3643
3644 if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return,
3645 options::OPT_freg_struct_return)) {
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003646 if (TC.getArch() != llvm::Triple::x86) {
David L. Jonesf561aba2017-03-08 01:02:16 +00003647 D.Diag(diag::err_drv_unsupported_opt_for_target)
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003648 << A->getSpelling() << RawTriple.str();
David L. Jonesf561aba2017-03-08 01:02:16 +00003649 } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) {
3650 CmdArgs.push_back("-fpcc-struct-return");
3651 } else {
3652 assert(A->getOption().matches(options::OPT_freg_struct_return));
3653 CmdArgs.push_back("-freg-struct-return");
3654 }
3655 }
3656
3657 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
3658 CmdArgs.push_back("-fdefault-calling-conv=stdcall");
3659
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003660 if (shouldUseFramePointer(Args, RawTriple))
David L. Jonesf561aba2017-03-08 01:02:16 +00003661 CmdArgs.push_back("-mdisable-fp-elim");
3662 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
3663 options::OPT_fno_zero_initialized_in_bss))
3664 CmdArgs.push_back("-mno-zero-initialized-in-bss");
3665
3666 bool OFastEnabled = isOptimizationLevelFast(Args);
3667 // If -Ofast is the optimization level, then -fstrict-aliasing should be
3668 // enabled. This alias option is being used to simplify the hasFlag logic.
3669 OptSpecifier StrictAliasingAliasOption =
3670 OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing;
3671 // We turn strict aliasing off by default if we're in CL mode, since MSVC
3672 // doesn't do any TBAA.
Saleem Abdulrasool33d41382017-08-29 23:59:06 +00003673 bool TBAAOnByDefault = !D.IsCLMode();
David L. Jonesf561aba2017-03-08 01:02:16 +00003674 if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
3675 options::OPT_fno_strict_aliasing, TBAAOnByDefault))
3676 CmdArgs.push_back("-relaxed-aliasing");
3677 if (!Args.hasFlag(options::OPT_fstruct_path_tbaa,
3678 options::OPT_fno_struct_path_tbaa))
3679 CmdArgs.push_back("-no-struct-path-tbaa");
3680 if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
3681 false))
3682 CmdArgs.push_back("-fstrict-enums");
3683 if (!Args.hasFlag(options::OPT_fstrict_return, options::OPT_fno_strict_return,
3684 true))
3685 CmdArgs.push_back("-fno-strict-return");
Alex Lorenz1be800c52017-04-19 08:58:56 +00003686 if (Args.hasFlag(options::OPT_fallow_editor_placeholders,
3687 options::OPT_fno_allow_editor_placeholders, false))
3688 CmdArgs.push_back("-fallow-editor-placeholders");
David L. Jonesf561aba2017-03-08 01:02:16 +00003689 if (Args.hasFlag(options::OPT_fstrict_vtable_pointers,
3690 options::OPT_fno_strict_vtable_pointers,
3691 false))
3692 CmdArgs.push_back("-fstrict-vtable-pointers");
Piotr Padlewskie368de32018-06-13 13:55:42 +00003693 if (Args.hasFlag(options::OPT_fforce_emit_vtables,
3694 options::OPT_fno_force_emit_vtables,
3695 false))
3696 CmdArgs.push_back("-fforce-emit-vtables");
David L. Jonesf561aba2017-03-08 01:02:16 +00003697 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
3698 options::OPT_fno_optimize_sibling_calls))
3699 CmdArgs.push_back("-mdisable-tail-calls");
Akira Hatanaka627586b2018-03-02 01:53:15 +00003700 if (Args.hasFlag(options::OPT_fno_escaping_block_tail_calls,
Akira Hatanaka9f9d7662018-03-10 05:55:21 +00003701 options::OPT_fescaping_block_tail_calls, false))
Akira Hatanaka627586b2018-03-02 01:53:15 +00003702 CmdArgs.push_back("-fno-escaping-block-tail-calls");
David L. Jonesf561aba2017-03-08 01:02:16 +00003703
Wei Mi9b3d6272017-10-16 16:50:27 +00003704 Args.AddLastArg(CmdArgs, options::OPT_ffine_grained_bitfield_accesses,
3705 options::OPT_fno_fine_grained_bitfield_accesses);
3706
David L. Jonesf561aba2017-03-08 01:02:16 +00003707 // Handle segmented stacks.
3708 if (Args.hasArg(options::OPT_fsplit_stack))
3709 CmdArgs.push_back("-split-stacks");
3710
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003711 RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00003712
3713 // Decide whether to use verbose asm. Verbose assembly is the default on
3714 // toolchains which have the integrated assembler on by default.
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003715 bool IsIntegratedAssemblerDefault = TC.IsIntegratedAssemblerDefault();
David L. Jonesf561aba2017-03-08 01:02:16 +00003716 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
3717 IsIntegratedAssemblerDefault) ||
3718 Args.hasArg(options::OPT_dA))
3719 CmdArgs.push_back("-masm-verbose");
3720
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003721 if (!TC.useIntegratedAs())
David L. Jonesf561aba2017-03-08 01:02:16 +00003722 CmdArgs.push_back("-no-integrated-as");
3723
3724 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
3725 CmdArgs.push_back("-mdebug-pass");
3726 CmdArgs.push_back("Structure");
3727 }
3728 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
3729 CmdArgs.push_back("-mdebug-pass");
3730 CmdArgs.push_back("Arguments");
3731 }
3732
3733 // Enable -mconstructor-aliases except on darwin, where we have to work around
3734 // a linker bug (see <rdar://problem/7651567>), and CUDA device code, where
3735 // aliases aren't supported.
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003736 if (!RawTriple.isOSDarwin() && !RawTriple.isNVPTX())
David L. Jonesf561aba2017-03-08 01:02:16 +00003737 CmdArgs.push_back("-mconstructor-aliases");
3738
3739 // Darwin's kernel doesn't support guard variables; just die if we
3740 // try to use them.
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003741 if (KernelOrKext && RawTriple.isOSDarwin())
David L. Jonesf561aba2017-03-08 01:02:16 +00003742 CmdArgs.push_back("-fforbid-guard-variables");
3743
3744 if (Args.hasFlag(options::OPT_mms_bitfields, options::OPT_mno_ms_bitfields,
3745 false)) {
3746 CmdArgs.push_back("-mms-bitfields");
3747 }
3748
3749 if (Args.hasFlag(options::OPT_mpie_copy_relocations,
3750 options::OPT_mno_pie_copy_relocations,
3751 false)) {
3752 CmdArgs.push_back("-mpie-copy-relocations");
3753 }
3754
Sriraman Tallam5c651482017-11-07 19:37:51 +00003755 if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) {
3756 CmdArgs.push_back("-fno-plt");
3757 }
3758
Vedant Kumardf502592017-09-12 22:51:53 +00003759 // -fhosted is default.
3760 // TODO: Audit uses of KernelOrKext and see where it'd be more appropriate to
3761 // use Freestanding.
3762 bool Freestanding =
3763 Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
3764 KernelOrKext;
3765 if (Freestanding)
3766 CmdArgs.push_back("-ffreestanding");
3767
David L. Jonesf561aba2017-03-08 01:02:16 +00003768 // This is a coarse approximation of what llvm-gcc actually does, both
3769 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
3770 // complicated ways.
3771 bool AsynchronousUnwindTables =
3772 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
3773 options::OPT_fno_asynchronous_unwind_tables,
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003774 (TC.IsUnwindTablesDefault(Args) ||
3775 TC.getSanitizerArgs().needsUnwindTables()) &&
Vedant Kumardf502592017-09-12 22:51:53 +00003776 !Freestanding);
David L. Jonesf561aba2017-03-08 01:02:16 +00003777 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
3778 AsynchronousUnwindTables))
3779 CmdArgs.push_back("-munwind-tables");
3780
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003781 TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind());
David L. Jonesf561aba2017-03-08 01:02:16 +00003782
David L. Jonesf561aba2017-03-08 01:02:16 +00003783 // FIXME: Handle -mtune=.
3784 (void)Args.hasArg(options::OPT_mtune_EQ);
3785
3786 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
3787 CmdArgs.push_back("-mcode-model");
3788 CmdArgs.push_back(A->getValue());
3789 }
3790
3791 // Add the target cpu
3792 std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false);
3793 if (!CPU.empty()) {
3794 CmdArgs.push_back("-target-cpu");
3795 CmdArgs.push_back(Args.MakeArgString(CPU));
3796 }
3797
Saleem Abdulrasool6c3ed7b2017-09-03 04:47:00 +00003798 RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00003799
David L. Jonesf561aba2017-03-08 01:02:16 +00003800 // These two are potentially updated by AddClangCLArgs.
3801 codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo;
3802 bool EmitCodeView = false;
3803
3804 // Add clang-cl arguments.
3805 types::ID InputType = Input.getType();
Saleem Abdulrasool33d41382017-08-29 23:59:06 +00003806 if (D.IsCLMode())
David L. Jonesf561aba2017-03-08 01:02:16 +00003807 AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView);
Reid Kleckner54af3e72018-02-26 22:55:33 +00003808 else
3809 EmitCodeView = Args.hasArg(options::OPT_gcodeview);
David L. Jonesf561aba2017-03-08 01:02:16 +00003810
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003811 const Arg *SplitDWARFArg = nullptr;
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003812 RenderDebugOptions(TC, D, RawTriple, Args, EmitCodeView, IsWindowsMSVC,
3813 CmdArgs, DebugInfoKind, SplitDWARFArg);
Saleem Abdulrasool9934eab2017-09-03 04:46:59 +00003814
3815 // Add the split debug info name to the command lines here so we
3816 // can propagate it to the backend.
3817 bool SplitDWARF = SplitDWARFArg && RawTriple.isOSLinux() &&
3818 (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) ||
3819 isa<BackendJobAction>(JA));
3820 const char *SplitDWARFOut;
3821 if (SplitDWARF) {
3822 CmdArgs.push_back("-split-dwarf-file");
3823 SplitDWARFOut = SplitDebugName(Args, Input);
3824 CmdArgs.push_back(SplitDWARFOut);
3825 }
3826
David L. Jonesf561aba2017-03-08 01:02:16 +00003827 // Pass the linker version in use.
3828 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
3829 CmdArgs.push_back("-target-linker-version");
3830 CmdArgs.push_back(A->getValue());
3831 }
3832
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003833 if (!shouldUseLeafFramePointer(Args, RawTriple))
David L. Jonesf561aba2017-03-08 01:02:16 +00003834 CmdArgs.push_back("-momit-leaf-frame-pointer");
3835
3836 // Explicitly error on some things we know we don't support and can't just
3837 // ignore.
3838 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
3839 Arg *Unsupported;
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00003840 if (types::isCXX(InputType) && RawTriple.isOSDarwin() &&
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003841 TC.getArch() == llvm::Triple::x86) {
David L. Jonesf561aba2017-03-08 01:02:16 +00003842 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
3843 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
3844 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
3845 << Unsupported->getOption().getName();
3846 }
Eric Christopher758aad72017-03-21 22:06:18 +00003847 // The faltivec option has been superseded by the maltivec option.
3848 if ((Unsupported = Args.getLastArg(options::OPT_faltivec)))
3849 D.Diag(diag::err_drv_clang_unsupported_opt_faltivec)
3850 << Unsupported->getOption().getName()
3851 << "please use -maltivec and include altivec.h explicitly";
3852 if ((Unsupported = Args.getLastArg(options::OPT_fno_altivec)))
3853 D.Diag(diag::err_drv_clang_unsupported_opt_faltivec)
3854 << Unsupported->getOption().getName() << "please use -mno-altivec";
David L. Jonesf561aba2017-03-08 01:02:16 +00003855 }
3856
3857 Args.AddAllArgs(CmdArgs, options::OPT_v);
3858 Args.AddLastArg(CmdArgs, options::OPT_H);
3859 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
3860 CmdArgs.push_back("-header-include-file");
3861 CmdArgs.push_back(D.CCPrintHeadersFilename ? D.CCPrintHeadersFilename
3862 : "-");
3863 }
3864 Args.AddLastArg(CmdArgs, options::OPT_P);
3865 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
3866
3867 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
3868 CmdArgs.push_back("-diagnostic-log-file");
3869 CmdArgs.push_back(D.CCLogDiagnosticsFilename ? D.CCLogDiagnosticsFilename
3870 : "-");
3871 }
3872
David L. Jonesf561aba2017-03-08 01:02:16 +00003873 bool UseSeparateSections = isUseSeparateSections(Triple);
3874
3875 if (Args.hasFlag(options::OPT_ffunction_sections,
3876 options::OPT_fno_function_sections, UseSeparateSections)) {
3877 CmdArgs.push_back("-ffunction-sections");
3878 }
3879
3880 if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections,
3881 UseSeparateSections)) {
3882 CmdArgs.push_back("-fdata-sections");
3883 }
3884
3885 if (!Args.hasFlag(options::OPT_funique_section_names,
3886 options::OPT_fno_unique_section_names, true))
3887 CmdArgs.push_back("-fno-unique-section-names");
3888
Hans Wennborg14e8a5a2017-11-21 17:30:34 +00003889 if (auto *A = Args.getLastArg(
3890 options::OPT_finstrument_functions,
3891 options::OPT_finstrument_functions_after_inlining,
3892 options::OPT_finstrument_function_entry_bare))
3893 A->render(Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00003894
Artem Belevichc30bcad2018-01-24 17:41:02 +00003895 // NVPTX doesn't support PGO or coverage. There's no runtime support for
3896 // sampling, overhead of call arc collection is way too high and there's no
3897 // way to collect the output.
3898 if (!Triple.isNVPTX())
3899 addPGOAndCoverageFlags(C, D, Output, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00003900
Richard Smithf667ad52017-08-26 01:04:35 +00003901 if (auto *ABICompatArg = Args.getLastArg(options::OPT_fclang_abi_compat_EQ))
3902 ABICompatArg->render(Args, CmdArgs);
3903
Pierre Gousseau1abf9432018-06-06 14:04:15 +00003904 // Add runtime flag for PS4 when PGO, coverage, or sanitizers are enabled.
3905 if (RawTriple.isPS4CPU()) {
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00003906 PS4cpu::addProfileRTArgs(TC, Args, CmdArgs);
3907 PS4cpu::addSanitizerArgs(TC, CmdArgs);
Pierre Gousseau1abf9432018-06-06 14:04:15 +00003908 }
David L. Jonesf561aba2017-03-08 01:02:16 +00003909
3910 // Pass options for controlling the default header search paths.
3911 if (Args.hasArg(options::OPT_nostdinc)) {
3912 CmdArgs.push_back("-nostdsysteminc");
3913 CmdArgs.push_back("-nobuiltininc");
3914 } else {
3915 if (Args.hasArg(options::OPT_nostdlibinc))
3916 CmdArgs.push_back("-nostdsysteminc");
3917 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
3918 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
3919 }
3920
3921 // Pass the path to compiler resource files.
3922 CmdArgs.push_back("-resource-dir");
3923 CmdArgs.push_back(D.ResourceDir.c_str());
3924
3925 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
3926
Saleem Abdulrasool0a322c62017-08-31 15:35:01 +00003927 RenderARCMigrateToolOptions(D, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00003928
3929 // Add preprocessing options like -I, -D, etc. if we are using the
3930 // preprocessor.
3931 //
3932 // FIXME: Support -fpreprocessed
3933 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
3934 AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
3935
3936 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
3937 // that "The compiler can only warn and ignore the option if not recognized".
3938 // When building with ccache, it will pass -D options to clang even on
3939 // preprocessed inputs and configure concludes that -fPIC is not supported.
3940 Args.ClaimAllArgs(options::OPT_D);
3941
3942 // Manually translate -O4 to -O3; let clang reject others.
3943 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
3944 if (A->getOption().matches(options::OPT_O4)) {
3945 CmdArgs.push_back("-O3");
3946 D.Diag(diag::warn_O4_is_O3);
3947 } else {
3948 A->render(Args, CmdArgs);
3949 }
3950 }
3951
3952 // Warn about ignored options to clang.
3953 for (const Arg *A :
3954 Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) {
3955 D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args);
3956 A->claim();
3957 }
3958
Joerg Sonnenbergerc9199682017-07-01 21:36:21 +00003959 for (const Arg *A :
3960 Args.filtered(options::OPT_clang_ignored_legacy_options_Group)) {
3961 D.Diag(diag::warn_ignored_clang_option) << A->getAsString(Args);
3962 A->claim();
3963 }
3964
David L. Jonesf561aba2017-03-08 01:02:16 +00003965 claimNoWarnArgs(Args);
3966
3967 Args.AddAllArgs(CmdArgs, options::OPT_R_Group);
3968
3969 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
3970 if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
3971 CmdArgs.push_back("-pedantic");
3972 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
3973 Args.AddLastArg(CmdArgs, options::OPT_w);
3974
Leonard Chanf921d852018-06-04 16:07:52 +00003975 // Fixed point flags
3976 if (Args.hasFlag(options::OPT_ffixed_point, options::OPT_fno_fixed_point,
3977 /*Default=*/false))
3978 Args.AddLastArg(CmdArgs, options::OPT_ffixed_point);
3979
David L. Jonesf561aba2017-03-08 01:02:16 +00003980 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
3981 // (-ansi is equivalent to -std=c89 or -std=c++98).
3982 //
3983 // If a std is supplied, only add -trigraphs if it follows the
3984 // option.
3985 bool ImplyVCPPCXXVer = false;
3986 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
3987 if (Std->getOption().matches(options::OPT_ansi))
3988 if (types::isCXX(InputType))
3989 CmdArgs.push_back("-std=c++98");
3990 else
3991 CmdArgs.push_back("-std=c89");
3992 else
3993 Std->render(Args, CmdArgs);
3994
3995 // If -f(no-)trigraphs appears after the language standard flag, honor it.
3996 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
3997 options::OPT_ftrigraphs,
3998 options::OPT_fno_trigraphs))
3999 if (A != Std)
4000 A->render(Args, CmdArgs);
4001 } else {
4002 // Honor -std-default.
4003 //
4004 // FIXME: Clang doesn't correctly handle -std= when the input language
4005 // doesn't match. For the time being just ignore this for C++ inputs;
4006 // eventually we want to do all the standard defaulting here instead of
4007 // splitting it between the driver and clang -cc1.
4008 if (!types::isCXX(InputType))
4009 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=",
4010 /*Joined=*/true);
4011 else if (IsWindowsMSVC)
4012 ImplyVCPPCXXVer = true;
4013
4014 Args.AddLastArg(CmdArgs, options::OPT_ftrigraphs,
4015 options::OPT_fno_trigraphs);
4016 }
4017
4018 // GCC's behavior for -Wwrite-strings is a bit strange:
4019 // * In C, this "warning flag" changes the types of string literals from
4020 // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning
4021 // for the discarded qualifier.
4022 // * In C++, this is just a normal warning flag.
4023 //
4024 // Implementing this warning correctly in C is hard, so we follow GCC's
4025 // behavior for now. FIXME: Directly diagnose uses of a string literal as
4026 // a non-const char* in C, rather than using this crude hack.
4027 if (!types::isCXX(InputType)) {
4028 // FIXME: This should behave just like a warning flag, and thus should also
4029 // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on.
4030 Arg *WriteStrings =
4031 Args.getLastArg(options::OPT_Wwrite_strings,
4032 options::OPT_Wno_write_strings, options::OPT_w);
4033 if (WriteStrings &&
4034 WriteStrings->getOption().matches(options::OPT_Wwrite_strings))
4035 CmdArgs.push_back("-fconst-strings");
4036 }
4037
4038 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
4039 // during C++ compilation, which it is by default. GCC keeps this define even
4040 // in the presence of '-w', match this behavior bug-for-bug.
4041 if (types::isCXX(InputType) &&
4042 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
4043 true)) {
4044 CmdArgs.push_back("-fdeprecated-macro");
4045 }
4046
4047 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
4048 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
4049 if (Asm->getOption().matches(options::OPT_fasm))
4050 CmdArgs.push_back("-fgnu-keywords");
4051 else
4052 CmdArgs.push_back("-fno-gnu-keywords");
4053 }
4054
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004055 if (ShouldDisableDwarfDirectory(Args, TC))
David L. Jonesf561aba2017-03-08 01:02:16 +00004056 CmdArgs.push_back("-fno-dwarf-directory-asm");
4057
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004058 if (ShouldDisableAutolink(Args, TC))
David L. Jonesf561aba2017-03-08 01:02:16 +00004059 CmdArgs.push_back("-fno-autolink");
4060
4061 // Add in -fdebug-compilation-dir if necessary.
4062 addDebugCompDirArg(Args, CmdArgs);
4063
Paul Robinson9b292b42018-07-10 15:15:24 +00004064 addDebugPrefixMapArg(D, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004065
4066 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
4067 options::OPT_ftemplate_depth_EQ)) {
4068 CmdArgs.push_back("-ftemplate-depth");
4069 CmdArgs.push_back(A->getValue());
4070 }
4071
4072 if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) {
4073 CmdArgs.push_back("-foperator-arrow-depth");
4074 CmdArgs.push_back(A->getValue());
4075 }
4076
4077 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
4078 CmdArgs.push_back("-fconstexpr-depth");
4079 CmdArgs.push_back(A->getValue());
4080 }
4081
4082 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) {
4083 CmdArgs.push_back("-fconstexpr-steps");
4084 CmdArgs.push_back(A->getValue());
4085 }
4086
4087 if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
4088 CmdArgs.push_back("-fbracket-depth");
4089 CmdArgs.push_back(A->getValue());
4090 }
4091
4092 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
4093 options::OPT_Wlarge_by_value_copy_def)) {
4094 if (A->getNumValues()) {
4095 StringRef bytes = A->getValue();
4096 CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
4097 } else
4098 CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
4099 }
4100
4101 if (Args.hasArg(options::OPT_relocatable_pch))
4102 CmdArgs.push_back("-relocatable-pch");
4103
4104 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
4105 CmdArgs.push_back("-fconstant-string-class");
4106 CmdArgs.push_back(A->getValue());
4107 }
4108
4109 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
4110 CmdArgs.push_back("-ftabstop");
4111 CmdArgs.push_back(A->getValue());
4112 }
4113
Sean Eveson5110d4f2018-01-08 13:42:26 +00004114 if (Args.hasFlag(options::OPT_fstack_size_section,
4115 options::OPT_fno_stack_size_section, RawTriple.isPS4()))
4116 CmdArgs.push_back("-fstack-size-section");
4117
David L. Jonesf561aba2017-03-08 01:02:16 +00004118 CmdArgs.push_back("-ferror-limit");
4119 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
4120 CmdArgs.push_back(A->getValue());
4121 else
4122 CmdArgs.push_back("19");
4123
4124 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
4125 CmdArgs.push_back("-fmacro-backtrace-limit");
4126 CmdArgs.push_back(A->getValue());
4127 }
4128
4129 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
4130 CmdArgs.push_back("-ftemplate-backtrace-limit");
4131 CmdArgs.push_back(A->getValue());
4132 }
4133
4134 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
4135 CmdArgs.push_back("-fconstexpr-backtrace-limit");
4136 CmdArgs.push_back(A->getValue());
4137 }
4138
4139 if (Arg *A = Args.getLastArg(options::OPT_fspell_checking_limit_EQ)) {
4140 CmdArgs.push_back("-fspell-checking-limit");
4141 CmdArgs.push_back(A->getValue());
4142 }
4143
4144 // Pass -fmessage-length=.
4145 CmdArgs.push_back("-fmessage-length");
4146 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
4147 CmdArgs.push_back(A->getValue());
4148 } else {
4149 // If -fmessage-length=N was not specified, determine whether this is a
4150 // terminal and, if so, implicitly define -fmessage-length appropriately.
4151 unsigned N = llvm::sys::Process::StandardErrColumns();
4152 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
4153 }
4154
4155 // -fvisibility= and -fvisibility-ms-compat are of a piece.
4156 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
4157 options::OPT_fvisibility_ms_compat)) {
4158 if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
4159 CmdArgs.push_back("-fvisibility");
4160 CmdArgs.push_back(A->getValue());
4161 } else {
4162 assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
4163 CmdArgs.push_back("-fvisibility");
4164 CmdArgs.push_back("hidden");
4165 CmdArgs.push_back("-ftype-visibility");
4166 CmdArgs.push_back("default");
4167 }
4168 }
4169
4170 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
4171
4172 Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
4173
David L. Jonesf561aba2017-03-08 01:02:16 +00004174 // Forward -f (flag) options which we can pass directly.
4175 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
4176 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Jacob Bandes-Storch33f3e632018-07-17 04:56:22 +00004177 Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004178 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
Chih-Hung Hsiehca552b82018-03-01 22:26:19 +00004179 Args.AddLastArg(CmdArgs, options::OPT_femulated_tls,
4180 options::OPT_fno_emulated_tls);
Elizabeth Andrews6593df22018-08-22 19:05:19 +00004181 Args.AddLastArg(CmdArgs, options::OPT_fkeep_static_consts);
Chih-Hung Hsiehca552b82018-03-01 22:26:19 +00004182
David L. Jonesf561aba2017-03-08 01:02:16 +00004183 // AltiVec-like language extensions aren't relevant for assembling.
Eric Christopher758aad72017-03-21 22:06:18 +00004184 if (!isa<PreprocessJobAction>(JA) || Output.getType() != types::TY_PP_Asm)
David L. Jonesf561aba2017-03-08 01:02:16 +00004185 Args.AddLastArg(CmdArgs, options::OPT_fzvector);
Eric Christopher758aad72017-03-21 22:06:18 +00004186
David L. Jonesf561aba2017-03-08 01:02:16 +00004187 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
4188 Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
4189
4190 // Forward flags for OpenMP. We don't do this if the current action is an
4191 // device offloading action other than OpenMP.
4192 if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
4193 options::OPT_fno_openmp, false) &&
4194 (JA.isDeviceOffloading(Action::OFK_None) ||
4195 JA.isDeviceOffloading(Action::OFK_OpenMP))) {
Saleem Abdulrasool33d41382017-08-29 23:59:06 +00004196 switch (D.getOpenMPRuntime(Args)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00004197 case Driver::OMPRT_OMP:
4198 case Driver::OMPRT_IOMP5:
4199 // Clang can generate useful OpenMP code for these two runtime libraries.
4200 CmdArgs.push_back("-fopenmp");
4201
4202 // If no option regarding the use of TLS in OpenMP codegeneration is
4203 // given, decide a default based on the target. Otherwise rely on the
4204 // options and pass the right information to the frontend.
4205 if (!Args.hasFlag(options::OPT_fopenmp_use_tls,
4206 options::OPT_fnoopenmp_use_tls, /*Default=*/true))
4207 CmdArgs.push_back("-fnoopenmp-use-tls");
Alexey Bataev66f95772018-05-21 16:40:32 +00004208 Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd,
4209 options::OPT_fno_openmp_simd);
David L. Jonesf561aba2017-03-08 01:02:16 +00004210 Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ);
Carlo Bertolli79712092018-02-28 20:48:35 +00004211
4212 // When in OpenMP offloading mode with NVPTX target, forward
4213 // cuda-mode flag
Alexey Bataev80a9a612018-08-30 14:45:24 +00004214 if (Args.hasFlag(options::OPT_fopenmp_cuda_mode,
4215 options::OPT_fno_openmp_cuda_mode, /*Default=*/false))
4216 CmdArgs.push_back("-fopenmp-cuda-mode");
4217
4218 // When in OpenMP offloading mode with NVPTX target, check if full runtime
4219 // is required.
4220 if (Args.hasFlag(options::OPT_fopenmp_cuda_force_full_runtime,
4221 options::OPT_fno_openmp_cuda_force_full_runtime,
4222 /*Default=*/false))
4223 CmdArgs.push_back("-fopenmp-cuda-force-full-runtime");
David L. Jonesf561aba2017-03-08 01:02:16 +00004224 break;
4225 default:
4226 // By default, if Clang doesn't know how to generate useful OpenMP code
4227 // for a specific runtime library, we just don't pass the '-fopenmp' flag
4228 // down to the actual compilation.
4229 // FIXME: It would be better to have a mode which *only* omits IR
4230 // generation based on the OpenMP support so that we get consistent
4231 // semantic analysis, etc.
4232 break;
4233 }
Alexey Bataeve927ca72017-12-29 17:36:15 +00004234 } else {
4235 Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd,
4236 options::OPT_fno_openmp_simd);
4237 Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ);
David L. Jonesf561aba2017-03-08 01:02:16 +00004238 }
4239
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004240 const SanitizerArgs &Sanitize = TC.getSanitizerArgs();
4241 Sanitize.addArgs(TC, Args, CmdArgs, InputType);
David L. Jonesf561aba2017-03-08 01:02:16 +00004242
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004243 const XRayArgs &XRay = TC.getXRayArgs();
4244 XRay.addArgs(TC, Args, CmdArgs, InputType);
Dean Michael Berris835832d2017-03-30 00:29:36 +00004245
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004246 if (TC.SupportsProfiling())
David L. Jonesf561aba2017-03-08 01:02:16 +00004247 Args.AddLastArg(CmdArgs, options::OPT_pg);
4248
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004249 if (TC.SupportsProfiling())
David L. Jonesf561aba2017-03-08 01:02:16 +00004250 Args.AddLastArg(CmdArgs, options::OPT_mfentry);
4251
4252 // -flax-vector-conversions is default.
4253 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
4254 options::OPT_fno_lax_vector_conversions))
4255 CmdArgs.push_back("-fno-lax-vector-conversions");
4256
4257 if (Args.getLastArg(options::OPT_fapple_kext) ||
4258 (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType)))
4259 CmdArgs.push_back("-fapple-kext");
4260
4261 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
4262 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
4263 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
4264 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
4265 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
4266
4267 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
4268 CmdArgs.push_back("-ftrapv-handler");
4269 CmdArgs.push_back(A->getValue());
4270 }
4271
4272 Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
4273
4274 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
4275 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
4276 if (Arg *A = Args.getLastArg(options::OPT_fwrapv, options::OPT_fno_wrapv)) {
4277 if (A->getOption().matches(options::OPT_fwrapv))
4278 CmdArgs.push_back("-fwrapv");
4279 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
4280 options::OPT_fno_strict_overflow)) {
4281 if (A->getOption().matches(options::OPT_fno_strict_overflow))
4282 CmdArgs.push_back("-fwrapv");
4283 }
4284
4285 if (Arg *A = Args.getLastArg(options::OPT_freroll_loops,
4286 options::OPT_fno_reroll_loops))
4287 if (A->getOption().matches(options::OPT_freroll_loops))
4288 CmdArgs.push_back("-freroll-loops");
4289
4290 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
4291 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops,
4292 options::OPT_fno_unroll_loops);
4293
4294 Args.AddLastArg(CmdArgs, options::OPT_pthread);
4295
Chandler Carruth664aa862018-09-04 12:38:00 +00004296 Args.AddLastArg(CmdArgs, options::OPT_mspeculative_load_hardening,
4297 options::OPT_mno_speculative_load_hardening);
4298
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004299 RenderSSPOptions(TC, Args, CmdArgs, KernelOrKext);
David L. Jonesf561aba2017-03-08 01:02:16 +00004300
4301 // Translate -mstackrealign
4302 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
4303 false))
4304 CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
4305
4306 if (Args.hasArg(options::OPT_mstack_alignment)) {
4307 StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
4308 CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
4309 }
4310
4311 if (Args.hasArg(options::OPT_mstack_probe_size)) {
4312 StringRef Size = Args.getLastArgValue(options::OPT_mstack_probe_size);
4313
4314 if (!Size.empty())
4315 CmdArgs.push_back(Args.MakeArgString("-mstack-probe-size=" + Size));
4316 else
4317 CmdArgs.push_back("-mstack-probe-size=0");
4318 }
4319
Hans Wennborgd43f40d2018-02-23 13:47:36 +00004320 if (!Args.hasFlag(options::OPT_mstack_arg_probe,
4321 options::OPT_mno_stack_arg_probe, true))
4322 CmdArgs.push_back(Args.MakeArgString("-mno-stack-arg-probe"));
4323
David L. Jonesf561aba2017-03-08 01:02:16 +00004324 if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it,
4325 options::OPT_mno_restrict_it)) {
4326 if (A->getOption().matches(options::OPT_mrestrict_it)) {
Eli Friedman01d349b2018-04-12 22:21:36 +00004327 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00004328 CmdArgs.push_back("-arm-restrict-it");
4329 } else {
Eli Friedman01d349b2018-04-12 22:21:36 +00004330 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00004331 CmdArgs.push_back("-arm-no-restrict-it");
4332 }
4333 } else if (Triple.isOSWindows() &&
4334 (Triple.getArch() == llvm::Triple::arm ||
4335 Triple.getArch() == llvm::Triple::thumb)) {
4336 // Windows on ARM expects restricted IT blocks
Eli Friedman01d349b2018-04-12 22:21:36 +00004337 CmdArgs.push_back("-mllvm");
David L. Jonesf561aba2017-03-08 01:02:16 +00004338 CmdArgs.push_back("-arm-restrict-it");
4339 }
4340
4341 // Forward -cl options to -cc1
Saleem Abdulrasool68c808f62017-08-29 23:59:07 +00004342 RenderOpenCLOptions(Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004343
Oren Ben Simhon57cc1a52018-01-09 08:53:59 +00004344 if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) {
4345 CmdArgs.push_back(
4346 Args.MakeArgString(Twine("-fcf-protection=") + A->getValue()));
4347 }
4348
David L. Jonesf561aba2017-03-08 01:02:16 +00004349 // Forward -f options with positive and negative forms; we translate
4350 // these by hand.
Dehao Chenea4b78f2017-03-21 21:40:53 +00004351 if (Arg *A = getLastProfileSampleUseArg(Args)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00004352 StringRef fname = A->getValue();
4353 if (!llvm::sys::fs::exists(fname))
4354 D.Diag(diag::err_drv_no_such_file) << fname;
4355 else
4356 A->render(Args, CmdArgs);
4357 }
Richard Smith8654ae52018-10-10 23:13:35 +00004358 Args.AddLastArg(CmdArgs, options::OPT_fprofile_remapping_file_EQ);
David L. Jonesf561aba2017-03-08 01:02:16 +00004359
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004360 RenderBuiltinOptions(TC, RawTriple, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004361
4362 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
4363 options::OPT_fno_assume_sane_operator_new))
4364 CmdArgs.push_back("-fno-assume-sane-operator-new");
4365
4366 // -fblocks=0 is default.
4367 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004368 TC.IsBlocksDefault()) ||
David L. Jonesf561aba2017-03-08 01:02:16 +00004369 (Args.hasArg(options::OPT_fgnu_runtime) &&
4370 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
4371 !Args.hasArg(options::OPT_fno_blocks))) {
4372 CmdArgs.push_back("-fblocks");
4373
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004374 if (!Args.hasArg(options::OPT_fgnu_runtime) && !TC.hasBlocksRuntime())
David L. Jonesf561aba2017-03-08 01:02:16 +00004375 CmdArgs.push_back("-fblocks-runtime-optional");
4376 }
4377
Saleem Abdulrasoolb2d4ebb2017-09-01 17:43:59 +00004378 // -fencode-extended-block-signature=1 is default.
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004379 if (TC.IsEncodeExtendedBlockSignatureDefault())
Saleem Abdulrasoolb2d4ebb2017-09-01 17:43:59 +00004380 CmdArgs.push_back("-fencode-extended-block-signature");
4381
David L. Jonesf561aba2017-03-08 01:02:16 +00004382 if (Args.hasFlag(options::OPT_fcoroutines_ts, options::OPT_fno_coroutines_ts,
4383 false) &&
4384 types::isCXX(InputType)) {
4385 CmdArgs.push_back("-fcoroutines-ts");
4386 }
4387
Aaron Ballman61736552017-10-21 20:28:58 +00004388 Args.AddLastArg(CmdArgs, options::OPT_fdouble_square_bracket_attributes,
4389 options::OPT_fno_double_square_bracket_attributes);
4390
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00004391 bool HaveModules = false;
4392 RenderModulesOptions(C, D, Args, Input, Output, CmdArgs, HaveModules);
David L. Jonesf561aba2017-03-08 01:02:16 +00004393
4394 // -faccess-control is default.
4395 if (Args.hasFlag(options::OPT_fno_access_control,
4396 options::OPT_faccess_control, false))
4397 CmdArgs.push_back("-fno-access-control");
4398
4399 // -felide-constructors is the default.
4400 if (Args.hasFlag(options::OPT_fno_elide_constructors,
4401 options::OPT_felide_constructors, false))
4402 CmdArgs.push_back("-fno-elide-constructors");
4403
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004404 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
David L. Jonesf561aba2017-03-08 01:02:16 +00004405
4406 if (KernelOrKext || (types::isCXX(InputType) &&
Sunil Srivastava2ada2492018-05-18 23:32:01 +00004407 (RTTIMode == ToolChain::RM_Disabled)))
David L. Jonesf561aba2017-03-08 01:02:16 +00004408 CmdArgs.push_back("-fno-rtti");
4409
4410 // -fshort-enums=0 is default for all architectures except Hexagon.
4411 if (Args.hasFlag(options::OPT_fshort_enums, options::OPT_fno_short_enums,
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004412 TC.getArch() == llvm::Triple::hexagon))
David L. Jonesf561aba2017-03-08 01:02:16 +00004413 CmdArgs.push_back("-fshort-enums");
4414
Saleem Abdulrasool729379a2017-10-06 23:09:55 +00004415 RenderCharacterOptions(Args, AuxTriple ? *AuxTriple : RawTriple, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004416
4417 // -fuse-cxa-atexit is default.
4418 if (!Args.hasFlag(
4419 options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
Saleem Abdulrasool015bded2017-09-11 20:18:09 +00004420 !RawTriple.isOSWindows() &&
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00004421 RawTriple.getOS() != llvm::Triple::Solaris &&
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004422 TC.getArch() != llvm::Triple::xcore &&
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00004423 ((RawTriple.getVendor() != llvm::Triple::MipsTechnologies) ||
4424 RawTriple.hasEnvironment())) ||
David L. Jonesf561aba2017-03-08 01:02:16 +00004425 KernelOrKext)
4426 CmdArgs.push_back("-fno-use-cxa-atexit");
4427
Akira Hatanaka617e2612018-04-17 18:41:52 +00004428 if (Args.hasFlag(options::OPT_fregister_global_dtors_with_atexit,
4429 options::OPT_fno_register_global_dtors_with_atexit,
Akira Hatanaka18db58e2018-04-27 01:42:33 +00004430 RawTriple.isOSDarwin() && !KernelOrKext))
Akira Hatanaka617e2612018-04-17 18:41:52 +00004431 CmdArgs.push_back("-fregister-global-dtors-with-atexit");
4432
David L. Jonesf561aba2017-03-08 01:02:16 +00004433 // -fms-extensions=0 is default.
4434 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
4435 IsWindowsMSVC))
4436 CmdArgs.push_back("-fms-extensions");
4437
Martin Storsjof1f8f4a2018-05-09 09:11:01 +00004438 // -fno-use-line-directives is default.
David L. Jonesf561aba2017-03-08 01:02:16 +00004439 if (Args.hasFlag(options::OPT_fuse_line_directives,
Martin Storsjof1f8f4a2018-05-09 09:11:01 +00004440 options::OPT_fno_use_line_directives, false))
David L. Jonesf561aba2017-03-08 01:02:16 +00004441 CmdArgs.push_back("-fuse-line-directives");
4442
4443 // -fms-compatibility=0 is default.
4444 if (Args.hasFlag(options::OPT_fms_compatibility,
4445 options::OPT_fno_ms_compatibility,
4446 (IsWindowsMSVC &&
4447 Args.hasFlag(options::OPT_fms_extensions,
4448 options::OPT_fno_ms_extensions, true))))
4449 CmdArgs.push_back("-fms-compatibility");
4450
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004451 VersionTuple MSVT = TC.computeMSVCVersion(&D, Args);
David L. Jonesf561aba2017-03-08 01:02:16 +00004452 if (!MSVT.empty())
4453 CmdArgs.push_back(
4454 Args.MakeArgString("-fms-compatibility-version=" + MSVT.getAsString()));
4455
4456 bool IsMSVC2015Compatible = MSVT.getMajor() >= 19;
4457 if (ImplyVCPPCXXVer) {
4458 StringRef LanguageStandard;
4459 if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) {
4460 LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue())
4461 .Case("c++14", "-std=c++14")
Martell Malonef6f6a9c2017-10-15 17:27:58 +00004462 .Case("c++17", "-std=c++17")
4463 .Case("c++latest", "-std=c++2a")
David L. Jonesf561aba2017-03-08 01:02:16 +00004464 .Default("");
4465 if (LanguageStandard.empty())
4466 D.Diag(clang::diag::warn_drv_unused_argument)
4467 << StdArg->getAsString(Args);
4468 }
4469
4470 if (LanguageStandard.empty()) {
4471 if (IsMSVC2015Compatible)
4472 LanguageStandard = "-std=c++14";
4473 else
4474 LanguageStandard = "-std=c++11";
4475 }
4476
4477 CmdArgs.push_back(LanguageStandard.data());
4478 }
4479
4480 // -fno-borland-extensions is default.
4481 if (Args.hasFlag(options::OPT_fborland_extensions,
4482 options::OPT_fno_borland_extensions, false))
4483 CmdArgs.push_back("-fborland-extensions");
4484
4485 // -fno-declspec is default, except for PS4.
4486 if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec,
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00004487 RawTriple.isPS4()))
David L. Jonesf561aba2017-03-08 01:02:16 +00004488 CmdArgs.push_back("-fdeclspec");
4489 else if (Args.hasArg(options::OPT_fno_declspec))
4490 CmdArgs.push_back("-fno-declspec"); // Explicitly disabling __declspec.
4491
4492 // -fthreadsafe-static is default, except for MSVC compatibility versions less
4493 // than 19.
4494 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
4495 options::OPT_fno_threadsafe_statics,
4496 !IsWindowsMSVC || IsMSVC2015Compatible))
4497 CmdArgs.push_back("-fno-threadsafe-statics");
4498
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00004499 // -fno-delayed-template-parsing is default, except when targeting MSVC.
Reid Klecknerea2683e2017-08-28 17:59:24 +00004500 // Many old Windows SDK versions require this to parse.
4501 // FIXME: MSVC introduced /Zc:twoPhase- to disable this behavior in their
4502 // compiler. We should be able to disable this by default at some point.
David L. Jonesf561aba2017-03-08 01:02:16 +00004503 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
4504 options::OPT_fno_delayed_template_parsing, IsWindowsMSVC))
4505 CmdArgs.push_back("-fdelayed-template-parsing");
4506
4507 // -fgnu-keywords default varies depending on language; only pass if
4508 // specified.
4509 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
4510 options::OPT_fno_gnu_keywords))
4511 A->render(Args, CmdArgs);
4512
4513 if (Args.hasFlag(options::OPT_fgnu89_inline, options::OPT_fno_gnu89_inline,
4514 false))
4515 CmdArgs.push_back("-fgnu89-inline");
4516
4517 if (Args.hasArg(options::OPT_fno_inline))
4518 CmdArgs.push_back("-fno-inline");
4519
4520 if (Arg* InlineArg = Args.getLastArg(options::OPT_finline_functions,
4521 options::OPT_finline_hint_functions,
4522 options::OPT_fno_inline_functions))
4523 InlineArg->render(Args, CmdArgs);
4524
4525 Args.AddLastArg(CmdArgs, options::OPT_fexperimental_new_pass_manager,
4526 options::OPT_fno_experimental_new_pass_manager);
4527
Saleem Abdulrasoolb2d4ebb2017-09-01 17:43:59 +00004528 ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004529 RenderObjCOptions(TC, D, RawTriple, Args, Runtime, rewriteKind != RK_None,
4530 Input, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004531
4532 if (Args.hasFlag(options::OPT_fapplication_extension,
4533 options::OPT_fno_application_extension, false))
4534 CmdArgs.push_back("-fapplication-extension");
4535
4536 // Handle GCC-style exception args.
4537 if (!C.getDriver().IsCLMode())
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004538 addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004539
Martell Malonec950c652017-11-29 07:25:12 +00004540 // Handle exception personalities
4541 Arg *A = Args.getLastArg(options::OPT_fsjlj_exceptions,
4542 options::OPT_fseh_exceptions,
4543 options::OPT_fdwarf_exceptions);
4544 if (A) {
4545 const Option &Opt = A->getOption();
4546 if (Opt.matches(options::OPT_fsjlj_exceptions))
4547 CmdArgs.push_back("-fsjlj-exceptions");
4548 if (Opt.matches(options::OPT_fseh_exceptions))
4549 CmdArgs.push_back("-fseh-exceptions");
4550 if (Opt.matches(options::OPT_fdwarf_exceptions))
4551 CmdArgs.push_back("-fdwarf-exceptions");
4552 } else {
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004553 switch (TC.GetExceptionModel(Args)) {
Reid Kleckner7383b8e2017-11-29 21:36:00 +00004554 default:
4555 break;
4556 case llvm::ExceptionHandling::DwarfCFI:
4557 CmdArgs.push_back("-fdwarf-exceptions");
4558 break;
4559 case llvm::ExceptionHandling::SjLj:
4560 CmdArgs.push_back("-fsjlj-exceptions");
4561 break;
4562 case llvm::ExceptionHandling::WinEH:
4563 CmdArgs.push_back("-fseh-exceptions");
4564 break;
Martell Malonec950c652017-11-29 07:25:12 +00004565 }
4566 }
David L. Jonesf561aba2017-03-08 01:02:16 +00004567
4568 // C++ "sane" operator new.
4569 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
4570 options::OPT_fno_assume_sane_operator_new))
4571 CmdArgs.push_back("-fno-assume-sane-operator-new");
4572
4573 // -frelaxed-template-template-args is off by default, as it is a severe
4574 // breaking change until a corresponding change to template partial ordering
4575 // is provided.
4576 if (Args.hasFlag(options::OPT_frelaxed_template_template_args,
4577 options::OPT_fno_relaxed_template_template_args, false))
4578 CmdArgs.push_back("-frelaxed-template-template-args");
4579
4580 // -fsized-deallocation is off by default, as it is an ABI-breaking change for
4581 // most platforms.
4582 if (Args.hasFlag(options::OPT_fsized_deallocation,
4583 options::OPT_fno_sized_deallocation, false))
4584 CmdArgs.push_back("-fsized-deallocation");
4585
4586 // -faligned-allocation is on by default in C++17 onwards and otherwise off
4587 // by default.
4588 if (Arg *A = Args.getLastArg(options::OPT_faligned_allocation,
4589 options::OPT_fno_aligned_allocation,
4590 options::OPT_faligned_new_EQ)) {
4591 if (A->getOption().matches(options::OPT_fno_aligned_allocation))
4592 CmdArgs.push_back("-fno-aligned-allocation");
4593 else
4594 CmdArgs.push_back("-faligned-allocation");
4595 }
4596
4597 // The default new alignment can be specified using a dedicated option or via
4598 // a GCC-compatible option that also turns on aligned allocation.
4599 if (Arg *A = Args.getLastArg(options::OPT_fnew_alignment_EQ,
4600 options::OPT_faligned_new_EQ))
4601 CmdArgs.push_back(
4602 Args.MakeArgString(Twine("-fnew-alignment=") + A->getValue()));
4603
4604 // -fconstant-cfstrings is default, and may be subject to argument translation
4605 // on Darwin.
4606 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
4607 options::OPT_fno_constant_cfstrings) ||
4608 !Args.hasFlag(options::OPT_mconstant_cfstrings,
4609 options::OPT_mno_constant_cfstrings))
4610 CmdArgs.push_back("-fno-constant-cfstrings");
4611
David L. Jonesf561aba2017-03-08 01:02:16 +00004612 // -fno-pascal-strings is default, only pass non-default.
4613 if (Args.hasFlag(options::OPT_fpascal_strings,
4614 options::OPT_fno_pascal_strings, false))
4615 CmdArgs.push_back("-fpascal-strings");
4616
4617 // Honor -fpack-struct= and -fpack-struct, if given. Note that
4618 // -fno-pack-struct doesn't apply to -fpack-struct=.
4619 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
4620 std::string PackStructStr = "-fpack-struct=";
4621 PackStructStr += A->getValue();
4622 CmdArgs.push_back(Args.MakeArgString(PackStructStr));
4623 } else if (Args.hasFlag(options::OPT_fpack_struct,
4624 options::OPT_fno_pack_struct, false)) {
4625 CmdArgs.push_back("-fpack-struct=1");
4626 }
4627
4628 // Handle -fmax-type-align=N and -fno-type-align
4629 bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align);
4630 if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) {
4631 if (!SkipMaxTypeAlign) {
4632 std::string MaxTypeAlignStr = "-fmax-type-align=";
4633 MaxTypeAlignStr += A->getValue();
4634 CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
4635 }
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00004636 } else if (RawTriple.isOSDarwin()) {
David L. Jonesf561aba2017-03-08 01:02:16 +00004637 if (!SkipMaxTypeAlign) {
4638 std::string MaxTypeAlignStr = "-fmax-type-align=16";
4639 CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
4640 }
4641 }
4642
Mikhail Maltsev4a4e7a32018-04-23 10:08:46 +00004643 if (!Args.hasFlag(options::OPT_Qy, options::OPT_Qn, true))
4644 CmdArgs.push_back("-Qn");
4645
David L. Jonesf561aba2017-03-08 01:02:16 +00004646 // -fcommon is the default unless compiling kernel code or the target says so
Saleem Abdulrasool374b5582017-08-29 23:59:05 +00004647 bool NoCommonDefault = KernelOrKext || isNoCommonDefault(RawTriple);
David L. Jonesf561aba2017-03-08 01:02:16 +00004648 if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common,
4649 !NoCommonDefault))
4650 CmdArgs.push_back("-fno-common");
4651
4652 // -fsigned-bitfields is default, and clang doesn't yet support
4653 // -funsigned-bitfields.
4654 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
4655 options::OPT_funsigned_bitfields))
4656 D.Diag(diag::warn_drv_clang_unsupported)
4657 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
4658
4659 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
4660 if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope))
4661 D.Diag(diag::err_drv_clang_unsupported)
4662 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
4663
4664 // -finput_charset=UTF-8 is default. Reject others
4665 if (Arg *inputCharset = Args.getLastArg(options::OPT_finput_charset_EQ)) {
4666 StringRef value = inputCharset->getValue();
4667 if (!value.equals_lower("utf-8"))
4668 D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args)
4669 << value;
4670 }
4671
4672 // -fexec_charset=UTF-8 is default. Reject others
4673 if (Arg *execCharset = Args.getLastArg(options::OPT_fexec_charset_EQ)) {
4674 StringRef value = execCharset->getValue();
4675 if (!value.equals_lower("utf-8"))
4676 D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args)
4677 << value;
4678 }
4679
Saleem Abdulrasool75557fa2017-09-01 18:57:34 +00004680 RenderDiagnosticsOptions(D, Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00004681
4682 // -fno-asm-blocks is default.
4683 if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
4684 false))
4685 CmdArgs.push_back("-fasm-blocks");
4686
4687 // -fgnu-inline-asm is default.
4688 if (!Args.hasFlag(options::OPT_fgnu_inline_asm,
4689 options::OPT_fno_gnu_inline_asm, true))
4690 CmdArgs.push_back("-fno-gnu-inline-asm");
4691
4692 // Enable vectorization per default according to the optimization level
4693 // selected. For optimization levels that want vectorization we use the alias
4694 // option to simplify the hasFlag logic.
4695 bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false);
4696 OptSpecifier VectorizeAliasOption =
4697 EnableVec ? options::OPT_O_Group : options::OPT_fvectorize;
4698 if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
4699 options::OPT_fno_vectorize, EnableVec))
4700 CmdArgs.push_back("-vectorize-loops");
4701
4702 // -fslp-vectorize is enabled based on the optimization level selected.
4703 bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true);
4704 OptSpecifier SLPVectAliasOption =
4705 EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize;
4706 if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption,
4707 options::OPT_fno_slp_vectorize, EnableSLPVec))
4708 CmdArgs.push_back("-vectorize-slp");
4709
Craig Topper9a724aa2017-12-11 21:09:19 +00004710 ParseMPreferVectorWidth(D, Args, CmdArgs);
4711
David L. Jonesf561aba2017-03-08 01:02:16 +00004712 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
4713 A->render(Args, CmdArgs);
4714
4715 if (Arg *A = Args.getLastArg(
4716 options::OPT_fsanitize_undefined_strip_path_components_EQ))
4717 A->render(Args, CmdArgs);
4718
4719 // -fdollars-in-identifiers default varies depending on platform and
4720 // language; only pass if specified.
4721 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
4722 options::OPT_fno_dollars_in_identifiers)) {
4723 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
4724 CmdArgs.push_back("-fdollars-in-identifiers");
4725 else
4726 CmdArgs.push_back("-fno-dollars-in-identifiers");
4727 }
4728
4729 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
4730 // practical purposes.
4731 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
4732 options::OPT_fno_unit_at_a_time)) {
4733 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
4734 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
4735 }
4736
4737 if (Args.hasFlag(options::OPT_fapple_pragma_pack,
4738 options::OPT_fno_apple_pragma_pack, false))
4739 CmdArgs.push_back("-fapple-pragma-pack");
4740
David L. Jonesf561aba2017-03-08 01:02:16 +00004741 if (Args.hasFlag(options::OPT_fsave_optimization_record,
Jonas Devliegherecf73eba2017-12-19 17:16:45 +00004742 options::OPT_foptimization_record_file_EQ,
David L. Jonesf561aba2017-03-08 01:02:16 +00004743 options::OPT_fno_save_optimization_record, false)) {
4744 CmdArgs.push_back("-opt-record-file");
4745
4746 const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ);
4747 if (A) {
4748 CmdArgs.push_back(A->getValue());
4749 } else {
4750 SmallString<128> F;
Hal Finkel67814df2017-08-16 21:34:27 +00004751
4752 if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) {
4753 if (Arg *FinalOutput = Args.getLastArg(options::OPT_o))
4754 F = FinalOutput->getValue();
4755 }
4756
4757 if (F.empty()) {
David L. Jonesf561aba2017-03-08 01:02:16 +00004758 // Use the input filename.
4759 F = llvm::sys::path::stem(Input.getBaseInput());
4760
4761 // If we're compiling for an offload architecture (i.e. a CUDA device),
4762 // we need to make the file name for the device compilation different
4763 // from the host compilation.
4764 if (!JA.isDeviceOffloading(Action::OFK_None) &&
4765 !JA.isDeviceOffloading(Action::OFK_Host)) {
4766 llvm::sys::path::replace_extension(F, "");
4767 F += Action::GetOffloadingFileNamePrefix(JA.getOffloadingDeviceKind(),
4768 Triple.normalize());
4769 F += "-";
4770 F += JA.getOffloadingArch();
4771 }
4772 }
4773
4774 llvm::sys::path::replace_extension(F, "opt.yaml");
4775 CmdArgs.push_back(Args.MakeArgString(F));
4776 }
4777 }
4778
Richard Smith86a3ef52017-06-09 21:24:02 +00004779 bool RewriteImports = Args.hasFlag(options::OPT_frewrite_imports,
4780 options::OPT_fno_rewrite_imports, false);
4781 if (RewriteImports)
4782 CmdArgs.push_back("-frewrite-imports");
4783
David L. Jonesf561aba2017-03-08 01:02:16 +00004784 // Enable rewrite includes if the user's asked for it or if we're generating
4785 // diagnostics.
4786 // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be
4787 // nice to enable this when doing a crashdump for modules as well.
4788 if (Args.hasFlag(options::OPT_frewrite_includes,
4789 options::OPT_fno_rewrite_includes, false) ||
Saleem Abdulrasoole196e942017-09-01 15:25:17 +00004790 (C.isForDiagnostics() && (RewriteImports || !HaveModules)))
David L. Jonesf561aba2017-03-08 01:02:16 +00004791 CmdArgs.push_back("-frewrite-includes");
4792
4793 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
4794 if (Arg *A = Args.getLastArg(options::OPT_traditional,
4795 options::OPT_traditional_cpp)) {
4796 if (isa<PreprocessJobAction>(JA))
4797 CmdArgs.push_back("-traditional-cpp");
4798 else
4799 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
4800 }
4801
4802 Args.AddLastArg(CmdArgs, options::OPT_dM);
4803 Args.AddLastArg(CmdArgs, options::OPT_dD);
4804
4805 // Handle serialized diagnostics.
4806 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
4807 CmdArgs.push_back("-serialize-diagnostic-file");
4808 CmdArgs.push_back(Args.MakeArgString(A->getValue()));
4809 }
4810
4811 if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
4812 CmdArgs.push_back("-fretain-comments-from-system-headers");
4813
4814 // Forward -fcomment-block-commands to -cc1.
4815 Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
4816 // Forward -fparse-all-comments to -cc1.
4817 Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments);
4818
4819 // Turn -fplugin=name.so into -load name.so
4820 for (const Arg *A : Args.filtered(options::OPT_fplugin_EQ)) {
4821 CmdArgs.push_back("-load");
4822 CmdArgs.push_back(A->getValue());
4823 A->claim();
4824 }
4825
4826 // Setup statistics file output.
Florian Hahn2e081d12018-04-20 12:50:10 +00004827 SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D);
4828 if (!StatsFile.empty())
4829 CmdArgs.push_back(Args.MakeArgString(Twine("-stats-file=") + StatsFile));
David L. Jonesf561aba2017-03-08 01:02:16 +00004830
4831 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
4832 // parser.
Guansong Zhang4747cf52017-03-15 20:57:11 +00004833 // -finclude-default-header flag is for preprocessor,
4834 // do not pass it to other cc1 commands when save-temps is enabled
4835 if (C.getDriver().isSaveTempsEnabled() &&
4836 !isa<PreprocessJobAction>(JA)) {
4837 for (auto Arg : Args.filtered(options::OPT_Xclang)) {
4838 Arg->claim();
4839 if (StringRef(Arg->getValue()) != "-finclude-default-header")
4840 CmdArgs.push_back(Arg->getValue());
4841 }
4842 }
4843 else {
4844 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
4845 }
David L. Jonesf561aba2017-03-08 01:02:16 +00004846 for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
4847 A->claim();
4848
4849 // We translate this by hand to the -cc1 argument, since nightly test uses
4850 // it and developers have been trained to spell it with -mllvm. Both
4851 // spellings are now deprecated and should be removed.
4852 if (StringRef(A->getValue(0)) == "-disable-llvm-optzns") {
4853 CmdArgs.push_back("-disable-llvm-optzns");
4854 } else {
4855 A->render(Args, CmdArgs);
4856 }
4857 }
4858
4859 // With -save-temps, we want to save the unoptimized bitcode output from the
4860 // CompileJobAction, use -disable-llvm-passes to get pristine IR generated
4861 // by the frontend.
4862 // When -fembed-bitcode is enabled, optimized bitcode is emitted because it
4863 // has slightly different breakdown between stages.
4864 // FIXME: -fembed-bitcode -save-temps will save optimized bitcode instead of
4865 // pristine IR generated by the frontend. Ideally, a new compile action should
4866 // be added so both IR can be captured.
4867 if (C.getDriver().isSaveTempsEnabled() &&
4868 !(C.getDriver().embedBitcodeInObject() && !C.getDriver().isUsingLTO()) &&
4869 isa<CompileJobAction>(JA))
4870 CmdArgs.push_back("-disable-llvm-passes");
4871
4872 if (Output.getType() == types::TY_Dependencies) {
4873 // Handled with other dependency code.
4874 } else if (Output.isFilename()) {
4875 CmdArgs.push_back("-o");
4876 CmdArgs.push_back(Output.getFilename());
4877 } else {
4878 assert(Output.isNothing() && "Invalid output.");
4879 }
4880
4881 addDashXForInput(Args, Input, CmdArgs);
4882
Richard Smithcd35eff2018-09-15 01:21:16 +00004883 ArrayRef<InputInfo> FrontendInputs = Input;
4884 if (IsHeaderModulePrecompile)
4885 FrontendInputs = ModuleHeaderInputs;
4886 else if (Input.isNothing())
4887 FrontendInputs = {};
4888
4889 for (const InputInfo &Input : FrontendInputs) {
4890 if (Input.isFilename())
4891 CmdArgs.push_back(Input.getFilename());
4892 else
4893 Input.getInputArg().renderAsInput(Args, CmdArgs);
4894 }
David L. Jonesf561aba2017-03-08 01:02:16 +00004895
4896 Args.AddAllArgs(CmdArgs, options::OPT_undef);
4897
Saleem Abdulrasool33d41382017-08-29 23:59:06 +00004898 const char *Exec = D.getClangProgramPath();
David L. Jonesf561aba2017-03-08 01:02:16 +00004899
4900 // Optionally embed the -cc1 level arguments into the debug info, for build
4901 // analysis.
Eric Christopherca325172017-03-29 23:34:20 +00004902 // Also record command line arguments into the debug info if
4903 // -grecord-gcc-switches options is set on.
4904 // By default, -gno-record-gcc-switches is set on and no recording.
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00004905 if (TC.UseDwarfDebugFlags() ||
Eric Christopherca325172017-03-29 23:34:20 +00004906 Args.hasFlag(options::OPT_grecord_gcc_switches,
4907 options::OPT_gno_record_gcc_switches, false)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00004908 ArgStringList OriginalArgs;
4909 for (const auto &Arg : Args)
4910 Arg->render(Args, OriginalArgs);
4911
4912 SmallString<256> Flags;
4913 Flags += Exec;
4914 for (const char *OriginalArg : OriginalArgs) {
4915 SmallString<128> EscapedArg;
4916 EscapeSpacesAndBackslashes(OriginalArg, EscapedArg);
4917 Flags += " ";
4918 Flags += EscapedArg;
4919 }
4920 CmdArgs.push_back("-dwarf-debug-flags");
4921 CmdArgs.push_back(Args.MakeArgString(Flags));
4922 }
4923
Yaxun Liu97670892018-10-02 17:48:54 +00004924 // Host-side cuda compilation receives all device-side outputs in a single
4925 // fatbin as Inputs[1]. Include the binary with -fcuda-include-gpubinary.
4926 if ((IsCuda || IsHIP) && CudaDeviceInput) {
Jonas Hahnfelde7681322018-02-28 17:53:46 +00004927 CmdArgs.push_back("-fcuda-include-gpubinary");
Richard Smithcd35eff2018-09-15 01:21:16 +00004928 CmdArgs.push_back(CudaDeviceInput->getFilename());
Yaxun Liu97670892018-10-02 17:48:54 +00004929 if (Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false))
4930 CmdArgs.push_back("-fgpu-rdc");
4931 }
David L. Jonesf561aba2017-03-08 01:02:16 +00004932
Yaxun Liu97670892018-10-02 17:48:54 +00004933 if (IsCuda) {
Artem Belevich679dafe2018-05-09 23:10:09 +00004934 if (Args.hasFlag(options::OPT_fcuda_short_ptr,
4935 options::OPT_fno_cuda_short_ptr, false))
4936 CmdArgs.push_back("-fcuda-short-ptr");
Jonas Hahnfeld5379c6d2018-02-12 10:46:45 +00004937 }
4938
David L. Jonesf561aba2017-03-08 01:02:16 +00004939 // OpenMP offloading device jobs take the argument -fopenmp-host-ir-file-path
4940 // to specify the result of the compile phase on the host, so the meaningful
4941 // device declarations can be identified. Also, -fopenmp-is-device is passed
4942 // along to tell the frontend that it is generating code for a device, so that
4943 // only the relevant declarations are emitted.
Gheorghe-Teodor Bercea3addb7d2017-06-29 15:59:19 +00004944 if (IsOpenMPDevice) {
David L. Jonesf561aba2017-03-08 01:02:16 +00004945 CmdArgs.push_back("-fopenmp-is-device");
Richard Smithcd35eff2018-09-15 01:21:16 +00004946 if (OpenMPDeviceInput) {
Gheorghe-Teodor Bercea3addb7d2017-06-29 15:59:19 +00004947 CmdArgs.push_back("-fopenmp-host-ir-file-path");
Richard Smithcd35eff2018-09-15 01:21:16 +00004948 CmdArgs.push_back(Args.MakeArgString(OpenMPDeviceInput->getFilename()));
Gheorghe-Teodor Bercea3addb7d2017-06-29 15:59:19 +00004949 }
David L. Jonesf561aba2017-03-08 01:02:16 +00004950 }
4951
4952 // For all the host OpenMP offloading compile jobs we need to pass the targets
4953 // information using -fopenmp-targets= option.
Alexey Bataev77403de2018-07-26 15:17:38 +00004954 if (JA.isHostOffloading(Action::OFK_OpenMP)) {
David L. Jonesf561aba2017-03-08 01:02:16 +00004955 SmallString<128> TargetInfo("-fopenmp-targets=");
4956
4957 Arg *Tgts = Args.getLastArg(options::OPT_fopenmp_targets_EQ);
4958 assert(Tgts && Tgts->getNumValues() &&
4959 "OpenMP offloading has to have targets specified.");
4960 for (unsigned i = 0; i < Tgts->getNumValues(); ++i) {
4961 if (i)
4962 TargetInfo += ',';
4963 // We need to get the string from the triple because it may be not exactly
4964 // the same as the one we get directly from the arguments.
4965 llvm::Triple T(Tgts->getValue(i));
4966 TargetInfo += T.getTriple();
4967 }
4968 CmdArgs.push_back(Args.MakeArgString(TargetInfo.str()));
4969 }
4970
4971 bool WholeProgramVTables =
4972 Args.hasFlag(options::OPT_fwhole_program_vtables,
4973 options::OPT_fno_whole_program_vtables, false);
4974 if (WholeProgramVTables) {
4975 if (!D.isUsingLTO())
4976 D.Diag(diag::err_drv_argument_only_allowed_with)
4977 << "-fwhole-program-vtables"
4978 << "-flto";
4979 CmdArgs.push_back("-fwhole-program-vtables");
4980 }
4981
Amara Emerson4ee9f822018-01-26 00:27:22 +00004982 if (Arg *A = Args.getLastArg(options::OPT_fexperimental_isel,
4983 options::OPT_fno_experimental_isel)) {
4984 CmdArgs.push_back("-mllvm");
4985 if (A->getOption().matches(options::OPT_fexperimental_isel)) {
4986 CmdArgs.push_back("-global-isel=1");
4987
4988 // GISel is on by default on AArch64 -O0, so don't bother adding
4989 // the fallback remarks for it. Other combinations will add a warning of
4990 // some kind.
4991 bool IsArchSupported = Triple.getArch() == llvm::Triple::aarch64;
4992 bool IsOptLevelSupported = false;
4993
4994 Arg *A = Args.getLastArg(options::OPT_O_Group);
4995 if (Triple.getArch() == llvm::Triple::aarch64) {
4996 if (!A || A->getOption().matches(options::OPT_O0))
4997 IsOptLevelSupported = true;
4998 }
4999 if (!IsArchSupported || !IsOptLevelSupported) {
5000 CmdArgs.push_back("-mllvm");
5001 CmdArgs.push_back("-global-isel-abort=2");
5002
5003 if (!IsArchSupported)
5004 D.Diag(diag::warn_drv_experimental_isel_incomplete) << Triple.getArchName();
5005 else
5006 D.Diag(diag::warn_drv_experimental_isel_incomplete_opt);
5007 }
5008 } else {
5009 CmdArgs.push_back("-global-isel=0");
5010 }
5011 }
5012
Mandeep Singh Grangac24bb52018-02-25 03:58:23 +00005013 if (Arg *A = Args.getLastArg(options::OPT_fforce_enable_int128,
5014 options::OPT_fno_force_enable_int128)) {
5015 if (A->getOption().matches(options::OPT_fforce_enable_int128))
5016 CmdArgs.push_back("-fforce-enable-int128");
5017 }
5018
Peter Collingbourne54d13b42018-05-30 03:40:04 +00005019 if (Args.hasFlag(options::OPT_fcomplete_member_pointers,
5020 options::OPT_fno_complete_member_pointers, false))
5021 CmdArgs.push_back("-fcomplete-member-pointers");
5022
Erik Pilkington5a559e62018-08-21 17:24:06 +00005023 if (!Args.hasFlag(options::OPT_fcxx_static_destructors,
5024 options::OPT_fno_cxx_static_destructors, true))
5025 CmdArgs.push_back("-fno-c++-static-destructors");
5026
Jessica Paquette36a25672018-06-29 18:06:10 +00005027 if (Arg *A = Args.getLastArg(options::OPT_moutline,
5028 options::OPT_mno_outline)) {
5029 if (A->getOption().matches(options::OPT_moutline)) {
5030 // We only support -moutline in AArch64 right now. If we're not compiling
5031 // for AArch64, emit a warning and ignore the flag. Otherwise, add the
5032 // proper mllvm flags.
5033 if (Triple.getArch() != llvm::Triple::aarch64) {
5034 D.Diag(diag::warn_drv_moutline_unsupported_opt) << Triple.getArchName();
5035 } else {
Jessica Paquette36a25672018-06-29 18:06:10 +00005036 CmdArgs.push_back("-mllvm");
Jessica Paquette33648c32018-07-06 22:24:56 +00005037 CmdArgs.push_back("-enable-machine-outliner");
Jessica Paquettea67abc82018-06-26 22:09:48 +00005038 }
Jessica Paquette36a25672018-06-29 18:06:10 +00005039 } else {
5040 // Disable all outlining behaviour.
5041 CmdArgs.push_back("-mllvm");
5042 CmdArgs.push_back("-enable-machine-outliner=never");
Jessica Paquettea67abc82018-06-26 22:09:48 +00005043 }
5044 }
5045
Peter Collingbourne14b468b2018-07-18 00:27:07 +00005046 if (Args.hasFlag(options::OPT_faddrsig, options::OPT_fno_addrsig,
Saleem Abdulrasool3806c532018-09-18 22:14:50 +00005047 (TC.getTriple().isOSBinFormatELF() ||
5048 TC.getTriple().isOSBinFormatCOFF()) &&
5049 TC.useIntegratedAs()))
Peter Collingbourne14b468b2018-07-18 00:27:07 +00005050 CmdArgs.push_back("-faddrsig");
5051
David L. Jonesf561aba2017-03-08 01:02:16 +00005052 // Finally add the compile command to the compilation.
5053 if (Args.hasArg(options::OPT__SLASH_fallback) &&
5054 Output.getType() == types::TY_Object &&
5055 (InputType == types::TY_C || InputType == types::TY_CXX)) {
5056 auto CLCommand =
5057 getCLFallback()->GetCommand(C, JA, Output, Inputs, Args, LinkingOutput);
5058 C.addCommand(llvm::make_unique<FallbackCommand>(
5059 JA, *this, Exec, CmdArgs, Inputs, std::move(CLCommand)));
5060 } else if (Args.hasArg(options::OPT__SLASH_fallback) &&
5061 isa<PrecompileJobAction>(JA)) {
5062 // In /fallback builds, run the main compilation even if the pch generation
5063 // fails, so that the main compilation's fallback to cl.exe runs.
5064 C.addCommand(llvm::make_unique<ForceSuccessCommand>(JA, *this, Exec,
5065 CmdArgs, Inputs));
5066 } else {
5067 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
5068 }
5069
David L. Jonesf561aba2017-03-08 01:02:16 +00005070 if (Arg *A = Args.getLastArg(options::OPT_pg))
David Blaikie5941da32018-09-18 20:11:45 +00005071 if (!shouldUseFramePointer(Args, Triple))
David L. Jonesf561aba2017-03-08 01:02:16 +00005072 D.Diag(diag::err_drv_argument_not_allowed_with) << "-fomit-frame-pointer"
5073 << A->getAsString(Args);
5074
5075 // Claim some arguments which clang supports automatically.
5076
5077 // -fpch-preprocess is used with gcc to add a special marker in the output to
5078 // include the PCH file. Clang's PTH solution is completely transparent, so we
5079 // do not need to deal with it at all.
5080 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
5081
5082 // Claim some arguments which clang doesn't support, but we don't
5083 // care to warn the user about.
5084 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
5085 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
5086
5087 // Disable warnings for clang -E -emit-llvm foo.c
5088 Args.ClaimAllArgs(options::OPT_emit_llvm);
5089}
5090
5091Clang::Clang(const ToolChain &TC)
5092 // CAUTION! The first constructor argument ("clang") is not arbitrary,
5093 // as it is for other tools. Some operations on a Tool actually test
5094 // whether that tool is Clang based on the Tool's Name as a string.
5095 : Tool("clang", "clang frontend", TC, RF_Full) {}
5096
5097Clang::~Clang() {}
5098
5099/// Add options related to the Objective-C runtime/ABI.
5100///
5101/// Returns true if the runtime is non-fragile.
5102ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
5103 ArgStringList &cmdArgs,
5104 RewriteKind rewriteKind) const {
5105 // Look for the controlling runtime option.
5106 Arg *runtimeArg =
5107 args.getLastArg(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
5108 options::OPT_fobjc_runtime_EQ);
5109
5110 // Just forward -fobjc-runtime= to the frontend. This supercedes
5111 // options about fragility.
5112 if (runtimeArg &&
5113 runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
5114 ObjCRuntime runtime;
5115 StringRef value = runtimeArg->getValue();
5116 if (runtime.tryParse(value)) {
5117 getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
5118 << value;
5119 }
David Chisnall404bbcb2018-05-22 10:13:06 +00005120 if ((runtime.getKind() == ObjCRuntime::GNUstep) &&
5121 (runtime.getVersion() >= VersionTuple(2, 0)))
David Chisnallef16ea72018-09-04 10:07:27 +00005122 if (!getToolChain().getTriple().isOSBinFormatELF() &&
5123 !getToolChain().getTriple().isOSBinFormatCOFF()) {
David Chisnall404bbcb2018-05-22 10:13:06 +00005124 getToolChain().getDriver().Diag(
5125 diag::err_drv_gnustep_objc_runtime_incompatible_binary)
5126 << runtime.getVersion().getMajor();
5127 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005128
5129 runtimeArg->render(args, cmdArgs);
5130 return runtime;
5131 }
5132
5133 // Otherwise, we'll need the ABI "version". Version numbers are
5134 // slightly confusing for historical reasons:
5135 // 1 - Traditional "fragile" ABI
5136 // 2 - Non-fragile ABI, version 1
5137 // 3 - Non-fragile ABI, version 2
5138 unsigned objcABIVersion = 1;
5139 // If -fobjc-abi-version= is present, use that to set the version.
5140 if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
5141 StringRef value = abiArg->getValue();
5142 if (value == "1")
5143 objcABIVersion = 1;
5144 else if (value == "2")
5145 objcABIVersion = 2;
5146 else if (value == "3")
5147 objcABIVersion = 3;
5148 else
5149 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) << value;
5150 } else {
5151 // Otherwise, determine if we are using the non-fragile ABI.
5152 bool nonFragileABIIsDefault =
5153 (rewriteKind == RK_NonFragile ||
5154 (rewriteKind == RK_None &&
5155 getToolChain().IsObjCNonFragileABIDefault()));
5156 if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
5157 options::OPT_fno_objc_nonfragile_abi,
5158 nonFragileABIIsDefault)) {
5159// Determine the non-fragile ABI version to use.
5160#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
5161 unsigned nonFragileABIVersion = 1;
5162#else
5163 unsigned nonFragileABIVersion = 2;
5164#endif
5165
5166 if (Arg *abiArg =
5167 args.getLastArg(options::OPT_fobjc_nonfragile_abi_version_EQ)) {
5168 StringRef value = abiArg->getValue();
5169 if (value == "1")
5170 nonFragileABIVersion = 1;
5171 else if (value == "2")
5172 nonFragileABIVersion = 2;
5173 else
5174 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
5175 << value;
5176 }
5177
5178 objcABIVersion = 1 + nonFragileABIVersion;
5179 } else {
5180 objcABIVersion = 1;
5181 }
5182 }
5183
5184 // We don't actually care about the ABI version other than whether
5185 // it's non-fragile.
5186 bool isNonFragile = objcABIVersion != 1;
5187
5188 // If we have no runtime argument, ask the toolchain for its default runtime.
5189 // However, the rewriter only really supports the Mac runtime, so assume that.
5190 ObjCRuntime runtime;
5191 if (!runtimeArg) {
5192 switch (rewriteKind) {
5193 case RK_None:
5194 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
5195 break;
5196 case RK_Fragile:
5197 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
5198 break;
5199 case RK_NonFragile:
5200 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
5201 break;
5202 }
5203
5204 // -fnext-runtime
5205 } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
5206 // On Darwin, make this use the default behavior for the toolchain.
5207 if (getToolChain().getTriple().isOSDarwin()) {
5208 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
5209
5210 // Otherwise, build for a generic macosx port.
5211 } else {
5212 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
5213 }
5214
5215 // -fgnu-runtime
5216 } else {
5217 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
5218 // Legacy behaviour is to target the gnustep runtime if we are in
5219 // non-fragile mode or the GCC runtime in fragile mode.
5220 if (isNonFragile)
David Chisnall404bbcb2018-05-22 10:13:06 +00005221 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(2, 0));
David L. Jonesf561aba2017-03-08 01:02:16 +00005222 else
5223 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
5224 }
5225
5226 cmdArgs.push_back(
5227 args.MakeArgString("-fobjc-runtime=" + runtime.getAsString()));
5228 return runtime;
5229}
5230
5231static bool maybeConsumeDash(const std::string &EH, size_t &I) {
5232 bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-');
5233 I += HaveDash;
5234 return !HaveDash;
5235}
5236
5237namespace {
5238struct EHFlags {
5239 bool Synch = false;
5240 bool Asynch = false;
5241 bool NoUnwindC = false;
5242};
5243} // end anonymous namespace
5244
5245/// /EH controls whether to run destructor cleanups when exceptions are
5246/// thrown. There are three modifiers:
5247/// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions.
5248/// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions.
5249/// The 'a' modifier is unimplemented and fundamentally hard in LLVM IR.
5250/// - c: Assume that extern "C" functions are implicitly nounwind.
5251/// The default is /EHs-c-, meaning cleanups are disabled.
5252static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) {
5253 EHFlags EH;
5254
5255 std::vector<std::string> EHArgs =
5256 Args.getAllArgValues(options::OPT__SLASH_EH);
5257 for (auto EHVal : EHArgs) {
5258 for (size_t I = 0, E = EHVal.size(); I != E; ++I) {
5259 switch (EHVal[I]) {
5260 case 'a':
5261 EH.Asynch = maybeConsumeDash(EHVal, I);
5262 if (EH.Asynch)
5263 EH.Synch = false;
5264 continue;
5265 case 'c':
5266 EH.NoUnwindC = maybeConsumeDash(EHVal, I);
5267 continue;
5268 case 's':
5269 EH.Synch = maybeConsumeDash(EHVal, I);
5270 if (EH.Synch)
5271 EH.Asynch = false;
5272 continue;
5273 default:
5274 break;
5275 }
5276 D.Diag(clang::diag::err_drv_invalid_value) << "/EH" << EHVal;
5277 break;
5278 }
5279 }
5280 // The /GX, /GX- flags are only processed if there are not /EH flags.
5281 // The default is that /GX is not specified.
5282 if (EHArgs.empty() &&
5283 Args.hasFlag(options::OPT__SLASH_GX, options::OPT__SLASH_GX_,
5284 /*default=*/false)) {
5285 EH.Synch = true;
5286 EH.NoUnwindC = true;
5287 }
5288
5289 return EH;
5290}
5291
5292void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
5293 ArgStringList &CmdArgs,
5294 codegenoptions::DebugInfoKind *DebugInfoKind,
5295 bool *EmitCodeView) const {
5296 unsigned RTOptionID = options::OPT__SLASH_MT;
5297
5298 if (Args.hasArg(options::OPT__SLASH_LDd))
5299 // The /LDd option implies /MTd. The dependent lib part can be overridden,
5300 // but defining _DEBUG is sticky.
5301 RTOptionID = options::OPT__SLASH_MTd;
5302
5303 if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group))
5304 RTOptionID = A->getOption().getID();
5305
5306 StringRef FlagForCRT;
5307 switch (RTOptionID) {
5308 case options::OPT__SLASH_MD:
5309 if (Args.hasArg(options::OPT__SLASH_LDd))
5310 CmdArgs.push_back("-D_DEBUG");
5311 CmdArgs.push_back("-D_MT");
5312 CmdArgs.push_back("-D_DLL");
5313 FlagForCRT = "--dependent-lib=msvcrt";
5314 break;
5315 case options::OPT__SLASH_MDd:
5316 CmdArgs.push_back("-D_DEBUG");
5317 CmdArgs.push_back("-D_MT");
5318 CmdArgs.push_back("-D_DLL");
5319 FlagForCRT = "--dependent-lib=msvcrtd";
5320 break;
5321 case options::OPT__SLASH_MT:
5322 if (Args.hasArg(options::OPT__SLASH_LDd))
5323 CmdArgs.push_back("-D_DEBUG");
5324 CmdArgs.push_back("-D_MT");
5325 CmdArgs.push_back("-flto-visibility-public-std");
5326 FlagForCRT = "--dependent-lib=libcmt";
5327 break;
5328 case options::OPT__SLASH_MTd:
5329 CmdArgs.push_back("-D_DEBUG");
5330 CmdArgs.push_back("-D_MT");
5331 CmdArgs.push_back("-flto-visibility-public-std");
5332 FlagForCRT = "--dependent-lib=libcmtd";
5333 break;
5334 default:
5335 llvm_unreachable("Unexpected option ID.");
5336 }
5337
5338 if (Args.hasArg(options::OPT__SLASH_Zl)) {
5339 CmdArgs.push_back("-D_VC_NODEFAULTLIB");
5340 } else {
5341 CmdArgs.push_back(FlagForCRT.data());
5342
5343 // This provides POSIX compatibility (maps 'open' to '_open'), which most
5344 // users want. The /Za flag to cl.exe turns this off, but it's not
5345 // implemented in clang.
5346 CmdArgs.push_back("--dependent-lib=oldnames");
5347 }
5348
Erich Keane425f48d2018-05-04 15:58:31 +00005349 if (Arg *A = Args.getLastArg(options::OPT_show_includes))
5350 A->render(Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +00005351
5352 // This controls whether or not we emit RTTI data for polymorphic types.
5353 if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,
5354 /*default=*/false))
5355 CmdArgs.push_back("-fno-rtti-data");
5356
5357 // This controls whether or not we emit stack-protector instrumentation.
5358 // In MSVC, Buffer Security Check (/GS) is on by default.
5359 if (Args.hasFlag(options::OPT__SLASH_GS, options::OPT__SLASH_GS_,
5360 /*default=*/true)) {
5361 CmdArgs.push_back("-stack-protector");
5362 CmdArgs.push_back(Args.MakeArgString(Twine(LangOptions::SSPStrong)));
5363 }
5364
5365 // Emit CodeView if -Z7, -Zd, or -gline-tables-only are present.
5366 if (Arg *DebugInfoArg =
5367 Args.getLastArg(options::OPT__SLASH_Z7, options::OPT__SLASH_Zd,
5368 options::OPT_gline_tables_only)) {
5369 *EmitCodeView = true;
5370 if (DebugInfoArg->getOption().matches(options::OPT__SLASH_Z7))
5371 *DebugInfoKind = codegenoptions::LimitedDebugInfo;
5372 else
5373 *DebugInfoKind = codegenoptions::DebugLineTablesOnly;
5374 CmdArgs.push_back("-gcodeview");
5375 } else {
5376 *EmitCodeView = false;
5377 }
5378
5379 const Driver &D = getToolChain().getDriver();
5380 EHFlags EH = parseClangCLEHFlags(D, Args);
5381 if (EH.Synch || EH.Asynch) {
5382 if (types::isCXX(InputType))
5383 CmdArgs.push_back("-fcxx-exceptions");
5384 CmdArgs.push_back("-fexceptions");
5385 }
5386 if (types::isCXX(InputType) && EH.Synch && EH.NoUnwindC)
5387 CmdArgs.push_back("-fexternc-nounwind");
5388
5389 // /EP should expand to -E -P.
5390 if (Args.hasArg(options::OPT__SLASH_EP)) {
5391 CmdArgs.push_back("-E");
5392 CmdArgs.push_back("-P");
5393 }
5394
5395 unsigned VolatileOptionID;
5396 if (getToolChain().getArch() == llvm::Triple::x86_64 ||
5397 getToolChain().getArch() == llvm::Triple::x86)
5398 VolatileOptionID = options::OPT__SLASH_volatile_ms;
5399 else
5400 VolatileOptionID = options::OPT__SLASH_volatile_iso;
5401
5402 if (Arg *A = Args.getLastArg(options::OPT__SLASH_volatile_Group))
5403 VolatileOptionID = A->getOption().getID();
5404
5405 if (VolatileOptionID == options::OPT__SLASH_volatile_ms)
5406 CmdArgs.push_back("-fms-volatile");
5407
5408 Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg);
5409 Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb);
5410 if (MostGeneralArg && BestCaseArg)
5411 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
5412 << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args);
5413
5414 if (MostGeneralArg) {
5415 Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms);
5416 Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm);
5417 Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv);
5418
5419 Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg;
5420 Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg;
5421 if (FirstConflict && SecondConflict && FirstConflict != SecondConflict)
5422 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
5423 << FirstConflict->getAsString(Args)
5424 << SecondConflict->getAsString(Args);
5425
5426 if (SingleArg)
5427 CmdArgs.push_back("-fms-memptr-rep=single");
5428 else if (MultipleArg)
5429 CmdArgs.push_back("-fms-memptr-rep=multiple");
5430 else
5431 CmdArgs.push_back("-fms-memptr-rep=virtual");
5432 }
5433
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005434 // Parse the default calling convention options.
5435 if (Arg *CCArg =
5436 Args.getLastArg(options::OPT__SLASH_Gd, options::OPT__SLASH_Gr,
Erich Keanea957ffb2017-11-02 21:08:00 +00005437 options::OPT__SLASH_Gz, options::OPT__SLASH_Gv,
5438 options::OPT__SLASH_Gregcall)) {
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005439 unsigned DCCOptId = CCArg->getOption().getID();
5440 const char *DCCFlag = nullptr;
5441 bool ArchSupported = true;
5442 llvm::Triple::ArchType Arch = getToolChain().getArch();
5443 switch (DCCOptId) {
5444 case options::OPT__SLASH_Gd:
Reid Kleckner6344f102017-05-31 15:50:35 +00005445 DCCFlag = "-fdefault-calling-conv=cdecl";
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005446 break;
5447 case options::OPT__SLASH_Gr:
5448 ArchSupported = Arch == llvm::Triple::x86;
Reid Kleckner6344f102017-05-31 15:50:35 +00005449 DCCFlag = "-fdefault-calling-conv=fastcall";
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005450 break;
5451 case options::OPT__SLASH_Gz:
5452 ArchSupported = Arch == llvm::Triple::x86;
Reid Kleckner6344f102017-05-31 15:50:35 +00005453 DCCFlag = "-fdefault-calling-conv=stdcall";
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005454 break;
5455 case options::OPT__SLASH_Gv:
5456 ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64;
Reid Kleckner6344f102017-05-31 15:50:35 +00005457 DCCFlag = "-fdefault-calling-conv=vectorcall";
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005458 break;
Erich Keanea957ffb2017-11-02 21:08:00 +00005459 case options::OPT__SLASH_Gregcall:
5460 ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64;
5461 DCCFlag = "-fdefault-calling-conv=regcall";
5462 break;
Reid Kleckner4b2f3262017-05-31 15:39:28 +00005463 }
5464
5465 // MSVC doesn't warn if /Gr or /Gz is used on x64, so we don't either.
5466 if (ArchSupported && DCCFlag)
5467 CmdArgs.push_back(DCCFlag);
5468 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005469
5470 if (Arg *A = Args.getLastArg(options::OPT_vtordisp_mode_EQ))
5471 A->render(Args, CmdArgs);
5472
5473 if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) {
5474 CmdArgs.push_back("-fdiagnostics-format");
5475 if (Args.hasArg(options::OPT__SLASH_fallback))
5476 CmdArgs.push_back("msvc-fallback");
5477 else
5478 CmdArgs.push_back("msvc");
5479 }
Adrian McCarthydb2736d2018-01-09 23:49:30 +00005480
Hans Wennborga912e3e2018-08-10 09:49:21 +00005481 if (Arg *A = Args.getLastArg(options::OPT__SLASH_guard)) {
5482 SmallVector<StringRef, 1> SplitArgs;
5483 StringRef(A->getValue()).split(SplitArgs, ",");
5484 bool Instrument = false;
5485 bool NoChecks = false;
5486 for (StringRef Arg : SplitArgs) {
5487 if (Arg.equals_lower("cf"))
5488 Instrument = true;
5489 else if (Arg.equals_lower("cf-"))
5490 Instrument = false;
5491 else if (Arg.equals_lower("nochecks"))
5492 NoChecks = true;
5493 else if (Arg.equals_lower("nochecks-"))
5494 NoChecks = false;
5495 else
5496 D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << Arg;
5497 }
5498 // Currently there's no support emitting CFG instrumentation; the flag only
5499 // emits the table of address-taken functions.
5500 if (Instrument || NoChecks)
5501 CmdArgs.push_back("-cfguard");
5502 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005503}
5504
5505visualstudio::Compiler *Clang::getCLFallback() const {
5506 if (!CLFallback)
5507 CLFallback.reset(new visualstudio::Compiler(getToolChain()));
5508 return CLFallback.get();
5509}
5510
5511
5512const char *Clang::getBaseInputName(const ArgList &Args,
5513 const InputInfo &Input) {
5514 return Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput()));
5515}
5516
5517const char *Clang::getBaseInputStem(const ArgList &Args,
5518 const InputInfoList &Inputs) {
5519 const char *Str = getBaseInputName(Args, Inputs[0]);
5520
5521 if (const char *End = strrchr(Str, '.'))
5522 return Args.MakeArgString(std::string(Str, End));
5523
5524 return Str;
5525}
5526
5527const char *Clang::getDependencyFileName(const ArgList &Args,
5528 const InputInfoList &Inputs) {
5529 // FIXME: Think about this more.
5530 std::string Res;
5531
5532 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
5533 std::string Str(OutputOpt->getValue());
5534 Res = Str.substr(0, Str.rfind('.'));
5535 } else {
5536 Res = getBaseInputStem(Args, Inputs);
5537 }
5538 return Args.MakeArgString(Res + ".d");
5539}
5540
5541// Begin ClangAs
5542
5543void ClangAs::AddMIPSTargetArgs(const ArgList &Args,
5544 ArgStringList &CmdArgs) const {
5545 StringRef CPUName;
5546 StringRef ABIName;
5547 const llvm::Triple &Triple = getToolChain().getTriple();
5548 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
5549
5550 CmdArgs.push_back("-target-abi");
5551 CmdArgs.push_back(ABIName.data());
5552}
5553
5554void ClangAs::AddX86TargetArgs(const ArgList &Args,
5555 ArgStringList &CmdArgs) const {
5556 if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
5557 StringRef Value = A->getValue();
5558 if (Value == "intel" || Value == "att") {
5559 CmdArgs.push_back("-mllvm");
5560 CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
5561 } else {
5562 getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
5563 << A->getOption().getName() << Value;
5564 }
5565 }
5566}
5567
5568void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
5569 const InputInfo &Output, const InputInfoList &Inputs,
5570 const ArgList &Args,
5571 const char *LinkingOutput) const {
5572 ArgStringList CmdArgs;
5573
5574 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
5575 const InputInfo &Input = Inputs[0];
5576
5577 const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
5578 const std::string &TripleStr = Triple.getTriple();
Saleem Abdulrasoold064e912017-06-23 15:34:16 +00005579 const auto &D = getToolChain().getDriver();
David L. Jonesf561aba2017-03-08 01:02:16 +00005580
5581 // Don't warn about "clang -w -c foo.s"
5582 Args.ClaimAllArgs(options::OPT_w);
5583 // and "clang -emit-llvm -c foo.s"
5584 Args.ClaimAllArgs(options::OPT_emit_llvm);
5585
5586 claimNoWarnArgs(Args);
5587
5588 // Invoke ourselves in -cc1as mode.
5589 //
5590 // FIXME: Implement custom jobs for internal actions.
5591 CmdArgs.push_back("-cc1as");
5592
5593 // Add the "effective" target triple.
5594 CmdArgs.push_back("-triple");
5595 CmdArgs.push_back(Args.MakeArgString(TripleStr));
5596
5597 // Set the output mode, we currently only expect to be used as a real
5598 // assembler.
5599 CmdArgs.push_back("-filetype");
5600 CmdArgs.push_back("obj");
5601
5602 // Set the main file name, so that debug info works even with
5603 // -save-temps or preprocessed assembly.
5604 CmdArgs.push_back("-main-file-name");
5605 CmdArgs.push_back(Clang::getBaseInputName(Args, Input));
5606
5607 // Add the target cpu
5608 std::string CPU = getCPUName(Args, Triple, /*FromAs*/ true);
5609 if (!CPU.empty()) {
5610 CmdArgs.push_back("-target-cpu");
5611 CmdArgs.push_back(Args.MakeArgString(CPU));
5612 }
5613
5614 // Add the target features
5615 getTargetFeatures(getToolChain(), Triple, Args, CmdArgs, true);
5616
5617 // Ignore explicit -force_cpusubtype_ALL option.
5618 (void)Args.hasArg(options::OPT_force__cpusubtype__ALL);
5619
5620 // Pass along any -I options so we get proper .include search paths.
5621 Args.AddAllArgs(CmdArgs, options::OPT_I_Group);
5622
5623 // Determine the original source input.
5624 const Action *SourceAction = &JA;
5625 while (SourceAction->getKind() != Action::InputClass) {
5626 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
5627 SourceAction = SourceAction->getInputs()[0];
5628 }
5629
5630 // Forward -g and handle debug info related flags, assuming we are dealing
5631 // with an actual assembly file.
5632 bool WantDebug = false;
5633 unsigned DwarfVersion = 0;
5634 Args.ClaimAllArgs(options::OPT_g_Group);
5635 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
5636 WantDebug = !A->getOption().matches(options::OPT_g0) &&
5637 !A->getOption().matches(options::OPT_ggdb0);
5638 if (WantDebug)
5639 DwarfVersion = DwarfVersionNum(A->getSpelling());
5640 }
5641 if (DwarfVersion == 0)
5642 DwarfVersion = getToolChain().GetDefaultDwarfVersion();
5643
5644 codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo;
5645
5646 if (SourceAction->getType() == types::TY_Asm ||
5647 SourceAction->getType() == types::TY_PP_Asm) {
5648 // You might think that it would be ok to set DebugInfoKind outside of
5649 // the guard for source type, however there is a test which asserts
5650 // that some assembler invocation receives no -debug-info-kind,
5651 // and it's not clear whether that test is just overly restrictive.
5652 DebugInfoKind = (WantDebug ? codegenoptions::LimitedDebugInfo
5653 : codegenoptions::NoDebugInfo);
5654 // Add the -fdebug-compilation-dir flag if needed.
5655 addDebugCompDirArg(Args, CmdArgs);
5656
Paul Robinson9b292b42018-07-10 15:15:24 +00005657 addDebugPrefixMapArg(getToolChain().getDriver(), Args, CmdArgs);
5658
David L. Jonesf561aba2017-03-08 01:02:16 +00005659 // Set the AT_producer to the clang version when using the integrated
5660 // assembler on assembly source files.
5661 CmdArgs.push_back("-dwarf-debug-producer");
5662 CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
5663
5664 // And pass along -I options
5665 Args.AddAllArgs(CmdArgs, options::OPT_I);
5666 }
5667 RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion,
5668 llvm::DebuggerKind::Default);
Alexey Bataevb83b4e42018-07-27 19:45:14 +00005669 RenderDebugInfoCompressionArgs(Args, CmdArgs, D, getToolChain());
Saleem Abdulrasoold064e912017-06-23 15:34:16 +00005670
David L. Jonesf561aba2017-03-08 01:02:16 +00005671
5672 // Handle -fPIC et al -- the relocation-model affects the assembler
5673 // for some targets.
5674 llvm::Reloc::Model RelocationModel;
5675 unsigned PICLevel;
5676 bool IsPIE;
5677 std::tie(RelocationModel, PICLevel, IsPIE) =
5678 ParsePICArgs(getToolChain(), Args);
5679
5680 const char *RMName = RelocationModelName(RelocationModel);
5681 if (RMName) {
5682 CmdArgs.push_back("-mrelocation-model");
5683 CmdArgs.push_back(RMName);
5684 }
5685
5686 // Optionally embed the -cc1as level arguments into the debug info, for build
5687 // analysis.
5688 if (getToolChain().UseDwarfDebugFlags()) {
5689 ArgStringList OriginalArgs;
5690 for (const auto &Arg : Args)
5691 Arg->render(Args, OriginalArgs);
5692
5693 SmallString<256> Flags;
5694 const char *Exec = getToolChain().getDriver().getClangProgramPath();
5695 Flags += Exec;
5696 for (const char *OriginalArg : OriginalArgs) {
5697 SmallString<128> EscapedArg;
5698 EscapeSpacesAndBackslashes(OriginalArg, EscapedArg);
5699 Flags += " ";
5700 Flags += EscapedArg;
5701 }
5702 CmdArgs.push_back("-dwarf-debug-flags");
5703 CmdArgs.push_back(Args.MakeArgString(Flags));
5704 }
5705
5706 // FIXME: Add -static support, once we have it.
5707
5708 // Add target specific flags.
5709 switch (getToolChain().getArch()) {
5710 default:
5711 break;
5712
5713 case llvm::Triple::mips:
5714 case llvm::Triple::mipsel:
5715 case llvm::Triple::mips64:
5716 case llvm::Triple::mips64el:
5717 AddMIPSTargetArgs(Args, CmdArgs);
5718 break;
5719
5720 case llvm::Triple::x86:
5721 case llvm::Triple::x86_64:
5722 AddX86TargetArgs(Args, CmdArgs);
5723 break;
Oliver Stannard692dc542017-04-18 13:21:05 +00005724
5725 case llvm::Triple::arm:
5726 case llvm::Triple::armeb:
5727 case llvm::Triple::thumb:
5728 case llvm::Triple::thumbeb:
5729 // This isn't in AddARMTargetArgs because we want to do this for assembly
5730 // only, not C/C++.
5731 if (Args.hasFlag(options::OPT_mdefault_build_attributes,
5732 options::OPT_mno_default_build_attributes, true)) {
5733 CmdArgs.push_back("-mllvm");
5734 CmdArgs.push_back("-arm-add-build-attributes");
5735 }
5736 break;
David L. Jonesf561aba2017-03-08 01:02:16 +00005737 }
5738
5739 // Consume all the warning flags. Usually this would be handled more
5740 // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as
5741 // doesn't handle that so rather than warning about unused flags that are
5742 // actually used, we'll lie by omission instead.
5743 // FIXME: Stop lying and consume only the appropriate driver flags
5744 Args.ClaimAllArgs(options::OPT_W_Group);
5745
5746 CollectArgsForIntegratedAssembler(C, Args, CmdArgs,
5747 getToolChain().getDriver());
5748
5749 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
5750
5751 assert(Output.isFilename() && "Unexpected lipo output.");
5752 CmdArgs.push_back("-o");
5753 CmdArgs.push_back(Output.getFilename());
5754
Peter Collingbourne91d02842018-05-22 18:52:37 +00005755 if (Args.hasArg(options::OPT_gsplit_dwarf) &&
5756 getToolChain().getTriple().isOSLinux()) {
5757 CmdArgs.push_back("-split-dwarf-file");
5758 CmdArgs.push_back(SplitDebugName(Args, Input));
5759 }
5760
David L. Jonesf561aba2017-03-08 01:02:16 +00005761 assert(Input.isFilename() && "Invalid input.");
5762 CmdArgs.push_back(Input.getFilename());
5763
5764 const char *Exec = getToolChain().getDriver().getClangProgramPath();
5765 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
David L. Jonesf561aba2017-03-08 01:02:16 +00005766}
5767
5768// Begin OffloadBundler
5769
5770void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA,
5771 const InputInfo &Output,
5772 const InputInfoList &Inputs,
5773 const llvm::opt::ArgList &TCArgs,
5774 const char *LinkingOutput) const {
5775 // The version with only one output is expected to refer to a bundling job.
5776 assert(isa<OffloadBundlingJobAction>(JA) && "Expecting bundling job!");
5777
5778 // The bundling command looks like this:
5779 // clang-offload-bundler -type=bc
5780 // -targets=host-triple,openmp-triple1,openmp-triple2
5781 // -outputs=input_file
5782 // -inputs=unbundle_file_host,unbundle_file_tgt1,unbundle_file_tgt2"
5783
5784 ArgStringList CmdArgs;
5785
5786 // Get the type.
5787 CmdArgs.push_back(TCArgs.MakeArgString(
5788 Twine("-type=") + types::getTypeTempSuffix(Output.getType())));
5789
5790 assert(JA.getInputs().size() == Inputs.size() &&
5791 "Not have inputs for all dependence actions??");
5792
5793 // Get the targets.
5794 SmallString<128> Triples;
5795 Triples += "-targets=";
5796 for (unsigned I = 0; I < Inputs.size(); ++I) {
5797 if (I)
5798 Triples += ',';
5799
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +00005800 // Find ToolChain for this input.
David L. Jonesf561aba2017-03-08 01:02:16 +00005801 Action::OffloadKind CurKind = Action::OFK_Host;
5802 const ToolChain *CurTC = &getToolChain();
5803 const Action *CurDep = JA.getInputs()[I];
5804
5805 if (const auto *OA = dyn_cast<OffloadAction>(CurDep)) {
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +00005806 CurTC = nullptr;
David L. Jonesf561aba2017-03-08 01:02:16 +00005807 OA->doOnEachDependence([&](Action *A, const ToolChain *TC, const char *) {
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +00005808 assert(CurTC == nullptr && "Expected one dependence!");
David L. Jonesf561aba2017-03-08 01:02:16 +00005809 CurKind = A->getOffloadingDeviceKind();
5810 CurTC = TC;
5811 });
5812 }
5813 Triples += Action::GetOffloadKindName(CurKind);
5814 Triples += '-';
5815 Triples += CurTC->getTriple().normalize();
Yaxun Liu609f7522018-05-11 19:02:18 +00005816 if (CurKind == Action::OFK_HIP && CurDep->getOffloadingArch()) {
5817 Triples += '-';
5818 Triples += CurDep->getOffloadingArch();
5819 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005820 }
5821 CmdArgs.push_back(TCArgs.MakeArgString(Triples));
5822
5823 // Get bundled file command.
5824 CmdArgs.push_back(
5825 TCArgs.MakeArgString(Twine("-outputs=") + Output.getFilename()));
5826
5827 // Get unbundled files command.
5828 SmallString<128> UB;
5829 UB += "-inputs=";
5830 for (unsigned I = 0; I < Inputs.size(); ++I) {
5831 if (I)
5832 UB += ',';
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +00005833
5834 // Find ToolChain for this input.
5835 const ToolChain *CurTC = &getToolChain();
5836 if (const auto *OA = dyn_cast<OffloadAction>(JA.getInputs()[I])) {
5837 CurTC = nullptr;
5838 OA->doOnEachDependence([&](Action *, const ToolChain *TC, const char *) {
5839 assert(CurTC == nullptr && "Expected one dependence!");
5840 CurTC = TC;
5841 });
5842 }
5843 UB += CurTC->getInputFilename(Inputs[I]);
David L. Jonesf561aba2017-03-08 01:02:16 +00005844 }
5845 CmdArgs.push_back(TCArgs.MakeArgString(UB));
5846
5847 // All the inputs are encoded as commands.
5848 C.addCommand(llvm::make_unique<Command>(
5849 JA, *this,
5850 TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
5851 CmdArgs, None));
5852}
5853
5854void OffloadBundler::ConstructJobMultipleOutputs(
5855 Compilation &C, const JobAction &JA, const InputInfoList &Outputs,
5856 const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs,
5857 const char *LinkingOutput) const {
5858 // The version with multiple outputs is expected to refer to a unbundling job.
5859 auto &UA = cast<OffloadUnbundlingJobAction>(JA);
5860
5861 // The unbundling command looks like this:
5862 // clang-offload-bundler -type=bc
5863 // -targets=host-triple,openmp-triple1,openmp-triple2
5864 // -inputs=input_file
5865 // -outputs=unbundle_file_host,unbundle_file_tgt1,unbundle_file_tgt2"
5866 // -unbundle
5867
5868 ArgStringList CmdArgs;
5869
5870 assert(Inputs.size() == 1 && "Expecting to unbundle a single file!");
5871 InputInfo Input = Inputs.front();
5872
5873 // Get the type.
5874 CmdArgs.push_back(TCArgs.MakeArgString(
5875 Twine("-type=") + types::getTypeTempSuffix(Input.getType())));
5876
5877 // Get the targets.
5878 SmallString<128> Triples;
5879 Triples += "-targets=";
5880 auto DepInfo = UA.getDependentActionsInfo();
5881 for (unsigned I = 0; I < DepInfo.size(); ++I) {
5882 if (I)
5883 Triples += ',';
5884
5885 auto &Dep = DepInfo[I];
5886 Triples += Action::GetOffloadKindName(Dep.DependentOffloadKind);
5887 Triples += '-';
5888 Triples += Dep.DependentToolChain->getTriple().normalize();
Yaxun Liu609f7522018-05-11 19:02:18 +00005889 if (Dep.DependentOffloadKind == Action::OFK_HIP &&
5890 !Dep.DependentBoundArch.empty()) {
5891 Triples += '-';
5892 Triples += Dep.DependentBoundArch;
5893 }
David L. Jonesf561aba2017-03-08 01:02:16 +00005894 }
5895
5896 CmdArgs.push_back(TCArgs.MakeArgString(Triples));
5897
5898 // Get bundled file command.
5899 CmdArgs.push_back(
5900 TCArgs.MakeArgString(Twine("-inputs=") + Input.getFilename()));
5901
5902 // Get unbundled files command.
5903 SmallString<128> UB;
5904 UB += "-outputs=";
5905 for (unsigned I = 0; I < Outputs.size(); ++I) {
5906 if (I)
5907 UB += ',';
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +00005908 UB += DepInfo[I].DependentToolChain->getInputFilename(Outputs[I]);
David L. Jonesf561aba2017-03-08 01:02:16 +00005909 }
5910 CmdArgs.push_back(TCArgs.MakeArgString(UB));
5911 CmdArgs.push_back("-unbundle");
5912
5913 // All the inputs are encoded as commands.
5914 C.addCommand(llvm::make_unique<Command>(
5915 JA, *this,
5916 TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
5917 CmdArgs, None));
5918}