1 //===- CompilerInvocation.h - Compiler Invocation Helper Data ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
11 #define LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
13 #include "clang/Basic/CodeGenOptions.h"
14 #include "clang/Basic/DiagnosticOptions.h"
15 #include "clang/Basic/FileSystemOptions.h"
16 #include "clang/Basic/LLVM.h"
17 #include "clang/Basic/LangOptions.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/StaticAnalyzer/Core/AnalyzerOptions.h"
24 #include "llvm/ADT/IntrusiveRefCntPtr.h"
48 class DiagnosticsEngine;
49 class HeaderSearchOptions;
50 class PreprocessorOptions;
53 /// Fill out Opts based on the options given in Args.
55 /// Args must have been created from the OptTable returned by
56 /// createCC1OptTable().
58 /// When errors are encountered, return false and, if Diags is non-null,
59 /// report the error(s).
60 bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
61 DiagnosticsEngine *Diags = nullptr,
62 bool DefaultDiagColor = true,
63 bool DefaultShowOpt = true);
65 class CompilerInvocationBase {
67 /// Options controlling the language variant.
68 std::shared_ptr<LangOptions> LangOpts;
70 /// Options controlling the target.
71 std::shared_ptr<TargetOptions> TargetOpts;
73 /// Options controlling the diagnostic engine.
74 IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
76 /// Options controlling the \#include directive.
77 std::shared_ptr<HeaderSearchOptions> HeaderSearchOpts;
79 /// Options controlling the preprocessor (aside from \#include handling).
80 std::shared_ptr<PreprocessorOptions> PreprocessorOpts;
82 CompilerInvocationBase();
83 CompilerInvocationBase(const CompilerInvocationBase &X);
84 CompilerInvocationBase &operator=(const CompilerInvocationBase &) = delete;
85 ~CompilerInvocationBase();
87 LangOptions *getLangOpts() { return LangOpts.get(); }
88 const LangOptions *getLangOpts() const { return LangOpts.get(); }
90 TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
91 const TargetOptions &getTargetOpts() const { return *TargetOpts.get(); }
93 DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
95 HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
97 const HeaderSearchOptions &getHeaderSearchOpts() const {
98 return *HeaderSearchOpts;
101 std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const {
102 return HeaderSearchOpts;
105 std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
106 return PreprocessorOpts;
109 PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
111 const PreprocessorOptions &getPreprocessorOpts() const {
112 return *PreprocessorOpts;
116 /// Helper class for holding the data necessary to invoke the compiler.
118 /// This class is designed to represent an abstract "invocation" of the
119 /// compiler, including data such as the include paths, the code generation
120 /// options, the warning flags, and so on.
121 class CompilerInvocation : public CompilerInvocationBase {
122 /// Options controlling the static analyzer.
123 AnalyzerOptionsRef AnalyzerOpts;
125 MigratorOptions MigratorOpts;
127 /// Options controlling IRgen and the backend.
128 CodeGenOptions CodeGenOpts;
130 /// Options controlling dependency output.
131 DependencyOutputOptions DependencyOutputOpts;
133 /// Options controlling file system operations.
134 FileSystemOptions FileSystemOpts;
136 /// Options controlling the frontend itself.
137 FrontendOptions FrontendOpts;
139 /// Options controlling preprocessed output.
140 PreprocessorOutputOptions PreprocessorOutputOpts;
143 CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
145 /// @name Utility Methods
148 /// Create a compiler invocation from a list of input options.
149 /// \returns true on success.
151 /// \param [out] Res - The resulting invocation.
152 /// \param ArgBegin - The first element in the argument vector.
153 /// \param ArgEnd - The last element in the argument vector.
154 /// \param Diags - The diagnostic engine to use for errors.
155 static bool CreateFromArgs(CompilerInvocation &Res,
156 const char* const *ArgBegin,
157 const char* const *ArgEnd,
158 DiagnosticsEngine &Diags);
160 /// Get the directory where the compiler headers
161 /// reside, relative to the compiler binary (found by the passed in
164 /// \param Argv0 - The program path (from argv[0]), for finding the builtin
166 /// \param MainAddr - The address of main (or some other function in the main
167 /// executable), for finding the builtin compiler path.
168 static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
170 /// Set language defaults for the given input language and
171 /// language standard in the given LangOptions object.
173 /// \param Opts - The LangOptions object to set up.
174 /// \param IK - The input language.
175 /// \param T - The target triple.
176 /// \param PPOpts - The PreprocessorOptions affected.
177 /// \param LangStd - The input language standard.
178 static void setLangDefaults(LangOptions &Opts, InputKind IK,
179 const llvm::Triple &T, PreprocessorOptions &PPOpts,
180 LangStandard::Kind LangStd = LangStandard::lang_unspecified);
182 /// Retrieve a module hash string that is suitable for uniquely
183 /// identifying the conditions under which the module was built.
184 std::string getModuleHash() const;
187 /// @name Option Subgroups
190 AnalyzerOptionsRef getAnalyzerOpts() const { return AnalyzerOpts; }
192 MigratorOptions &getMigratorOpts() { return MigratorOpts; }
193 const MigratorOptions &getMigratorOpts() const { return MigratorOpts; }
195 CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
196 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
198 DependencyOutputOptions &getDependencyOutputOpts() {
199 return DependencyOutputOpts;
202 const DependencyOutputOptions &getDependencyOutputOpts() const {
203 return DependencyOutputOpts;
206 FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
208 const FileSystemOptions &getFileSystemOpts() const {
209 return FileSystemOpts;
212 FrontendOptions &getFrontendOpts() { return FrontendOpts; }
213 const FrontendOptions &getFrontendOpts() const { return FrontendOpts; }
215 PreprocessorOutputOptions &getPreprocessorOutputOpts() {
216 return PreprocessorOutputOpts;
219 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
220 return PreprocessorOutputOpts;
226 IntrusiveRefCntPtr<llvm::vfs::FileSystem>
227 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
228 DiagnosticsEngine &Diags);
230 IntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation(
231 const CompilerInvocation &CI, DiagnosticsEngine &Diags,
232 IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
236 #endif // LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H