Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Driver/ToolChains/Clang.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- Clang.cpp - Clang+LLVM ToolChain Implementations --------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "Clang.h"
10
#include "Arch/AArch64.h"
11
#include "Arch/ARM.h"
12
#include "Arch/Mips.h"
13
#include "Arch/PPC.h"
14
#include "Arch/RISCV.h"
15
#include "Arch/Sparc.h"
16
#include "Arch/SystemZ.h"
17
#include "Arch/X86.h"
18
#include "AMDGPU.h"
19
#include "CommonArgs.h"
20
#include "Hexagon.h"
21
#include "MSP430.h"
22
#include "InputInfo.h"
23
#include "PS4CPU.h"
24
#include "clang/Basic/CharInfo.h"
25
#include "clang/Basic/CodeGenOptions.h"
26
#include "clang/Basic/LangOptions.h"
27
#include "clang/Basic/ObjCRuntime.h"
28
#include "clang/Basic/Version.h"
29
#include "clang/Driver/Distro.h"
30
#include "clang/Driver/DriverDiagnostic.h"
31
#include "clang/Driver/Options.h"
32
#include "clang/Driver/SanitizerArgs.h"
33
#include "clang/Driver/XRayArgs.h"
34
#include "llvm/ADT/StringExtras.h"
35
#include "llvm/Config/llvm-config.h"
36
#include "llvm/Option/ArgList.h"
37
#include "llvm/Support/CodeGen.h"
38
#include "llvm/Support/Compression.h"
39
#include "llvm/Support/FileSystem.h"
40
#include "llvm/Support/Path.h"
41
#include "llvm/Support/Process.h"
42
#include "llvm/Support/TargetParser.h"
43
#include "llvm/Support/YAMLParser.h"
44
45
#ifdef LLVM_ON_UNIX
46
#include <unistd.h> // For getuid().
47
#endif
48
49
using namespace clang::driver;
50
using namespace clang::driver::tools;
51
using namespace clang;
52
using namespace llvm::opt;
53
54
25.1k
static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
55
25.1k
  if (Arg *A =
56
5
          Args.getLastArg(clang::driver::options::OPT_C, options::OPT_CC)) {
57
5
    if (!Args.hasArg(options::OPT_E) && 
!Args.hasArg(options::OPT__SLASH_P)2
&&
58
5
        
!Args.hasArg(options::OPT__SLASH_EP)1
&&
!D.CCCIsCPP()1
) {
59
1
      D.Diag(clang::diag::err_drv_argument_only_allowed_with)
60
1
          << A->getBaseArg().getAsString(Args)
61
1
          << (D.IsCLMode() ? "/E, /P or /EP" : 
"-E"0
);
62
1
    }
63
5
  }
64
25.1k
}
65
66
29.5k
static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
67
29.5k
  // In gcc, only ARM checks this, but it seems reasonable to check universally.
68
29.5k
  if (Args.hasArg(options::OPT_static))
69
1.19k
    if (const Arg *A =
70
1
            Args.getLastArg(options::OPT_dynamic, options::OPT_mdynamic_no_pic))
71
1
      D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args)
72
1
                                                      << "-static";
73
29.5k
}
74
75
// Add backslashes to escape spaces and other backslashes.
76
// This is used for the space-separated argument list specified with
77
// the -dwarf-debug-flags option.
78
static void EscapeSpacesAndBackslashes(const char *Arg,
79
963
                                       SmallVectorImpl<char> &Res) {
80
32.1k
  for (; *Arg; 
++Arg31.1k
) {
81
31.1k
    switch (*Arg) {
82
31.1k
    default:
83
31.1k
      break;
84
31.1k
    case ' ':
85
9
    case '\\':
86
9
      Res.push_back('\\');
87
9
      break;
88
31.1k
    }
89
31.1k
    Res.push_back(*Arg);
90
31.1k
  }
91
963
}
92
93
// Quote target names for inclusion in GNU Make dependency files.
94
// Only the characters '$', '#', ' ', '\t' are quoted.
95
39
static void QuoteTarget(StringRef Target, SmallVectorImpl<char> &Res) {
96
1.08k
  for (unsigned i = 0, e = Target.size(); i != e; 
++i1.04k
) {
97
1.04k
    switch (Target[i]) {
98
1.04k
    case ' ':
99
5
    case '\t':
100
5
      // Escape the preceding backslashes
101
8
      for (int j = i - 1; j >= 0 && 
Target[j] == '\\'7
;
--j3
)
102
3
        Res.push_back('\\');
103
5
104
5
      // Escape the space/tab
105
5
      Res.push_back('\\');
106
5
      break;
107
5
    case '$':
108
2
      Res.push_back('$');
109
2
      break;
110
5
    case '#':
111
1
      Res.push_back('\\');
112
1
      break;
113
1.03k
    default:
114
1.03k
      break;
115
1.04k
    }
116
1.04k
117
1.04k
    Res.push_back(Target[i]);
118
1.04k
  }
119
39
}
120
121
/// Apply \a Work on the current tool chain \a RegularToolChain and any other
122
/// offloading tool chain that is associated with the current action \a JA.
123
static void
124
forAllAssociatedToolChains(Compilation &C, const JobAction &JA,
125
                           const ToolChain &RegularToolChain,
126
36.7k
                           llvm::function_ref<void(const ToolChain &)> Work) {
127
36.7k
  // Apply Work on the current/regular tool chain.
128
36.7k
  Work(RegularToolChain);
129
36.7k
130
36.7k
  // Apply Work on all the offloading tool chains associated with the current
131
36.7k
  // action.
132
36.7k
  if (JA.isHostOffloading(Action::OFK_Cuda))
133
390
    Work(*C.getSingleOffloadToolChain<Action::OFK_Cuda>());
134
36.3k
  else if (JA.isDeviceOffloading(Action::OFK_Cuda))
135
458
    Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
136
35.8k
  else if (JA.isHostOffloading(Action::OFK_HIP))
137
28
    Work(*C.getSingleOffloadToolChain<Action::OFK_HIP>());
138
35.8k
  else if (JA.isDeviceOffloading(Action::OFK_HIP))
139
52
    Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
140
36.7k
141
36.7k
  if (JA.isHostOffloading(Action::OFK_OpenMP)) {
142
110
    auto TCs = C.getOffloadToolChains<Action::OFK_OpenMP>();
143
230
    for (auto II = TCs.first, IE = TCs.second; II != IE; 
++II120
)
144
120
      Work(*II->second);
145
36.6k
  } else if (JA.isDeviceOffloading(Action::OFK_OpenMP))
146
75
    Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
147
36.7k
148
36.7k
  //
149
36.7k
  // TODO: Add support for other offloading programming models here.
150
36.7k
  //
151
36.7k
}
152
153
/// This is a helper function for validating the optional refinement step
154
/// parameter in reciprocal argument strings. Return false if there is an error
155
/// parsing the refinement step. Otherwise, return true and set the Position
156
/// of the refinement step in the input string.
157
static bool getRefinementStep(StringRef In, const Driver &D,
158
32
                              const Arg &A, size_t &Position) {
159
32
  const char RefinementStepToken = ':';
160
32
  Position = In.find(RefinementStepToken);
161
32
  if (Position != StringRef::npos) {
162
12
    StringRef Option = A.getOption().getName();
163
12
    StringRef RefStep = In.substr(Position + 1);
164
12
    // Allow exactly one numeric character for the additional refinement
165
12
    // step parameter. This is reasonable for all currently-supported
166
12
    // operations and architectures because we would expect that a larger value
167
12
    // of refinement steps would cause the estimate "optimization" to
168
12
    // under-perform the native operation. Also, if the estimate does not
169
12
    // converge quickly, it probably will not ever converge, so further
170
12
    // refinement steps will not produce a better answer.
171
12
    if (RefStep.size() != 1) {
172
2
      D.Diag(diag::err_drv_invalid_value) << Option << RefStep;
173
2
      return false;
174
2
    }
175
10
    char RefStepChar = RefStep[0];
176
10
    if (RefStepChar < '0' || RefStepChar > '9') {
177
0
      D.Diag(diag::err_drv_invalid_value) << Option << RefStep;
178
0
      return false;
179
0
    }
180
30
  }
181
30
  return true;
182
30
}
183
184
/// The -mrecip flag requires processing of many optional parameters.
185
static void ParseMRecip(const Driver &D, const ArgList &Args,
186
29.6k
                        ArgStringList &OutStrings) {
187
29.6k
  StringRef DisabledPrefixIn = "!";
188
29.6k
  StringRef DisabledPrefixOut = "!";
189
29.6k
  StringRef EnabledPrefixOut = "";
190
29.6k
  StringRef Out = "-mrecip=";
191
29.6k
192
29.6k
  Arg *A = Args.getLastArg(options::OPT_mrecip, options::OPT_mrecip_EQ);
193
29.6k
  if (!A)
194
29.5k
    return;
195
18
196
18
  unsigned NumOptions = A->getNumValues();
197
18
  if (NumOptions == 0) {
198
1
    // No option is the same as "all".
199
1
    OutStrings.push_back(Args.MakeArgString(Out + "all"));
200
1
    return;
201
1
  }
202
17
203
17
  // Pass through "all", "none", or "default" with an optional refinement step.
204
17
  if (NumOptions == 1) {
205
12
    StringRef Val = A->getValue(0);
206
12
    size_t RefStepLoc;
207
12
    if (!getRefinementStep(Val, D, *A, RefStepLoc))
208
2
      return;
209
10
    StringRef ValBase = Val.slice(0, RefStepLoc);
210
10
    if (ValBase == "all" || 
ValBase == "none"8
||
ValBase == "default"7
) {
211
4
      OutStrings.push_back(Args.MakeArgString(Out + Val));
212
4
      return;
213
4
    }
214
11
  }
215
11
216
11
  // Each reciprocal type may be enabled or disabled individually.
217
11
  // Check each input value for validity, concatenate them all back together,
218
11
  // and pass through.
219
11
220
11
  llvm::StringMap<bool> OptionStrings;
221
11
  OptionStrings.insert(std::make_pair("divd", false));
222
11
  OptionStrings.insert(std::make_pair("divf", false));
223
11
  OptionStrings.insert(std::make_pair("vec-divd", false));
224
11
  OptionStrings.insert(std::make_pair("vec-divf", false));
225
11
  OptionStrings.insert(std::make_pair("sqrtd", false));
226
11
  OptionStrings.insert(std::make_pair("sqrtf", false));
227
11
  OptionStrings.insert(std::make_pair("vec-sqrtd", false));
228
11
  OptionStrings.insert(std::make_pair("vec-sqrtf", false));
229
11
230
28
  for (unsigned i = 0; i != NumOptions; 
++i17
) {
231
20
    StringRef Val = A->getValue(i);
232
20
233
20
    bool IsDisabled = Val.startswith(DisabledPrefixIn);
234
20
    // Ignore the disablement token for string matching.
235
20
    if (IsDisabled)
236
3
      Val = Val.substr(1);
237
20
238
20
    size_t RefStep;
239
20
    if (!getRefinementStep(Val, D, *A, RefStep))
240
0
      return;
241
20
242
20
    StringRef ValBase = Val.slice(0, RefStep);
243
20
    llvm::StringMap<bool>::iterator OptionIter = OptionStrings.find(ValBase);
244
20
    if (OptionIter == OptionStrings.end()) {
245
6
      // Try again specifying float suffix.
246
6
      OptionIter = OptionStrings.find(ValBase.str() + 'f');
247
6
      if (OptionIter == OptionStrings.end()) {
248
1
        // The input name did not match any known option string.
249
1
        D.Diag(diag::err_drv_unknown_argument) << Val;
250
1
        return;
251
1
      }
252
5
      // The option was specified without a float or double suffix.
253
5
      // Make sure that the double entry was not already specified.
254
5
      // The float entry will be checked below.
255
5
      if (OptionStrings[ValBase.str() + 'd']) {
256
0
        D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val;
257
0
        return;
258
0
      }
259
19
    }
260
19
261
19
    if (OptionIter->second == true) {
262
2
      // Duplicate option specified.
263
2
      D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val;
264
2
      return;
265
2
    }
266
17
267
17
    // Mark the matched option as found. Do not allow duplicate specifiers.
268
17
    OptionIter->second = true;
269
17
270
17
    // If the precision was not specified, also mark the double entry as found.
271
17
    if (ValBase.back() != 'f' && 
ValBase.back() != 'd'11
)
272
5
      OptionStrings[ValBase.str() + 'd'] = true;
273
17
274
17
    // Build the output string.
275
17
    StringRef Prefix = IsDisabled ? 
DisabledPrefixOut3
:
EnabledPrefixOut14
;
276
17
    Out = Args.MakeArgString(Out + Prefix + Val);
277
17
    if (i != NumOptions - 1)
278
9
      Out = Args.MakeArgString(Out + ",");
279
17
  }
280
11
281
11
  OutStrings.push_back(Args.MakeArgString(Out));
282
8
}
283
284
/// The -mprefer-vector-width option accepts either a positive integer
285
/// or the string "none".
286
static void ParseMPreferVectorWidth(const Driver &D, const ArgList &Args,
287
29.5k
                                    ArgStringList &CmdArgs) {
288
29.5k
  Arg *A = Args.getLastArg(options::OPT_mprefer_vector_width_EQ);
289
29.5k
  if (!A)
290
29.5k
    return;
291
5
292
5
  StringRef Value = A->getValue();
293
5
  if (Value == "none") {
294
1
    CmdArgs.push_back("-mprefer-vector-width=none");
295
4
  } else {
296
4
    unsigned Width;
297
4
    if (Value.getAsInteger(10, Width)) {
298
3
      D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value;
299
3
      return;
300
3
    }
301
1
    CmdArgs.push_back(Args.MakeArgString("-mprefer-vector-width=" + Value));
302
1
  }
303
5
}
304
305
static void getWebAssemblyTargetFeatures(const ArgList &Args,
306
61
                                         std::vector<StringRef> &Features) {
307
61
  handleTargetFeaturesGroup(Args, Features, options::OPT_m_wasm_Features_Group);
308
61
}
309
310
static void getTargetFeatures(const ToolChain &TC, const llvm::Triple &Triple,
311
                              const ArgList &Args, ArgStringList &CmdArgs,
312
30.6k
                              bool ForAS) {
313
30.6k
  const Driver &D = TC.getDriver();
314
30.6k
  std::vector<StringRef> Features;
315
30.6k
  switch (Triple.getArch()) {
316
30.6k
  default:
317
375
    break;
318
30.6k
  case llvm::Triple::mips:
319
666
  case llvm::Triple::mipsel:
320
666
  case llvm::Triple::mips64:
321
666
  case llvm::Triple::mips64el:
322
666
    mips::getMIPSTargetFeatures(D, Triple, Args, Features);
323
666
    break;
324
666
325
5.11k
  case llvm::Triple::arm:
326
5.11k
  case llvm::Triple::armeb:
327
5.11k
  case llvm::Triple::thumb:
328
5.11k
  case llvm::Triple::thumbeb:
329
5.11k
    arm::getARMTargetFeatures(TC, Triple, Args, CmdArgs, Features, ForAS);
330
5.11k
    break;
331
5.11k
332
5.11k
  case llvm::Triple::ppc:
333
338
  case llvm::Triple::ppc64:
334
338
  case llvm::Triple::ppc64le:
335
338
    ppc::getPPCTargetFeatures(D, Triple, Args, Features);
336
338
    break;
337
338
  case llvm::Triple::riscv32:
338
166
  case llvm::Triple::riscv64:
339
166
    riscv::getRISCVTargetFeatures(D, Args, Features);
340
166
    break;
341
166
  case llvm::Triple::systemz:
342
45
    systemz::getSystemZTargetFeatures(Args, Features);
343
45
    break;
344
8.21k
  case llvm::Triple::aarch64:
345
8.21k
  case llvm::Triple::aarch64_be:
346
8.21k
    aarch64::getAArch64TargetFeatures(D, Triple, Args, Features);
347
8.21k
    break;
348
15.1k
  case llvm::Triple::x86:
349
15.1k
  case llvm::Triple::x86_64:
350
15.1k
    x86::getX86TargetFeatures(D, Triple, Args, Features);
351
15.1k
    break;
352
15.1k
  case llvm::Triple::hexagon:
353
80
    hexagon::getHexagonTargetFeatures(D, Args, Features);
354
80
    break;
355
15.1k
  case llvm::Triple::wasm32:
356
61
  case llvm::Triple::wasm64:
357
61
    getWebAssemblyTargetFeatures(Args, Features);
358
61
    break;
359
156
  case llvm::Triple::sparc:
360
156
  case llvm::Triple::sparcel:
361
156
  case llvm::Triple::sparcv9:
362
156
    sparc::getSparcTargetFeatures(D, Args, Features);
363
156
    break;
364
198
  case llvm::Triple::r600:
365
198
  case llvm::Triple::amdgcn:
366
198
    amdgpu::getAMDGPUTargetFeatures(D, Args, Features);
367
198
    break;
368
198
  case llvm::Triple::msp430:
369
35
    msp430::getMSP430TargetFeatures(D, Args, Features);
370
30.6k
  }
371
30.6k
372
30.6k
  // Find the last of each feature.
373
30.6k
  llvm::StringMap<unsigned> LastOpt;
374
197k
  for (unsigned I = 0, N = Features.size(); I < N; 
++I167k
) {
375
167k
    StringRef Name = Features[I];
376
167k
    assert(Name[0] == '-' || Name[0] == '+');
377
167k
    LastOpt[Name.drop_front(1)] = I;
378
167k
  }
379
30.6k
380
197k
  for (unsigned I = 0, N = Features.size(); I < N; 
++I167k
) {
381
167k
    // If this feature was overridden, ignore it.
382
167k
    StringRef Name = Features[I];
383
167k
    llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name.drop_front(1));
384
167k
    assert(LastI != LastOpt.end());
385
167k
    unsigned Last = LastI->second;
386
167k
    if (Last != I)
387
47.2k
      continue;
388
119k
389
119k
    CmdArgs.push_back("-target-feature");
390
119k
    CmdArgs.push_back(Name.data());
391
119k
  }
392
30.6k
}
393
394
static bool
395
shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
396
709
                                          const llvm::Triple &Triple) {
397
709
  // We use the zero-cost exception tables for Objective-C if the non-fragile
398
709
  // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
399
709
  // later.
400
709
  if (runtime.isNonFragile())
401
690
    return true;
402
19
403
19
  if (!Triple.isMacOSX())
404
3
    return false;
405
16
406
16
  return (!Triple.isMacOSXVersionLT(10, 5) &&
407
16
          
(13
Triple.getArch() == llvm::Triple::x86_6413
||
408
13
           
Triple.getArch() == llvm::Triple::arm9
));
409
16
}
410
411
/// Adds exception related arguments to the driver command arguments. There's a
412
/// master flag, -fexceptions and also language specific flags to enable/disable
413
/// C++ and Objective-C exceptions. This makes it possible to for example
414
/// disable C++ exceptions but enable Objective-C exceptions.
415
static void addExceptionArgs(const ArgList &Args, types::ID InputType,
416
                             const ToolChain &TC, bool KernelOrKext,
417
                             const ObjCRuntime &objcRuntime,
418
29.0k
                             ArgStringList &CmdArgs) {
419
29.0k
  const llvm::Triple &Triple = TC.getTriple();
420
29.0k
421
29.0k
  if (KernelOrKext) {
422
1.04k
    // -mkernel and -fapple-kext imply no exceptions, so claim exception related
423
1.04k
    // arguments now to avoid warnings about unused arguments.
424
1.04k
    Args.ClaimAllArgs(options::OPT_fexceptions);
425
1.04k
    Args.ClaimAllArgs(options::OPT_fno_exceptions);
426
1.04k
    Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
427
1.04k
    Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
428
1.04k
    Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
429
1.04k
    Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
430
1.04k
    return;
431
1.04k
  }
432
28.0k
433
28.0k
  // See if the user explicitly enabled exceptions.
434
28.0k
  bool EH = Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
435
28.0k
                         false);
436
28.0k
437
28.0k
  // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
438
28.0k
  // is not necessarily sensible, but follows GCC.
439
28.0k
  if (types::isObjC(InputType) &&
440
28.0k
      Args.hasFlag(options::OPT_fobjc_exceptions,
441
709
                   options::OPT_fno_objc_exceptions, true)) {
442
709
    CmdArgs.push_back("-fobjc-exceptions");
443
709
444
709
    EH |= shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
445
709
  }
446
28.0k
447
28.0k
  if (types::isCXX(InputType)) {
448
11.4k
    // Disable C++ EH by default on XCore and PS4.
449
11.4k
    bool CXXExceptionsEnabled =
450
11.4k
        Triple.getArch() != llvm::Triple::xcore && 
!Triple.isPS4CPU()11.4k
;
451
11.4k
    Arg *ExceptionArg = Args.getLastArg(
452
11.4k
        options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
453
11.4k
        options::OPT_fexceptions, options::OPT_fno_exceptions);
454
11.4k
    if (ExceptionArg)
455
3.83k
      CXXExceptionsEnabled =
456
3.83k
          ExceptionArg->getOption().matches(options::OPT_fcxx_exceptions) ||
457
3.83k
          
ExceptionArg->getOption().matches(options::OPT_fexceptions)3.82k
;
458
11.4k
459
11.4k
    if (CXXExceptionsEnabled) {
460
9.22k
      CmdArgs.push_back("-fcxx-exceptions");
461
9.22k
462
9.22k
      EH = true;
463
9.22k
    }
464
11.4k
  }
465
28.0k
466
28.0k
  if (EH)
467
9.88k
    CmdArgs.push_back("-fexceptions");
468
28.0k
}
469
470
29.5k
static bool ShouldDisableAutolink(const ArgList &Args, const ToolChain &TC) {
471
29.5k
  bool Default = true;
472
29.5k
  if (TC.getTriple().isOSDarwin()) {
473
20.0k
    // The native darwin assembler doesn't support the linker_option directives,
474
20.0k
    // so we disable them if we think the .s file will be passed to it.
475
20.0k
    Default = TC.useIntegratedAs();
476
20.0k
  }
477
29.5k
  return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
478
29.5k
                       Default);
479
29.5k
}
480
481
static bool ShouldDisableDwarfDirectory(const ArgList &Args,
482
29.5k
                                        const ToolChain &TC) {
483
29.5k
  bool UseDwarfDirectory =
484
29.5k
      Args.hasFlag(options::OPT_fdwarf_directory_asm,
485
29.5k
                   options::OPT_fno_dwarf_directory_asm, TC.useIntegratedAs());
486
29.5k
  return !UseDwarfDirectory;
487
29.5k
}
488
489
// Convert an arg of the form "-gN" or "-ggdbN" or one of their aliases
490
// to the corresponding DebugInfoKind.
491
2.32k
static codegenoptions::DebugInfoKind DebugLevelToInfoKind(const Arg &A) {
492
2.32k
  assert(A.getOption().matches(options::OPT_gN_Group) &&
493
2.32k
         "Not a -g option that specifies a debug-info level");
494
2.32k
  if (A.getOption().matches(options::OPT_g0) ||
495
2.32k
      
A.getOption().matches(options::OPT_ggdb0)2.30k
)
496
23
    return codegenoptions::NoDebugInfo;
497
2.30k
  if (A.getOption().matches(options::OPT_gline_tables_only) ||
498
2.30k
      
A.getOption().matches(options::OPT_ggdb1)72
)
499
2.24k
    return codegenoptions::DebugLineTablesOnly;
500
59
  if (A.getOption().matches(options::OPT_gline_directives_only))
501
10
    return codegenoptions::DebugDirectivesOnly;
502
49
  return codegenoptions::LimitedDebugInfo;
503
49
}
504
505
29.4k
static bool mustUseNonLeafFramePointerForTarget(const llvm::Triple &Triple) {
506
29.4k
  switch (Triple.getArch()){
507
29.4k
  default:
508
25.2k
    return false;
509
29.4k
  case llvm::Triple::arm:
510
4.26k
  case llvm::Triple::thumb:
511
4.26k
    // ARM Darwin targets require a frame pointer to be always present to aid
512
4.26k
    // offline debugging via backtraces.
513
4.26k
    return Triple.isOSDarwin();
514
29.4k
  }
515
29.4k
}
516
517
static bool useFramePointerForTargetByDefault(const ArgList &Args,
518
25.0k
                                              const llvm::Triple &Triple) {
519
25.0k
  if (Args.hasArg(options::OPT_pg))
520
43
    return true;
521
24.9k
522
24.9k
  switch (Triple.getArch()) {
523
24.9k
  case llvm::Triple::xcore:
524
101
  case llvm::Triple::wasm32:
525
101
  case llvm::Triple::wasm64:
526
101
  case llvm::Triple::msp430:
527
101
    // XCore never wants frame pointers, regardless of OS.
528
101
    // WebAssembly never wants frame pointers.
529
101
    return false;
530
487
  case llvm::Triple::ppc:
531
487
  case llvm::Triple::ppc64:
532
487
  case llvm::Triple::ppc64le:
533
487
  case llvm::Triple::riscv32:
534
487
  case llvm::Triple::riscv64:
535
487
    return !areOptimizationsEnabled(Args);
536
24.4k
  default:
537
24.4k
    break;
538
24.4k
  }
539
24.4k
540
24.4k
  if (Triple.isOSNetBSD()) {
541
175
    return !areOptimizationsEnabled(Args);
542
175
  }
543
24.2k
544
24.2k
  if (Triple.isOSLinux() || 
Triple.getOS() == llvm::Triple::CloudABI21.7k
||
545
24.2k
      
Triple.isOSHurd()21.6k
) {
546
2.54k
    switch (Triple.getArch()) {
547
2.54k
    // Don't use a frame pointer on linux if optimizing for certain targets.
548
2.54k
    case llvm::Triple::mips64:
549
1.80k
    case llvm::Triple::mips64el:
550
1.80k
    case llvm::Triple::mips:
551
1.80k
    case llvm::Triple::mipsel:
552
1.80k
    case llvm::Triple::systemz:
553
1.80k
    case llvm::Triple::x86:
554
1.80k
    case llvm::Triple::x86_64:
555
1.80k
      return !areOptimizationsEnabled(Args);
556
1.80k
    default:
557
746
      return true;
558
21.6k
    }
559
21.6k
  }
560
21.6k
561
21.6k
  if (Triple.isOSWindows()) {
562
769
    switch (Triple.getArch()) {
563
769
    case llvm::Triple::x86:
564
148
      return !areOptimizationsEnabled(Args);
565
769
    case llvm::Triple::x86_64:
566
575
      return Triple.isOSBinFormatMachO();
567
769
    case llvm::Triple::arm:
568
39
    case llvm::Triple::thumb:
569
39
      // Windows on ARM builds with FPO disabled to aid fast stack walking
570
39
      return true;
571
39
    default:
572
7
      // All other supported Windows ISAs use xdata unwind information, so frame
573
7
      // pointers are not generally useful.
574
7
      return false;
575
20.9k
    }
576
20.9k
  }
577
20.9k
578
20.9k
  return true;
579
20.9k
}
580
581
static CodeGenOptions::FramePointerKind
582
29.5k
getFramePointerKind(const ArgList &Args, const llvm::Triple &Triple) {
583
29.5k
  Arg *A = Args.getLastArg(options::OPT_fomit_frame_pointer,
584
29.5k
                           options::OPT_fno_omit_frame_pointer);
585
29.5k
  bool OmitFP = A && 
A->getOption().matches(options::OPT_fomit_frame_pointer)2.43k
;
586
29.5k
  bool NoOmitFP =
587
29.5k
      A && 
A->getOption().matches(options::OPT_fno_omit_frame_pointer)2.43k
;
588
29.5k
  if (NoOmitFP || 
mustUseNonLeafFramePointerForTarget(Triple)29.4k
||
589
29.5k
      
(27.3k
!OmitFP27.3k
&&
useFramePointerForTargetByDefault(Args, Triple)25.0k
)) {
590
26.4k
    if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
591
26.4k
                     options::OPT_mno_omit_leaf_frame_pointer,
592
26.4k
                     Triple.isPS4CPU()))
593
120
      return CodeGenOptions::FramePointerKind::NonLeaf;
594
26.3k
    return CodeGenOptions::FramePointerKind::All;
595
26.3k
  }
596
3.11k
  return CodeGenOptions::FramePointerKind::None;
597
3.11k
}
598
599
/// Add a CC1 option to specify the debug compilation directory.
600
static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs,
601
30.5k
                               const llvm::vfs::FileSystem &VFS) {
602
30.5k
  if (Arg *A = Args.getLastArg(options::OPT_fdebug_compilation_dir)) {
603
2
    CmdArgs.push_back("-fdebug-compilation-dir");
604
2
    CmdArgs.push_back(A->getValue());
605
30.5k
  } else if (llvm::ErrorOr<std::string> CWD =
606
30.5k
                 VFS.getCurrentWorkingDirectory()) {
607
30.5k
    CmdArgs.push_back("-fdebug-compilation-dir");
608
30.5k
    CmdArgs.push_back(Args.MakeArgString(*CWD));
609
30.5k
  }
610
30.5k
}
611
612
/// Add a CC1 and CC1AS option to specify the debug file path prefix map.
613
30.5k
static void addDebugPrefixMapArg(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs) {
614
30.5k
  for (const Arg *A : Args.filtered(options::OPT_fdebug_prefix_map_EQ)) {
615
6
    StringRef Map = A->getValue();
616
6
    if (Map.find('=') == StringRef::npos)
617
1
      D.Diag(diag::err_drv_invalid_argument_to_fdebug_prefix_map) << Map;
618
5
    else
619
5
      CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map));
620
6
    A->claim();
621
6
  }
622
30.5k
}
623
624
/// Vectorize at all optimization levels greater than 1 except for -Oz.
625
/// For -Oz the loop vectorizer is disabled, while the slp vectorizer is
626
/// enabled.
627
59.1k
static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) {
628
59.1k
  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
629
26.0k
    if (A->getOption().matches(options::OPT_O4) ||
630
26.0k
        
A->getOption().matches(options::OPT_Ofast)26.0k
)
631
56
      return true;
632
25.9k
633
25.9k
    if (A->getOption().matches(options::OPT_O0))
634
270
      return false;
635
25.7k
636
25.7k
    assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
637
25.7k
638
25.7k
    // Vectorize -Os.
639
25.7k
    StringRef S(A->getValue());
640
25.7k
    if (S == "s")
641
50
      return true;
642
25.6k
643
25.6k
    // Don't vectorize -Oz, unless it's the slp vectorizer.
644
25.6k
    if (S == "z")
645
3.10k
      return isSlpVec;
646
22.5k
647
22.5k
    unsigned OptLevel = 0;
648
22.5k
    if (S.getAsInteger(10, OptLevel))
649
6
      return false;
650
22.5k
651
22.5k
    return OptLevel > 1;
652
22.5k
  }
653
33.1k
654
33.1k
  return false;
655
33.1k
}
656
657
/// Add -x lang to \p CmdArgs for \p Input.
658
static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
659
29.6k
                             ArgStringList &CmdArgs) {
660
29.6k
  // When using -verify-pch, we don't want to provide the type
661
29.6k
  // 'precompiled-header' if it was inferred from the file extension
662
29.6k
  if (Args.hasArg(options::OPT_verify_pch) && 
Input.getType() == types::TY_PCH2
)
663
1
    return;
664
29.6k
665
29.6k
  CmdArgs.push_back("-x");
666
29.6k
  if (Args.hasArg(options::OPT_rewrite_objc))
667
10
    CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
668
29.5k
  else {
669
29.5k
    // Map the driver type to the frontend type. This is mostly an identity
670
29.5k
    // mapping, except that the distinction between module interface units
671
29.5k
    // and other source files does not exist at the frontend layer.
672
29.5k
    const char *ClangType;
673
29.5k
    switch (Input.getType()) {
674
29.5k
    case types::TY_CXXModule:
675
6
      ClangType = "c++";
676
6
      break;
677
29.5k
    case types::TY_PP_CXXModule:
678
0
      ClangType = "c++-cpp-output";
679
0
      break;
680
29.5k
    default:
681
29.5k
      ClangType = types::getTypeName(Input.getType());
682
29.5k
      break;
683
29.5k
    }
684
29.5k
    CmdArgs.push_back(ClangType);
685
29.5k
  }
686
29.6k
}
687
688
17
static void appendUserToPath(SmallVectorImpl<char> &Result) {
689
17
#ifdef LLVM_ON_UNIX
690
17
  const char *Username = getenv("LOGNAME");
691
#else
692
  const char *Username = getenv("USERNAME");
693
#endif
694
17
  if (Username) {
695
1
    // Validate that LoginName can be used in a path, and get its length.
696
1
    size_t Len = 0;
697
5
    for (const char *P = Username; *P; 
++P, ++Len4
) {
698
4
      if (!clang::isAlphanumeric(*P) && 
*P != '_'0
) {
699
0
        Username = nullptr;
700
0
        break;
701
0
      }
702
4
    }
703
1
704
1
    if (Username && Len > 0) {
705
1
      Result.append(Username, Username + Len);
706
1
      return;
707
1
    }
708
16
  }
709
16
710
16
// Fallback to user id.
711
16
#ifdef LLVM_ON_UNIX
712
16
  std::string UID = llvm::utostr(getuid());
713
#else
714
  // FIXME: Windows seems to have an 'SID' that might work.
715
  std::string UID = "9999";
716
#endif
717
  Result.append(UID.begin(), UID.end());
718
16
}
719
720
static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
721
                                   const Driver &D, const InputInfo &Output,
722
                                   const ArgList &Args,
723
29.2k
                                   ArgStringList &CmdArgs) {
724
29.2k
725
29.2k
  auto *PGOGenerateArg = Args.getLastArg(options::OPT_fprofile_generate,
726
29.2k
                                         options::OPT_fprofile_generate_EQ,
727
29.2k
                                         options::OPT_fno_profile_generate);
728
29.2k
  if (PGOGenerateArg &&
729
29.2k
      
PGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)21
)
730
2
    PGOGenerateArg = nullptr;
731
29.2k
732
29.2k
  auto *CSPGOGenerateArg = Args.getLastArg(options::OPT_fcs_profile_generate,
733
29.2k
                                           options::OPT_fcs_profile_generate_EQ,
734
29.2k
                                           options::OPT_fno_profile_generate);
735
29.2k
  if (CSPGOGenerateArg &&
736
29.2k
      
CSPGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)2
)
737
2
    CSPGOGenerateArg = nullptr;
738
29.2k
739
29.2k
  auto *ProfileGenerateArg = Args.getLastArg(
740
29.2k
      options::OPT_fprofile_instr_generate,
741
29.2k
      options::OPT_fprofile_instr_generate_EQ,
742
29.2k
      options::OPT_fno_profile_instr_generate);
743
29.2k
  if (ProfileGenerateArg &&
744
29.2k
      ProfileGenerateArg->getOption().matches(
745
41
          options::OPT_fno_profile_instr_generate))
746
2
    ProfileGenerateArg = nullptr;
747
29.2k
748
29.2k
  if (PGOGenerateArg && 
ProfileGenerateArg19
)
749
2
    D.Diag(diag::err_drv_argument_not_allowed_with)
750
2
        << PGOGenerateArg->getSpelling() << ProfileGenerateArg->getSpelling();
751
29.2k
752
29.2k
  auto *ProfileUseArg = getLastProfileUseArg(Args);
753
29.2k
754
29.2k
  if (PGOGenerateArg && 
ProfileUseArg19
)
755
8
    D.Diag(diag::err_drv_argument_not_allowed_with)
756
8
        << ProfileUseArg->getSpelling() << PGOGenerateArg->getSpelling();
757
29.2k
758
29.2k
  if (ProfileGenerateArg && 
ProfileUseArg39
)
759
10
    D.Diag(diag::err_drv_argument_not_allowed_with)
760
10
        << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling();
761
29.2k
762
29.2k
  if (CSPGOGenerateArg && 
PGOGenerateArg0
)
763
0
    D.Diag(diag::err_drv_argument_not_allowed_with)
764
0
        << CSPGOGenerateArg->getSpelling() << PGOGenerateArg->getSpelling();
765
29.2k
766
29.2k
  if (ProfileGenerateArg) {
767
39
    if (ProfileGenerateArg->getOption().matches(
768
39
            options::OPT_fprofile_instr_generate_EQ))
769
7
      CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-instrument-path=") +
770
7
                                           ProfileGenerateArg->getValue()));
771
39
    // The default is to use Clang Instrumentation.
772
39
    CmdArgs.push_back("-fprofile-instrument=clang");
773
39
    if (TC.getTriple().isWindowsMSVCEnvironment()) {
774
7
      // Add dependent lib for clang_rt.profile
775
7
      CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
776
7
                                           TC.getCompilerRT(Args, "profile")));
777
7
    }
778
39
  }
779
29.2k
780
29.2k
  Arg *PGOGenArg = nullptr;
781
29.2k
  if (PGOGenerateArg) {
782
19
    assert(!CSPGOGenerateArg);
783
19
    PGOGenArg = PGOGenerateArg;
784
19
    CmdArgs.push_back("-fprofile-instrument=llvm");
785
19
  }
786
29.2k
  if (CSPGOGenerateArg) {
787
0
    assert(!PGOGenerateArg);
788
0
    PGOGenArg = CSPGOGenerateArg;
789
0
    CmdArgs.push_back("-fprofile-instrument=csllvm");
790
0
  }
791
29.2k
  if (PGOGenArg) {
792
19
    if (TC.getTriple().isWindowsMSVCEnvironment()) {
793
1
      CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
794
1
                                           TC.getCompilerRT(Args, "profile")));
795
1
    }
796
19
    if (PGOGenArg->getOption().matches(
797
19
            PGOGenerateArg ? options::OPT_fprofile_generate_EQ
798
19
                           : 
options::OPT_fcs_profile_generate_EQ0
)) {
799
8
      SmallString<128> Path(PGOGenArg->getValue());
800
8
      llvm::sys::path::append(Path, "default_%m.profraw");
801
8
      CmdArgs.push_back(
802
8
          Args.MakeArgString(Twine("-fprofile-instrument-path=") + Path));
803
8
    }
804
19
  }
805
29.2k
806
29.2k
  if (ProfileUseArg) {
807
28
    if (ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ))
808
9
      CmdArgs.push_back(Args.MakeArgString(
809
9
          Twine("-fprofile-instrument-use-path=") + ProfileUseArg->getValue()));
810
19
    else if ((ProfileUseArg->getOption().matches(
811
19
                  options::OPT_fprofile_use_EQ) ||
812
19
              ProfileUseArg->getOption().matches(
813
19
                  options::OPT_fprofile_instr_use))) {
814
19
      SmallString<128> Path(
815
19
          ProfileUseArg->getNumValues() == 0 ? 
""12
:
ProfileUseArg->getValue()7
);
816
19
      if (Path.empty() || 
llvm::sys::fs::is_directory(Path)7
)
817
14
        llvm::sys::path::append(Path, "default.profdata");
818
19
      CmdArgs.push_back(
819
19
          Args.MakeArgString(Twine("-fprofile-instrument-use-path=") + Path));
820
19
    }
821
28
  }
822
29.2k
823
29.2k
  if (Args.hasArg(options::OPT_ftest_coverage) ||
824
29.2k
      
Args.hasArg(options::OPT_coverage)29.2k
)
825
20
    CmdArgs.push_back("-femit-coverage-notes");
826
29.2k
  if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
827
29.2k
                   false) ||
828
29.2k
      
Args.hasArg(options::OPT_coverage)29.2k
)
829
35
    CmdArgs.push_back("-femit-coverage-data");
830
29.2k
831
29.2k
  if (Args.hasFlag(options::OPT_fcoverage_mapping,
832
29.2k
                   options::OPT_fno_coverage_mapping, false)) {
833
2
    if (!ProfileGenerateArg)
834
1
      D.Diag(clang::diag::err_drv_argument_only_allowed_with)
835
1
          << "-fcoverage-mapping"
836
1
          << "-fprofile-instr-generate";
837
2
838
2
    CmdArgs.push_back("-fcoverage-mapping");
839
2
  }
840
29.2k
841
29.2k
  if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) {
842
5
    auto *Arg = Args.getLastArg(options::OPT_fprofile_exclude_files_EQ);
843
5
    if (!Args.hasArg(options::OPT_coverage))
844
0
      D.Diag(clang::diag::err_drv_argument_only_allowed_with)
845
0
          << "-fprofile-exclude-files="
846
0
          << "--coverage";
847
5
848
5
    StringRef v = Arg->getValue();
849
5
    CmdArgs.push_back(
850
5
        Args.MakeArgString(Twine("-fprofile-exclude-files=" + v)));
851
5
  }
852
29.2k
853
29.2k
  if (Args.hasArg(options::OPT_fprofile_filter_files_EQ)) {
854
4
    auto *Arg = Args.getLastArg(options::OPT_fprofile_filter_files_EQ);
855
4
    if (!Args.hasArg(options::OPT_coverage))
856
0
      D.Diag(clang::diag::err_drv_argument_only_allowed_with)
857
0
          << "-fprofile-filter-files="
858
0
          << "--coverage";
859
4
860
4
    StringRef v = Arg->getValue();
861
4
    CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-filter-files=" + v)));
862
4
  }
863
29.2k
864
29.2k
  if (C.getArgs().hasArg(options::OPT_c) ||
865
29.2k
      
C.getArgs().hasArg(options::OPT_S)13.5k
) {
866
16.6k
    if (Output.isFilename()) {
867
16.6k
      CmdArgs.push_back("-coverage-notes-file");
868
16.6k
      SmallString<128> OutputFilename;
869
16.6k
      if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
870
13.4k
        OutputFilename = FinalOutput->getValue();
871
3.17k
      else
872
3.17k
        OutputFilename = llvm::sys::path::filename(Output.getBaseInput());
873
16.6k
      SmallString<128> CoverageFilename = OutputFilename;
874
16.6k
      if (llvm::sys::path::is_relative(CoverageFilename))
875
16.3k
        (void)D.getVFS().makeAbsolute(CoverageFilename);
876
16.6k
      llvm::sys::path::replace_extension(CoverageFilename, "gcno");
877
16.6k
      CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
878
16.6k
879
16.6k
      // Leave -fprofile-dir= an unused argument unless .gcda emission is
880
16.6k
      // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider
881
16.6k
      // the flag used. There is no -fno-profile-dir, so the user has no
882
16.6k
      // targeted way to suppress the warning.
883
16.6k
      if (Args.hasArg(options::OPT_fprofile_arcs) ||
884
16.6k
          
Args.hasArg(options::OPT_coverage)16.6k
) {
885
28
        CmdArgs.push_back("-coverage-data-file");
886
28
        if (Arg *FProfileDir = Args.getLastArg(options::OPT_fprofile_dir)) {
887
3
          CoverageFilename = FProfileDir->getValue();
888
3
          llvm::sys::path::append(CoverageFilename, OutputFilename);
889
3
        }
890
28
        llvm::sys::path::replace_extension(CoverageFilename, "gcda");
891
28
        CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
892
28
      }
893
16.6k
    }
894
16.6k
  }
895
29.2k
}
896
897
/// Check whether the given input tree contains any compilation actions.
898
15.2k
static bool ContainsCompileAction(const Action *A) {
899
15.2k
  if (isa<CompileJobAction>(A) || 
isa<BackendJobAction>(A)15.1k
)
900
5.33k
    return true;
901
9.86k
902
9.86k
  for (const auto &AI : A->inputs())
903
9.63k
    if (ContainsCompileAction(AI))
904
9.24k
      return true;
905
9.86k
906
9.86k
  
return false626
;
907
9.86k
}
908
909
/// Check if -relax-all should be passed to the internal assembler.
910
/// This is done by default when compiling non-assembler source with -O0.
911
18.2k
static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
912
18.2k
  bool RelaxDefault = true;
913
18.2k
914
18.2k
  if (Arg *A = Args.getLastArg(options::OPT_O_Group))
915
12.7k
    RelaxDefault = A->getOption().matches(options::OPT_O0);
916
18.2k
917
18.2k
  if (RelaxDefault) {
918
5.54k
    RelaxDefault = false;
919
5.57k
    for (const auto &Act : C.getActions()) {
920
5.57k
      if (ContainsCompileAction(Act)) {
921
5.33k
        RelaxDefault = true;
922
5.33k
        break;
923
5.33k
      }
924
5.57k
    }
925
5.54k
  }
926
18.2k
927
18.2k
  return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
928
18.2k
                      RelaxDefault);
929
18.2k
}
930
931
// Extract the integer N from a string spelled "-dwarf-N", returning 0
932
// on mismatch. The StringRef input (rather than an Arg) allows
933
// for use by the "-Xassembler" option parser.
934
78
static unsigned DwarfVersionNum(StringRef ArgValue) {
935
78
  return llvm::StringSwitch<unsigned>(ArgValue)
936
78
      .Case("-gdwarf-2", 2)
937
78
      .Case("-gdwarf-3", 3)
938
78
      .Case("-gdwarf-4", 4)
939
78
      .Case("-gdwarf-5", 5)
940
78
      .Default(0);
941
78
}
942
943
static void RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs,
944
                                    codegenoptions::DebugInfoKind DebugInfoKind,
945
                                    unsigned DwarfVersion,
946
30.6k
                                    llvm::DebuggerKind DebuggerTuning) {
947
30.6k
  switch (DebugInfoKind) {
948
30.6k
  case codegenoptions::DebugDirectivesOnly:
949
16
    CmdArgs.push_back("-debug-info-kind=line-directives-only");
950
16
    break;
951
30.6k
  case codegenoptions::DebugLineTablesOnly:
952
2.24k
    CmdArgs.push_back("-debug-info-kind=line-tables-only");
953
2.24k
    break;
954
30.6k
  case codegenoptions::LimitedDebugInfo:
955
221
    CmdArgs.push_back("-debug-info-kind=limited");
956
221
    break;
957
30.6k
  case codegenoptions::FullDebugInfo:
958
142
    CmdArgs.push_back("-debug-info-kind=standalone");
959
142
    break;
960
30.6k
  default:
961
27.9k
    break;
962
30.6k
  }
963
30.6k
  if (DwarfVersion > 0)
964
3.57k
    CmdArgs.push_back(
965
3.57k
        Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion)));
966
30.6k
  switch (DebuggerTuning) {
967
30.6k
  case llvm::DebuggerKind::GDB:
968
8.79k
    CmdArgs.push_back("-debugger-tuning=gdb");
969
8.79k
    break;
970
30.6k
  case llvm::DebuggerKind::LLDB:
971
20.0k
    CmdArgs.push_back("-debugger-tuning=lldb");
972
20.0k
    break;
973
30.6k
  case llvm::DebuggerKind::SCE:
974
117
    CmdArgs.push_back("-debugger-tuning=sce");
975
117
    break;
976
30.6k
  default:
977
1.65k
    break;
978
30.6k
  }
979
30.6k
}
980
981
static bool checkDebugInfoOption(const Arg *A, const ArgList &Args,
982
2.84k
                                 const Driver &D, const ToolChain &TC) {
983
2.84k
  assert(A && "Expected non-nullptr argument.");
984
2.84k
  if (TC.supportsDebugInfoOption(A))
985
2.81k
    return true;
986
30
  D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target)
987
30
      << A->getAsString(Args) << TC.getTripleString();
988
30
  return false;
989
30
}
990
991
static void RenderDebugInfoCompressionArgs(const ArgList &Args,
992
                                           ArgStringList &CmdArgs,
993
                                           const Driver &D,
994
30.6k
                                           const ToolChain &TC) {
995
30.6k
  const Arg *A = Args.getLastArg(options::OPT_gz, options::OPT_gz_EQ);
996
30.6k
  if (!A)
997
30.5k
    return;
998
19
  if (checkDebugInfoOption(A, Args, D, TC)) {
999
18
    if (A->getOption().getID() == options::OPT_gz) {
1000
6
      if (llvm::zlib::isAvailable())
1001
6
        CmdArgs.push_back("--compress-debug-sections");
1002
0
      else
1003
0
        D.Diag(diag::warn_debug_compression_unavailable);
1004
6
      return;
1005
6
    }
1006
12
1007
12
    StringRef Value = A->getValue();
1008
12
    if (Value == "none") {
1009
3
      CmdArgs.push_back("--compress-debug-sections=none");
1010
9
    } else if (Value == "zlib" || 
Value == "zlib-gnu"6
) {
1011
6
      if (llvm::zlib::isAvailable()) {
1012
6
        CmdArgs.push_back(
1013
6
            Args.MakeArgString("--compress-debug-sections=" + Twine(Value)));
1014
6
      } else {
1015
0
        D.Diag(diag::warn_debug_compression_unavailable);
1016
0
      }
1017
6
    } else {
1018
3
      D.Diag(diag::err_drv_unsupported_option_argument)
1019
3
          << A->getOption().getName() << Value;
1020
3
    }
1021
12
  }
1022
19
}
1023
1024
30.6k
static const char *RelocationModelName(llvm::Reloc::Model Model) {
1025
30.6k
  switch (Model) {
1026
30.6k
  case llvm::Reloc::Static:
1027
8.51k
    return "static";
1028
30.6k
  case llvm::Reloc::PIC_:
1029
22.0k
    return "pic";
1030
30.6k
  case llvm::Reloc::DynamicNoPIC:
1031
8
    return "dynamic-no-pic";
1032
30.6k
  case llvm::Reloc::ROPI:
1033
9
    return "ropi";
1034
30.6k
  case llvm::Reloc::RWPI:
1035
4
    return "rwpi";
1036
30.6k
  case llvm::Reloc::ROPI_RWPI:
1037
4
    return "ropi-rwpi";
1038
0
  }
1039
0
  llvm_unreachable("Unknown Reloc::Model kind");
1040
0
}
1041
1042
void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
1043
                                    const Driver &D, const ArgList &Args,
1044
                                    ArgStringList &CmdArgs,
1045
                                    const InputInfo &Output,
1046
25.1k
                                    const InputInfoList &Inputs) const {
1047
25.1k
  Arg *A;
1048
25.1k
  const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU();
1049
25.1k
1050
25.1k
  CheckPreprocessingOptions(D, Args);
1051
25.1k
1052
25.1k
  Args.AddLastArg(CmdArgs, options::OPT_C);
1053
25.1k
  Args.AddLastArg(CmdArgs, options::OPT_CC);
1054
25.1k
1055
25.1k
  // Handle dependency file generation.
1056
25.1k
  if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
1057
25.1k
      
(A = Args.getLastArg(options::OPT_MD))25.1k
||
1058
25.1k
      
(A = Args.getLastArg(options::OPT_MMD))16.7k
) {
1059
8.43k
    // Determine the output location.
1060
8.43k
    const char *DepFile;
1061
8.43k
    if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
1062
8.41k
      DepFile = MF->getValue();
1063
8.41k
      C.addFailureResultFile(DepFile, &JA);
1064
8.41k
    } else 
if (13
Output.getType() == types::TY_Dependencies13
) {
1065
9
      DepFile = Output.getFilename();
1066
9
    } else 
if (4
A->getOption().matches(options::OPT_M)4
||
1067
4
               A->getOption().matches(options::OPT_MM)) {
1068
0
      DepFile = "-";
1069
4
    } else {
1070
4
      DepFile = getDependencyFileName(Args, Inputs);
1071
4
      C.addFailureResultFile(DepFile, &JA);
1072
4
    }
1073
8.43k
    CmdArgs.push_back("-dependency-file");
1074
8.43k
    CmdArgs.push_back(DepFile);
1075
8.43k
1076
8.43k
    // Add a default target if one wasn't specified.
1077
8.43k
    if (!Args.hasArg(options::OPT_MT) && 
!Args.hasArg(options::OPT_MQ)36
) {
1078
34
      const char *DepTarget;
1079
34
1080
34
      // If user provided -o, that is the dependency target, except
1081
34
      // when we are only generating a dependency file.
1082
34
      Arg *OutputOpt = Args.getLastArg(options::OPT_o);
1083
34
      if (OutputOpt && 
Output.getType() != types::TY_Dependencies7
) {
1084
7
        DepTarget = OutputOpt->getValue();
1085
27
      } else {
1086
27
        // Otherwise derive from the base input.
1087
27
        //
1088
27
        // FIXME: This should use the computed output file location.
1089
27
        SmallString<128> P(Inputs[0].getBaseInput());
1090
27
        llvm::sys::path::replace_extension(P, "o");
1091
27
        DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
1092
27
      }
1093
34
1094
34
      if (!A->getOption().matches(options::OPT_MD) && 
!A->getOption().matches(options::OPT_MMD)17
) {
1095
11
        CmdArgs.push_back("-w");
1096
11
      }
1097
34
      CmdArgs.push_back("-MT");
1098
34
      SmallString<128> Quoted;
1099
34
      QuoteTarget(DepTarget, Quoted);
1100
34
      CmdArgs.push_back(Args.MakeArgString(Quoted));
1101
34
    }
1102
8.43k
1103
8.43k
    if (A->getOption().matches(options::OPT_M) ||
1104
8.43k
        
A->getOption().matches(options::OPT_MD)8.42k
)
1105
8.41k
      CmdArgs.push_back("-sys-header-deps");
1106
8.43k
    if ((isa<PrecompileJobAction>(JA) &&
1107
8.43k
         
!Args.hasArg(options::OPT_fno_module_file_deps)2
) ||
1108
8.43k
        
Args.hasArg(options::OPT_fmodule_file_deps)8.43k
)
1109
2
      CmdArgs.push_back("-module-file-deps");
1110
8.43k
  }
1111
25.1k
1112
25.1k
  if (Args.hasArg(options::OPT_MG)) {
1113
4
    if (!A || A->getOption().matches(options::OPT_MD) ||
1114
4
        A->getOption().matches(options::OPT_MMD))
1115
0
      D.Diag(diag::err_drv_mg_requires_m_or_mm);
1116
4
    CmdArgs.push_back("-MG");
1117
4
  }
1118
25.1k
1119
25.1k
  Args.AddLastArg(CmdArgs, options::OPT_MP);
1120
25.1k
  Args.AddLastArg(CmdArgs, options::OPT_MV);
1121
25.1k
1122
25.1k
  // Convert all -MQ <target> args to -MT <quoted target>
1123
25.1k
  for (const Arg *A : Args.filtered(options::OPT_MT, options::OPT_MQ)) {
1124
8.40k
    A->claim();
1125
8.40k
1126
8.40k
    if (A->getOption().matches(options::OPT_MQ)) {
1127
5
      CmdArgs.push_back("-MT");
1128
5
      SmallString<128> Quoted;
1129
5
      QuoteTarget(A->getValue(), Quoted);
1130
5
      CmdArgs.push_back(Args.MakeArgString(Quoted));
1131
5
1132
5
      // -MT flag - no change
1133
8.39k
    } else {
1134
8.39k
      A->render(Args, CmdArgs);
1135
8.39k
    }
1136
8.40k
  }
1137
25.1k
1138
25.1k
  // Add offload include arguments specific for CUDA.  This must happen before
1139
25.1k
  // we -I or -include anything else, because we must pick up the CUDA headers
1140
25.1k
  // from the particular CUDA installation, rather than from e.g.
1141
25.1k
  // /usr/local/include.
1142
25.1k
  if (JA.isOffloading(Action::OFK_Cuda))
1143
424
    getToolChain().AddCudaIncludeArgs(Args, CmdArgs);
1144
25.1k
1145
25.1k
  // If we are offloading to a target via OpenMP we need to include the
1146
25.1k
  // openmp_wrappers folder which contains alternative system headers.
1147
25.1k
  if (JA.isDeviceOffloading(Action::OFK_OpenMP) &&
1148
25.1k
      
getToolChain().getTriple().isNVPTX()75
){
1149
53
    if (!Args.hasArg(options::OPT_nobuiltininc)) {
1150
53
      // Add openmp_wrappers/* to our system include path.  This lets us wrap
1151
53
      // standard library headers.
1152
53
      SmallString<128> P(D.ResourceDir);
1153
53
      llvm::sys::path::append(P, "include");
1154
53
      llvm::sys::path::append(P, "openmp_wrappers");
1155
53
      CmdArgs.push_back("-internal-isystem");
1156
53
      CmdArgs.push_back(Args.MakeArgString(P));
1157
53
    }
1158
53
1159
53
    CmdArgs.push_back("-include");
1160
53
    CmdArgs.push_back("__clang_openmp_math_declares.h");
1161
53
  }
1162
25.1k
1163
25.1k
  // Add -i* options, and automatically translate to
1164
25.1k
  // -include-pch/-include-pth for transparent PCH support. It's
1165
25.1k
  // wonky, but we include looking for .gch so we can support seamless
1166
25.1k
  // replacement into a build system already set up to be generating
1167
25.1k
  // .gch files.
1168
25.1k
1169
25.1k
  if (getToolChain().getDriver().IsCLMode()) {
1170
513
    const Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc);
1171
513
    const Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu);
1172
513
    if (YcArg && 
JA.getKind() >= Action::PrecompileJobClass34
&&
1173
513
        
JA.getKind() <= Action::AssembleJobClass34
) {
1174
34
      CmdArgs.push_back(Args.MakeArgString("-building-pch-with-obj"));
1175
34
    }
1176
513
    if (YcArg || 
YuArg479
) {
1177
48
      StringRef ThroughHeader = YcArg ? 
YcArg->getValue()34
:
YuArg->getValue()14
;
1178
48
      if (!isa<PrecompileJobAction>(JA)) {
1179
31
        CmdArgs.push_back("-include-pch");
1180
31
        CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath(
1181
31
            C, !ThroughHeader.empty()
1182
31
                   ? 
ThroughHeader28
1183
31
                   : 
llvm::sys::path::filename(Inputs[0].getBaseInput())3
)));
1184
31
      }
1185
48
1186
48
      if (ThroughHeader.empty()) {
1187
5
        CmdArgs.push_back(Args.MakeArgString(
1188
5
            Twine("-pch-through-hdrstop-") + (YcArg ? 
"create"4
:
"use"1
)));
1189
43
      } else {
1190
43
        CmdArgs.push_back(
1191
43
            Args.MakeArgString(Twine("-pch-through-header=") + ThroughHeader));
1192
43
      }
1193
48
    }
1194
513
  }
1195
25.1k
1196
25.1k
  bool RenderedImplicitInclude = false;
1197
25.1k
  for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) {
1198
15.7k
    if (A->getOption().matches(options::OPT_include)) {
1199
150
      // Handling of gcc-style gch precompiled headers.
1200
150
      bool IsFirstImplicitInclude = !RenderedImplicitInclude;
1201
150
      RenderedImplicitInclude = true;
1202
150
1203
150
      bool FoundPCH = false;
1204
150
      SmallString<128> P(A->getValue());
1205
150
      // We want the files to have a name like foo.h.pch. Add a dummy extension
1206
150
      // so that replace_extension does the right thing.
1207
150
      P += ".dummy";
1208
150
      llvm::sys::path::replace_extension(P, "pch");
1209
150
      if (llvm::sys::fs::exists(P))
1210
67
        FoundPCH = true;
1211
150
1212
150
      if (!FoundPCH) {
1213
83
        llvm::sys::path::replace_extension(P, "gch");
1214
83
        if (llvm::sys::fs::exists(P)) {
1215
8
          FoundPCH = true;
1216
8
        }
1217
83
      }
1218
150
1219
150
      if (FoundPCH) {
1220
75
        if (IsFirstImplicitInclude) {
1221
75
          A->claim();
1222
75
          CmdArgs.push_back("-include-pch");
1223
75
          CmdArgs.push_back(Args.MakeArgString(P));
1224
75
          continue;
1225
75
        } else {
1226
0
          // Ignore the PCH if not first on command line and emit warning.
1227
0
          D.Diag(diag::warn_drv_pch_not_first_include) << P
1228
0
                                                       << A->getAsString(Args);
1229
0
        }
1230
75
      }
1231
15.5k
    } else if (A->getOption().matches(options::OPT_isystem_after)) {
1232
4
      // Handling of paths which must come late.  These entries are handled by
1233
4
      // the toolchain itself after the resource dir is inserted in the right
1234
4
      // search order.
1235
4
      // Do not claim the argument so that the use of the argument does not
1236
4
      // silently go unnoticed on toolchains which do not honour the option.
1237
4
      continue;
1238
4
    }
1239
15.6k
1240
15.6k
    // Not translated, render as usual.
1241
15.6k
    A->claim();
1242
15.6k
    A->render(Args, CmdArgs);
1243
15.6k
  }
1244
25.1k
1245
25.1k
  Args.AddAllArgs(CmdArgs,
1246
25.1k
                  {options::OPT_D, options::OPT_U, options::OPT_I_Group,
1247
25.1k
                   options::OPT_F, options::OPT_index_header_map});
1248
25.1k
1249
25.1k
  // Add -Wp, and -Xpreprocessor if using the preprocessor.
1250
25.1k
1251
25.1k
  // FIXME: There is a very unfortunate problem here, some troubled
1252
25.1k
  // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
1253
25.1k
  // really support that we would have to parse and then translate
1254
25.1k
  // those options. :(
1255
25.1k
  Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
1256
25.1k
                       options::OPT_Xpreprocessor);
1257
25.1k
1258
25.1k
  // -I- is a deprecated GCC feature, reject it.
1259
25.1k
  if (Arg *A = Args.getLastArg(options::OPT_I_))
1260
0
    D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
1261
25.1k
1262
25.1k
  // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
1263
25.1k
  // -isysroot to the CC1 invocation.
1264
25.1k
  StringRef sysroot = C.getSysRoot();
1265
25.1k
  if (sysroot != "") {
1266
840
    if (!Args.hasArg(options::OPT_isysroot)) {
1267
587
      CmdArgs.push_back("-isysroot");
1268
587
      CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
1269
587
    }
1270
840
  }
1271
25.1k
1272
25.1k
  // Parse additional include paths from environment variables.
1273
25.1k
  // FIXME: We should probably sink the logic for handling these from the
1274
25.1k
  // frontend into the driver. It will allow deleting 4 otherwise unused flags.
1275
25.1k
  // CPATH - included following the user specified includes (but prior to
1276
25.1k
  // builtin and standard includes).
1277
25.1k
  addDirectoryList(Args, CmdArgs, "-I", "CPATH");
1278
25.1k
  // C_INCLUDE_PATH - system includes enabled when compiling C.
1279
25.1k
  addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
1280
25.1k
  // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
1281
25.1k
  addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
1282
25.1k
  // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
1283
25.1k
  addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
1284
25.1k
  // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
1285
25.1k
  addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
1286
25.1k
1287
25.1k
  // While adding the include arguments, we also attempt to retrieve the
1288
25.1k
  // arguments of related offloading toolchains or arguments that are specific
1289
25.1k
  // of an offloading programming model.
1290
25.1k
1291
25.1k
  // Add C++ include arguments, if needed.
1292
25.1k
  if (types::isCXX(Inputs[0].getType()))
1293
11.5k
    forAllAssociatedToolChains(C, JA, getToolChain(),
1294
11.9k
                               [&Args, &CmdArgs](const ToolChain &TC) {
1295
11.9k
                                 TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
1296
11.9k
                               });
1297
25.1k
1298
25.1k
  // Add system include arguments for all targets but IAMCU.
1299
25.1k
  if (!IsIAMCU)
1300
25.1k
    forAllAssociatedToolChains(C, JA, getToolChain(),
1301
25.8k
                               [&Args, &CmdArgs](const ToolChain &TC) {
1302
25.8k
                                 TC.AddClangSystemIncludeArgs(Args, CmdArgs);
1303
25.8k
                               });
1304
9
  else {
1305
9
    // For IAMCU add special include arguments.
1306
9
    getToolChain().AddIAMCUIncludeArgs(Args, CmdArgs);
1307
9
  }
1308
25.1k
}
1309
1310
// FIXME: Move to target hook.
1311
29.5k
static bool isSignedCharDefault(const llvm::Triple &Triple) {
1312
29.5k
  switch (Triple.getArch()) {
1313
29.5k
  default:
1314
16.3k
    return true;
1315
29.5k
1316
29.5k
  case llvm::Triple::aarch64:
1317
12.6k
  case llvm::Triple::aarch64_be:
1318
12.6k
  case llvm::Triple::arm:
1319
12.6k
  case llvm::Triple::armeb:
1320
12.6k
  case llvm::Triple::thumb:
1321
12.6k
  case llvm::Triple::thumbeb:
1322
12.6k
    if (Triple.isOSDarwin() || 
Triple.isOSWindows()3.13k
)
1323
9.54k
      return true;
1324
3.06k
    return false;
1325
3.06k
1326
3.06k
  case llvm::Triple::ppc:
1327
183
  case llvm::Triple::ppc64:
1328
183
    if (Triple.isOSDarwin())
1329
8
      return true;
1330
175
    return false;
1331
175
1332
451
  case llvm::Triple::hexagon:
1333
451
  case llvm::Triple::ppc64le:
1334
451
  case llvm::Triple::riscv32:
1335
451
  case llvm::Triple::riscv64:
1336
451
  case llvm::Triple::systemz:
1337
451
  case llvm::Triple::xcore:
1338
451
    return false;
1339
29.5k
  }
1340
29.5k
}
1341
1342
28.5k
static bool isNoCommonDefault(const llvm::Triple &Triple) {
1343
28.5k
  switch (Triple.getArch()) {
1344
28.5k
  default:
1345
28.4k
    if (Triple.isOSFuchsia())
1346
33
      return true;
1347
28.4k
    return false;
1348
28.4k
1349
28.4k
  case llvm::Triple::xcore:
1350
66
  case llvm::Triple::wasm32:
1351
66
  case llvm::Triple::wasm64:
1352
66
    return true;
1353
28.5k
  }
1354
28.5k
}
1355
1356
namespace {
1357
void RenderARMABI(const llvm::Triple &Triple, const ArgList &Args,
1358
4.41k
                  ArgStringList &CmdArgs) {
1359
4.41k
  // Select the ABI to use.
1360
4.41k
  // FIXME: Support -meabi.
1361
4.41k
  // FIXME: Parts of this are duplicated in the backend, unify this somehow.
1362
4.41k
  const char *ABIName = nullptr;
1363
4.41k
  if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
1364
3
    ABIName = A->getValue();
1365
4.41k
  } else {
1366
4.41k
    std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false);
1367
4.41k
    ABIName = llvm::ARM::computeDefaultTargetABI(Triple, CPU).data();
1368
4.41k
  }
1369
4.41k
1370
4.41k
  CmdArgs.push_back("-target-abi");
1371
4.41k
  CmdArgs.push_back(ABIName);
1372
4.41k
}
1373
}
1374
1375
void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args,
1376
4.41k
                             ArgStringList &CmdArgs, bool KernelOrKext) const {
1377
4.41k
  RenderARMABI(Triple, Args, CmdArgs);
1378
4.41k
1379
4.41k
  // Determine floating point ABI from the options & target defaults.
1380
4.41k
  arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args);
1381
4.41k
  if (ABI == arm::FloatABI::Soft) {
1382
1.52k
    // Floating point operations and argument passing are soft.
1383
1.52k
    // FIXME: This changes CPP defines, we need -target-soft-float.
1384
1.52k
    CmdArgs.push_back("-msoft-float");
1385
1.52k
    CmdArgs.push_back("-mfloat-abi");
1386
1.52k
    CmdArgs.push_back("soft");
1387
2.89k
  } else if (ABI == arm::FloatABI::SoftFP) {
1388
1.62k
    // Floating point operations are hard, but argument passing is soft.
1389
1.62k
    CmdArgs.push_back("-mfloat-abi");
1390
1.62k
    CmdArgs.push_back("soft");
1391
1.62k
  } else {
1392
1.26k
    // Floating point operations and argument passing are hard.
1393
1.26k
    assert(ABI == arm::FloatABI::Hard && "Invalid float abi!");
1394
1.26k
    CmdArgs.push_back("-mfloat-abi");
1395
1.26k
    CmdArgs.push_back("hard");
1396
1.26k
  }
1397
4.41k
1398
4.41k
  // Forward the -mglobal-merge option for explicit control over the pass.
1399
4.41k
  if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
1400
2
                               options::OPT_mno_global_merge)) {
1401
2
    CmdArgs.push_back("-mllvm");
1402
2
    if (A->getOption().matches(options::OPT_mno_global_merge))
1403
1
      CmdArgs.push_back("-arm-global-merge=false");
1404
1
    else
1405
1
      CmdArgs.push_back("-arm-global-merge=true");
1406
2
  }
1407
4.41k
1408
4.41k
  if (!Args.hasFlag(options::OPT_mimplicit_float,
1409
4.41k
                    options::OPT_mno_implicit_float, true))
1410
1
    CmdArgs.push_back("-no-implicit-float");
1411
4.41k
1412
4.41k
  if (Args.getLastArg(options::OPT_mcmse))
1413
5
    CmdArgs.push_back("-mcmse");
1414
4.41k
}
1415
1416
void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple,
1417
                                const ArgList &Args, bool KernelOrKext,
1418
29.5k
                                ArgStringList &CmdArgs) const {
1419
29.5k
  const ToolChain &TC = getToolChain();
1420
29.5k
1421
29.5k
  // Add the target features
1422
29.5k
  getTargetFeatures(TC, EffectiveTriple, Args, CmdArgs, false);
1423
29.5k
1424
29.5k
  // Add target specific flags.
1425
29.5k
  switch (TC.getArch()) {
1426
29.5k
  default:
1427
597
    break;
1428
29.5k
1429
29.5k
  case llvm::Triple::arm:
1430
4.41k
  case llvm::Triple::armeb:
1431
4.41k
  case llvm::Triple::thumb:
1432
4.41k
  case llvm::Triple::thumbeb:
1433
4.41k
    // Use the effective triple, which takes into account the deployment target.
1434
4.41k
    AddARMTargetArgs(EffectiveTriple, Args, CmdArgs, KernelOrKext);
1435
4.41k
    CmdArgs.push_back("-fallow-half-arguments-and-returns");
1436
4.41k
    break;
1437
4.41k
1438
8.19k
  case llvm::Triple::aarch64:
1439
8.19k
  case llvm::Triple::aarch64_be:
1440
8.19k
    AddAArch64TargetArgs(Args, CmdArgs);
1441
8.19k
    CmdArgs.push_back("-fallow-half-arguments-and-returns");
1442
8.19k
    break;
1443
8.19k
1444
8.19k
  case llvm::Triple::mips:
1445
580
  case llvm::Triple::mipsel:
1446
580
  case llvm::Triple::mips64:
1447
580
  case llvm::Triple::mips64el:
1448
580
    AddMIPSTargetArgs(Args, CmdArgs);
1449
580
    break;
1450
580
1451
580
  case llvm::Triple::ppc:
1452
325
  case llvm::Triple::ppc64:
1453
325
  case llvm::Triple::ppc64le:
1454
325
    AddPPCTargetArgs(Args, CmdArgs);
1455
325
    break;
1456
325
1457
325
  case llvm::Triple::riscv32:
1458
162
  case llvm::Triple::riscv64:
1459
162
    AddRISCVTargetArgs(Args, CmdArgs);
1460
162
    break;
1461
162
1462
162
  case llvm::Triple::sparc:
1463
156
  case llvm::Triple::sparcel:
1464
156
  case llvm::Triple::sparcv9:
1465
156
    AddSparcTargetArgs(Args, CmdArgs);
1466
156
    break;
1467
156
1468
156
  case llvm::Triple::systemz:
1469
43
    AddSystemZTargetArgs(Args, CmdArgs);
1470
43
    break;
1471
156
1472
14.9k
  case llvm::Triple::x86:
1473
14.9k
  case llvm::Triple::x86_64:
1474
14.9k
    AddX86TargetArgs(Args, CmdArgs);
1475
14.9k
    break;
1476
14.9k
1477
14.9k
  case llvm::Triple::lanai:
1478
2
    AddLanaiTargetArgs(Args, CmdArgs);
1479
2
    break;
1480
14.9k
1481
14.9k
  case llvm::Triple::hexagon:
1482
80
    AddHexagonTargetArgs(Args, CmdArgs);
1483
80
    break;
1484
14.9k
1485
14.9k
  case llvm::Triple::wasm32:
1486
61
  case llvm::Triple::wasm64:
1487
61
    AddWebAssemblyTargetArgs(Args, CmdArgs);
1488
61
    break;
1489
29.5k
  }
1490
29.5k
}
1491
1492
// Parse -mbranch-protection=<protection>[+<protection>]* where
1493
//   <protection> ::= standard | none | [bti,pac-ret[+b-key,+leaf]*]
1494
// Returns a triple of (return address signing Scope, signing key, require
1495
// landing pads)
1496
static std::tuple<StringRef, StringRef, bool>
1497
ParseAArch64BranchProtection(const Driver &D, const ArgList &Args,
1498
14
                             const Arg *A) {
1499
14
  StringRef Scope = "none";
1500
14
  StringRef Key = "a_key";
1501
14
  bool IndirectBranches = false;
1502
14
1503
14
  StringRef Value = A->getValue();
1504
14
  // This maps onto -mbranch-protection=<scope>+<key>
1505
14
1506
14
  if (Value.equals("standard")) {
1507
2
    Scope = "non-leaf";
1508
2
    Key = "a_key";
1509
2
    IndirectBranches = true;
1510
2
1511
12
  } else if (!Value.equals("none")) {
1512
10
    SmallVector<StringRef, 4> BranchProtection;
1513
10
    StringRef(A->getValue()).split(BranchProtection, '+');
1514
10
1515
10
    auto Protection = BranchProtection.begin();
1516
21
    while (Protection != BranchProtection.end()) {
1517
11
      if (Protection->equals("bti"))
1518
3
        IndirectBranches = true;
1519
8
      else if (Protection->equals("pac-ret")) {
1520
7
        Scope = "non-leaf";
1521
16
        while (++Protection != BranchProtection.end()) {
1522
10
          // Inner loop as "leaf" and "b-key" options must only appear attached
1523
10
          // to pac-ret.
1524
10
          if (Protection->equals("leaf"))
1525
4
            Scope = "all";
1526
6
          else if (Protection->equals("b-key"))
1527
5
            Key = "b_key";
1528
1
          else
1529
1
            break;
1530
10
        }
1531
7
        Protection--;
1532
7
      } else
1533
1
        D.Diag(diag::err_invalid_branch_protection)
1534
1
            << *Protection << A->getAsString(Args);
1535
11
      Protection++;
1536
11
    }
1537
10
  }
1538
14
1539
14
  return std::make_tuple(Scope, Key, IndirectBranches);
1540
14
}
1541
1542
namespace {
1543
void RenderAArch64ABI(const llvm::Triple &Triple, const ArgList &Args,
1544
8.19k
                      ArgStringList &CmdArgs) {
1545
8.19k
  const char *ABIName = nullptr;
1546
8.19k
  if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
1547
0
    ABIName = A->getValue();
1548
8.19k
  else if (Triple.isOSDarwin())
1549
7.35k
    ABIName = "darwinpcs";
1550
846
  else
1551
846
    ABIName = "aapcs";
1552
8.19k
1553
8.19k
  CmdArgs.push_back("-target-abi");
1554
8.19k
  CmdArgs.push_back(ABIName);
1555
8.19k
}
1556
}
1557
1558
void Clang::AddAArch64TargetArgs(const ArgList &Args,
1559
8.19k
                                 ArgStringList &CmdArgs) const {
1560
8.19k
  const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
1561
8.19k
1562
8.19k
  if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
1563
8.19k
      Args.hasArg(options::OPT_mkernel) ||
1564
8.19k
      
Args.hasArg(options::OPT_fapple_kext)8.04k
)
1565
157
    CmdArgs.push_back("-disable-red-zone");
1566
8.19k
1567
8.19k
  if (!Args.hasFlag(options::OPT_mimplicit_float,
1568
8.19k
                    options::OPT_mno_implicit_float, true))
1569
0
    CmdArgs.push_back("-no-implicit-float");
1570
8.19k
1571
8.19k
  RenderAArch64ABI(Triple, Args, CmdArgs);
1572
8.19k
1573
8.19k
  if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a53_835769,
1574
6
                               options::OPT_mno_fix_cortex_a53_835769)) {
1575
6
    CmdArgs.push_back("-mllvm");
1576
6
    if (A->getOption().matches(options::OPT_mfix_cortex_a53_835769))
1577
3
      CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
1578
3
    else
1579
3
      CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=0");
1580
8.19k
  } else if (Triple.isAndroid()) {
1581
49
    // Enabled A53 errata (835769) workaround by default on android
1582
49
    CmdArgs.push_back("-mllvm");
1583
49
    CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
1584
49
  }
1585
8.19k
1586
8.19k
  // Forward the -mglobal-merge option for explicit control over the pass.
1587
8.19k
  if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
1588
2
                               options::OPT_mno_global_merge)) {
1589
2
    CmdArgs.push_back("-mllvm");
1590
2
    if (A->getOption().matches(options::OPT_mno_global_merge))
1591
1
      CmdArgs.push_back("-aarch64-enable-global-merge=false");
1592
1
    else
1593
1
      CmdArgs.push_back("-aarch64-enable-global-merge=true");
1594
2
  }
1595
8.19k
1596
8.19k
  // Enable/disable return address signing and indirect branch targets.
1597
8.19k
  if (Arg *A = Args.getLastArg(options::OPT_msign_return_address_EQ,
1598
30
                               options::OPT_mbranch_protection_EQ)) {
1599
30
1600
30
    const Driver &D = getToolChain().getDriver();
1601
30
1602
30
    StringRef Scope, Key;
1603
30
    bool IndirectBranches;
1604
30
1605
30
    if (A->getOption().matches(options::OPT_msign_return_address_EQ)) {
1606
16
      Scope = A->getValue();
1607
16
      if (!Scope.equals("none") && 
!Scope.equals("non-leaf")12
&&
1608
16
          
!Scope.equals("all")8
)
1609
1
        D.Diag(diag::err_invalid_branch_protection)
1610
1
            << Scope << A->getAsString(Args);
1611
16
      Key = "a_key";
1612
16
      IndirectBranches = false;
1613
16
    } else
1614
14
      std::tie(Scope, Key, IndirectBranches) =
1615
14
          ParseAArch64BranchProtection(D, Args, A);
1616
30
1617
30
    CmdArgs.push_back(
1618
30
        Args.MakeArgString(Twine("-msign-return-address=") + Scope));
1619
30
    CmdArgs.push_back(
1620
30
        Args.MakeArgString(Twine("-msign-return-address-key=") + Key));
1621
30
    if (IndirectBranches)
1622
5
      CmdArgs.push_back("-mbranch-target-enforce");
1623
30
  }
1624
8.19k
}
1625
1626
void Clang::AddMIPSTargetArgs(const ArgList &Args,
1627
580
                              ArgStringList &CmdArgs) const {
1628
580
  const Driver &D = getToolChain().getDriver();
1629
580
  StringRef CPUName;
1630
580
  StringRef ABIName;
1631
580
  const llvm::Triple &Triple = getToolChain().getTriple();
1632
580
  mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
1633
580
1634
580
  CmdArgs.push_back("-target-abi");
1635
580
  CmdArgs.push_back(ABIName.data());
1636
580
1637
580
  mips::FloatABI ABI = mips::getMipsFloatABI(D, Args);
1638
580
  if (ABI == mips::FloatABI::Soft) {
1639
55
    // Floating point operations and argument passing are soft.
1640
55
    CmdArgs.push_back("-msoft-float");
1641
55
    CmdArgs.push_back("-mfloat-abi");
1642
55
    CmdArgs.push_back("soft");
1643
525
  } else {
1644
525
    // Floating point operations and argument passing are hard.
1645
525
    assert(ABI == mips::FloatABI::Hard && "Invalid float abi!");
1646
525
    CmdArgs.push_back("-mfloat-abi");
1647
525
    CmdArgs.push_back("hard");
1648
525
  }
1649
580
1650
580
  if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) {
1651
2
    if (A->getOption().matches(options::OPT_mxgot)) {
1652
1
      CmdArgs.push_back("-mllvm");
1653
1
      CmdArgs.push_back("-mxgot");
1654
1
    }
1655
2
  }
1656
580
1657
580
  if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1,
1658
2
                               options::OPT_mno_ldc1_sdc1)) {
1659
2
    if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) {
1660
1
      CmdArgs.push_back("-mllvm");
1661
1
      CmdArgs.push_back("-mno-ldc1-sdc1");
1662
1
    }
1663
2
  }
1664
580
1665
580
  if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division,
1666
2
                               options::OPT_mno_check_zero_division)) {
1667
2
    if (A->getOption().matches(options::OPT_mno_check_zero_division)) {
1668
1
      CmdArgs.push_back("-mllvm");
1669
1
      CmdArgs.push_back("-mno-check-zero-division");
1670
1
    }
1671
2
  }
1672
580
1673
580
  if (Arg *A = Args.getLastArg(options::OPT_G)) {
1674
3
    StringRef v = A->getValue();
1675
3
    CmdArgs.push_back("-mllvm");
1676
3
    CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
1677
3
    A->claim();
1678
3
  }
1679
580
1680
580
  Arg *GPOpt = Args.getLastArg(options::OPT_mgpopt, options::OPT_mno_gpopt);
1681
580
  Arg *ABICalls =
1682
580
      Args.getLastArg(options::OPT_mabicalls, options::OPT_mno_abicalls);
1683
580
1684
580
  // -mabicalls is the default for many MIPS environments, even with -fno-pic.
1685
580
  // -mgpopt is the default for static, -fno-pic environments but these two
1686
580
  // options conflict. We want to be certain that -mno-abicalls -mgpopt is
1687
580
  // the only case where -mllvm -mgpopt is passed.
1688
580
  // NOTE: We need a warning here or in the backend to warn when -mgpopt is
1689
580
  //       passed explicitly when compiling something with -mabicalls
1690
580
  //       (implictly) in affect. Currently the warning is in the backend.
1691
580
  //
1692
580
  // When the ABI in use is  N64, we also need to determine the PIC mode that
1693
580
  // is in use, as -fno-pic for N64 implies -mno-abicalls.
1694
580
  bool NoABICalls =
1695
580
      ABICalls && 
ABICalls->getOption().matches(options::OPT_mno_abicalls)35
;
1696
580
1697
580
  llvm::Reloc::Model RelocationModel;
1698
580
  unsigned PICLevel;
1699
580
  bool IsPIE;
1700
580
  std::tie(RelocationModel, PICLevel, IsPIE) =
1701
580
      ParsePICArgs(getToolChain(), Args);
1702
580
1703
580
  NoABICalls = NoABICalls ||
1704
580
               
(553
RelocationModel == llvm::Reloc::Static553
&&
ABIName == "n64"321
);
1705
580
1706
580
  bool WantGPOpt = GPOpt && 
GPOpt->getOption().matches(options::OPT_mgpopt)17
;
1707
580
  // We quietly ignore -mno-gpopt as the backend defaults to -mno-gpopt.
1708
580
  if (NoABICalls && 
(27
!GPOpt27
||
WantGPOpt12
)) {
1709
25
    CmdArgs.push_back("-mllvm");
1710
25
    CmdArgs.push_back("-mgpopt");
1711
25
1712
25
    Arg *LocalSData = Args.getLastArg(options::OPT_mlocal_sdata,
1713
25
                                      options::OPT_mno_local_sdata);
1714
25
    Arg *ExternSData = Args.getLastArg(options::OPT_mextern_sdata,
1715
25
                                       options::OPT_mno_extern_sdata);
1716
25
    Arg *EmbeddedData = Args.getLastArg(options::OPT_membedded_data,
1717
25
                                        options::OPT_mno_embedded_data);
1718
25
    if (LocalSData) {
1719
2
      CmdArgs.push_back("-mllvm");
1720
2
      if (LocalSData->getOption().matches(options::OPT_mlocal_sdata)) {
1721
1
        CmdArgs.push_back("-mlocal-sdata=1");
1722
1
      } else {
1723
1
        CmdArgs.push_back("-mlocal-sdata=0");
1724
1
      }
1725
2
      LocalSData->claim();
1726
2
    }
1727
25
1728
25
    if (ExternSData) {
1729
2
      CmdArgs.push_back("-mllvm");
1730
2
      if (ExternSData->getOption().matches(options::OPT_mextern_sdata)) {
1731
1
        CmdArgs.push_back("-mextern-sdata=1");
1732
1
      } else {
1733
1
        CmdArgs.push_back("-mextern-sdata=0");
1734
1
      }
1735
2
      ExternSData->claim();
1736
2
    }
1737
25
1738
25
    if (EmbeddedData) {
1739
2
      CmdArgs.push_back("-mllvm");
1740
2
      if (EmbeddedData->getOption().matches(options::OPT_membedded_data)) {
1741
1
        CmdArgs.push_back("-membedded-data=1");
1742
1
      } else {
1743
1
        CmdArgs.push_back("-membedded-data=0");
1744
1
      }
1745
2
      EmbeddedData->claim();
1746
2
    }
1747
25
1748
555
  } else if ((!ABICalls || 
(10
!NoABICalls10
&&
ABICalls8
)) &&
WantGPOpt553
)
1749
5
    D.Diag(diag::warn_drv_unsupported_gpopt) << (ABICalls ? 
02
:
13
);
1750
580
1751
580
  if (GPOpt)
1752
17
    GPOpt->claim();
1753
580
1754
580
  if (Arg *A = Args.getLastArg(options::OPT_mcompact_branches_EQ)) {
1755
3
    StringRef Val = StringRef(A->getValue());
1756
3
    if (mips::hasCompactBranches(CPUName)) {
1757
3
      if (Val == "never" || 
Val == "always"2
||
Val == "optimal"1
) {
1758
3
        CmdArgs.push_back("-mllvm");
1759
3
        CmdArgs.push_back(Args.MakeArgString("-mips-compact-branches=" + Val));
1760
3
      } else
1761
0
        D.Diag(diag::err_drv_unsupported_option_argument)
1762
0
            << A->getOption().getName() << Val;
1763
3
    } else
1764
0
      D.Diag(diag::warn_target_unsupported_compact_branches) << CPUName;
1765
3
  }
1766
580
1767
580
  if (Arg *A = Args.getLastArg(options::OPT_mrelax_pic_calls,
1768
2
                               options::OPT_mno_relax_pic_calls)) {
1769
2
    if (A->getOption().matches(options::OPT_mno_relax_pic_calls)) {
1770
1
      CmdArgs.push_back("-mllvm");
1771
1
      CmdArgs.push_back("-mips-jalr-reloc=0");
1772
1
    }
1773
2
  }
1774
580
}
1775
1776
void Clang::AddPPCTargetArgs(const ArgList &Args,
1777
325
                             ArgStringList &CmdArgs) const {
1778
325
  // Select the ABI to use.
1779
325
  const char *ABIName = nullptr;
1780
325
  if (getToolChain().getTriple().isOSLinux())
1781
271
    switch (getToolChain().getArch()) {
1782
271
    case llvm::Triple::ppc64: {
1783
119
      // When targeting a processor that supports QPX, or if QPX is
1784
119
      // specifically enabled, default to using the ABI that supports QPX (so
1785
119
      // long as it is not specifically disabled).
1786
119
      bool HasQPX = false;
1787
119
      if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
1788
52
        HasQPX = A->getValue() == StringRef("a2q");
1789
119
      HasQPX = Args.hasFlag(options::OPT_mqpx, options::OPT_mno_qpx, HasQPX);
1790
119
      if (HasQPX) {
1791
6
        ABIName = "elfv1-qpx";
1792
6
        break;
1793
6
      }
1794
113
1795
113
      ABIName = "elfv1";
1796
113
      break;
1797
113
    }
1798
126
    case llvm::Triple::ppc64le:
1799
126
      ABIName = "elfv2";
1800
126
      break;
1801
113
    default:
1802
26
      break;
1803
325
    }
1804
325
1805
325
  bool IEEELongDouble = false;
1806
325
  for (const Arg *A : Args.filtered(options::OPT_mabi_EQ)) {
1807
26
    StringRef V = A->getValue();
1808
26
    if (V == "ieeelongdouble")
1809
3
      IEEELongDouble = true;
1810
23
    else if (V == "ibmlongdouble")
1811
2
      IEEELongDouble = false;
1812
21
    else if (V != "altivec")
1813
19
      // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore
1814
19
      // the option if given as we don't have backend support for any targets
1815
19
      // that don't use the altivec abi.
1816
19
      ABIName = A->getValue();
1817
26
  }
1818
325
  if (IEEELongDouble)
1819
3
    CmdArgs.push_back("-mabi=ieeelongdouble");
1820
325
1821
325
  ppc::FloatABI FloatABI =
1822
325
      ppc::getPPCFloatABI(getToolChain().getDriver(), Args);
1823
325
1824
325
  if (FloatABI == ppc::FloatABI::Soft) {
1825
9
    // Floating point operations and argument passing are soft.
1826
9
    CmdArgs.push_back("-msoft-float");
1827
9
    CmdArgs.push_back("-mfloat-abi");
1828
9
    CmdArgs.push_back("soft");
1829
316
  } else {
1830
316
    // Floating point operations and argument passing are hard.
1831
316
    assert(FloatABI == ppc::FloatABI::Hard && "Invalid float abi!");
1832
316
    CmdArgs.push_back("-mfloat-abi");
1833
316
    CmdArgs.push_back("hard");
1834
316
  }
1835
325
1836
325
  if (ABIName) {
1837
245
    CmdArgs.push_back("-target-abi");
1838
245
    CmdArgs.push_back(ABIName);
1839
245
  }
1840
325
}
1841
1842
void Clang::AddRISCVTargetArgs(const ArgList &Args,
1843
162
                               ArgStringList &CmdArgs) const {
1844
162
  // FIXME: currently defaults to the soft-float ABIs. Will need to be
1845
162
  // expanded to select ilp32f, ilp32d, lp64f, lp64d when appropriate.
1846
162
  const char *ABIName = nullptr;
1847
162
  const llvm::Triple &Triple = getToolChain().getTriple();
1848
162
  if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
1849
14
    ABIName = A->getValue();
1850
148
  else if (Triple.getArch() == llvm::Triple::riscv32)
1851
94
    ABIName = "ilp32";
1852
54
  else if (Triple.getArch() == llvm::Triple::riscv64)
1853
54
    ABIName = "lp64";
1854
54
  else
1855
54
    
llvm_unreachable0
("Unexpected triple!");
1856
162
1857
162
  CmdArgs.push_back("-target-abi");
1858
162
  CmdArgs.push_back(ABIName);
1859
162
}
1860
1861
void Clang::AddSparcTargetArgs(const ArgList &Args,
1862
156
                               ArgStringList &CmdArgs) const {
1863
156
  sparc::FloatABI FloatABI =
1864
156
      sparc::getSparcFloatABI(getToolChain().getDriver(), Args);
1865
156
1866
156
  if (FloatABI == sparc::FloatABI::Soft) {
1867
4
    // Floating point operations and argument passing are soft.
1868
4
    CmdArgs.push_back("-msoft-float");
1869
4
    CmdArgs.push_back("-mfloat-abi");
1870
4
    CmdArgs.push_back("soft");
1871
152
  } else {
1872
152
    // Floating point operations and argument passing are hard.
1873
152
    assert(FloatABI == sparc::FloatABI::Hard && "Invalid float abi!");
1874
152
    CmdArgs.push_back("-mfloat-abi");
1875
152
    CmdArgs.push_back("hard");
1876
152
  }
1877
156
}
1878
1879
void Clang::AddSystemZTargetArgs(const ArgList &Args,
1880
43
                                 ArgStringList &CmdArgs) const {
1881
43
  if (Args.hasFlag(options::OPT_mbackchain, options::OPT_mno_backchain, false))
1882
1
    CmdArgs.push_back("-mbackchain");
1883
43
}
1884
1885
void Clang::AddX86TargetArgs(const ArgList &Args,
1886
14.9k
                             ArgStringList &CmdArgs) const {
1887
14.9k
  if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
1888
14.9k
      
Args.hasArg(options::OPT_mkernel)14.9k
||
1889
14.9k
      
Args.hasArg(options::OPT_fapple_kext)14.6k
)
1890
338
    CmdArgs.push_back("-disable-red-zone");
1891
14.9k
1892
14.9k
  if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs,
1893
14.9k
                    options::OPT_mno_tls_direct_seg_refs, true))
1894
1
    CmdArgs.push_back("-mno-tls-direct-seg-refs");
1895
14.9k
1896
14.9k
  // Default to avoid implicit floating-point for kernel/kext code, but allow
1897
14.9k
  // that to be overridden with -mno-soft-float.
1898
14.9k
  bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
1899
14.9k
                          
Args.hasArg(options::OPT_fapple_kext)14.6k
);
1900
14.9k
  if (Arg *A = Args.getLastArg(
1901
6
          options::OPT_msoft_float, options::OPT_mno_soft_float,
1902
6
          options::OPT_mimplicit_float, options::OPT_mno_implicit_float)) {
1903
6
    const Option &O = A->getOption();
1904
6
    NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
1905
6
                       
O.matches(options::OPT_msoft_float)5
);
1906
6
  }
1907
14.9k
  if (NoImplicitFloat)
1908
339
    CmdArgs.push_back("-no-implicit-float");
1909
14.9k
1910
14.9k
  if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
1911
3
    StringRef Value = A->getValue();
1912
3
    if (Value == "intel" || 
Value == "att"2
) {
1913
2
      CmdArgs.push_back("-mllvm");
1914
2
      CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
1915
2
    } else {
1916
1
      getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
1917
1
          << A->getOption().getName() << Value;
1918
1
    }
1919
14.9k
  } else if (getToolChain().getDriver().IsCLMode()) {
1920
512
    CmdArgs.push_back("-mllvm");
1921
512
    CmdArgs.push_back("-x86-asm-syntax=intel");
1922
512
  }
1923
14.9k
1924
14.9k
  // Set flags to support MCU ABI.
1925
14.9k
  if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) {
1926
8
    CmdArgs.push_back("-mfloat-abi");
1927
8
    CmdArgs.push_back("soft");
1928
8
    CmdArgs.push_back("-mstack-alignment=4");
1929
8
  }
1930
14.9k
}
1931
1932
void Clang::AddHexagonTargetArgs(const ArgList &Args,
1933
80
                                 ArgStringList &CmdArgs) const {
1934
80
  CmdArgs.push_back("-mqdsp6-compat");
1935
80
  CmdArgs.push_back("-Wreturn-type");
1936
80
1937
80
  if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) {
1938
8
    CmdArgs.push_back("-mllvm");
1939
8
    CmdArgs.push_back(Args.MakeArgString("-hexagon-small-data-threshold=" +
1940
8
                                         Twine(G.getValue())));
1941
8
  }
1942
80
1943
80
  if (!Args.hasArg(options::OPT_fno_short_enums))
1944
80
    CmdArgs.push_back("-fshort-enums");
1945
80
  if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
1946
0
    CmdArgs.push_back("-mllvm");
1947
0
    CmdArgs.push_back("-enable-hexagon-ieee-rnd-near");
1948
0
  }
1949
80
  CmdArgs.push_back("-mllvm");
1950
80
  CmdArgs.push_back("-machine-sink-split=0");
1951
80
}
1952
1953
void Clang::AddLanaiTargetArgs(const ArgList &Args,
1954
2
                               ArgStringList &CmdArgs) const {
1955
2
  if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
1956
0
    StringRef CPUName = A->getValue();
1957
0
1958
0
    CmdArgs.push_back("-target-cpu");
1959
0
    CmdArgs.push_back(Args.MakeArgString(CPUName));
1960
0
  }
1961
2
  if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1962
0
    StringRef Value = A->getValue();
1963
0
    // Only support mregparm=4 to support old usage. Report error for all other
1964
0
    // cases.
1965
0
    int Mregparm;
1966
0
    if (Value.getAsInteger(10, Mregparm)) {
1967
0
      if (Mregparm != 4) {
1968
0
        getToolChain().getDriver().Diag(
1969
0
            diag::err_drv_unsupported_option_argument)
1970
0
            << A->getOption().getName() << Value;
1971
0
      }
1972
0
    }
1973
0
  }
1974
2
}
1975
1976
void Clang::AddWebAssemblyTargetArgs(const ArgList &Args,
1977
61
                                     ArgStringList &CmdArgs) const {
1978
61
  // Default to "hidden" visibility.
1979
61
  if (!Args.hasArg(options::OPT_fvisibility_EQ,
1980
61
                   options::OPT_fvisibility_ms_compat)) {
1981
59
    CmdArgs.push_back("-fvisibility");
1982
59
    CmdArgs.push_back("hidden");
1983
59
  }
1984
61
}
1985
1986
void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename,
1987
                                    StringRef Target, const InputInfo &Output,
1988
3
                                    const InputInfo &Input, const ArgList &Args) const {
1989
3
  // If this is a dry run, do not create the compilation database file.
1990
3
  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
1991
0
    return;
1992
3
1993
3
  using llvm::yaml::escape;
1994
3
  const Driver &D = getToolChain().getDriver();
1995
3
1996
3
  if (!CompilationDatabase) {
1997
2
    std::error_code EC;
1998
2
    auto File = llvm::make_unique<llvm::raw_fd_ostream>(Filename, EC, llvm::sys::fs::F_Text);
1999
2
    if (EC) {
2000
1
      D.Diag(clang::diag::err_drv_compilationdatabase) << Filename
2001
1
                                                       << EC.message();
2002
1
      return;
2003
1
    }
2004
1
    CompilationDatabase = std::move(File);
2005
1
  }
2006
3
  auto &CDB = *CompilationDatabase;
2007
2
  SmallString<128> Buf;
2008
2
  if (llvm::sys::fs::current_path(Buf))
2009
0
    Buf = ".";
2010
2
  CDB << "{ \"directory\": \"" << escape(Buf) << "\"";
2011
2
  CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\"";
2012
2
  CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\"";
2013
2
  CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\"";
2014
2
  Buf = "-x";
2015
2
  Buf += types::getTypeName(Input.getType());
2016
2
  CDB << ", \"" << escape(Buf) << "\"";
2017
2
  if (!D.SysRoot.empty() && !Args.hasArg(options::OPT__sysroot_EQ)) {
2018
0
    Buf = "--sysroot=";
2019
0
    Buf += D.SysRoot;
2020
0
    CDB << ", \"" << escape(Buf) << "\"";
2021
0
  }
2022
2
  CDB << ", \"" << escape(Input.getFilename()) << "\"";
2023
32
  for (auto &A: Args) {
2024
32
    auto &O = A->getOption();
2025
32
    // Skip language selection, which is positional.
2026
32
    if (O.getID() == options::OPT_x)
2027
4
      continue;
2028
28
    // Skip writing dependency output and the compilation database itself.
2029
28
    if (O.getGroup().isValid() && 
O.getGroup().getID() == options::OPT_M_Group16
)
2030
6
      continue;
2031
22
    // Skip inputs.
2032
22
    if (O.getKind() == Option::InputClass)
2033
4
      continue;
2034
18
    // All other arguments are quoted and appended.
2035
18
    ArgStringList ASL;
2036
18
    A->render(Args, ASL);
2037
18
    for (auto &it: ASL)
2038
18
      CDB << ", \"" << escape(it) << "\"";
2039
18
  }
2040
2
  Buf = "--target=";
2041
2
  Buf += Target;
2042
2
  CDB << ", \"" << escape(Buf) << "\"]},\n";
2043
2
}
2044
2045
static void CollectArgsForIntegratedAssembler(Compilation &C,
2046
                                              const ArgList &Args,
2047
                                              ArgStringList &CmdArgs,
2048
18.2k
                                              const Driver &D) {
2049
18.2k
  if (UseRelaxAll(C, Args))
2050
5.33k
    CmdArgs.push_back("-mrelax-all");
2051
18.2k
2052
18.2k
  // Only default to -mincremental-linker-compatible if we think we are
2053
18.2k
  // targeting the MSVC linker.
2054
18.2k
  bool DefaultIncrementalLinkerCompatible =
2055
18.2k
      C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment();
2056
18.2k
  if (Args.hasFlag(options::OPT_mincremental_linker_compatible,
2057
18.2k
                   options::OPT_mno_incremental_linker_compatible,
2058
18.2k
                   DefaultIncrementalLinkerCompatible))
2059
533
    CmdArgs.push_back("-mincremental-linker-compatible");
2060
18.2k
2061
18.2k
  switch (C.getDefaultToolChain().getArch()) {
2062
18.2k
  case llvm::Triple::arm:
2063
4.18k
  case llvm::Triple::armeb:
2064
4.18k
  case llvm::Triple::thumb:
2065
4.18k
  case llvm::Triple::thumbeb:
2066
4.18k
    if (Arg *A = Args.getLastArg(options::OPT_mimplicit_it_EQ)) {
2067
5
      StringRef Value = A->getValue();
2068
5
      if (Value == "always" || 
Value == "never"4
||
Value == "arm"3
||
2069
5
          
Value == "thumb"2
) {
2070
4
        CmdArgs.push_back("-mllvm");
2071
4
        CmdArgs.push_back(Args.MakeArgString("-arm-implicit-it=" + Value));
2072
4
      } else {
2073
1
        D.Diag(diag::err_drv_unsupported_option_argument)
2074
1
            << A->getOption().getName() << Value;
2075
1
      }
2076
5
    }
2077
4.18k
    break;
2078
14.0k
  default:
2079
14.0k
    break;
2080
18.2k
  }
2081
18.2k
2082
18.2k
  // When passing -I arguments to the assembler we sometimes need to
2083
18.2k
  // unconditionally take the next argument.  For example, when parsing
2084
18.2k
  // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the
2085
18.2k
  // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo'
2086
18.2k
  // arg after parsing the '-I' arg.
2087
18.2k
  bool TakeNextArg = false;
2088
18.2k
2089
18.2k
  bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations();
2090
18.2k
  bool UseNoExecStack = C.getDefaultToolChain().isNoExecStackDefault();
2091
18.2k
  const char *MipsTargetFeature = nullptr;
2092
18.2k
  for (const Arg *A :
2093
18.2k
       Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
2094
76
    A->claim();
2095
76
2096
87
    for (StringRef Value : A->getValues()) {
2097
87
      if (TakeNextArg) {
2098
7
        CmdArgs.push_back(Value.data());
2099
7
        TakeNextArg = false;
2100
7
        continue;
2101
7
      }
2102
80
2103
80
      if (C.getDefaultToolChain().getTriple().isOSBinFormatCOFF() &&
2104
80
          
Value == "-mbig-obj"2
)
2105
2
        continue; // LLVM handles bigobj automatically
2106
78
2107
78
      switch (C.getDefaultToolChain().getArch()) {
2108
78
      default:
2109
31
        break;
2110
78
      case llvm::Triple::thumb:
2111
15
      case llvm::Triple::thumbeb:
2112
15
      case llvm::Triple::arm:
2113
15
      case llvm::Triple::armeb:
2114
15
        if (Value == "-mthumb")
2115
3
          // -mthumb has already been processed in ComputeLLVMTriple()
2116
3
          // recognize but skip over here.
2117
3
          continue;
2118
12
        break;
2119
32
      case llvm::Triple::mips:
2120
32
      case llvm::Triple::mipsel:
2121
32
      case llvm::Triple::mips64:
2122
32
      case llvm::Triple::mips64el:
2123
32
        if (Value == "--trap") {
2124
3
          CmdArgs.push_back("-target-feature");
2125
3
          CmdArgs.push_back("+use-tcc-in-div");
2126
3
          continue;
2127
3
        }
2128
29
        if (Value == "--break") {
2129
3
          CmdArgs.push_back("-target-feature");
2130
3
          CmdArgs.push_back("-use-tcc-in-div");
2131
3
          continue;
2132
3
        }
2133
26
        if (Value.startswith("-msoft-float")) {
2134
3
          CmdArgs.push_back("-target-feature");
2135
3
          CmdArgs.push_back("+soft-float");
2136
3
          continue;
2137
3
        }
2138
23
        if (Value.startswith("-mhard-float")) {
2139
3
          CmdArgs.push_back("-target-feature");
2140
3
          CmdArgs.push_back("-soft-float");
2141
3
          continue;
2142
3
        }
2143
20
2144
20
        MipsTargetFeature = llvm::StringSwitch<const char *>(Value)
2145
20
                                .Case("-mips1", "+mips1")
2146
20
                                .Case("-mips2", "+mips2")
2147
20
                                .Case("-mips3", "+mips3")
2148
20
                                .Case("-mips4", "+mips4")
2149
20
                                .Case("-mips5", "+mips5")
2150
20
                                .Case("-mips32", "+mips32")
2151
20
                                .Case("-mips32r2", "+mips32r2")
2152
20
                                .Case("-mips32r3", "+mips32r3")
2153
20
                                .Case("-mips32r5", "+mips32r5")
2154
20
                                .Case("-mips32r6", "+mips32r6")
2155
20
                                .Case("-mips64", "+mips64")
2156
20
                                .Case("-mips64r2", "+mips64r2")
2157
20
                                .Case("-mips64r3", "+mips64r3")
2158
20
                                .Case("-mips64r5", "+mips64r5")
2159
20
                                .Case("-mips64r6", "+mips64r6")
2160
20
                                .Default(nullptr);
2161
20
        if (MipsTargetFeature)
2162
20
          continue;
2163
43
      }
2164
43
2165
43
      if (Value == "-force_cpusubtype_ALL") {
2166
0
        // Do nothing, this is the default and we don't support anything else.
2167
43
      } else if (Value == "-L") {
2168
1
        CmdArgs.push_back("-msave-temp-labels");
2169
42
      } else if (Value == "--fatal-warnings") {
2170
2
        CmdArgs.push_back("-massembler-fatal-warnings");
2171
40
      } else if (Value == "--noexecstack") {
2172
1
        UseNoExecStack = true;
2173
39
      } else if (Value.startswith("-compress-debug-sections") ||
2174
39
                 
Value.startswith("--compress-debug-sections")37
||
2175
39
                 
Value == "-nocompress-debug-sections"34
||
2176
39
                 
Value == "--nocompress-debug-sections"34
) {
2177
6
        CmdArgs.push_back(Value.data());
2178
33
      } else if (Value == "-mrelax-relocations=yes" ||
2179
33
                 Value == "--mrelax-relocations=yes") {
2180
2
        UseRelaxRelocations = true;
2181
31
      } else if (Value == "-mrelax-relocations=no" ||
2182
31
                 Value == "--mrelax-relocations=no") {
2183
0
        UseRelaxRelocations = false;
2184
31
      } else if (Value.startswith("-I")) {
2185
5
        CmdArgs.push_back(Value.data());
2186
5
        // We need to consume the next argument if the current arg is a plain
2187
5
        // -I. The next arg will be the include directory.
2188
5
        if (Value == "-I")
2189
3
          TakeNextArg = true;
2190
26
      } else if (Value.startswith("-gdwarf-")) {
2191
3
        // "-gdwarf-N" options are not cc1as options.
2192
3
        unsigned DwarfVersion = DwarfVersionNum(Value);
2193
3
        if (DwarfVersion == 0) { // Send it onward, and let cc1as complain.
2194
1
          CmdArgs.push_back(Value.data());
2195
2
        } else {
2196
2
          RenderDebugEnablingArgs(Args, CmdArgs,
2197
2
                                  codegenoptions::LimitedDebugInfo,
2198
2
                                  DwarfVersion, llvm::DebuggerKind::Default);
2199
2
        }
2200
23
      } else if (Value.startswith("-mcpu") || 
Value.startswith("-mfpu")20
||
2201
23
                 
Value.startswith("-mhwdiv")17
||
Value.startswith("-march")13
) {
2202
12
        // Do nothing, we'll validate it later.
2203
12
      } else 
if (11
Value == "-defsym"11
) {
2204
7
          if (A->getNumValues() != 2) {
2205
2
            D.Diag(diag::err_drv_defsym_invalid_format) << Value;
2206
2
            break;
2207
2
          }
2208
5
          const char *S = A->getValue(1);
2209
5
          auto Pair = StringRef(S).split('=');
2210
5
          auto Sym = Pair.first;
2211
5
          auto SVal = Pair.second;
2212
5
2213
5
          if (Sym.empty() || 
SVal.empty()4
) {
2214
2
            D.Diag(diag::err_drv_defsym_invalid_format) << S;
2215
2
            break;
2216
2
          }
2217
3
          int64_t IVal;
2218
3
          if (SVal.getAsInteger(0, IVal)) {
2219
1
            D.Diag(diag::err_drv_defsym_invalid_symval) << SVal;
2220
1
            break;
2221
1
          }
2222
2
          CmdArgs.push_back(Value.data());
2223
2
          TakeNextArg = true;
2224
4
      } else if (Value == "-fdebug-compilation-dir") {
2225
2
        CmdArgs.push_back("-fdebug-compilation-dir");
2226
2
        TakeNextArg = true;
2227
2
      } else {
2228
2
        D.Diag(diag::err_drv_unsupported_option_argument)
2229
2
            << A->getOption().getName() << Value;
2230
2
      }
2231
43
    }
2232
76
  }
2233
18.2k
  if (UseRelaxRelocations)
2234
33
    CmdArgs.push_back("--mrelax-relocations");
2235
18.2k
  if (UseNoExecStack)
2236
193
    CmdArgs.push_back("-mnoexecstack");
2237
18.2k
  if (MipsTargetFeature != nullptr) {
2238
17
    CmdArgs.push_back("-target-feature");
2239
17
    CmdArgs.push_back(MipsTargetFeature);
2240
17
  }
2241
18.2k
2242
18.2k
  // forward -fembed-bitcode to assmebler
2243
18.2k
  if (C.getDriver().embedBitcodeEnabled() ||
2244
18.2k
      
C.getDriver().embedBitcodeMarkerOnly()18.2k
)
2245
16
    Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ);
2246
18.2k
}
2247
2248
static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
2249
                                       bool OFastEnabled, const ArgList &Args,
2250
29.6k
                                       ArgStringList &CmdArgs) {
2251
29.6k
  // Handle various floating point optimization flags, mapping them to the
2252
29.6k
  // appropriate LLVM code generation flags. This is complicated by several
2253
29.6k
  // "umbrella" flags, so we do this by stepping through the flags incrementally
2254
29.6k
  // adjusting what we think is enabled/disabled, then at the end setting the
2255
29.6k
  // LLVM flags based on the final state.
2256
29.6k
  bool HonorINFs = true;
2257
29.6k
  bool HonorNaNs = true;
2258
29.6k
  // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
2259
29.6k
  bool MathErrno = TC.IsMathErrnoDefault();
2260
29.6k
  bool AssociativeMath = false;
2261
29.6k
  bool ReciprocalMath = false;
2262
29.6k
  bool SignedZeros = true;
2263
29.6k
  bool TrappingMath = true;
2264
29.6k
  StringRef DenormalFPMath = "";
2265
29.6k
  StringRef FPContract = "";
2266
29.6k
2267
29.6k
  if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
2268
0
    CmdArgs.push_back("-mlimit-float-precision");
2269
0
    CmdArgs.push_back(A->getValue());
2270
0
  }
2271
29.6k
2272
760k
  for (const Arg *A : Args) {
2273
760k
    switch (A->getOption().getID()) {
2274
760k
    // If this isn't an FP option skip the claim below
2275
760k
    
default: continue760k
;
2276
760k
2277
760k
    // Options controlling individual features
2278
760k
    
case options::OPT_fhonor_infinities: HonorINFs = true; break3
;
2279
760k
    
case options::OPT_fno_honor_infinities: HonorINFs = false; break6
;
2280
760k
    
case options::OPT_fhonor_nans: HonorNaNs = true; break3
;
2281
760k
    
case options::OPT_fno_honor_nans: HonorNaNs = false; break7
;
2282
760k
    
case options::OPT_fmath_errno: MathErrno = true; break8
;
2283
760k
    
case options::OPT_fno_math_errno: MathErrno = false; break14
;
2284
760k
    
case options::OPT_fassociative_math: AssociativeMath = true; break10
;
2285
760k
    
case options::OPT_fno_associative_math: AssociativeMath = false; break4
;
2286
760k
    
case options::OPT_freciprocal_math: ReciprocalMath = true; break13
;
2287
760k
    
case options::OPT_fno_reciprocal_math: ReciprocalMath = false; break3
;
2288
760k
    
case options::OPT_fsigned_zeros: SignedZeros = true; break3
;
2289
760k
    
case options::OPT_fno_signed_zeros: SignedZeros = false; break13
;
2290
760k
    
case options::OPT_ftrapping_math: TrappingMath = true; break6
;
2291
760k
    
case options::OPT_fno_trapping_math: TrappingMath = false; break12
;
2292
760k
2293
760k
    case options::OPT_fdenormal_fp_math_EQ:
2294
6
      DenormalFPMath = A->getValue();
2295
6
      break;
2296
760k
2297
760k
    // Validate and pass through -fp-contract option.
2298
760k
    case options::OPT_ffp_contract: {
2299
5
      StringRef Val = A->getValue();
2300
5
      if (Val == "fast" || 
Val == "on"2
||
Val == "off"2
)
2301
5
        FPContract = Val;
2302
0
      else
2303
0
        D.Diag(diag::err_drv_unsupported_option_argument)
2304
0
            << A->getOption().getName() << Val;
2305
5
      break;
2306
760k
    }
2307
760k
2308
760k
    case options::OPT_ffinite_math_only:
2309
7
      HonorINFs = false;
2310
7
      HonorNaNs = false;
2311
7
      break;
2312
760k
    case options::OPT_fno_finite_math_only:
2313
6
      HonorINFs = true;
2314
6
      HonorNaNs = true;
2315
6
      break;
2316
760k
2317
760k
    case options::OPT_funsafe_math_optimizations:
2318
8
      AssociativeMath = true;
2319
8
      ReciprocalMath = true;
2320
8
      SignedZeros = false;
2321
8
      TrappingMath = false;
2322
8
      break;
2323
760k
    case options::OPT_fno_unsafe_math_optimizations:
2324
6
      AssociativeMath = false;
2325
6
      ReciprocalMath = false;
2326
6
      SignedZeros = true;
2327
6
      TrappingMath = true;
2328
6
      // -fno_unsafe_math_optimizations restores default denormal handling
2329
6
      DenormalFPMath = "";
2330
6
      break;
2331
760k
2332
760k
    case options::OPT_Ofast:
2333
23
      // If -Ofast is the optimization level, then -ffast-math should be enabled
2334
23
      if (!OFastEnabled)
2335
2
        continue;
2336
21
      LLVM_FALLTHROUGH;
2337
60
    case options::OPT_ffast_math:
2338
60
      HonorINFs = false;
2339
60
      HonorNaNs = false;
2340
60
      MathErrno = false;
2341
60
      AssociativeMath = true;
2342
60
      ReciprocalMath = true;
2343
60
      SignedZeros = false;
2344
60
      TrappingMath = false;
2345
60
      // If fast-math is set then set the fp-contract mode to fast.
2346
60
      FPContract = "fast";
2347
60
      break;
2348
31
    case options::OPT_fno_fast_math:
2349
31
      HonorINFs = true;
2350
31
      HonorNaNs = true;
2351
31
      // Turning on -ffast-math (with either flag) removes the need for
2352
31
      // MathErrno. However, turning *off* -ffast-math merely restores the
2353
31
      // toolchain default (which may be false).
2354
31
      MathErrno = TC.IsMathErrnoDefault();
2355
31
      AssociativeMath = false;
2356
31
      ReciprocalMath = false;
2357
31
      SignedZeros = true;
2358
31
      TrappingMath = true;
2359
31
      // -fno_fast_math restores default denormal and fpcontract handling
2360
31
      DenormalFPMath = "";
2361
31
      FPContract = "";
2362
31
      break;
2363
234
    }
2364
234
2365
234
    // If we handled this option claim it
2366
234
    A->claim();
2367
234
  }
2368
29.6k
2369
29.6k
  
if (29.6k
!HonorINFs29.6k
)
2370
58
    CmdArgs.push_back("-menable-no-infs");
2371
29.6k
2372
29.6k
  if (!HonorNaNs)
2373
59
    CmdArgs.push_back("-menable-no-nans");
2374
29.6k
2375
29.6k
  if (MathErrno)
2376
7.56k
    CmdArgs.push_back("-fmath-errno");
2377
29.6k
2378
29.6k
  if (!MathErrno && 
AssociativeMath22.0k
&&
ReciprocalMath60
&&
!SignedZeros57
&&
2379
29.6k
      
!TrappingMath54
)
2380
50
    CmdArgs.push_back("-menable-unsafe-fp-math");
2381
29.6k
2382
29.6k
  if (!SignedZeros)
2383
67
    CmdArgs.push_back("-fno-signed-zeros");
2384
29.6k
2385
29.6k
  if (AssociativeMath && 
!SignedZeros63
&&
!TrappingMath60
)
2386
56
    CmdArgs.push_back("-mreassociate");
2387
29.6k
2388
29.6k
  if (ReciprocalMath)
2389
66
    CmdArgs.push_back("-freciprocal-math");
2390
29.6k
2391
29.6k
  if (!TrappingMath)
2392
65
    CmdArgs.push_back("-fno-trapping-math");
2393
29.6k
2394
29.6k
  if (!DenormalFPMath.empty())
2395
4
    CmdArgs.push_back(
2396
4
        Args.MakeArgString("-fdenormal-fp-math=" + DenormalFPMath));
2397
29.6k
2398
29.6k
  if (!FPContract.empty())
2399
59
    CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract));
2400
29.6k
2401
29.6k
  ParseMRecip(D, Args, CmdArgs);
2402
29.6k
2403
29.6k
  // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for the
2404
29.6k
  // individual features enabled by -ffast-math instead of the option itself as
2405
29.6k
  // that's consistent with gcc's behaviour.
2406
29.6k
  if (!HonorINFs && 
!HonorNaNs58
&&
!MathErrno53
&&
AssociativeMath51
&&
2407
29.6k
      
ReciprocalMath44
&&
!SignedZeros43
&&
!TrappingMath42
)
2408
40
    CmdArgs.push_back("-ffast-math");
2409
29.6k
2410
29.6k
  // Handle __FINITE_MATH_ONLY__ similarly.
2411
29.6k
  if (!HonorINFs && 
!HonorNaNs58
)
2412
53
    CmdArgs.push_back("-ffinite-math-only");
2413
29.6k
2414
29.6k
  if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) {
2415
0
    CmdArgs.push_back("-mfpmath");
2416
0
    CmdArgs.push_back(A->getValue());
2417
0
  }
2418
29.6k
2419
29.6k
  // Disable a codegen optimization for floating-point casts.
2420
29.6k
  if (Args.hasFlag(options::OPT_fno_strict_float_cast_overflow,
2421
29.6k
                   options::OPT_fstrict_float_cast_overflow, false))
2422
1
    CmdArgs.push_back("-fno-strict-float-cast-overflow");
2423
29.6k
}
2424
2425
static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs,
2426
                                  const llvm::Triple &Triple,
2427
17
                                  const InputInfo &Input) {
2428
17
  // Enable region store model by default.
2429
17
  CmdArgs.push_back("-analyzer-store=region");
2430
17
2431
17
  // Treat blocks as analysis entry points.
2432
17
  CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
2433
17
2434
17
  // Add default argument set.
2435
17
  if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
2436
17
    CmdArgs.push_back("-analyzer-checker=core");
2437
17
    CmdArgs.push_back("-analyzer-checker=apiModeling");
2438
17
2439
17
    if (!Triple.isWindowsMSVCEnvironment()) {
2440
16
      CmdArgs.push_back("-analyzer-checker=unix");
2441
16
    } else {
2442
1
      // Enable "unix" checkers that also work on Windows.
2443
1
      CmdArgs.push_back("-analyzer-checker=unix.API");
2444
1
      CmdArgs.push_back("-analyzer-checker=unix.Malloc");
2445
1
      CmdArgs.push_back("-analyzer-checker=unix.MallocSizeof");
2446
1
      CmdArgs.push_back("-analyzer-checker=unix.MismatchedDeallocator");
2447
1
      CmdArgs.push_back("-analyzer-checker=unix.cstring.BadSizeArg");
2448
1
      CmdArgs.push_back("-analyzer-checker=unix.cstring.NullArg");
2449
1
    }
2450
17
2451
17
    // Disable some unix checkers for PS4.
2452
17
    if (Triple.isPS4CPU()) {
2453
3
      CmdArgs.push_back("-analyzer-disable-checker=unix.API");
2454
3
      CmdArgs.push_back("-analyzer-disable-checker=unix.Vfork");
2455
3
    }
2456
17
2457
17
    if (Triple.isOSDarwin())
2458
12
      CmdArgs.push_back("-analyzer-checker=osx");
2459
17
2460
17
    CmdArgs.push_back("-analyzer-checker=deadcode");
2461
17
2462
17
    if (types::isCXX(Input.getType()))
2463
10
      CmdArgs.push_back("-analyzer-checker=cplusplus");
2464
17
2465
17
    if (!Triple.isPS4CPU()) {
2466
14
      CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn");
2467
14
      CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
2468
14
      CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
2469
14
      CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
2470
14
      CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
2471
14
      CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
2472
14
    }
2473
17
2474
17
    // Default nullability checks.
2475
17
    CmdArgs.push_back("-analyzer-checker=nullability.NullPassedToNonnull");
2476
17
    CmdArgs.push_back("-analyzer-checker=nullability.NullReturnedFromNonnull");
2477
17
  }
2478
17
2479
17
  // Set the output format. The default is plist, for (lame) historical reasons.
2480
17
  CmdArgs.push_back("-analyzer-output");
2481
17
  if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
2482
0
    CmdArgs.push_back(A->getValue());
2483
17
  else
2484
17
    CmdArgs.push_back("plist");
2485
17
2486
17
  // Disable the presentation of standard compiler warnings when using
2487
17
  // --analyze.  We only want to show static analyzer diagnostics or frontend
2488
17
  // errors.
2489
17
  CmdArgs.push_back("-w");
2490
17
2491
17
  // Add -Xanalyzer arguments when running as analyzer.
2492
17
  Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
2493
17
}
2494
2495
static void RenderSSPOptions(const ToolChain &TC, const ArgList &Args,
2496
29.5k
                             ArgStringList &CmdArgs, bool KernelOrKext) {
2497
29.5k
  const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple();
2498
29.5k
2499
29.5k
  // NVPTX doesn't support stack protectors; from the compiler's perspective, it
2500
29.5k
  // doesn't even have a stack!
2501
29.5k
  if (EffectiveTriple.isNVPTX())
2502
308
    return;
2503
29.2k
2504
29.2k
  // -stack-protector=0 is default.
2505
29.2k
  unsigned StackProtectorLevel = 0;
2506
29.2k
  unsigned DefaultStackProtectorLevel =
2507
29.2k
      TC.GetDefaultStackProtectorLevel(KernelOrKext);
2508
29.2k
2509
29.2k
  if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
2510
2.22k
                               options::OPT_fstack_protector_all,
2511
2.22k
                               options::OPT_fstack_protector_strong,
2512
2.22k
                               options::OPT_fstack_protector)) {
2513
2.22k
    if (A->getOption().matches(options::OPT_fstack_protector))
2514
7
      StackProtectorLevel =
2515
7
          std::max<unsigned>(LangOptions::SSPOn, DefaultStackProtectorLevel);
2516
2.21k
    else if (A->getOption().matches(options::OPT_fstack_protector_strong))
2517
2
      StackProtectorLevel = LangOptions::SSPStrong;
2518
2.21k
    else if (A->getOption().matches(options::OPT_fstack_protector_all))
2519
3
      StackProtectorLevel = LangOptions::SSPReq;
2520
27.0k
  } else {
2521
27.0k
    StackProtectorLevel = DefaultStackProtectorLevel;
2522
27.0k
  }
2523
29.2k
2524
29.2k
  if (StackProtectorLevel) {
2525
17.7k
    CmdArgs.push_back("-stack-protector");
2526
17.7k
    CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
2527
17.7k
  }
2528
29.2k
2529
29.2k
  // --param ssp-buffer-size=
2530
29.2k
  for (const Arg *A : Args.filtered(options::OPT__param)) {
2531
5
    StringRef Str(A->getValue());
2532
5
    if (Str.startswith("ssp-buffer-size=")) {
2533
2
      if (StackProtectorLevel) {
2534
2
        CmdArgs.push_back("-stack-protector-buffer-size");
2535
2
        // FIXME: Verify the argument is a valid integer.
2536
2
        CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
2537
2
      }
2538
2
      A->claim();
2539
2
    }
2540
5
  }
2541
29.2k
}
2542
2543
static void RenderTrivialAutoVarInitOptions(const Driver &D,
2544
                                            const ToolChain &TC,
2545
                                            const ArgList &Args,
2546
29.5k
                                            ArgStringList &CmdArgs) {
2547
29.5k
  auto DefaultTrivialAutoVarInit = TC.GetDefaultTrivialAutoVarInit();
2548
29.5k
  StringRef TrivialAutoVarInit = "";
2549
29.5k
2550
760k
  for (const Arg *A : Args) {
2551
760k
    switch (A->getOption().getID()) {
2552
760k
    default:
2553
760k
      continue;
2554
760k
    case options::OPT_ftrivial_auto_var_init: {
2555
4
      A->claim();
2556
4
      StringRef Val = A->getValue();
2557
4
      if (Val == "uninitialized" || 
Val == "zero"3
||
Val == "pattern"1
)
2558
4
        TrivialAutoVarInit = Val;
2559
0
      else
2560
0
        D.Diag(diag::err_drv_unsupported_option_argument)
2561
0
            << A->getOption().getName() << Val;
2562
4
      break;
2563
760k
    }
2564
760k
    }
2565
760k
  }
2566
29.5k
2567
29.6k
  if (TrivialAutoVarInit.empty())
2568
29.5k
    switch (DefaultTrivialAutoVarInit) {
2569
29.5k
    case LangOptions::TrivialAutoVarInitKind::Uninitialized:
2570
29.5k
      break;
2571
29.5k
    case LangOptions::TrivialAutoVarInitKind::Pattern:
2572
0
      TrivialAutoVarInit = "pattern";
2573
0
      break;
2574
29.5k
    case LangOptions::TrivialAutoVarInitKind::Zero:
2575
0
      TrivialAutoVarInit = "zero";
2576
0
      break;
2577
29.6k
    }
2578
29.6k
2579
29.6k
  if (!TrivialAutoVarInit.empty()) {
2580
4
    if (TrivialAutoVarInit == "zero" && 
!Args.hasArg(options::OPT_enable_trivial_var_init_zero)2
)
2581
1
      D.Diag(diag::err_drv_trivial_auto_var_init_zero_disabled);
2582
4
    CmdArgs.push_back(
2583
4
        Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit));
2584
4
  }
2585
29.6k
}
2586
2587
29.5k
static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs) {
2588
29.5k
  const unsigned ForwardedArguments[] = {
2589
29.5k
      options::OPT_cl_opt_disable,
2590
29.5k
      options::OPT_cl_strict_aliasing,
2591
29.5k
      options::OPT_cl_single_precision_constant,
2592
29.5k
      options::OPT_cl_finite_math_only,
2593
29.5k
      options::OPT_cl_kernel_arg_info,
2594
29.5k
      options::OPT_cl_unsafe_math_optimizations,
2595
29.5k
      options::OPT_cl_fast_relaxed_math,
2596
29.5k
      options::OPT_cl_mad_enable,
2597
29.5k
      options::OPT_cl_no_signed_zeros,
2598
29.5k
      options::OPT_cl_denorms_are_zero,
2599
29.5k
      options::OPT_cl_fp32_correctly_rounded_divide_sqrt,
2600
29.5k
      options::OPT_cl_uniform_work_group_size
2601
29.5k
  };
2602
29.5k
2603
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_cl_std_EQ)) {
2604
12
    std::string CLStdStr = std::string("-cl-std=") + A->getValue();
2605
12
    CmdArgs.push_back(Args.MakeArgString(CLStdStr));
2606
12
  }
2607
29.5k
2608
29.5k
  for (const auto &Arg : ForwardedArguments)
2609
355k
    if (const auto *A = Args.getLastArg(Arg))
2610
12
      CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName()));
2611
29.5k
}
2612
2613
static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args,
2614
29.5k
                                        ArgStringList &CmdArgs) {
2615
29.5k
  bool ARCMTEnabled = false;
2616
29.5k
  if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) {
2617
29.5k
    if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
2618
1
                                       options::OPT_ccc_arcmt_modify,
2619
1
                                       options::OPT_ccc_arcmt_migrate)) {
2620
1
      ARCMTEnabled = true;
2621
1
      switch (A->getOption().getID()) {
2622
1
      
default: 0
llvm_unreachable0
("missed a case");
2623
1
      case options::OPT_ccc_arcmt_check:
2624
0
        CmdArgs.push_back("-arcmt-check");
2625
0
        break;
2626
1
      case options::OPT_ccc_arcmt_modify:
2627
0
        CmdArgs.push_back("-arcmt-modify");
2628
0
        break;
2629
1
      case options::OPT_ccc_arcmt_migrate:
2630
1
        CmdArgs.push_back("-arcmt-migrate");
2631
1
        CmdArgs.push_back("-mt-migrate-directory");
2632
1
        CmdArgs.push_back(A->getValue());
2633
1
2634
1
        Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
2635
1
        Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
2636
1
        break;
2637
34
      }
2638
34
    }
2639
34
  } else {
2640
34
    Args.ClaimAllArgs(options::OPT_ccc_arcmt_check);
2641
34
    Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify);
2642
34
    Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate);
2643
34
  }
2644
29.5k
2645
29.5k
  if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
2646
0
    if (ARCMTEnabled)
2647
0
      D.Diag(diag::err_drv_argument_not_allowed_with)
2648
0
          << A->getAsString(Args) << "-ccc-arcmt-migrate";
2649
0
2650
0
    CmdArgs.push_back("-mt-migrate-directory");
2651
0
    CmdArgs.push_back(A->getValue());
2652
0
2653
0
    if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
2654
0
                     options::OPT_objcmt_migrate_subscripting,
2655
0
                     options::OPT_objcmt_migrate_property)) {
2656
0
      // None specified, means enable them all.
2657
0
      CmdArgs.push_back("-objcmt-migrate-literals");
2658
0
      CmdArgs.push_back("-objcmt-migrate-subscripting");
2659
0
      CmdArgs.push_back("-objcmt-migrate-property");
2660
0
    } else {
2661
0
      Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2662
0
      Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
2663
0
      Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
2664
0
    }
2665
29.5k
  } else {
2666
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2667
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
2668
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
2669
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all);
2670
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property);
2671
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property);
2672
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property_dot_syntax);
2673
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation);
2674
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype);
2675
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros);
2676
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance);
2677
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property);
2678
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property);
2679
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly);
2680
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init);
2681
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_objcmt_whitelist_dir_path);
2682
29.5k
  }
2683
29.5k
}
2684
2685
static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T,
2686
29.5k
                                 const ArgList &Args, ArgStringList &CmdArgs) {
2687
29.5k
  // -fbuiltin is default unless -mkernel is used.
2688
29.5k
  bool UseBuiltins =
2689
29.5k
      Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin,
2690
29.5k
                   !Args.hasArg(options::OPT_mkernel));
2691
29.5k
  if (!UseBuiltins)
2692
3.25k
    CmdArgs.push_back("-fno-builtin");
2693
29.5k
2694
29.5k
  // -ffreestanding implies -fno-builtin.
2695
29.5k
  if (Args.hasArg(options::OPT_ffreestanding))
2696
1.59k
    UseBuiltins = false;
2697
29.5k
2698
29.5k
  // Process the -fno-builtin-* options.
2699
760k
  for (const auto &Arg : Args) {
2700
760k
    const Option &O = Arg->getOption();
2701
760k
    if (!O.matches(options::OPT_fno_builtin_))
2702
760k
      continue;
2703
3
2704
3
    Arg->claim();
2705
3
2706
3
    // If -fno-builtin is specified, then there's no need to pass the option to
2707
3
    // the frontend.
2708
3
    if (!UseBuiltins)
2709
3
      continue;
2710
0
2711
0
    StringRef FuncName = Arg->getValue();
2712
0
    CmdArgs.push_back(Args.MakeArgString("-fno-builtin-" + FuncName));
2713
0
  }
2714
29.5k
2715
29.5k
  // le32-specific flags:
2716
29.5k
  //  -fno-math-builtin: clang should not convert math builtins to intrinsics
2717
29.5k
  //                     by default.
2718
29.5k
  if (TC.getArch() == llvm::Triple::le32)
2719
5
    CmdArgs.push_back("-fno-math-builtin");
2720
29.5k
}
2721
2722
17
void Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) {
2723
17
  llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false, Result);
2724
17
  llvm::sys::path::append(Result, "org.llvm.clang.");
2725
17
  appendUserToPath(Result);
2726
17
  llvm::sys::path::append(Result, "ModuleCache");
2727
17
}
2728
2729
static void RenderModulesOptions(Compilation &C, const Driver &D,
2730
                                 const ArgList &Args, const InputInfo &Input,
2731
                                 const InputInfo &Output,
2732
29.5k
                                 ArgStringList &CmdArgs, bool &HaveModules) {
2733
29.5k
  // -fmodules enables the use of precompiled modules (off by default).
2734
29.5k
  // Users can pass -fno-cxx-modules to turn off modules support for
2735
29.5k
  // C++/Objective-C++ programs.
2736
29.5k
  bool HaveClangModules = false;
2737
29.5k
  if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
2738
99
    bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
2739
99
                                     options::OPT_fno_cxx_modules, true);
2740
99
    if (AllowedInCXX || 
!types::isCXX(Input.getType())1
) {
2741
98
      CmdArgs.push_back("-fmodules");
2742
98
      HaveClangModules = true;
2743
98
    }
2744
99
  }
2745
29.5k
2746
29.5k
  HaveModules |= HaveClangModules;
2747
29.5k
  if (Args.hasArg(options::OPT_fmodules_ts)) {
2748
8
    CmdArgs.push_back("-fmodules-ts");
2749
8
    HaveModules = true;
2750
8
  }
2751
29.5k
2752
29.5k
  // -fmodule-maps enables implicit reading of module map files. By default,
2753
29.5k
  // this is enabled if we are using Clang's flavor of precompiled modules.
2754
29.5k
  if (Args.hasFlag(options::OPT_fimplicit_module_maps,
2755
29.5k
                   options::OPT_fno_implicit_module_maps, HaveClangModules))
2756
99
    CmdArgs.push_back("-fimplicit-module-maps");
2757
29.5k
2758
29.5k
  // -fmodules-decluse checks that modules used are declared so (off by default)
2759
29.5k
  if (Args.hasFlag(options::OPT_fmodules_decluse,
2760
29.5k
                   options::OPT_fno_modules_decluse, false))
2761
1
    CmdArgs.push_back("-fmodules-decluse");
2762
29.5k
2763
29.5k
  // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that
2764
29.5k
  // all #included headers are part of modules.
2765
29.5k
  if (Args.hasFlag(options::OPT_fmodules_strict_decluse,
2766
29.5k
                   options::OPT_fno_modules_strict_decluse, false))
2767
0
    CmdArgs.push_back("-fmodules-strict-decluse");
2768
29.5k
2769
29.5k
  // -fno-implicit-modules turns off implicitly compiling modules on demand.
2770
29.5k
  bool ImplicitModules = false;
2771
29.5k
  if (!Args.hasFlag(options::OPT_fimplicit_modules,
2772
29.5k
                    options::OPT_fno_implicit_modules, HaveClangModules)) {
2773
29.5k
    if (HaveModules)
2774
60
      CmdArgs.push_back("-fno-implicit-modules");
2775
29.5k
  } else 
if (97
HaveModules97
) {
2776
97
    ImplicitModules = true;
2777
97
    // -fmodule-cache-path specifies where our implicitly-built module files
2778
97
    // should be written.
2779
97
    SmallString<128> Path;
2780
97
    if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path))
2781
81
      Path = A->getValue();
2782
97
2783
97
    if (C.isForDiagnostics()) {
2784
14
      // When generating crash reports, we want to emit the modules along with
2785
14
      // the reproduction sources, so we ignore any provided module path.
2786
14
      Path = Output.getFilename();
2787
14
      llvm::sys::path::replace_extension(Path, ".cache");
2788
14
      llvm::sys::path::append(Path, "modules");
2789
83
    } else if (Path.empty()) {
2790
16
      // No module path was provided: use the default.
2791
16
      Driver::getDefaultModuleCachePath(Path);
2792
16
    }
2793
97
2794
97
    const char Arg[] = "-fmodules-cache-path=";
2795
97
    Path.insert(Path.begin(), Arg, Arg + strlen(Arg));
2796
97
    CmdArgs.push_back(Args.MakeArgString(Path));
2797
97
  }
2798
29.5k
2799
29.5k
  if (HaveModules) {
2800
157
    // -fprebuilt-module-path specifies where to load the prebuilt module files.
2801
157
    for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) {
2802
2
      CmdArgs.push_back(Args.MakeArgString(
2803
2
          std::string("-fprebuilt-module-path=") + A->getValue()));
2804
2
      A->claim();
2805
2
    }
2806
157
  }
2807
29.5k
2808
29.5k
  // -fmodule-name specifies the module that is currently being built (or
2809
29.5k
  // used for header checking by -fmodule-maps).
2810
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fmodule_name_EQ);
2811
29.5k
2812
29.5k
  // -fmodule-map-file can be used to specify files containing module
2813
29.5k
  // definitions.
2814
29.5k
  Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file);
2815
29.5k
2816
29.5k
  // -fbuiltin-module-map can be used to load the clang
2817
29.5k
  // builtin headers modulemap file.
2818
29.5k
  if (Args.hasArg(options::OPT_fbuiltin_module_map)) {
2819
1
    SmallString<128> BuiltinModuleMap(D.ResourceDir);
2820
1
    llvm::sys::path::append(BuiltinModuleMap, "include");
2821
1
    llvm::sys::path::append(BuiltinModuleMap, "module.modulemap");
2822
1
    if (llvm::sys::fs::exists(BuiltinModuleMap))
2823
1
      CmdArgs.push_back(
2824
1
          Args.MakeArgString("-fmodule-map-file=" + BuiltinModuleMap));
2825
1
  }
2826
29.5k
2827
29.5k
  // The -fmodule-file=<name>=<file> form specifies the mapping of module
2828
29.5k
  // names to precompiled module files (the module is loaded only if used).
2829
29.5k
  // The -fmodule-file=<file> form can be used to unconditionally load
2830
29.5k
  // precompiled module files (whether used or not).
2831
29.5k
  if (HaveModules)
2832
157
    Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file);
2833
29.4k
  else
2834
29.4k
    Args.ClaimAllArgs(options::OPT_fmodule_file);
2835
29.5k
2836
29.5k
  // When building modules and generating crashdumps, we need to dump a module
2837
29.5k
  // dependency VFS alongside the output.
2838
29.5k
  if (HaveClangModules && 
C.isForDiagnostics()98
) {
2839
14
    SmallString<128> VFSDir(Output.getFilename());
2840
14
    llvm::sys::path::replace_extension(VFSDir, ".cache");
2841
14
    // Add the cache directory as a temp so the crash diagnostics pick it up.
2842
14
    C.addTempFile(Args.MakeArgString(VFSDir));
2843
14
2844
14
    llvm::sys::path::append(VFSDir, "vfs");
2845
14
    CmdArgs.push_back("-module-dependency-dir");
2846
14
    CmdArgs.push_back(Args.MakeArgString(VFSDir));
2847
14
  }
2848
29.5k
2849
29.5k
  if (HaveClangModules)
2850
98
    Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path);
2851
29.5k
2852
29.5k
  // Pass through all -fmodules-ignore-macro arguments.
2853
29.5k
  Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
2854
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
2855
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
2856
29.5k
2857
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp);
2858
29.5k
2859
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) {
2860
4
    if (Args.hasArg(options::OPT_fbuild_session_timestamp))
2861
1
      D.Diag(diag::err_drv_argument_not_allowed_with)
2862
1
          << A->getAsString(Args) << "-fbuild-session-timestamp";
2863
4
2864
4
    llvm::sys::fs::file_status Status;
2865
4
    if (llvm::sys::fs::status(A->getValue(), Status))
2866
1
      D.Diag(diag::err_drv_no_such_file) << A->getValue();
2867
4
    CmdArgs.push_back(
2868
4
        Args.MakeArgString("-fbuild-session-timestamp=" +
2869
4
                           Twine((uint64_t)Status.getLastModificationTime()
2870
4
                                     .time_since_epoch()
2871
4
                                     .count())));
2872
4
  }
2873
29.5k
2874
29.5k
  if (Args.getLastArg(options::OPT_fmodules_validate_once_per_build_session)) {
2875
3
    if (!Args.getLastArg(options::OPT_fbuild_session_timestamp,
2876
3
                         options::OPT_fbuild_session_file))
2877
1
      D.Diag(diag::err_drv_modules_validate_once_requires_timestamp);
2878
3
2879
3
    Args.AddLastArg(CmdArgs,
2880
3
                    options::OPT_fmodules_validate_once_per_build_session);
2881
3
  }
2882
29.5k
2883
29.5k
  if (Args.hasFlag(options::OPT_fmodules_validate_system_headers,
2884
29.5k
                   options::OPT_fno_modules_validate_system_headers,
2885
29.5k
                   ImplicitModules))
2886
98
    CmdArgs.push_back("-fmodules-validate-system-headers");
2887
29.5k
2888
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fmodules_disable_diagnostic_validation);
2889
29.5k
}
2890
2891
static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T,
2892
29.5k
                                   ArgStringList &CmdArgs) {
2893
29.5k
  // -fsigned-char is default.
2894
29.5k
  if (const Arg *A = Args.getLastArg(options::OPT_fsigned_char,
2895
6
                                     options::OPT_fno_signed_char,
2896
6
                                     options::OPT_funsigned_char,
2897
6
                                     options::OPT_fno_unsigned_char)) {
2898
6
    if (A->getOption().matches(options::OPT_funsigned_char) ||
2899
6
        
A->getOption().matches(options::OPT_fno_signed_char)4
) {
2900
4
      CmdArgs.push_back("-fno-signed-char");
2901
4
    }
2902
29.5k
  } else if (!isSignedCharDefault(T)) {
2903
3.69k
    CmdArgs.push_back("-fno-signed-char");
2904
3.69k
  }
2905
29.5k
2906
29.5k
  // The default depends on the language standard.
2907
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fchar8__t, options::OPT_fno_char8__t);
2908
29.5k
2909
29.5k
  if (const Arg *A = Args.getLastArg(options::OPT_fshort_wchar,
2910
26
                                     options::OPT_fno_short_wchar)) {
2911
26
    if (A->getOption().matches(options::OPT_fshort_wchar)) {
2912
4
      CmdArgs.push_back("-fwchar-type=short");
2913
4
      CmdArgs.push_back("-fno-signed-wchar");
2914
22
    } else {
2915
22
      bool IsARM = T.isARM() || 
T.isThumb()9
||
T.isAArch64()9
;
2916
22
      CmdArgs.push_back("-fwchar-type=int");
2917
22
      if (IsARM && 
!(20
T.isOSWindows()20
||
T.isOSNetBSD()18
||
2918
20
                     
T.isOSOpenBSD()12
))
2919
6
        CmdArgs.push_back("-fno-signed-wchar");
2920
16
      else
2921
16
        CmdArgs.push_back("-fsigned-wchar");
2922
22
    }
2923
26
  }
2924
29.5k
}
2925
2926
static void RenderObjCOptions(const ToolChain &TC, const Driver &D,
2927
                              const llvm::Triple &T, const ArgList &Args,
2928
                              ObjCRuntime &Runtime, bool InferCovariantReturns,
2929
29.5k
                              const InputInfo &Input, ArgStringList &CmdArgs) {
2930
29.5k
  const llvm::Triple::ArchType Arch = TC.getArch();
2931
29.5k
2932
29.5k
  // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and legacy
2933
29.5k
  // is the default. Except for deployment target of 10.5, next runtime is
2934
29.5k
  // always legacy dispatch and -fno-objc-legacy-dispatch gets ignored silently.
2935
29.5k
  if (Runtime.isNonFragile()) {
2936
21.2k
    if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
2937
21.2k
                      options::OPT_fno_objc_legacy_dispatch,
2938
21.2k
                      Runtime.isLegacyDispatchDefaultForArch(Arch))) {
2939
660
      if (TC.UseObjCMixedDispatch())
2940
2
        CmdArgs.push_back("-fobjc-dispatch-method=mixed");
2941
658
      else
2942
658
        CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
2943
660
    }
2944
21.2k
  }
2945
29.5k
2946
29.5k
  // When ObjectiveC legacy runtime is in effect on MacOSX, turn on the option
2947
29.5k
  // to do Array/Dictionary subscripting by default.
2948
29.5k
  if (Arch == llvm::Triple::x86 && 
T.isMacOSX()3.24k
&&
2949
29.5k
      
Runtime.getKind() == ObjCRuntime::FragileMacOSX625
&&
Runtime.isNeXTFamily()259
)
2950
259
    CmdArgs.push_back("-fobjc-subscripting-legacy-runtime");
2951
29.5k
2952
29.5k
  // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
2953
29.5k
  // NOTE: This logic is duplicated in ToolChains.cpp.
2954
29.5k
  if (isObjCAutoRefCount(Args)) {
2955
29
    TC.CheckObjCARC();
2956
29
2957
29
    CmdArgs.push_back("-fobjc-arc");
2958
29
2959
29
    // FIXME: It seems like this entire block, and several around it should be
2960
29
    // wrapped in isObjC, but for now we just use it here as this is where it
2961
29
    // was being used previously.
2962
29
    if (types::isCXX(Input.getType()) && 
types::isObjC(Input.getType())3
) {
2963
2
      if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
2964
1
        CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
2965
1
      else
2966
1
        CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
2967
2
    }
2968
29
2969
29
    // Allow the user to enable full exceptions code emission.
2970
29
    // We default off for Objective-C, on for Objective-C++.
2971
29
    if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
2972
29
                     options::OPT_fno_objc_arc_exceptions,
2973
29
                     /*Default=*/types::isCXX(Input.getType())))
2974
3
      CmdArgs.push_back("-fobjc-arc-exceptions");
2975
29
  }
2976
29.5k
2977
29.5k
  // Silence warning for full exception code emission options when explicitly
2978
29.5k
  // set to use no ARC.
2979
29.5k
  if (Args.hasArg(options::OPT_fno_objc_arc)) {
2980
5
    Args.ClaimAllArgs(options::OPT_fobjc_arc_exceptions);
2981
5
    Args.ClaimAllArgs(options::OPT_fno_objc_arc_exceptions);
2982
5
  }
2983
29.5k
2984
29.5k
  // Allow the user to control whether messages can be converted to runtime
2985
29.5k
  // functions.
2986
29.5k
  if (types::isObjC(Input.getType())) {
2987
709
    auto *Arg = Args.getLastArg(
2988
709
        options::OPT_fobjc_convert_messages_to_runtime_calls,
2989
709
        options::OPT_fno_objc_convert_messages_to_runtime_calls);
2990
709
    if (Arg &&
2991
709
        Arg->getOption().matches(
2992
2
            options::OPT_fno_objc_convert_messages_to_runtime_calls))
2993
1
      CmdArgs.push_back("-fno-objc-convert-messages-to-runtime-calls");
2994
709
  }
2995
29.5k
2996
29.5k
  // -fobjc-infer-related-result-type is the default, except in the Objective-C
2997
29.5k
  // rewriter.
2998
29.5k
  if (InferCovariantReturns)
2999
8
    CmdArgs.push_back("-fno-objc-infer-related-result-type");
3000
29.5k
3001
29.5k
  // Pass down -fobjc-weak or -fno-objc-weak if present.
3002
29.5k
  if (types::isObjC(Input.getType())) {
3003
709
    auto WeakArg =
3004
709
        Args.getLastArg(options::OPT_fobjc_weak, options::OPT_fno_objc_weak);
3005
709
    if (!WeakArg) {
3006
697
      // nothing to do
3007
697
    } else 
if (12
!Runtime.allowsWeak()12
) {
3008
4
      if (WeakArg->getOption().matches(options::OPT_fobjc_weak))
3009
4
        D.Diag(diag::err_objc_weak_unsupported);
3010
8
    } else {
3011
8
      WeakArg->render(Args, CmdArgs);
3012
8
    }
3013
709
  }
3014
29.5k
}
3015
3016
static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args,
3017
29.5k
                                     ArgStringList &CmdArgs) {
3018
29.5k
  bool CaretDefault = true;
3019
29.5k
  bool ColumnDefault = true;
3020
29.5k
3021
29.5k
  if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diagnostics_classic,
3022
5
                                     options::OPT__SLASH_diagnostics_column,
3023
5
                                     options::OPT__SLASH_diagnostics_caret)) {
3024
5
    switch (A->getOption().getID()) {
3025
5
    case options::OPT__SLASH_diagnostics_caret:
3026
2
      CaretDefault = true;
3027
2
      ColumnDefault = true;
3028
2
      break;
3029
5
    case options::OPT__SLASH_diagnostics_column:
3030
1
      CaretDefault = false;
3031
1
      ColumnDefault = true;
3032
1
      break;
3033
5
    case options::OPT__SLASH_diagnostics_classic:
3034
2
      CaretDefault = false;
3035
2
      ColumnDefault = false;
3036
2
      break;
3037
29.5k
    }
3038
29.5k
  }
3039
29.5k
3040
29.5k
  // -fcaret-diagnostics is default.
3041
29.5k
  if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
3042
29.5k
                    options::OPT_fno_caret_diagnostics, CaretDefault))
3043
3
    CmdArgs.push_back("-fno-caret-diagnostics");
3044
29.5k
3045
29.5k
  // -fdiagnostics-fixit-info is default, only pass non-default.
3046
29.5k
  if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
3047
29.5k
                    options::OPT_fno_diagnostics_fixit_info))
3048
0
    CmdArgs.push_back("-fno-diagnostics-fixit-info");
3049
29.5k
3050
29.5k
  // Enable -fdiagnostics-show-option by default.
3051
29.5k
  if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
3052
29.5k
                   options::OPT_fno_diagnostics_show_option))
3053
29.5k
    CmdArgs.push_back("-fdiagnostics-show-option");
3054
29.5k
3055
29.5k
  if (const Arg *A =
3056
0
          Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
3057
0
    CmdArgs.push_back("-fdiagnostics-show-category");
3058
0
    CmdArgs.push_back(A->getValue());
3059
0
  }
3060
29.5k
3061
29.5k
  if (Args.hasFlag(options::OPT_fdiagnostics_show_hotness,
3062
29.5k
                   options::OPT_fno_diagnostics_show_hotness, false))
3063
4
    CmdArgs.push_back("-fdiagnostics-show-hotness");
3064
29.5k
3065
29.5k
  if (const Arg *A =
3066
0
          Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) {
3067
0
    std::string Opt =
3068
0
        std::string("-fdiagnostics-hotness-threshold=") + A->getValue();
3069
0
    CmdArgs.push_back(Args.MakeArgString(Opt));
3070
0
  }
3071
29.5k
3072
29.5k
  if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
3073
20
    CmdArgs.push_back("-fdiagnostics-format");
3074
20
    CmdArgs.push_back(A->getValue());
3075
20
  }
3076
29.5k
3077
29.5k
  if (const Arg *A = Args.getLastArg(
3078
0
          options::OPT_fdiagnostics_show_note_include_stack,
3079
0
          options::OPT_fno_diagnostics_show_note_include_stack)) {
3080
0
    const Option &O = A->getOption();
3081
0
    if (O.matches(options::OPT_fdiagnostics_show_note_include_stack))
3082
0
      CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
3083
0
    else
3084
0
      CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
3085
0
  }
3086
29.5k
3087
29.5k
  // Color diagnostics are parsed by the driver directly from argv and later
3088
29.5k
  // re-parsed to construct this job; claim any possible color diagnostic here
3089
29.5k
  // to avoid warn_drv_unused_argument and diagnose bad
3090
29.5k
  // OPT_fdiagnostics_color_EQ values.
3091
760k
  for (const Arg *A : Args) {
3092
760k
    const Option &O = A->getOption();
3093
760k
    if (!O.matches(options::OPT_fcolor_diagnostics) &&
3094
760k
        
!O.matches(options::OPT_fdiagnostics_color)760k
&&
3095
760k
        
!O.matches(options::OPT_fno_color_diagnostics)760k
&&
3096
760k
        
!O.matches(options::OPT_fno_diagnostics_color)760k
&&
3097
760k
        
!O.matches(options::OPT_fdiagnostics_color_EQ)760k
)
3098
760k
      continue;
3099
29
3100
29
    if (O.matches(options::OPT_fdiagnostics_color_EQ)) {
3101
6
      StringRef Value(A->getValue());
3102
6
      if (Value != "always" && 
Value != "never"4
&&
Value != "auto"3
)
3103
1
        D.Diag(diag::err_drv_clang_unsupported)
3104
1
            << ("-fdiagnostics-color=" + Value).str();
3105
6
    }
3106
29
    A->claim();
3107
29
  }
3108
29.5k
3109
29.5k
  if (D.getDiags().getDiagnosticOptions().ShowColors)
3110
9
    CmdArgs.push_back("-fcolor-diagnostics");
3111
29.5k
3112
29.5k
  if (Args.hasArg(options::OPT_fansi_escape_codes))
3113
1
    CmdArgs.push_back("-fansi-escape-codes");
3114
29.5k
3115
29.5k
  if (!Args.hasFlag(options::OPT_fshow_source_location,
3116
29.5k
                    options::OPT_fno_show_source_location))
3117
1
    CmdArgs.push_back("-fno-show-source-location");
3118
29.5k
3119
29.5k
  if (Args.hasArg(options::OPT_fdiagnostics_absolute_paths))
3120
0
    CmdArgs.push_back("-fdiagnostics-absolute-paths");
3121
29.5k
3122
29.5k
  if (!Args.hasFlag(options::OPT_fshow_column, options::OPT_fno_show_column,
3123
29.5k
                    ColumnDefault))
3124
4
    CmdArgs.push_back("-fno-show-column");
3125
29.5k
3126
29.5k
  if (!Args.hasFlag(options::OPT_fspell_checking,
3127
29.5k
                    options::OPT_fno_spell_checking))
3128
969
    CmdArgs.push_back("-fno-spell-checking");
3129
29.5k
}
3130
3131
enum class DwarfFissionKind { None, Split, Single };
3132
3133
static DwarfFissionKind getDebugFissionKind(const Driver &D,
3134
30.6k
                                            const ArgList &Args, Arg *&Arg) {
3135
30.6k
  Arg =
3136
30.6k
      Args.getLastArg(options::OPT_gsplit_dwarf, options::OPT_gsplit_dwarf_EQ);
3137
30.6k
  if (!Arg)
3138
30.5k
    return DwarfFissionKind::None;
3139
40
3140
40
  if (Arg->getOption().matches(options::OPT_gsplit_dwarf))
3141
33
    return DwarfFissionKind::Split;
3142
7
3143
7
  StringRef Value = Arg->getValue();
3144
7
  if (Value == "split")
3145
4
    return DwarfFissionKind::Split;
3146
3
  if (Value == "single")
3147
3
    return DwarfFissionKind::Single;
3148
0
3149
0
  D.Diag(diag::err_drv_unsupported_option_argument)
3150
0
      << Arg->getOption().getName() << Arg->getValue();
3151
0
  return DwarfFissionKind::None;
3152
0
}
3153
3154
static void RenderDebugOptions(const ToolChain &TC, const Driver &D,
3155
                               const llvm::Triple &T, const ArgList &Args,
3156
                               bool EmitCodeView, bool IsWindowsMSVC,
3157
                               ArgStringList &CmdArgs,
3158
                               codegenoptions::DebugInfoKind &DebugInfoKind,
3159
29.5k
                               DwarfFissionKind &DwarfFission) {
3160
29.5k
  if (Args.hasFlag(options::OPT_fdebug_info_for_profiling,
3161
29.5k
                   options::OPT_fno_debug_info_for_profiling, false) &&
3162
29.5k
      checkDebugInfoOption(
3163
6
          Args.getLastArg(options::OPT_fdebug_info_for_profiling), Args, D, TC))
3164
4
    CmdArgs.push_back("-fdebug-info-for-profiling");
3165
29.5k
3166
29.5k
  // The 'g' groups options involve a somewhat intricate sequence of decisions
3167
29.5k
  // about what to pass from the driver to the frontend, but by the time they
3168
29.5k
  // reach cc1 they've been factored into three well-defined orthogonal choices:
3169
29.5k
  //  * what level of debug info to generate
3170
29.5k
  //  * what dwarf version to write
3171
29.5k
  //  * what debugger tuning to use
3172
29.5k
  // This avoids having to monkey around further in cc1 other than to disable
3173
29.5k
  // codeview if not running in a Windows environment. Perhaps even that
3174
29.5k
  // decision should be made in the driver as well though.
3175
29.5k
  unsigned DWARFVersion = 0;
3176
29.5k
  llvm::DebuggerKind DebuggerTuning = TC.getDefaultDebuggerTuning();
3177
29.5k
3178
29.5k
  bool SplitDWARFInlining =
3179
29.5k
      Args.hasFlag(options::OPT_fsplit_dwarf_inlining,
3180
29.5k
                   options::OPT_fno_split_dwarf_inlining, true);
3181
29.5k
3182
29.5k
  Args.ClaimAllArgs(options::OPT_g_Group);
3183
29.5k
3184
29.5k
  Arg* SplitDWARFArg;
3185
29.5k
  DwarfFission = getDebugFissionKind(D, Args, SplitDWARFArg);
3186
29.5k
3187
29.5k
  if (DwarfFission != DwarfFissionKind::None &&
3188
29.5k
      
!checkDebugInfoOption(SplitDWARFArg, Args, D, TC)35
) {
3189
2
    DwarfFission = DwarfFissionKind::None;
3190
2
    SplitDWARFInlining = false;
3191
2
  }
3192
29.5k
3193
29.5k
  if (const Arg *A =
3194
2.60k
          Args.getLastArg(options::OPT_g_Group, options::OPT_gsplit_dwarf,
3195
2.60k
                          options::OPT_gsplit_dwarf_EQ)) {
3196
2.60k
    DebugInfoKind = codegenoptions::LimitedDebugInfo;
3197
2.60k
3198
2.60k
    // If the last option explicitly specified a debug-info level, use it.
3199
2.60k
    if (checkDebugInfoOption(A, Args, D, TC) &&
3200
2.60k
        
A->getOption().matches(options::OPT_gN_Group)2.59k
) {
3201
2.32k
      DebugInfoKind = DebugLevelToInfoKind(*A);
3202
2.32k
      // For -g0 or -gline-tables-only, drop -gsplit-dwarf. This gets a bit more
3203
2.32k
      // complicated if you've disabled inline info in the skeleton CUs
3204
2.32k
      // (SplitDWARFInlining) - then there's value in composing split-dwarf and
3205
2.32k
      // line-tables-only, so let those compose naturally in that case.
3206
2.32k
      if (DebugInfoKind == codegenoptions::NoDebugInfo ||
3207
2.32k
          
DebugInfoKind == codegenoptions::DebugDirectivesOnly2.30k
||
3208
2.32k
          
(2.29k
DebugInfoKind == codegenoptions::DebugLineTablesOnly2.29k
&&
3209
2.29k
           
SplitDWARFInlining2.24k
))
3210
2.27k
        DwarfFission = DwarfFissionKind::None;
3211
2.32k
    }
3212
2.60k
  }
3213
29.5k
3214
29.5k
  // If a debugger tuning argument appeared, remember it.
3215
29.5k
  if (const Arg *A =
3216
69
          Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) {
3217
69
    if (checkDebugInfoOption(A, Args, D, TC)) {
3218
66
      if (A->getOption().matches(options::OPT_glldb))
3219
10
        DebuggerTuning = llvm::DebuggerKind::LLDB;
3220
56
      else if (A->getOption().matches(options::OPT_gsce))
3221
2
        DebuggerTuning = llvm::DebuggerKind::SCE;
3222
54
      else
3223
54
        DebuggerTuning = llvm::DebuggerKind::GDB;
3224
66
    }
3225
69
  }
3226
29.5k
3227
29.5k
  // If a -gdwarf argument appeared, remember it.
3228
29.5k
  if (const Arg *A =
3229
45
          Args.getLastArg(options::OPT_gdwarf_2, options::OPT_gdwarf_3,
3230
45
                          options::OPT_gdwarf_4, options::OPT_gdwarf_5))
3231
45
    if (checkDebugInfoOption(A, Args, D, TC))
3232
45
      DWARFVersion = DwarfVersionNum(A->getSpelling());
3233
29.5k
3234
29.5k
  if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) {
3235
15
    if (checkDebugInfoOption(A, Args, D, TC))
3236
13
      EmitCodeView = true;
3237
15
  }
3238
29.5k
3239
29.5k
  // If the user asked for debug info but did not explicitly specify -gcodeview
3240
29.5k
  // or -gdwarf, ask the toolchain for the default format.
3241
29.5k
  if (!EmitCodeView && 
DWARFVersion == 029.5k
&&
3242
29.5k
      
DebugInfoKind != codegenoptions::NoDebugInfo29.5k
) {
3243
2.52k
    switch (TC.getDefaultDebugFormat()) {
3244
2.52k
    case codegenoptions::DIF_CodeView:
3245
3
      EmitCodeView = true;
3246
3
      break;
3247
2.52k
    case codegenoptions::DIF_DWARF:
3248
2.52k
      DWARFVersion = TC.GetDefaultDwarfVersion();
3249
2.52k
      break;
3250
29.5k
    }
3251
29.5k
  }
3252
29.5k
3253
29.5k
  // -gline-directives-only supported only for the DWARF debug info.
3254
29.5k
  if (DWARFVersion == 0 && 
DebugInfoKind == codegenoptions::DebugDirectivesOnly27.0k
)
3255
0
    DebugInfoKind = codegenoptions::NoDebugInfo;
3256
29.5k
3257
29.5k
  // We ignore flag -gstrict-dwarf for now.
3258
29.5k
  // And we handle flag -grecord-gcc-switches later with DWARFDebugFlags.
3259
29.5k
  Args.ClaimAllArgs(options::OPT_g_flags_Group);
3260
29.5k
3261
29.5k
  // Column info is included by default for everything except SCE and
3262
29.5k
  // CodeView. Clang doesn't track end columns, just starting columns, which,
3263
29.5k
  // in theory, is fine for CodeView (and PDB).  In practice, however, the
3264
29.5k
  // Microsoft debuggers don't handle missing end columns well, so it's better
3265
29.5k
  // not to include any column info.
3266
29.5k
  if (const Arg *A = Args.getLastArg(options::OPT_gcolumn_info))
3267
9
    (void)checkDebugInfoOption(A, Args, D, TC);
3268
29.5k
  if (Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info,
3269
29.5k
                   /*Default=*/!EmitCodeView &&
3270
29.5k
                       
DebuggerTuning != llvm::DebuggerKind::SCE29.5k
))
3271
29.4k
    CmdArgs.push_back("-dwarf-column-info");
3272
29.5k
3273
29.5k
  // FIXME: Move backend command line options to the module.
3274
29.5k
  // If -gline-tables-only or -gline-directives-only is the last option it wins.
3275
29.5k
  if (const Arg *A = Args.getLastArg(options::OPT_gmodules))
3276
14
    if (checkDebugInfoOption(A, Args, D, TC)) {
3277
12
      if (DebugInfoKind != codegenoptions::DebugLineTablesOnly &&
3278
12
          
DebugInfoKind != codegenoptions::DebugDirectivesOnly11
) {
3279
10
        DebugInfoKind = codegenoptions::LimitedDebugInfo;
3280
10
        CmdArgs.push_back("-dwarf-ext-refs");
3281
10
        CmdArgs.push_back("-fmodule-format=obj");
3282
10
      }
3283
12
    }
3284
29.5k
3285
29.5k
  if (T.isOSBinFormatELF() && 
!SplitDWARFInlining7.87k
)
3286
7
    CmdArgs.push_back("-fno-split-dwarf-inlining");
3287
29.5k
3288
29.5k
  // After we've dealt with all combinations of things that could
3289
29.5k
  // make DebugInfoKind be other than None or DebugLineTablesOnly,
3290
29.5k
  // figure out if we need to "upgrade" it to standalone debug info.
3291
29.5k
  // We parse these two '-f' options whether or not they will be used,
3292
29.5k
  // to claim them even if you wrote "-fstandalone-debug -gline-tables-only"
3293
29.5k
  bool NeedFullDebug = Args.hasFlag(
3294
29.5k
      options::OPT_fstandalone_debug, options::OPT_fno_standalone_debug,
3295
29.5k
      DebuggerTuning == llvm::DebuggerKind::LLDB ||
3296
29.5k
          
TC.GetDefaultStandaloneDebug()9.55k
);
3297
29.5k
  if (const Arg *A = Args.getLastArg(options::OPT_fstandalone_debug))
3298
0
    (void)checkDebugInfoOption(A, Args, D, TC);
3299
29.5k
  if (DebugInfoKind == codegenoptions::LimitedDebugInfo && 
NeedFullDebug339
)
3300
142
    DebugInfoKind = codegenoptions::FullDebugInfo;
3301
29.5k
3302
29.5k
  if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source,
3303
29.5k
                   false)) {
3304
7
    // Source embedding is a vendor extension to DWARF v5. By now we have
3305
7
    // checked if a DWARF version was stated explicitly, and have otherwise
3306
7
    // fallen back to the target default, so if this is still not at least 5
3307
7
    // we emit an error.
3308
7
    const Arg *A = Args.getLastArg(options::OPT_gembed_source);
3309
7
    if (DWARFVersion < 5)
3310
1
      D.Diag(diag::err_drv_argument_only_allowed_with)
3311
1
          << A->getAsString(Args) << "-gdwarf-5";
3312
6
    else if (checkDebugInfoOption(A, Args, D, TC))
3313
4
      CmdArgs.push_back("-gembed-source");
3314
7
  }
3315
29.5k
3316
29.5k
  if (EmitCodeView) {
3317
32
    CmdArgs.push_back("-gcodeview");
3318
32
3319
32
    // Emit codeview type hashes if requested.
3320
32
    if (Args.hasFlag(options::OPT_gcodeview_ghash,
3321
32
                     options::OPT_gno_codeview_ghash, false)) {
3322
2
      CmdArgs.push_back("-gcodeview-ghash");
3323
2
    }
3324
32
  }
3325
29.5k
3326
29.5k
  // Adjust the debug info kind for the given toolchain.
3327
29.5k
  TC.adjustDebugInfoKind(DebugInfoKind, Args);
3328
29.5k
3329
29.5k
  RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DWARFVersion,
3330
29.5k
                          DebuggerTuning);
3331
29.5k
3332
29.5k
  // -fdebug-macro turns on macro debug info generation.
3333
29.5k
  if (Args.hasFlag(options::OPT_fdebug_macro, options::OPT_fno_debug_macro,
3334
29.5k
                   false))
3335
6
    if (checkDebugInfoOption(Args.getLastArg(options::OPT_fdebug_macro), Args,
3336
6
                             D, TC))
3337
4
      CmdArgs.push_back("-debug-info-macro");
3338
29.5k
3339
29.5k
  // -ggnu-pubnames turns on gnu style pubnames in the backend.
3340
29.5k
  const auto *PubnamesArg =
3341
29.5k
      Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames,
3342
29.5k
                      options::OPT_gpubnames, options::OPT_gno_pubnames);
3343
29.5k
  if (DwarfFission != DwarfFissionKind::None ||
3344
29.5k
      
DebuggerTuning == llvm::DebuggerKind::LLDB29.5k
||
3345
29.5k
      
(9.53k
PubnamesArg9.53k
&&
checkDebugInfoOption(PubnamesArg, Args, D, TC)5
))
3346
20.0k
    if (!PubnamesArg ||
3347
20.0k
        
(11
!PubnamesArg->getOption().matches(options::OPT_gno_gnu_pubnames)11
&&
3348
11
         
!PubnamesArg->getOption().matches(options::OPT_gno_pubnames)9
))
3349
20.0k
      CmdArgs.push_back(PubnamesArg && PubnamesArg->getOption().matches(
3350
5
                                           options::OPT_gpubnames)
3351
20.0k
                            ? 
"-gpubnames"1
3352
20.0k
                            : 
"-ggnu-pubnames"20.0k
);
3353
29.5k
3354
29.5k
  if (Args.hasFlag(options::OPT_fdebug_ranges_base_address,
3355
29.5k
                   options::OPT_fno_debug_ranges_base_address, false)) {
3356
1
    CmdArgs.push_back("-fdebug-ranges-base-address");
3357
1
  }
3358
29.5k
3359
29.5k
  // -gdwarf-aranges turns on the emission of the aranges section in the
3360
29.5k
  // backend.
3361
29.5k
  // Always enabled for SCE tuning.
3362
29.5k
  bool NeedAranges = DebuggerTuning == llvm::DebuggerKind::SCE;
3363
29.5k
  if (const Arg *A = Args.getLastArg(options::OPT_gdwarf_aranges))
3364
6
    NeedAranges = checkDebugInfoOption(A, Args, D, TC) || 
NeedAranges2
;
3365
29.5k
  if (NeedAranges) {
3366
121
    CmdArgs.push_back("-mllvm");
3367
121
    CmdArgs.push_back("-generate-arange-section");
3368
121
  }
3369
29.5k
3370
29.5k
  if (Args.hasFlag(options::OPT_fdebug_types_section,
3371
29.5k
                   options::OPT_fno_debug_types_section, false)) {
3372
7
    if (!T.isOSBinFormatELF()) {
3373
1
      D.Diag(diag::err_drv_unsupported_opt_for_target)
3374
1
          << Args.getLastArg(options::OPT_fdebug_types_section)
3375
1
                 ->getAsString(Args)
3376
1
          << T.getTriple();
3377
6
    } else if (checkDebugInfoOption(
3378
6
                   Args.getLastArg(options::OPT_fdebug_types_section), Args, D,
3379
6
                   TC)) {
3380
4
      CmdArgs.push_back("-mllvm");
3381
4
      CmdArgs.push_back("-generate-type-units");
3382
4
    }
3383
7
  }
3384
29.5k
3385
29.5k
  // Decide how to render forward declarations of template instantiations.
3386
29.5k
  // SCE wants full descriptions, others just get them in the name.
3387
29.5k
  if (DebuggerTuning == llvm::DebuggerKind::SCE)
3388
117
    CmdArgs.push_back("-debug-forward-template-params");
3389
29.5k
3390
29.5k
  // Do we need to explicitly import anonymous namespaces into the parent
3391
29.5k
  // scope?
3392
29.5k
  if (DebuggerTuning == llvm::DebuggerKind::SCE)
3393
117
    CmdArgs.push_back("-dwarf-explicit-import");
3394
29.5k
3395
29.5k
  RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC);
3396
29.5k
}
3397
3398
void Clang::ConstructJob(Compilation &C, const JobAction &JA,
3399
                         const InputInfo &Output, const InputInfoList &Inputs,
3400
29.6k
                         const ArgList &Args, const char *LinkingOutput) const {
3401
29.6k
  const auto &TC = getToolChain();
3402
29.6k
  const llvm::Triple &RawTriple = TC.getTriple();
3403
29.6k
  const llvm::Triple &Triple = TC.getEffectiveTriple();
3404
29.6k
  const std::string &TripleStr = Triple.getTriple();
3405
29.6k
3406
29.6k
  bool KernelOrKext =
3407
29.6k
      Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
3408
29.6k
  const Driver &D = TC.getDriver();
3409
29.6k
  ArgStringList CmdArgs;
3410
29.6k
3411
29.6k
  // Check number of inputs for sanity. We need at least one input.
3412
29.6k
  assert(Inputs.size() >= 1 && "Must have at least one input.");
3413
29.6k
  // CUDA/HIP compilation may have multiple inputs (source file + results of
3414
29.6k
  // device-side compilations). OpenMP device jobs also take the host IR as a
3415
29.6k
  // second input. Module precompilation accepts a list of header files to
3416
29.6k
  // include as part of the module. All other jobs are expected to have exactly
3417
29.6k
  // one input.
3418
29.6k
  bool IsCuda = JA.isOffloading(Action::OFK_Cuda);
3419
29.6k
  bool IsHIP = JA.isOffloading(Action::OFK_HIP);
3420
29.6k
  bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP);
3421
29.6k
  bool IsHeaderModulePrecompile = isa<HeaderModulePrecompileJobAction>(JA);
3422
29.6k
3423
29.6k
  // A header module compilation doesn't have a main input file, so invent a
3424
29.6k
  // fake one as a placeholder.
3425
29.6k
  const char *ModuleName = [&]{
3426
29.6k
    auto *ModuleNameArg = Args.getLastArg(options::OPT_fmodule_name_EQ);
3427
29.6k
    return ModuleNameArg ? 
ModuleNameArg->getValue()4
:
""29.6k
;
3428
29.6k
  }();
3429
29.6k
  InputInfo HeaderModuleInput(Inputs[0].getType(), ModuleName, ModuleName);
3430
29.6k
3431
29.6k
  const InputInfo &Input =
3432
29.6k
      IsHeaderModulePrecompile ? 
HeaderModuleInput3
:
Inputs[0]29.6k
;
3433
29.6k
3434
29.6k
  InputInfoList ModuleHeaderInputs;
3435
29.6k
  const InputInfo *CudaDeviceInput = nullptr;
3436
29.6k
  const InputInfo *OpenMPDeviceInput = nullptr;
3437
29.8k
  for (const InputInfo &I : Inputs) {
3438
29.8k
    if (&I == &Input) {
3439
29.6k
      // This is the primary input.
3440
29.6k
    } else 
if (254
IsHeaderModulePrecompile254
&&
3441
254
               
types::getPrecompiledType(I.getType()) == types::TY_PCH7
) {
3442
7
      types::ID Expected = HeaderModuleInput.getType();
3443
7
      if (I.getType() != Expected) {
3444
0
        D.Diag(diag::err_drv_module_header_wrong_kind)
3445
0
            << I.getFilename() << types::getTypeName(I.getType())
3446
0
            << types::getTypeName(Expected);
3447
0
      }
3448
7
      ModuleHeaderInputs.push_back(I);
3449
247
    } else if ((IsCuda || 
IsHIP87
) &&
!CudaDeviceInput165
) {
3450
165
      CudaDeviceInput = &I;
3451
165
    } else 
if (82
IsOpenMPDevice82
&&
!OpenMPDeviceInput82
) {
3452
82
      OpenMPDeviceInput = &I;
3453
82
    } else {
3454
0
      llvm_unreachable("unexpectedly given multiple inputs");
3455
0
    }
3456
29.8k
  }
3457
29.6k
3458
29.6k
  const llvm::Triple *AuxTriple = IsCuda ? 
TC.getAuxTriple()440
:
nullptr29.1k
;
3459
29.6k
  bool IsWindowsMSVC = RawTriple.isWindowsMSVCEnvironment();
3460
29.6k
  bool IsIAMCU = RawTriple.isOSIAMCU();
3461
29.6k
3462
29.6k
  // Adjust IsWindowsXYZ for CUDA/HIP compilations.  Even when compiling in
3463
29.6k
  // device mode (i.e., getToolchain().getTriple() is NVPTX/AMDGCN, not
3464
29.6k
  // Windows), we need to pass Windows-specific flags to cc1.
3465
29.6k
  if (IsCuda || 
IsHIP29.1k
)
3466
480
    IsWindowsMSVC |= AuxTriple && 
AuxTriple->isWindowsMSVCEnvironment()237
;
3467
29.6k
3468
29.6k
  // C++ is not supported for IAMCU.
3469
29.6k
  if (IsIAMCU && 
types::isCXX(Input.getType())9
)
3470
1
    D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU";
3471
29.6k
3472
29.6k
  // Invoke ourselves in -cc1 mode.
3473
29.6k
  //
3474
29.6k
  // FIXME: Implement custom jobs for internal actions.
3475
29.6k
  CmdArgs.push_back("-cc1");
3476
29.6k
3477
29.6k
  // Add the "effective" target triple.
3478
29.6k
  CmdArgs.push_back("-triple");
3479
29.6k
  CmdArgs.push_back(Args.MakeArgString(TripleStr));
3480
29.6k
3481
29.6k
  if (const Arg *MJ = Args.getLastArg(options::OPT_MJ)) {
3482
3
    DumpCompilationDatabase(C, MJ->getValue(), TripleStr, Output, Input, Args);
3483
3
    Args.ClaimAllArgs(options::OPT_MJ);
3484
3
  }
3485
29.6k
3486
29.6k
  if (IsCuda || 
IsHIP29.1k
) {
3487
480
    // We have to pass the triple of the host if compiling for a CUDA/HIP device
3488
480
    // and vice-versa.
3489
480
    std::string NormalizedTriple;
3490
480
    if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
3491
480
        
JA.isDeviceOffloading(Action::OFK_HIP)243
)
3492
263
      NormalizedTriple = C.getSingleOffloadToolChain<Action::OFK_Host>()
3493
263
                             ->getTriple()
3494
263
                             .normalize();
3495
217
    else {
3496
217
      // Host-side compilation.
3497
217
      NormalizedTriple =
3498
217
          (IsCuda ? 
C.getSingleOffloadToolChain<Action::OFK_Cuda>()203
3499
217
                  : 
C.getSingleOffloadToolChain<Action::OFK_HIP>()14
)
3500
217
              ->getTriple()
3501
217
              .normalize();
3502
217
      if (IsCuda) {
3503
203
        // We need to figure out which CUDA version we're compiling for, as that
3504
203
        // determines how we load and launch GPU kernels.
3505
203
        auto *CTC = static_cast<const toolchains::CudaToolChain *>(
3506
203
            C.getSingleOffloadToolChain<Action::OFK_Cuda>());
3507
203
        assert(CTC && "Expected valid CUDA Toolchain.");
3508
203
        if (CTC && CTC->CudaInstallation.version() != CudaVersion::UNKNOWN)
3509
30
          CmdArgs.push_back(Args.MakeArgString(
3510
30
              Twine("-target-sdk-version=") +
3511
30
              CudaVersionToString(CTC->CudaInstallation.version())));
3512
203
      }
3513
217
    }
3514
480
    CmdArgs.push_back("-aux-triple");
3515
480
    CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
3516
480
  }
3517
29.6k
3518
29.6k
  if (IsOpenMPDevice) {
3519
117
    // We have to pass the triple of the host if compiling for an OpenMP device.
3520
117
    std::string NormalizedTriple =
3521
117
        C.getSingleOffloadToolChain<Action::OFK_Host>()
3522
117
            ->getTriple()
3523
117
            .normalize();
3524
117
    CmdArgs.push_back("-aux-triple");
3525
117
    CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
3526
117
  }
3527
29.6k
3528
29.6k
  if (Triple.isOSWindows() && 
(799
Triple.getArch() == llvm::Triple::arm799
||
3529
799
                               Triple.getArch() == llvm::Triple::thumb)) {
3530
59
    unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 
40
: 6;
3531
59
    unsigned Version;
3532
59
    Triple.getArchName().substr(Offset).getAsInteger(10, Version);
3533
59
    if (Version < 7)
3534
1
      D.Diag(diag::err_target_unsupported_arch) << Triple.getArchName()
3535
1
                                                << TripleStr;
3536
59
  }
3537
29.6k
3538
29.6k
  // Push all default warning arguments that are specific to
3539
29.6k
  // the given target.  These come before user provided warning options
3540
29.6k
  // are provided.
3541
29.6k
  TC.addClangWarningOptions(CmdArgs);
3542
29.6k
3543
29.6k
  // Select the appropriate action.
3544
29.6k
  RewriteKind rewriteKind = RK_None;
3545
29.6k
3546
29.6k
  if (isa<AnalyzeJobAction>(JA)) {
3547
17
    assert(JA.getType() == types::TY_Plist && "Invalid output type.");
3548
17
    CmdArgs.push_back("-analyze");
3549
29.5k
  } else if (isa<MigrateJobAction>(JA)) {
3550
0
    CmdArgs.push_back("-migrate");
3551
29.5k
  } else if (isa<PreprocessJobAction>(JA)) {
3552
1.80k
    if (Output.getType() == types::TY_Dependencies)
3553
11
      CmdArgs.push_back("-Eonly");
3554
1.79k
    else {
3555
1.79k
      CmdArgs.push_back("-E");
3556
1.79k
      if (Args.hasArg(options::OPT_rewrite_objc) &&
3557
1.79k
          
!Args.hasArg(options::OPT_g_Group)5
)
3558
4
        CmdArgs.push_back("-P");
3559
1.79k
    }
3560
27.7k
  } else if (isa<AssembleJobAction>(JA)) {
3561
17.2k
    CmdArgs.push_back("-emit-obj");
3562
17.2k
3563
17.2k
    CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
3564
17.2k
3565
17.2k
    // Also ignore explicit -force_cpusubtype_ALL option.
3566
17.2k
    (void)Args.hasArg(options::OPT_force__cpusubtype__ALL);
3567
17.2k
  } else 
if (10.5k
isa<PrecompileJobAction>(JA)10.5k
) {
3568
68
    if (JA.getType() == types::TY_Nothing)
3569
30
      CmdArgs.push_back("-fsyntax-only");
3570
38
    else if (JA.getType() == types::TY_ModuleFile)
3571
8
      CmdArgs.push_back(IsHeaderModulePrecompile
3572
8
                            ? 
"-emit-header-module"2
3573
8
                            : 
"-emit-module-interface"6
);
3574
30
    else
3575
30
      CmdArgs.push_back("-emit-pch");
3576
10.5k
  } else if (isa<VerifyPCHJobAction>(JA)) {
3577
2
    CmdArgs.push_back("-verify-pch");
3578
10.5k
  } else {
3579
10.5k
    assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) &&
3580
10.5k
           "Invalid action for clang tool.");
3581
10.5k
    if (JA.getType() == types::TY_Nothing) {
3582
8.46k
      CmdArgs.push_back("-fsyntax-only");
3583
8.46k
    } else 
if (2.03k
JA.getType() == types::TY_LLVM_IR2.03k
||
3584
2.03k
               
JA.getType() == types::TY_LTO_IR1.71k
) {
3585
332
      CmdArgs.push_back("-emit-llvm");
3586
1.70k
    } else if (JA.getType() == types::TY_LLVM_BC ||
3587
1.70k
               
JA.getType() == types::TY_LTO_BC1.48k
) {
3588
316
      CmdArgs.push_back("-emit-llvm-bc");
3589
1.39k
    } else if (JA.getType() == types::TY_IFS) {
3590
35
      StringRef StubFormat =
3591
35
          llvm::StringSwitch<StringRef>(
3592
35
              Args.hasArg(options::OPT_iterface_stub_version_EQ)
3593
35
                  ? Args.getLastArgValue(options::OPT_iterface_stub_version_EQ)
3594
35
                  : 
""0
)
3595
35
              .Case("experimental-yaml-elf-v1", "experimental-yaml-elf-v1")
3596
35
              .Case("experimental-tapi-elf-v1", "experimental-tapi-elf-v1")
3597
35
              .Default("");
3598
35
3599
35
      if (StubFormat.empty())
3600
1
        D.Diag(diag::err_drv_invalid_value)
3601
1
            << "Must specify a valid interface stub format type using "
3602
1
            << "-interface-stub-version=<experimental-tapi-elf-v1 | "
3603
1
               "experimental-yaml-elf-v1>";
3604
35
3605
35
      CmdArgs.push_back("-emit-interface-stubs");
3606
35
      CmdArgs.push_back(
3607
35
          Args.MakeArgString(Twine("-interface-stub-version=") + StubFormat));
3608
1.35k
    } else if (JA.getType() == types::TY_PP_Asm) {
3609
1.33k
      CmdArgs.push_back("-S");
3610
1.33k
    } else 
if (18
JA.getType() == types::TY_AST18
) {
3611
10
      CmdArgs.push_back("-emit-pch");
3612
10
    } else 
if (8
JA.getType() == types::TY_ModuleFile8
) {
3613
0
      CmdArgs.push_back("-module-file-info");
3614
8
    } else if (JA.getType() == types::TY_RewrittenObjC) {
3615
5
      CmdArgs.push_back("-rewrite-objc");
3616
5
      rewriteKind = RK_NonFragile;
3617
5
    } else 
if (3
JA.getType() == types::TY_RewrittenLegacyObjC3
) {
3618
3
      CmdArgs.push_back("-rewrite-objc");
3619
3
      rewriteKind = RK_Fragile;
3620
3
    } else {
3621
0
      assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!");
3622
0
    }
3623
10.5k
3624
10.5k
    // Preserve use-list order by default when emitting bitcode, so that
3625
10.5k
    // loading the bitcode up in 'opt' or 'llc' and running passes gives the
3626
10.5k
    // same result as running passes here.  For LTO, we don't need to preserve
3627
10.5k
    // the use-list order, since serialization to bitcode is part of the flow.
3628
10.5k
    if (JA.getType() == types::TY_LLVM_BC)
3629
224
      CmdArgs.push_back("-emit-llvm-uselists");
3630
10.5k
3631
10.5k
    // Device-side jobs do not support LTO.
3632
10.5k
    bool isDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) ||
3633
10.5k
                                   
JA.isDeviceOffloading(Action::OFK_Host)327
);
3634
10.5k
3635
10.5k
    if (D.isUsingLTO() && 
!isDeviceOffloadAction111
) {
3636
102
      Args.AddLastArg(CmdArgs, options::OPT_flto, options::OPT_flto_EQ);
3637
102
3638
102
      // The Darwin and PS4 linkers currently use the legacy LTO API, which
3639
102
      // does not support LTO unit features (CFI, whole program vtable opt)
3640
102
      // under ThinLTO.
3641
102
      if (!(RawTriple.isOSDarwin() || 
RawTriple.isPS4()67
) ||
3642
102
          
D.getLTOMode() == LTOK_Full37
)
3643
89
        CmdArgs.push_back("-flto-unit");
3644
102
    }
3645
10.5k
  }
3646
29.6k
3647
29.6k
  if (const Arg *A = Args.getLastArg(options::OPT_fthinlto_index_EQ)) {
3648
18
    if (!types::isLLVMIR(Input.getType()))
3649
1
      D.Diag(diag::err_drv_arg_requires_bitcode_input) << A->getAsString(Args);
3650
18
    Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ);
3651
18
  }
3652
29.6k
3653
29.6k
  if (Args.getLastArg(options::OPT_save_temps_EQ))
3654
217
    Args.AddLastArg(CmdArgs, options::OPT_save_temps_EQ);
3655
29.6k
3656
29.6k
  // Embed-bitcode option.
3657
29.6k
  // Only white-listed flags below are allowed to be embedded.
3658
29.6k
  if (C.getDriver().embedBitcodeInObject() && 
!C.getDriver().isUsingLTO()30
&&
3659
29.6k
      
(25
isa<BackendJobAction>(JA)25
||
isa<AssembleJobAction>(JA)24
)) {
3660
11
    // Add flags implied by -fembed-bitcode.
3661
11
    Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ);
3662
11
    // Disable all llvm IR level optimizations.
3663
11
    CmdArgs.push_back("-disable-llvm-passes");
3664
11
3665
11
    // Render target options such as -fuse-init-array on modern ELF platforms.
3666
11
    TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind());
3667
11
3668
11
    // reject options that shouldn't be supported in bitcode
3669
11
    // also reject kernel/kext
3670
11
    static const constexpr unsigned kBitcodeOptionBlacklist[] = {
3671
11
        options::OPT_mkernel,
3672
11
        options::OPT_fapple_kext,
3673
11
        options::OPT_ffunction_sections,
3674
11
        options::OPT_fno_function_sections,
3675
11
        options::OPT_fdata_sections,
3676
11
        options::OPT_fno_data_sections,
3677
11
        options::OPT_funique_section_names,
3678
11
        options::OPT_fno_unique_section_names,
3679
11
        options::OPT_mrestrict_it,
3680
11
        options::OPT_mno_restrict_it,
3681
11
        options::OPT_mstackrealign,
3682
11
        options::OPT_mno_stackrealign,
3683
11
        options::OPT_mstack_alignment,
3684
11
        options::OPT_mcmodel_EQ,
3685
11
        options::OPT_mlong_calls,
3686
11
        options::OPT_mno_long_calls,
3687
11
        options::OPT_ggnu_pubnames,
3688
11
        options::OPT_gdwarf_aranges,
3689
11
        options::OPT_fdebug_types_section,
3690
11
        options::OPT_fno_debug_types_section,
3691
11
        options::OPT_fdwarf_directory_asm,
3692
11
        options::OPT_fno_dwarf_directory_asm,
3693
11
        options::OPT_mrelax_all,
3694
11
        options::OPT_mno_relax_all,
3695
11
        options::OPT_ftrap_function_EQ,
3696
11
        options::OPT_ffixed_r9,
3697
11
        options::OPT_mfix_cortex_a53_835769,
3698
11
        options::OPT_mno_fix_cortex_a53_835769,
3699
11
        options::OPT_ffixed_x18,
3700
11
        options::OPT_mglobal_merge,
3701
11
        options::OPT_mno_global_merge,
3702
11
        options::OPT_mred_zone,
3703
11
        options::OPT_mno_red_zone,
3704
11
        options::OPT_Wa_COMMA,
3705
11
        options::OPT_Xassembler,
3706
11
        options::OPT_mllvm,
3707
11
    };
3708
11
    for (const auto &A : Args)
3709
95
      if (llvm::find(kBitcodeOptionBlacklist, A->getOption().getID()) !=
3710
95
          std::end(kBitcodeOptionBlacklist))
3711
0
        D.Diag(diag::err_drv_unsupported_embed_bitcode) << A->getSpelling();
3712
11
3713
11
    // Render the CodeGen options that need to be passed.
3714
11
    if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
3715
11
                      options::OPT_fno_optimize_sibling_calls))
3716
0
      CmdArgs.push_back("-mdisable-tail-calls");
3717
11
3718
11
    RenderFloatingPointOptions(TC, D, isOptimizationLevelFast(Args), Args,
3719
11
                               CmdArgs);
3720
11
3721
11
    // Render ABI arguments
3722
11
    switch (TC.getArch()) {
3723
11
    
default: break7
;
3724
11
    case llvm::Triple::arm:
3725
3
    case llvm::Triple::armeb:
3726
3
    case llvm::Triple::thumbeb:
3727
3
      RenderARMABI(Triple, Args, CmdArgs);
3728
3
      break;
3729
3
    case llvm::Triple::aarch64:
3730
1
    case llvm::Triple::aarch64_be:
3731
1
      RenderAArch64ABI(Triple, Args, CmdArgs);
3732
1
      break;
3733
11
    }
3734
11
3735
11
    // Optimization level for CodeGen.
3736
11
    if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) {
3737
0
      if (A->getOption().matches(options::OPT_O4)) {
3738
0
        CmdArgs.push_back("-O3");
3739
0
        D.Diag(diag::warn_O4_is_O3);
3740
0
      } else {
3741
0
        A->render(Args, CmdArgs);
3742
0
      }
3743
0
    }
3744
11
3745
11
    // Input/Output file.
3746
11
    if (Output.getType() == types::TY_Dependencies) {
3747
0
      // Handled with other dependency code.
3748
11
    } else if (Output.isFilename()) {
3749
11
      CmdArgs.push_back("-o");
3750
11
      CmdArgs.push_back(Output.getFilename());
3751
11
    } else {
3752
0
      assert(Output.isNothing() && "Input output.");
3753
0
    }
3754
11
3755
11
    for (const auto &II : Inputs) {
3756
11
      addDashXForInput(Args, II, CmdArgs);
3757
11
      if (II.isFilename())
3758
11
        CmdArgs.push_back(II.getFilename());
3759
0
      else
3760
0
        II.getInputArg().renderAsInput(Args, CmdArgs);
3761
11
    }
3762
11
3763
11
    C.addCommand(llvm::make_unique<Command>(JA, *this, D.getClangProgramPath(),
3764
11
                                            CmdArgs, Inputs));
3765
11
    return;
3766
11
  }
3767
29.5k
3768
29.5k
  if (C.getDriver().embedBitcodeMarkerOnly() && 
!C.getDriver().isUsingLTO()3
)
3769
3
    CmdArgs.push_back("-fembed-bitcode=marker");
3770
29.5k
3771
29.5k
  // We normally speed up the clang process a bit by skipping destructors at
3772
29.5k
  // exit, but when we're generating diagnostics we can rely on some of the
3773
29.5k
  // cleanup.
3774
29.5k
  if (!C.isForDiagnostics())
3775
29.5k
    CmdArgs.push_back("-disable-free");
3776
29.5k
3777
29.5k
#ifdef NDEBUG
3778
29.5k
  const bool IsAssertBuild = false;
3779
#else
3780
  const bool IsAssertBuild = true;
3781
#endif
3782
3783
29.5k
  // Disable the verification pass in -asserts builds.
3784
29.5k
  if (!IsAssertBuild)
3785
29.5k
    CmdArgs.push_back("-disable-llvm-verifier");
3786
29.5k
3787
29.5k
  // Discard value names in assert builds unless otherwise specified.
3788
29.5k
  if (Args.hasFlag(options::OPT_fdiscard_value_names,
3789
29.5k
                   options::OPT_fno_discard_value_names, !IsAssertBuild))
3790
29.5k
    CmdArgs.push_back("-discard-value-names");
3791
29.5k
3792
29.5k
  // Set the main file name, so that debug info works even with
3793
29.5k
  // -save-temps.
3794
29.5k
  CmdArgs.push_back("-main-file-name");
3795
29.5k
  CmdArgs.push_back(getBaseInputName(Args, Input));
3796
29.5k
3797
29.5k
  // Some flags which affect the language (via preprocessor
3798
29.5k
  // defines).
3799
29.5k
  if (Args.hasArg(options::OPT_static))
3800
1.19k
    CmdArgs.push_back("-static-define");
3801
29.5k
3802
29.5k
  if (Args.hasArg(options::OPT_municode))
3803
1
    CmdArgs.push_back("-DUNICODE");
3804
29.5k
3805
29.5k
  if (isa<AnalyzeJobAction>(JA))
3806
17
    RenderAnalyzerOptions(Args, CmdArgs, Triple, Input);
3807
29.5k
3808
29.5k
  // Enable compatilibily mode to avoid analyzer-config related errors.
3809
29.5k
  // Since we can't access frontend flags through hasArg, let's manually iterate
3810
29.5k
  // through them.
3811
29.5k
  bool FoundAnalyzerConfig = false;
3812
29.5k
  for (auto Arg : Args.filtered(options::OPT_Xclang))
3813
1.22k
    if (StringRef(Arg->getValue()) == "-analyzer-config") {
3814
4
      FoundAnalyzerConfig = true;
3815
4
      break;
3816
4
    }
3817
29.5k
  if (!FoundAnalyzerConfig)
3818
29.5k
    for (auto Arg : Args.filtered(options::OPT_Xanalyzer))
3819
4
      if (StringRef(Arg->getValue()) == "-analyzer-config") {
3820
4
        FoundAnalyzerConfig = true;
3821
4
        break;
3822
4
      }
3823
29.5k
  if (FoundAnalyzerConfig)
3824
8
    CmdArgs.push_back("-analyzer-config-compatibility-mode=true");
3825
29.5k
3826
29.5k
  CheckCodeGenerationOptions(D, Args);
3827
29.5k
3828
29.5k
  unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args);
3829
29.5k
  assert(FunctionAlignment <= 31 && "function alignment will be truncated!");
3830
29.5k
  if (FunctionAlignment) {
3831
4
    CmdArgs.push_back("-function-alignment");
3832
4
    CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment)));
3833
4
  }
3834
29.5k
3835
29.5k
  llvm::Reloc::Model RelocationModel;
3836
29.5k
  unsigned PICLevel;
3837
29.5k
  bool IsPIE;
3838
29.5k
  std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(TC, Args);
3839
29.5k
3840
29.5k
  const char *RMName = RelocationModelName(RelocationModel);
3841
29.5k
3842
29.5k
  if ((RelocationModel == llvm::Reloc::ROPI ||
3843
29.5k
       
RelocationModel == llvm::Reloc::ROPI_RWPI29.5k
) &&
3844
29.5k
      
types::isCXX(Input.getType())13
&&
3845
29.5k
      
!Args.hasArg(options::OPT_fallow_unsupported)3
)
3846
2
    D.Diag(diag::err_drv_ropi_incompatible_with_cxx);
3847
29.5k
3848
29.5k
  if (RMName) {
3849
29.5k
    CmdArgs.push_back("-mrelocation-model");
3850
29.5k
    CmdArgs.push_back(RMName);
3851
29.5k
  }
3852
29.5k
  if (PICLevel > 0) {
3853
21.4k
    CmdArgs.push_back("-pic-level");
3854
21.4k
    CmdArgs.push_back(PICLevel == 1 ? 
"1"504
:
"2"20.8k
);
3855
21.4k
    if (IsPIE)
3856
356
      CmdArgs.push_back("-pic-is-pie");
3857
21.4k
  }
3858
29.5k
3859
29.5k
  if (RelocationModel == llvm::Reloc::ROPI ||
3860
29.5k
      
RelocationModel == llvm::Reloc::ROPI_RWPI29.5k
)
3861
13
    CmdArgs.push_back("-fropi");
3862
29.5k
  if (RelocationModel == llvm::Reloc::RWPI ||
3863
29.5k
      
RelocationModel == llvm::Reloc::ROPI_RWPI29.5k
)
3864
8
    CmdArgs.push_back("-frwpi");
3865
29.5k
3866
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_meabi)) {
3867
28
    CmdArgs.push_back("-meabi");
3868
28
    CmdArgs.push_back(A->getValue());
3869
28
  }
3870
29.5k
3871
29.5k
  CmdArgs.push_back("-mthread-model");
3872
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) {
3873
11
    if (!TC.isThreadModelSupported(A->getValue()))
3874
4
      D.Diag(diag::err_drv_invalid_thread_model_for_target)
3875
4
          << A->getValue() << A->getAsString(Args);
3876
11
    CmdArgs.push_back(A->getValue());
3877
11
  }
3878
29.5k
  else
3879
29.5k
    CmdArgs.push_back(Args.MakeArgString(TC.getThreadModel()));
3880
29.5k
3881
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fveclib);
3882
29.5k
3883
29.5k
  if (Args.hasFlag(options::OPT_fmerge_all_constants,
3884
29.5k
                   options::OPT_fno_merge_all_constants, false))
3885
3
    CmdArgs.push_back("-fmerge-all-constants");
3886
29.5k
3887
29.5k
  if (Args.hasFlag(options::OPT_fno_delete_null_pointer_checks,
3888
29.5k
                   options::OPT_fdelete_null_pointer_checks, false))
3889
2
    CmdArgs.push_back("-fno-delete-null-pointer-checks");
3890
29.5k
3891
29.5k
  // LLVM Code Generator Options.
3892
29.5k
3893
29.5k
  if (Args.hasArg(options::OPT_frewrite_map_file) ||
3894
29.5k
      
Args.hasArg(options::OPT_frewrite_map_file_EQ)29.5k
) {
3895
8
    for (const Arg *A : Args.filtered(options::OPT_frewrite_map_file,
3896
11
                                      options::OPT_frewrite_map_file_EQ)) {
3897
11
      StringRef Map = A->getValue();
3898
11
      if (!llvm::sys::fs::exists(Map)) {
3899
1
        D.Diag(diag::err_drv_no_such_file) << Map;
3900
10
      } else {
3901
10
        CmdArgs.push_back("-frewrite-map-file");
3902
10
        CmdArgs.push_back(A->getValue());
3903
10
        A->claim();
3904
10
      }
3905
11
    }
3906
8
  }
3907
29.5k
3908
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) {
3909
1.61k
    StringRef v = A->getValue();
3910
1.61k
    CmdArgs.push_back("-mllvm");
3911
1.61k
    CmdArgs.push_back(Args.MakeArgString("-warn-stack-size=" + v));
3912
1.61k
    A->claim();
3913
1.61k
  }
3914
29.5k
3915
29.5k
  if (!Args.hasFlag(options::OPT_fjump_tables, options::OPT_fno_jump_tables,
3916
29.5k
                    true))
3917
0
    CmdArgs.push_back("-fno-jump-tables");
3918
29.5k
3919
29.5k
  if (Args.hasFlag(options::OPT_fprofile_sample_accurate,
3920
29.5k
                   options::OPT_fno_profile_sample_accurate, false))
3921
3
    CmdArgs.push_back("-fprofile-sample-accurate");
3922
29.5k
3923
29.5k
  if (!Args.hasFlag(options::OPT_fpreserve_as_comments,
3924
29.5k
                    options::OPT_fno_preserve_as_comments, true))
3925
1
    CmdArgs.push_back("-fno-preserve-as-comments");
3926
29.5k
3927
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
3928
0
    CmdArgs.push_back("-mregparm");
3929
0
    CmdArgs.push_back(A->getValue());
3930
0
  }
3931
29.5k
3932
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return,
3933
0
                               options::OPT_freg_struct_return)) {
3934
0
    if (TC.getArch() != llvm::Triple::x86) {
3935
0
      D.Diag(diag::err_drv_unsupported_opt_for_target)
3936
0
          << A->getSpelling() << RawTriple.str();
3937
0
    } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) {
3938
0
      CmdArgs.push_back("-fpcc-struct-return");
3939
0
    } else {
3940
0
      assert(A->getOption().matches(options::OPT_freg_struct_return));
3941
0
      CmdArgs.push_back("-freg-struct-return");
3942
0
    }
3943
0
  }
3944
29.5k
3945
29.5k
  if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
3946
0
    CmdArgs.push_back("-fdefault-calling-conv=stdcall");
3947
29.5k
3948
29.5k
  CodeGenOptions::FramePointerKind FPKeepKind =
3949
29.5k
                  getFramePointerKind(Args, RawTriple);
3950
29.5k
  const char *FPKeepKindStr = nullptr;
3951
29.5k
  switch (FPKeepKind) {
3952
29.5k
  case CodeGenOptions::FramePointerKind::None:
3953
3.11k
    FPKeepKindStr = "-mframe-pointer=none";
3954
3.11k
    break;
3955
29.5k
  case CodeGenOptions::FramePointerKind::NonLeaf:
3956
120
    FPKeepKindStr = "-mframe-pointer=non-leaf";
3957
120
    break;
3958
29.5k
  case CodeGenOptions::FramePointerKind::All:
3959
26.3k
    FPKeepKindStr = "-mframe-pointer=all";
3960
26.3k
    break;
3961
29.5k
  }
3962
29.5k
  assert(FPKeepKindStr && "unknown FramePointerKind");
3963
29.5k
  CmdArgs.push_back(FPKeepKindStr);
3964
29.5k
3965
29.5k
  if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
3966
29.5k
                    options::OPT_fno_zero_initialized_in_bss))
3967
0
    CmdArgs.push_back("-mno-zero-initialized-in-bss");
3968
29.5k
3969
29.5k
  bool OFastEnabled = isOptimizationLevelFast(Args);
3970
29.5k
  // If -Ofast is the optimization level, then -fstrict-aliasing should be
3971
29.5k
  // enabled.  This alias option is being used to simplify the hasFlag logic.
3972
29.5k
  OptSpecifier StrictAliasingAliasOption =
3973
29.5k
      OFastEnabled ? 
options::OPT_Ofast21
:
options::OPT_fstrict_aliasing29.5k
;
3974
29.5k
  // We turn strict aliasing off by default if we're in CL mode, since MSVC
3975
29.5k
  // doesn't do any TBAA.
3976
29.5k
  bool TBAAOnByDefault = !D.IsCLMode();
3977
29.5k
  if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
3978
29.5k
                    options::OPT_fno_strict_aliasing, TBAAOnByDefault))
3979
567
    CmdArgs.push_back("-relaxed-aliasing");
3980
29.5k
  if (!Args.hasFlag(options::OPT_fstruct_path_tbaa,
3981
29.5k
                    options::OPT_fno_struct_path_tbaa))
3982
0
    CmdArgs.push_back("-no-struct-path-tbaa");
3983
29.5k
  if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
3984
29.5k
                   false))
3985
0
    CmdArgs.push_back("-fstrict-enums");
3986
29.5k
  if (!Args.hasFlag(options::OPT_fstrict_return, options::OPT_fno_strict_return,
3987
29.5k
                    true))
3988
1
    CmdArgs.push_back("-fno-strict-return");
3989
29.5k
  if (Args.hasFlag(options::OPT_fallow_editor_placeholders,
3990
29.5k
                   options::OPT_fno_allow_editor_placeholders, false))
3991
972
    CmdArgs.push_back("-fallow-editor-placeholders");
3992
29.5k
  if (Args.hasFlag(options::OPT_fstrict_vtable_pointers,
3993
29.5k
                   options::OPT_fno_strict_vtable_pointers,
3994
29.5k
                   false))
3995
0
    CmdArgs.push_back("-fstrict-vtable-pointers");
3996
29.5k
  if (Args.hasFlag(options::OPT_fforce_emit_vtables,
3997
29.5k
                   options::OPT_fno_force_emit_vtables,
3998
29.5k
                   false))
3999
0
    CmdArgs.push_back("-fforce-emit-vtables");
4000
29.5k
  if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
4001
29.5k
                    options::OPT_fno_optimize_sibling_calls))
4002
1
    CmdArgs.push_back("-mdisable-tail-calls");
4003
29.5k
  if (Args.hasFlag(options::OPT_fno_escaping_block_tail_calls,
4004
29.5k
                   options::OPT_fescaping_block_tail_calls, false))
4005
1
    CmdArgs.push_back("-fno-escaping-block-tail-calls");
4006
29.5k
4007
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_ffine_grained_bitfield_accesses,
4008
29.5k
                  options::OPT_fno_fine_grained_bitfield_accesses);
4009
29.5k
4010
29.5k
  // Handle segmented stacks.
4011
29.5k
  if (Args.hasArg(options::OPT_fsplit_stack))
4012
5
    CmdArgs.push_back("-split-stacks");
4013
29.5k
4014
29.5k
  RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs);
4015
29.5k
4016
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_mlong_double_64,
4017
12
                               options::OPT_mlong_double_128)) {
4018
12
    if (TC.getArch() == llvm::Triple::x86 ||
4019
12
        
TC.getArch() == llvm::Triple::x86_6410
||
4020
12
        
TC.getArch() == llvm::Triple::ppc8
||
TC.getTriple().isPPC64()6
)
4021
10
      A->render(Args, CmdArgs);
4022
2
    else
4023
2
      D.Diag(diag::err_drv_unsupported_opt_for_target)
4024
2
          << A->getAsString(Args) << TripleStr;
4025
12
  }
4026
29.5k
4027
29.5k
  // Decide whether to use verbose asm. Verbose assembly is the default on
4028
29.5k
  // toolchains which have the integrated assembler on by default.
4029
29.5k
  bool IsIntegratedAssemblerDefault = TC.IsIntegratedAssemblerDefault();
4030
29.5k
  if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
4031
29.5k
                   IsIntegratedAssemblerDefault) ||
4032
29.5k
      
Args.hasArg(options::OPT_dA)928
)
4033
28.6k
    CmdArgs.push_back("-masm-verbose");
4034
29.5k
4035
29.5k
  if (!TC.useIntegratedAs())
4036
847
    CmdArgs.push_back("-no-integrated-as");
4037
29.5k
4038
29.5k
  if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
4039
0
    CmdArgs.push_back("-mdebug-pass");
4040
0
    CmdArgs.push_back("Structure");
4041
0
  }
4042
29.5k
  if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
4043
0
    CmdArgs.push_back("-mdebug-pass");
4044
0
    CmdArgs.push_back("Arguments");
4045
0
  }
4046
29.5k
4047
29.5k
  // Enable -mconstructor-aliases except on darwin, where we have to work around
4048
29.5k
  // a linker bug (see <rdar://problem/7651567>), and CUDA device code, where
4049
29.5k
  // aliases aren't supported.
4050
29.5k
  if (!RawTriple.isOSDarwin() && 
!RawTriple.isNVPTX()9.54k
)
4051
9.23k
    CmdArgs.push_back("-mconstructor-aliases");
4052
29.5k
4053
29.5k
  // Darwin's kernel doesn't support guard variables; just die if we
4054
29.5k
  // try to use them.
4055
29.5k
  if (KernelOrKext && 
RawTriple.isOSDarwin()1.04k
)
4056
1.03k
    CmdArgs.push_back("-fforbid-guard-variables");
4057
29.5k
4058
29.5k
  if (Args.hasFlag(options::OPT_mms_bitfields, options::OPT_mno_ms_bitfields,
4059
29.5k
                   false)) {
4060
1
    CmdArgs.push_back("-mms-bitfields");
4061
1
  }
4062
29.5k
4063
29.5k
  if (Args.hasFlag(options::OPT_mpie_copy_relocations,
4064
29.5k
                   options::OPT_mno_pie_copy_relocations,
4065
29.5k
                   false)) {
4066
0
    CmdArgs.push_back("-mpie-copy-relocations");
4067
0
  }
4068
29.5k
4069
29.5k
  if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) {
4070
0
    CmdArgs.push_back("-fno-plt");
4071
0
  }
4072
29.5k
4073
29.5k
  // -fhosted is default.
4074
29.5k
  // TODO: Audit uses of KernelOrKext and see where it'd be more appropriate to
4075
29.5k
  // use Freestanding.
4076
29.5k
  bool Freestanding =
4077
29.5k
      Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
4078
29.5k
      
KernelOrKext28.0k
;
4079
29.5k
  if (Freestanding)
4080
2.63k
    CmdArgs.push_back("-ffreestanding");
4081
29.5k
4082
29.5k
  // This is a coarse approximation of what llvm-gcc actually does, both
4083
29.5k
  // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
4084
29.5k
  // complicated ways.
4085
29.5k
  bool AsynchronousUnwindTables =
4086
29.5k
      Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
4087
29.5k
                   options::OPT_fno_asynchronous_unwind_tables,
4088
29.5k
                   (TC.IsUnwindTablesDefault(Args) ||
4089
29.5k
                    
TC.getSanitizerArgs().needsUnwindTables()8.98k
) &&
4090
29.5k
                       
!Freestanding20.6k
);
4091
29.5k
  if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
4092
29.5k
                   AsynchronousUnwindTables))
4093
20.0k
    CmdArgs.push_back("-munwind-tables");
4094
29.5k
4095
29.5k
  TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind());
4096
29.5k
4097
29.5k
  // FIXME: Handle -mtune=.
4098
29.5k
  (void)Args.hasArg(options::OPT_mtune_EQ);
4099
29.5k
4100
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
4101
11
    CmdArgs.push_back("-mcode-model");
4102
11
    CmdArgs.push_back(A->getValue());
4103
11
  }
4104
29.5k
4105
29.5k
  // Add the target cpu
4106
29.5k
  std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false);
4107
29.5k
  if (!CPU.empty()) {
4108
29.2k
    CmdArgs.push_back("-target-cpu");
4109
29.2k
    CmdArgs.push_back(Args.MakeArgString(CPU));
4110
29.2k
  }
4111
29.5k
4112
29.5k
  RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs);
4113
29.5k
4114
29.5k
  // These two are potentially updated by AddClangCLArgs.
4115
29.5k
  codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo;
4116
29.5k
  bool EmitCodeView = false;
4117
29.5k
4118
29.5k
  // Add clang-cl arguments.
4119
29.5k
  types::ID InputType = Input.getType();
4120
29.5k
  if (D.IsCLMode())
4121
515
    AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView);
4122
29.5k
4123
29.5k
  DwarfFissionKind DwarfFission;
4124
29.5k
  RenderDebugOptions(TC, D, RawTriple, Args, EmitCodeView, IsWindowsMSVC,
4125
29.5k
                     CmdArgs, DebugInfoKind, DwarfFission);
4126
29.5k
4127
29.5k
  // Add the split debug info name to the command lines here so we
4128
29.5k
  // can propagate it to the backend.
4129
29.5k
  bool SplitDWARF = (DwarfFission != DwarfFissionKind::None) &&
4130
29.5k
                    
TC.getTriple().isOSBinFormatELF()29
&&
4131
29.5k
                    
(24
isa<AssembleJobAction>(JA)24
||
isa<CompileJobAction>(JA)11
||
4132
24
                     
isa<BackendJobAction>(JA)10
);
4133
29.5k
  if (SplitDWARF) {
4134
23
    const char *SplitDWARFOut = SplitDebugName(Args, Input, Output);
4135
23
    CmdArgs.push_back("-split-dwarf-file");
4136
23
    CmdArgs.push_back(SplitDWARFOut);
4137
23
    if (DwarfFission == DwarfFissionKind::Split) {
4138
21
      CmdArgs.push_back("-split-dwarf-output");
4139
21
      CmdArgs.push_back(SplitDWARFOut);
4140
21
    }
4141
23
  }
4142
29.5k
4143
29.5k
  // Pass the linker version in use.
4144
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
4145
29.5k
    CmdArgs.push_back("-target-linker-version");
4146
29.5k
    CmdArgs.push_back(A->getValue());
4147
29.5k
  }
4148
29.5k
4149
29.5k
  // Explicitly error on some things we know we don't support and can't just
4150
29.5k
  // ignore.
4151
29.5k
  if (!Args.hasArg(options::OPT_fallow_unsupported)) {
4152
29.5k
    Arg *Unsupported;
4153
29.5k
    if (types::isCXX(InputType) && 
RawTriple.isOSDarwin()11.5k
&&
4154
29.5k
        
TC.getArch() == llvm::Triple::x868.49k
) {
4155
249
      if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
4156
249
          (Unsupported = Args.getLastArg(options::OPT_mkernel)))
4157
1
        D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
4158
1
            << Unsupported->getOption().getName();
4159
249
    }
4160
29.5k
    // The faltivec option has been superseded by the maltivec option.
4161
29.5k
    if ((Unsupported = Args.getLastArg(options::OPT_faltivec)))
4162
1
      D.Diag(diag::err_drv_clang_unsupported_opt_faltivec)
4163
1
          << Unsupported->getOption().getName()
4164
1
          << "please use -maltivec and include altivec.h explicitly";
4165
29.5k
    if ((Unsupported = Args.getLastArg(options::OPT_fno_altivec)))
4166
1
      D.Diag(diag::err_drv_clang_unsupported_opt_faltivec)
4167
1
          << Unsupported->getOption().getName() << "please use -mno-altivec";
4168
29.5k
  }
4169
29.5k
4170
29.5k
  Args.AddAllArgs(CmdArgs, options::OPT_v);
4171
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_H);
4172
29.5k
  if (D.CCPrintHeaders && 
!D.CCGenDiagnostics2
) {
4173
1
    CmdArgs.push_back("-header-include-file");
4174
1
    CmdArgs.push_back(D.CCPrintHeadersFilename ? 
D.CCPrintHeadersFilename0
4175
1
                                               : "-");
4176
1
  }
4177
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_P);
4178
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
4179
29.5k
4180
29.5k
  if (D.CCLogDiagnostics && 
!D.CCGenDiagnostics4
) {
4181
3
    CmdArgs.push_back("-diagnostic-log-file");
4182
3
    CmdArgs.push_back(D.CCLogDiagnosticsFilename ? 
D.CCLogDiagnosticsFilename2
4183
3
                                                 : 
"-"1
);
4184
3
  }
4185
29.5k
4186
29.5k
  bool UseSeparateSections = isUseSeparateSections(Triple);
4187
29.5k
4188
29.5k
  if (Args.hasFlag(options::OPT_ffunction_sections,
4189
29.5k
                   options::OPT_fno_function_sections, UseSeparateSections)) {
4190
39
    CmdArgs.push_back("-ffunction-sections");
4191
39
  }
4192
29.5k
4193
29.5k
  if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections,
4194
29.5k
                   UseSeparateSections)) {
4195
18
    CmdArgs.push_back("-fdata-sections");
4196
18
  }
4197
29.5k
4198
29.5k
  if (!Args.hasFlag(options::OPT_funique_section_names,
4199
29.5k
                    options::OPT_fno_unique_section_names, true))
4200
1
    CmdArgs.push_back("-fno-unique-section-names");
4201
29.5k
4202
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_finstrument_functions,
4203
29.5k
                  options::OPT_finstrument_functions_after_inlining,
4204
29.5k
                  options::OPT_finstrument_function_entry_bare);
4205
29.5k
4206
29.5k
  // NVPTX doesn't support PGO or coverage. There's no runtime support for
4207
29.5k
  // sampling, overhead of call arc collection is way too high and there's no
4208
29.5k
  // way to collect the output.
4209
29.5k
  if (!Triple.isNVPTX())
4210
29.2k
    addPGOAndCoverageFlags(TC, C, D, Output, Args, CmdArgs);
4211
29.5k
4212
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fclang_abi_compat_EQ);
4213
29.5k
4214
29.5k
  // Add runtime flag for PS4 when PGO, coverage, or sanitizers are enabled.
4215
29.5k
  if (RawTriple.isPS4CPU() &&
4216
29.5k
      
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)116
) {
4217
110
    PS4cpu::addProfileRTArgs(TC, Args, CmdArgs);
4218
110
    PS4cpu::addSanitizerArgs(TC, CmdArgs);
4219
110
  }
4220
29.5k
4221
29.5k
  // Pass options for controlling the default header search paths.
4222
29.5k
  if (Args.hasArg(options::OPT_nostdinc)) {
4223
22
    CmdArgs.push_back("-nostdsysteminc");
4224
22
    CmdArgs.push_back("-nobuiltininc");
4225
29.5k
  } else {
4226
29.5k
    if (Args.hasArg(options::OPT_nostdlibinc))
4227
9
      CmdArgs.push_back("-nostdsysteminc");
4228
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
4229
29.5k
    Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
4230
29.5k
  }
4231
29.5k
4232
29.5k
  // Pass the path to compiler resource files.
4233
29.5k
  CmdArgs.push_back("-resource-dir");
4234
29.5k
  CmdArgs.push_back(D.ResourceDir.c_str());
4235
29.5k
4236
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_working_directory);
4237
29.5k
4238
29.5k
  RenderARCMigrateToolOptions(D, Args, CmdArgs);
4239
29.5k
4240
29.5k
  // Add preprocessing options like -I, -D, etc. if we are using the
4241
29.5k
  // preprocessor.
4242
29.5k
  //
4243
29.5k
  // FIXME: Support -fpreprocessed
4244
29.5k
  if (types::getPreprocessedType(InputType) != types::TY_INVALID)
4245
25.1k
    AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
4246
29.5k
4247
29.5k
  // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
4248
29.5k
  // that "The compiler can only warn and ignore the option if not recognized".
4249
29.5k
  // When building with ccache, it will pass -D options to clang even on
4250
29.5k
  // preprocessed inputs and configure concludes that -fPIC is not supported.
4251
29.5k
  Args.ClaimAllArgs(options::OPT_D);
4252
29.5k
4253
29.5k
  // Manually translate -O4 to -O3; let clang reject others.
4254
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
4255
13.0k
    if (A->getOption().matches(options::OPT_O4)) {
4256
7
      CmdArgs.push_back("-O3");
4257
7
      D.Diag(diag::warn_O4_is_O3);
4258
13.0k
    } else {
4259
13.0k
      A->render(Args, CmdArgs);
4260
13.0k
    }
4261
13.0k
  }
4262
29.5k
4263
29.5k
  // Warn about ignored options to clang.
4264
29.5k
  for (const Arg *A :
4265
29.5k
       Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) {
4266
139
    D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args);
4267
139
    A->claim();
4268
139
  }
4269
29.5k
4270
29.5k
  for (const Arg *A :
4271
29.5k
       Args.filtered(options::OPT_clang_ignored_legacy_options_Group)) {
4272
2
    D.Diag(diag::warn_ignored_clang_option) << A->getAsString(Args);
4273
2
    A->claim();
4274
2
  }
4275
29.5k
4276
29.5k
  claimNoWarnArgs(Args);
4277
29.5k
4278
29.5k
  Args.AddAllArgs(CmdArgs, options::OPT_R_Group);
4279
29.5k
4280
29.5k
  Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
4281
29.5k
  if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
4282
54
    CmdArgs.push_back("-pedantic");
4283
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
4284
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_w);
4285
29.5k
4286
29.5k
  // Fixed point flags
4287
29.5k
  if (Args.hasFlag(options::OPT_ffixed_point, options::OPT_fno_fixed_point,
4288
29.5k
                   /*Default=*/false))
4289
6
    Args.AddLastArg(CmdArgs, options::OPT_ffixed_point);
4290
29.5k
4291
29.5k
  // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
4292
29.5k
  // (-ansi is equivalent to -std=c89 or -std=c++98).
4293
29.5k
  //
4294
29.5k
  // If a std is supplied, only add -trigraphs if it follows the
4295
29.5k
  // option.
4296
29.5k
  bool ImplyVCPPCXXVer = false;
4297
29.5k
  const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi);
4298
29.5k
  if (Std) {
4299
8.86k
    if (Std->getOption().matches(options::OPT_ansi))
4300
2
      if (types::isCXX(InputType))
4301
0
        CmdArgs.push_back("-std=c++98");
4302
2
      else
4303
2
        CmdArgs.push_back("-std=c89");
4304
8.86k
    else
4305
8.86k
      Std->render(Args, CmdArgs);
4306
8.86k
4307
8.86k
    // If -f(no-)trigraphs appears after the language standard flag, honor it.
4308
8.86k
    if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
4309
8.86k
                                 options::OPT_ftrigraphs,
4310
8.86k
                                 options::OPT_fno_trigraphs))
4311
8.86k
      if (A != Std)
4312
3
        A->render(Args, CmdArgs);
4313
20.7k
  } else {
4314
20.7k
    // Honor -std-default.
4315
20.7k
    //
4316
20.7k
    // FIXME: Clang doesn't correctly handle -std= when the input language
4317
20.7k
    // doesn't match. For the time being just ignore this for C++ inputs;
4318
20.7k
    // eventually we want to do all the standard defaulting here instead of
4319
20.7k
    // splitting it between the driver and clang -cc1.
4320
20.7k
    if (!types::isCXX(InputType))
4321
17.5k
      Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=",
4322
17.5k
                                /*Joined=*/true);
4323
3.14k
    else if (IsWindowsMSVC)
4324
142
      ImplyVCPPCXXVer = true;
4325
20.7k
4326
20.7k
    Args.AddLastArg(CmdArgs, options::OPT_ftrigraphs,
4327
20.7k
                    options::OPT_fno_trigraphs);
4328
20.7k
  }
4329
29.5k
4330
29.5k
  // GCC's behavior for -Wwrite-strings is a bit strange:
4331
29.5k
  //  * In C, this "warning flag" changes the types of string literals from
4332
29.5k
  //    'char[N]' to 'const char[N]', and thus triggers an unrelated warning
4333
29.5k
  //    for the discarded qualifier.
4334
29.5k
  //  * In C++, this is just a normal warning flag.
4335
29.5k
  //
4336
29.5k
  // Implementing this warning correctly in C is hard, so we follow GCC's
4337
29.5k
  // behavior for now. FIXME: Directly diagnose uses of a string literal as
4338
29.5k
  // a non-const char* in C, rather than using this crude hack.
4339
29.5k
  if (!types::isCXX(InputType)) {
4340
18.0k
    // FIXME: This should behave just like a warning flag, and thus should also
4341
18.0k
    // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on.
4342
18.0k
    Arg *WriteStrings =
4343
18.0k
        Args.getLastArg(options::OPT_Wwrite_strings,
4344
18.0k
                        options::OPT_Wno_write_strings, options::OPT_w);
4345
18.0k
    if (WriteStrings &&
4346
18.0k
        
WriteStrings->getOption().matches(options::OPT_Wwrite_strings)6.02k
)
4347
1
      CmdArgs.push_back("-fconst-strings");
4348
18.0k
  }
4349
29.5k
4350
29.5k
  // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
4351
29.5k
  // during C++ compilation, which it is by default. GCC keeps this define even
4352
29.5k
  // in the presence of '-w', match this behavior bug-for-bug.
4353
29.5k
  if (types::isCXX(InputType) &&
4354
29.5k
      Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
4355
11.5k
                   true)) {
4356
11.5k
    CmdArgs.push_back("-fdeprecated-macro");
4357
11.5k
  }
4358
29.5k
4359
29.5k
  // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
4360
29.5k
  if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
4361
2
    if (Asm->getOption().matches(options::OPT_fasm))
4362
1
      CmdArgs.push_back("-fgnu-keywords");
4363
1
    else
4364
1
      CmdArgs.push_back("-fno-gnu-keywords");
4365
2
  }
4366
29.5k
4367
29.5k
  if (ShouldDisableDwarfDirectory(Args, TC))
4368
847
    CmdArgs.push_back("-fno-dwarf-directory-asm");
4369
29.5k
4370
29.5k
  if (ShouldDisableAutolink(Args, TC))
4371
12
    CmdArgs.push_back("-fno-autolink");
4372
29.5k
4373
29.5k
  // Add in -fdebug-compilation-dir if necessary.
4374
29.5k
  addDebugCompDirArg(Args, CmdArgs, D.getVFS());
4375
29.5k
4376
29.5k
  addDebugPrefixMapArg(D, Args, CmdArgs);
4377
29.5k
4378
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
4379
2
                               options::OPT_ftemplate_depth_EQ)) {
4380
2
    CmdArgs.push_back("-ftemplate-depth");
4381
2
    CmdArgs.push_back(A->getValue());
4382
2
  }
4383
29.5k
4384
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) {
4385
1
    CmdArgs.push_back("-foperator-arrow-depth");
4386
1
    CmdArgs.push_back(A->getValue());
4387
1
  }
4388
29.5k
4389
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
4390
1
    CmdArgs.push_back("-fconstexpr-depth");
4391
1
    CmdArgs.push_back(A->getValue());
4392
1
  }
4393
29.5k
4394
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) {
4395
1
    CmdArgs.push_back("-fconstexpr-steps");
4396
1
    CmdArgs.push_back(A->getValue());
4397
1
  }
4398
29.5k
4399
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
4400
2
    CmdArgs.push_back("-fbracket-depth");
4401
2
    CmdArgs.push_back(A->getValue());
4402
2
  }
4403
29.5k
4404
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
4405
2
                               options::OPT_Wlarge_by_value_copy_def)) {
4406
2
    if (A->getNumValues()) {
4407
1
      StringRef bytes = A->getValue();
4408
1
      CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
4409
1
    } else
4410
1
      CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
4411
2
  }
4412
29.5k
4413
29.5k
  if (Args.hasArg(options::OPT_relocatable_pch))
4414
1
    CmdArgs.push_back("-relocatable-pch");
4415
29.5k
4416
29.5k
  if (const Arg *A = Args.getLastArg(options::OPT_fcf_runtime_abi_EQ)) {
4417
6
    static const char *kCFABIs[] = {
4418
6
      "standalone", "objc", "swift", "swift-5.0", "swift-4.2", "swift-4.1",
4419
6
    };
4420
6
4421
6
    if (find(kCFABIs, StringRef(A->getValue())) == std::end(kCFABIs))
4422
1
      D.Diag(diag::err_drv_invalid_cf_runtime_abi) << A->getValue();
4423
5
    else
4424
5
      A->render(Args, CmdArgs);
4425
6
  }
4426
29.5k
4427
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
4428
0
    CmdArgs.push_back("-fconstant-string-class");
4429
0
    CmdArgs.push_back(A->getValue());
4430
0
  }
4431
29.5k
4432
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
4433
0
    CmdArgs.push_back("-ftabstop");
4434
0
    CmdArgs.push_back(A->getValue());
4435
0
  }
4436
29.5k
4437
29.5k
  if (Args.hasFlag(options::OPT_fstack_size_section,
4438
29.5k
                   options::OPT_fno_stack_size_section, RawTriple.isPS4()))
4439
115
    CmdArgs.push_back("-fstack-size-section");
4440
29.5k
4441
29.5k
  CmdArgs.push_back("-ferror-limit");
4442
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
4443
1
    CmdArgs.push_back(A->getValue());
4444
29.5k
  else
4445
29.5k
    CmdArgs.push_back("19");
4446
29.5k
4447
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
4448
1
    CmdArgs.push_back("-fmacro-backtrace-limit");
4449
1
    CmdArgs.push_back(A->getValue());
4450
1
  }
4451
29.5k
4452
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
4453
2
    CmdArgs.push_back("-ftemplate-backtrace-limit");
4454
2
    CmdArgs.push_back(A->getValue());
4455
2
  }
4456
29.5k
4457
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
4458
0
    CmdArgs.push_back("-fconstexpr-backtrace-limit");
4459
0
    CmdArgs.push_back(A->getValue());
4460
0
  }
4461
29.5k
4462
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fspell_checking_limit_EQ)) {
4463
0
    CmdArgs.push_back("-fspell-checking-limit");
4464
0
    CmdArgs.push_back(A->getValue());
4465
0
  }
4466
29.5k
4467
29.5k
  // Pass -fmessage-length=.
4468
29.5k
  CmdArgs.push_back("-fmessage-length");
4469
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
4470
0
    CmdArgs.push_back(A->getValue());
4471
29.5k
  } else {
4472
29.5k
    // If -fmessage-length=N was not specified, determine whether this is a
4473
29.5k
    // terminal and, if so, implicitly define -fmessage-length appropriately.
4474
29.5k
    unsigned N = llvm::sys::Process::StandardErrColumns();
4475
29.5k
    CmdArgs.push_back(Args.MakeArgString(Twine(N)));
4476
29.5k
  }
4477
29.5k
4478
29.5k
  // -fvisibility= and -fvisibility-ms-compat are of a piece.
4479
29.5k
  if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
4480
4.29k
                                     options::OPT_fvisibility_ms_compat)) {
4481
4.29k
    if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
4482
4.29k
      CmdArgs.push_back("-fvisibility");
4483
4.29k
      CmdArgs.push_back(A->getValue());
4484
4.29k
    } else {
4485
3
      assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
4486
3
      CmdArgs.push_back("-fvisibility");
4487
3
      CmdArgs.push_back("hidden");
4488
3
      CmdArgs.push_back("-ftype-visibility");
4489
3
      CmdArgs.push_back("default");
4490
3
    }
4491
4.29k
  }
4492
29.5k
4493
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
4494
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden);
4495
29.5k
4496
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
4497
29.5k
4498
29.5k
  // Forward -f (flag) options which we can pass directly.
4499
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
4500
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
4501
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs);
4502
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
4503
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_femulated_tls,
4504
29.5k
                  options::OPT_fno_emulated_tls);
4505
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fkeep_static_consts);
4506
29.5k
4507
29.5k
  // AltiVec-like language extensions aren't relevant for assembling.
4508
29.5k
  if (!isa<PreprocessJobAction>(JA) || 
Output.getType() != types::TY_PP_Asm1.80k
)
4509
28.8k
    Args.AddLastArg(CmdArgs, options::OPT_fzvector);
4510
29.5k
4511
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
4512
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
4513
29.5k
4514
29.5k
  // Forward flags for OpenMP. We don't do this if the current action is an
4515
29.5k
  // device offloading action other than OpenMP.
4516
29.5k
  if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
4517
29.5k
                   options::OPT_fno_openmp, false) &&
4518
29.5k
      
(587
JA.isDeviceOffloading(Action::OFK_None)587
||
4519
587
       
JA.isDeviceOffloading(Action::OFK_OpenMP)118
)) {
4520
586
    switch (D.getOpenMPRuntime(Args)) {
4521
586
    case Driver::OMPRT_OMP:
4522
564
    case Driver::OMPRT_IOMP5:
4523
564
      // Clang can generate useful OpenMP code for these two runtime libraries.
4524
564
      CmdArgs.push_back("-fopenmp");
4525
564
4526
564
      // If no option regarding the use of TLS in OpenMP codegeneration is
4527
564
      // given, decide a default based on the target. Otherwise rely on the
4528
564
      // options and pass the right information to the frontend.
4529
564
      if (!Args.hasFlag(options::OPT_fopenmp_use_tls,
4530
564
                        options::OPT_fnoopenmp_use_tls, /*Default=*/true))
4531
1
        CmdArgs.push_back("-fnoopenmp-use-tls");
4532
564
      Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd,
4533
564
                      options::OPT_fno_openmp_simd);
4534
564
      Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ);
4535
564
      Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_number_of_sm_EQ);
4536
564
      Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_blocks_per_sm_EQ);
4537
564
      Args.AddAllArgs(CmdArgs,
4538
564
                      options::OPT_fopenmp_cuda_teams_reduction_recs_num_EQ);
4539
564
      if (Args.hasFlag(options::OPT_fopenmp_optimistic_collapse,
4540
564
                       options::OPT_fno_openmp_optimistic_collapse,
4541
564
                       /*Default=*/false))
4542
0
        CmdArgs.push_back("-fopenmp-optimistic-collapse");
4543
564
4544
564
      // When in OpenMP offloading mode with NVPTX target, forward
4545
564
      // cuda-mode flag
4546
564
      if (Args.hasFlag(options::OPT_fopenmp_cuda_mode,
4547
564
                       options::OPT_fno_openmp_cuda_mode, /*Default=*/false))
4548
8
        CmdArgs.push_back("-fopenmp-cuda-mode");
4549
564
4550
564
      // When in OpenMP offloading mode with NVPTX target, check if full runtime
4551
564
      // is required.
4552
564
      if (Args.hasFlag(options::OPT_fopenmp_cuda_force_full_runtime,
4553
564
                       options::OPT_fno_openmp_cuda_force_full_runtime,
4554
564
                       /*Default=*/false))
4555
8
        CmdArgs.push_back("-fopenmp-cuda-force-full-runtime");
4556
564
      break;
4557
564
    default:
4558
22
      // By default, if Clang doesn't know how to generate useful OpenMP code
4559
22
      // for a specific runtime library, we just don't pass the '-fopenmp' flag
4560
22
      // down to the actual compilation.
4561
22
      // FIXME: It would be better to have a mode which *only* omits IR
4562
22
      // generation based on the OpenMP support so that we get consistent
4563
22
      // semantic analysis, etc.
4564
22
      break;
4565
29.0k
    }
4566
29.0k
  } else {
4567
29.0k
    Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd,
4568
29.0k
                    options::OPT_fno_openmp_simd);
4569
29.0k
    Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ);
4570
29.0k
  }
4571
29.5k
4572
29.5k
  const SanitizerArgs &Sanitize = TC.getSanitizerArgs();
4573
29.5k
  Sanitize.addArgs(TC, Args, CmdArgs, InputType);
4574
29.5k
4575
29.5k
  const XRayArgs &XRay = TC.getXRayArgs();
4576
29.5k
  XRay.addArgs(TC, Args, CmdArgs, InputType);
4577
29.5k
4578
29.5k
  if (TC.SupportsProfiling())
4579
18.6k
    Args.AddLastArg(CmdArgs, options::OPT_pg);
4580
29.5k
4581
29.5k
  if (TC.SupportsProfiling())
4582
18.6k
    Args.AddLastArg(CmdArgs, options::OPT_mfentry);
4583
29.5k
4584
29.5k
  // -flax-vector-conversions is default.
4585
29.5k
  if (!Args.hasFlag(options::OPT_flax_vector_conversions,
4586
29.5k
                    options::OPT_fno_lax_vector_conversions))
4587
0
    CmdArgs.push_back("-fno-lax-vector-conversions");
4588
29.5k
4589
29.5k
  if (Args.getLastArg(options::OPT_fapple_kext) ||
4590
29.5k
      
(29.5k
Args.hasArg(options::OPT_mkernel)29.5k
&&
types::isCXX(InputType)1.03k
))
4591
8
    CmdArgs.push_back("-fapple-kext");
4592
29.5k
4593
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
4594
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
4595
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
4596
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
4597
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_ftime_trace);
4598
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
4599
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_malign_double);
4600
29.5k
4601
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
4602
0
    CmdArgs.push_back("-ftrapv-handler");
4603
0
    CmdArgs.push_back(A->getValue());
4604
0
  }
4605
29.5k
4606
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
4607
29.5k
4608
29.5k
  // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
4609
29.5k
  // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
4610
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fwrapv, options::OPT_fno_wrapv)) {
4611
3
    if (A->getOption().matches(options::OPT_fwrapv))
4612
2
      CmdArgs.push_back("-fwrapv");
4613
29.5k
  } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
4614
1
                                      options::OPT_fno_strict_overflow)) {
4615
1
    if (A->getOption().matches(options::OPT_fno_strict_overflow))
4616
1
      CmdArgs.push_back("-fwrapv");
4617
1
  }
4618
29.5k
4619
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_freroll_loops,
4620
4
                               options::OPT_fno_reroll_loops))
4621
4
    if (A->getOption().matches(options::OPT_freroll_loops))
4622
2
      CmdArgs.push_back("-freroll-loops");
4623
29.5k
4624
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
4625
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_funroll_loops,
4626
29.5k
                  options::OPT_fno_unroll_loops);
4627
29.5k
4628
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_pthread);
4629
29.5k
4630
29.5k
  if (Args.hasFlag(options::OPT_mspeculative_load_hardening, options::OPT_mno_speculative_load_hardening,
4631
29.5k
                   false))
4632
2
    CmdArgs.push_back(Args.MakeArgString("-mspeculative-load-hardening"));
4633
29.5k
4634
29.5k
  RenderSSPOptions(TC, Args, CmdArgs, KernelOrKext);
4635
29.5k
  RenderTrivialAutoVarInitOptions(D, TC, Args, CmdArgs);
4636
29.5k
4637
29.5k
  // Translate -mstackrealign
4638
29.5k
  if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
4639
29.5k
                   false))
4640
1
    CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
4641
29.5k
4642
29.5k
  if (Args.hasArg(options::OPT_mstack_alignment)) {
4643
0
    StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
4644
0
    CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
4645
0
  }
4646
29.5k
4647
29.5k
  if (Args.hasArg(options::OPT_mstack_probe_size)) {
4648
3
    StringRef Size = Args.getLastArgValue(options::OPT_mstack_probe_size);
4649
3
4650
3
    if (!Size.empty())
4651
3
      CmdArgs.push_back(Args.MakeArgString("-mstack-probe-size=" + Size));
4652
0
    else
4653
0
      CmdArgs.push_back("-mstack-probe-size=0");
4654
3
  }
4655
29.5k
4656
29.5k
  if (!Args.hasFlag(options::OPT_mstack_arg_probe,
4657
29.5k
                    options::OPT_mno_stack_arg_probe, true))
4658
1
    CmdArgs.push_back(Args.MakeArgString("-mno-stack-arg-probe"));
4659
29.5k
4660
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it,
4661
4
                               options::OPT_mno_restrict_it)) {
4662
4
    if (A->getOption().matches(options::OPT_mrestrict_it)) {
4663
2
      CmdArgs.push_back("-mllvm");
4664
2
      CmdArgs.push_back("-arm-restrict-it");
4665
2
    } else {
4666
2
      CmdArgs.push_back("-mllvm");
4667
2
      CmdArgs.push_back("-arm-no-restrict-it");
4668
2
    }
4669
29.5k
  } else if (Triple.isOSWindows() &&
4670
29.5k
             
(799
Triple.getArch() == llvm::Triple::arm799
||
4671
799
              Triple.getArch() == llvm::Triple::thumb)) {
4672
59
    // Windows on ARM expects restricted IT blocks
4673
59
    CmdArgs.push_back("-mllvm");
4674
59
    CmdArgs.push_back("-arm-restrict-it");
4675
59
  }
4676
29.5k
4677
29.5k
  // Forward -cl options to -cc1
4678
29.5k
  RenderOpenCLOptions(Args, CmdArgs);
4679
29.5k
4680
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) {
4681
7
    CmdArgs.push_back(
4682
7
        Args.MakeArgString(Twine("-fcf-protection=") + A->getValue()));
4683
7
  }
4684
29.5k
4685
29.5k
  // Forward -f options with positive and negative forms; we translate
4686
29.5k
  // these by hand.
4687
29.5k
  if (Arg *A = getLastProfileSampleUseArg(Args)) {
4688
4
    auto *PGOArg = Args.getLastArg(
4689
4
        options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ,
4690
4
        options::OPT_fcs_profile_generate, options::OPT_fcs_profile_generate_EQ,
4691
4
        options::OPT_fprofile_use, options::OPT_fprofile_use_EQ);
4692
4
    if (PGOArg)
4693
0
      D.Diag(diag::err_drv_argument_not_allowed_with)
4694
0
          << "SampleUse with PGO options";
4695
4
4696
4
    StringRef fname = A->getValue();
4697
4
    if (!llvm::sys::fs::exists(fname))
4698
0
      D.Diag(diag::err_drv_no_such_file) << fname;
4699
4
    else
4700
4
      A->render(Args, CmdArgs);
4701
4
  }
4702
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fprofile_remapping_file_EQ);
4703
29.5k
4704
29.5k
  RenderBuiltinOptions(TC, RawTriple, Args, CmdArgs);
4705
29.5k
4706
29.5k
  if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
4707
29.5k
                    options::OPT_fno_assume_sane_operator_new))
4708
0
    CmdArgs.push_back("-fno-assume-sane-operator-new");
4709
29.5k
4710
29.5k
  // -fblocks=0 is default.
4711
29.5k
  if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
4712
29.5k
                   TC.IsBlocksDefault()) ||
4713
29.5k
      
(8.68k
Args.hasArg(options::OPT_fgnu_runtime)8.68k
&&
4714
8.68k
       
Args.hasArg(options::OPT_fobjc_nonfragile_abi)0
&&
4715
20.9k
       
!Args.hasArg(options::OPT_fno_blocks)0
)) {
4716
20.9k
    CmdArgs.push_back("-fblocks");
4717
20.9k
4718
20.9k
    if (!Args.hasArg(options::OPT_fgnu_runtime) && 
!TC.hasBlocksRuntime()20.9k
)
4719
706
      CmdArgs.push_back("-fblocks-runtime-optional");
4720
20.9k
  }
4721
29.5k
4722
29.5k
  // -fencode-extended-block-signature=1 is default.
4723
29.5k
  if (TC.IsEncodeExtendedBlockSignatureDefault())
4724
20.8k
    CmdArgs.push_back("-fencode-extended-block-signature");
4725
29.5k
4726
29.5k
  if (Args.hasFlag(options::OPT_fcoroutines_ts, options::OPT_fno_coroutines_ts,
4727
29.5k
                   false) &&
4728
29.5k
      
types::isCXX(InputType)5
) {
4729
3
    CmdArgs.push_back("-fcoroutines-ts");
4730
3
  }
4731
29.5k
4732
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fdouble_square_bracket_attributes,
4733
29.5k
                  options::OPT_fno_double_square_bracket_attributes);
4734
29.5k
4735
29.5k
  // -faccess-control is default.
4736
29.5k
  if (Args.hasFlag(options::OPT_fno_access_control,
4737
29.5k
                   options::OPT_faccess_control, false))
4738
0
    CmdArgs.push_back("-fno-access-control");
4739
29.5k
4740
29.5k
  // -felide-constructors is the default.
4741
29.5k
  if (Args.hasFlag(options::OPT_fno_elide_constructors,
4742
29.5k
                   options::OPT_felide_constructors, false))
4743
1
    CmdArgs.push_back("-fno-elide-constructors");
4744
29.5k
4745
29.5k
  ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
4746
29.5k
4747
29.5k
  if (KernelOrKext || 
(28.5k
types::isCXX(InputType)28.5k
&&
4748
28.5k
                       
(RTTIMode == ToolChain::RM_Disabled)11.5k
))
4749
3.06k
    CmdArgs.push_back("-fno-rtti");
4750
29.5k
4751
29.5k
  // -fshort-enums=0 is default for all architectures except Hexagon.
4752
29.5k
  if (Args.hasFlag(options::OPT_fshort_enums, options::OPT_fno_short_enums,
4753
29.5k
                   TC.getArch() == llvm::Triple::hexagon))
4754
84
    CmdArgs.push_back("-fshort-enums");
4755
29.5k
4756
29.5k
  RenderCharacterOptions(Args, AuxTriple ? 
*AuxTriple237
:
RawTriple29.3k
, CmdArgs);
4757
29.5k
4758
29.5k
  // -fuse-cxa-atexit is default.
4759
29.5k
  if (!Args.hasFlag(
4760
29.5k
          options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
4761
29.5k
          !RawTriple.isOSWindows() &&
4762
29.5k
              
TC.getArch() != llvm::Triple::xcore28.8k
&&
4763
29.5k
              
(28.7k
(RawTriple.getVendor() != llvm::Triple::MipsTechnologies)28.7k
||
4764
28.7k
               
RawTriple.hasEnvironment()151
)) ||
4765
29.5k
      
KernelOrKext28.7k
)
4766
1.84k
    CmdArgs.push_back("-fno-use-cxa-atexit");
4767
29.5k
4768
29.5k
  if (Args.hasFlag(options::OPT_fregister_global_dtors_with_atexit,
4769
29.5k
                   options::OPT_fno_register_global_dtors_with_atexit,
4770
29.5k
                   RawTriple.isOSDarwin() && 
!KernelOrKext20.0k
))
4771
19.0k
    CmdArgs.push_back("-fregister-global-dtors-with-atexit");
4772
29.5k
4773
29.5k
  // -fms-extensions=0 is default.
4774
29.5k
  if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
4775
29.5k
                   IsWindowsMSVC))
4776
664
    CmdArgs.push_back("-fms-extensions");
4777
29.5k
4778
29.5k
  // -fno-use-line-directives is default.
4779
29.5k
  if (Args.hasFlag(options::OPT_fuse_line_directives,
4780
29.5k
                   options::OPT_fno_use_line_directives, false))
4781
0
    CmdArgs.push_back("-fuse-line-directives");
4782
29.5k
4783
29.5k
  // -fms-compatibility=0 is default.
4784
29.5k
  if (Args.hasFlag(options::OPT_fms_compatibility,
4785
29.5k
                   options::OPT_fno_ms_compatibility,
4786
29.5k
                   (IsWindowsMSVC &&
4787
29.5k
                    Args.hasFlag(options::OPT_fms_extensions,
4788
653
                                 options::OPT_fno_ms_extensions, true))))
4789
2.11k
    CmdArgs.push_back("-fms-compatibility");
4790
29.5k
4791
29.5k
  VersionTuple MSVT = TC.computeMSVCVersion(&D, Args);
4792
29.5k
  if (!MSVT.empty())
4793
659
    CmdArgs.push_back(
4794
659
        Args.MakeArgString("-fms-compatibility-version=" + MSVT.getAsString()));
4795
29.5k
4796
29.5k
  bool IsMSVC2015Compatible = MSVT.getMajor() >= 19;
4797
29.5k
  if (ImplyVCPPCXXVer) {
4798
142
    StringRef LanguageStandard;
4799
142
    if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) {
4800
5
      Std = StdArg;
4801
5
      LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue())
4802
5
                             .Case("c++14", "-std=c++14")
4803
5
                             .Case("c++17", "-std=c++17")
4804
5
                             .Case("c++latest", "-std=c++2a")
4805
5
                             .Default("");
4806
5
      if (LanguageStandard.empty())
4807
0
        D.Diag(clang::diag::warn_drv_unused_argument)
4808
0
            << StdArg->getAsString(Args);
4809
5
    }
4810
142
4811
142
    if (LanguageStandard.empty()) {
4812
137
      if (IsMSVC2015Compatible)
4813
136
        LanguageStandard = "-std=c++14";
4814
1
      else
4815
1
        LanguageStandard = "-std=c++11";
4816
137
    }
4817
142
4818
142
    CmdArgs.push_back(LanguageStandard.data());
4819
142
  }
4820
29.5k
4821
29.5k
  // -fno-borland-extensions is default.
4822
29.5k
  if (Args.hasFlag(options::OPT_fborland_extensions,
4823
29.5k
                   options::OPT_fno_borland_extensions, false))
4824
0
    CmdArgs.push_back("-fborland-extensions");
4825
29.5k
4826
29.5k
  // -fno-declspec is default, except for PS4.
4827
29.5k
  if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec,
4828
29.5k
                   RawTriple.isPS4()))
4829
119
    CmdArgs.push_back("-fdeclspec");
4830
29.4k
  else if (Args.hasArg(options::OPT_fno_declspec))
4831
2
    CmdArgs.push_back("-fno-declspec"); // Explicitly disabling __declspec.
4832
29.5k
4833
29.5k
  // -fthreadsafe-static is default, except for MSVC compatibility versions less
4834
29.5k
  // than 19.
4835
29.5k
  if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
4836
29.5k
                    options::OPT_fno_threadsafe_statics,
4837
29.5k
                    !IsWindowsMSVC || 
IsMSVC2015Compatible653
))
4838
19
    CmdArgs.push_back("-fno-threadsafe-statics");
4839
29.5k
4840
29.5k
  // -fno-delayed-template-parsing is default, except when targeting MSVC.
4841
29.5k
  // Many old Windows SDK versions require this to parse.
4842
29.5k
  // FIXME: MSVC introduced /Zc:twoPhase- to disable this behavior in their
4843
29.5k
  // compiler. We should be able to disable this by default at some point.
4844
29.5k
  if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
4845
29.5k
                   options::OPT_fno_delayed_template_parsing, IsWindowsMSVC))
4846
2.10k
    CmdArgs.push_back("-fdelayed-template-parsing");
4847
29.5k
4848
29.5k
  // -fgnu-keywords default varies depending on language; only pass if
4849
29.5k
  // specified.
4850
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fgnu_keywords,
4851
29.5k
                  options::OPT_fno_gnu_keywords);
4852
29.5k
4853
29.5k
  if (Args.hasFlag(options::OPT_fgnu89_inline, options::OPT_fno_gnu89_inline,
4854
29.5k
                   false))
4855
1
    CmdArgs.push_back("-fgnu89-inline");
4856
29.5k
4857
29.5k
  if (Args.hasArg(options::OPT_fno_inline))
4858
2
    CmdArgs.push_back("-fno-inline");
4859
29.5k
4860
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_finline_functions,
4861
29.5k
                  options::OPT_finline_hint_functions,
4862
29.5k
                  options::OPT_fno_inline_functions);
4863
29.5k
4864
29.5k
  // FIXME: Find a better way to determine whether the language has modules
4865
29.5k
  // support by default, or just assume that all languages do.
4866
29.5k
  bool HaveModules =
4867
29.5k
      Std && 
(8.87k
Std->containsValue("c++2a")8.87k
||
Std->containsValue("c++latest")8.82k
);
4868
29.5k
  RenderModulesOptions(C, D, Args, Input, Output, CmdArgs, HaveModules);
4869
29.5k
4870
29.5k
  Args.AddLastArg(CmdArgs, options::OPT_fexperimental_new_pass_manager,
4871
29.5k
                  options::OPT_fno_experimental_new_pass_manager);
4872
29.5k
4873
29.5k
  ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
4874
29.5k
  RenderObjCOptions(TC, D, RawTriple, Args, Runtime, rewriteKind != RK_None,
4875
29.5k
                    Input, CmdArgs);
4876
29.5k
4877
29.5k
  if (Args.hasFlag(options::OPT_fapplication_extension,
4878
29.5k
                   options::OPT_fno_application_extension, false))
4879
1
    CmdArgs.push_back("-fapplication-extension");
4880
29.5k
4881
29.5k
  // Handle GCC-style exception args.
4882
29.5k
  if (!C.getDriver().IsCLMode())
4883
29.0k
    addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs);
4884
29.5k
4885
29.5k
  // Handle exception personalities
4886
29.5k
  Arg *A = Args.getLastArg(options::OPT_fsjlj_exceptions,
4887
29.5k
                           options::OPT_fseh_exceptions,
4888
29.5k
                           options::OPT_fdwarf_exceptions);
4889
29.5k
  if (A) {
4890
7
    const Option &Opt = A->getOption();
4891
7
    if (Opt.matches(options::OPT_fsjlj_exceptions))
4892
3
      CmdArgs.push_back("-fsjlj-exceptions");
4893
7
    if (Opt.matches(options::OPT_fseh_exceptions))
4894
1
      CmdArgs.push_back("-fseh-exceptions");
4895
7
    if (Opt.matches(options::OPT_fdwarf_exceptions))
4896
3
      CmdArgs.push_back("-fdwarf-exceptions");
4897
29.5k
  } else {
4898
29.5k
    switch (TC.GetExceptionModel(Args)) {
4899
29.5k
    default:
4900
27.3k
      break;
4901
29.5k
    case llvm::ExceptionHandling::DwarfCFI:
4902
777
      CmdArgs.push_back("-fdwarf-exceptions");
4903
777
      break;
4904
29.5k
    case llvm::ExceptionHandling::SjLj:
4905
1.43k
      CmdArgs.push_back("-fsjlj-exceptions");
4906
1.43k
      break;
4907
29.5k
    case llvm::ExceptionHandling::WinEH:
4908
37
      CmdArgs.push_back("-fseh-exceptions");
4909
37
      break;
4910
29.5k
    }
4911
29.5k
  }
4912
29.5k
4913
29.5k
  // C++ "sane" operator new.
4914
29.5k
  if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
4915
29.5k
                    options::OPT_fno_assume_sane_operator_new))
4916
0
    CmdArgs.push_back("-fno-assume-sane-operator-new");
4917
29.5k
4918
29.5k
  // -frelaxed-template-template-args is off by default, as it is a severe
4919
29.5k
  // breaking change until a corresponding change to template partial ordering
4920
29.5k
  // is provided.
4921
29.5k
  if (Args.hasFlag(options::OPT_frelaxed_template_template_args,
4922
29.5k
                   options::OPT_fno_relaxed_template_template_args, false))
4923
0
    CmdArgs.push_back("-frelaxed-template-template-args");
4924
29.5k
4925
29.5k
  // -fsized-deallocation is off by default, as it is an ABI-breaking change for
4926
29.5k
  // most platforms.
4927
29.5k
  if (Args.hasFlag(options::OPT_fsized_deallocation,
4928
29.5k
                   options::OPT_fno_sized_deallocation, false))
4929
1
    CmdArgs.push_back("-fsized-deallocation");
4930
29.5k
4931
29.5k
  // -faligned-allocation is on by default in C++17 onwards and otherwise off
4932
29.5k
  // by default.
4933
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_faligned_allocation,
4934
4
                               options::OPT_fno_aligned_allocation,
4935
4
                               options::OPT_faligned_new_EQ)) {
4936
4
    if (A->getOption().matches(options::OPT_fno_aligned_allocation))
4937
2
      CmdArgs.push_back("-fno-aligned-allocation");
4938
2
    else
4939
2
      CmdArgs.push_back("-faligned-allocation");
4940
4
  }
4941
29.5k
4942
29.5k
  // The default new alignment can be specified using a dedicated option or via
4943
29.5k
  // a GCC-compatible option that also turns on aligned allocation.
4944
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fnew_alignment_EQ,
4945
0
                               options::OPT_faligned_new_EQ))
4946
0
    CmdArgs.push_back(
4947
0
        Args.MakeArgString(Twine("-fnew-alignment=") + A->getValue()));
4948
29.5k
4949
29.5k
  // -fconstant-cfstrings is default, and may be subject to argument translation
4950
29.5k
  // on Darwin.
4951
29.5k
  if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
4952
29.5k
                    options::OPT_fno_constant_cfstrings) ||
4953
29.5k
      !Args.hasFlag(options::OPT_mconstant_cfstrings,
4954
29.5k
                    options::OPT_mno_constant_cfstrings))
4955
0
    CmdArgs.push_back("-fno-constant-cfstrings");
4956
29.5k
4957
29.5k
  // -fno-pascal-strings is default, only pass non-default.
4958
29.5k
  if (Args.hasFlag(options::OPT_fpascal_strings,
4959
29.5k
                   options::OPT_fno_pascal_strings, false))
4960
2
    CmdArgs.push_back("-fpascal-strings");
4961
29.5k
4962
29.5k
  // Honor -fpack-struct= and -fpack-struct, if given. Note that
4963
29.5k
  // -fno-pack-struct doesn't apply to -fpack-struct=.
4964
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
4965
3
    std::string PackStructStr = "-fpack-struct=";
4966
3
    PackStructStr += A->getValue();
4967
3
    CmdArgs.push_back(Args.MakeArgString(PackStructStr));
4968
29.5k
  } else if (Args.hasFlag(options::OPT_fpack_struct,
4969
29.5k
                          options::OPT_fno_pack_struct, false)) {
4970
1
    CmdArgs.push_back("-fpack-struct=1");
4971
1
  }
4972
29.5k
4973
29.5k
  // Handle -fmax-type-align=N and -fno-type-align
4974
29.5k
  bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align);
4975
29.5k
  if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) {
4976
2
    if (!SkipMaxTypeAlign) {
4977
1
      std::string MaxTypeAlignStr = "-fmax-type-align=";
4978
1
      MaxTypeAlignStr += A->getValue();
4979
1
      CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
4980
1
    }
4981
29.5k
  } else if (RawTriple.isOSDarwin()) {
4982
20.0k
    if (!SkipMaxTypeAlign) {
4983
20.0k
      std::string MaxTypeAlignStr = "-fmax-type-align=16";
4984
20.0k
      CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
4985
20.0k
    }
4986
20.0k
  }
4987
29.5k
4988
29.5k
  if (!Args.hasFlag(options::OPT_Qy, options::OPT_Qn, true))
4989
1
    CmdArgs.push_back("-Qn");
4990
29.5k
4991
29.5k
  // -fcommon is the default unless compiling kernel code or the target says so
4992
29.5k
  bool NoCommonDefault = KernelOrKext || 
isNoCommonDefault(RawTriple)28.5k
;
4993
29.5k
  if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common,
4994
29.5k
                    !NoCommonDefault))
4995
1.14k
    CmdArgs.push_back("-fno-common");
4996
29.5k
4997
29.5k
  // -fsigned-bitfields is default, and clang doesn't yet support
4998
29.5k
  // -funsigned-bitfields.
4999
29.5k
  if (!Args.hasFlag(options::OPT_fsigned_bitfields,
5000
29.5k
                    options::OPT_funsigned_bitfields))
5001
0
    D.Diag(diag::warn_drv_clang_unsupported)
5002
0
        << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
5003
29.5k
5004
29.5k
  // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
5005
29.5k
  if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope))
5006
0
    D.Diag(diag::err_drv_clang_unsupported)
5007
0
        << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
5008
29.5k
5009
29.5k
  // -finput_charset=UTF-8 is default. Reject others
5010
29.5k
  if (Arg *inputCharset = Args.getLastArg(options::OPT_finput_charset_EQ)) {
5011
4
    StringRef value = inputCharset->getValue();
5012
4
    if (!value.equals_lower("utf-8"))
5013
2
      D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args)