clang 19.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
16#include "clang/Basic/LLVM.h"
24#include "llvm/ADT/IntrusiveRefCntPtr.h"
25#include "llvm/ADT/ArrayRef.h"
26#include <memory>
27#include <string>
28
29namespace llvm {
30
31class Triple;
32
33namespace opt {
34
35class ArgList;
36
37} // namespace opt
38
39namespace vfs {
40
41class FileSystem;
42
43} // namespace vfs
44
45} // namespace llvm
46
47namespace clang {
48
49class DiagnosticsEngine;
50class HeaderSearchOptions;
51class PreprocessorOptions;
52class TargetOptions;
53
54// This lets us create the DiagnosticsEngine with a properly-filled-out
55// DiagnosticOptions instance.
56std::unique_ptr<DiagnosticOptions>
57CreateAndPopulateDiagOpts(ArrayRef<const char *> Argv);
58
59/// Fill out Opts based on the options given in Args.
60///
61/// Args must have been created from the OptTable returned by
62/// createCC1OptTable().
63///
64/// When errors are encountered, return false and, if Diags is non-null,
65/// report the error(s).
66bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
67 DiagnosticsEngine *Diags = nullptr,
68 bool DefaultDiagColor = true);
69
70/// The base class of CompilerInvocation. It keeps individual option objects
71/// behind reference-counted pointers, which is useful for clients that want to
72/// keep select option objects alive (even after CompilerInvocation gets
73/// destroyed) without making a copy.
75protected:
76 /// Options controlling the language variant.
77 std::shared_ptr<LangOptions> LangOpts;
78
79 /// Options controlling the target.
80 std::shared_ptr<TargetOptions> TargetOpts;
81
82 /// Options controlling the diagnostic engine.
84
85 /// Options controlling the \#include directive.
86 std::shared_ptr<HeaderSearchOptions> HSOpts;
87
88 /// Options controlling the preprocessor (aside from \#include handling).
89 std::shared_ptr<PreprocessorOptions> PPOpts;
90
91 /// Options controlling the static analyzer.
93
94 std::shared_ptr<MigratorOptions> MigratorOpts;
95
96 /// Options controlling API notes.
97 std::shared_ptr<APINotesOptions> APINotesOpts;
98
99 /// Options controlling IRgen and the backend.
100 std::shared_ptr<CodeGenOptions> CodeGenOpts;
101
102 /// Options controlling file system operations.
103 std::shared_ptr<FileSystemOptions> FSOpts;
104
105 /// Options controlling the frontend itself.
106 std::shared_ptr<FrontendOptions> FrontendOpts;
107
108 /// Options controlling dependency output.
109 std::shared_ptr<DependencyOutputOptions> DependencyOutputOpts;
110
111 /// Options controlling preprocessed output.
112 std::shared_ptr<PreprocessorOutputOptions> PreprocessorOutputOpts;
113
114 /// Dummy tag type whose instance can be passed into the constructor to
115 /// prevent creation of the reference-counted option objects.
117
127
128public:
129 /// Const getters.
130 /// @{
131 const LangOptions &getLangOpts() const { return *LangOpts; }
132 const TargetOptions &getTargetOpts() const { return *TargetOpts; }
136 const AnalyzerOptions &getAnalyzerOpts() const { return *AnalyzerOpts; }
137 const MigratorOptions &getMigratorOpts() const { return *MigratorOpts; }
138 const APINotesOptions &getAPINotesOpts() const { return *APINotesOpts; }
139 const CodeGenOptions &getCodeGenOpts() const { return *CodeGenOpts; }
140 const FileSystemOptions &getFileSystemOpts() const { return *FSOpts; }
141 const FrontendOptions &getFrontendOpts() const { return *FrontendOpts; }
143 return *DependencyOutputOpts;
144 }
147 }
148 /// @}
149
150 /// Command line generation.
151 /// @{
152 using StringAllocator = llvm::function_ref<const char *(const Twine &)>;
153 /// Generate cc1-compatible command line arguments from this instance.
154 ///
155 /// \param [out] Args - The generated arguments. Note that the caller is
156 /// responsible for inserting the path to the clang executable and "-cc1" if
157 /// desired.
158 /// \param SA - A function that given a Twine can allocate storage for a given
159 /// command line argument and return a pointer to the newly allocated string.
160 /// The returned pointer is what gets appended to Args.
162 StringAllocator SA) const {
163 generateCC1CommandLine([&](const Twine &Arg) {
164 // No need to allocate static string literals.
165 Args.push_back(Arg.isSingleStringLiteral()
166 ? Arg.getSingleStringRef().data()
167 : SA(Arg));
168 });
169 }
170
171 using ArgumentConsumer = llvm::function_ref<void(const Twine &)>;
172 /// Generate cc1-compatible command line arguments from this instance.
173 ///
174 /// \param Consumer - Callback that gets invoked for every single generated
175 /// command line argument.
176 void generateCC1CommandLine(ArgumentConsumer Consumer) const;
177
178 /// Generate cc1-compatible command line arguments from this instance,
179 /// wrapping the result as a std::vector<std::string>.
180 ///
181 /// This is a (less-efficient) wrapper over generateCC1CommandLine().
182 std::vector<std::string> getCC1CommandLine() const;
183
184private:
185 /// Generate command line options from DiagnosticOptions.
186 static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
187 ArgumentConsumer Consumer,
188 bool DefaultDiagColor);
189
190 /// Generate command line options from LangOptions.
191 static void GenerateLangArgs(const LangOptions &Opts,
192 ArgumentConsumer Consumer, const llvm::Triple &T,
193 InputKind IK);
194
195 // Generate command line options from CodeGenOptions.
196 static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
197 ArgumentConsumer Consumer,
198 const llvm::Triple &T,
199 const std::string &OutputFile,
200 const LangOptions *LangOpts);
201 /// @}
202};
203
204/// Helper class for holding the data necessary to invoke the compiler.
205///
206/// This class is designed to represent an abstract "invocation" of the
207/// compiler, including data such as the include paths, the code generation
208/// options, the warning flags, and so on.
210public:
215 }
219 return *this;
220 }
222
223 /// Const getters.
224 /// @{
225 // Note: These need to be pulled in manually. Otherwise, they get hidden by
226 // the mutable getters with the same names.
240 /// @}
241
242 /// Mutable getters.
243 /// @{
256 return *DependencyOutputOpts;
257 }
260 }
261 /// @}
262
263 /// Base class internals.
264 /// @{
268 std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() {
269 return HSOpts;
270 }
271 std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
272 return PPOpts;
273 }
274 std::shared_ptr<LangOptions> getLangOptsPtr() { return LangOpts; }
275 /// @}
276
277 /// Create a compiler invocation from a list of input options.
278 /// \returns true on success.
279 ///
280 /// \returns false if an error was encountered while parsing the arguments
281 /// and attempts to recover and continue parsing the rest of the arguments.
282 /// The recovery is best-effort and only guarantees that \p Res will end up in
283 /// one of the vaild-to-access (albeit arbitrary) states.
284 ///
285 /// \param [out] Res - The resulting invocation.
286 /// \param [in] CommandLineArgs - Array of argument strings, this must not
287 /// contain "-cc1".
288 static bool CreateFromArgs(CompilerInvocation &Res,
289 ArrayRef<const char *> CommandLineArgs,
290 DiagnosticsEngine &Diags,
291 const char *Argv0 = nullptr);
292
293 /// Get the directory where the compiler headers
294 /// reside, relative to the compiler binary (found by the passed in
295 /// arguments).
296 ///
297 /// \param Argv0 - The program path (from argv[0]), for finding the builtin
298 /// compiler path.
299 /// \param MainAddr - The address of main (or some other function in the main
300 /// executable), for finding the builtin compiler path.
301 static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
302
303 /// Retrieve a module hash string that is suitable for uniquely
304 /// identifying the conditions under which the module was built.
305 std::string getModuleHash() const;
306
307 /// Check that \p Args can be parsed and re-serialized without change,
308 /// emiting diagnostics for any differences.
309 ///
310 /// This check is only suitable for command-lines that are expected to already
311 /// be canonical.
312 ///
313 /// \return false if there are any errors.
315 DiagnosticsEngine &Diags,
316 const char *Argv0 = nullptr);
317
318 /// Reset all of the options that are not considered when building a
319 /// module.
321
322 /// Disable implicit modules and canonicalize options that are only used by
323 /// implicit modules.
325
326private:
327 static bool CreateFromArgsImpl(CompilerInvocation &Res,
328 ArrayRef<const char *> CommandLineArgs,
329 DiagnosticsEngine &Diags, const char *Argv0);
330
331 /// Parse command line options that map to LangOptions.
332 static bool ParseLangArgs(LangOptions &Opts, llvm::opt::ArgList &Args,
333 InputKind IK, const llvm::Triple &T,
334 std::vector<std::string> &Includes,
335 DiagnosticsEngine &Diags);
336
337 /// Parse command line options that map to CodeGenOptions.
338 static bool ParseCodeGenArgs(CodeGenOptions &Opts, llvm::opt::ArgList &Args,
339 InputKind IK, DiagnosticsEngine &Diags,
340 const llvm::Triple &T,
341 const std::string &OutputFile,
342 const LangOptions &LangOptsRef);
343};
344
345/// Same as \c CompilerInvocation, but with copy-on-write optimization.
347public:
352 }
356 return *this;
357 }
359
363 }
364
366 : CompilerInvocationBase(std::move(X)) {}
367
368 // Const getters are inherited from the base class.
369
370 /// Mutable getters.
371 /// @{
385 /// @}
386};
387
388IntrusiveRefCntPtr<llvm::vfs::FileSystem>
389createVFSFromCompilerInvocation(const CompilerInvocation &CI,
390 DiagnosticsEngine &Diags);
391
392IntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation(
393 const CompilerInvocation &CI, DiagnosticsEngine &Diags,
394 IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
395
396IntrusiveRefCntPtr<llvm::vfs::FileSystem>
397createVFSFromOverlayFiles(ArrayRef<std::string> VFSOverlayFiles,
398 DiagnosticsEngine &Diags,
399 IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
400
401} // namespace clang
402
403#endif // LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
Defines the clang::FileSystemOptions interface.
#define X(type, name)
Definition: Value.h:142
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::LangOptions interface.
Tracks various options which control how API notes are found and handled.
Stores options for the analyzer from the command line.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
The base class of CompilerInvocation.
std::shared_ptr< MigratorOptions > MigratorOpts
std::shared_ptr< PreprocessorOutputOptions > PreprocessorOutputOpts
Options controlling preprocessed output.
std::shared_ptr< APINotesOptions > APINotesOpts
Options controlling API notes.
std::shared_ptr< TargetOptions > TargetOpts
Options controlling the target.
const FrontendOptions & getFrontendOpts() const
const CodeGenOptions & getCodeGenOpts() const
CompilerInvocationBase(CompilerInvocationBase &&X)=default
llvm::function_ref< const char *(const Twine &)> StringAllocator
Command line generation.
const FileSystemOptions & getFileSystemOpts() const
std::shared_ptr< PreprocessorOptions > PPOpts
Options controlling the preprocessor (aside from #include handling).
const PreprocessorOutputOptions & getPreprocessorOutputOpts() const
std::vector< std::string > getCC1CommandLine() const
Generate cc1-compatible command line arguments from this instance, wrapping the result as a std::vect...
CompilerInvocationBase(EmptyConstructor)
std::shared_ptr< FileSystemOptions > FSOpts
Options controlling file system operations.
const AnalyzerOptions & getAnalyzerOpts() const
const MigratorOptions & getMigratorOpts() const
void generateCC1CommandLine(llvm::SmallVectorImpl< const char * > &Args, StringAllocator SA) const
Generate cc1-compatible command line arguments from this instance.
CompilerInvocationBase & deep_copy_assign(const CompilerInvocationBase &X)
const DependencyOutputOptions & getDependencyOutputOpts() const
AnalyzerOptionsRef AnalyzerOpts
Options controlling the static analyzer.
IntrusiveRefCntPtr< DiagnosticOptions > DiagnosticOpts
Options controlling the diagnostic engine.
CompilerInvocationBase & shallow_copy_assign(const CompilerInvocationBase &X)
CompilerInvocationBase(const CompilerInvocationBase &X)=delete
const TargetOptions & getTargetOpts() const
CompilerInvocationBase & operator=(CompilerInvocationBase &&X)=default
std::shared_ptr< CodeGenOptions > CodeGenOpts
Options controlling IRgen and the backend.
CompilerInvocationBase & operator=(const CompilerInvocationBase &X)=delete
std::shared_ptr< LangOptions > LangOpts
Options controlling the language variant.
const APINotesOptions & getAPINotesOpts() const
const HeaderSearchOptions & getHeaderSearchOpts() const
std::shared_ptr< HeaderSearchOptions > HSOpts
Options controlling the #include directive.
const PreprocessorOptions & getPreprocessorOpts() const
const DiagnosticOptions & getDiagnosticOpts() const
const LangOptions & getLangOpts() const
Const getters.
std::shared_ptr< FrontendOptions > FrontendOpts
Options controlling the frontend itself.
llvm::function_ref< void(const Twine &)> ArgumentConsumer
std::shared_ptr< DependencyOutputOptions > DependencyOutputOpts
Options controlling dependency output.
Helper class for holding the data necessary to invoke the compiler.
PreprocessorOptions & getPreprocessorOpts()
void clearImplicitModuleBuildOptions()
Disable implicit modules and canonicalize options that are only used by implicit modules.
MigratorOptions & getMigratorOpts()
AnalyzerOptions & getAnalyzerOpts()
APINotesOptions & getAPINotesOpts()
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.
std::shared_ptr< TargetOptions > TargetOpts
Options controlling the target.
std::shared_ptr< HeaderSearchOptions > getHeaderSearchOptsPtr()
LangOptions & getLangOpts()
Mutable getters.
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...
DependencyOutputOptions & getDependencyOutputOpts()
void resetNonModularOptions()
Reset all of the options that are not considered when building a module.
FrontendOptions & getFrontendOpts()
std::string getModuleHash() const
Retrieve a module hash string that is suitable for uniquely identifying the conditions under which th...
CompilerInvocation(const CompilerInvocation &X)
FileSystemOptions & getFileSystemOpts()
IntrusiveRefCntPtr< DiagnosticOptions > DiagnosticOpts
Options controlling the diagnostic engine.
CompilerInvocation(CompilerInvocation &&)=default
CompilerInvocation & operator=(const CompilerInvocation &X)
std::shared_ptr< LangOptions > getLangOptsPtr()
CodeGenOptions & getCodeGenOpts()
std::shared_ptr< LangOptions > LangOpts
Base class internals.
TargetOptions & getTargetOpts()
HeaderSearchOptions & getHeaderSearchOpts()
DiagnosticOptions & getDiagnosticOpts()
PreprocessorOutputOptions & getPreprocessorOutputOpts()
std::shared_ptr< PreprocessorOptions > getPreprocessorOptsPtr()
Same as CompilerInvocation, but with copy-on-write optimization.
FrontendOptions & getMutFrontendOpts()
LangOptions & getMutLangOpts()
Mutable getters.
HeaderSearchOptions & getMutHeaderSearchOpts()
MigratorOptions & getMutMigratorOpts()
CowCompilerInvocation(CowCompilerInvocation &&)=default
PreprocessorOptions & getMutPreprocessorOpts()
APINotesOptions & getMutAPINotesOpts()
PreprocessorOutputOptions & getMutPreprocessorOutputOpts()
FileSystemOptions & getMutFileSystemOpts()
AnalyzerOptions & getMutAnalyzerOpts()
CowCompilerInvocation(const CompilerInvocation &X)
DiagnosticOptions & getMutDiagnosticOpts()
CowCompilerInvocation(const CowCompilerInvocation &X)
DependencyOutputOptions & getMutDependencyOutputOpts()
CowCompilerInvocation & operator=(const CowCompilerInvocation &X)
CowCompilerInvocation(CompilerInvocation &&X)
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:418
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
The JSON file list parser is used to communicate input to InstallAPI.
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
Definition: Format.h:5279
Dummy tag type whose instance can be passed into the constructor to prevent creation of the reference...