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 LLVM_OPTION_OPTTABLE_H 11 #define LLVM_OPTION_OPTTABLE_H 12 13 #include "llvm/ADT/ArrayRef.h" 14 #include "llvm/ADT/StringSet.h" 15 #include "llvm/Option/OptSpecifier.h" 16 17 namespace llvm { 18 class raw_ostream; 19 namespace opt { 20 class Arg; 21 class ArgList; 22 class InputArgList; 23 class Option; 24 25 /// \brief Provide access to the Option info table. 26 /// 27 /// The OptTable class provides a layer of indirection which allows Option 28 /// instance to be created lazily. In the common case, only a few options will 29 /// be needed at runtime; the OptTable class maintains enough information to 30 /// parse command lines without instantiating Options, while letting other 31 /// parts of the driver still use Option instances where convenient. 32 class OptTable { 33 public: 34 /// \brief Entry for a single option instance in the option data table. 35 struct Info { 36 /// A null terminated array of prefix strings to apply to name while 37 /// matching. 38 const char *const *Prefixes; 39 const char *Name; 40 const char *HelpText; 41 const char *MetaVar; 42 unsigned ID; 43 unsigned char Kind; 44 unsigned char Param; 45 unsigned short Flags; 46 unsigned short GroupID; 47 unsigned short AliasID; 48 const char *AliasArgs; 49 }; 50 51 private: 52 /// \brief The static option information table. 53 ArrayRef<Info> OptionInfos; 54 bool IgnoreCase; 55 56 unsigned TheInputOptionID; 57 unsigned TheUnknownOptionID; 58 59 /// The index of the first option which can be parsed (i.e., is not a 60 /// special option like 'input' or 'unknown', and is not an option group). 61 unsigned FirstSearchableIndex; 62 63 /// The union of all option prefixes. If an argument does not begin with 64 /// one of these, it is an input. 65 StringSet<> PrefixesUnion; 66 std::string PrefixChars; 67 68 private: getInfo(OptSpecifier Opt)69 const Info &getInfo(OptSpecifier Opt) const { 70 unsigned id = Opt.getID(); 71 assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID."); 72 return OptionInfos[id - 1]; 73 } 74 75 protected: 76 OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase = false); 77 78 public: 79 ~OptTable(); 80 81 /// \brief Return the total number of option classes. getNumOptions()82 unsigned getNumOptions() const { return OptionInfos.size(); } 83 84 /// \brief Get the given Opt's Option instance, lazily creating it 85 /// if necessary. 86 /// 87 /// \return The option, or null for the INVALID option id. 88 const Option getOption(OptSpecifier Opt) const; 89 90 /// \brief Lookup the name of the given option. getOptionName(OptSpecifier id)91 const char *getOptionName(OptSpecifier id) const { 92 return getInfo(id).Name; 93 } 94 95 /// \brief Get the kind of the given option. getOptionKind(OptSpecifier id)96 unsigned getOptionKind(OptSpecifier id) const { 97 return getInfo(id).Kind; 98 } 99 100 /// \brief Get the group id for the given option. getOptionGroupID(OptSpecifier id)101 unsigned getOptionGroupID(OptSpecifier id) const { 102 return getInfo(id).GroupID; 103 } 104 105 /// \brief Get the help text to use to describe this option. getOptionHelpText(OptSpecifier id)106 const char *getOptionHelpText(OptSpecifier id) const { 107 return getInfo(id).HelpText; 108 } 109 110 /// \brief Get the meta-variable name to use when describing 111 /// this options values in the help text. getOptionMetaVar(OptSpecifier id)112 const char *getOptionMetaVar(OptSpecifier id) const { 113 return getInfo(id).MetaVar; 114 } 115 116 /// \brief Parse a single argument; returning the new argument and 117 /// updating Index. 118 /// 119 /// \param [in,out] Index - The current parsing position in the argument 120 /// string list; on return this will be the index of the next argument 121 /// string to parse. 122 /// \param [in] FlagsToInclude - Only parse options with any of these flags. 123 /// Zero is the default which includes all flags. 124 /// \param [in] FlagsToExclude - Don't parse options with this flag. Zero 125 /// is the default and means exclude nothing. 126 /// 127 /// \return The parsed argument, or 0 if the argument is missing values 128 /// (in which case Index still points at the conceptual next argument string 129 /// to parse). 130 Arg *ParseOneArg(const ArgList &Args, unsigned &Index, 131 unsigned FlagsToInclude = 0, 132 unsigned FlagsToExclude = 0) const; 133 134 /// \brief Parse an list of arguments into an InputArgList. 135 /// 136 /// The resulting InputArgList will reference the strings in [\p ArgBegin, 137 /// \p ArgEnd), and their lifetime should extend past that of the returned 138 /// InputArgList. 139 /// 140 /// The only error that can occur in this routine is if an argument is 141 /// missing values; in this case \p MissingArgCount will be non-zero. 142 /// 143 /// \param MissingArgIndex - On error, the index of the option which could 144 /// not be parsed. 145 /// \param MissingArgCount - On error, the number of missing options. 146 /// \param FlagsToInclude - Only parse options with any of these flags. 147 /// Zero is the default which includes all flags. 148 /// \param FlagsToExclude - Don't parse options with this flag. Zero 149 /// is the default and means exclude nothing. 150 /// \return An InputArgList; on error this will contain all the options 151 /// which could be parsed. 152 InputArgList ParseArgs(ArrayRef<const char *> Args, unsigned &MissingArgIndex, 153 unsigned &MissingArgCount, unsigned FlagsToInclude = 0, 154 unsigned FlagsToExclude = 0) const; 155 156 /// \brief Render the help text for an option table. 157 /// 158 /// \param OS - The stream to write the help text to. 159 /// \param Name - The name to use in the usage line. 160 /// \param Title - The title to use in the usage line. 161 /// \param FlagsToInclude - If non-zero, only include options with any 162 /// of these flags set. 163 /// \param FlagsToExclude - Exclude options with any of these flags set. 164 void PrintHelp(raw_ostream &OS, const char *Name, 165 const char *Title, unsigned FlagsToInclude, 166 unsigned FlagsToExclude) const; 167 168 void PrintHelp(raw_ostream &OS, const char *Name, 169 const char *Title, bool ShowHidden = false) const; 170 }; 171 } // end namespace opt 172 } // end namespace llvm 173 174 #endif 175