/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Frontend/CompilerInvocation.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
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. |
77 | | class CompilerInvocationRefBase { |
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. |
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 | 1.74M | LangOptions *getLangOpts() { return LangOpts.get(); } |
105 | 12.9k | const LangOptions *getLangOpts() const { return LangOpts.get(); } |
106 | | |
107 | 654k | TargetOptions &getTargetOpts() { return *TargetOpts.get(); } |
108 | 12.9k | const TargetOptions &getTargetOpts() const { return *TargetOpts.get(); } |
109 | | |
110 | 464k | DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; } |
111 | | |
112 | 657k | HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; } |
113 | | |
114 | 70.7k | const HeaderSearchOptions &getHeaderSearchOpts() const { |
115 | 70.7k | return *HeaderSearchOpts; |
116 | 70.7k | } |
117 | | |
118 | 90.8k | std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const { |
119 | 90.8k | return HeaderSearchOpts; |
120 | 90.8k | } |
121 | | |
122 | 90.8k | std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() { |
123 | 90.8k | return PreprocessorOpts; |
124 | 90.8k | } |
125 | | |
126 | 732k | PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; } |
127 | | |
128 | 42.9k | const PreprocessorOptions &getPreprocessorOpts() const { |
129 | 42.9k | return *PreprocessorOpts; |
130 | 42.9k | } |
131 | | |
132 | 224k | AnalyzerOptionsRef getAnalyzerOpts() const { return AnalyzerOpts; } |
133 | | }; |
134 | | |
135 | | /// The base class of CompilerInvocation with value semantics. |
136 | | class CompilerInvocationValueBase { |
137 | | protected: |
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 | | |
155 | | public: |
156 | 144k | MigratorOptions &getMigratorOpts() { return MigratorOpts; } |
157 | 0 | const MigratorOptions &getMigratorOpts() const { return MigratorOpts; } |
158 | | |
159 | 1.30M | CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; } |
160 | 16.5k | const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } |
161 | | |
162 | 388k | DependencyOutputOptions &getDependencyOutputOpts() { |
163 | 388k | return DependencyOutputOpts; |
164 | 388k | } |
165 | | |
166 | 0 | const DependencyOutputOptions &getDependencyOutputOpts() const { |
167 | 0 | return DependencyOutputOpts; |
168 | 0 | } |
169 | | |
170 | 373k | FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; } |
171 | | |
172 | 0 | const FileSystemOptions &getFileSystemOpts() const { |
173 | 0 | return FileSystemOpts; |
174 | 0 | } |
175 | | |
176 | 3.44M | FrontendOptions &getFrontendOpts() { return FrontendOpts; } |
177 | 14.7k | const FrontendOptions &getFrontendOpts() const { return FrontendOpts; } |
178 | | |
179 | 386k | PreprocessorOutputOptions &getPreprocessorOutputOpts() { |
180 | 386k | return PreprocessorOutputOpts; |
181 | 386k | } |
182 | | |
183 | 0 | const PreprocessorOutputOptions &getPreprocessorOutputOpts() const { |
184 | 0 | return PreprocessorOutputOpts; |
185 | 0 | } |
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. |
193 | | class CompilerInvocation : public CompilerInvocationRefBase, |
194 | | public CompilerInvocationValueBase { |
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 | | /// 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 | | |
238 | | private: |
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 | | |
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 |