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 &) = delete;
54 
55 public:
56   /// Options controlling the language variant.
57   std::shared_ptr<LangOptions> LangOpts;
58 
59   /// Options controlling the target.
60   std::shared_ptr<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   CompilerInvocationBase();
72   ~CompilerInvocationBase();
73 
74   CompilerInvocationBase(const CompilerInvocationBase &X);
75 
76   LangOptions *getLangOpts() { return LangOpts.get(); }
77   const LangOptions *getLangOpts() const { return LangOpts.get(); }
78 
79   TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
80   const TargetOptions &getTargetOpts() const {
81     return *TargetOpts.get();
82   }
83 
84   DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
85 
86   HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
87   const HeaderSearchOptions &getHeaderSearchOpts() const {
88     return *HeaderSearchOpts;
89   }
90 
91   PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
92   const PreprocessorOptions &getPreprocessorOpts() const {
93     return *PreprocessorOpts;
94   }
95 };
96 
97 /// \brief Helper class for holding the data necessary to invoke the compiler.
98 ///
99 /// This class is designed to represent an abstract "invocation" of the
100 /// compiler, including data such as the include paths, the code generation
101 /// options, the warning flags, and so on.
102 class CompilerInvocation : public CompilerInvocationBase {
103   /// Options controlling the static analyzer.
104   AnalyzerOptionsRef AnalyzerOpts;
105 
106   MigratorOptions MigratorOpts;
107 
108   /// Options controlling IRgen and the backend.
109   CodeGenOptions CodeGenOpts;
110 
111   /// Options controlling dependency output.
112   DependencyOutputOptions DependencyOutputOpts;
113 
114   /// Options controlling file system operations.
115   FileSystemOptions FileSystemOpts;
116 
117   /// Options controlling the frontend itself.
118   FrontendOptions FrontendOpts;
119 
120   /// Options controlling preprocessed output.
121   PreprocessorOutputOptions PreprocessorOutputOpts;
122 
123 public:
124   CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
125 
126   /// @name Utility Methods
127   /// @{
128 
129   /// \brief Create a compiler invocation from a list of input options.
130   /// \returns true on success.
131   ///
132   /// \param [out] Res - The resulting invocation.
133   /// \param ArgBegin - The first element in the argument vector.
134   /// \param ArgEnd - The last element in the argument vector.
135   /// \param Diags - The diagnostic engine to use for errors.
136   static bool CreateFromArgs(CompilerInvocation &Res,
137                              const char* const *ArgBegin,
138                              const char* const *ArgEnd,
139                              DiagnosticsEngine &Diags);
140 
141   /// \brief Get the directory where the compiler headers
142   /// reside, relative to the compiler binary (found by the passed in
143   /// arguments).
144   ///
145   /// \param Argv0 - The program path (from argv[0]), for finding the builtin
146   /// compiler path.
147   /// \param MainAddr - The address of main (or some other function in the main
148   /// executable), for finding the builtin compiler path.
149   static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
150 
151   /// \brief Set language defaults for the given input language and
152   /// language standard in the given LangOptions object.
153   ///
154   /// \param Opts - The LangOptions object to set up.
155   /// \param IK - The input language.
156   /// \param T - The target triple.
157   /// \param LangStd - The input language standard.
158   static void setLangDefaults(LangOptions &Opts, InputKind IK,
159                    const llvm::Triple &T,
160                    LangStandard::Kind LangStd = LangStandard::lang_unspecified);
161 
162   /// \brief Retrieve a module hash string that is suitable for uniquely
163   /// identifying the conditions under which the module was built.
164   std::string getModuleHash() const;
165 
166   /// @}
167   /// @name Option Subgroups
168   /// @{
169 
170   AnalyzerOptionsRef getAnalyzerOpts() const {
171     return AnalyzerOpts;
172   }
173 
174   MigratorOptions &getMigratorOpts() { return MigratorOpts; }
175   const MigratorOptions &getMigratorOpts() const {
176     return MigratorOpts;
177   }
178 
179   CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
180   const CodeGenOptions &getCodeGenOpts() const {
181     return CodeGenOpts;
182   }
183 
184   DependencyOutputOptions &getDependencyOutputOpts() {
185     return DependencyOutputOpts;
186   }
187   const DependencyOutputOptions &getDependencyOutputOpts() const {
188     return DependencyOutputOpts;
189   }
190 
191   FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
192   const FileSystemOptions &getFileSystemOpts() const {
193     return FileSystemOpts;
194   }
195 
196   FrontendOptions &getFrontendOpts() { return FrontendOpts; }
197   const FrontendOptions &getFrontendOpts() const {
198     return FrontendOpts;
199   }
200 
201   PreprocessorOutputOptions &getPreprocessorOutputOpts() {
202     return PreprocessorOutputOpts;
203   }
204   const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
205     return PreprocessorOutputOpts;
206   }
207 
208   /// @}
209 };
210 
211 namespace vfs {
212   class FileSystem;
213 }
214 
215 IntrusiveRefCntPtr<vfs::FileSystem>
216 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
217                                 DiagnosticsEngine &Diags);
218 
219 } // end namespace clang
220 
221 #endif
222