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