CompilerInvocation.h revision 57016dda61498294120b1a881d9e6606337b29d9
1//===-- CompilerInvocation.h - Compiler Invocation Helper Data --*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H_
11#define LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H_
12
13#include "clang/Basic/LangOptions.h"
14#include "clang/Basic/TargetOptions.h"
15#include "clang/Basic/FileSystemOptions.h"
16#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
17#include "clang/Frontend/MigratorOptions.h"
18#include "clang/Frontend/CodeGenOptions.h"
19#include "clang/Frontend/DependencyOutputOptions.h"
20#include "clang/Frontend/DiagnosticOptions.h"
21#include "clang/Frontend/FrontendOptions.h"
22#include "clang/Frontend/HeaderSearchOptions.h"
23#include "clang/Frontend/LangStandard.h"
24#include "clang/Frontend/PreprocessorOptions.h"
25#include "clang/Frontend/PreprocessorOutputOptions.h"
26#include "llvm/ADT/IntrusiveRefCntPtr.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/ADT/StringMap.h"
29#include <string>
30#include <vector>
31
32namespace clang {
33
34class CompilerInvocation;
35class DiagnosticsEngine;
36
37namespace driver {
38class ArgList;
39}
40
41/// \brief Fill out Opts based on the options given in Args.
42///
43/// Args must have been created from the OptTable returned by
44/// createCC1OptTable().
45///
46/// When errors are encountered, return false and, if Diags is non-null,
47/// report the error(s).
48bool ParseDiagnosticArgs(DiagnosticOptions &Opts, driver::ArgList &Args,
49                         DiagnosticsEngine *Diags = 0);
50
51class CompilerInvocationBase : public RefCountedBase<CompilerInvocation> {
52protected:
53  /// Options controlling the language variant.
54  IntrusiveRefCntPtr<LangOptions> LangOpts;
55
56  /// Options controlling the target.
57  IntrusiveRefCntPtr<TargetOptions> TargetOpts;
58public:
59  CompilerInvocationBase();
60
61  CompilerInvocationBase(const CompilerInvocationBase &X);
62
63  LangOptions *getLangOpts() { return LangOpts.getPtr(); }
64  const LangOptions *getLangOpts() const { return LangOpts.getPtr(); }
65
66  TargetOptions &getTargetOpts() { return *TargetOpts.getPtr(); }
67  const TargetOptions &getTargetOpts() const {
68    return *TargetOpts.getPtr();
69  }
70};
71
72/// \brief Helper class for holding the data necessary to invoke the compiler.
73///
74/// This class is designed to represent an abstract "invocation" of the
75/// compiler, including data such as the include paths, the code generation
76/// options, the warning flags, and so on.
77class CompilerInvocation : public CompilerInvocationBase {
78  /// Options controlling the static analyzer.
79  AnalyzerOptionsRef AnalyzerOpts;
80
81  MigratorOptions MigratorOpts;
82
83  /// Options controlling IRgen and the backend.
84  CodeGenOptions CodeGenOpts;
85
86  /// Options controlling dependency output.
87  DependencyOutputOptions DependencyOutputOpts;
88
89  /// Options controlling the diagnostic engine.
90  DiagnosticOptions DiagnosticOpts;
91
92  /// Options controlling file system operations.
93  FileSystemOptions FileSystemOpts;
94
95  /// Options controlling the frontend itself.
96  FrontendOptions FrontendOpts;
97
98  /// Options controlling the \#include directive.
99  HeaderSearchOptions HeaderSearchOpts;
100
101  /// Options controlling the preprocessor (aside from \#include handling).
102  PreprocessorOptions PreprocessorOpts;
103
104  /// Options controlling preprocessed output.
105  PreprocessorOutputOptions PreprocessorOutputOpts;
106
107public:
108  CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
109
110  /// @name Utility Methods
111  /// @{
112
113  /// \brief Create a compiler invocation from a list of input options.
114  /// \returns true on success.
115  ///
116  /// \param [out] Res - The resulting invocation.
117  /// \param ArgBegin - The first element in the argument vector.
118  /// \param ArgEnd - The last element in the argument vector.
119  /// \param Diags - The diagnostic engine to use for errors.
120  static bool CreateFromArgs(CompilerInvocation &Res,
121                             const char* const *ArgBegin,
122                             const char* const *ArgEnd,
123                             DiagnosticsEngine &Diags);
124
125  /// \brief Get the directory where the compiler headers
126  /// reside, relative to the compiler binary (found by the passed in
127  /// arguments).
128  ///
129  /// \param Argv0 - The program path (from argv[0]), for finding the builtin
130  /// compiler path.
131  /// \param MainAddr - The address of main (or some other function in the main
132  /// executable), for finding the builtin compiler path.
133  static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
134
135  /// \brief Convert the CompilerInvocation to a list of strings suitable for
136  /// passing to CreateFromArgs.
137  void toArgs(std::vector<std::string> &Res) const;
138
139  /// \brief Set language defaults for the given input language and
140  /// language standard in the given LangOptions object.
141  ///
142  /// \param Opts - The LangOptions object to set up.
143  /// \param IK - The input language.
144  /// \param LangStd - The input language standard.
145  static void setLangDefaults(LangOptions &Opts, InputKind IK,
146                   LangStandard::Kind LangStd = LangStandard::lang_unspecified);
147
148  /// \brief Retrieve a module hash string that is suitable for uniquely
149  /// identifying the conditions under which the module was built.
150  std::string getModuleHash() const;
151
152  /// @}
153  /// @name Option Subgroups
154  /// @{
155
156  AnalyzerOptionsRef getAnalyzerOpts() const {
157    return AnalyzerOpts;
158  }
159
160  MigratorOptions &getMigratorOpts() { return MigratorOpts; }
161  const MigratorOptions &getMigratorOpts() const {
162    return MigratorOpts;
163  }
164
165  CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
166  const CodeGenOptions &getCodeGenOpts() const {
167    return CodeGenOpts;
168  }
169
170  DependencyOutputOptions &getDependencyOutputOpts() {
171    return DependencyOutputOpts;
172  }
173  const DependencyOutputOptions &getDependencyOutputOpts() const {
174    return DependencyOutputOpts;
175  }
176
177  DiagnosticOptions &getDiagnosticOpts() { return DiagnosticOpts; }
178  const DiagnosticOptions &getDiagnosticOpts() const { return DiagnosticOpts; }
179
180  FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
181  const FileSystemOptions &getFileSystemOpts() const {
182    return FileSystemOpts;
183  }
184
185  HeaderSearchOptions &getHeaderSearchOpts() { return HeaderSearchOpts; }
186  const HeaderSearchOptions &getHeaderSearchOpts() const {
187    return HeaderSearchOpts;
188  }
189
190  FrontendOptions &getFrontendOpts() { return FrontendOpts; }
191  const FrontendOptions &getFrontendOpts() const {
192    return FrontendOpts;
193  }
194
195  PreprocessorOptions &getPreprocessorOpts() { return PreprocessorOpts; }
196  const PreprocessorOptions &getPreprocessorOpts() const {
197    return PreprocessorOpts;
198  }
199
200  PreprocessorOutputOptions &getPreprocessorOutputOpts() {
201    return PreprocessorOutputOpts;
202  }
203  const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
204    return PreprocessorOutputOpts;
205  }
206
207  /// @}
208};
209
210} // end namespace clang
211
212#endif
213