LLVM API Documentation

OptTable.h
Go to the documentation of this file.
00001 //===--- OptTable.h - Option Table ------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_OPTION_OPTTABLE_H
00011 #define LLVM_OPTION_OPTTABLE_H
00012 
00013 #include "llvm/ADT/StringSet.h"
00014 #include "llvm/Option/OptSpecifier.h"
00015 
00016 namespace llvm {
00017 class raw_ostream;
00018 namespace opt {
00019 class Arg;
00020 class ArgList;
00021 class InputArgList;
00022 class Option;
00023 
00024 /// \brief Provide access to the Option info table.
00025 ///
00026 /// The OptTable class provides a layer of indirection which allows Option
00027 /// instance to be created lazily. In the common case, only a few options will
00028 /// be needed at runtime; the OptTable class maintains enough information to
00029 /// parse command lines without instantiating Options, while letting other
00030 /// parts of the driver still use Option instances where convenient.
00031 class OptTable {
00032 public:
00033   /// \brief Entry for a single option instance in the option data table.
00034   struct Info {
00035     /// A null terminated array of prefix strings to apply to name while
00036     /// matching.
00037     const char *const *Prefixes;
00038     const char *Name;
00039     const char *HelpText;
00040     const char *MetaVar;
00041     unsigned ID;
00042     unsigned char Kind;
00043     unsigned char Param;
00044     unsigned short Flags;
00045     unsigned short GroupID;
00046     unsigned short AliasID;
00047     const char *AliasArgs;
00048   };
00049 
00050 private:
00051   /// \brief The static option information table.
00052   const Info *OptionInfos;
00053   unsigned NumOptionInfos;
00054   bool IgnoreCase;
00055 
00056   unsigned TheInputOptionID;
00057   unsigned TheUnknownOptionID;
00058 
00059   /// The index of the first option which can be parsed (i.e., is not a
00060   /// special option like 'input' or 'unknown', and is not an option group).
00061   unsigned FirstSearchableIndex;
00062 
00063   /// The union of all option prefixes. If an argument does not begin with
00064   /// one of these, it is an input.
00065   StringSet<> PrefixesUnion;
00066   std::string PrefixChars;
00067 
00068 private:
00069   const Info &getInfo(OptSpecifier Opt) const {
00070     unsigned id = Opt.getID();
00071     assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID.");
00072     return OptionInfos[id - 1];
00073   }
00074 
00075 protected:
00076   OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos,
00077            bool _IgnoreCase = false);
00078 public:
00079   ~OptTable();
00080 
00081   /// \brief Return the total number of option classes.
00082   unsigned getNumOptions() const { return NumOptionInfos; }
00083 
00084   /// \brief Get the given Opt's Option instance, lazily creating it
00085   /// if necessary.
00086   ///
00087   /// \return The option, or null for the INVALID option id.
00088   const Option getOption(OptSpecifier Opt) const;
00089 
00090   /// \brief Lookup the name of the given option.
00091   const char *getOptionName(OptSpecifier id) const {
00092     return getInfo(id).Name;
00093   }
00094 
00095   /// \brief Get the kind of the given option.
00096   unsigned getOptionKind(OptSpecifier id) const {
00097     return getInfo(id).Kind;
00098   }
00099 
00100   /// \brief Get the group id for the given option.
00101   unsigned getOptionGroupID(OptSpecifier id) const {
00102     return getInfo(id).GroupID;
00103   }
00104 
00105   /// \brief Get the help text to use to describe this option.
00106   const char *getOptionHelpText(OptSpecifier id) const {
00107     return getInfo(id).HelpText;
00108   }
00109 
00110   /// \brief Get the meta-variable name to use when describing
00111   /// this options values in the help text.
00112   const char *getOptionMetaVar(OptSpecifier id) const {
00113     return getInfo(id).MetaVar;
00114   }
00115 
00116   /// \brief Parse a single argument; returning the new argument and
00117   /// updating Index.
00118   ///
00119   /// \param [in,out] Index - The current parsing position in the argument
00120   /// string list; on return this will be the index of the next argument
00121   /// string to parse.
00122   /// \param [in] FlagsToInclude - Only parse options with any of these flags.
00123   /// Zero is the default which includes all flags.
00124   /// \param [in] FlagsToExclude - Don't parse options with this flag.  Zero
00125   /// is the default and means exclude nothing.
00126   ///
00127   /// \return The parsed argument, or 0 if the argument is missing values
00128   /// (in which case Index still points at the conceptual next argument string
00129   /// to parse).
00130   Arg *ParseOneArg(const ArgList &Args, unsigned &Index,
00131                    unsigned FlagsToInclude = 0,
00132                    unsigned FlagsToExclude = 0) const;
00133 
00134   /// \brief Parse an list of arguments into an InputArgList.
00135   ///
00136   /// The resulting InputArgList will reference the strings in [\p ArgBegin,
00137   /// \p ArgEnd), and their lifetime should extend past that of the returned
00138   /// InputArgList.
00139   ///
00140   /// The only error that can occur in this routine is if an argument is
00141   /// missing values; in this case \p MissingArgCount will be non-zero.
00142   ///
00143   /// \param ArgBegin - The beginning of the argument vector.
00144   /// \param ArgEnd - The end of the argument vector.
00145   /// \param MissingArgIndex - On error, the index of the option which could
00146   /// not be parsed.
00147   /// \param MissingArgCount - On error, the number of missing options.
00148   /// \param FlagsToInclude - Only parse options with any of these flags.
00149   /// Zero is the default which includes all flags.
00150   /// \param FlagsToExclude - Don't parse options with this flag.  Zero
00151   /// is the default and means exclude nothing.
00152   /// \return An InputArgList; on error this will contain all the options
00153   /// which could be parsed.
00154   InputArgList *ParseArgs(const char* const *ArgBegin,
00155                           const char* const *ArgEnd,
00156                           unsigned &MissingArgIndex,
00157                           unsigned &MissingArgCount,
00158                           unsigned FlagsToInclude = 0,
00159                           unsigned FlagsToExclude = 0) const;
00160 
00161   /// \brief Render the help text for an option table.
00162   ///
00163   /// \param OS - The stream to write the help text to.
00164   /// \param Name - The name to use in the usage line.
00165   /// \param Title - The title to use in the usage line.
00166   /// \param FlagsToInclude - If non-zero, only include options with any
00167   ///                         of these flags set.
00168   /// \param FlagsToExclude - Exclude options with any of these flags set.
00169   void PrintHelp(raw_ostream &OS, const char *Name,
00170                  const char *Title, unsigned FlagsToInclude,
00171                  unsigned FlagsToExclude) const;
00172 
00173   void PrintHelp(raw_ostream &OS, const char *Name,
00174                   const char *Title, bool ShowHidden = false) const;
00175 };
00176 } // end namespace opt
00177 } // end namespace llvm
00178 
00179 #endif