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