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/DiagnosticOptions.h"
14 #include "clang/Basic/FileSystemOptions.h"
15 #include "clang/Basic/LLVM.h"
16 #include "clang/Basic/LangOptions.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/StaticAnalyzer/Core/AnalyzerOptions.h"
24 #include "llvm/ADT/IntrusiveRefCntPtr.h"
42 class DiagnosticsEngine;
43 class HeaderSearchOptions;
44 class PreprocessorOptions;
47 /// Fill out Opts based on the options given in Args.
49 /// Args must have been created from the OptTable returned by
50 /// createCC1OptTable().
52 /// When errors are encountered, return false and, if Diags is non-null,
53 /// report the error(s).
54 bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
55 DiagnosticsEngine *Diags = nullptr,
56 bool DefaultDiagColor = true,
57 bool DefaultShowOpt = true);
59 class CompilerInvocationBase {
61 /// Options controlling the language variant.
62 std::shared_ptr<LangOptions> LangOpts;
64 /// Options controlling the target.
65 std::shared_ptr<TargetOptions> TargetOpts;
67 /// Options controlling the diagnostic engine.
68 IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
70 /// Options controlling the \#include directive.
71 std::shared_ptr<HeaderSearchOptions> HeaderSearchOpts;
73 /// Options controlling the preprocessor (aside from \#include handling).
74 std::shared_ptr<PreprocessorOptions> PreprocessorOpts;
76 CompilerInvocationBase();
77 CompilerInvocationBase(const CompilerInvocationBase &X);
78 CompilerInvocationBase &operator=(const CompilerInvocationBase &) = delete;
79 ~CompilerInvocationBase();
81 LangOptions *getLangOpts() { return LangOpts.get(); }
82 const LangOptions *getLangOpts() const { return LangOpts.get(); }
84 TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
85 const TargetOptions &getTargetOpts() const { return *TargetOpts.get(); }
87 DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
89 HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
91 const HeaderSearchOptions &getHeaderSearchOpts() const {
92 return *HeaderSearchOpts;
95 std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const {
96 return HeaderSearchOpts;
99 std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
100 return PreprocessorOpts;
103 PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
105 const PreprocessorOptions &getPreprocessorOpts() const {
106 return *PreprocessorOpts;
110 /// Helper class for holding the data necessary to invoke the compiler.
112 /// This class is designed to represent an abstract "invocation" of the
113 /// compiler, including data such as the include paths, the code generation
114 /// options, the warning flags, and so on.
115 class CompilerInvocation : public CompilerInvocationBase {
116 /// Options controlling the static analyzer.
117 AnalyzerOptionsRef AnalyzerOpts;
119 MigratorOptions MigratorOpts;
121 /// Options controlling IRgen and the backend.
122 CodeGenOptions CodeGenOpts;
124 /// Options controlling dependency output.
125 DependencyOutputOptions DependencyOutputOpts;
127 /// Options controlling file system operations.
128 FileSystemOptions FileSystemOpts;
130 /// Options controlling the frontend itself.
131 FrontendOptions FrontendOpts;
133 /// Options controlling preprocessed output.
134 PreprocessorOutputOptions PreprocessorOutputOpts;
137 CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
139 /// @name Utility Methods
142 /// Create a compiler invocation from a list of input options.
143 /// \returns true on success.
145 /// \param [out] Res - The resulting invocation.
146 /// \param ArgBegin - The first element in the argument vector.
147 /// \param ArgEnd - The last element in the argument vector.
148 /// \param Diags - The diagnostic engine to use for errors.
149 static bool CreateFromArgs(CompilerInvocation &Res,
150 const char* const *ArgBegin,
151 const char* const *ArgEnd,
152 DiagnosticsEngine &Diags);
154 /// Get the directory where the compiler headers
155 /// reside, relative to the compiler binary (found by the passed in
158 /// \param Argv0 - The program path (from argv[0]), for finding the builtin
160 /// \param MainAddr - The address of main (or some other function in the main
161 /// executable), for finding the builtin compiler path.
162 static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
164 /// Set language defaults for the given input language and
165 /// language standard in the given LangOptions object.
167 /// \param Opts - The LangOptions object to set up.
168 /// \param IK - The input language.
169 /// \param T - The target triple.
170 /// \param PPOpts - The PreprocessorOptions affected.
171 /// \param LangStd - The input language standard.
172 static void setLangDefaults(LangOptions &Opts, InputKind IK,
173 const llvm::Triple &T, PreprocessorOptions &PPOpts,
174 LangStandard::Kind LangStd = LangStandard::lang_unspecified);
176 /// Retrieve a module hash string that is suitable for uniquely
177 /// identifying the conditions under which the module was built.
178 std::string getModuleHash() const;
181 /// @name Option Subgroups
184 AnalyzerOptionsRef getAnalyzerOpts() const { return AnalyzerOpts; }
186 MigratorOptions &getMigratorOpts() { return MigratorOpts; }
187 const MigratorOptions &getMigratorOpts() const { return MigratorOpts; }
189 CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
190 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
192 DependencyOutputOptions &getDependencyOutputOpts() {
193 return DependencyOutputOpts;
196 const DependencyOutputOptions &getDependencyOutputOpts() const {
197 return DependencyOutputOpts;
200 FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
202 const FileSystemOptions &getFileSystemOpts() const {
203 return FileSystemOpts;
206 FrontendOptions &getFrontendOpts() { return FrontendOpts; }
207 const FrontendOptions &getFrontendOpts() const { return FrontendOpts; }
209 PreprocessorOutputOptions &getPreprocessorOutputOpts() {
210 return PreprocessorOutputOpts;
213 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
214 return PreprocessorOutputOpts;
226 IntrusiveRefCntPtr<vfs::FileSystem>
227 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
228 DiagnosticsEngine &Diags);
230 IntrusiveRefCntPtr<vfs::FileSystem>
231 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
232 DiagnosticsEngine &Diags,
233 IntrusiveRefCntPtr<vfs::FileSystem> BaseFS);
237 #endif // LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H