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/LangOptions.h"
16 #include "clang/Frontend/CodeGenOptions.h"
17 #include "clang/Frontend/DependencyOutputOptions.h"
18 #include "clang/Frontend/FrontendOptions.h"
19 #include "clang/Frontend/LangStandard.h"
20 #include "clang/Frontend/MigratorOptions.h"
21 #include "clang/Frontend/PreprocessorOutputOptions.h"
22 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
23 #include "llvm/ADT/IntrusiveRefCntPtr.h"
35 class PreprocessorOptions;
36 class HeaderSearchOptions;
39 class CompilerInvocation;
40 class DiagnosticsEngine;
42 /// \brief Fill out Opts based on the options given in Args.
44 /// Args must have been created from the OptTable returned by
45 /// createCC1OptTable().
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 bool DefaultDiagColor = true,
52 bool DefaultShowOpt = true);
54 class CompilerInvocationBase {
55 void operator=(const CompilerInvocationBase &) = delete;
58 /// Options controlling the language variant.
59 std::shared_ptr<LangOptions> LangOpts;
61 /// Options controlling the target.
62 std::shared_ptr<TargetOptions> TargetOpts;
64 /// Options controlling the diagnostic engine.
65 IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
67 /// Options controlling the \#include directive.
68 std::shared_ptr<HeaderSearchOptions> HeaderSearchOpts;
70 /// Options controlling the preprocessor (aside from \#include handling).
71 std::shared_ptr<PreprocessorOptions> PreprocessorOpts;
73 CompilerInvocationBase();
74 ~CompilerInvocationBase();
76 CompilerInvocationBase(const CompilerInvocationBase &X);
78 LangOptions *getLangOpts() { return LangOpts.get(); }
79 const LangOptions *getLangOpts() const { return LangOpts.get(); }
81 TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
82 const TargetOptions &getTargetOpts() const {
83 return *TargetOpts.get();
86 DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
88 HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
89 const HeaderSearchOptions &getHeaderSearchOpts() const {
90 return *HeaderSearchOpts;
92 std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const {
93 return HeaderSearchOpts;
96 std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
97 return PreprocessorOpts;
99 PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
100 const PreprocessorOptions &getPreprocessorOpts() const {
101 return *PreprocessorOpts;
105 /// \brief Helper class for holding the data necessary to invoke the compiler.
107 /// This class is designed to represent an abstract "invocation" of the
108 /// compiler, including data such as the include paths, the code generation
109 /// options, the warning flags, and so on.
110 class CompilerInvocation : public CompilerInvocationBase {
111 /// Options controlling the static analyzer.
112 AnalyzerOptionsRef AnalyzerOpts;
114 MigratorOptions MigratorOpts;
116 /// Options controlling IRgen and the backend.
117 CodeGenOptions CodeGenOpts;
119 /// Options controlling dependency output.
120 DependencyOutputOptions DependencyOutputOpts;
122 /// Options controlling file system operations.
123 FileSystemOptions FileSystemOpts;
125 /// Options controlling the frontend itself.
126 FrontendOptions FrontendOpts;
128 /// Options controlling preprocessed output.
129 PreprocessorOutputOptions PreprocessorOutputOpts;
132 CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
134 /// @name Utility Methods
137 /// \brief Create a compiler invocation from a list of input options.
138 /// \returns true on success.
140 /// \param [out] Res - The resulting invocation.
141 /// \param ArgBegin - The first element in the argument vector.
142 /// \param ArgEnd - The last element in the argument vector.
143 /// \param Diags - The diagnostic engine to use for errors.
144 static bool CreateFromArgs(CompilerInvocation &Res,
145 const char* const *ArgBegin,
146 const char* const *ArgEnd,
147 DiagnosticsEngine &Diags);
149 /// \brief Get the directory where the compiler headers
150 /// reside, relative to the compiler binary (found by the passed in
153 /// \param Argv0 - The program path (from argv[0]), for finding the builtin
155 /// \param MainAddr - The address of main (or some other function in the main
156 /// executable), for finding the builtin compiler path.
157 static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
159 /// \brief Set language defaults for the given input language and
160 /// language standard in the given LangOptions object.
162 /// \param Opts - The LangOptions object to set up.
163 /// \param IK - The input language.
164 /// \param T - The target triple.
165 /// \param PPOpts - The PreprocessorOptions affected.
166 /// \param LangStd - The input language standard.
167 static void setLangDefaults(LangOptions &Opts, InputKind IK,
168 const llvm::Triple &T, PreprocessorOptions &PPOpts,
169 LangStandard::Kind LangStd = LangStandard::lang_unspecified);
171 /// \brief Retrieve a module hash string that is suitable for uniquely
172 /// identifying the conditions under which the module was built.
173 std::string getModuleHash() const;
176 /// @name Option Subgroups
179 AnalyzerOptionsRef getAnalyzerOpts() const {
183 MigratorOptions &getMigratorOpts() { return MigratorOpts; }
184 const MigratorOptions &getMigratorOpts() const {
188 CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
189 const CodeGenOptions &getCodeGenOpts() const {
193 DependencyOutputOptions &getDependencyOutputOpts() {
194 return DependencyOutputOpts;
196 const DependencyOutputOptions &getDependencyOutputOpts() const {
197 return DependencyOutputOpts;
200 FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
201 const FileSystemOptions &getFileSystemOpts() const {
202 return FileSystemOpts;
205 FrontendOptions &getFrontendOpts() { return FrontendOpts; }
206 const FrontendOptions &getFrontendOpts() const {
210 PreprocessorOutputOptions &getPreprocessorOutputOpts() {
211 return PreprocessorOutputOpts;
213 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
214 return PreprocessorOutputOpts;
224 IntrusiveRefCntPtr<vfs::FileSystem>
225 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
226 DiagnosticsEngine &Diags);
228 IntrusiveRefCntPtr<vfs::FileSystem>
229 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
230 DiagnosticsEngine &Diags,
231 IntrusiveRefCntPtr<vfs::FileSystem> BaseFS);
233 } // end namespace clang