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/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"
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);
53 class CompilerInvocationBase : public RefCountedBase<CompilerInvocation> {
54 void operator=(const CompilerInvocationBase &) = delete;
57 /// Options controlling the language variant.
58 std::shared_ptr<LangOptions> LangOpts;
60 /// Options controlling the target.
61 std::shared_ptr<TargetOptions> TargetOpts;
63 /// Options controlling the diagnostic engine.
64 IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
66 /// Options controlling the \#include directive.
67 IntrusiveRefCntPtr<HeaderSearchOptions> HeaderSearchOpts;
69 /// Options controlling the preprocessor (aside from \#include handling).
70 IntrusiveRefCntPtr<PreprocessorOptions> PreprocessorOpts;
72 CompilerInvocationBase();
73 ~CompilerInvocationBase();
75 CompilerInvocationBase(const CompilerInvocationBase &X);
77 LangOptions *getLangOpts() { return LangOpts.get(); }
78 const LangOptions *getLangOpts() const { return LangOpts.get(); }
80 TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
81 const TargetOptions &getTargetOpts() const {
82 return *TargetOpts.get();
85 DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
87 HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
88 const HeaderSearchOptions &getHeaderSearchOpts() const {
89 return *HeaderSearchOpts;
92 PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
93 const PreprocessorOptions &getPreprocessorOpts() const {
94 return *PreprocessorOpts;
98 /// \brief Helper class for holding the data necessary to invoke the compiler.
100 /// This class is designed to represent an abstract "invocation" of the
101 /// compiler, including data such as the include paths, the code generation
102 /// options, the warning flags, and so on.
103 class CompilerInvocation : public CompilerInvocationBase {
104 /// Options controlling the static analyzer.
105 AnalyzerOptionsRef AnalyzerOpts;
107 MigratorOptions MigratorOpts;
109 /// Options controlling IRgen and the backend.
110 CodeGenOptions CodeGenOpts;
112 /// Options controlling dependency output.
113 DependencyOutputOptions DependencyOutputOpts;
115 /// Options controlling file system operations.
116 FileSystemOptions FileSystemOpts;
118 /// Options controlling the frontend itself.
119 FrontendOptions FrontendOpts;
121 /// Options controlling preprocessed output.
122 PreprocessorOutputOptions PreprocessorOutputOpts;
125 CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
127 /// @name Utility Methods
130 /// \brief Create a compiler invocation from a list of input options.
131 /// \returns true on success.
133 /// \param [out] Res - The resulting invocation.
134 /// \param ArgBegin - The first element in the argument vector.
135 /// \param ArgEnd - The last element in the argument vector.
136 /// \param Diags - The diagnostic engine to use for errors.
137 static bool CreateFromArgs(CompilerInvocation &Res,
138 const char* const *ArgBegin,
139 const char* const *ArgEnd,
140 DiagnosticsEngine &Diags);
142 /// \brief Get the directory where the compiler headers
143 /// reside, relative to the compiler binary (found by the passed in
146 /// \param Argv0 - The program path (from argv[0]), for finding the builtin
148 /// \param MainAddr - The address of main (or some other function in the main
149 /// executable), for finding the builtin compiler path.
150 static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
152 /// \brief Set language defaults for the given input language and
153 /// language standard in the given LangOptions object.
155 /// \param Opts - The LangOptions object to set up.
156 /// \param IK - The input language.
157 /// \param T - The target triple.
158 /// \param PPOpts - The PreprocessorOptions affected.
159 /// \param LangStd - The input language standard.
160 static void setLangDefaults(LangOptions &Opts, InputKind IK,
161 const llvm::Triple &T, PreprocessorOptions &PPOpts,
162 LangStandard::Kind LangStd = LangStandard::lang_unspecified);
164 /// \brief Retrieve a module hash string that is suitable for uniquely
165 /// identifying the conditions under which the module was built.
166 std::string getModuleHash() const;
169 /// @name Option Subgroups
172 AnalyzerOptionsRef getAnalyzerOpts() const {
176 MigratorOptions &getMigratorOpts() { return MigratorOpts; }
177 const MigratorOptions &getMigratorOpts() const {
181 CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
182 const CodeGenOptions &getCodeGenOpts() const {
186 DependencyOutputOptions &getDependencyOutputOpts() {
187 return DependencyOutputOpts;
189 const DependencyOutputOptions &getDependencyOutputOpts() const {
190 return DependencyOutputOpts;
193 FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
194 const FileSystemOptions &getFileSystemOpts() const {
195 return FileSystemOpts;
198 FrontendOptions &getFrontendOpts() { return FrontendOpts; }
199 const FrontendOptions &getFrontendOpts() const {
203 PreprocessorOutputOptions &getPreprocessorOutputOpts() {
204 return PreprocessorOutputOpts;
206 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
207 return PreprocessorOutputOpts;
217 IntrusiveRefCntPtr<vfs::FileSystem>
218 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
219 DiagnosticsEngine &Diags);
221 } // end namespace clang