clang 18.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. It keeps individual option objects
70/// behind reference-counted pointers, which is useful for clients that want to
71/// keep select option objects alive (even after CompilerInvocation gets
72/// destroyed) without making a copy.
74protected:
75 /// Options controlling the language variant.
76 std::shared_ptr<LangOptions> LangOpts;
77
78 /// Options controlling the target.
79 std::shared_ptr<TargetOptions> TargetOpts;
80
81 /// Options controlling the diagnostic engine.
83
84 /// Options controlling the \#include directive.
85 std::shared_ptr<HeaderSearchOptions> HSOpts;
86
87 /// Options controlling the preprocessor (aside from \#include handling).
88 std::shared_ptr<PreprocessorOptions> PPOpts;
89
90 /// Options controlling the static analyzer.
92
93 std::shared_ptr<MigratorOptions> MigratorOpts;
94
95 /// Options controlling IRgen and the backend.
96 std::shared_ptr<CodeGenOptions> CodeGenOpts;
97
98 /// Options controlling file system operations.
99 std::shared_ptr<FileSystemOptions> FSOpts;
100
101 /// Options controlling the frontend itself.
102 std::shared_ptr<FrontendOptions> FrontendOpts;
103
104 /// Options controlling dependency output.
105 std::shared_ptr<DependencyOutputOptions> DependencyOutputOpts;
106
107 /// Options controlling preprocessed output.
108 std::shared_ptr<PreprocessorOutputOptions> PreprocessorOutputOpts;
109
110 /// Dummy tag type whose instance can be passed into the constructor to
111 /// prevent creation of the reference-counted option objects.
113
123
124public:
125 /// Const getters.
126 /// @{
127 const LangOptions &getLangOpts() const { return *LangOpts; }
128 const TargetOptions &getTargetOpts() const { return *TargetOpts; }
132 const AnalyzerOptions &getAnalyzerOpts() const { return *AnalyzerOpts; }
133 const MigratorOptions &getMigratorOpts() const { return *MigratorOpts; }
134 const CodeGenOptions &getCodeGenOpts() const { return *CodeGenOpts; }
135 const FileSystemOptions &getFileSystemOpts() const { return *FSOpts; }
136 const FrontendOptions &getFrontendOpts() const { return *FrontendOpts; }
138 return *DependencyOutputOpts;
139 }
142 }
143 /// @}
144
145 /// Command line generation.
146 /// @{
147 using StringAllocator = llvm::function_ref<const char *(const Twine &)>;
148 /// Generate cc1-compatible command line arguments from this instance.
149 ///
150 /// \param [out] Args - The generated arguments. Note that the caller is
151 /// responsible for inserting the path to the clang executable and "-cc1" if
152 /// desired.
153 /// \param SA - A function that given a Twine can allocate storage for a given
154 /// command line argument and return a pointer to the newly allocated string.
155 /// The returned pointer is what gets appended to Args.
157 StringAllocator SA) const {
158 generateCC1CommandLine([&](const Twine &Arg) {
159 // No need to allocate static string literals.
160 Args.push_back(Arg.isSingleStringLiteral()
161 ? Arg.getSingleStringRef().data()
162 : SA(Arg));
163 });
164 }
165
166 using ArgumentConsumer = llvm::function_ref<void(const Twine &)>;
167 /// Generate cc1-compatible command line arguments from this instance.
168 ///
169 /// \param Consumer - Callback that gets invoked for every single generated
170 /// command line argument.
171 void generateCC1CommandLine(ArgumentConsumer Consumer) const;
172
173 /// Generate cc1-compatible command line arguments from this instance,
174 /// wrapping the result as a std::vector<std::string>.
175 ///
176 /// This is a (less-efficient) wrapper over generateCC1CommandLine().
177 std::vector<std::string> getCC1CommandLine() const;
178
179private:
180 /// Generate command line options from DiagnosticOptions.
181 static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
182 ArgumentConsumer Consumer,
183 bool DefaultDiagColor);
184
185 /// Generate command line options from LangOptions.
186 static void GenerateLangArgs(const LangOptions &Opts,
187 ArgumentConsumer Consumer, const llvm::Triple &T,
188 InputKind IK);
189
190 // Generate command line options from CodeGenOptions.
191 static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
192 ArgumentConsumer Consumer,
193 const llvm::Triple &T,
194 const std::string &OutputFile,
195 const LangOptions *LangOpts);
196 /// @}
197};
198
199/// Helper class for holding the data necessary to invoke the compiler.
200///
201/// This class is designed to represent an abstract "invocation" of the
202/// compiler, including data such as the include paths, the code generation
203/// options, the warning flags, and so on.
205public:
210 }
214 return *this;
215 }
217
218 /// Const getters.
219 /// @{
220 // Note: These need to be pulled in manually. Otherwise, they get hidden by
221 // the mutable getters with the same names.
234 /// @}
235
236 /// Mutable getters.
237 /// @{
249 return *DependencyOutputOpts;
250 }
253 }
254 /// @}
255
256 /// Base class internals.
257 /// @{
261 std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() {
262 return HSOpts;
263 }
264 std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
265 return PPOpts;
266 }
267 /// @}
268
269 /// Create a compiler invocation from a list of input options.
270 /// \returns true on success.
271 ///
272 /// \returns false if an error was encountered while parsing the arguments
273 /// and attempts to recover and continue parsing the rest of the arguments.
274 /// The recovery is best-effort and only guarantees that \p Res will end up in
275 /// one of the vaild-to-access (albeit arbitrary) states.
276 ///
277 /// \param [out] Res - The resulting invocation.
278 /// \param [in] CommandLineArgs - Array of argument strings, this must not
279 /// contain "-cc1".
280 static bool CreateFromArgs(CompilerInvocation &Res,
281 ArrayRef<const char *> CommandLineArgs,
282 DiagnosticsEngine &Diags,
283 const char *Argv0 = nullptr);
284
285 /// Get the directory where the compiler headers
286 /// reside, relative to the compiler binary (found by the passed in
287 /// arguments).
288 ///
289 /// \param Argv0 - The program path (from argv[0]), for finding the builtin
290 /// compiler path.
291 /// \param MainAddr - The address of main (or some other function in the main
292 /// executable), for finding the builtin compiler path.
293 static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
294
295 /// Retrieve a module hash string that is suitable for uniquely
296 /// identifying the conditions under which the module was built.
297 std::string getModuleHash() const;
298
299 /// Check that \p Args can be parsed and re-serialized without change,
300 /// emiting diagnostics for any differences.
301 ///
302 /// This check is only suitable for command-lines that are expected to already
303 /// be canonical.
304 ///
305 /// \return false if there are any errors.
307 DiagnosticsEngine &Diags,
308 const char *Argv0 = nullptr);
309
310 /// Reset all of the options that are not considered when building a
311 /// module.
313
314 /// Disable implicit modules and canonicalize options that are only used by
315 /// implicit modules.
317
318private:
319 static bool CreateFromArgsImpl(CompilerInvocation &Res,
320 ArrayRef<const char *> CommandLineArgs,
321 DiagnosticsEngine &Diags, const char *Argv0);
322
323 /// Parse command line options that map to LangOptions.
324 static bool ParseLangArgs(LangOptions &Opts, llvm::opt::ArgList &Args,
325 InputKind IK, const llvm::Triple &T,
326 std::vector<std::string> &Includes,
327 DiagnosticsEngine &Diags);
328
329 /// Parse command line options that map to CodeGenOptions.
330 static bool ParseCodeGenArgs(CodeGenOptions &Opts, llvm::opt::ArgList &Args,
331 InputKind IK, DiagnosticsEngine &Diags,
332 const llvm::Triple &T,
333 const std::string &OutputFile,
334 const LangOptions &LangOptsRef);
335};
336
337/// Same as \c CompilerInvocation, but with copy-on-write optimization.
339public:
344 }
348 return *this;
349 }
351
355 }
356
358 : CompilerInvocationBase(std::move(X)) {}
359
360 // Const getters are inherited from the base class.
361
362 /// Mutable getters.
363 /// @{
376 /// @}
377};
378
379IntrusiveRefCntPtr<llvm::vfs::FileSystem>
380createVFSFromCompilerInvocation(const CompilerInvocation &CI,
381 DiagnosticsEngine &Diags);
382
383IntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation(
384 const CompilerInvocation &CI, DiagnosticsEngine &Diags,
385 IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
386
387IntrusiveRefCntPtr<llvm::vfs::FileSystem>
388createVFSFromOverlayFiles(ArrayRef<std::string> VFSOverlayFiles,
389 DiagnosticsEngine &Diags,
390 IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
391
392} // namespace clang
393
394#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.
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< 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 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()
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)
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()
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:83
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
Definition: Format.h:5078
Dummy tag type whose instance can be passed into the constructor to prevent creation of the reference...