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"
34 class CompilerInvocation;
35 class DiagnosticsEngine;
41 /// \brief Fill out Opts based on the options given in Args.
43 /// Args must have been created from the OptTable returned by
44 /// createCC1OptTable().
46 /// When errors are encountered, return false and, if Diags is non-null,
47 /// report the error(s).
48 bool ParseDiagnosticArgs(DiagnosticOptions &Opts, driver::ArgList &Args,
49 DiagnosticsEngine *Diags = 0);
51 class CompilerInvocationBase : public RefCountedBase<CompilerInvocation> {
53 /// Options controlling the language variant.
54 IntrusiveRefCntPtr<LangOptions> LangOpts;
56 /// Options controlling the target.
57 IntrusiveRefCntPtr<TargetOptions> TargetOpts;
59 /// Options controlling the diagnostic engine.
60 IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
62 /// Options controlling the \#include directive.
63 IntrusiveRefCntPtr<HeaderSearchOptions> HeaderSearchOpts;
65 /// Options controlling the preprocessor (aside from \#include handling).
66 IntrusiveRefCntPtr<PreprocessorOptions> PreprocessorOpts;
69 CompilerInvocationBase();
71 CompilerInvocationBase(const CompilerInvocationBase &X);
73 LangOptions *getLangOpts() { return LangOpts.getPtr(); }
74 const LangOptions *getLangOpts() const { return LangOpts.getPtr(); }
76 TargetOptions &getTargetOpts() { return *TargetOpts.getPtr(); }
77 const TargetOptions &getTargetOpts() const {
78 return *TargetOpts.getPtr();
81 DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
83 HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
84 const HeaderSearchOptions &getHeaderSearchOpts() const {
85 return *HeaderSearchOpts;
88 PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
89 const PreprocessorOptions &getPreprocessorOpts() const {
90 return *PreprocessorOpts;
94 /// \brief Helper class for holding the data necessary to invoke the compiler.
96 /// This class is designed to represent an abstract "invocation" of the
97 /// compiler, including data such as the include paths, the code generation
98 /// options, the warning flags, and so on.
99 class CompilerInvocation : public CompilerInvocationBase {
100 /// Options controlling the static analyzer.
101 AnalyzerOptionsRef AnalyzerOpts;
103 MigratorOptions MigratorOpts;
105 /// Options controlling IRgen and the backend.
106 CodeGenOptions CodeGenOpts;
108 /// Options controlling dependency output.
109 DependencyOutputOptions DependencyOutputOpts;
111 /// Options controlling file system operations.
112 FileSystemOptions FileSystemOpts;
114 /// Options controlling the frontend itself.
115 FrontendOptions FrontendOpts;
117 /// Options controlling preprocessed output.
118 PreprocessorOutputOptions PreprocessorOutputOpts;
121 CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
123 /// @name Utility Methods
126 /// \brief Create a compiler invocation from a list of input options.
127 /// \returns true on success.
129 /// \param [out] Res - The resulting invocation.
130 /// \param ArgBegin - The first element in the argument vector.
131 /// \param ArgEnd - The last element in the argument vector.
132 /// \param Diags - The diagnostic engine to use for errors.
133 static bool CreateFromArgs(CompilerInvocation &Res,
134 const char* const *ArgBegin,
135 const char* const *ArgEnd,
136 DiagnosticsEngine &Diags);
138 /// \brief Get the directory where the compiler headers
139 /// reside, relative to the compiler binary (found by the passed in
142 /// \param Argv0 - The program path (from argv[0]), for finding the builtin
144 /// \param MainAddr - The address of main (or some other function in the main
145 /// executable), for finding the builtin compiler path.
146 static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
148 /// \brief Set language defaults for the given input language and
149 /// language standard in the given LangOptions object.
151 /// \param Opts - The LangOptions object to set up.
152 /// \param IK - The input language.
153 /// \param LangStd - The input language standard.
154 static void setLangDefaults(LangOptions &Opts, InputKind IK,
155 LangStandard::Kind LangStd = LangStandard::lang_unspecified);
157 /// \brief Retrieve a module hash string that is suitable for uniquely
158 /// identifying the conditions under which the module was built.
159 std::string getModuleHash() const;
162 /// @name Option Subgroups
165 AnalyzerOptionsRef getAnalyzerOpts() const {
169 MigratorOptions &getMigratorOpts() { return MigratorOpts; }
170 const MigratorOptions &getMigratorOpts() const {
174 CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
175 const CodeGenOptions &getCodeGenOpts() const {
179 DependencyOutputOptions &getDependencyOutputOpts() {
180 return DependencyOutputOpts;
182 const DependencyOutputOptions &getDependencyOutputOpts() const {
183 return DependencyOutputOpts;
186 FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
187 const FileSystemOptions &getFileSystemOpts() const {
188 return FileSystemOpts;
191 FrontendOptions &getFrontendOpts() { return FrontendOpts; }
192 const FrontendOptions &getFrontendOpts() const {
196 PreprocessorOutputOptions &getPreprocessorOutputOpts() {
197 return PreprocessorOutputOpts;
199 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
200 return PreprocessorOutputOpts;
206 } // end namespace clang