Clang Project

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