clang  11.0.0git
CompilerInvocation.h
Go to the documentation of this file.
1 //===- CompilerInvocation.h - Compiler Invocation Helper Data ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
10 #define LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
11 
15 #include "clang/Basic/LLVM.h"
23 #include "llvm/ADT/IntrusiveRefCntPtr.h"
24 #include "llvm/ADT/ArrayRef.h"
25 #include <memory>
26 #include <string>
27 
28 namespace llvm {
29 
30 class Triple;
31 
32 namespace opt {
33 
34 class ArgList;
35 
36 } // namespace opt
37 
38 namespace vfs {
39 
40 class FileSystem;
41 
42 } // namespace vfs
43 
44 } // namespace llvm
45 
46 namespace clang {
47 
48 class DiagnosticsEngine;
49 class HeaderSearchOptions;
50 class PreprocessorOptions;
51 class TargetOptions;
52 
53 /// Fill out Opts based on the options given in Args.
54 ///
55 /// Args must have been created from the OptTable returned by
56 /// createCC1OptTable().
57 ///
58 /// When errors are encountered, return false and, if Diags is non-null,
59 /// report the error(s).
60 bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
61  DiagnosticsEngine *Diags = nullptr,
62  bool DefaultDiagColor = true);
63 
65 public:
66  /// Options controlling the language variant.
67  std::shared_ptr<LangOptions> LangOpts;
68 
69  /// Options controlling the target.
70  std::shared_ptr<TargetOptions> TargetOpts;
71 
72  /// Options controlling the diagnostic engine.
74 
75  /// Options controlling the \#include directive.
76  std::shared_ptr<HeaderSearchOptions> HeaderSearchOpts;
77 
78  /// Options controlling the preprocessor (aside from \#include handling).
79  std::shared_ptr<PreprocessorOptions> PreprocessorOpts;
80 
83  CompilerInvocationBase &operator=(const CompilerInvocationBase &) = delete;
85 
86  LangOptions *getLangOpts() { return LangOpts.get(); }
87  const LangOptions *getLangOpts() const { return LangOpts.get(); }
88 
89  TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
90  const TargetOptions &getTargetOpts() const { return *TargetOpts.get(); }
91 
92  DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
93 
94  HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
95 
97  return *HeaderSearchOpts;
98  }
99 
100  std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const {
101  return HeaderSearchOpts;
102  }
103 
104  std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
105  return PreprocessorOpts;
106  }
107 
108  PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
109 
111  return *PreprocessorOpts;
112  }
113 };
114 
115 /// Helper class for holding the data necessary to invoke the compiler.
116 ///
117 /// This class is designed to represent an abstract "invocation" of the
118 /// compiler, including data such as the include paths, the code generation
119 /// options, the warning flags, and so on.
121  /// Options controlling the static analyzer.
122  AnalyzerOptionsRef AnalyzerOpts;
123 
124  MigratorOptions MigratorOpts;
125 
126  /// Options controlling IRgen and the backend.
127  CodeGenOptions CodeGenOpts;
128 
129  /// Options controlling dependency output.
130  DependencyOutputOptions DependencyOutputOpts;
131 
132  /// Options controlling file system operations.
133  FileSystemOptions FileSystemOpts;
134 
135  /// Options controlling the frontend itself.
136  FrontendOptions FrontendOpts;
137 
138  /// Options controlling preprocessed output.
139  PreprocessorOutputOptions PreprocessorOutputOpts;
140 
141 public:
142  CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
143 
144  /// @name Utility Methods
145  /// @{
146 
147  /// Create a compiler invocation from a list of input options.
148  /// \returns true on success.
149  ///
150  /// \returns false if an error was encountered while parsing the arguments
151  /// and attempts to recover and continue parsing the rest of the arguments.
152  /// The recovery is best-effort and only guarantees that \p Res will end up in
153  /// one of the vaild-to-access (albeit arbitrary) states.
154  ///
155  /// \param [out] Res - The resulting invocation.
156  /// \param [in] CommandLineArgs - Array of argument strings, this must not
157  /// contain "-cc1".
158  static bool CreateFromArgs(CompilerInvocation &Res,
159  ArrayRef<const char *> CommandLineArgs,
160  DiagnosticsEngine &Diags,
161  const char *Argv0 = nullptr);
162 
163  /// Get the directory where the compiler headers
164  /// reside, relative to the compiler binary (found by the passed in
165  /// arguments).
166  ///
167  /// \param Argv0 - The program path (from argv[0]), for finding the builtin
168  /// compiler path.
169  /// \param MainAddr - The address of main (or some other function in the main
170  /// executable), for finding the builtin compiler path.
171  static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
172 
173  /// Set language defaults for the given input language and
174  /// language standard in the given LangOptions object.
175  ///
176  /// \param Opts - The LangOptions object to set up.
177  /// \param IK - The input language.
178  /// \param T - The target triple.
179  /// \param PPOpts - The PreprocessorOptions affected.
180  /// \param LangStd - The input language standard.
181  static void setLangDefaults(LangOptions &Opts, InputKind IK,
182  const llvm::Triple &T, PreprocessorOptions &PPOpts,
183  LangStandard::Kind LangStd = LangStandard::lang_unspecified);
184 
185  /// Retrieve a module hash string that is suitable for uniquely
186  /// identifying the conditions under which the module was built.
187  std::string getModuleHash() const;
188 
189  using StringAllocator = llvm::function_ref<const char *(const llvm::Twine &)>;
190  /// Generate a cc1-compatible command line arguments from this instance.
191  ///
192  /// \param [out] Args - The generated arguments. Note that the caller is
193  /// responsible for inserting the path to the clang executable and "-cc1" if
194  /// desired.
195  /// \param SA - A function that given a Twine can allocate storage for a given
196  /// command line argument and return a pointer to the newly allocated string.
197  /// The returned pointer is what gets appended to Args.
198  void generateCC1CommandLine(llvm::SmallVectorImpl<const char *> &Args,
199  StringAllocator SA) const;
200 
201  /// @}
202  /// @name Option Subgroups
203  /// @{
204 
205  AnalyzerOptionsRef getAnalyzerOpts() const { return AnalyzerOpts; }
206 
207  MigratorOptions &getMigratorOpts() { return MigratorOpts; }
208  const MigratorOptions &getMigratorOpts() const { return MigratorOpts; }
209 
210  CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
211  const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
212 
214  return DependencyOutputOpts;
215  }
216 
218  return DependencyOutputOpts;
219  }
220 
221  FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
222 
224  return FileSystemOpts;
225  }
226 
227  FrontendOptions &getFrontendOpts() { return FrontendOpts; }
228  const FrontendOptions &getFrontendOpts() const { return FrontendOpts; }
229 
231  return PreprocessorOutputOpts;
232  }
233 
235  return PreprocessorOutputOpts;
236  }
237 
238  /// @}
239 
240 private:
241  /// Parse options for flags that expose marshalling information in their
242  /// table-gen definition
243  ///
244  /// \param Args - The argument list containing the arguments to parse
245  /// \param Diags - The DiagnosticsEngine associated with CreateFromArgs
246  /// \returns - True if parsing was successful, false otherwise
247  bool parseSimpleArgs(const llvm::opt::ArgList &Args,
248  DiagnosticsEngine &Diags);
249 };
250 
253  DiagnosticsEngine &Diags);
254 
256  const CompilerInvocation &CI, DiagnosticsEngine &Diags,
258 
259 } // namespace clang
260 
261 #endif // LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
HeaderSearchOptions & getHeaderSearchOpts()
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags, IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS)
const HeaderSearchOptions & getHeaderSearchOpts() const
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
DependencyOutputOptions & getDependencyOutputOpts()
std::shared_ptr< HeaderSearchOptions > HeaderSearchOpts
Options controlling the #include directive.
bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args, DiagnosticsEngine *Diags=nullptr, bool DefaultDiagColor=true)
Fill out Opts based on the options given in Args.
std::shared_ptr< LangOptions > LangOpts
Options controlling the language variant.
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
llvm::function_ref< const char *(const llvm::Twine &)> StringAllocator
const MigratorOptions & getMigratorOpts() const
Options for controlling the target.
Definition: TargetOptions.h:26
const PreprocessorOutputOptions & getPreprocessorOutputOpts() const
const LangOptions * getLangOpts() const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:54
const PreprocessorOptions & getPreprocessorOpts() const
std::shared_ptr< PreprocessorOptions > PreprocessorOpts
Options controlling the preprocessor (aside from #include handling).
CodeGenOptions & getCodeGenOpts()
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:153
std::shared_ptr< PreprocessorOptions > getPreprocessorOptsPtr()
PreprocessorOutputOptions & getPreprocessorOutputOpts()
FrontendOptions & getFrontendOpts()
MigratorOptions & getMigratorOpts()
PreprocessorOutputOptions - Options for controlling the C preprocessor output (e.g., -E).
Defines the clang::LangOptions interface.
AnalyzerOptionsRef getAnalyzerOpts() const
Options for controlling the compiler diagnostics engine.
The kind of a file that we&#39;ve been handed as an input.
const FrontendOptions & getFrontendOpts() const
const CodeGenOptions & getCodeGenOpts() const
DiagnosticOptions & getDiagnosticOpts() const
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
std::shared_ptr< TargetOptions > TargetOpts
Options controlling the target.
std::shared_ptr< HeaderSearchOptions > getHeaderSearchOptsPtr() const
const FileSystemOptions & getFileSystemOpts() const
Dataflow Directional Tag Classes.
PreprocessorOptions & getPreprocessorOpts()
Helper class for holding the data necessary to invoke the compiler.
FrontendOptions - Options for controlling the behavior of the frontend.
Defines the clang::FileSystemOptions interface.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
IntrusiveRefCntPtr< DiagnosticOptions > DiagnosticOpts
Options controlling the diagnostic engine.
Stores options for the analyzer from the command line.
Keeps track of options that affect how file operations are performed.
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:15145
FileSystemOptions & getFileSystemOpts()
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
const TargetOptions & getTargetOpts() const
const DependencyOutputOptions & getDependencyOutputOpts() const