clang  13.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 
81  /// Options controlling the static analyzer.
83 
88 
89  LangOptions *getLangOpts() { return LangOpts.get(); }
90  const LangOptions *getLangOpts() const { return LangOpts.get(); }
91 
92  TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
93  const TargetOptions &getTargetOpts() const { return *TargetOpts.get(); }
94 
96 
98 
100  return *HeaderSearchOpts;
101  }
102 
103  std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const {
104  return HeaderSearchOpts;
105  }
106 
107  std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
108  return PreprocessorOpts;
109  }
110 
112 
114  return *PreprocessorOpts;
115  }
116 
118 };
119 
120 /// Helper class for holding the data necessary to invoke the compiler.
121 ///
122 /// This class is designed to represent an abstract "invocation" of the
123 /// compiler, including data such as the include paths, the code generation
124 /// options, the warning flags, and so on.
126  MigratorOptions MigratorOpts;
127 
128  /// Options controlling IRgen and the backend.
129  CodeGenOptions CodeGenOpts;
130 
131  /// Options controlling dependency output.
132  DependencyOutputOptions DependencyOutputOpts;
133 
134  /// Options controlling file system operations.
135  FileSystemOptions FileSystemOpts;
136 
137  /// Options controlling the frontend itself.
138  FrontendOptions FrontendOpts;
139 
140  /// Options controlling preprocessed output.
141  PreprocessorOutputOptions PreprocessorOutputOpts;
142 
143 public:
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 Includes - The affected list of included files.
180  /// \param LangStd - The input language standard.
181  static void
182  setLangDefaults(LangOptions &Opts, InputKind IK, const llvm::Triple &T,
183  std::vector<std::string> &Includes,
185 
186  /// Retrieve a module hash string that is suitable for uniquely
187  /// identifying the conditions under which the module was built.
188  std::string getModuleHash() const;
189 
190  using StringAllocator = llvm::function_ref<const char *(const llvm::Twine &)>;
191  /// Generate a cc1-compatible command line arguments from this instance.
192  ///
193  /// \param [out] Args - The generated arguments. Note that the caller is
194  /// responsible for inserting the path to the clang executable and "-cc1" if
195  /// desired.
196  /// \param SA - A function that given a Twine can allocate storage for a given
197  /// command line argument and return a pointer to the newly allocated string.
198  /// The returned pointer is what gets appended to Args.
200  StringAllocator SA) const;
201 
202  /// @}
203  /// @name Option Subgroups
204  /// @{
205 
206  MigratorOptions &getMigratorOpts() { return MigratorOpts; }
207  const MigratorOptions &getMigratorOpts() const { return MigratorOpts; }
208 
209  CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
210  const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
211 
213  return DependencyOutputOpts;
214  }
215 
217  return DependencyOutputOpts;
218  }
219 
220  FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
221 
223  return FileSystemOpts;
224  }
225 
226  FrontendOptions &getFrontendOpts() { return FrontendOpts; }
227  const FrontendOptions &getFrontendOpts() const { return FrontendOpts; }
228 
230  return PreprocessorOutputOpts;
231  }
232 
234  return PreprocessorOutputOpts;
235  }
236 
237  /// @}
238 
239 private:
240  static bool CreateFromArgsImpl(CompilerInvocation &Res,
241  ArrayRef<const char *> CommandLineArgs,
242  DiagnosticsEngine &Diags, const char *Argv0);
243 
244  /// Generate command line options from DiagnosticOptions.
245  static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
247  StringAllocator SA, bool DefaultDiagColor);
248 
249  /// Parse command line options that map to LangOptions.
250  static bool ParseLangArgs(LangOptions &Opts, llvm::opt::ArgList &Args,
251  InputKind IK, const llvm::Triple &T,
252  std::vector<std::string> &Includes,
253  DiagnosticsEngine &Diags);
254 
255  /// Generate command line options from LangOptions.
256  static void GenerateLangArgs(const LangOptions &Opts,
258  StringAllocator SA, const llvm::Triple &T);
259 
260  /// Parse command line options that map to CodeGenOptions.
261  static bool ParseCodeGenArgs(CodeGenOptions &Opts, llvm::opt::ArgList &Args,
262  InputKind IK, DiagnosticsEngine &Diags,
263  const llvm::Triple &T,
264  const std::string &OutputFile,
265  const LangOptions &LangOptsRef);
266 
267  // Generate command line options from CodeGenOptions.
268  static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
270  StringAllocator SA, const llvm::Triple &T,
271  const std::string &OutputFile,
272  const LangOptions *LangOpts);
273 };
274 
275 IntrusiveRefCntPtr<llvm::vfs::FileSystem>
276 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
277  DiagnosticsEngine &Diags);
278 
279 IntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation(
280  const CompilerInvocation &CI, DiagnosticsEngine &Diags,
281  IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
282 
283 } // namespace clang
284 
285 #endif // LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
clang::DependencyOutputOptions
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
Definition: DependencyOutputOptions.h:33
AnalyzerOptions.h
llvm
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion.
Definition: Dominators.h:30
clang::CompilerInvocationBase::getPreprocessorOpts
PreprocessorOptions & getPreprocessorOpts()
Definition: CompilerInvocation.h:111
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::PreprocessorOptions
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
Definition: PreprocessorOptions.h:64
clang::CompilerInvocationBase::AnalyzerOpts
AnalyzerOptionsRef AnalyzerOpts
Options controlling the static analyzer.
Definition: CompilerInvocation.h:82
clang::ParseDiagnosticArgs
bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args, DiagnosticsEngine *Diags=nullptr, bool DefaultDiagColor=true)
Fill out Opts based on the options given in Args.
clang::CompilerInvocationBase::getPreprocessorOptsPtr
std::shared_ptr< PreprocessorOptions > getPreprocessorOptsPtr()
Definition: CompilerInvocation.h:107
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:191
clang::CompilerInvocation::getCodeGenOpts
CodeGenOptions & getCodeGenOpts()
Definition: CompilerInvocation.h:209
clang::tooling::X
static ToolExecutorPluginRegistry::Add< AllTUsToolExecutorPlugin > X("all-TUs", "Runs FrontendActions on all TUs in the compilation database. " "Tool results are stored in memory.")
clang::InputKind
The kind of a file that we've been handed as an input.
Definition: FrontendOptions.h:144
PreprocessorOutputOptions.h
clang::CompilerInvocation::getFrontendOpts
const FrontendOptions & getFrontendOpts() const
Definition: CompilerInvocation.h:227
clang::FileSystemOptions
Keeps track of options that affect how file operations are performed.
Definition: FileSystemOptions.h:22
clang::LangStandard::Kind
Kind
Definition: LangStandard.h:64
CodeGenOptions.h
clang::CompilerInvocation::getFileSystemOpts
const FileSystemOptions & getFileSystemOpts() const
Definition: CompilerInvocation.h:222
clang::CompilerInvocation::getMigratorOpts
const MigratorOptions & getMigratorOpts() const
Definition: CompilerInvocation.h:207
clang::CompilerInvocationBase::CompilerInvocationBase
CompilerInvocationBase()
Definition: CompilerInvocation.cpp:112
clang::CompilerInvocationBase::TargetOpts
std::shared_ptr< TargetOptions > TargetOpts
Options controlling the target.
Definition: CompilerInvocation.h:70
clang::PreprocessorOutputOptions
PreprocessorOutputOptions - Options for controlling the C preprocessor output (e.g....
Definition: PreprocessorOutputOptions.h:16
FrontendOptions.h
clang::CompilerInvocation::setLangDefaults
static void setLangDefaults(LangOptions &Opts, InputKind IK, const llvm::Triple &T, std::vector< std::string > &Includes, LangStandard::Kind LangStd=LangStandard::lang_unspecified)
Set language defaults for the given input language and language standard in the given LangOptions obj...
Definition: CompilerInvocation.cpp:3059
LangOptions.h
clang::FrontendOptions
FrontendOptions - Options for controlling the behavior of the frontend.
Definition: FrontendOptions.h:227
clang::CompilerInvocationBase
Definition: CompilerInvocation.h:64
clang::CompilerInvocationBase::DiagnosticOpts
IntrusiveRefCntPtr< DiagnosticOptions > DiagnosticOpts
Options controlling the diagnostic engine.
Definition: CompilerInvocation.h:73
clang::CompilerInvocationBase::getAnalyzerOpts
AnalyzerOptionsRef getAnalyzerOpts() const
Definition: CompilerInvocation.h:117
clang::CompilerInvocationBase::getHeaderSearchOptsPtr
std::shared_ptr< HeaderSearchOptions > getHeaderSearchOptsPtr() const
Definition: CompilerInvocation.h:103
DependencyOutputOptions.h
clang::CompilerInvocationBase::HeaderSearchOpts
std::shared_ptr< HeaderSearchOptions > HeaderSearchOpts
Options controlling the #include directive.
Definition: CompilerInvocation.h:76
clang::CompilerInvocation::generateCC1CommandLine
void generateCC1CommandLine(llvm::SmallVectorImpl< const char * > &Args, StringAllocator SA) const
Generate a cc1-compatible command line arguments from this instance.
Definition: CompilerInvocation.cpp:4525
clang::CompilerInvocationBase::getPreprocessorOpts
const PreprocessorOptions & getPreprocessorOpts() const
Definition: CompilerInvocation.h:113
clang::CodeGenOptions
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Definition: CodeGenOptions.h:49
clang::CompilerInvocationBase::~CompilerInvocationBase
~CompilerInvocationBase()
clang::CompilerInvocation::getFileSystemOpts
FileSystemOptions & getFileSystemOpts()
Definition: CompilerInvocation.h:220
clang::CompilerInvocationBase::getTargetOpts
const TargetOptions & getTargetOpts() const
Definition: CompilerInvocation.h:93
clang::CompilerInvocationBase::PreprocessorOpts
std::shared_ptr< PreprocessorOptions > PreprocessorOpts
Options controlling the preprocessor (aside from #include handling).
Definition: CompilerInvocation.h:79
clang::CompilerInvocation::getModuleHash
std::string getModuleHash() const
Retrieve a module hash string that is suitable for uniquely identifying the conditions under which th...
Definition: CompilerInvocation.cpp:4412
LangStandard.h
clang::CompilerInvocationBase::getTargetOpts
TargetOptions & getTargetOpts()
Definition: CompilerInvocation.h:92
clang::CompilerInvocationBase::getLangOpts
LangOptions * getLangOpts()
Definition: CompilerInvocation.h:89
llvm::ArrayRef< const char * >
clang::MigratorOptions
Definition: MigratorOptions.h:19
LLVM.h
clang::CompilerInvocation::getCodeGenOpts
const CodeGenOptions & getCodeGenOpts() const
Definition: CompilerInvocation.h:210
clang::createVFSFromCompilerInvocation
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
Definition: CompilerInvocation.cpp:4564
clang::LangStandard::lang_unspecified
@ lang_unspecified
Definition: LangStandard.h:68
clang::CompilerInvocation::getPreprocessorOutputOpts
PreprocessorOutputOptions & getPreprocessorOutputOpts()
Definition: CompilerInvocation.h:229
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:58
clang::CompilerInvocation::getFrontendOpts
FrontendOptions & getFrontendOpts()
Definition: CompilerInvocation.h:226
clang::CompilerInvocation::getPreprocessorOutputOpts
const PreprocessorOutputOptions & getPreprocessorOutputOpts() const
Definition: CompilerInvocation.h:233
clang::CompilerInvocation::getMigratorOpts
MigratorOptions & getMigratorOpts()
Definition: CompilerInvocation.h:206
clang::CompilerInvocationBase::getHeaderSearchOpts
HeaderSearchOptions & getHeaderSearchOpts()
Definition: CompilerInvocation.h:97
MigratorOptions.h
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
clang::CompilerInvocation
Helper class for holding the data necessary to invoke the compiler.
Definition: CompilerInvocation.h:125
clang::CompilerInvocationBase::operator=
CompilerInvocationBase & operator=(const CompilerInvocationBase &)=delete
DiagnosticOptions.h
FileSystemOptions.h
clang::TargetOptions
Options for controlling the target.
Definition: TargetOptions.h:26
clang::CompilerInvocation::GetResourcesPath
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...
Definition: CompilerInvocation.cpp:2791
llvm::SmallVectorImpl
Definition: LLVM.h:39
clang::CompilerInvocation::getDependencyOutputOpts
DependencyOutputOptions & getDependencyOutputOpts()
Definition: CompilerInvocation.h:212
clang::CompilerInvocationBase::getHeaderSearchOpts
const HeaderSearchOptions & getHeaderSearchOpts() const
Definition: CompilerInvocation.h:99
clang::CompilerInvocation::StringAllocator
llvm::function_ref< const char *(const llvm::Twine &)> StringAllocator
Definition: CompilerInvocation.h:190
clang::CompilerInvocationBase::getLangOpts
const LangOptions * getLangOpts() const
Definition: CompilerInvocation.h:90
clang::CompilerInvocation::CreateFromArgs
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.
Definition: CompilerInvocation.cpp:4396
clang::CompilerInvocationBase::getDiagnosticOpts
DiagnosticOptions & getDiagnosticOpts() const
Definition: CompilerInvocation.h:95
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:47
clang::DiagnosticOptions
Options for controlling the compiler diagnostics engine.
Definition: DiagnosticOptions.h:70
clang::HeaderSearchOptions
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
Definition: HeaderSearchOptions.h:67
clang::CompilerInvocation::getDependencyOutputOpts
const DependencyOutputOptions & getDependencyOutputOpts() const
Definition: CompilerInvocation.h:216
clang::CompilerInvocationBase::LangOpts
std::shared_ptr< LangOptions > LangOpts
Options controlling the language variant.
Definition: CompilerInvocation.h:67