FrontendOptions.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- FrontendOptions.h ----------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_FRONTEND_FRONTENDOPTIONS_H
  14. #define LLVM_CLANG_FRONTEND_FRONTENDOPTIONS_H
  15. #include "clang/AST/ASTDumperUtils.h"
  16. #include "clang/Basic/LangStandard.h"
  17. #include "clang/Frontend/CommandLineSourceLoc.h"
  18. #include "clang/Sema/CodeCompleteOptions.h"
  19. #include "clang/Serialization/ModuleFileExtension.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/Support/MemoryBuffer.h"
  22. #include <cassert>
  23. #include <map>
  24. #include <memory>
  25. #include <string>
  26. #include <vector>
  27. namespace llvm {
  28. class MemoryBuffer;
  29. } // namespace llvm
  30. namespace clang {
  31. namespace frontend {
  32. enum ActionKind {
  33. /// Parse ASTs and list Decl nodes.
  34. ASTDeclList,
  35. /// Parse ASTs and dump them.
  36. ASTDump,
  37. /// Parse ASTs and print them.
  38. ASTPrint,
  39. /// Parse ASTs and view them in Graphviz.
  40. ASTView,
  41. /// Dump the compiler configuration.
  42. DumpCompilerOptions,
  43. /// Dump out raw tokens.
  44. DumpRawTokens,
  45. /// Dump out preprocessed tokens.
  46. DumpTokens,
  47. /// Emit a .s file.
  48. EmitAssembly,
  49. /// Emit a .bc file.
  50. EmitBC,
  51. /// Translate input source into HTML.
  52. EmitHTML,
  53. /// Emit a .ll file.
  54. EmitLLVM,
  55. /// Generate LLVM IR, but do not emit anything.
  56. EmitLLVMOnly,
  57. /// Generate machine code, but don't emit anything.
  58. EmitCodeGenOnly,
  59. /// Emit a .o file.
  60. EmitObj,
  61. // Extract API information
  62. ExtractAPI,
  63. /// Parse and apply any fixits to the source.
  64. FixIt,
  65. /// Generate pre-compiled module from a module map.
  66. GenerateModule,
  67. /// Generate pre-compiled module from a C++ module interface file.
  68. GenerateModuleInterface,
  69. /// Generate pre-compiled module from a set of header files.
  70. GenerateHeaderModule,
  71. /// Generate pre-compiled header.
  72. GeneratePCH,
  73. /// Generate Interface Stub Files.
  74. GenerateInterfaceStubs,
  75. /// Only execute frontend initialization.
  76. InitOnly,
  77. /// Dump information about a module file.
  78. ModuleFileInfo,
  79. /// Load and verify that a PCH file is usable.
  80. VerifyPCH,
  81. /// Parse and perform semantic analysis.
  82. ParseSyntaxOnly,
  83. /// Run a plugin action, \see ActionName.
  84. PluginAction,
  85. /// Print the "preamble" of the input file
  86. PrintPreamble,
  87. /// -E mode.
  88. PrintPreprocessedInput,
  89. /// Expand macros but not \#includes.
  90. RewriteMacros,
  91. /// ObjC->C Rewriter.
  92. RewriteObjC,
  93. /// Rewriter playground
  94. RewriteTest,
  95. /// Run one or more source code analyses.
  96. RunAnalysis,
  97. /// Dump template instantiations
  98. TemplightDump,
  99. /// Run migrator.
  100. MigrateSource,
  101. /// Just lex, no output.
  102. RunPreprocessorOnly,
  103. /// Print the output of the dependency directives source minimizer.
  104. PrintDependencyDirectivesSourceMinimizerOutput
  105. };
  106. } // namespace frontend
  107. /// The kind of a file that we've been handed as an input.
  108. class InputKind {
  109. private:
  110. Language Lang;
  111. unsigned Fmt : 3;
  112. unsigned Preprocessed : 1;
  113. public:
  114. /// The input file format.
  115. enum Format {
  116. Source,
  117. ModuleMap,
  118. Precompiled
  119. };
  120. constexpr InputKind(Language L = Language::Unknown, Format F = Source,
  121. bool PP = false)
  122. : Lang(L), Fmt(F), Preprocessed(PP) {}
  123. Language getLanguage() const { return static_cast<Language>(Lang); }
  124. Format getFormat() const { return static_cast<Format>(Fmt); }
  125. bool isPreprocessed() const { return Preprocessed; }
  126. /// Is the input kind fully-unknown?
  127. bool isUnknown() const { return Lang == Language::Unknown && Fmt == Source; }
  128. /// Is the language of the input some dialect of Objective-C?
  129. bool isObjectiveC() const {
  130. return Lang == Language::ObjC || Lang == Language::ObjCXX;
  131. }
  132. InputKind getPreprocessed() const {
  133. return InputKind(getLanguage(), getFormat(), true);
  134. }
  135. InputKind withFormat(Format F) const {
  136. return InputKind(getLanguage(), F, isPreprocessed());
  137. }
  138. };
  139. /// An input file for the front end.
  140. class FrontendInputFile {
  141. /// The file name, or "-" to read from standard input.
  142. std::string File;
  143. /// The input, if it comes from a buffer rather than a file. This object
  144. /// does not own the buffer, and the caller is responsible for ensuring
  145. /// that it outlives any users.
  146. llvm::Optional<llvm::MemoryBufferRef> Buffer;
  147. /// The kind of input, e.g., C source, AST file, LLVM IR.
  148. InputKind Kind;
  149. /// Whether we're dealing with a 'system' input (vs. a 'user' input).
  150. bool IsSystem = false;
  151. public:
  152. FrontendInputFile() = default;
  153. FrontendInputFile(StringRef File, InputKind Kind, bool IsSystem = false)
  154. : File(File.str()), Kind(Kind), IsSystem(IsSystem) {}
  155. FrontendInputFile(llvm::MemoryBufferRef Buffer, InputKind Kind,
  156. bool IsSystem = false)
  157. : Buffer(Buffer), Kind(Kind), IsSystem(IsSystem) {}
  158. InputKind getKind() const { return Kind; }
  159. bool isSystem() const { return IsSystem; }
  160. bool isEmpty() const { return File.empty() && Buffer == None; }
  161. bool isFile() const { return !isBuffer(); }
  162. bool isBuffer() const { return Buffer != None; }
  163. bool isPreprocessed() const { return Kind.isPreprocessed(); }
  164. StringRef getFile() const {
  165. assert(isFile());
  166. return File;
  167. }
  168. llvm::MemoryBufferRef getBuffer() const {
  169. assert(isBuffer());
  170. return *Buffer;
  171. }
  172. };
  173. /// FrontendOptions - Options for controlling the behavior of the frontend.
  174. class FrontendOptions {
  175. public:
  176. /// Disable memory freeing on exit.
  177. unsigned DisableFree : 1;
  178. /// When generating PCH files, instruct the AST writer to create relocatable
  179. /// PCH files.
  180. unsigned RelocatablePCH : 1;
  181. /// Show the -help text.
  182. unsigned ShowHelp : 1;
  183. /// Show frontend performance metrics and statistics.
  184. unsigned ShowStats : 1;
  185. /// print the supported cpus for the current target
  186. unsigned PrintSupportedCPUs : 1;
  187. /// Output time trace profile.
  188. unsigned TimeTrace : 1;
  189. /// Show the -version text.
  190. unsigned ShowVersion : 1;
  191. /// Apply fixes even if there are unfixable errors.
  192. unsigned FixWhatYouCan : 1;
  193. /// Apply fixes only for warnings.
  194. unsigned FixOnlyWarnings : 1;
  195. /// Apply fixes and recompile.
  196. unsigned FixAndRecompile : 1;
  197. /// Apply fixes to temporary files.
  198. unsigned FixToTemporaries : 1;
  199. /// Emit ARC errors even if the migrator can fix them.
  200. unsigned ARCMTMigrateEmitARCErrors : 1;
  201. /// Skip over function bodies to speed up parsing in cases you do not need
  202. /// them (e.g. with code completion).
  203. unsigned SkipFunctionBodies : 1;
  204. /// Whether we can use the global module index if available.
  205. unsigned UseGlobalModuleIndex : 1;
  206. /// Whether we can generate the global module index if needed.
  207. unsigned GenerateGlobalModuleIndex : 1;
  208. /// Whether we include declaration dumps in AST dumps.
  209. unsigned ASTDumpDecls : 1;
  210. /// Whether we deserialize all decls when forming AST dumps.
  211. unsigned ASTDumpAll : 1;
  212. /// Whether we include lookup table dumps in AST dumps.
  213. unsigned ASTDumpLookups : 1;
  214. /// Whether we include declaration type dumps in AST dumps.
  215. unsigned ASTDumpDeclTypes : 1;
  216. /// Whether we are performing an implicit module build.
  217. unsigned BuildingImplicitModule : 1;
  218. /// Whether to use a filesystem lock when building implicit modules.
  219. unsigned BuildingImplicitModuleUsesLock : 1;
  220. /// Whether we should embed all used files into the PCM file.
  221. unsigned ModulesEmbedAllFiles : 1;
  222. /// Whether timestamps should be written to the produced PCH file.
  223. unsigned IncludeTimestamps : 1;
  224. /// Should a temporary file be used during compilation.
  225. unsigned UseTemporary : 1;
  226. /// When using -emit-module, treat the modulemap as a system module.
  227. unsigned IsSystemModule : 1;
  228. /// Output (and read) PCM files regardless of compiler errors.
  229. unsigned AllowPCMWithCompilerErrors : 1;
  230. CodeCompleteOptions CodeCompleteOpts;
  231. /// Specifies the output format of the AST.
  232. ASTDumpOutputFormat ASTDumpFormat = ADOF_Default;
  233. enum {
  234. ARCMT_None,
  235. ARCMT_Check,
  236. ARCMT_Modify,
  237. ARCMT_Migrate
  238. } ARCMTAction = ARCMT_None;
  239. enum {
  240. ObjCMT_None = 0,
  241. /// Enable migration to modern ObjC literals.
  242. ObjCMT_Literals = 0x1,
  243. /// Enable migration to modern ObjC subscripting.
  244. ObjCMT_Subscripting = 0x2,
  245. /// Enable migration to modern ObjC readonly property.
  246. ObjCMT_ReadonlyProperty = 0x4,
  247. /// Enable migration to modern ObjC readwrite property.
  248. ObjCMT_ReadwriteProperty = 0x8,
  249. /// Enable migration to modern ObjC property.
  250. ObjCMT_Property = (ObjCMT_ReadonlyProperty | ObjCMT_ReadwriteProperty),
  251. /// Enable annotation of ObjCMethods of all kinds.
  252. ObjCMT_Annotation = 0x10,
  253. /// Enable migration of ObjC methods to 'instancetype'.
  254. ObjCMT_Instancetype = 0x20,
  255. /// Enable migration to NS_ENUM/NS_OPTIONS macros.
  256. ObjCMT_NsMacros = 0x40,
  257. /// Enable migration to add conforming protocols.
  258. ObjCMT_ProtocolConformance = 0x80,
  259. /// prefer 'atomic' property over 'nonatomic'.
  260. ObjCMT_AtomicProperty = 0x100,
  261. /// annotate property with NS_RETURNS_INNER_POINTER
  262. ObjCMT_ReturnsInnerPointerProperty = 0x200,
  263. /// use NS_NONATOMIC_IOSONLY for property 'atomic' attribute
  264. ObjCMT_NsAtomicIOSOnlyProperty = 0x400,
  265. /// Enable inferring NS_DESIGNATED_INITIALIZER for ObjC methods.
  266. ObjCMT_DesignatedInitializer = 0x800,
  267. /// Enable converting setter/getter expressions to property-dot syntx.
  268. ObjCMT_PropertyDotSyntax = 0x1000,
  269. ObjCMT_MigrateDecls = (ObjCMT_ReadonlyProperty | ObjCMT_ReadwriteProperty |
  270. ObjCMT_Annotation | ObjCMT_Instancetype |
  271. ObjCMT_NsMacros | ObjCMT_ProtocolConformance |
  272. ObjCMT_NsAtomicIOSOnlyProperty |
  273. ObjCMT_DesignatedInitializer),
  274. ObjCMT_MigrateAll = (ObjCMT_Literals | ObjCMT_Subscripting |
  275. ObjCMT_MigrateDecls | ObjCMT_PropertyDotSyntax)
  276. };
  277. unsigned ObjCMTAction = ObjCMT_None;
  278. std::string ObjCMTAllowListPath;
  279. std::string MTMigrateDir;
  280. std::string ARCMTMigrateReportOut;
  281. /// The input kind, either specified via -x argument or deduced from the input
  282. /// file name.
  283. InputKind DashX;
  284. /// The input files and their types.
  285. SmallVector<FrontendInputFile, 0> Inputs;
  286. /// When the input is a module map, the original module map file from which
  287. /// that map was inferred, if any (for umbrella modules).
  288. std::string OriginalModuleMap;
  289. /// The output file, if any.
  290. std::string OutputFile;
  291. /// If given, the new suffix for fix-it rewritten files.
  292. std::string FixItSuffix;
  293. /// If given, filter dumped AST Decl nodes by this substring.
  294. std::string ASTDumpFilter;
  295. /// If given, enable code completion at the provided location.
  296. ParsedSourceLocation CodeCompletionAt;
  297. /// The frontend action to perform.
  298. frontend::ActionKind ProgramAction = frontend::ParseSyntaxOnly;
  299. /// The name of the action to run when using a plugin action.
  300. std::string ActionName;
  301. /// Args to pass to the plugins
  302. std::map<std::string, std::vector<std::string>> PluginArgs;
  303. /// The list of plugin actions to run in addition to the normal action.
  304. std::vector<std::string> AddPluginActions;
  305. /// The list of plugins to load.
  306. std::vector<std::string> Plugins;
  307. /// The list of module file extensions.
  308. std::vector<std::shared_ptr<ModuleFileExtension>> ModuleFileExtensions;
  309. /// The list of module map files to load before processing the input.
  310. std::vector<std::string> ModuleMapFiles;
  311. /// The list of additional prebuilt module files to load before
  312. /// processing the input.
  313. std::vector<std::string> ModuleFiles;
  314. /// The list of files to embed into the compiled module file.
  315. std::vector<std::string> ModulesEmbedFiles;
  316. /// The list of AST files to merge.
  317. std::vector<std::string> ASTMergeFiles;
  318. /// A list of arguments to forward to LLVM's option processing; this
  319. /// should only be used for debugging and experimental features.
  320. std::vector<std::string> LLVMArgs;
  321. /// File name of the file that will provide record layouts
  322. /// (in the format produced by -fdump-record-layouts).
  323. std::string OverrideRecordLayoutsFile;
  324. /// Auxiliary triple for CUDA/HIP compilation.
  325. std::string AuxTriple;
  326. /// Auxiliary target CPU for CUDA/HIP compilation.
  327. Optional<std::string> AuxTargetCPU;
  328. /// Auxiliary target features for CUDA/HIP compilation.
  329. Optional<std::vector<std::string>> AuxTargetFeatures;
  330. /// Filename to write statistics to.
  331. std::string StatsFile;
  332. /// Minimum time granularity (in microseconds) traced by time profiler.
  333. unsigned TimeTraceGranularity;
  334. public:
  335. FrontendOptions()
  336. : DisableFree(false), RelocatablePCH(false), ShowHelp(false),
  337. ShowStats(false), TimeTrace(false), ShowVersion(false),
  338. FixWhatYouCan(false), FixOnlyWarnings(false), FixAndRecompile(false),
  339. FixToTemporaries(false), ARCMTMigrateEmitARCErrors(false),
  340. SkipFunctionBodies(false), UseGlobalModuleIndex(true),
  341. GenerateGlobalModuleIndex(true), ASTDumpDecls(false),
  342. ASTDumpLookups(false), BuildingImplicitModule(false),
  343. BuildingImplicitModuleUsesLock(true), ModulesEmbedAllFiles(false),
  344. IncludeTimestamps(true), UseTemporary(true),
  345. AllowPCMWithCompilerErrors(false), TimeTraceGranularity(500) {}
  346. /// getInputKindForExtension - Return the appropriate input kind for a file
  347. /// extension. For example, "c" would return Language::C.
  348. ///
  349. /// \return The input kind for the extension, or Language::Unknown if the
  350. /// extension is not recognized.
  351. static InputKind getInputKindForExtension(StringRef Extension);
  352. };
  353. } // namespace clang
  354. #endif // LLVM_CLANG_FRONTEND_FRONTENDOPTIONS_H
  355. #ifdef __GNUC__
  356. #pragma GCC diagnostic pop
  357. #endif