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 : public RefCountedBase<CompilerInvocation> {
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 IntrusiveRefCntPtr<HeaderSearchOptions> HeaderSearchOpts;
70 /// Options controlling the preprocessor (aside from \#include handling).
71 IntrusiveRefCntPtr<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;
93 PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
94 const PreprocessorOptions &getPreprocessorOpts() const {
95 return *PreprocessorOpts;
99 /// \brief Helper class for holding the data necessary to invoke the compiler.
101 /// This class is designed to represent an abstract "invocation" of the
102 /// compiler, including data such as the include paths, the code generation
103 /// options, the warning flags, and so on.
104 class CompilerInvocation : public CompilerInvocationBase {
105 /// Options controlling the static analyzer.
106 AnalyzerOptionsRef AnalyzerOpts;
108 MigratorOptions MigratorOpts;
110 /// Options controlling IRgen and the backend.
111 CodeGenOptions CodeGenOpts;
113 /// Options controlling dependency output.
114 DependencyOutputOptions DependencyOutputOpts;
116 /// Options controlling file system operations.
117 FileSystemOptions FileSystemOpts;
119 /// Options controlling the frontend itself.
120 FrontendOptions FrontendOpts;
122 /// Options controlling preprocessed output.
123 PreprocessorOutputOptions PreprocessorOutputOpts;
126 CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
128 /// @name Utility Methods
131 /// \brief Create a compiler invocation from a list of input options.
132 /// \returns true on success.
134 /// \param [out] Res - The resulting invocation.
135 /// \param ArgBegin - The first element in the argument vector.
136 /// \param ArgEnd - The last element in the argument vector.
137 /// \param Diags - The diagnostic engine to use for errors.
138 static bool CreateFromArgs(CompilerInvocation &Res,
139 const char* const *ArgBegin,
140 const char* const *ArgEnd,
141 DiagnosticsEngine &Diags);
143 /// \brief Get the directory where the compiler headers
144 /// reside, relative to the compiler binary (found by the passed in
147 /// \param Argv0 - The program path (from argv[0]), for finding the builtin
149 /// \param MainAddr - The address of main (or some other function in the main
150 /// executable), for finding the builtin compiler path.
151 static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
153 /// \brief Set language defaults for the given input language and
154 /// language standard in the given LangOptions object.
156 /// \param Opts - The LangOptions object to set up.
157 /// \param IK - The input language.
158 /// \param T - The target triple.
159 /// \param PPOpts - The PreprocessorOptions affected.
160 /// \param LangStd - The input language standard.
161 static void setLangDefaults(LangOptions &Opts, InputKind IK,
162 const llvm::Triple &T, PreprocessorOptions &PPOpts,
163 LangStandard::Kind LangStd = LangStandard::lang_unspecified);
165 /// \brief Retrieve a module hash string that is suitable for uniquely
166 /// identifying the conditions under which the module was built.
167 std::string getModuleHash() const;
170 /// @name Option Subgroups
173 AnalyzerOptionsRef getAnalyzerOpts() const {
177 MigratorOptions &getMigratorOpts() { return MigratorOpts; }
178 const MigratorOptions &getMigratorOpts() const {
182 CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
183 const CodeGenOptions &getCodeGenOpts() const {
187 DependencyOutputOptions &getDependencyOutputOpts() {
188 return DependencyOutputOpts;
190 const DependencyOutputOptions &getDependencyOutputOpts() const {
191 return DependencyOutputOpts;
194 FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
195 const FileSystemOptions &getFileSystemOpts() const {
196 return FileSystemOpts;
199 FrontendOptions &getFrontendOpts() { return FrontendOpts; }
200 const FrontendOptions &getFrontendOpts() const {
204 PreprocessorOutputOptions &getPreprocessorOutputOpts() {
205 return PreprocessorOutputOpts;
207 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
208 return PreprocessorOutputOpts;
218 IntrusiveRefCntPtr<vfs::FileSystem>
219 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
220 DiagnosticsEngine &Diags);
222 } // end namespace clang