1//===- FrontendOptions.h ----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_CLANG_FRONTEND_FRONTENDOPTIONS_H
11#define LLVM_CLANG_FRONTEND_FRONTENDOPTIONS_H
12
13#include "clang/Frontend/CommandLineSourceLoc.h"
14#include "clang/Serialization/ModuleFileExtension.h"
15#include "clang/Sema/CodeCompleteOptions.h"
16#include "llvm/ADT/StringRef.h"
17#include <cassert>
18#include <memory>
19#include <string>
20#include <vector>
21#include <unordered_map>
22
23namespace llvm {
24
25class MemoryBuffer;
26
27} // namespace llvm
28
29namespace clang {
30
31namespace frontend {
32
33enum ActionKind {
34 /// Parse ASTs and list Decl nodes.
35 ASTDeclList,
36
37 /// Parse ASTs and dump them.
38 ASTDump,
39
40 /// Parse ASTs and print them.
41 ASTPrint,
42
43 /// Parse ASTs and view them in Graphviz.
44 ASTView,
45
46 /// Dump the compiler configuration.
47 DumpCompilerOptions,
48
49 /// Dump out raw tokens.
50 DumpRawTokens,
51
52 /// Dump out preprocessed tokens.
53 DumpTokens,
54
55 /// Emit a .s file.
56 EmitAssembly,
57
58 /// Emit a .bc file.
59 EmitBC,
60
61 /// Translate input source into HTML.
62 EmitHTML,
63
64 /// Emit a .ll file.
65 EmitLLVM,
66
67 /// Generate LLVM IR, but do not emit anything.
68 EmitLLVMOnly,
69
70 /// Generate machine code, but don't emit anything.
71 EmitCodeGenOnly,
72
73 /// Emit a .o file.
74 EmitObj,
75
76 /// Parse and apply any fixits to the source.
77 FixIt,
78
79 /// Generate pre-compiled module from a module map.
80 GenerateModule,
81
82 /// Generate pre-compiled module from a C++ module interface file.
83 GenerateModuleInterface,
84
85 /// Generate pre-compiled module from a set of header files.
86 GenerateHeaderModule,
87
88 /// Generate pre-compiled header.
89 GeneratePCH,
90
91 /// Generate pre-tokenized header.
92 GeneratePTH,
93
94 /// Only execute frontend initialization.
95 InitOnly,
96
97 /// Dump information about a module file.
98 ModuleFileInfo,
99
100 /// Load and verify that a PCH file is usable.
101 VerifyPCH,
102
103 /// Parse and perform semantic analysis.
104 ParseSyntaxOnly,
105
106 /// Run a plugin action, \see ActionName.
107 PluginAction,
108
109 /// Print the "preamble" of the input file
110 PrintPreamble,
111
112 /// -E mode.
113 PrintPreprocessedInput,
114
115 /// Expand macros but not \#includes.
116 RewriteMacros,
117
118 /// ObjC->C Rewriter.
119 RewriteObjC,
120
121 /// Rewriter playground
122 RewriteTest,
123
124 /// Run one or more source code analyses.
125 RunAnalysis,
126
127 /// Dump template instantiations
128 TemplightDump,
129
130 /// Run migrator.
131 MigrateSource,
132
133 /// Just lex, no output.
134 RunPreprocessorOnly
135};
136
137} // namespace frontend
138
139/// The kind of a file that we've been handed as an input.
140class InputKind {
141private:
142 unsigned Lang : 4;
143 unsigned Fmt : 3;
144 unsigned Preprocessed : 1;
145
146public:
147 /// The language for the input, used to select and validate the language
148 /// standard and possible actions.
149 enum Language {
150 Unknown,
151
152 /// Assembly: we accept this only so that we can preprocess it.
153 Asm,
154
155 /// LLVM IR: we accept this so that we can run the optimizer on it,
156 /// and compile it to assembly or object code.
157 LLVM_IR,
158
159 ///@{ Languages that the frontend can parse and compile.
160 C,
161 CXX,
162 ObjC,
163 ObjCXX,
164 OpenCL,
165 CUDA,
166 RenderScript,
167 HIP,
168 ///@}
169 };
170
171 /// The input file format.
172 enum Format {
173 Source,
174 ModuleMap,
175 Precompiled
176 };
177
178 constexpr InputKind(Language L = Unknown, Format F = Source,
179 bool PP = false)
180 : Lang(L), Fmt(F), Preprocessed(PP) {}
181
182 Language getLanguage() const { return static_cast<Language>(Lang); }
183 Format getFormat() const { return static_cast<Format>(Fmt); }
184 bool isPreprocessed() const { return Preprocessed; }
185
186 /// Is the input kind fully-unknown?
187 bool isUnknown() const { return Lang == Unknown && Fmt == Source; }
188
189 /// Is the language of the input some dialect of Objective-C?
190 bool isObjectiveC() const { return Lang == ObjC || Lang == ObjCXX; }
191
192 InputKind getPreprocessed() const {
193 return InputKind(getLanguage(), getFormat(), true);
194 }
195
196 InputKind withFormat(Format F) const {
197 return InputKind(getLanguage(), F, isPreprocessed());
198 }
199};
200
201/// An input file for the front end.
202class FrontendInputFile {
203 /// The file name, or "-" to read from standard input.
204 std::string File;
205
206 /// The input, if it comes from a buffer rather than a file. This object
207 /// does not own the buffer, and the caller is responsible for ensuring
208 /// that it outlives any users.
209 llvm::MemoryBuffer *Buffer = nullptr;
210
211 /// The kind of input, e.g., C source, AST file, LLVM IR.
212 InputKind Kind;
213
214 /// Whether we're dealing with a 'system' input (vs. a 'user' input).
215 bool IsSystem = false;
216
217public:
218 FrontendInputFile() = default;
219 FrontendInputFile(StringRef File, InputKind Kind, bool IsSystem = false)
220 : File(File.str()), Kind(Kind), IsSystem(IsSystem) {}
221 FrontendInputFile(llvm::MemoryBuffer *Buffer, InputKind Kind,
222 bool IsSystem = false)
223 : Buffer(Buffer), Kind(Kind), IsSystem(IsSystem) {}
224
225 InputKind getKind() const { return Kind; }
226 bool isSystem() const { return IsSystem; }
227
228 bool isEmpty() const { return File.empty() && Buffer == nullptr; }
229 bool isFile() const { return !isBuffer(); }
230 bool isBuffer() const { return Buffer != nullptr; }
231 bool isPreprocessed() const { return Kind.isPreprocessed(); }
232
233 StringRef getFile() const {
234 assert(isFile());
235 return File;
236 }
237
238 llvm::MemoryBuffer *getBuffer() const {
239 assert(isBuffer());
240 return Buffer;
241 }
242};
243
244/// FrontendOptions - Options for controlling the behavior of the frontend.
245class FrontendOptions {
246public:
247 /// Disable memory freeing on exit.
248 unsigned DisableFree : 1;
249
250 /// When generating PCH files, instruct the AST writer to create relocatable
251 /// PCH files.
252 unsigned RelocatablePCH : 1;
253
254 /// Show the -help text.
255 unsigned ShowHelp : 1;
256
257 /// Show frontend performance metrics and statistics.
258 unsigned ShowStats : 1;
259
260 /// Show timers for individual actions.
261 unsigned ShowTimers : 1;
262
263 /// Show the -version text.
264 unsigned ShowVersion : 1;
265
266 /// Apply fixes even if there are unfixable errors.
267 unsigned FixWhatYouCan : 1;
268
269 /// Apply fixes only for warnings.
270 unsigned FixOnlyWarnings : 1;
271
272 /// Apply fixes and recompile.
273 unsigned FixAndRecompile : 1;
274
275 /// Apply fixes to temporary files.
276 unsigned FixToTemporaries : 1;
277
278 /// Emit ARC errors even if the migrator can fix them.
279 unsigned ARCMTMigrateEmitARCErrors : 1;
280
281 /// Skip over function bodies to speed up parsing in cases you do not need
282 /// them (e.g. with code completion).
283 unsigned SkipFunctionBodies : 1;
284
285 /// Whether we can use the global module index if available.
286 unsigned UseGlobalModuleIndex : 1;
287
288 /// Whether we can generate the global module index if needed.
289 unsigned GenerateGlobalModuleIndex : 1;
290
291 /// Whether we include declaration dumps in AST dumps.
292 unsigned ASTDumpDecls : 1;
293
294 /// Whether we deserialize all decls when forming AST dumps.
295 unsigned ASTDumpAll : 1;
296
297 /// Whether we include lookup table dumps in AST dumps.
298 unsigned ASTDumpLookups : 1;
299
300 /// Whether we are performing an implicit module build.
301 unsigned BuildingImplicitModule : 1;
302
303 /// Whether we should embed all used files into the PCM file.
304 unsigned ModulesEmbedAllFiles : 1;
305
306 /// Whether timestamps should be written to the produced PCH file.
307 unsigned IncludeTimestamps : 1;
308
309 CodeCompleteOptions CodeCompleteOpts;
310
311 enum {
312 ARCMT_None,
313 ARCMT_Check,
314 ARCMT_Modify,
315 ARCMT_Migrate
316 } ARCMTAction = ARCMT_None;
317
318 enum {
319 ObjCMT_None = 0,
320
321 /// Enable migration to modern ObjC literals.
322 ObjCMT_Literals = 0x1,
323
324 /// Enable migration to modern ObjC subscripting.
325 ObjCMT_Subscripting = 0x2,
326
327 /// Enable migration to modern ObjC readonly property.
328 ObjCMT_ReadonlyProperty = 0x4,
329
330 /// Enable migration to modern ObjC readwrite property.
331 ObjCMT_ReadwriteProperty = 0x8,
332
333 /// Enable migration to modern ObjC property.
334 ObjCMT_Property = (ObjCMT_ReadonlyProperty | ObjCMT_ReadwriteProperty),
335
336 /// Enable annotation of ObjCMethods of all kinds.
337 ObjCMT_Annotation = 0x10,
338
339 /// Enable migration of ObjC methods to 'instancetype'.
340 ObjCMT_Instancetype = 0x20,
341
342 /// Enable migration to NS_ENUM/NS_OPTIONS macros.
343 ObjCMT_NsMacros = 0x40,
344
345 /// Enable migration to add conforming protocols.
346 ObjCMT_ProtocolConformance = 0x80,
347
348 /// prefer 'atomic' property over 'nonatomic'.
349 ObjCMT_AtomicProperty = 0x100,
350
351 /// annotate property with NS_RETURNS_INNER_POINTER
352 ObjCMT_ReturnsInnerPointerProperty = 0x200,
353
354 /// use NS_NONATOMIC_IOSONLY for property 'atomic' attribute
355 ObjCMT_NsAtomicIOSOnlyProperty = 0x400,
356
357 /// Enable inferring NS_DESIGNATED_INITIALIZER for ObjC methods.
358 ObjCMT_DesignatedInitializer = 0x800,
359
360 /// Enable converting setter/getter expressions to property-dot syntx.
361 ObjCMT_PropertyDotSyntax = 0x1000,
362
363 ObjCMT_MigrateDecls = (ObjCMT_ReadonlyProperty | ObjCMT_ReadwriteProperty |
364 ObjCMT_Annotation | ObjCMT_Instancetype |
365 ObjCMT_NsMacros | ObjCMT_ProtocolConformance |
366 ObjCMT_NsAtomicIOSOnlyProperty |
367 ObjCMT_DesignatedInitializer),
368 ObjCMT_MigrateAll = (ObjCMT_Literals | ObjCMT_Subscripting |
369 ObjCMT_MigrateDecls | ObjCMT_PropertyDotSyntax)
370 };
371 unsigned ObjCMTAction = ObjCMT_None;
372 std::string ObjCMTWhiteListPath;
373
374 std::string MTMigrateDir;
375 std::string ARCMTMigrateReportOut;
376
377 /// The input files and their types.
378 std::vector<FrontendInputFile> Inputs;
379
380 /// When the input is a module map, the original module map file from which
381 /// that map was inferred, if any (for umbrella modules).
382 std::string OriginalModuleMap;
383
384 /// The output file, if any.
385 std::string OutputFile;
386
387 /// If given, the new suffix for fix-it rewritten files.
388 std::string FixItSuffix;
389
390 /// If given, filter dumped AST Decl nodes by this substring.
391 std::string ASTDumpFilter;
392
393 /// If given, enable code completion at the provided location.
394 ParsedSourceLocation CodeCompletionAt;
395
396 /// The frontend action to perform.
397 frontend::ActionKind ProgramAction = frontend::ParseSyntaxOnly;
398
399 /// The name of the action to run when using a plugin action.
400 std::string ActionName;
401
402 /// Args to pass to the plugins
403 std::unordered_map<std::string,std::vector<std::string>> PluginArgs;
404
405 /// The list of plugin actions to run in addition to the normal action.
406 std::vector<std::string> AddPluginActions;
407
408 /// The list of plugins to load.
409 std::vector<std::string> Plugins;
410
411 /// The list of module file extensions.
412 std::vector<std::shared_ptr<ModuleFileExtension>> ModuleFileExtensions;
413
414 /// The list of module map files to load before processing the input.
415 std::vector<std::string> ModuleMapFiles;
416
417 /// The list of additional prebuilt module files to load before
418 /// processing the input.
419 std::vector<std::string> ModuleFiles;
420
421 /// The list of files to embed into the compiled module file.
422 std::vector<std::string> ModulesEmbedFiles;
423
424 /// The list of AST files to merge.
425 std::vector<std::string> ASTMergeFiles;
426
427 /// A list of arguments to forward to LLVM's option processing; this
428 /// should only be used for debugging and experimental features.
429 std::vector<std::string> LLVMArgs;
430
431 /// File name of the file that will provide record layouts
432 /// (in the format produced by -fdump-record-layouts).
433 std::string OverrideRecordLayoutsFile;
434
435 /// Auxiliary triple for CUDA compilation.
436 std::string AuxTriple;
437
438 /// Filename to write statistics to.
439 std::string StatsFile;
440
441public:
442 FrontendOptions()
443 : DisableFree(false), RelocatablePCH(false), ShowHelp(false),
444 ShowStats(false), ShowTimers(false), ShowVersion(false),
445 FixWhatYouCan(false), FixOnlyWarnings(false), FixAndRecompile(false),
446 FixToTemporaries(false), ARCMTMigrateEmitARCErrors(false),
447 SkipFunctionBodies(false), UseGlobalModuleIndex(true),
448 GenerateGlobalModuleIndex(true), ASTDumpDecls(false),
449 ASTDumpLookups(false), BuildingImplicitModule(false),
450 ModulesEmbedAllFiles(false), IncludeTimestamps(true) {}
451
452 /// getInputKindForExtension - Return the appropriate input kind for a file
453 /// extension. For example, "c" would return InputKind::C.
454 ///
455 /// \return The input kind for the extension, or InputKind::Unknown if the
456 /// extension is not recognized.
457 static InputKind getInputKindForExtension(StringRef Extension);
458};
459
460} // namespace clang
461
462#endif // LLVM_CLANG_FRONTEND_FRONTENDOPTIONS_H
463