Coverage Report

Created: 2019-07-24 05:18

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