• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   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(const Info *OptionInfos, unsigned NumOptionInfos,
77            bool IgnoreCase = false);
78 
79 public:
80   ~OptTable();
81 
82   /// \brief Return the total number of option classes.
getNumOptions()83   unsigned getNumOptions() const { return NumOptionInfos; }
84 
85   /// \brief Get the given Opt's Option instance, lazily creating it
86   /// if necessary.
87   ///
88   /// \return The option, or null for the INVALID option id.
89   const Option getOption(OptSpecifier Opt) const;
90 
91   /// \brief Lookup the name of the given option.
getOptionName(OptSpecifier id)92   const char *getOptionName(OptSpecifier id) const {
93     return getInfo(id).Name;
94   }
95 
96   /// \brief Get the kind of the given option.
getOptionKind(OptSpecifier id)97   unsigned getOptionKind(OptSpecifier id) const {
98     return getInfo(id).Kind;
99   }
100 
101   /// \brief Get the group id for the given option.
getOptionGroupID(OptSpecifier id)102   unsigned getOptionGroupID(OptSpecifier id) const {
103     return getInfo(id).GroupID;
104   }
105 
106   /// \brief Get the help text to use to describe this option.
getOptionHelpText(OptSpecifier id)107   const char *getOptionHelpText(OptSpecifier id) const {
108     return getInfo(id).HelpText;
109   }
110 
111   /// \brief Get the meta-variable name to use when describing
112   /// this options values in the help text.
getOptionMetaVar(OptSpecifier id)113   const char *getOptionMetaVar(OptSpecifier id) const {
114     return getInfo(id).MetaVar;
115   }
116 
117   /// \brief Parse a single argument; returning the new argument and
118   /// updating Index.
119   ///
120   /// \param [in,out] Index - The current parsing position in the argument
121   /// string list; on return this will be the index of the next argument
122   /// string to parse.
123   /// \param [in] FlagsToInclude - Only parse options with any of these flags.
124   /// Zero is the default which includes all flags.
125   /// \param [in] FlagsToExclude - Don't parse options with this flag.  Zero
126   /// is the default and means exclude nothing.
127   ///
128   /// \return The parsed argument, or 0 if the argument is missing values
129   /// (in which case Index still points at the conceptual next argument string
130   /// to parse).
131   Arg *ParseOneArg(const ArgList &Args, unsigned &Index,
132                    unsigned FlagsToInclude = 0,
133                    unsigned FlagsToExclude = 0) const;
134 
135   /// \brief Parse an list of arguments into an InputArgList.
136   ///
137   /// The resulting InputArgList will reference the strings in [\p ArgBegin,
138   /// \p ArgEnd), and their lifetime should extend past that of the returned
139   /// InputArgList.
140   ///
141   /// The only error that can occur in this routine is if an argument is
142   /// missing values; in this case \p MissingArgCount will be non-zero.
143   ///
144   /// \param ArgBegin - The beginning of the argument vector.
145   /// \param ArgEnd - The end of the argument vector.
146   /// \param MissingArgIndex - On error, the index of the option which could
147   /// not be parsed.
148   /// \param MissingArgCount - On error, the number of missing options.
149   /// \param FlagsToInclude - Only parse options with any of these flags.
150   /// Zero is the default which includes all flags.
151   /// \param FlagsToExclude - Don't parse options with this flag.  Zero
152   /// is the default and means exclude nothing.
153   /// \return An InputArgList; on error this will contain all the options
154   /// which could be parsed.
155   InputArgList *ParseArgs(const char* const *ArgBegin,
156                           const char* const *ArgEnd,
157                           unsigned &MissingArgIndex,
158                           unsigned &MissingArgCount,
159                           unsigned FlagsToInclude = 0,
160                           unsigned FlagsToExclude = 0) const;
161 
162   /// \brief Render the help text for an option table.
163   ///
164   /// \param OS - The stream to write the help text to.
165   /// \param Name - The name to use in the usage line.
166   /// \param Title - The title to use in the usage line.
167   /// \param FlagsToInclude - If non-zero, only include options with any
168   ///                         of these flags set.
169   /// \param FlagsToExclude - Exclude options with any of these flags set.
170   void PrintHelp(raw_ostream &OS, const char *Name,
171                  const char *Title, unsigned FlagsToInclude,
172                  unsigned FlagsToExclude) const;
173 
174   void PrintHelp(raw_ostream &OS, const char *Name,
175                   const char *Title, bool ShowHidden = false) const;
176 };
177 } // end namespace opt
178 } // end namespace llvm
179 
180 #endif
181