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/Sema/CodeCompleteOptions.h"
15 #include "llvm/ADT/StringRef.h"
16 #include <string>
17 #include <vector>
18 
19 namespace llvm {
20 class MemoryBuffer;
21 }
22 
23 namespace clang {
24 
25 namespace frontend {
26   enum ActionKind {
27     ASTDeclList,            ///< Parse ASTs and list Decl nodes.
28     ASTDump,                ///< Parse ASTs and dump them.
29     ASTPrint,               ///< Parse ASTs and print them.
30     ASTView,                ///< Parse ASTs and view them in Graphviz.
31     DumpRawTokens,          ///< Dump out raw tokens.
32     DumpTokens,             ///< Dump out preprocessed tokens.
33     EmitAssembly,           ///< Emit a .s file.
34     EmitBC,                 ///< Emit a .bc file.
35     EmitHTML,               ///< Translate input source into HTML.
36     EmitLLVM,               ///< Emit a .ll file.
37     EmitLLVMOnly,           ///< Generate LLVM IR, but do not emit anything.
38     EmitCodeGenOnly,        ///< Generate machine code, but don't emit anything.
39     EmitObj,                ///< Emit a .o file.
40     FixIt,                  ///< Parse and apply any fixits to the source.
41     GenerateModule,         ///< Generate pre-compiled module.
42     GeneratePCH,            ///< Generate pre-compiled header.
43     GeneratePTH,            ///< Generate pre-tokenized header.
44     InitOnly,               ///< Only execute frontend initialization.
45     ModuleFileInfo,         ///< Dump information about a module file.
46     VerifyPCH,              ///< Load and verify that a PCH file is usable.
47     ParseSyntaxOnly,        ///< Parse and perform semantic analysis.
48     PluginAction,           ///< Run a plugin action, \see ActionName.
49     PrintDeclContext,       ///< Print DeclContext and their Decls.
50     PrintPreamble,          ///< Print the "preamble" of the input file
51     PrintPreprocessedInput, ///< -E mode.
52     RewriteMacros,          ///< Expand macros but not \#includes.
53     RewriteObjC,            ///< ObjC->C Rewriter.
54     RewriteTest,            ///< Rewriter playground
55     RunAnalysis,            ///< Run one or more source code analyses.
56     MigrateSource,          ///< Run migrator.
57     RunPreprocessorOnly     ///< Just lex, no output.
58   };
59 }
60 
61 enum InputKind {
62   IK_None,
63   IK_Asm,
64   IK_C,
65   IK_CXX,
66   IK_ObjC,
67   IK_ObjCXX,
68   IK_PreprocessedC,
69   IK_PreprocessedCXX,
70   IK_PreprocessedObjC,
71   IK_PreprocessedObjCXX,
72   IK_OpenCL,
73   IK_CUDA,
74   IK_PreprocessedCuda,
75   IK_AST,
76   IK_LLVM_IR
77 };
78 
79 
80 /// \brief An input file for the front end.
81 class FrontendInputFile {
82   /// \brief The file name, or "-" to read from standard input.
83   std::string File;
84 
85   llvm::MemoryBuffer *Buffer;
86 
87   /// \brief The kind of input, e.g., C source, AST file, LLVM IR.
88   InputKind Kind;
89 
90   /// \brief Whether we're dealing with a 'system' input (vs. a 'user' input).
91   bool IsSystem;
92 
93 public:
FrontendInputFile()94   FrontendInputFile() : Buffer(nullptr), Kind(IK_None) { }
95   FrontendInputFile(StringRef File, InputKind Kind, bool IsSystem = false)
96     : File(File.str()), Buffer(nullptr), Kind(Kind), IsSystem(IsSystem) { }
97   FrontendInputFile(llvm::MemoryBuffer *buffer, InputKind Kind,
98                     bool IsSystem = false)
Buffer(buffer)99     : Buffer(buffer), Kind(Kind), IsSystem(IsSystem) { }
100 
getKind()101   InputKind getKind() const { return Kind; }
isSystem()102   bool isSystem() const { return IsSystem; }
103 
isEmpty()104   bool isEmpty() const { return File.empty() && Buffer == nullptr; }
isFile()105   bool isFile() const { return !isBuffer(); }
isBuffer()106   bool isBuffer() const { return Buffer != nullptr; }
107 
getFile()108   StringRef getFile() const {
109     assert(isFile());
110     return File;
111   }
getBuffer()112   llvm::MemoryBuffer *getBuffer() const {
113     assert(isBuffer());
114     return Buffer;
115   }
116 };
117 
118 /// FrontendOptions - Options for controlling the behavior of the frontend.
119 class FrontendOptions {
120 public:
121   unsigned DisableFree : 1;                ///< Disable memory freeing on exit.
122   unsigned RelocatablePCH : 1;             ///< When generating PCH files,
123                                            /// instruct the AST writer to create
124                                            /// relocatable PCH files.
125   unsigned ShowHelp : 1;                   ///< Show the -help text.
126   unsigned ShowStats : 1;                  ///< Show frontend performance
127                                            /// metrics and statistics.
128   unsigned ShowTimers : 1;                 ///< Show timers for individual
129                                            /// actions.
130   unsigned ShowVersion : 1;                ///< Show the -version text.
131   unsigned FixWhatYouCan : 1;              ///< Apply fixes even if there are
132                                            /// unfixable errors.
133   unsigned FixOnlyWarnings : 1;            ///< Apply fixes only for warnings.
134   unsigned FixAndRecompile : 1;            ///< Apply fixes and recompile.
135   unsigned FixToTemporaries : 1;           ///< Apply fixes to temporary files.
136   unsigned ARCMTMigrateEmitARCErrors : 1;  /// Emit ARC errors even if the
137                                            /// migrator can fix them
138   unsigned SkipFunctionBodies : 1;         ///< Skip over function bodies to
139                                            /// speed up parsing in cases you do
140                                            /// not need them (e.g. with code
141                                            /// completion).
142   unsigned UseGlobalModuleIndex : 1;       ///< Whether we can use the
143                                            ///< global module index if available.
144   unsigned GenerateGlobalModuleIndex : 1;  ///< Whether we can generate the
145                                            ///< global module index if needed.
146   unsigned ASTDumpDecls : 1;               ///< Whether we include declaration
147                                            ///< dumps in AST dumps.
148   unsigned ASTDumpLookups : 1;             ///< Whether we include lookup table
149                                            ///< dumps in AST dumps.
150 
151   CodeCompleteOptions CodeCompleteOpts;
152 
153   enum {
154     ARCMT_None,
155     ARCMT_Check,
156     ARCMT_Modify,
157     ARCMT_Migrate
158   } ARCMTAction;
159 
160   enum {
161     ObjCMT_None = 0,
162     /// \brief Enable migration to modern ObjC literals.
163     ObjCMT_Literals = 0x1,
164     /// \brief Enable migration to modern ObjC subscripting.
165     ObjCMT_Subscripting = 0x2,
166     /// \brief Enable migration to modern ObjC readonly property.
167     ObjCMT_ReadonlyProperty = 0x4,
168     /// \brief Enable migration to modern ObjC readwrite property.
169     ObjCMT_ReadwriteProperty = 0x8,
170     /// \brief Enable migration to modern ObjC property.
171     ObjCMT_Property = (ObjCMT_ReadonlyProperty | ObjCMT_ReadwriteProperty),
172     /// \brief Enable annotation of ObjCMethods of all kinds.
173     ObjCMT_Annotation = 0x10,
174     /// \brief Enable migration of ObjC methods to 'instancetype'.
175     ObjCMT_Instancetype = 0x20,
176     /// \brief Enable migration to NS_ENUM/NS_OPTIONS macros.
177     ObjCMT_NsMacros = 0x40,
178     /// \brief Enable migration to add conforming protocols.
179     ObjCMT_ProtocolConformance = 0x80,
180     /// \brief prefer 'atomic' property over 'nonatomic'.
181     ObjCMT_AtomicProperty = 0x100,
182     /// \brief annotate property with NS_RETURNS_INNER_POINTER
183     ObjCMT_ReturnsInnerPointerProperty = 0x200,
184     /// \brief use NS_NONATOMIC_IOSONLY for property 'atomic' attribute
185     ObjCMT_NsAtomicIOSOnlyProperty = 0x400,
186     /// \brief Enable inferring NS_DESIGNATED_INITIALIZER for ObjC methods.
187     ObjCMT_DesignatedInitializer = 0x800,
188     /// \brief Enable converting setter/getter expressions to property-dot syntx.
189     ObjCMT_PropertyDotSyntax = 0x1000,
190     ObjCMT_MigrateDecls = (ObjCMT_ReadonlyProperty | ObjCMT_ReadwriteProperty |
191                            ObjCMT_Annotation | ObjCMT_Instancetype |
192                            ObjCMT_NsMacros | ObjCMT_ProtocolConformance |
193                            ObjCMT_NsAtomicIOSOnlyProperty |
194                            ObjCMT_DesignatedInitializer),
195     ObjCMT_MigrateAll = (ObjCMT_Literals | ObjCMT_Subscripting |
196                          ObjCMT_MigrateDecls | ObjCMT_PropertyDotSyntax)
197   };
198   unsigned ObjCMTAction;
199   std::string ObjCMTWhiteListPath;
200 
201   std::string MTMigrateDir;
202   std::string ARCMTMigrateReportOut;
203 
204   /// The input files and their types.
205   std::vector<FrontendInputFile> Inputs;
206 
207   /// The output file, if any.
208   std::string OutputFile;
209 
210   /// If given, the new suffix for fix-it rewritten files.
211   std::string FixItSuffix;
212 
213   /// If given, filter dumped AST Decl nodes by this substring.
214   std::string ASTDumpFilter;
215 
216   /// If given, enable code completion at the provided location.
217   ParsedSourceLocation CodeCompletionAt;
218 
219   /// The frontend action to perform.
220   frontend::ActionKind ProgramAction;
221 
222   /// The name of the action to run when using a plugin action.
223   std::string ActionName;
224 
225   /// Args to pass to the plugin
226   std::vector<std::string> PluginArgs;
227 
228   /// The list of plugin actions to run in addition to the normal action.
229   std::vector<std::string> AddPluginActions;
230 
231   /// Args to pass to the additional plugins
232   std::vector<std::vector<std::string> > AddPluginArgs;
233 
234   /// The list of plugins to load.
235   std::vector<std::string> Plugins;
236 
237   /// \brief The list of module map files to load before processing the input.
238   std::vector<std::string> ModuleMapFiles;
239 
240   /// \brief The list of additional prebuilt module files to load before
241   /// processing the input.
242   std::vector<std::string> ModuleFiles;
243 
244   /// \brief The list of AST files to merge.
245   std::vector<std::string> ASTMergeFiles;
246 
247   /// \brief A list of arguments to forward to LLVM's option processing; this
248   /// should only be used for debugging and experimental features.
249   std::vector<std::string> LLVMArgs;
250 
251   /// \brief File name of the file that will provide record layouts
252   /// (in the format produced by -fdump-record-layouts).
253   std::string OverrideRecordLayoutsFile;
254 
255 public:
FrontendOptions()256   FrontendOptions() :
257     DisableFree(false), RelocatablePCH(false), ShowHelp(false),
258     ShowStats(false), ShowTimers(false), ShowVersion(false),
259     FixWhatYouCan(false), FixOnlyWarnings(false), FixAndRecompile(false),
260     FixToTemporaries(false), ARCMTMigrateEmitARCErrors(false),
261     SkipFunctionBodies(false), UseGlobalModuleIndex(true),
262     GenerateGlobalModuleIndex(true), ASTDumpDecls(false), ASTDumpLookups(false),
263     ARCMTAction(ARCMT_None), ObjCMTAction(ObjCMT_None),
264     ProgramAction(frontend::ParseSyntaxOnly)
265   {}
266 
267   /// getInputKindForExtension - Return the appropriate input kind for a file
268   /// extension. For example, "c" would return IK_C.
269   ///
270   /// \return The input kind for the extension, or IK_None if the extension is
271   /// not recognized.
272   static InputKind getInputKindForExtension(StringRef Extension);
273 };
274 
275 }  // end namespace clang
276 
277 #endif
278