Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChain.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ToolChain.cpp - Collections of tools for one platform --------------===//
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/Driver/ToolChain.h"
10
#include "InputInfo.h"
11
#include "ToolChains/Arch/ARM.h"
12
#include "ToolChains/Clang.h"
13
#include "ToolChains/InterfaceStubs.h"
14
#include "ToolChains/Flang.h"
15
#include "clang/Basic/ObjCRuntime.h"
16
#include "clang/Basic/Sanitizers.h"
17
#include "clang/Config/config.h"
18
#include "clang/Driver/Action.h"
19
#include "clang/Driver/Driver.h"
20
#include "clang/Driver/DriverDiagnostic.h"
21
#include "clang/Driver/Job.h"
22
#include "clang/Driver/Options.h"
23
#include "clang/Driver/SanitizerArgs.h"
24
#include "clang/Driver/XRayArgs.h"
25
#include "llvm/ADT/STLExtras.h"
26
#include "llvm/ADT/SmallString.h"
27
#include "llvm/ADT/StringRef.h"
28
#include "llvm/ADT/Triple.h"
29
#include "llvm/ADT/Twine.h"
30
#include "llvm/Config/llvm-config.h"
31
#include "llvm/MC/MCTargetOptions.h"
32
#include "llvm/Option/Arg.h"
33
#include "llvm/Option/ArgList.h"
34
#include "llvm/Option/OptTable.h"
35
#include "llvm/Option/Option.h"
36
#include "llvm/Support/ErrorHandling.h"
37
#include "llvm/Support/FileSystem.h"
38
#include "llvm/Support/Path.h"
39
#include "llvm/Support/TargetParser.h"
40
#include "llvm/Support/TargetRegistry.h"
41
#include "llvm/Support/VersionTuple.h"
42
#include "llvm/Support/VirtualFileSystem.h"
43
#include <cassert>
44
#include <cstddef>
45
#include <cstring>
46
#include <string>
47
48
using namespace clang;
49
using namespace driver;
50
using namespace tools;
51
using namespace llvm;
52
using namespace llvm::opt;
53
54
30.1k
static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) {
55
30.1k
  return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
56
30.1k
                         options::OPT_fno_rtti, options::OPT_frtti);
57
30.1k
}
58
59
static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
60
                                             const llvm::Triple &Triple,
61
30.1k
                                             const Arg *CachedRTTIArg) {
62
30.1k
  // Explicit rtti/no-rtti args
63
30.1k
  if (CachedRTTIArg) {
64
6.06k
    if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
65
5.99k
      return ToolChain::RM_Enabled;
66
70
    else
67
70
      return ToolChain::RM_Disabled;
68
24.1k
  }
69
24.1k
70
24.1k
  // -frtti is default, except for the PS4 CPU.
71
24.1k
  return (Triple.isPS4CPU()) ? 
ToolChain::RM_Disabled118
:
ToolChain::RM_Enabled23.9k
;
72
24.1k
}
73
74
ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
75
                     const ArgList &Args)
76
    : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
77
30.1k
      CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) {
78
30.1k
  if (D.CCCIsCXX()) {
79
6.02k
    if (auto CXXStdlibPath = getCXXStdlibPath())
80
7
      getFilePaths().push_back(*CXXStdlibPath);
81
6.02k
  }
82
30.1k
83
30.1k
  if (auto RuntimePath = getRuntimePath())
84
31
    getLibraryPaths().push_back(*RuntimePath);
85
30.1k
86
30.1k
  std::string CandidateLibPath = getArchSpecificLibPath();
87
30.1k
  if (getVFS().exists(CandidateLibPath))
88
28
    getFilePaths().push_back(CandidateLibPath);
89
30.1k
}
90
91
81
void ToolChain::setTripleEnvironment(llvm::Triple::EnvironmentType Env) {
92
81
  Triple.setEnvironment(Env);
93
81
  if (EffectiveTriple != llvm::Triple())
94
0
    EffectiveTriple.setEnvironment(Env);
95
81
}
96
97
30.1k
ToolChain::~ToolChain() = default;
98
99
139k
llvm::vfs::FileSystem &ToolChain::getVFS() const {
100
139k
  return getDriver().getVFS();
101
139k
}
102
103
124k
bool ToolChain::useIntegratedAs() const {
104
124k
  return Args.hasFlag(options::OPT_fintegrated_as,
105
124k
                      options::OPT_fno_integrated_as,
106
124k
                      IsIntegratedAssemblerDefault());
107
124k
}
108
109
8.95k
bool ToolChain::useRelaxRelocations() const {
110
8.95k
  return ENABLE_X86_RELAX_RELOCATIONS;
111
8.95k
}
112
113
7.01k
bool ToolChain::isNoExecStackDefault() const {
114
7.01k
    return false;
115
7.01k
}
116
117
46.7k
const SanitizerArgs& ToolChain::getSanitizerArgs() const {
118
46.7k
  if (!SanitizerArguments.get())
119
28.5k
    SanitizerArguments.reset(new SanitizerArgs(*this, Args));
120
46.7k
  return *SanitizerArguments.get();
121
46.7k
}
122
123
31.0k
const XRayArgs& ToolChain::getXRayArgs() const {
124
31.0k
  if (!XRayArguments.get())
125
28.3k
    XRayArguments.reset(new XRayArgs(*this, Args));
126
31.0k
  return *XRayArguments.get();
127
31.0k
}
128
129
namespace {
130
131
struct DriverSuffix {
132
  const char *Suffix;
133
  const char *ModeFlag;
134
};
135
136
} // namespace
137
138
92.1k
static const DriverSuffix *FindDriverSuffix(StringRef ProgName, size_t &Pos) {
139
92.1k
  // A list of known driver suffixes. Suffixes are compared against the
140
92.1k
  // program name in order. If there is a match, the frontend type is updated as
141
92.1k
  // necessary by applying the ModeFlag.
142
92.1k
  static const DriverSuffix DriverSuffixes[] = {
143
92.1k
      {"clang", nullptr},
144
92.1k
      {"clang++", "--driver-mode=g++"},
145
92.1k
      {"clang-c++", "--driver-mode=g++"},
146
92.1k
      {"clang-cc", nullptr},
147
92.1k
      {"clang-cpp", "--driver-mode=cpp"},
148
92.1k
      {"clang-g++", "--driver-mode=g++"},
149
92.1k
      {"clang-gcc", nullptr},
150
92.1k
      {"clang-cl", "--driver-mode=cl"},
151
92.1k
      {"cc", nullptr},
152
92.1k
      {"cpp", "--driver-mode=cpp"},
153
92.1k
      {"cl", "--driver-mode=cl"},
154
92.1k
      {"++", "--driver-mode=g++"},
155
92.1k
      {"flang", "--driver-mode=flang"},
156
92.1k
  };
157
92.1k
158
569k
  for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); 
++i477k
) {
159
532k
    StringRef Suffix(DriverSuffixes[i].Suffix);
160
532k
    if (ProgName.endswith(Suffix)) {
161
55.4k
      Pos = ProgName.size() - Suffix.size();
162
55.4k
      return &DriverSuffixes[i];
163
55.4k
    }
164
532k
  }
165
92.1k
  
return nullptr36.7k
;
166
92.1k
}
167
168
/// Normalize the program name from argv[0] by stripping the file extension if
169
/// present and lower-casing the string on Windows.
170
55.6k
static std::string normalizeProgramName(llvm::StringRef Argv0) {
171
55.6k
  std::string ProgName = std::string(llvm::sys::path::stem(Argv0));
172
#ifdef _WIN32
173
  // Transform to lowercase for case insensitive file systems.
174
  std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower);
175
#endif
176
  return ProgName;
177
55.6k
}
178
179
55.6k
static const DriverSuffix *parseDriverSuffix(StringRef ProgName, size_t &Pos) {
180
55.6k
  // Try to infer frontend type and default target from the program name by
181
55.6k
  // comparing it against DriverSuffixes in order.
182
55.6k
183
55.6k
  // If there is a match, the function tries to identify a target as prefix.
184
55.6k
  // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target
185
55.6k
  // prefix "x86_64-linux". If such a target prefix is found, it may be
186
55.6k
  // added via -target as implicit first argument.
187
55.6k
  const DriverSuffix *DS = FindDriverSuffix(ProgName, Pos);
188
55.6k
189
55.6k
  if (!DS) {
190
18.2k
    // Try again after stripping any trailing version number:
191
18.2k
    // clang++3.5 -> clang++
192
18.2k
    ProgName = ProgName.rtrim("0123456789.");
193
18.2k
    DS = FindDriverSuffix(ProgName, Pos);
194
18.2k
  }
195
55.6k
196
55.6k
  if (!DS) {
197
18.2k
    // Try again after stripping trailing -component.
198
18.2k
    // clang++-tot -> clang++
199
18.2k
    ProgName = ProgName.slice(0, ProgName.rfind('-'));
200
18.2k
    DS = FindDriverSuffix(ProgName, Pos);
201
18.2k
  }
202
55.6k
  return DS;
203
55.6k
}
204
205
ParsedClangName
206
55.6k
ToolChain::getTargetAndModeFromProgramName(StringRef PN) {
207
55.6k
  std::string ProgName = normalizeProgramName(PN);
208
55.6k
  size_t SuffixPos;
209
55.6k
  const DriverSuffix *DS = parseDriverSuffix(ProgName, SuffixPos);
210
55.6k
  if (!DS)
211
216
    return {};
212
55.4k
  size_t SuffixEnd = SuffixPos + strlen(DS->Suffix);
213
55.4k
214
55.4k
  size_t LastComponent = ProgName.rfind('-', SuffixPos);
215
55.4k
  if (LastComponent == std::string::npos)
216
55.4k
    return ParsedClangName(ProgName.substr(0, SuffixEnd), DS->ModeFlag);
217
30
  std::string ModeSuffix = ProgName.substr(LastComponent + 1,
218
30
                                           SuffixEnd - LastComponent - 1);
219
30
220
30
  // Infer target from the prefix.
221
30
  StringRef Prefix(ProgName);
222
30
  Prefix = Prefix.slice(0, LastComponent);
223
30
  std::string IgnoredError;
224
30
  bool IsRegistered =
225
30
      llvm::TargetRegistry::lookupTarget(std::string(Prefix), IgnoredError);
226
30
  return ParsedClangName{std::string(Prefix), ModeSuffix, DS->ModeFlag,
227
30
                         IsRegistered};
228
30
}
229
230
22.1k
StringRef ToolChain::getDefaultUniversalArchName() const {
231
22.1k
  // In universal driver terms, the arch name accepted by -arch isn't exactly
232
22.1k
  // the same as the ones that appear in the triple. Roughly speaking, this is
233
22.1k
  // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
234
22.1k
  // only interesting special case is powerpc.
235
22.1k
  switch (Triple.getArch()) {
236
12
  case llvm::Triple::ppc:
237
12
    return "ppc";
238
9
  case llvm::Triple::ppc64:
239
9
    return "ppc64";
240
0
  case llvm::Triple::ppc64le:
241
0
    return "ppc64le";
242
22.1k
  default:
243
22.1k
    return Triple.getArchName();
244
22.1k
  }
245
22.1k
}
246
247
374
std::string ToolChain::getInputFilename(const InputInfo &Input) const {
248
374
  return Input.getFilename();
249
374
}
250
251
768
bool ToolChain::IsUnwindTablesDefault(const ArgList &Args) const {
252
768
  return false;
253
768
}
254
255
26.2k
Tool *ToolChain::getClang() const {
256
26.2k
  if (!Clang)
257
25.7k
    Clang.reset(new tools::Clang(*this));
258
26.2k
  return Clang.get();
259
26.2k
}
260
261
17
Tool *ToolChain::getFlang() const {
262
17
  if (!Flang)
263
16
    Flang.reset(new tools::Flang(*this));
264
17
  return Flang.get();
265
17
}
266
267
0
Tool *ToolChain::buildAssembler() const {
268
0
  return new tools::ClangAs(*this);
269
0
}
270
271
0
Tool *ToolChain::buildLinker() const {
272
0
  llvm_unreachable("Linking is not supported by this toolchain");
273
0
}
274
275
742
Tool *ToolChain::getAssemble() const {
276
742
  if (!Assemble)
277
718
    Assemble.reset(buildAssembler());
278
742
  return Assemble.get();
279
742
}
280
281
326
Tool *ToolChain::getClangAs() const {
282
326
  if (!Assemble)
283
311
    Assemble.reset(new tools::ClangAs(*this));
284
326
  return Assemble.get();
285
326
}
286
287
6.29k
Tool *ToolChain::getLink() const {
288
6.29k
  if (!Link)
289
6.25k
    Link.reset(buildLinker());
290
6.29k
  return Link.get();
291
6.29k
}
292
293
20
Tool *ToolChain::getIfsMerge() const {
294
20
  if (!IfsMerge)
295
20
    IfsMerge.reset(new tools::ifstool::Merger(*this));
296
20
  return IfsMerge.get();
297
20
}
298
299
33
Tool *ToolChain::getOffloadBundler() const {
300
33
  if (!OffloadBundler)
301
28
    OffloadBundler.reset(new tools::OffloadBundler(*this));
302
33
  return OffloadBundler.get();
303
33
}
304
305
57
Tool *ToolChain::getOffloadWrapper() const {
306
57
  if (!OffloadWrapper)
307
57
    OffloadWrapper.reset(new tools::OffloadWrapper(*this));
308
57
  return OffloadWrapper.get();
309
57
}
310
311
7.18k
Tool *ToolChain::getTool(Action::ActionClass AC) const {
312
7.18k
  switch (AC) {
313
742
  case Action::AssembleJobClass:
314
742
    return getAssemble();
315
0
316
20
  case Action::IfsMergeJobClass:
317
20
    return getIfsMerge();
318
0
319
6.29k
  case Action::LinkJobClass:
320
6.29k
    return getLink();
321
0
322
0
  case Action::InputClass:
323
0
  case Action::BindArchClass:
324
0
  case Action::OffloadClass:
325
0
  case Action::LipoJobClass:
326
0
  case Action::DsymutilJobClass:
327
0
  case Action::VerifyDebugInfoJobClass:
328
0
    llvm_unreachable("Invalid tool kind.");
329
0
330
39
  case Action::CompileJobClass:
331
39
  case Action::PrecompileJobClass:
332
39
  case Action::HeaderModulePrecompileJobClass:
333
39
  case Action::PreprocessJobClass:
334
39
  case Action::AnalyzeJobClass:
335
39
  case Action::MigrateJobClass:
336
39
  case Action::VerifyPCHJobClass:
337
39
  case Action::BackendJobClass:
338
39
    return getClang();
339
39
340
39
  case Action::OffloadBundlingJobClass:
341
33
  case Action::OffloadUnbundlingJobClass:
342
33
    return getOffloadBundler();
343
33
344
57
  case Action::OffloadWrapperJobClass:
345
57
    return getOffloadWrapper();
346
0
  }
347
0
348
0
  llvm_unreachable("Invalid tool kind.");
349
0
}
350
351
static StringRef getArchNameForCompilerRTLib(const ToolChain &TC,
352
1.10k
                                             const ArgList &Args) {
353
1.10k
  const llvm::Triple &Triple = TC.getTriple();
354
1.10k
  bool IsWindows = Triple.isOSWindows();
355
1.10k
356
1.10k
  if (TC.getArch() == llvm::Triple::arm || 
TC.getArch() == llvm::Triple::armeb1.03k
)
357
67
    return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && 
!IsWindows24
)
358
67
               ? 
"armhf"12
359
67
               : 
"arm"55
;
360
1.03k
361
1.03k
  // For historic reasons, Android library is using i686 instead of i386.
362
1.03k
  if (TC.getArch() == llvm::Triple::x86 && 
Triple.isAndroid()109
)
363
1
    return "i686";
364
1.03k
365
1.03k
  return llvm::Triple::getArchTypeName(TC.getArch());
366
1.03k
}
367
368
39.0k
StringRef ToolChain::getOSLibName() const {
369
39.0k
  switch (Triple.getOS()) {
370
475
  case llvm::Triple::FreeBSD:
371
475
    return "freebsd";
372
997
  case llvm::Triple::NetBSD:
373
997
    return "netbsd";
374
205
  case llvm::Triple::OpenBSD:
375
205
    return "openbsd";
376
189
  case llvm::Triple::Solaris:
377
189
    return "sunos";
378
37.1k
  default:
379
37.1k
    return getOS();
380
39.0k
  }
381
39.0k
}
382
383
9.85k
std::string ToolChain::getCompilerRTPath() const {
384
9.85k
  SmallString<128> Path(getDriver().ResourceDir);
385
9.85k
  if (Triple.isOSUnknown()) {
386
984
    llvm::sys::path::append(Path, "lib");
387
8.86k
  } else {
388
8.86k
    llvm::sys::path::append(Path, "lib", getOSLibName());
389
8.86k
  }
390
9.85k
  return std::string(Path.str());
391
9.85k
}
392
393
std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component,
394
1.15k
                                     FileType Type) const {
395
1.15k
  const llvm::Triple &TT = getTriple();
396
1.15k
  bool IsITANMSVCWindows =
397
1.15k
      TT.isWindowsMSVCEnvironment() || 
TT.isWindowsItaniumEnvironment()1.06k
;
398
1.15k
399
1.15k
  const char *Prefix =
400
1.15k
      IsITANMSVCWindows || 
Type == ToolChain::FT_Object1.05k
?
""297
:
"lib"853
;
401
1.15k
  const char *Suffix;
402
1.15k
  switch (Type) {
403
200
  case ToolChain::FT_Object:
404
200
    Suffix = IsITANMSVCWindows ? 
".obj"0
: ".o";
405
200
    break;
406
907
  case ToolChain::FT_Static:
407
907
    Suffix = IsITANMSVCWindows ? 
".lib"97
:
".a"810
;
408
907
    break;
409
43
  case ToolChain::FT_Shared:
410
43
    Suffix = Triple.isOSWindows()
411
43
                 ? 
(Triple.isWindowsGNUEnvironment() 2
?
".dll.a"2
:
".lib"0
)
412
43
                 : 
".so"41
;
413
43
    break;
414
1.15k
  }
415
1.15k
416
1.15k
  for (const auto &LibPath : getLibraryPaths()) {
417
50
    SmallString<128> P(LibPath);
418
50
    llvm::sys::path::append(P, Prefix + Twine("clang_rt.") + Component + Suffix);
419
50
    if (getVFS().exists(P))
420
48
      return std::string(P.str());
421
50
  }
422
1.15k
423
1.15k
  StringRef Arch = getArchNameForCompilerRTLib(*this, Args);
424
1.10k
  const char *Env = TT.isAndroid() ? 
"-android"29
:
""1.07k
;
425
1.10k
  SmallString<128> Path(getCompilerRTPath());
426
1.10k
  llvm::sys::path::append(Path, Prefix + Twine("clang_rt.") + Component + "-" +
427
1.10k
                                    Arch + Env + Suffix);
428
1.10k
  return std::string(Path.str());
429
1.15k
}
430
431
const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args,
432
                                              StringRef Component,
433
786
                                              FileType Type) const {
434
786
  return Args.MakeArgString(getCompilerRT(Args, Component, Type));
435
786
}
436
437
438
30.1k
Optional<std::string> ToolChain::getRuntimePath() const {
439
30.1k
  SmallString<128> P;
440
30.1k
441
30.1k
  // First try the triple passed to driver as --target=<triple>.
442
30.1k
  P.assign(D.ResourceDir);
443
30.1k
  llvm::sys::path::append(P, "lib", D.getTargetTriple());
444
30.1k
  if (getVFS().exists(P))
445
31
    return llvm::Optional<std::string>(std::string(P.str()));
446
30.1k
447
30.1k
  // Second try the normalized triple.
448
30.1k
  P.assign(D.ResourceDir);
449
30.1k
  llvm::sys::path::append(P, "lib", Triple.str());
450
30.1k
  if (getVFS().exists(P))
451
0
    return llvm::Optional<std::string>(std::string(P.str()));
452
30.1k
453
30.1k
  return None;
454
30.1k
}
455
456
6.06k
Optional<std::string> ToolChain::getCXXStdlibPath() const {
457
6.06k
  SmallString<128> P;
458
6.06k
459
6.06k
  // First try the triple passed to driver as --target=<triple>.
460
6.06k
  P.assign(D.Dir);
461
6.06k
  llvm::sys::path::append(P, "..", "lib", D.getTargetTriple(), "c++");
462
6.06k
  if (getVFS().exists(P))
463
34
    return llvm::Optional<std::string>(std::string(P.str()));
464
6.03k
465
6.03k
  // Second try the normalized triple.
466
6.03k
  P.assign(D.Dir);
467
6.03k
  llvm::sys::path::append(P, "..", "lib", Triple.str(), "c++");
468
6.03k
  if (getVFS().exists(P))
469
0
    return llvm::Optional<std::string>(std::string(P.str()));
470
6.03k
471
6.03k
  return None;
472
6.03k
}
473
474
30.1k
std::string ToolChain::getArchSpecificLibPath() const {
475
30.1k
  SmallString<128> Path(getDriver().ResourceDir);
476
30.1k
  llvm::sys::path::append(Path, "lib", getOSLibName(),
477
30.1k
                          llvm::Triple::getArchTypeName(getArch()));
478
30.1k
  return std::string(Path.str());
479
30.1k
}
480
481
5.27k
bool ToolChain::needsProfileRT(const ArgList &Args) {
482
5.27k
  if (Args.hasArg(options::OPT_noprofilelib))
483
2
    return false;
484
5.26k
485
5.26k
  if (needsGCovInstrumentation(Args) ||
486
5.26k
      
Args.hasArg(options::OPT_fprofile_generate)5.25k
||
487
5.26k
      
Args.hasArg(options::OPT_fprofile_generate_EQ)5.25k
||
488
5.26k
      
Args.hasArg(options::OPT_fcs_profile_generate)5.25k
||
489
5.26k
      
Args.hasArg(options::OPT_fcs_profile_generate_EQ)5.25k
||
490
5.26k
      
Args.hasArg(options::OPT_fprofile_instr_generate)5.25k
||
491
5.26k
      
Args.hasArg(options::OPT_fprofile_instr_generate_EQ)5.21k
||
492
5.26k
      
Args.hasArg(options::OPT_fcreate_profile)5.21k
||
493
5.26k
      
Args.hasArg(options::OPT_forder_file_instrumentation)5.21k
)
494
57
    return true;
495
5.21k
496
5.21k
  return false;
497
5.21k
}
498
499
5.29k
bool ToolChain::needsGCovInstrumentation(const llvm::opt::ArgList &Args) {
500
5.29k
  return Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
501
5.29k
                      false) ||
502
5.29k
         
Args.hasArg(options::OPT_coverage)5.28k
;
503
5.29k
}
504
505
33.8k
Tool *ToolChain::SelectTool(const JobAction &JA) const {
506
33.8k
  if (D.IsFlangMode() && 
getDriver().ShouldUseFlangCompiler(JA)20
)
return getFlang()17
;
507
33.8k
  if (getDriver().ShouldUseClangCompiler(JA)) 
return getClang()26.2k
;
508
7.61k
  Action::ActionClass AC = JA.getKind();
509
7.61k
  if (AC == Action::AssembleJobClass && 
useIntegratedAs()1.06k
)
510
326
    return getClangAs();
511
7.28k
  return getTool(AC);
512
7.28k
}
513
514
8.74k
std::string ToolChain::GetFilePath(const char *Name) const {
515
8.74k
  return D.GetFilePath(Name, *this);
516
8.74k
}
517
518
7.47k
std::string ToolChain::GetProgramPath(const char *Name) const {
519
7.47k
  return D.GetProgramPath(Name, *this);
520
7.47k
}
521
522
5.25k
std::string ToolChain::GetLinkerPath() const {
523
5.25k
  const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ);
524
5.25k
  StringRef UseLinker = A ? 
A->getValue()209
:
CLANG_DEFAULT_LINKER5.04k
;
525
5.25k
526
5.25k
  if (llvm::sys::path::is_absolute(UseLinker)) {
527
1
    // If we're passed what looks like an absolute path, don't attempt to
528
1
    // second-guess that.
529
1
    if (llvm::sys::fs::can_execute(UseLinker))
530
0
      return std::string(UseLinker);
531
5.25k
  } else if (UseLinker.empty() || 
UseLinker == "ld"208
) {
532
5.17k
    // If we're passed -fuse-ld= with no argument, or with the argument ld,
533
5.17k
    // then use whatever the default system linker is.
534
5.17k
    return GetProgramPath(getDefaultLinker());
535
5.17k
  } else {
536
79
    llvm::SmallString<8> LinkerName;
537
79
    if (Triple.isOSDarwin())
538
0
      LinkerName.append("ld64.");
539
79
    else
540
79
      LinkerName.append("ld.");
541
79
    LinkerName.append(UseLinker);
542
79
543
79
    std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
544
79
    if (llvm::sys::fs::can_execute(LinkerPath))
545
30
      return LinkerPath;
546
50
  }
547
50
548
50
  if (A)
549
50
    getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
550
50
551
50
  return GetProgramPath(getDefaultLinker());
552
50
}
553
554
26.3k
types::ID ToolChain::LookupTypeForExtension(StringRef Ext) const {
555
26.3k
  types::ID id = types::lookupTypeForExtension(Ext);
556
26.3k
557
26.3k
  // Flang always runs the preprocessor and has no notion of "preprocessed
558
26.3k
  // fortran". Here, TY_PP_Fortran is coerced to TY_Fortran to avoid treating
559
26.3k
  // them differently.
560
26.3k
  if (D.IsFlangMode() && 
id == types::TY_PP_Fortran18
)
561
10
    id = types::TY_Fortran;
562
26.3k
563
26.3k
  return id;
564
26.3k
}
565
566
513
bool ToolChain::HasNativeLLVMSupport() const {
567
513
  return false;
568
513
}
569
570
2.12k
bool ToolChain::isCrossCompiling() const {
571
2.12k
  llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
572
2.12k
  switch (HostTriple.getArch()) {
573
0
  // The A32/T32/T16 instruction sets are not separate architectures in this
574
0
  // context.
575
0
  case llvm::Triple::arm:
576
0
  case llvm::Triple::armeb:
577
0
  case llvm::Triple::thumb:
578
0
  case llvm::Triple::thumbeb:
579
0
    return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
580
0
           getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
581
2.12k
  default:
582
2.12k
    return HostTriple.getArch() != getArch();
583
2.12k
  }
584
2.12k
}
585
586
9.37k
ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
587
9.37k
  return ObjCRuntime(isNonFragile ? 
ObjCRuntime::GNUstep740
:
ObjCRuntime::GCC8.63k
,
588
9.37k
                     VersionTuple());
589
9.37k
}
590
591
llvm::ExceptionHandling
592
8.92k
ToolChain::GetExceptionModel(const llvm::opt::ArgList &Args) const {
593
8.92k
  return llvm::ExceptionHandling::None;
594
8.92k
}
595
596
24
bool ToolChain::isThreadModelSupported(const StringRef Model) const {
597
24
  if (Model == "single") {
598
9
    // FIXME: 'single' is only supported on ARM and WebAssembly so far.
599
9
    return Triple.getArch() == llvm::Triple::arm ||
600
9
           
Triple.getArch() == llvm::Triple::armeb4
||
601
9
           
Triple.getArch() == llvm::Triple::thumb4
||
602
9
           
Triple.getArch() == llvm::Triple::thumbeb4
||
603
9
           
Triple.getArch() == llvm::Triple::wasm324
||
604
9
           
Triple.getArch() == llvm::Triple::wasm642
;
605
15
  } else if (Model == "posix")
606
7
    return true;
607
8
608
8
  return false;
609
8
}
610
611
std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
612
34.0k
                                         types::ID InputType) const {
613
34.0k
  switch (getTriple().getArch()) {
614
7.82k
  default:
615
7.82k
    return getTripleString();
616
0
617
22.8k
  case llvm::Triple::x86_64: {
618
22.8k
    llvm::Triple Triple = getTriple();
619
22.8k
    if (!Triple.isOSBinFormatMachO())
620
2.99k
      return getTripleString();
621
19.8k
622
19.8k
    if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
623
2
      // x86_64h goes in the triple. Other -march options just use the
624
2
      // vanilla triple we already have.
625
2
      StringRef MArch = A->getValue();
626
2
      if (MArch == "x86_64h")
627
0
        Triple.setArchName(MArch);
628
2
    }
629
19.8k
    return Triple.getTriple();
630
19.8k
  }
631
19.8k
  case llvm::Triple::aarch64: {
632
993
    llvm::Triple Triple = getTriple();
633
993
    if (!Triple.isOSBinFormatMachO())
634
905
      return getTripleString();
635
88
636
88
    // FIXME: older versions of ld64 expect the "arm64" component in the actual
637
88
    // triple string and query it to determine whether an LTO file can be
638
88
    // handled. Remove this when we don't care any more.
639
88
    Triple.setArchName("arm64");
640
88
    return Triple.getTriple();
641
88
  }
642
88
  case llvm::Triple::aarch64_32:
643
5
    return getTripleString();
644
2.37k
  case llvm::Triple::arm:
645
2.37k
  case llvm::Triple::armeb:
646
2.37k
  case llvm::Triple::thumb:
647
2.37k
  case llvm::Triple::thumbeb: {
648
2.37k
    // FIXME: Factor into subclasses.
649
2.37k
    llvm::Triple Triple = getTriple();
650
2.37k
    bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb ||
651
2.37k
                       
getTriple().getArch() == llvm::Triple::thumbeb2.23k
;
652
2.37k
653
2.37k
    // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
654
2.37k
    // '-mbig-endian'/'-EB'.
655
2.37k
    if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
656
218
                                 options::OPT_mbig_endian)) {
657
218
      IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
658
218
    }
659
2.37k
660
2.37k
    // Thumb2 is the default for V7 on Darwin.
661
2.37k
    //
662
2.37k
    // FIXME: Thumb should just be another -target-feaure, not in the triple.
663
2.37k
    StringRef MCPU, MArch;
664
2.37k
    if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
665
531
      MCPU = A->getValue();
666
2.37k
    if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
667
786
      MArch = A->getValue();
668
2.37k
    std::string CPU =
669
2.37k
        Triple.isOSBinFormatMachO()
670
2.37k
            ? 
tools::arm::getARMCPUForMArch(MArch, Triple).str()421
671
2.37k
            : 
tools::arm::getARMTargetCPU(MCPU, MArch, Triple)1.95k
;
672
2.37k
    StringRef Suffix =
673
2.37k
      tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
674
2.37k
    bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::ProfileKind::M;
675
2.37k
    bool ThumbDefault = IsMProfile || 
(2.12k
ARM::parseArchVersion(Suffix) == 72.12k
&&
676
2.12k
                                       
getTriple().isOSBinFormatMachO()880
);
677
2.37k
    // FIXME: this is invalid for WindowsCE
678
2.37k
    if (getTriple().isOSWindows())
679
79
      ThumbDefault = true;
680
2.37k
    std::string ArchName;
681
2.37k
    if (IsBigEndian)
682
270
      ArchName = "armeb";
683
2.10k
    else
684
2.10k
      ArchName = "arm";
685
2.37k
686
2.37k
    // Check if ARM ISA was explicitly selected (using -mno-thumb or -marm) for
687
2.37k
    // M-Class CPUs/architecture variants, which is not supported.
688
2.37k
    bool ARMModeRequested = !Args.hasFlag(options::OPT_mthumb,
689
2.37k
                                          options::OPT_mno_thumb, ThumbDefault);
690
2.37k
    if (IsMProfile && 
ARMModeRequested255
) {
691
6
      if (!MCPU.empty())
692
2
        getDriver().Diag(diag::err_cpu_unsupported_isa) << CPU << "ARM";
693
4
       else
694
4
        getDriver().Diag(diag::err_arch_unsupported_isa)
695
4
          << tools::arm::getARMArch(MArch, getTriple()) << "ARM";
696
6
    }
697
2.37k
698
2.37k
    // Check to see if an explicit choice to use thumb has been made via
699
2.37k
    // -mthumb. For assembler files we must check for -mthumb in the options
700
2.37k
    // passed to the assembler via -Wa or -Xassembler.
701
2.37k
    bool IsThumb = false;
702
2.37k
    if (InputType != types::TY_PP_Asm)
703
2.28k
      IsThumb = Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb,
704
2.28k
                              ThumbDefault);
705
99
    else {
706
99
      // Ideally we would check for these flags in
707
99
      // CollectArgsForIntegratedAssembler but we can't change the ArchName at
708
99
      // that point. There is no assembler equivalent of -mno-thumb, -marm, or
709
99
      // -mno-arm.
710
99
      for (const auto *A :
711
99
           Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
712
14
        for (StringRef Value : A->getValues()) {
713
14
          if (Value == "-mthumb")
714
2
            IsThumb = true;
715
14
        }
716
14
      }
717
99
    }
718
2.37k
    // Assembly files should start in ARM mode, unless arch is M-profile, or
719
2.37k
    // -mthumb has been passed explicitly to the assembler. Windows is always
720
2.37k
    // thumb.
721
2.37k
    if (IsThumb || 
IsMProfile1.47k
||
getTriple().isOSWindows()1.45k
) {
722
923
      if (IsBigEndian)
723
97
        ArchName = "thumbeb";
724
826
      else
725
826
        ArchName = "thumb";
726
923
    }
727
2.37k
    Triple.setArchName(ArchName + Suffix.str());
728
2.37k
729
2.37k
    return Triple.getTriple();
730
2.37k
  }
731
34.0k
  }
732
34.0k
}
733
734
std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
735
13.4k
                                                   types::ID InputType) const {
736
13.4k
  return ComputeLLVMTriple(Args, InputType);
737
13.4k
}
738
739
void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
740
4.44k
                                          ArgStringList &CC1Args) const {
741
4.44k
  // Each toolchain should provide the appropriate include flags.
742
4.44k
}
743
744
void ToolChain::addClangTargetOptions(
745
    const ArgList &DriverArgs, ArgStringList &CC1Args,
746
870
    Action::OffloadKind DeviceOffloadKind) const {}
747
748
9.28k
void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
749
750
void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args,
751
696
                                 llvm::opt::ArgStringList &CmdArgs) const {
752
696
  if (!needsProfileRT(Args)) 
return675
;
753
21
754
21
  CmdArgs.push_back(getCompilerRTArgString(Args, "profile"));
755
21
}
756
757
ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
758
9.00k
    const ArgList &Args) const {
759
9.00k
  const Arg* A = Args.getLastArg(options::OPT_rtlib_EQ);
760
9.00k
  StringRef LibName = A ? 
A->getValue()1.05k
:
CLANG_DEFAULT_RTLIB7.95k
;
761
9.00k
762
9.00k
  // Only use "platform" in tests to override CLANG_DEFAULT_RTLIB!
763
9.00k
  if (LibName == "compiler-rt")
764
100
    return ToolChain::RLT_CompilerRT;
765
8.90k
  else if (LibName == "libgcc")
766
55
    return ToolChain::RLT_Libgcc;
767
8.85k
  else if (LibName == "platform")
768
896
    return GetDefaultRuntimeLibType();
769
7.95k
770
7.95k
  if (A)
771
6
    getDriver().Diag(diag::err_drv_invalid_rtlib_name) << A->getAsString(Args);
772
7.95k
773
7.95k
  return GetDefaultRuntimeLibType();
774
7.95k
}
775
776
ToolChain::UnwindLibType ToolChain::GetUnwindLibType(
777
2.79k
    const ArgList &Args) const {
778
2.79k
  const Arg *A = Args.getLastArg(options::OPT_unwindlib_EQ);
779
2.79k
  StringRef LibName = A ? 
A->getValue()9
:
CLANG_DEFAULT_UNWINDLIB2.78k
;
780
2.79k
781
2.79k
  if (LibName == "none")
782
0
    return ToolChain::UNW_None;
783
2.79k
  else if (LibName == "platform" || LibName == "") {
784
2.78k
    ToolChain::RuntimeLibType RtLibType = GetRuntimeLibType(Args);
785
2.78k
    if (RtLibType == ToolChain::RLT_CompilerRT)
786
93
      return ToolChain::UNW_None;
787
2.69k
    else if (RtLibType == ToolChain::RLT_Libgcc)
788
2.69k
      return ToolChain::UNW_Libgcc;
789
9
  } else if (LibName == "libunwind") {
790
6
    if (GetRuntimeLibType(Args) == RLT_Libgcc)
791
6
      getDriver().Diag(diag::err_drv_incompatible_unwindlib);
792
6
    return ToolChain::UNW_CompilerRT;
793
6
  } else 
if (3
LibName == "libgcc"3
)
794
3
    return ToolChain::UNW_Libgcc;
795
0
796
0
  if (A)
797
0
    getDriver().Diag(diag::err_drv_invalid_unwindlib_name)
798
0
        << A->getAsString(Args);
799
0
800
0
  return GetDefaultUnwindLibType();
801
0
}
802
803
56.7k
ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
804
56.7k
  const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
805
56.7k
  StringRef LibName = A ? 
A->getValue()34.6k
:
CLANG_DEFAULT_CXX_STDLIB22.1k
;
806
56.7k
807
56.7k
  // Only use "platform" in tests to override CLANG_DEFAULT_CXX_STDLIB!
808
56.7k
  if (LibName == "libc++")
809
34.2k
    return ToolChain::CST_Libcxx;
810
22.5k
  else if (LibName == "libstdc++")
811
244
    return ToolChain::CST_Libstdcxx;
812
22.2k
  else if (LibName == "platform")
813
117
    return GetDefaultCXXStdlibType();
814
22.1k
815
22.1k
  if (A)
816
0
    getDriver().Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
817
22.1k
818
22.1k
  return GetDefaultCXXStdlibType();
819
22.1k
}
820
821
/// Utility function to add a system include directory to CC1 arguments.
822
/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
823
                                            ArgStringList &CC1Args,
824
68.4k
                                            const Twine &Path) {
825
68.4k
  CC1Args.push_back("-internal-isystem");
826
68.4k
  CC1Args.push_back(DriverArgs.MakeArgString(Path));
827
68.4k
}
828
829
/// Utility function to add a system include directory with extern "C"
830
/// semantics to CC1 arguments.
831
///
832
/// Note that this should be used rarely, and only for directories that
833
/// historically and for legacy reasons are treated as having implicit extern
834
/// "C" semantics. These semantics are *ignored* by and large today, but its
835
/// important to preserve the preprocessor changes resulting from the
836
/// classification.
837
/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
838
                                                   ArgStringList &CC1Args,
839
24.6k
                                                   const Twine &Path) {
840
24.6k
  CC1Args.push_back("-internal-externc-isystem");
841
24.6k
  CC1Args.push_back(DriverArgs.MakeArgString(Path));
842
24.6k
}
843
844
void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
845
                                                ArgStringList &CC1Args,
846
300
                                                const Twine &Path) {
847
300
  if (llvm::sys::fs::exists(Path))
848
296
    addExternCSystemInclude(DriverArgs, CC1Args, Path);
849
300
}
850
851
/// Utility function to add a list of system include directories to CC1.
852
/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
853
                                             ArgStringList &CC1Args,
854
0
                                             ArrayRef<StringRef> Paths) {
855
0
  for (const auto &Path : Paths) {
856
0
    CC1Args.push_back("-internal-isystem");
857
0
    CC1Args.push_back(DriverArgs.MakeArgString(Path));
858
0
  }
859
0
}
860
861
void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
862
11.7k
                                             ArgStringList &CC1Args) const {
863
11.7k
  // Header search paths should be handled by each of the subclasses.
864
11.7k
  // Historically, they have not been, and instead have been handled inside of
865
11.7k
  // the CC1-layer frontend. As the logic is hoisted out, this generic function
866
11.7k
  // will slowly stop being called.
867
11.7k
  //
868
11.7k
  // While it is being called, replicate a bit of a hack to propagate the
869
11.7k
  // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
870
11.7k
  // header search paths with it. Once all systems are overriding this
871
11.7k
  // function, the CC1 flag and this line can be removed.
872
11.7k
  DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
873
11.7k
}
874
875
void ToolChain::AddClangCXXStdlibIsystemArgs(
876
    const llvm::opt::ArgList &DriverArgs,
877
8
    llvm::opt::ArgStringList &CC1Args) const {
878
8
  DriverArgs.ClaimAllArgs(options::OPT_stdlibxx_isystem);
879
8
  if (!DriverArgs.hasArg(options::OPT_nostdincxx))
880
6
    for (const auto &P :
881
6
         DriverArgs.getAllArgValues(options::OPT_stdlibxx_isystem))
882
10
      addSystemInclude(DriverArgs, CC1Args, P);
883
8
}
884
885
3.60k
bool ToolChain::ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const {
886
3.60k
  return getDriver().CCCIsCXX() &&
887
3.60k
         !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
888
2.63k
                      options::OPT_nostdlibxx);
889
3.60k
}
890
891
void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
892
73
                                    ArgStringList &CmdArgs) const {
893
73
  assert(!Args.hasArg(options::OPT_nostdlibxx) &&
894
73
         "should not have called this");
895
73
  CXXStdlibType Type = GetCXXStdlibType(Args);
896
73
897
73
  switch (Type) {
898
28
  case ToolChain::CST_Libcxx:
899
28
    CmdArgs.push_back("-lc++");
900
28
    break;
901
0
902
45
  case ToolChain::CST_Libstdcxx:
903
45
    CmdArgs.push_back("-lstdc++");
904
45
    break;
905
73
  }
906
73
}
907
908
void ToolChain::AddFilePathLibArgs(const ArgList &Args,
909
1.84k
                                   ArgStringList &CmdArgs) const {
910
1.84k
  for (const auto &LibPath : getFilePaths())
911
5.01k
    if(LibPath.length() > 0)
912
4.96k
      CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
913
1.84k
}
914
915
void ToolChain::AddCCKextLibArgs(const ArgList &Args,
916
0
                                 ArgStringList &CmdArgs) const {
917
0
  CmdArgs.push_back("-lcc_kext");
918
0
}
919
920
bool ToolChain::isFastMathRuntimeAvailable(const ArgList &Args,
921
4.03k
                                           std::string &Path) const {
922
4.03k
  // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
923
4.03k
  // (to keep the linker options consistent with gcc and clang itself).
924
4.03k
  if (!isOptimizationLevelFast(Args)) {
925
4.01k
    // Check if -ffast-math or -funsafe-math.
926
4.01k
    Arg *A =
927
4.01k
      Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
928
4.01k
                      options::OPT_funsafe_math_optimizations,
929
4.01k
                      options::OPT_fno_unsafe_math_optimizations);
930
4.01k
931
4.01k
    if (!A || 
A->getOption().getID() == options::OPT_fno_fast_math20
||
932
4.01k
        
A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations13
)
933
4.00k
      return false;
934
36
  }
935
36
  // If crtfastmath.o exists add it to the arguments.
936
36
  Path = GetFilePath("crtfastmath.o");
937
36
  return (Path != "crtfastmath.o"); // Not found.
938
36
}
939
940
bool ToolChain::addFastMathRuntimeIfAvailable(const ArgList &Args,
941
1.42k
                                              ArgStringList &CmdArgs) const {
942
1.42k
  std::string Path;
943
1.42k
  if (isFastMathRuntimeAvailable(Args, Path)) {
944
8
    CmdArgs.push_back(Args.MakeArgString(Path));
945
8
    return true;
946
8
  }
947
1.41k
948
1.41k
  return false;
949
1.41k
}
950
951
28.4k
SanitizerMask ToolChain::getSupportedSanitizers() const {
952
28.4k
  // Return sanitizers which don't require runtime support and are not
953
28.4k
  // platform dependent.
954
28.4k
955
28.4k
  SanitizerMask Res = (SanitizerKind::Undefined & ~SanitizerKind::Vptr &
956
28.4k
                       ~SanitizerKind::Function) |
957
28.4k
                      (SanitizerKind::CFI & ~SanitizerKind::CFIICall) |
958
28.4k
                      SanitizerKind::CFICastStrict |
959
28.4k
                      SanitizerKind::FloatDivideByZero |
960
28.4k
                      SanitizerKind::UnsignedIntegerOverflow |
961
28.4k
                      SanitizerKind::ImplicitConversion |
962
28.4k
                      SanitizerKind::Nullability | SanitizerKind::LocalBounds;
963
28.4k
  if (getTriple().getArch() == llvm::Triple::x86 ||
964
28.4k
      
getTriple().getArch() == llvm::Triple::x86_6425.4k
||
965
28.4k
      
getTriple().getArch() == llvm::Triple::arm4.50k
||
966
28.4k
      
getTriple().getArch() == llvm::Triple::aarch643.01k
||
967
28.4k
      
getTriple().getArch() == llvm::Triple::wasm322.24k
||
968
28.4k
      
getTriple().getArch() == llvm::Triple::wasm642.19k
)
969
26.3k
    Res |= SanitizerKind::CFIICall;
970
28.4k
  if (getTriple().getArch() == llvm::Triple::x86_64 ||
971
28.4k
      
getTriple().getArch() == llvm::Triple::aarch647.49k
)
972
21.7k
    Res |= SanitizerKind::ShadowCallStack;
973
28.4k
  if (getTriple().getArch() == llvm::Triple::aarch64 ||
974
28.4k
      
getTriple().getArch() == llvm::Triple::aarch64_be27.7k
)
975
929
    Res |= SanitizerKind::MemTag;
976
28.4k
  return Res;
977
28.4k
}
978
979
void ToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
980
4
                                   ArgStringList &CC1Args) const {}
981
982
void ToolChain::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
983
0
                                    ArgStringList &CC1Args) const {}
984
985
38
static VersionTuple separateMSVCFullVersion(unsigned Version) {
986
38
  if (Version < 100)
987
2
    return VersionTuple(Version);
988
36
989
36
  if (Version < 10000)
990
34
    return VersionTuple(Version / 100, Version % 100);
991
2
992
2
  unsigned Build = 0, Factor = 1;
993
12
  for (; Version > 10000; 
Version = Version / 10, Factor = Factor * 1010
)
994
10
    Build = Build + (Version % 10) * Factor;
995
2
  return VersionTuple(Version / 100, Version % 100, Build);
996
2
}
997
998
VersionTuple
999
ToolChain::computeMSVCVersion(const Driver *D,
1000
27.2k
                              const llvm::opt::ArgList &Args) const {
1001
27.2k
  const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version);
1002
27.2k
  const Arg *MSCompatibilityVersion =
1003
27.2k
      Args.getLastArg(options::OPT_fms_compatibility_version);
1004
27.2k
1005
27.2k
  if (MSCVersion && 
MSCompatibilityVersion40
) {
1006
2
    if (D)
1007
1
      D->Diag(diag::err_drv_argument_not_allowed_with)
1008
1
          << MSCVersion->getAsString(Args)
1009
1
          << MSCompatibilityVersion->getAsString(Args);
1010
2
    return VersionTuple();
1011
2
  }
1012
27.2k
1013
27.2k
  if (MSCompatibilityVersion) {
1014
22
    VersionTuple MSVT;
1015
22
    if (MSVT.tryParse(MSCompatibilityVersion->getValue())) {
1016
0
      if (D)
1017
0
        D->Diag(diag::err_drv_invalid_value)
1018
0
            << MSCompatibilityVersion->getAsString(Args)
1019
0
            << MSCompatibilityVersion->getValue();
1020
22
    } else {
1021
22
      return MSVT;
1022
22
    }
1023
27.2k
  }
1024
27.2k
1025
27.2k
  if (MSCVersion) {
1026
38
    unsigned Version = 0;
1027
38
    if (StringRef(MSCVersion->getValue()).getAsInteger(10, Version)) {
1028
0
      if (D)
1029
0
        D->Diag(diag::err_drv_invalid_value)
1030
0
            << MSCVersion->getAsString(Args) << MSCVersion->getValue();
1031
38
    } else {
1032
38
      return separateMSVCFullVersion(Version);
1033
38
    }
1034
27.1k
  }
1035
27.1k
1036
27.1k
  return VersionTuple();
1037
27.1k
}
1038
1039
llvm::opt::DerivedArgList *ToolChain::TranslateOpenMPTargetArgs(
1040
    const llvm::opt::DerivedArgList &Args, bool SameTripleAsHost,
1041
77
    SmallVectorImpl<llvm::opt::Arg *> &AllocatedArgs) const {
1042
77
  DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
1043
77
  const OptTable &Opts = getDriver().getOpts();
1044
77
  bool Modified = false;
1045
77
1046
77
  // Handle -Xopenmp-target flags
1047
676
  for (auto *A : Args) {
1048
676
    // Exclude flags which may only apply to the host toolchain.
1049
676
    // Do not exclude flags when the host triple (AuxTriple)
1050
676
    // matches the current toolchain triple. If it is not present
1051
676
    // at all, target and host share a toolchain.
1052
676
    if (A->getOption().matches(options::OPT_m_Group)) {
1053
4
      if (SameTripleAsHost)
1054
1
        DAL->append(A);
1055
3
      else
1056
3
        Modified = true;
1057
4
      continue;
1058
4
    }
1059
672
1060
672
    unsigned Index;
1061
672
    unsigned Prev;
1062
672
    bool XOpenMPTargetNoTriple =
1063
672
        A->getOption().matches(options::OPT_Xopenmp_target);
1064
672
1065
672
    if (A->getOption().matches(options::OPT_Xopenmp_target_EQ)) {
1066
3
      // Passing device args: -Xopenmp-target=<triple> -opt=val.
1067
3
      if (A->getValue(0) == getTripleString())
1068
2
        Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
1069
1
      else
1070
1
        continue;
1071
669
    } else if (XOpenMPTargetNoTriple) {
1072
36
      // Passing device args: -Xopenmp-target -opt=val.
1073
36
      Index = Args.getBaseArgs().MakeIndex(A->getValue(0));
1074
633
    } else {
1075
633
      DAL->append(A);
1076
633
      continue;
1077
633
    }
1078
38
1079
38
    // Parse the argument to -Xopenmp-target.
1080
38
    Prev = Index;
1081
38
    std::unique_ptr<Arg> XOpenMPTargetArg(Opts.ParseOneArg(Args, Index));
1082
38
    if (!XOpenMPTargetArg || 
Index > Prev + 137
) {
1083
1
      getDriver().Diag(diag::err_drv_invalid_Xopenmp_target_with_args)
1084
1
          << A->getAsString(Args);
1085
1
      continue;
1086
1
    }
1087
37
    if (XOpenMPTargetNoTriple && 
XOpenMPTargetArg35
&&
1088
37
        
Args.getAllArgValues(options::OPT_fopenmp_targets_EQ).size() != 135
) {
1089
2
      getDriver().Diag(diag::err_drv_Xopenmp_target_missing_triple);
1090
2
      continue;
1091
2
    }
1092
35
    XOpenMPTargetArg->setBaseArg(A);
1093
35
    A = XOpenMPTargetArg.release();
1094
35
    AllocatedArgs.push_back(A);
1095
35
    DAL->append(A);
1096
35
    Modified = true;
1097
35
  }
1098
77
1099
77
  if (Modified)
1100
37
    return DAL;
1101
40
1102
40
  delete DAL;
1103
40
  return nullptr;
1104
40
}