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