Coverage Report

Created: 2018-07-20 23:04

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Driver/ToolChain.h
Line
Count
Source (jump to first uncovered line)
1
//===- ToolChain.h - Collections of tools for one platform ------*- C++ -*-===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#ifndef LLVM_CLANG_DRIVER_TOOLCHAIN_H
11
#define LLVM_CLANG_DRIVER_TOOLCHAIN_H
12
13
#include "clang/Basic/LLVM.h"
14
#include "clang/Basic/Sanitizers.h"
15
#include "clang/Driver/Action.h"
16
#include "clang/Driver/Multilib.h"
17
#include "clang/Driver/Types.h"
18
#include "llvm/ADT/ArrayRef.h"
19
#include "llvm/ADT/SmallVector.h"
20
#include "llvm/ADT/StringRef.h"
21
#include "llvm/ADT/Triple.h"
22
#include "llvm/MC/MCTargetOptions.h"
23
#include "llvm/Option/Option.h"
24
#include "llvm/Support/VersionTuple.h"
25
#include "llvm/Target/TargetOptions.h"
26
#include <cassert>
27
#include <memory>
28
#include <string>
29
#include <utility>
30
31
namespace llvm {
32
namespace opt {
33
34
class Arg;
35
class ArgList;
36
class DerivedArgList;
37
38
} // namespace opt
39
} // namespace llvm
40
41
namespace clang {
42
43
class ObjCRuntime;
44
45
namespace vfs {
46
47
class FileSystem;
48
49
} // namespace vfs
50
51
namespace driver {
52
53
class Driver;
54
class InputInfo;
55
class SanitizerArgs;
56
class Tool;
57
class XRayArgs;
58
59
/// Helper structure used to pass information extracted from clang executable
60
/// name such as `i686-linux-android-g++`.
61
0
struct ParsedClangName {
62
  /// Target part of the executable name, as `i686-linux-android`.
63
  std::string TargetPrefix;
64
65
  /// Driver mode part of the executable name, as `g++`.
66
  std::string ModeSuffix;
67
68
  /// Corresponding driver mode argument, as '--driver-mode=g++'
69
  const char *DriverMode = nullptr;
70
71
  /// True if TargetPrefix is recognized as a registered target name.
72
  bool TargetIsValid = false;
73
74
26.2k
  ParsedClangName() = default;
75
  ParsedClangName(std::string Suffix, const char *Mode)
76
61.7k
      : ModeSuffix(Suffix), DriverMode(Mode) {}
77
  ParsedClangName(std::string Target, std::string Suffix, const char *Mode,
78
                  bool IsRegistered)
79
      : TargetPrefix(Target), ModeSuffix(Suffix), DriverMode(Mode),
80
32
        TargetIsValid(IsRegistered) {}
81
82
26.0k
  bool isEmpty() const {
83
26.0k
    return TargetPrefix.empty() && 
ModeSuffix.empty()26.0k
&&
DriverMode == nullptr5.25k
;
84
26.0k
  }
85
};
86
87
/// ToolChain - Access to tools for a single platform.
88
class ToolChain {
89
public:
90
  using path_list = SmallVector<std::string, 16>;
91
92
  enum CXXStdlibType {
93
    CST_Libcxx,
94
    CST_Libstdcxx
95
  };
96
97
  enum RuntimeLibType {
98
    RLT_CompilerRT,
99
    RLT_Libgcc
100
  };
101
102
  enum RTTIMode {
103
    RM_Enabled,
104
    RM_Disabled,
105
  };
106
107
private:
108
  friend class RegisterEffectiveTriple;
109
110
  const Driver &D;
111
  llvm::Triple Triple;
112
  const llvm::opt::ArgList &Args;
113
114
  // We need to initialize CachedRTTIArg before CachedRTTIMode
115
  const llvm::opt::Arg *const CachedRTTIArg;
116
117
  const RTTIMode CachedRTTIMode;
118
119
  /// The list of toolchain specific path prefixes to search for files.
120
  path_list FilePaths;
121
122
  /// The list of toolchain specific path prefixes to search for programs.
123
  path_list ProgramPaths;
124
125
  mutable std::unique_ptr<Tool> Clang;
126
  mutable std::unique_ptr<Tool> Assemble;
127
  mutable std::unique_ptr<Tool> Link;
128
  mutable std::unique_ptr<Tool> OffloadBundler;
129
130
  Tool *getClang() const;
131
  Tool *getAssemble() const;
132
  Tool *getLink() const;
133
  Tool *getClangAs() const;
134
  Tool *getOffloadBundler() const;
135
136
  mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
137
  mutable std::unique_ptr<XRayArgs> XRayArguments;
138
139
  /// The effective clang triple for the current Job.
140
  mutable llvm::Triple EffectiveTriple;
141
142
  /// Set the toolchain's effective clang triple.
143
66.3k
  void setEffectiveTriple(llvm::Triple ET) const {
144
66.3k
    EffectiveTriple = std::move(ET);
145
66.3k
  }
146
147
protected:
148
  MultilibSet Multilibs;
149
150
  ToolChain(const Driver &D, const llvm::Triple &T,
151
            const llvm::opt::ArgList &Args);
152
153
  void setTripleEnvironment(llvm::Triple::EnvironmentType Env);
154
155
  virtual Tool *buildAssembler() const;
156
  virtual Tool *buildLinker() const;
157
  virtual Tool *getTool(Action::ActionClass AC) const;
158
159
  /// \name Utilities for implementing subclasses.
160
  ///@{
161
  static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
162
                               llvm::opt::ArgStringList &CC1Args,
163
                               const Twine &Path);
164
  static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
165
                                      llvm::opt::ArgStringList &CC1Args,
166
                                      const Twine &Path);
167
  static void
168
      addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
169
                                      llvm::opt::ArgStringList &CC1Args,
170
                                      const Twine &Path);
171
  static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
172
                                llvm::opt::ArgStringList &CC1Args,
173
                                ArrayRef<StringRef> Paths);
174
  ///@}
175
176
public:
177
  virtual ~ToolChain();
178
179
  // Accessors
180
181
691k
  const Driver &getDriver() const { return D; }
182
  vfs::FileSystem &getVFS() const;
183
1.17M
  const llvm::Triple &getTriple() const { return Triple; }
184
185
  /// Get the toolchain's aux triple, if it has one.
186
  ///
187
  /// Exactly what the aux triple represents depends on the toolchain, but for
188
  /// example when compiling CUDA code for the GPU, the triple might be NVPTX,
189
  /// while the aux triple is the host (CPU) toolchain, e.g. x86-linux-gnu.
190
187
  virtual const llvm::Triple *getAuxTriple() const { return nullptr; }
191
192
  /// Some toolchains need to modify the file name, for example to replace the
193
  /// extension for object files with .cubin for OpenMP offloading to Nvidia
194
  /// GPUs.
195
  virtual std::string getInputFilename(const InputInfo &Input) const;
196
197
360k
  llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
198
2
  StringRef getArchName() const { return Triple.getArchName(); }
199
0
  StringRef getPlatform() const { return Triple.getVendorName(); }
200
33.7k
  StringRef getOS() const { return Triple.getOSName(); }
201
202
  /// Provide the default architecture name (as expected by -arch) for
203
  /// this toolchain.
204
  StringRef getDefaultUniversalArchName() const;
205
206
16.3k
  std::string getTripleString() const {
207
16.3k
    return Triple.getTriple();
208
16.3k
  }
209
210
  /// Get the toolchain's effective clang triple.
211
98.0k
  const llvm::Triple &getEffectiveTriple() const {
212
98.0k
    assert(!EffectiveTriple.getTriple().empty() && "No effective triple");
213
98.0k
    return EffectiveTriple;
214
98.0k
  }
215
216
3.83k
  path_list &getFilePaths() { return FilePaths; }
217
8.58k
  const path_list &getFilePaths() const { return FilePaths; }
218
219
36.4k
  path_list &getProgramPaths() { return ProgramPaths; }
220
4.33k
  const path_list &getProgramPaths() const { return ProgramPaths; }
221
222
1
  const MultilibSet &getMultilibs() const { return Multilibs; }
223
224
  const SanitizerArgs& getSanitizerArgs() const;
225
226
  const XRayArgs& getXRayArgs() const;
227
228
  // Returns the Arg * that explicitly turned on/off rtti, or nullptr.
229
4
  const llvm::opt::Arg *getRTTIArg() const { return CachedRTTIArg; }
230
231
  // Returns the RTTIMode for the toolchain with the current arguments.
232
52.5k
  RTTIMode getRTTIMode() const { return CachedRTTIMode; }
233
234
  /// Return any implicit target and/or mode flag for an invocation of
235
  /// the compiler driver as `ProgName`.
236
  ///
237
  /// For example, when called with i686-linux-android-g++, the first element
238
  /// of the return value will be set to `"i686-linux-android"` and the second
239
  /// will be set to "--driver-mode=g++"`.
240
  /// It is OK if the target name is not registered. In this case the return
241
  /// value contains false in the field TargetIsValid.
242
  ///
243
  /// \pre `llvm::InitializeAllTargets()` has been called.
244
  /// \param ProgName The name the Clang driver was invoked with (from,
245
  /// e.g., argv[0]).
246
  /// \return A structure of type ParsedClangName that contains the executable
247
  /// name parts.
248
  static ParsedClangName getTargetAndModeFromProgramName(StringRef ProgName);
249
250
  // Tool access.
251
252
  /// TranslateArgs - Create a new derived argument list for any argument
253
  /// translations this ToolChain may wish to perform, or 0 if no tool chain
254
  /// specific translations are needed. If \p DeviceOffloadKind is specified
255
  /// the translation specific for that offload kind is performed.
256
  ///
257
  /// \param BoundArch - The bound architecture name, or 0.
258
  /// \param DeviceOffloadKind - The device offload kind used for the
259
  /// translation.
260
  virtual llvm::opt::DerivedArgList *
261
  TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
262
340
                Action::OffloadKind DeviceOffloadKind) const {
263
340
    return nullptr;
264
340
  }
265
266
  /// TranslateOpenMPTargetArgs - Create a new derived argument list for
267
  /// that contains the OpenMP target specific flags passed via
268
  /// -Xopenmp-target -opt=val OR -Xopenmp-target=<triple> -opt=val
269
  virtual llvm::opt::DerivedArgList *TranslateOpenMPTargetArgs(
270
      const llvm::opt::DerivedArgList &Args, bool SameTripleAsHost,
271
      SmallVectorImpl<llvm::opt::Arg *> &AllocatedArgs) const;
272
273
  /// Choose a tool to use to handle the action \p JA.
274
  ///
275
  /// This can be overridden when a particular ToolChain needs to use
276
  /// a compiler other than Clang.
277
  virtual Tool *SelectTool(const JobAction &JA) const;
278
279
  // Helper methods
280
281
  std::string GetFilePath(const char *Name) const;
282
  std::string GetProgramPath(const char *Name) const;
283
284
  /// Returns the linker path, respecting the -fuse-ld= argument to determine
285
  /// the linker suffix or name.
286
  std::string GetLinkerPath() const;
287
288
  /// Dispatch to the specific toolchain for verbose printing.
289
  ///
290
  /// This is used when handling the verbose option to print detailed,
291
  /// toolchain-specific information useful for understanding the behavior of
292
  /// the driver on a specific platform.
293
14
  virtual void printVerboseInfo(raw_ostream &OS) const {}
294
295
  // Platform defaults information
296
297
  /// Returns true if the toolchain is targeting a non-native
298
  /// architecture.
299
  virtual bool isCrossCompiling() const;
300
301
  /// HasNativeLTOLinker - Check whether the linker and related tools have
302
  /// native LLVM support.
303
  virtual bool HasNativeLLVMSupport() const;
304
305
  /// LookupTypeForExtension - Return the default language type to use for the
306
  /// given extension.
307
  virtual types::ID LookupTypeForExtension(StringRef Ext) const;
308
309
  /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
310
7.36k
  virtual bool IsBlocksDefault() const { return false; }
311
312
  /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
313
  /// by default.
314
524
  virtual bool IsIntegratedAssemblerDefault() const { return false; }
315
316
  /// Check if the toolchain should use the integrated assembler.
317
  virtual bool useIntegratedAs() const;
318
319
  /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
320
6.58k
  virtual bool IsMathErrnoDefault() const { return true; }
321
322
  /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
323
  /// -fencode-extended-block-signature by default.
324
7.36k
  virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
325
326
  /// IsObjCNonFragileABIDefault - Does this tool chain set
327
  /// -fobjc-nonfragile-abi by default.
328
6.86k
  virtual bool IsObjCNonFragileABIDefault() const { return false; }
329
330
  /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
331
  /// mixed dispatch method be used?
332
0
  virtual bool UseObjCMixedDispatch() const { return false; }
333
334
  /// Check whether to enable x86 relax relocations by default.
335
  virtual bool useRelaxRelocations() const;
336
337
  /// GetDefaultStackProtectorLevel - Get the default stack protector level for
338
  /// this tool chain (0=off, 1=on, 2=strong, 3=all).
339
7.64k
  virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
340
7.64k
    return 0;
341
7.64k
  }
342
343
  /// GetDefaultLinker - Get the default linker to use.
344
4.08k
  virtual const char *getDefaultLinker() const { return "ld"; }
345
346
  /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
347
2.84k
  virtual RuntimeLibType GetDefaultRuntimeLibType() const {
348
2.84k
    return ToolChain::RLT_Libgcc;
349
2.84k
  }
350
351
2.55k
  virtual CXXStdlibType GetDefaultCXXStdlibType() const {
352
2.55k
    return ToolChain::CST_Libstdcxx;
353
2.55k
  }
354
355
  virtual std::string getCompilerRTPath() const;
356
357
  virtual std::string getCompilerRT(const llvm::opt::ArgList &Args,
358
                                    StringRef Component,
359
                                    bool Shared = false) const;
360
361
  const char *getCompilerRTArgString(const llvm::opt::ArgList &Args,
362
                                     StringRef Component,
363
                                     bool Shared = false) const;
364
365
  // Returns <ResourceDir>/lib/<OSName>/<arch>.  This is used by runtimes (such
366
  // as OpenMP) to find arch-specific libraries.
367
  std::string getArchSpecificLibPath() const;
368
369
  // Returns <OSname> part of above.
370
  StringRef getOSLibName() const;
371
372
  /// needsProfileRT - returns true if instrumentation profile is on.
373
  static bool needsProfileRT(const llvm::opt::ArgList &Args);
374
375
  /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
376
  /// by default.
377
  virtual bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const;
378
379
  /// Test whether this toolchain defaults to PIC.
380
  virtual bool isPICDefault() const = 0;
381
382
  /// Test whether this toolchain defaults to PIE.
383
  virtual bool isPIEDefault() const = 0;
384
385
  /// Tests whether this toolchain forces its default for PIC, PIE or
386
  /// non-PIC.  If this returns true, any PIC related flags should be ignored
387
  /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
388
  /// used exclusively.
389
  virtual bool isPICDefaultForced() const = 0;
390
391
  /// SupportsProfiling - Does this tool chain support -pg.
392
13.7k
  virtual bool SupportsProfiling() const { return true; }
393
394
  /// Complain if this tool chain doesn't support Objective-C ARC.
395
0
  virtual void CheckObjCARC() const {}
396
397
  /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
398
  /// compile unit information.
399
7.56k
  virtual bool UseDwarfDebugFlags() const { return false; }
400
401
  // Return the DWARF version to emit, in the absence of arguments
402
  // to the contrary.
403
390
  virtual unsigned GetDefaultDwarfVersion() const { return 4; }
404
405
  // True if the driver should assume "-fstandalone-debug"
406
  // in the absence of an option specifying otherwise,
407
  // provided that debugging was requested in the first place.
408
  // i.e. a value of 'true' does not imply that debugging is wanted.
409
8.07k
  virtual bool GetDefaultStandaloneDebug() const { return false; }
410
411
  // Return the default debugger "tuning."
412
8.04k
  virtual llvm::DebuggerKind getDefaultDebuggerTuning() const {
413
8.04k
    return llvm::DebuggerKind::GDB;
414
8.04k
  }
415
416
  /// GetExceptionModel - Return the tool chain exception model.
417
  virtual llvm::ExceptionHandling
418
  GetExceptionModel(const llvm::opt::ArgList &Args) const;
419
420
  /// SupportsEmbeddedBitcode - Does this tool chain support embedded bitcode.
421
0
  virtual bool SupportsEmbeddedBitcode() const { return false; }
422
423
  /// getThreadModel() - Which thread model does this target use?
424
31.3k
  virtual std::string getThreadModel() const { return "posix"; }
425
426
  /// isThreadModelSupported() - Does this target support a thread model?
427
  virtual bool isThreadModelSupported(const StringRef Model) const;
428
429
  /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
430
  /// command line arguments into account.
431
  virtual std::string
432
  ComputeLLVMTriple(const llvm::opt::ArgList &Args,
433
                    types::ID InputType = types::TY_INVALID) const;
434
435
  /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
436
  /// target, which may take into account the command line arguments. For
437
  /// example, on Darwin the -mmacosx-version-min= command line argument (which
438
  /// sets the deployment target) determines the version in the triple passed to
439
  /// Clang.
440
  virtual std::string ComputeEffectiveClangTriple(
441
      const llvm::opt::ArgList &Args,
442
      types::ID InputType = types::TY_INVALID) const;
443
444
  /// getDefaultObjCRuntime - Return the default Objective-C runtime
445
  /// for this platform.
446
  ///
447
  /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
448
  virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
449
450
  /// hasBlocksRuntime - Given that the user is compiling with
451
  /// -fblocks, does this tool chain guarantee the existence of a
452
  /// blocks runtime?
453
  ///
454
  /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
455
852
  virtual bool hasBlocksRuntime() const { return true; }
456
457
  /// Add the clang cc1 arguments for system include paths.
458
  ///
459
  /// This routine is responsible for adding the necessary cc1 arguments to
460
  /// include headers from standard system header directories.
461
  virtual void
462
  AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
463
                            llvm::opt::ArgStringList &CC1Args) const;
464
465
  /// Add options that need to be passed to cc1 for this target.
466
  virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
467
                                     llvm::opt::ArgStringList &CC1Args,
468
                                     Action::OffloadKind DeviceOffloadKind) const;
469
470
  /// Add warning options that need to be passed to cc1 for this target.
471
  virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
472
473
  // GetRuntimeLibType - Determine the runtime library type to use with the
474
  // given compilation arguments.
475
  virtual RuntimeLibType
476
  GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
477
478
  // GetCXXStdlibType - Determine the C++ standard library type to use with the
479
  // given compilation arguments.
480
  virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
481
482
  /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
483
  /// the include paths to use for the given C++ standard library type.
484
  virtual void
485
  AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
486
                               llvm::opt::ArgStringList &CC1Args) const;
487
488
  /// Returns if the C++ standard library should be linked in.
489
  /// Note that e.g. -lm should still be linked even if this returns false.
490
  bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const;
491
492
  /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
493
  /// for the given C++ standard library type.
494
  virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
495
                                   llvm::opt::ArgStringList &CmdArgs) const;
496
497
  /// AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
498
  void AddFilePathLibArgs(const llvm::opt::ArgList &Args,
499
                          llvm::opt::ArgStringList &CmdArgs) const;
500
501
  /// AddCCKextLibArgs - Add the system specific linker arguments to use
502
  /// for kernel extensions (Darwin-specific).
503
  virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
504
                                llvm::opt::ArgStringList &CmdArgs) const;
505
506
  /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
507
  /// global flags for unsafe floating point math, add it and return true.
508
  ///
509
  /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
510
  virtual bool AddFastMathRuntimeIfAvailable(
511
      const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const;
512
513
  /// addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass
514
  /// a suitable profile runtime library to the linker.
515
  virtual void addProfileRTLibs(const llvm::opt::ArgList &Args,
516
                                llvm::opt::ArgStringList &CmdArgs) const;
517
518
  /// Add arguments to use system-specific CUDA includes.
519
  virtual void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
520
                                  llvm::opt::ArgStringList &CC1Args) const;
521
522
  /// Add arguments to use MCU GCC toolchain includes.
523
  virtual void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs,
524
                                   llvm::opt::ArgStringList &CC1Args) const;
525
526
  /// On Windows, returns the MSVC compatibility version.
527
  virtual VersionTuple computeMSVCVersion(const Driver *D,
528
                                          const llvm::opt::ArgList &Args) const;
529
530
  /// Return sanitizers which are available in this toolchain.
531
  virtual SanitizerMask getSupportedSanitizers() const;
532
533
  /// Return sanitizers which are enabled by default.
534
27.0k
  virtual SanitizerMask getDefaultSanitizers() const { return 0; }
535
};
536
537
/// Set a ToolChain's effective triple. Reset it when the registration object
538
/// is destroyed.
539
class RegisterEffectiveTriple {
540
  const ToolChain &TC;
541
542
public:
543
33.1k
  RegisterEffectiveTriple(const ToolChain &TC, llvm::Triple T) : TC(TC) {
544
33.1k
    TC.setEffectiveTriple(std::move(T));
545
33.1k
  }
546
547
33.1k
  ~RegisterEffectiveTriple() { TC.setEffectiveTriple(llvm::Triple()); }
548
};
549
550
} // namespace driver
551
552
} // namespace clang
553
554
#endif // LLVM_CLANG_DRIVER_TOOLCHAIN_H