clang 17.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
28namespace llvm {
29
30class Triple;
31
32namespace opt {
33
34class ArgList;
35
36} // namespace opt
37
38namespace vfs {
39
40class FileSystem;
41
42} // namespace vfs
43
44} // namespace llvm
45
46namespace clang {
47
48class DiagnosticsEngine;
49class HeaderSearchOptions;
50class PreprocessorOptions;
51class TargetOptions;
52
53// This lets us create the DiagnosticsEngine with a properly-filled-out
54// DiagnosticOptions instance.
55std::unique_ptr<DiagnosticOptions>
56CreateAndPopulateDiagOpts(ArrayRef<const char *> Argv);
57
58/// Fill out Opts based on the options given in Args.
59///
60/// Args must have been created from the OptTable returned by
61/// createCC1OptTable().
62///
63/// When errors are encountered, return false and, if Diags is non-null,
64/// report the error(s).
65bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
66 DiagnosticsEngine *Diags = nullptr,
67 bool DefaultDiagColor = true);
68
69/// The base class of CompilerInvocation with reference semantics.
70///
71/// This class stores option objects behind reference-counted pointers. This is
72/// useful for clients that want to keep some option object around even after
73/// CompilerInvocation gets destroyed, without making a copy.
74///
75/// This is a separate class so that we can implement the copy constructor and
76/// assignment here and leave them defaulted in the rest of CompilerInvocation.
78public:
79 /// Options controlling the language variant.
80 std::shared_ptr<LangOptions> LangOpts;
81
82 /// Options controlling the target.
83 std::shared_ptr<TargetOptions> TargetOpts;
84
85 /// Options controlling the diagnostic engine.
87
88 /// Options controlling the \#include directive.
89 std::shared_ptr<HeaderSearchOptions> HeaderSearchOpts;
90
91 /// Options controlling the preprocessor (aside from \#include handling).
92 std::shared_ptr<PreprocessorOptions> PreprocessorOpts;
93
94 /// Options controlling the static analyzer.
96
103
104 LangOptions *getLangOpts() { return LangOpts.get(); }
105 const LangOptions *getLangOpts() const { return LangOpts.get(); }
106
108 const TargetOptions &getTargetOpts() const { return *TargetOpts.get(); }
109
111
113
115 return *HeaderSearchOpts;
116 }
117
118 std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const {
119 return HeaderSearchOpts;
120 }
121
122 std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
123 return PreprocessorOpts;
124 }
125
127
129 return *PreprocessorOpts;
130 }
131
133};
134
135/// The base class of CompilerInvocation with value semantics.
137protected:
139
140 /// Options controlling IRgen and the backend.
142
143 /// Options controlling dependency output.
145
146 /// Options controlling file system operations.
148
149 /// Options controlling the frontend itself.
151
152 /// Options controlling preprocessed output.
154
155public:
158
160 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
161
164 }
165
168 }
169
171
173 return FileSystemOpts;
174 }
175
178
181 }
182
185 }
186};
187
188/// Helper class for holding the data necessary to invoke the compiler.
189///
190/// This class is designed to represent an abstract "invocation" of the
191/// compiler, including data such as the include paths, the code generation
192/// options, the warning flags, and so on.
195public:
196 /// Create a compiler invocation from a list of input options.
197 /// \returns true on success.
198 ///
199 /// \returns false if an error was encountered while parsing the arguments
200 /// and attempts to recover and continue parsing the rest of the arguments.
201 /// The recovery is best-effort and only guarantees that \p Res will end up in
202 /// one of the vaild-to-access (albeit arbitrary) states.
203 ///
204 /// \param [out] Res - The resulting invocation.
205 /// \param [in] CommandLineArgs - Array of argument strings, this must not
206 /// contain "-cc1".
207 static bool CreateFromArgs(CompilerInvocation &Res,
208 ArrayRef<const char *> CommandLineArgs,
209 DiagnosticsEngine &Diags,
210 const char *Argv0 = nullptr);
211
212 /// Get the directory where the compiler headers
213 /// reside, relative to the compiler binary (found by the passed in
214 /// arguments).
215 ///
216 /// \param Argv0 - The program path (from argv[0]), for finding the builtin
217 /// compiler path.
218 /// \param MainAddr - The address of main (or some other function in the main
219 /// executable), for finding the builtin compiler path.
220 static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
221
222 /// Retrieve a module hash string that is suitable for uniquely
223 /// identifying the conditions under which the module was built.
224 std::string getModuleHash() const;
225
226 using StringAllocator = llvm::function_ref<const char *(const llvm::Twine &)>;
227 /// Generate cc1-compatible command line arguments from this instance.
228 ///
229 /// \param [out] Args - The generated arguments. Note that the caller is
230 /// responsible for inserting the path to the clang executable and "-cc1" if
231 /// desired.
232 /// \param SA - A function that given a Twine can allocate storage for a given
233 /// command line argument and return a pointer to the newly allocated string.
234 /// The returned pointer is what gets appended to Args.
236 StringAllocator SA) const;
237
238 /// Generate cc1-compatible command line arguments from this instance,
239 /// wrapping the result as a std::vector<std::string>.
240 ///
241 /// This is a (less-efficient) wrapper over generateCC1CommandLine().
242 std::vector<std::string> getCC1CommandLine() const;
243
244 /// Check that \p Args can be parsed and re-serialized without change,
245 /// emiting diagnostics for any differences.
246 ///
247 /// This check is only suitable for command-lines that are expected to already
248 /// be canonical.
249 ///
250 /// \return false if there are any errors.
252 DiagnosticsEngine &Diags,
253 const char *Argv0 = nullptr);
254
255 /// Reset all of the options that are not considered when building a
256 /// module.
258
259 /// Disable implicit modules and canonicalize options that are only used by
260 /// implicit modules.
262
263private:
264 static bool CreateFromArgsImpl(CompilerInvocation &Res,
265 ArrayRef<const char *> CommandLineArgs,
266 DiagnosticsEngine &Diags, const char *Argv0);
267
268 /// Generate command line options from DiagnosticOptions.
269 static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
271 StringAllocator SA, bool DefaultDiagColor);
272
273 /// Parse command line options that map to LangOptions.
274 static bool ParseLangArgs(LangOptions &Opts, llvm::opt::ArgList &Args,
275 InputKind IK, const llvm::Triple &T,
276 std::vector<std::string> &Includes,
277 DiagnosticsEngine &Diags);
278
279 /// Generate command line options from LangOptions.
280 static void GenerateLangArgs(const LangOptions &Opts,
282 StringAllocator SA, const llvm::Triple &T,
283 InputKind IK);
284
285 /// Parse command line options that map to CodeGenOptions.
286 static bool ParseCodeGenArgs(CodeGenOptions &Opts, llvm::opt::ArgList &Args,
287 InputKind IK, DiagnosticsEngine &Diags,
288 const llvm::Triple &T,
289 const std::string &OutputFile,
290 const LangOptions &LangOptsRef);
291
292 // Generate command line options from CodeGenOptions.
293 static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
295 StringAllocator SA, const llvm::Triple &T,
296 const std::string &OutputFile,
297 const LangOptions *LangOpts);
298};
299
302 DiagnosticsEngine &Diags);
303
305 const CompilerInvocation &CI, DiagnosticsEngine &Diags,
307
310 DiagnosticsEngine &Diags,
312
313} // namespace clang
314
315#endif // LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
static CompilationDatabasePluginRegistry::Add< FixedCompilationDatabasePlugin > X("fixed-compilation-database", "Reads plain-text flags file")
Defines the clang::FileSystemOptions interface.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::LangOptions interface.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
The base class of CompilerInvocation with reference semantics.
PreprocessorOptions & getPreprocessorOpts()
AnalyzerOptionsRef AnalyzerOpts
Options controlling the static analyzer.
CompilerInvocationRefBase & operator=(CompilerInvocationRefBase X)
const PreprocessorOptions & getPreprocessorOpts() const
DiagnosticOptions & getDiagnosticOpts() const
CompilerInvocationRefBase(CompilerInvocationRefBase &&X)
CompilerInvocationRefBase & operator=(CompilerInvocationRefBase &&X)
IntrusiveRefCntPtr< DiagnosticOptions > DiagnosticOpts
Options controlling the diagnostic engine.
AnalyzerOptionsRef getAnalyzerOpts() const
const HeaderSearchOptions & getHeaderSearchOpts() const
std::shared_ptr< PreprocessorOptions > PreprocessorOpts
Options controlling the preprocessor (aside from #include handling).
std::shared_ptr< LangOptions > LangOpts
Options controlling the language variant.
HeaderSearchOptions & getHeaderSearchOpts()
std::shared_ptr< PreprocessorOptions > getPreprocessorOptsPtr()
std::shared_ptr< TargetOptions > TargetOpts
Options controlling the target.
std::shared_ptr< HeaderSearchOptions > HeaderSearchOpts
Options controlling the #include directive.
std::shared_ptr< HeaderSearchOptions > getHeaderSearchOptsPtr() const
const LangOptions * getLangOpts() const
const TargetOptions & getTargetOpts() const
The base class of CompilerInvocation with value semantics.
FileSystemOptions FileSystemOpts
Options controlling file system operations.
const MigratorOptions & getMigratorOpts() const
const FileSystemOptions & getFileSystemOpts() const
const FrontendOptions & getFrontendOpts() const
DependencyOutputOptions & getDependencyOutputOpts()
CodeGenOptions CodeGenOpts
Options controlling IRgen and the backend.
const PreprocessorOutputOptions & getPreprocessorOutputOpts() const
PreprocessorOutputOptions PreprocessorOutputOpts
Options controlling preprocessed output.
PreprocessorOutputOptions & getPreprocessorOutputOpts()
FrontendOptions FrontendOpts
Options controlling the frontend itself.
const CodeGenOptions & getCodeGenOpts() const
const DependencyOutputOptions & getDependencyOutputOpts() const
DependencyOutputOptions DependencyOutputOpts
Options controlling dependency output.
Helper class for holding the data necessary to invoke the compiler.
void clearImplicitModuleBuildOptions()
Disable implicit modules and canonicalize options that are only used by implicit modules.
static std::string GetResourcesPath(const char *Argv0, void *MainAddr)
Get the directory where the compiler headers reside, relative to the compiler binary (found by the pa...
static bool CreateFromArgs(CompilerInvocation &Res, ArrayRef< const char * > CommandLineArgs, DiagnosticsEngine &Diags, const char *Argv0=nullptr)
Create a compiler invocation from a list of input options.
static bool checkCC1RoundTrip(ArrayRef< const char * > Args, DiagnosticsEngine &Diags, const char *Argv0=nullptr)
Check that Args can be parsed and re-serialized without change, emiting diagnostics for any differenc...
void resetNonModularOptions()
Reset all of the options that are not considered when building a module.
void generateCC1CommandLine(llvm::SmallVectorImpl< const char * > &Args, StringAllocator SA) const
Generate cc1-compatible command line arguments from this instance.
std::string getModuleHash() const
Retrieve a module hash string that is suitable for uniquely identifying the conditions under which th...
std::vector< std::string > getCC1CommandLine() const
Generate cc1-compatible command line arguments from this instance, wrapping the result as a std::vect...
llvm::function_ref< const char *(const llvm::Twine &)> StringAllocator
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
Options for controlling the compiler diagnostics engine.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
Keeps track of options that affect how file operations are performed.
FrontendOptions - Options for controlling the behavior of the frontend.
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
The kind of a file that we've been handed as an input.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:82
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
PreprocessorOutputOptions - Options for controlling the C preprocessor output (e.g....
Options for controlling the target.
Definition: TargetOptions.h:26
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromOverlayFiles(ArrayRef< std::string > VFSOverlayFiles, DiagnosticsEngine &Diags, IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS)
std::unique_ptr< DiagnosticOptions > CreateAndPopulateDiagOpts(ArrayRef< const char * > Argv)
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args, DiagnosticsEngine *Diags=nullptr, bool DefaultDiagColor=true)
Fill out Opts based on the options given in Args.
YAML serialization mapping.
Definition: Dominators.h:30