Warning: This file is not a C or C++ file. It does not have highlighting.

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
12#include "clang/Basic/CodeGenOptions.h"
13#include "clang/Basic/DiagnosticOptions.h"
14#include "clang/Basic/FileSystemOptions.h"
15#include "clang/Basic/LLVM.h"
16#include "clang/Basic/LangOptions.h"
17#include "clang/Basic/LangStandard.h"
18#include "clang/Frontend/DependencyOutputOptions.h"
19#include "clang/Frontend/FrontendOptions.h"
20#include "clang/Frontend/MigratorOptions.h"
21#include "clang/Frontend/PreprocessorOutputOptions.h"
22#include "clang/StaticAnalyzer/Core/AnalyzerOptions.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.
77class CompilerInvocationRefBase {
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.
86 IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
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.
95 AnalyzerOptionsRef AnalyzerOpts;
96
97 CompilerInvocationRefBase();
98 CompilerInvocationRefBase(const CompilerInvocationRefBase &X);
99 CompilerInvocationRefBase(CompilerInvocationRefBase &&X);
100 CompilerInvocationRefBase &operator=(CompilerInvocationRefBase X);
101 CompilerInvocationRefBase &operator=(CompilerInvocationRefBase &&X);
102 ~CompilerInvocationRefBase();
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
110 DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
111
112 HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
113
114 const HeaderSearchOptions &getHeaderSearchOpts() const {
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
126 PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
127
128 const PreprocessorOptions &getPreprocessorOpts() const {
129 return *PreprocessorOpts;
130 }
131
132 AnalyzerOptionsRef getAnalyzerOpts() const { return AnalyzerOpts; }
133};
134
135/// The base class of CompilerInvocation with value semantics.
136class CompilerInvocationValueBase {
137protected:
138 MigratorOptions MigratorOpts;
139
140 /// Options controlling IRgen and the backend.
141 CodeGenOptions CodeGenOpts;
142
143 /// Options controlling dependency output.
144 DependencyOutputOptions DependencyOutputOpts;
145
146 /// Options controlling file system operations.
147 FileSystemOptions FileSystemOpts;
148
149 /// Options controlling the frontend itself.
150 FrontendOptions FrontendOpts;
151
152 /// Options controlling preprocessed output.
153 PreprocessorOutputOptions PreprocessorOutputOpts;
154
155public:
156 MigratorOptions &getMigratorOpts() { return MigratorOpts; }
157 const MigratorOptions &getMigratorOpts() const { return MigratorOpts; }
158
159 CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
160 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
161
162 DependencyOutputOptions &getDependencyOutputOpts() {
163 return DependencyOutputOpts;
164 }
165
166 const DependencyOutputOptions &getDependencyOutputOpts() const {
167 return DependencyOutputOpts;
168 }
169
170 FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
171
172 const FileSystemOptions &getFileSystemOpts() const {
173 return FileSystemOpts;
174 }
175
176 FrontendOptions &getFrontendOpts() { return FrontendOpts; }
177 const FrontendOptions &getFrontendOpts() const { return FrontendOpts; }
178
179 PreprocessorOutputOptions &getPreprocessorOutputOpts() {
180 return PreprocessorOutputOpts;
181 }
182
183 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
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.
193class CompilerInvocation : public CompilerInvocationRefBase,
194 public CompilerInvocationValueBase {
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 a 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.
235 void generateCC1CommandLine(llvm::SmallVectorImpl<const char *> &Args,
236 StringAllocator SA) const;
237
238private:
239 static bool CreateFromArgsImpl(CompilerInvocation &Res,
240 ArrayRef<const char *> CommandLineArgs,
241 DiagnosticsEngine &Diags, const char *Argv0);
242
243 /// Generate command line options from DiagnosticOptions.
244 static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
245 SmallVectorImpl<const char *> &Args,
246 StringAllocator SA, bool DefaultDiagColor);
247
248 /// Parse command line options that map to LangOptions.
249 static bool ParseLangArgs(LangOptions &Opts, llvm::opt::ArgList &Args,
250 InputKind IK, const llvm::Triple &T,
251 std::vector<std::string> &Includes,
252 DiagnosticsEngine &Diags);
253
254 /// Generate command line options from LangOptions.
255 static void GenerateLangArgs(const LangOptions &Opts,
256 SmallVectorImpl<const char *> &Args,
257 StringAllocator SA, const llvm::Triple &T,
258 InputKind IK);
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,
269 SmallVectorImpl<const char *> &Args,
270 StringAllocator SA, const llvm::Triple &T,
271 const std::string &OutputFile,
272 const LangOptions *LangOpts);
273};
274
275IntrusiveRefCntPtr<llvm::vfs::FileSystem>
276createVFSFromCompilerInvocation(const CompilerInvocation &CI,
277 DiagnosticsEngine &Diags);
278
279IntrusiveRefCntPtr<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
286

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of clang/include/clang/Frontend/CompilerInvocation.h