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