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 = 0);
52 class CompilerInvocationBase : public RefCountedBase<CompilerInvocation> {
54 /// Options controlling the language variant.
55 IntrusiveRefCntPtr<LangOptions> LangOpts;
57 /// Options controlling the target.
58 IntrusiveRefCntPtr<TargetOptions> TargetOpts;
60 /// Options controlling the diagnostic engine.
61 IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
63 /// Options controlling the \#include directive.
64 IntrusiveRefCntPtr<HeaderSearchOptions> HeaderSearchOpts;
66 /// Options controlling the preprocessor (aside from \#include handling).
67 IntrusiveRefCntPtr<PreprocessorOptions> PreprocessorOpts;
70 CompilerInvocationBase();
72 CompilerInvocationBase(const CompilerInvocationBase &X);
74 LangOptions *getLangOpts() { return LangOpts.getPtr(); }
75 const LangOptions *getLangOpts() const { return LangOpts.getPtr(); }
77 TargetOptions &getTargetOpts() { return *TargetOpts.getPtr(); }
78 const TargetOptions &getTargetOpts() const {
79 return *TargetOpts.getPtr();
82 DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
84 HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
85 const HeaderSearchOptions &getHeaderSearchOpts() const {
86 return *HeaderSearchOpts;
89 PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
90 const PreprocessorOptions &getPreprocessorOpts() const {
91 return *PreprocessorOpts;
95 /// \brief Helper class for holding the data necessary to invoke the compiler.
97 /// This class is designed to represent an abstract "invocation" of the
98 /// compiler, including data such as the include paths, the code generation
99 /// options, the warning flags, and so on.
100 class CompilerInvocation : public CompilerInvocationBase {
101 /// Options controlling the static analyzer.
102 AnalyzerOptionsRef AnalyzerOpts;
104 MigratorOptions MigratorOpts;
106 /// Options controlling IRgen and the backend.
107 CodeGenOptions CodeGenOpts;
109 /// Options controlling dependency output.
110 DependencyOutputOptions DependencyOutputOpts;
112 /// Options controlling file system operations.
113 FileSystemOptions FileSystemOpts;
115 /// Options controlling the frontend itself.
116 FrontendOptions FrontendOpts;
118 /// Options controlling preprocessed output.
119 PreprocessorOutputOptions PreprocessorOutputOpts;
122 CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
124 /// @name Utility Methods
127 /// \brief Create a compiler invocation from a list of input options.
128 /// \returns true on success.
130 /// \param [out] Res - The resulting invocation.
131 /// \param ArgBegin - The first element in the argument vector.
132 /// \param ArgEnd - The last element in the argument vector.
133 /// \param Diags - The diagnostic engine to use for errors.
134 static bool CreateFromArgs(CompilerInvocation &Res,
135 const char* const *ArgBegin,
136 const char* const *ArgEnd,
137 DiagnosticsEngine &Diags);
139 /// \brief Get the directory where the compiler headers
140 /// reside, relative to the compiler binary (found by the passed in
143 /// \param Argv0 - The program path (from argv[0]), for finding the builtin
145 /// \param MainAddr - The address of main (or some other function in the main
146 /// executable), for finding the builtin compiler path.
147 static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
149 /// \brief Set language defaults for the given input language and
150 /// language standard in the given LangOptions object.
152 /// \param Opts - The LangOptions object to set up.
153 /// \param IK - The input language.
154 /// \param LangStd - The input language standard.
155 static void setLangDefaults(LangOptions &Opts, InputKind IK,
156 LangStandard::Kind LangStd = LangStandard::lang_unspecified);
158 /// \brief Retrieve a module hash string that is suitable for uniquely
159 /// identifying the conditions under which the module was built.
160 std::string getModuleHash() const;
163 /// @name Option Subgroups
166 AnalyzerOptionsRef getAnalyzerOpts() const {
170 MigratorOptions &getMigratorOpts() { return MigratorOpts; }
171 const MigratorOptions &getMigratorOpts() const {
175 CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
176 const CodeGenOptions &getCodeGenOpts() const {
180 DependencyOutputOptions &getDependencyOutputOpts() {
181 return DependencyOutputOpts;
183 const DependencyOutputOptions &getDependencyOutputOpts() const {
184 return DependencyOutputOpts;
187 FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
188 const FileSystemOptions &getFileSystemOpts() const {
189 return FileSystemOpts;
192 FrontendOptions &getFrontendOpts() { return FrontendOpts; }
193 const FrontendOptions &getFrontendOpts() const {
197 PreprocessorOutputOptions &getPreprocessorOutputOpts() {
198 return PreprocessorOutputOpts;
200 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
201 return PreprocessorOutputOpts;
207 } // end namespace clang