]> granicus.if.org Git - clang/blob - include/clang/Driver/ToolChain.h
Driver: implement addClangWarningOptions
[clang] / include / clang / Driver / ToolChain.h
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 CLANG_DRIVER_TOOLCHAIN_H_
11 #define CLANG_DRIVER_TOOLCHAIN_H_
12
13 #include "clang/Driver/Action.h"
14 #include "clang/Driver/Multilib.h"
15 #include "clang/Driver/Types.h"
16 #include "clang/Driver/Util.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Support/Path.h"
20 #include <memory>
21 #include <string>
22
23 namespace llvm {
24 namespace opt {
25   class ArgList;
26   class DerivedArgList;
27   class InputArgList;
28 }
29 }
30
31 namespace clang {
32   class ObjCRuntime;
33
34 namespace driver {
35   class Compilation;
36   class Driver;
37   class JobAction;
38   class SanitizerArgs;
39   class Tool;
40
41 /// ToolChain - Access to tools for a single platform.
42 class ToolChain {
43 public:
44   typedef SmallVector<std::string, 4> path_list;
45
46   enum CXXStdlibType {
47     CST_Libcxx,
48     CST_Libstdcxx
49   };
50
51   enum RuntimeLibType {
52     RLT_CompilerRT,
53     RLT_Libgcc
54   };
55
56 private:
57   const Driver &D;
58   const llvm::Triple Triple;
59   const llvm::opt::ArgList &Args;
60
61   /// The list of toolchain specific path prefixes to search for
62   /// files.
63   path_list FilePaths;
64
65   /// The list of toolchain specific path prefixes to search for
66   /// programs.
67   path_list ProgramPaths;
68
69   mutable std::unique_ptr<Tool> Clang;
70   mutable std::unique_ptr<Tool> Assemble;
71   mutable std::unique_ptr<Tool> Link;
72   Tool *getClang() const;
73   Tool *getAssemble() const;
74   Tool *getLink() const;
75   Tool *getClangAs() const;
76
77   mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
78
79 protected:
80   MultilibSet Multilibs;
81
82   ToolChain(const Driver &D, const llvm::Triple &T,
83             const llvm::opt::ArgList &Args);
84
85   virtual Tool *buildAssembler() const;
86   virtual Tool *buildLinker() const;
87   virtual Tool *getTool(Action::ActionClass AC) const;
88
89   /// \name Utilities for implementing subclasses.
90   ///@{
91   static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
92                                llvm::opt::ArgStringList &CC1Args,
93                                const Twine &Path);
94   static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
95                                       llvm::opt::ArgStringList &CC1Args,
96                                       const Twine &Path);
97   static void
98       addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs,
99                                       llvm::opt::ArgStringList &CC1Args,
100                                       const Twine &Path);
101   static void addSystemIncludes(const llvm::opt::ArgList &DriverArgs,
102                                 llvm::opt::ArgStringList &CC1Args,
103                                 ArrayRef<StringRef> Paths);
104   ///@}
105
106 public:
107   virtual ~ToolChain();
108
109   // Accessors
110
111   const Driver &getDriver() const;
112   const llvm::Triple &getTriple() const { return Triple; }
113
114   llvm::Triple::ArchType getArch() const { return Triple.getArch(); }
115   StringRef getArchName() const { return Triple.getArchName(); }
116   StringRef getPlatform() const { return Triple.getVendorName(); }
117   StringRef getOS() const { return Triple.getOSName(); }
118
119   /// \brief Returns true if the toolchain is targeting a non-native architecture.
120   bool isCrossCompiling() const;
121
122   /// \brief Provide the default architecture name (as expected by -arch) for
123   /// this toolchain. Note t
124   std::string getDefaultUniversalArchName() const;
125
126   std::string getTripleString() const {
127     return Triple.getTriple();
128   }
129
130   path_list &getFilePaths() { return FilePaths; }
131   const path_list &getFilePaths() const { return FilePaths; }
132
133   path_list &getProgramPaths() { return ProgramPaths; }
134   const path_list &getProgramPaths() const { return ProgramPaths; }
135
136   const MultilibSet &getMultilibs() const { return Multilibs; }
137
138   const SanitizerArgs& getSanitizerArgs() const;
139
140   // Tool access.
141
142   /// TranslateArgs - Create a new derived argument list for any argument
143   /// translations this ToolChain may wish to perform, or 0 if no tool chain
144   /// specific translations are needed.
145   ///
146   /// \param BoundArch - The bound architecture name, or 0.
147   virtual llvm::opt::DerivedArgList *
148   TranslateArgs(const llvm::opt::DerivedArgList &Args,
149                 const char *BoundArch) const {
150     return 0;
151   }
152
153   /// Choose a tool to use to handle the action \p JA.
154   Tool *SelectTool(const JobAction &JA) const;
155
156   // Helper methods
157
158   std::string GetFilePath(const char *Name) const;
159   std::string GetProgramPath(const char *Name) const;
160
161   /// \brief Dispatch to the specific toolchain for verbose printing.
162   ///
163   /// This is used when handling the verbose option to print detailed,
164   /// toolchain-specific information useful for understanding the behavior of
165   /// the driver on a specific platform.
166   virtual void printVerboseInfo(raw_ostream &OS) const {};
167
168   // Platform defaults information
169
170   /// HasNativeLTOLinker - Check whether the linker and related tools have
171   /// native LLVM support.
172   virtual bool HasNativeLLVMSupport() const;
173
174   /// LookupTypeForExtension - Return the default language type to use for the
175   /// given extension.
176   virtual types::ID LookupTypeForExtension(const char *Ext) const;
177
178   /// IsBlocksDefault - Does this tool chain enable -fblocks by default.
179   virtual bool IsBlocksDefault() const { return false; }
180
181   /// IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as
182   /// by default.
183   virtual bool IsIntegratedAssemblerDefault() const { return false; }
184
185   /// \brief Check if the toolchain should use the integrated assembler.
186   bool useIntegratedAs() const;
187
188   /// IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
189   virtual bool IsMathErrnoDefault() const { return true; }
190
191   /// IsEncodeExtendedBlockSignatureDefault - Does this tool chain enable
192   /// -fencode-extended-block-signature by default.
193   virtual bool IsEncodeExtendedBlockSignatureDefault() const { return false; }
194
195   /// IsObjCNonFragileABIDefault - Does this tool chain set
196   /// -fobjc-nonfragile-abi by default.
197   virtual bool IsObjCNonFragileABIDefault() const { return false; }
198
199   /// UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the
200   /// mixed dispatch method be used?
201   virtual bool UseObjCMixedDispatch() const { return false; }
202
203   /// GetDefaultStackProtectorLevel - Get the default stack protector level for
204   /// this tool chain (0=off, 1=on, 2=strong, 3=all).
205   virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const {
206     return 0;
207   }
208
209   /// GetDefaultRuntimeLibType - Get the default runtime library variant to use.
210   virtual RuntimeLibType GetDefaultRuntimeLibType() const {
211     return ToolChain::RLT_Libgcc;
212   }
213
214   /// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
215   /// by default.
216   virtual bool IsUnwindTablesDefault() const;
217
218   /// \brief Test whether this toolchain defaults to PIC.
219   virtual bool isPICDefault() const = 0;
220
221   /// \brief Test whether this toolchain defaults to PIE.
222   virtual bool isPIEDefault() const = 0;
223
224   /// \brief Tests whether this toolchain forces its default for PIC, PIE or
225   /// non-PIC.  If this returns true, any PIC related flags should be ignored
226   /// and instead the results of \c isPICDefault() and \c isPIEDefault() are
227   /// used exclusively.
228   virtual bool isPICDefaultForced() const = 0;
229
230   /// SupportsProfiling - Does this tool chain support -pg.
231   virtual bool SupportsProfiling() const { return true; }
232
233   /// Does this tool chain support Objective-C garbage collection.
234   virtual bool SupportsObjCGC() const { return true; }
235
236   /// Complain if this tool chain doesn't support Objective-C ARC.
237   virtual void CheckObjCARC() const {}
238
239   /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf
240   /// compile unit information.
241   virtual bool UseDwarfDebugFlags() const { return false; }
242
243   /// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
244   virtual bool UseSjLjExceptions() const { return false; }
245
246   /// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
247   /// command line arguments into account.
248   virtual std::string
249   ComputeLLVMTriple(const llvm::opt::ArgList &Args,
250                     types::ID InputType = types::TY_INVALID) const;
251
252   /// ComputeEffectiveClangTriple - Return the Clang triple to use for this
253   /// target, which may take into account the command line arguments. For
254   /// example, on Darwin the -mmacosx-version-min= command line argument (which
255   /// sets the deployment target) determines the version in the triple passed to
256   /// Clang.
257   virtual std::string ComputeEffectiveClangTriple(
258       const llvm::opt::ArgList &Args,
259       types::ID InputType = types::TY_INVALID) const;
260
261   /// getDefaultObjCRuntime - Return the default Objective-C runtime
262   /// for this platform.
263   ///
264   /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
265   virtual ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const;
266
267   /// hasBlocksRuntime - Given that the user is compiling with
268   /// -fblocks, does this tool chain guarantee the existence of a
269   /// blocks runtime?
270   ///
271   /// FIXME: this really belongs on some sort of DeploymentTarget abstraction
272   virtual bool hasBlocksRuntime() const { return true; }
273
274   /// \brief Add the clang cc1 arguments for system include paths.
275   ///
276   /// This routine is responsible for adding the necessary cc1 arguments to
277   /// include headers from standard system header directories.
278   virtual void
279   AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
280                             llvm::opt::ArgStringList &CC1Args) const;
281
282   /// \brief Add options that need to be passed to cc1 for this target.
283   virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
284                                      llvm::opt::ArgStringList &CC1Args) const;
285
286   /// \brief Add warning options that need to be passed to cc1 for this target.
287   virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
288
289   // GetRuntimeLibType - Determine the runtime library type to use with the
290   // given compilation arguments.
291   virtual RuntimeLibType
292   GetRuntimeLibType(const llvm::opt::ArgList &Args) const;
293
294   // GetCXXStdlibType - Determine the C++ standard library type to use with the
295   // given compilation arguments.
296   virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const;
297
298   /// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
299   /// the include paths to use for the given C++ standard library type.
300   virtual void
301   AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
302                                llvm::opt::ArgStringList &CC1Args) const;
303
304   /// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
305   /// for the given C++ standard library type.
306   virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
307                                    llvm::opt::ArgStringList &CmdArgs) const;
308
309   /// AddCCKextLibArgs - Add the system specific linker arguments to use
310   /// for kernel extensions (Darwin-specific).
311   virtual void AddCCKextLibArgs(const llvm::opt::ArgList &Args,
312                                 llvm::opt::ArgStringList &CmdArgs) const;
313
314   /// AddFastMathRuntimeIfAvailable - If a runtime library exists that sets
315   /// global flags for unsafe floating point math, add it and return true.
316   ///
317   /// This checks for presence of the -Ofast, -ffast-math or -funsafe-math flags.
318   virtual bool
319   AddFastMathRuntimeIfAvailable(const llvm::opt::ArgList &Args,
320                                 llvm::opt::ArgStringList &CmdArgs) const;
321 };
322
323 } // end namespace driver
324 } // end namespace clang
325
326 #endif