1 //===-- CompilerInvocation.h - Compiler Invocation Helper Data --*- 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_FRONTEND_COMPILERINVOCATION_H_
11 #define LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H_
12 
13 #include "clang/Basic/DiagnosticOptions.h"
14 #include "clang/Basic/FileSystemOptions.h"
15 #include "clang/Basic/LangOptions.h"
16 #include "clang/Basic/TargetOptions.h"
17 #include "clang/Frontend/CodeGenOptions.h"
18 #include "clang/Frontend/DependencyOutputOptions.h"
19 #include "clang/Frontend/FrontendOptions.h"
20 #include "clang/Frontend/LangStandard.h"
21 #include "clang/Frontend/MigratorOptions.h"
22 #include "clang/Frontend/PreprocessorOutputOptions.h"
23 #include "clang/Lex/HeaderSearchOptions.h"
24 #include "clang/Lex/PreprocessorOptions.h"
25 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
26 #include "llvm/ADT/IntrusiveRefCntPtr.h"
27 #include "llvm/ADT/StringMap.h"
28 #include "llvm/ADT/StringRef.h"
29 #include <string>
30 #include <vector>
31 
32 namespace llvm {
33 namespace opt {
34 class ArgList;
35 }
36 }
37 
38 namespace clang {
39 class CompilerInvocation;
40 class DiagnosticsEngine;
41 
42 /// \brief Fill out Opts based on the options given in Args.
43 ///
44 /// Args must have been created from the OptTable returned by
45 /// createCC1OptTable().
46 ///
47 /// When errors are encountered, return false and, if Diags is non-null,
48 /// report the error(s).
49 bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
50                          DiagnosticsEngine *Diags = nullptr);
51 
52 class CompilerInvocationBase : public RefCountedBase<CompilerInvocation> {
53   void operator=(const CompilerInvocationBase &) LLVM_DELETED_FUNCTION;
54 
55 protected:
56   /// Options controlling the language variant.
57   IntrusiveRefCntPtr<LangOptions> LangOpts;
58 
59   /// Options controlling the target.
60   IntrusiveRefCntPtr<TargetOptions> TargetOpts;
61 
62   /// Options controlling the diagnostic engine.
63   IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
64 
65   /// Options controlling the \#include directive.
66   IntrusiveRefCntPtr<HeaderSearchOptions> HeaderSearchOpts;
67 
68   /// Options controlling the preprocessor (aside from \#include handling).
69   IntrusiveRefCntPtr<PreprocessorOptions> PreprocessorOpts;
70 
71 public:
72   CompilerInvocationBase();
73   ~CompilerInvocationBase();
74 
75   CompilerInvocationBase(const CompilerInvocationBase &X);
76 
77   LangOptions *getLangOpts() { return LangOpts.getPtr(); }
78   const LangOptions *getLangOpts() const { return LangOpts.getPtr(); }
79 
80   TargetOptions &getTargetOpts() { return *TargetOpts.getPtr(); }
81   const TargetOptions &getTargetOpts() const {
82     return *TargetOpts.getPtr();
83   }
84 
85   DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
86 
87   HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
88   const HeaderSearchOptions &getHeaderSearchOpts() const {
89     return *HeaderSearchOpts;
90   }
91 
92   PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
93   const PreprocessorOptions &getPreprocessorOpts() const {
94     return *PreprocessorOpts;
95   }
96 };
97 
98 /// \brief Helper class for holding the data necessary to invoke the compiler.
99 ///
100 /// This class is designed to represent an abstract "invocation" of the
101 /// compiler, including data such as the include paths, the code generation
102 /// options, the warning flags, and so on.
103 class CompilerInvocation : public CompilerInvocationBase {
104   /// Options controlling the static analyzer.
105   AnalyzerOptionsRef AnalyzerOpts;
106 
107   MigratorOptions MigratorOpts;
108 
109   /// Options controlling IRgen and the backend.
110   CodeGenOptions CodeGenOpts;
111 
112   /// Options controlling dependency output.
113   DependencyOutputOptions DependencyOutputOpts;
114 
115   /// Options controlling file system operations.
116   FileSystemOptions FileSystemOpts;
117 
118   /// Options controlling the frontend itself.
119   FrontendOptions FrontendOpts;
120 
121   /// Options controlling preprocessed output.
122   PreprocessorOutputOptions PreprocessorOutputOpts;
123 
124 public:
125   CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
126 
127   /// @name Utility Methods
128   /// @{
129 
130   /// \brief Create a compiler invocation from a list of input options.
131   /// \returns true on success.
132   ///
133   /// \param [out] Res - The resulting invocation.
134   /// \param ArgBegin - The first element in the argument vector.
135   /// \param ArgEnd - The last element in the argument vector.
136   /// \param Diags - The diagnostic engine to use for errors.
137   static bool CreateFromArgs(CompilerInvocation &Res,
138                              const char* const *ArgBegin,
139                              const char* const *ArgEnd,
140                              DiagnosticsEngine &Diags);
141 
142   /// \brief Get the directory where the compiler headers
143   /// reside, relative to the compiler binary (found by the passed in
144   /// arguments).
145   ///
146   /// \param Argv0 - The program path (from argv[0]), for finding the builtin
147   /// compiler path.
148   /// \param MainAddr - The address of main (or some other function in the main
149   /// executable), for finding the builtin compiler path.
150   static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
151 
152   /// \brief Set language defaults for the given input language and
153   /// language standard in the given LangOptions object.
154   ///
155   /// \param Opts - The LangOptions object to set up.
156   /// \param IK - The input language.
157   /// \param LangStd - The input language standard.
158   static void setLangDefaults(LangOptions &Opts, InputKind IK,
159                    LangStandard::Kind LangStd = LangStandard::lang_unspecified);
160 
161   /// \brief Retrieve a module hash string that is suitable for uniquely
162   /// identifying the conditions under which the module was built.
163   std::string getModuleHash() const;
164 
165   /// @}
166   /// @name Option Subgroups
167   /// @{
168 
169   AnalyzerOptionsRef getAnalyzerOpts() const {
170     return AnalyzerOpts;
171   }
172 
173   MigratorOptions &getMigratorOpts() { return MigratorOpts; }
174   const MigratorOptions &getMigratorOpts() const {
175     return MigratorOpts;
176   }
177 
178   CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
179   const CodeGenOptions &getCodeGenOpts() const {
180     return CodeGenOpts;
181   }
182 
183   DependencyOutputOptions &getDependencyOutputOpts() {
184     return DependencyOutputOpts;
185   }
186   const DependencyOutputOptions &getDependencyOutputOpts() const {
187     return DependencyOutputOpts;
188   }
189 
190   FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
191   const FileSystemOptions &getFileSystemOpts() const {
192     return FileSystemOpts;
193   }
194 
195   FrontendOptions &getFrontendOpts() { return FrontendOpts; }
196   const FrontendOptions &getFrontendOpts() const {
197     return FrontendOpts;
198   }
199 
200   PreprocessorOutputOptions &getPreprocessorOutputOpts() {
201     return PreprocessorOutputOpts;
202   }
203   const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
204     return PreprocessorOutputOpts;
205   }
206 
207   /// @}
208 };
209 
210 namespace vfs {
211   class FileSystem;
212 }
213 
214 IntrusiveRefCntPtr<vfs::FileSystem>
215 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
216                                 DiagnosticsEngine &Diags);
217 
218 } // end namespace clang
219 
220 #endif
221