1 //===--- OptTable.h - Option Table ------------------------------*- 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 CLANG_DRIVER_OPTTABLE_H 11 #define CLANG_DRIVER_OPTTABLE_H 12 13 #include "clang/Basic/LLVM.h" 14 #include "clang/Driver/OptSpecifier.h" 15 16 namespace clang { 17 namespace driver { 18 namespace options { 19 enum DriverFlag { 20 DriverOption = (1 << 0), 21 HelpHidden = (1 << 1), 22 LinkerInput = (1 << 2), 23 NoArgumentUnused = (1 << 3), 24 NoForward = (1 << 4), 25 RenderAsInput = (1 << 5), 26 RenderJoined = (1 << 6), 27 RenderSeparate = (1 << 7), 28 Unsupported = (1 << 8) 29 }; 30 } 31 32 class Arg; 33 class ArgList; 34 class InputArgList; 35 class Option; 36 37 /// OptTable - Provide access to the Option info table. 38 /// 39 /// The OptTable class provides a layer of indirection which allows Option 40 /// instance to be created lazily. In the common case, only a few options will 41 /// be needed at runtime; the OptTable class maintains enough information to 42 /// parse command lines without instantiating Options, while letting other 43 /// parts of the driver still use Option instances where convenient. 44 class OptTable { 45 public: 46 /// Info - Entry for a single option instance in the option data table. 47 struct Info { 48 const char *Name; 49 const char *HelpText; 50 const char *MetaVar; 51 unsigned char Kind; 52 unsigned char Param; 53 unsigned short Flags; 54 unsigned short GroupID; 55 unsigned short AliasID; 56 }; 57 58 private: 59 /// The static option information table. 60 const Info *OptionInfos; 61 unsigned NumOptionInfos; 62 63 /// The lazily constructed options table, indexed by option::ID - 1. 64 mutable Option **Options; 65 66 /// Prebound input option instance. 67 const Option *TheInputOption; 68 69 /// Prebound unknown option instance. 70 const Option *TheUnknownOption; 71 72 /// The index of the first option which can be parsed (i.e., is not a 73 /// special option like 'input' or 'unknown', and is not an option group). 74 unsigned FirstSearchableIndex; 75 76 private: getInfo(OptSpecifier Opt)77 const Info &getInfo(OptSpecifier Opt) const { 78 unsigned id = Opt.getID(); 79 assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID."); 80 return OptionInfos[id - 1]; 81 } 82 83 Option *CreateOption(unsigned id) const; 84 85 protected: 86 OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos); 87 public: 88 ~OptTable(); 89 90 /// getNumOptions - Return the total number of option classes. getNumOptions()91 unsigned getNumOptions() const { return NumOptionInfos; } 92 93 /// getOption - Get the given \arg id's Option instance, lazily creating it 94 /// if necessary. 95 /// 96 /// \return The option, or null for the INVALID option id. getOption(OptSpecifier Opt)97 const Option *getOption(OptSpecifier Opt) const { 98 unsigned id = Opt.getID(); 99 if (id == 0) 100 return 0; 101 102 assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID."); 103 Option *&Entry = Options[id - 1]; 104 if (!Entry) 105 Entry = CreateOption(id); 106 return Entry; 107 } 108 109 /// getOptionName - Lookup the name of the given option. getOptionName(OptSpecifier id)110 const char *getOptionName(OptSpecifier id) const { 111 return getInfo(id).Name; 112 } 113 114 /// getOptionKind - Get the kind of the given option. getOptionKind(OptSpecifier id)115 unsigned getOptionKind(OptSpecifier id) const { 116 return getInfo(id).Kind; 117 } 118 119 /// getOptionGroupID - Get the group id for the given option. getOptionGroupID(OptSpecifier id)120 unsigned getOptionGroupID(OptSpecifier id) const { 121 return getInfo(id).GroupID; 122 } 123 124 /// isOptionHelpHidden - Should the help for the given option be hidden by 125 /// default. isOptionHelpHidden(OptSpecifier id)126 bool isOptionHelpHidden(OptSpecifier id) const { 127 return getInfo(id).Flags & options::HelpHidden; 128 } 129 130 /// getOptionHelpText - Get the help text to use to describe this option. getOptionHelpText(OptSpecifier id)131 const char *getOptionHelpText(OptSpecifier id) const { 132 return getInfo(id).HelpText; 133 } 134 135 /// getOptionMetaVar - Get the meta-variable name to use when describing 136 /// this options values in the help text. getOptionMetaVar(OptSpecifier id)137 const char *getOptionMetaVar(OptSpecifier id) const { 138 return getInfo(id).MetaVar; 139 } 140 141 /// ParseOneArg - Parse a single argument; returning the new argument and 142 /// updating Index. 143 /// 144 /// \param [in] [out] Index - The current parsing position in the argument 145 /// string list; on return this will be the index of the next argument 146 /// string to parse. 147 /// 148 /// \return - The parsed argument, or 0 if the argument is missing values 149 /// (in which case Index still points at the conceptual next argument string 150 /// to parse). 151 Arg *ParseOneArg(const ArgList &Args, unsigned &Index) const; 152 153 /// ParseArgs - Parse an list of arguments into an InputArgList. 154 /// 155 /// The resulting InputArgList will reference the strings in [ArgBegin, 156 /// ArgEnd), and their lifetime should extend past that of the returned 157 /// InputArgList. 158 /// 159 /// The only error that can occur in this routine is if an argument is 160 /// missing values; in this case \arg MissingArgCount will be non-zero. 161 /// 162 /// \param ArgBegin - The beginning of the argument vector. 163 /// \param ArgEnd - The end of the argument vector. 164 /// \param MissingArgIndex - On error, the index of the option which could 165 /// not be parsed. 166 /// \param MissingArgCount - On error, the number of missing options. 167 /// \return - An InputArgList; on error this will contain all the options 168 /// which could be parsed. 169 InputArgList *ParseArgs(const char* const *ArgBegin, 170 const char* const *ArgEnd, 171 unsigned &MissingArgIndex, 172 unsigned &MissingArgCount) const; 173 174 /// PrintHelp - Render the help text for an option table. 175 /// 176 /// \param OS - The stream to write the help text to. 177 /// \param Name - The name to use in the usage line. 178 /// \param Title - The title to use in the usage line. 179 /// \param ShowHidden - Whether help-hidden arguments should be shown. 180 void PrintHelp(raw_ostream &OS, const char *Name, 181 const char *Title, bool ShowHidden = false) const; 182 }; 183 } 184 } 185 186 #endif 187