1341825Sdim//===- FrontendOptions.h ----------------------------------------*- C++ -*-===// 2199482Srdivacky// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6199482Srdivacky// 7199482Srdivacky//===----------------------------------------------------------------------===// 8199482Srdivacky 9199482Srdivacky#ifndef LLVM_CLANG_FRONTEND_FRONTENDOPTIONS_H 10199482Srdivacky#define LLVM_CLANG_FRONTEND_FRONTENDOPTIONS_H 11199482Srdivacky 12353358Sdim#include "clang/AST/ASTDumperUtils.h" 13360784Sdim#include "clang/Basic/LangStandard.h" 14199482Srdivacky#include "clang/Frontend/CommandLineSourceLoc.h" 15360784Sdim#include "clang/Sema/CodeCompleteOptions.h" 16296417Sdim#include "clang/Serialization/ModuleFileExtension.h" 17199482Srdivacky#include "llvm/ADT/StringRef.h" 18341825Sdim#include <cassert> 19341825Sdim#include <memory> 20199482Srdivacky#include <string> 21360784Sdim#include <unordered_map> 22199482Srdivacky#include <vector> 23199482Srdivacky 24243830Sdimnamespace llvm { 25341825Sdim 26243830Sdimclass MemoryBuffer; 27243830Sdim 28341825Sdim} // namespace llvm 29341825Sdim 30199482Srdivackynamespace clang { 31199482Srdivacky 32199482Srdivackynamespace frontend { 33199482Srdivacky 34341825Sdimenum ActionKind { 35341825Sdim /// Parse ASTs and list Decl nodes. 36341825Sdim ASTDeclList, 37341825Sdim 38341825Sdim /// Parse ASTs and dump them. 39341825Sdim ASTDump, 40341825Sdim 41341825Sdim /// Parse ASTs and print them. 42341825Sdim ASTPrint, 43341825Sdim 44341825Sdim /// Parse ASTs and view them in Graphviz. 45341825Sdim ASTView, 46341825Sdim 47341825Sdim /// Dump the compiler configuration. 48341825Sdim DumpCompilerOptions, 49341825Sdim 50341825Sdim /// Dump out raw tokens. 51341825Sdim DumpRawTokens, 52341825Sdim 53341825Sdim /// Dump out preprocessed tokens. 54341825Sdim DumpTokens, 55341825Sdim 56341825Sdim /// Emit a .s file. 57341825Sdim EmitAssembly, 58341825Sdim 59341825Sdim /// Emit a .bc file. 60341825Sdim EmitBC, 61341825Sdim 62341825Sdim /// Translate input source into HTML. 63341825Sdim EmitHTML, 64341825Sdim 65341825Sdim /// Emit a .ll file. 66341825Sdim EmitLLVM, 67341825Sdim 68341825Sdim /// Generate LLVM IR, but do not emit anything. 69341825Sdim EmitLLVMOnly, 70341825Sdim 71341825Sdim /// Generate machine code, but don't emit anything. 72341825Sdim EmitCodeGenOnly, 73341825Sdim 74341825Sdim /// Emit a .o file. 75341825Sdim EmitObj, 76341825Sdim 77341825Sdim /// Parse and apply any fixits to the source. 78341825Sdim FixIt, 79341825Sdim 80341825Sdim /// Generate pre-compiled module from a module map. 81341825Sdim GenerateModule, 82341825Sdim 83341825Sdim /// Generate pre-compiled module from a C++ module interface file. 84341825Sdim GenerateModuleInterface, 85341825Sdim 86344779Sdim /// Generate pre-compiled module from a set of header files. 87344779Sdim GenerateHeaderModule, 88344779Sdim 89341825Sdim /// Generate pre-compiled header. 90341825Sdim GeneratePCH, 91341825Sdim 92353358Sdim /// Generate Interface Stub Files. 93360784Sdim GenerateInterfaceIfsExpV1, 94353358Sdim 95341825Sdim /// Only execute frontend initialization. 96341825Sdim InitOnly, 97341825Sdim 98341825Sdim /// Dump information about a module file. 99341825Sdim ModuleFileInfo, 100341825Sdim 101341825Sdim /// Load and verify that a PCH file is usable. 102341825Sdim VerifyPCH, 103341825Sdim 104341825Sdim /// Parse and perform semantic analysis. 105341825Sdim ParseSyntaxOnly, 106341825Sdim 107341825Sdim /// Run a plugin action, \see ActionName. 108341825Sdim PluginAction, 109341825Sdim 110341825Sdim /// Print the "preamble" of the input file 111341825Sdim PrintPreamble, 112341825Sdim 113341825Sdim /// -E mode. 114341825Sdim PrintPreprocessedInput, 115341825Sdim 116341825Sdim /// Expand macros but not \#includes. 117341825Sdim RewriteMacros, 118341825Sdim 119341825Sdim /// ObjC->C Rewriter. 120341825Sdim RewriteObjC, 121341825Sdim 122341825Sdim /// Rewriter playground 123341825Sdim RewriteTest, 124341825Sdim 125341825Sdim /// Run one or more source code analyses. 126341825Sdim RunAnalysis, 127341825Sdim 128341825Sdim /// Dump template instantiations 129341825Sdim TemplightDump, 130341825Sdim 131341825Sdim /// Run migrator. 132341825Sdim MigrateSource, 133341825Sdim 134341825Sdim /// Just lex, no output. 135353358Sdim RunPreprocessorOnly, 136353358Sdim 137353358Sdim /// Print the output of the dependency directives source minimizer. 138353358Sdim PrintDependencyDirectivesSourceMinimizerOutput 139341825Sdim}; 140341825Sdim 141341825Sdim} // namespace frontend 142341825Sdim 143321369Sdim/// The kind of a file that we've been handed as an input. 144321369Sdimclass InputKind { 145321369Sdimprivate: 146360784Sdim Language Lang; 147321369Sdim unsigned Fmt : 3; 148321369Sdim unsigned Preprocessed : 1; 149321369Sdim 150321369Sdimpublic: 151321369Sdim /// The input file format. 152321369Sdim enum Format { 153321369Sdim Source, 154321369Sdim ModuleMap, 155321369Sdim Precompiled 156321369Sdim }; 157321369Sdim 158360784Sdim constexpr InputKind(Language L = Language::Unknown, Format F = Source, 159321369Sdim bool PP = false) 160321369Sdim : Lang(L), Fmt(F), Preprocessed(PP) {} 161321369Sdim 162321369Sdim Language getLanguage() const { return static_cast<Language>(Lang); } 163321369Sdim Format getFormat() const { return static_cast<Format>(Fmt); } 164321369Sdim bool isPreprocessed() const { return Preprocessed; } 165321369Sdim 166321369Sdim /// Is the input kind fully-unknown? 167360784Sdim bool isUnknown() const { return Lang == Language::Unknown && Fmt == Source; } 168321369Sdim 169321369Sdim /// Is the language of the input some dialect of Objective-C? 170360784Sdim bool isObjectiveC() const { 171360784Sdim return Lang == Language::ObjC || Lang == Language::ObjCXX; 172360784Sdim } 173321369Sdim 174321369Sdim InputKind getPreprocessed() const { 175321369Sdim return InputKind(getLanguage(), getFormat(), true); 176321369Sdim } 177341825Sdim 178321369Sdim InputKind withFormat(Format F) const { 179321369Sdim return InputKind(getLanguage(), F, isPreprocessed()); 180321369Sdim } 181234353Sdim}; 182234353Sdim 183341825Sdim/// An input file for the front end. 184243830Sdimclass FrontendInputFile { 185341825Sdim /// The file name, or "-" to read from standard input. 186234353Sdim std::string File; 187234353Sdim 188327952Sdim /// The input, if it comes from a buffer rather than a file. This object 189327952Sdim /// does not own the buffer, and the caller is responsible for ensuring 190327952Sdim /// that it outlives any users. 191353358Sdim const llvm::MemoryBuffer *Buffer = nullptr; 192243830Sdim 193341825Sdim /// The kind of input, e.g., C source, AST file, LLVM IR. 194234353Sdim InputKind Kind; 195234353Sdim 196341825Sdim /// Whether we're dealing with a 'system' input (vs. a 'user' input). 197327952Sdim bool IsSystem = false; 198243830Sdim 199243830Sdimpublic: 200341825Sdim FrontendInputFile() = default; 201234353Sdim FrontendInputFile(StringRef File, InputKind Kind, bool IsSystem = false) 202341825Sdim : File(File.str()), Kind(Kind), IsSystem(IsSystem) {} 203353358Sdim FrontendInputFile(const llvm::MemoryBuffer *Buffer, InputKind Kind, 204243830Sdim bool IsSystem = false) 205341825Sdim : Buffer(Buffer), Kind(Kind), IsSystem(IsSystem) {} 206243830Sdim 207243830Sdim InputKind getKind() const { return Kind; } 208243830Sdim bool isSystem() const { return IsSystem; } 209243830Sdim 210276479Sdim bool isEmpty() const { return File.empty() && Buffer == nullptr; } 211243830Sdim bool isFile() const { return !isBuffer(); } 212276479Sdim bool isBuffer() const { return Buffer != nullptr; } 213321369Sdim bool isPreprocessed() const { return Kind.isPreprocessed(); } 214243830Sdim 215243830Sdim StringRef getFile() const { 216243830Sdim assert(isFile()); 217243830Sdim return File; 218243830Sdim } 219341825Sdim 220353358Sdim const llvm::MemoryBuffer *getBuffer() const { 221243830Sdim assert(isBuffer()); 222243830Sdim return Buffer; 223243830Sdim } 224234353Sdim}; 225239462Sdim 226199482Srdivacky/// FrontendOptions - Options for controlling the behavior of the frontend. 227199482Srdivackyclass FrontendOptions { 228199482Srdivackypublic: 229341825Sdim /// Disable memory freeing on exit. 230341825Sdim unsigned DisableFree : 1; 231199482Srdivacky 232341825Sdim /// When generating PCH files, instruct the AST writer to create relocatable 233341825Sdim /// PCH files. 234341825Sdim unsigned RelocatablePCH : 1; 235341825Sdim 236341825Sdim /// Show the -help text. 237341825Sdim unsigned ShowHelp : 1; 238341825Sdim 239341825Sdim /// Show frontend performance metrics and statistics. 240341825Sdim unsigned ShowStats : 1; 241341825Sdim 242341825Sdim /// Show timers for individual actions. 243341825Sdim unsigned ShowTimers : 1; 244341825Sdim 245353358Sdim /// print the supported cpus for the current target 246353358Sdim unsigned PrintSupportedCPUs : 1; 247353358Sdim 248353358Sdim /// Output time trace profile. 249353358Sdim unsigned TimeTrace : 1; 250353358Sdim 251341825Sdim /// Show the -version text. 252341825Sdim unsigned ShowVersion : 1; 253341825Sdim 254341825Sdim /// Apply fixes even if there are unfixable errors. 255341825Sdim unsigned FixWhatYouCan : 1; 256341825Sdim 257341825Sdim /// Apply fixes only for warnings. 258341825Sdim unsigned FixOnlyWarnings : 1; 259341825Sdim 260341825Sdim /// Apply fixes and recompile. 261341825Sdim unsigned FixAndRecompile : 1; 262341825Sdim 263341825Sdim /// Apply fixes to temporary files. 264341825Sdim unsigned FixToTemporaries : 1; 265341825Sdim 266341825Sdim /// Emit ARC errors even if the migrator can fix them. 267341825Sdim unsigned ARCMTMigrateEmitARCErrors : 1; 268341825Sdim 269341825Sdim /// Skip over function bodies to speed up parsing in cases you do not need 270341825Sdim /// them (e.g. with code completion). 271341825Sdim unsigned SkipFunctionBodies : 1; 272341825Sdim 273341825Sdim /// Whether we can use the global module index if available. 274341825Sdim unsigned UseGlobalModuleIndex : 1; 275341825Sdim 276341825Sdim /// Whether we can generate the global module index if needed. 277341825Sdim unsigned GenerateGlobalModuleIndex : 1; 278341825Sdim 279341825Sdim /// Whether we include declaration dumps in AST dumps. 280341825Sdim unsigned ASTDumpDecls : 1; 281341825Sdim 282341825Sdim /// Whether we deserialize all decls when forming AST dumps. 283341825Sdim unsigned ASTDumpAll : 1; 284341825Sdim 285341825Sdim /// Whether we include lookup table dumps in AST dumps. 286341825Sdim unsigned ASTDumpLookups : 1; 287341825Sdim 288341825Sdim /// Whether we are performing an implicit module build. 289341825Sdim unsigned BuildingImplicitModule : 1; 290341825Sdim 291341825Sdim /// Whether we should embed all used files into the PCM file. 292341825Sdim unsigned ModulesEmbedAllFiles : 1; 293341825Sdim 294341825Sdim /// Whether timestamps should be written to the produced PCH file. 295341825Sdim unsigned IncludeTimestamps : 1; 296341825Sdim 297360784Sdim /// Should a temporary file be used during compilation. 298360784Sdim unsigned UseTemporary : 1; 299360784Sdim 300239462Sdim CodeCompleteOptions CodeCompleteOpts; 301239462Sdim 302353358Sdim /// Specifies the output format of the AST. 303353358Sdim ASTDumpOutputFormat ASTDumpFormat = ADOF_Default; 304353358Sdim 305224145Sdim enum { 306224145Sdim ARCMT_None, 307224145Sdim ARCMT_Check, 308224145Sdim ARCMT_Modify, 309224145Sdim ARCMT_Migrate 310341825Sdim } ARCMTAction = ARCMT_None; 311224145Sdim 312234353Sdim enum { 313234353Sdim ObjCMT_None = 0, 314341825Sdim 315341825Sdim /// Enable migration to modern ObjC literals. 316234353Sdim ObjCMT_Literals = 0x1, 317341825Sdim 318341825Sdim /// Enable migration to modern ObjC subscripting. 319261991Sdim ObjCMT_Subscripting = 0x2, 320341825Sdim 321341825Sdim /// Enable migration to modern ObjC readonly property. 322261991Sdim ObjCMT_ReadonlyProperty = 0x4, 323341825Sdim 324341825Sdim /// Enable migration to modern ObjC readwrite property. 325261991Sdim ObjCMT_ReadwriteProperty = 0x8, 326341825Sdim 327341825Sdim /// Enable migration to modern ObjC property. 328261991Sdim ObjCMT_Property = (ObjCMT_ReadonlyProperty | ObjCMT_ReadwriteProperty), 329341825Sdim 330341825Sdim /// Enable annotation of ObjCMethods of all kinds. 331261991Sdim ObjCMT_Annotation = 0x10, 332341825Sdim 333341825Sdim /// Enable migration of ObjC methods to 'instancetype'. 334261991Sdim ObjCMT_Instancetype = 0x20, 335341825Sdim 336341825Sdim /// Enable migration to NS_ENUM/NS_OPTIONS macros. 337261991Sdim ObjCMT_NsMacros = 0x40, 338341825Sdim 339341825Sdim /// Enable migration to add conforming protocols. 340261991Sdim ObjCMT_ProtocolConformance = 0x80, 341341825Sdim 342341825Sdim /// prefer 'atomic' property over 'nonatomic'. 343261991Sdim ObjCMT_AtomicProperty = 0x100, 344341825Sdim 345341825Sdim /// annotate property with NS_RETURNS_INNER_POINTER 346261991Sdim ObjCMT_ReturnsInnerPointerProperty = 0x200, 347341825Sdim 348341825Sdim /// use NS_NONATOMIC_IOSONLY for property 'atomic' attribute 349261991Sdim ObjCMT_NsAtomicIOSOnlyProperty = 0x400, 350341825Sdim 351341825Sdim /// Enable inferring NS_DESIGNATED_INITIALIZER for ObjC methods. 352276479Sdim ObjCMT_DesignatedInitializer = 0x800, 353341825Sdim 354341825Sdim /// Enable converting setter/getter expressions to property-dot syntx. 355280031Sdim ObjCMT_PropertyDotSyntax = 0x1000, 356341825Sdim 357261991Sdim ObjCMT_MigrateDecls = (ObjCMT_ReadonlyProperty | ObjCMT_ReadwriteProperty | 358261991Sdim ObjCMT_Annotation | ObjCMT_Instancetype | 359261991Sdim ObjCMT_NsMacros | ObjCMT_ProtocolConformance | 360276479Sdim ObjCMT_NsAtomicIOSOnlyProperty | 361276479Sdim ObjCMT_DesignatedInitializer), 362280031Sdim ObjCMT_MigrateAll = (ObjCMT_Literals | ObjCMT_Subscripting | 363280031Sdim ObjCMT_MigrateDecls | ObjCMT_PropertyDotSyntax) 364234353Sdim }; 365341825Sdim unsigned ObjCMTAction = ObjCMT_None; 366261991Sdim std::string ObjCMTWhiteListPath; 367234353Sdim 368234353Sdim std::string MTMigrateDir; 369226633Sdim std::string ARCMTMigrateReportOut; 370224145Sdim 371199482Srdivacky /// The input files and their types. 372360784Sdim SmallVector<FrontendInputFile, 0> Inputs; 373199482Srdivacky 374321369Sdim /// When the input is a module map, the original module map file from which 375321369Sdim /// that map was inferred, if any (for umbrella modules). 376321369Sdim std::string OriginalModuleMap; 377321369Sdim 378199482Srdivacky /// The output file, if any. 379199482Srdivacky std::string OutputFile; 380199482Srdivacky 381207619Srdivacky /// If given, the new suffix for fix-it rewritten files. 382207619Srdivacky std::string FixItSuffix; 383199482Srdivacky 384239462Sdim /// If given, filter dumped AST Decl nodes by this substring. 385239462Sdim std::string ASTDumpFilter; 386239462Sdim 387199482Srdivacky /// If given, enable code completion at the provided location. 388199482Srdivacky ParsedSourceLocation CodeCompletionAt; 389199482Srdivacky 390199482Srdivacky /// The frontend action to perform. 391341825Sdim frontend::ActionKind ProgramAction = frontend::ParseSyntaxOnly; 392199482Srdivacky 393199482Srdivacky /// The name of the action to run when using a plugin action. 394199482Srdivacky std::string ActionName; 395199482Srdivacky 396309124Sdim /// Args to pass to the plugins 397309124Sdim std::unordered_map<std::string,std::vector<std::string>> PluginArgs; 398210299Sed 399218893Sdim /// The list of plugin actions to run in addition to the normal action. 400218893Sdim std::vector<std::string> AddPluginActions; 401218893Sdim 402200583Srdivacky /// The list of plugins to load. 403200583Srdivacky std::vector<std::string> Plugins; 404200583Srdivacky 405296417Sdim /// The list of module file extensions. 406314564Sdim std::vector<std::shared_ptr<ModuleFileExtension>> ModuleFileExtensions; 407296417Sdim 408341825Sdim /// The list of module map files to load before processing the input. 409280031Sdim std::vector<std::string> ModuleMapFiles; 410280031Sdim 411341825Sdim /// The list of additional prebuilt module files to load before 412280031Sdim /// processing the input. 413280031Sdim std::vector<std::string> ModuleFiles; 414280031Sdim 415341825Sdim /// The list of files to embed into the compiled module file. 416296417Sdim std::vector<std::string> ModulesEmbedFiles; 417296417Sdim 418341825Sdim /// The list of AST files to merge. 419203955Srdivacky std::vector<std::string> ASTMergeFiles; 420203955Srdivacky 421341825Sdim /// A list of arguments to forward to LLVM's option processing; this 422207619Srdivacky /// should only be used for debugging and experimental features. 423207619Srdivacky std::vector<std::string> LLVMArgs; 424207619Srdivacky 425341825Sdim /// File name of the file that will provide record layouts 426234353Sdim /// (in the format produced by -fdump-record-layouts). 427234353Sdim std::string OverrideRecordLayoutsFile; 428296417Sdim 429341825Sdim /// Auxiliary triple for CUDA compilation. 430296417Sdim std::string AuxTriple; 431296417Sdim 432314564Sdim /// Filename to write statistics to. 433314564Sdim std::string StatsFile; 434314564Sdim 435360784Sdim /// Minimum time granularity (in microseconds) traced by time profiler. 436360784Sdim unsigned TimeTraceGranularity; 437360784Sdim 438199482Srdivackypublic: 439341825Sdim FrontendOptions() 440341825Sdim : DisableFree(false), RelocatablePCH(false), ShowHelp(false), 441353358Sdim ShowStats(false), ShowTimers(false), TimeTrace(false), 442353358Sdim ShowVersion(false), FixWhatYouCan(false), FixOnlyWarnings(false), 443353358Sdim FixAndRecompile(false), FixToTemporaries(false), 444353358Sdim ARCMTMigrateEmitARCErrors(false), SkipFunctionBodies(false), 445353358Sdim UseGlobalModuleIndex(true), GenerateGlobalModuleIndex(true), 446353358Sdim ASTDumpDecls(false), ASTDumpLookups(false), 447353358Sdim BuildingImplicitModule(false), ModulesEmbedAllFiles(false), 448360784Sdim IncludeTimestamps(true), UseTemporary(true), TimeTraceGranularity(500) {} 449199482Srdivacky 450199482Srdivacky /// getInputKindForExtension - Return the appropriate input kind for a file 451360784Sdim /// extension. For example, "c" would return Language::C. 452199482Srdivacky /// 453360784Sdim /// \return The input kind for the extension, or Language::Unknown if the 454321369Sdim /// extension is not recognized. 455226633Sdim static InputKind getInputKindForExtension(StringRef Extension); 456199482Srdivacky}; 457199482Srdivacky 458341825Sdim} // namespace clang 459199482Srdivacky 460341825Sdim#endif // LLVM_CLANG_FRONTEND_FRONTENDOPTIONS_H 461