clang  14.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 // This lets us create the DiagnosticsEngine with a properly-filled-out
54 // DiagnosticOptions instance.
55 std::unique_ptr<DiagnosticOptions>
56 CreateAndPopulateDiagOpts(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).
65 bool 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.
78 public:
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 
107  TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
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.
137 protected:
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 
155 public:
157  const MigratorOptions &getMigratorOpts() const { return MigratorOpts; }
158 
160  const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
161 
163  return DependencyOutputOpts;
164  }
165 
167  return DependencyOutputOpts;
168  }
169 
171 
173  return FileSystemOpts;
174  }
175 
177  const FrontendOptions &getFrontendOpts() const { return FrontendOpts; }
178 
180  return PreprocessorOutputOpts;
181  }
182 
184  return PreprocessorOutputOpts;
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.
195 public:
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  /// Set language defaults for the given input language and
223  /// language standard in the given LangOptions object.
224  ///
225  /// \param Opts - The LangOptions object to set up.
226  /// \param IK - The input language.
227  /// \param T - The target triple.
228  /// \param Includes - The affected list of included files.
229  /// \param LangStd - The input language standard.
230  static void
231  setLangDefaults(LangOptions &Opts, InputKind IK, const llvm::Triple &T,
232  std::vector<std::string> &Includes,
234 
235  /// Retrieve a module hash string that is suitable for uniquely
236  /// identifying the conditions under which the module was built.
237  std::string getModuleHash() const;
238 
239  using StringAllocator = llvm::function_ref<const char *(const llvm::Twine &)>;
240  /// Generate a cc1-compatible command line arguments from this instance.
241  ///
242  /// \param [out] Args - The generated arguments. Note that the caller is
243  /// responsible for inserting the path to the clang executable and "-cc1" if
244  /// desired.
245  /// \param SA - A function that given a Twine can allocate storage for a given
246  /// command line argument and return a pointer to the newly allocated string.
247  /// The returned pointer is what gets appended to Args.
249  StringAllocator SA) const;
250 
251 private:
252  static bool CreateFromArgsImpl(CompilerInvocation &Res,
253  ArrayRef<const char *> CommandLineArgs,
254  DiagnosticsEngine &Diags, const char *Argv0);
255 
256  /// Generate command line options from DiagnosticOptions.
257  static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
259  StringAllocator SA, bool DefaultDiagColor);
260 
261  /// Parse command line options that map to LangOptions.
262  static bool ParseLangArgs(LangOptions &Opts, llvm::opt::ArgList &Args,
263  InputKind IK, const llvm::Triple &T,
264  std::vector<std::string> &Includes,
265  DiagnosticsEngine &Diags);
266 
267  /// Generate command line options from LangOptions.
268  static void GenerateLangArgs(const LangOptions &Opts,
270  StringAllocator SA, const llvm::Triple &T,
271  InputKind IK);
272 
273  /// Parse command line options that map to CodeGenOptions.
274  static bool ParseCodeGenArgs(CodeGenOptions &Opts, llvm::opt::ArgList &Args,
275  InputKind IK, DiagnosticsEngine &Diags,
276  const llvm::Triple &T,
277  const std::string &OutputFile,
278  const LangOptions &LangOptsRef);
279 
280  // Generate command line options from CodeGenOptions.
281  static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
283  StringAllocator SA, const llvm::Triple &T,
284  const std::string &OutputFile,
285  const LangOptions *LangOpts);
286 };
287 
290  DiagnosticsEngine &Diags);
291 
293  const CompilerInvocation &CI, DiagnosticsEngine &Diags,
295 
296 } // namespace clang
297 
298 #endif // LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
clang::CompilerInvocationValueBase::FileSystemOpts
FileSystemOptions FileSystemOpts
Options controlling file system operations.
Definition: CompilerInvocation.h:147
clang::DependencyOutputOptions
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
Definition: DependencyOutputOptions.h:33
AnalyzerOptions.h
clang::CompilerInvocationRefBase::getTargetOpts
TargetOptions & getTargetOpts()
Definition: CompilerInvocation.h:107
llvm
Definition: Dominators.h:30
clang::CompilerInvocationValueBase::getCodeGenOpts
CodeGenOptions & getCodeGenOpts()
Definition: CompilerInvocation.h:159
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::CompilerInvocationRefBase::TargetOpts
std::shared_ptr< TargetOptions > TargetOpts
Options controlling the target.
Definition: CompilerInvocation.h:83
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::CompilerInvocationRefBase::CompilerInvocationRefBase
CompilerInvocationRefBase()
Definition: CompilerInvocation.cpp:113
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:191
clang::CompilerInvocationRefBase::getHeaderSearchOpts
HeaderSearchOptions & getHeaderSearchOpts()
Definition: CompilerInvocation.h:112
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::CompilerInvocationValueBase::getFileSystemOpts
const FileSystemOptions & getFileSystemOpts() const
Definition: CompilerInvocation.h:172
clang::InputKind
The kind of a file that we've been handed as an input.
Definition: FrontendOptions.h:145
clang::CompilerInvocationRefBase::LangOpts
std::shared_ptr< LangOptions > LangOpts
Options controlling the language variant.
Definition: CompilerInvocation.h:80
PreprocessorOutputOptions.h
clang::CompilerInvocationValueBase::CodeGenOpts
CodeGenOptions CodeGenOpts
Options controlling IRgen and the backend.
Definition: CompilerInvocation.h:141
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::CompilerInvocationRefBase::getPreprocessorOptsPtr
std::shared_ptr< PreprocessorOptions > getPreprocessorOptsPtr()
Definition: CompilerInvocation.h:122
clang::CompilerInvocationRefBase
The base class of CompilerInvocation with reference semantics.
Definition: CompilerInvocation.h:77
clang::CompilerInvocationValueBase::getPreprocessorOutputOpts
const PreprocessorOutputOptions & getPreprocessorOutputOpts() const
Definition: CompilerInvocation.h:183
clang::CompilerInvocationRefBase::PreprocessorOpts
std::shared_ptr< PreprocessorOptions > PreprocessorOpts
Options controlling the preprocessor (aside from #include handling).
Definition: CompilerInvocation.h:92
clang::CompilerInvocationValueBase::getFrontendOpts
FrontendOptions & getFrontendOpts()
Definition: CompilerInvocation.h:176
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:3100
clang::CompilerInvocationRefBase::HeaderSearchOpts
std::shared_ptr< HeaderSearchOptions > HeaderSearchOpts
Options controlling the #include directive.
Definition: CompilerInvocation.h:89
LangOptions.h
clang::FrontendOptions
FrontendOptions - Options for controlling the behavior of the frontend.
Definition: FrontendOptions.h:228
clang::CompilerInvocationValueBase::DependencyOutputOpts
DependencyOutputOptions DependencyOutputOpts
Options controlling dependency output.
Definition: CompilerInvocation.h:144
clang::CompilerInvocationRefBase::AnalyzerOpts
AnalyzerOptionsRef AnalyzerOpts
Options controlling the static analyzer.
Definition: CompilerInvocation.h:95
DependencyOutputOptions.h
clang::CompilerInvocationRefBase::getHeaderSearchOptsPtr
std::shared_ptr< HeaderSearchOptions > getHeaderSearchOptsPtr() const
Definition: CompilerInvocation.h:118
clang::CompilerInvocationRefBase::getLangOpts
LangOptions * getLangOpts()
Definition: CompilerInvocation.h:104
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:4618
clang::CompilerInvocationRefBase::~CompilerInvocationRefBase
~CompilerInvocationRefBase()
clang::CompilerInvocationRefBase::getAnalyzerOpts
AnalyzerOptionsRef getAnalyzerOpts() const
Definition: CompilerInvocation.h:132
clang::CodeGenOptions
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Definition: CodeGenOptions.h:49
clang::CompilerInvocationRefBase::getPreprocessorOpts
PreprocessorOptions & getPreprocessorOpts()
Definition: CompilerInvocation.h:126
clang::CompilerInvocationValueBase::PreprocessorOutputOpts
PreprocessorOutputOptions PreprocessorOutputOpts
Options controlling preprocessed output.
Definition: CompilerInvocation.h:153
clang::CompilerInvocationRefBase::getTargetOpts
const TargetOptions & getTargetOpts() const
Definition: CompilerInvocation.h:108
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:4522
LangStandard.h
clang::CompilerInvocationValueBase::getDependencyOutputOpts
DependencyOutputOptions & getDependencyOutputOpts()
Definition: CompilerInvocation.h:162
llvm::ArrayRef< const char * >
clang::MigratorOptions
Definition: MigratorOptions.h:19
LLVM.h
clang::createVFSFromCompilerInvocation
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
Definition: CompilerInvocation.cpp:4640
clang::LangStandard::lang_unspecified
@ lang_unspecified
Definition: LangStandard.h:68
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::CompilerInvocationValueBase::getCodeGenOpts
const CodeGenOptions & getCodeGenOpts() const
Definition: CompilerInvocation.h:160
MigratorOptions.h
clang::CompilerInvocationValueBase::FrontendOpts
FrontendOptions FrontendOpts
Options controlling the frontend itself.
Definition: CompilerInvocation.h:150
clang::CompilerInvocationValueBase::getFrontendOpts
const FrontendOptions & getFrontendOpts() const
Definition: CompilerInvocation.h:177
clang
Definition: CalledOnceCheck.h:17
clang::CompilerInvocationRefBase::getHeaderSearchOpts
const HeaderSearchOptions & getHeaderSearchOpts() const
Definition: CompilerInvocation.h:114
clang::CompilerInvocationValueBase
The base class of CompilerInvocation with value semantics.
Definition: CompilerInvocation.h:136
clang::CompilerInvocationValueBase::getDependencyOutputOpts
const DependencyOutputOptions & getDependencyOutputOpts() const
Definition: CompilerInvocation.h:166
clang::CompilerInvocation
Helper class for holding the data necessary to invoke the compiler.
Definition: CompilerInvocation.h:193
clang::CompilerInvocationRefBase::DiagnosticOpts
IntrusiveRefCntPtr< DiagnosticOptions > DiagnosticOpts
Options controlling the diagnostic engine.
Definition: CompilerInvocation.h:86
clang::CreateAndPopulateDiagOpts
std::unique_ptr< DiagnosticOptions > CreateAndPopulateDiagOpts(ArrayRef< const char * > Argv)
Definition: CompilerInvocation.cpp:2279
DiagnosticOptions.h
FileSystemOptions.h
clang::CompilerInvocationRefBase::getLangOpts
const LangOptions * getLangOpts() const
Definition: CompilerInvocation.h:105
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:2832
llvm::SmallVectorImpl
Definition: LLVM.h:39
clang::CompilerInvocationValueBase::getMigratorOpts
const MigratorOptions & getMigratorOpts() const
Definition: CompilerInvocation.h:157
clang::CompilerInvocationValueBase::MigratorOpts
MigratorOptions MigratorOpts
Definition: CompilerInvocation.h:138
clang::CompilerInvocation::StringAllocator
llvm::function_ref< const char *(const llvm::Twine &)> StringAllocator
Definition: CompilerInvocation.h:239
clang::CompilerInvocationValueBase::getFileSystemOpts
FileSystemOptions & getFileSystemOpts()
Definition: CompilerInvocation.h:170
clang::CompilerInvocationRefBase::operator=
CompilerInvocationRefBase & operator=(CompilerInvocationRefBase X)
Definition: CompilerInvocation.cpp:133
clang::CompilerInvocationRefBase::getPreprocessorOpts
const PreprocessorOptions & getPreprocessorOpts() const
Definition: CompilerInvocation.h:128
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:4506
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:68
clang::CompilerInvocationValueBase::getPreprocessorOutputOpts
PreprocessorOutputOptions & getPreprocessorOutputOpts()
Definition: CompilerInvocation.h:179
clang::CompilerInvocationRefBase::getDiagnosticOpts
DiagnosticOptions & getDiagnosticOpts() const
Definition: CompilerInvocation.h:110
clang::CompilerInvocationValueBase::getMigratorOpts
MigratorOptions & getMigratorOpts()
Definition: CompilerInvocation.h:156