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);
52 class CompilerInvocationBase : public RefCountedBase<CompilerInvocation> {
53 void operator=(const CompilerInvocationBase &) = delete;
56 /// Options controlling the language variant.
57 std::shared_ptr<LangOptions> LangOpts;
59 /// Options controlling the target.
60 std::shared_ptr<TargetOptions> TargetOpts;
62 /// Options controlling the diagnostic engine.
63 IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
65 /// Options controlling the \#include directive.
66 IntrusiveRefCntPtr<HeaderSearchOptions> HeaderSearchOpts;
68 /// Options controlling the preprocessor (aside from \#include handling).
69 IntrusiveRefCntPtr<PreprocessorOptions> PreprocessorOpts;
71 CompilerInvocationBase();
72 ~CompilerInvocationBase();
74 CompilerInvocationBase(const CompilerInvocationBase &X);
76 LangOptions *getLangOpts() { return LangOpts.get(); }
77 const LangOptions *getLangOpts() const { return LangOpts.get(); }
79 TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
80 const TargetOptions &getTargetOpts() const {
81 return *TargetOpts.get();
84 DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
86 HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
87 const HeaderSearchOptions &getHeaderSearchOpts() const {
88 return *HeaderSearchOpts;
91 PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
92 const PreprocessorOptions &getPreprocessorOpts() const {
93 return *PreprocessorOpts;
97 /// \brief Helper class for holding the data necessary to invoke the compiler.
99 /// This class is designed to represent an abstract "invocation" of the
100 /// compiler, including data such as the include paths, the code generation
101 /// options, the warning flags, and so on.
102 class CompilerInvocation : public CompilerInvocationBase {
103 /// Options controlling the static analyzer.
104 AnalyzerOptionsRef AnalyzerOpts;
106 MigratorOptions MigratorOpts;
108 /// Options controlling IRgen and the backend.
109 CodeGenOptions CodeGenOpts;
111 /// Options controlling dependency output.
112 DependencyOutputOptions DependencyOutputOpts;
114 /// Options controlling file system operations.
115 FileSystemOptions FileSystemOpts;
117 /// Options controlling the frontend itself.
118 FrontendOptions FrontendOpts;
120 /// Options controlling preprocessed output.
121 PreprocessorOutputOptions PreprocessorOutputOpts;
124 CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
126 /// @name Utility Methods
129 /// \brief Create a compiler invocation from a list of input options.
130 /// \returns true on success.
132 /// \param [out] Res - The resulting invocation.
133 /// \param ArgBegin - The first element in the argument vector.
134 /// \param ArgEnd - The last element in the argument vector.
135 /// \param Diags - The diagnostic engine to use for errors.
136 static bool CreateFromArgs(CompilerInvocation &Res,
137 const char* const *ArgBegin,
138 const char* const *ArgEnd,
139 DiagnosticsEngine &Diags);
141 /// \brief Get the directory where the compiler headers
142 /// reside, relative to the compiler binary (found by the passed in
145 /// \param Argv0 - The program path (from argv[0]), for finding the builtin
147 /// \param MainAddr - The address of main (or some other function in the main
148 /// executable), for finding the builtin compiler path.
149 static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
151 /// \brief Set language defaults for the given input language and
152 /// language standard in the given LangOptions object.
154 /// \param Opts - The LangOptions object to set up.
155 /// \param IK - The input language.
156 /// \param LangStd - The input language standard.
157 static void setLangDefaults(LangOptions &Opts, InputKind IK,
158 LangStandard::Kind LangStd = LangStandard::lang_unspecified);
160 /// \brief Retrieve a module hash string that is suitable for uniquely
161 /// identifying the conditions under which the module was built.
162 std::string getModuleHash() const;
165 /// @name Option Subgroups
168 AnalyzerOptionsRef getAnalyzerOpts() const {
172 MigratorOptions &getMigratorOpts() { return MigratorOpts; }
173 const MigratorOptions &getMigratorOpts() const {
177 CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
178 const CodeGenOptions &getCodeGenOpts() const {
182 DependencyOutputOptions &getDependencyOutputOpts() {
183 return DependencyOutputOpts;
185 const DependencyOutputOptions &getDependencyOutputOpts() const {
186 return DependencyOutputOpts;
189 FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
190 const FileSystemOptions &getFileSystemOpts() const {
191 return FileSystemOpts;
194 FrontendOptions &getFrontendOpts() { return FrontendOpts; }
195 const FrontendOptions &getFrontendOpts() const {
199 PreprocessorOutputOptions &getPreprocessorOutputOpts() {
200 return PreprocessorOutputOpts;
202 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
203 return PreprocessorOutputOpts;
213 IntrusiveRefCntPtr<vfs::FileSystem>
214 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
215 DiagnosticsEngine &Diags);
217 } // end namespace clang