Chris Lattner | 4848689 | 2003-09-30 18:37:50 +0000 | [diff] [blame] | 1 | //===- Support/CommandLine.h - Flexible Command line parser -----*- C++ -*-===// |
John Criswell | b2109ce | 2003-10-20 19:46:57 +0000 | [diff] [blame^] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 9 | // |
| 10 | // This class implements a command line argument processor that is useful when |
| 11 | // creating a tool. It provides a simple, minimalistic interface that is easily |
| 12 | // extensible and supports nonlocal (library) command line options. |
| 13 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 14 | // Note that rather than trying to figure out what this code does, you should |
| 15 | // read the library documentation located in docs/CommandLine.html or looks at |
| 16 | // the many example usages in tools/*/*.cpp |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Brian Gaeke | a9f6e4a | 2003-06-17 00:35:55 +0000 | [diff] [blame] | 20 | #ifndef SUPPORT_COMMANDLINE_H |
| 21 | #define SUPPORT_COMMANDLINE_H |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 22 | |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | #include <utility> |
Chris Lattner | b3b729b | 2003-05-22 20:25:57 +0000 | [diff] [blame] | 26 | #include <cstdarg> |
Chris Lattner | a44a4cd | 2003-07-25 17:23:27 +0000 | [diff] [blame] | 27 | #include <cassert> |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 28 | #include "boost/type_traits/object_traits.hpp" |
John Criswell | be583b9 | 2003-06-11 14:01:36 +0000 | [diff] [blame] | 29 | |
Chris Lattner | bf0ac3f | 2003-06-03 15:30:37 +0000 | [diff] [blame] | 30 | /// cl Namespace - This namespace contains all of the command line option |
| 31 | /// processing machinery. It is intentionally a short name to make qualified |
| 32 | /// usage concise. |
| 33 | namespace cl { |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 34 | |
| 35 | //===----------------------------------------------------------------------===// |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 36 | // ParseCommandLineOptions - Command line option processing entry point. |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 37 | // |
Chris Lattner | 20968a2 | 2003-08-14 22:04:41 +0000 | [diff] [blame] | 38 | void ParseCommandLineOptions(int &argc, char **argv, |
| 39 | const char *Overview = 0); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 40 | |
| 41 | //===----------------------------------------------------------------------===// |
Brian Gaeke | 06b06c5 | 2003-08-14 22:00:59 +0000 | [diff] [blame] | 42 | // ParseEnvironmentOptions - Environment variable option processing alternate |
| 43 | // entry point. |
| 44 | // |
Brian Gaeke | c48ef2a | 2003-08-15 21:05:57 +0000 | [diff] [blame] | 45 | void ParseEnvironmentOptions(const char *progName, const char *envvar, |
Chris Lattner | 20968a2 | 2003-08-14 22:04:41 +0000 | [diff] [blame] | 46 | const char *Overview = 0); |
Brian Gaeke | 06b06c5 | 2003-08-14 22:00:59 +0000 | [diff] [blame] | 47 | |
| 48 | //===----------------------------------------------------------------------===// |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 49 | // Flags permitted to be passed to command line arguments |
| 50 | // |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 51 | |
Misha Brukman | b5c520b | 2003-07-10 17:05:26 +0000 | [diff] [blame] | 52 | enum NumOccurrences { // Flags for the number of occurrences allowed |
| 53 | Optional = 0x01, // Zero or One occurrence |
| 54 | ZeroOrMore = 0x02, // Zero or more occurrences allowed |
| 55 | Required = 0x03, // One occurrence required |
| 56 | OneOrMore = 0x04, // One or more occurrences required |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 58 | // ConsumeAfter - Indicates that this option is fed anything that follows the |
| 59 | // last positional argument required by the application (it is an error if |
| 60 | // there are zero positional arguments, and a ConsumeAfter option is used). |
| 61 | // Thus, for example, all arguments to LLI are processed until a filename is |
| 62 | // found. Once a filename is found, all of the succeeding arguments are |
| 63 | // passed, unprocessed, to the ConsumeAfter option. |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 64 | // |
| 65 | ConsumeAfter = 0x05, |
| 66 | |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 67 | OccurrencesMask = 0x07, |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | enum ValueExpected { // Is a value required for the option? |
Chris Lattner | b3b729b | 2003-05-22 20:25:57 +0000 | [diff] [blame] | 71 | ValueOptional = 0x08, // The value can appear... or not |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 72 | ValueRequired = 0x10, // The value is required to appear! |
| 73 | ValueDisallowed = 0x18, // A value may not be specified (for flags) |
| 74 | ValueMask = 0x18, |
| 75 | }; |
| 76 | |
| 77 | enum OptionHidden { // Control whether -help shows this option |
| 78 | NotHidden = 0x20, // Option included in --help & --help-hidden |
| 79 | Hidden = 0x40, // -help doesn't, but --help-hidden does |
| 80 | ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg |
| 81 | HiddenMask = 0x60, |
| 82 | }; |
| 83 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 84 | // Formatting flags - This controls special features that the option might have |
| 85 | // that cause it to be parsed differently... |
| 86 | // |
| 87 | // Prefix - This option allows arguments that are otherwise unrecognized to be |
| 88 | // matched by options that are a prefix of the actual value. This is useful for |
| 89 | // cases like a linker, where options are typically of the form '-lfoo' or |
| 90 | // '-L../../include' where -l or -L are the actual flags. When prefix is |
| 91 | // enabled, and used, the value for the flag comes from the suffix of the |
| 92 | // argument. |
| 93 | // |
| 94 | // Grouping - With this option enabled, multiple letter options are allowed to |
| 95 | // bunch together with only a single hyphen for the whole group. This allows |
| 96 | // emulation of the behavior that ls uses for example: ls -la === ls -l -a |
| 97 | // |
| 98 | |
| 99 | enum FormattingFlags { |
| 100 | NormalFormatting = 0x000, // Nothing special |
| 101 | Positional = 0x080, // Is a positional argument, no '-' required |
| 102 | Prefix = 0x100, // Can this option directly prefix its value? |
| 103 | Grouping = 0x180, // Can this option group with other options? |
| 104 | FormattingMask = 0x180, |
| 105 | }; |
| 106 | |
Chris Lattner | b3b729b | 2003-05-22 20:25:57 +0000 | [diff] [blame] | 107 | enum MiscFlags { // Miscellaneous flags to adjust argument |
| 108 | CommaSeparated = 0x200, // Should this cl::list split between commas? |
| 109 | MiscMask = 0x200, |
| 110 | }; |
| 111 | |
| 112 | |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 113 | |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | // Option Base class |
| 116 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 117 | class alias; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 118 | class Option { |
| 119 | friend void cl::ParseCommandLineOptions(int &, char **, const char *, int); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 120 | friend class alias; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 121 | |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 122 | // handleOccurrences - Overriden by subclasses to handle the value passed into |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 123 | // an argument. Should return true if there was an error processing the |
| 124 | // argument and the program should exit. |
| 125 | // |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 126 | virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) = 0; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 127 | |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 128 | virtual enum NumOccurrences getNumOccurrencesFlagDefault() const { |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 129 | return Optional; |
| 130 | } |
| 131 | virtual enum ValueExpected getValueExpectedFlagDefault() const { |
| 132 | return ValueOptional; |
| 133 | } |
| 134 | virtual enum OptionHidden getOptionHiddenFlagDefault() const { |
| 135 | return NotHidden; |
| 136 | } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 137 | virtual enum FormattingFlags getFormattingFlagDefault() const { |
| 138 | return NormalFormatting; |
| 139 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 140 | |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 141 | int NumOccurrences; // The number of times specified |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 142 | int Flags; // Flags for the argument |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 143 | public: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 144 | const char *ArgStr; // The argument string itself (ex: "help", "o") |
| 145 | const char *HelpStr; // The descriptive text message for --help |
| 146 | const char *ValueStr; // String describing what the value of this option is |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 147 | |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 148 | inline enum NumOccurrences getNumOccurrencesFlag() const { |
| 149 | int NO = Flags & OccurrencesMask; |
| 150 | return NO ? (enum NumOccurrences)NO : getNumOccurrencesFlagDefault(); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 151 | } |
| 152 | inline enum ValueExpected getValueExpectedFlag() const { |
| 153 | int VE = Flags & ValueMask; |
| 154 | return VE ? (enum ValueExpected)VE : getValueExpectedFlagDefault(); |
| 155 | } |
| 156 | inline enum OptionHidden getOptionHiddenFlag() const { |
| 157 | int OH = Flags & HiddenMask; |
| 158 | return OH ? (enum OptionHidden)OH : getOptionHiddenFlagDefault(); |
| 159 | } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 160 | inline enum FormattingFlags getFormattingFlag() const { |
| 161 | int OH = Flags & FormattingMask; |
| 162 | return OH ? (enum FormattingFlags)OH : getFormattingFlagDefault(); |
| 163 | } |
Chris Lattner | b3b729b | 2003-05-22 20:25:57 +0000 | [diff] [blame] | 164 | inline unsigned getMiscFlags() const { |
| 165 | return Flags & MiscMask; |
| 166 | } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 167 | |
| 168 | // hasArgStr - Return true if the argstr != "" |
| 169 | bool hasArgStr() const { return ArgStr[0] != 0; } |
| 170 | |
| 171 | //-------------------------------------------------------------------------=== |
| 172 | // Accessor functions set by OptionModifiers |
| 173 | // |
| 174 | void setArgStr(const char *S) { ArgStr = S; } |
| 175 | void setDescription(const char *S) { HelpStr = S; } |
| 176 | void setValueStr(const char *S) { ValueStr = S; } |
| 177 | |
| 178 | void setFlag(unsigned Flag, unsigned FlagMask) { |
| 179 | if (Flags & FlagMask) { |
| 180 | error(": Specified two settings for the same option!"); |
| 181 | exit(1); |
| 182 | } |
| 183 | |
| 184 | Flags |= Flag; |
| 185 | } |
| 186 | |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 187 | void setNumOccurrencesFlag(enum NumOccurrences Val) { |
| 188 | setFlag(Val, OccurrencesMask); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 189 | } |
| 190 | void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); } |
| 191 | void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); } |
| 192 | void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); } |
Chris Lattner | b3b729b | 2003-05-22 20:25:57 +0000 | [diff] [blame] | 193 | void setMiscFlag(enum MiscFlags M) { setFlag(M, M); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 194 | protected: |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 195 | Option() : NumOccurrences(0), Flags(0), |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 196 | ArgStr(""), HelpStr(""), ValueStr("") {} |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 197 | |
| 198 | public: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 199 | // addArgument - Tell the system that this Option subclass will handle all |
Misha Brukman | b5c520b | 2003-07-10 17:05:26 +0000 | [diff] [blame] | 200 | // occurrences of -ArgStr on the command line. |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 201 | // |
| 202 | void addArgument(const char *ArgStr); |
Chris Lattner | ae1257a | 2002-07-23 00:44:37 +0000 | [diff] [blame] | 203 | void removeArgument(const char *ArgStr); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 204 | |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 205 | // Return the width of the option tag for printing... |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 206 | virtual unsigned getOptionWidth() const = 0; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 207 | |
| 208 | // printOptionInfo - Print out information about this option. The |
| 209 | // to-be-maintained width is specified. |
| 210 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 211 | virtual void printOptionInfo(unsigned GlobalWidth) const = 0; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 212 | |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 213 | // addOccurrence - Wrapper around handleOccurrence that enforces Flags |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 214 | // |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 215 | bool addOccurrence(const char *ArgName, const std::string &Value); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 216 | |
| 217 | // Prints option name followed by message. Always returns true. |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 218 | bool error(std::string Message, const char *ArgName = 0); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 219 | |
| 220 | public: |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 221 | inline int getNumOccurrences() const { return NumOccurrences; } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 222 | virtual ~Option() {} |
| 223 | }; |
| 224 | |
| 225 | |
| 226 | //===----------------------------------------------------------------------===// |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 227 | // Command line option modifiers that can be used to modify the behavior of |
| 228 | // command line option parsers... |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 229 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 230 | |
| 231 | // desc - Modifier to set the description shown in the --help output... |
| 232 | struct desc { |
| 233 | const char *Desc; |
| 234 | desc(const char *Str) : Desc(Str) {} |
| 235 | void apply(Option &O) const { O.setDescription(Desc); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 238 | // value_desc - Modifier to set the value description shown in the --help |
| 239 | // output... |
| 240 | struct value_desc { |
| 241 | const char *Desc; |
| 242 | value_desc(const char *Str) : Desc(Str) {} |
| 243 | void apply(Option &O) const { O.setValueStr(Desc); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 247 | // init - Specify a default (initial) value for the command line argument, if |
| 248 | // the default constructor for the argument type does not give you what you |
| 249 | // want. This is only valid on "opt" arguments, not on "list" arguments. |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 250 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 251 | template<class Ty> |
| 252 | struct initializer { |
| 253 | const Ty &Init; |
| 254 | initializer(const Ty &Val) : Init(Val) {} |
| 255 | |
| 256 | template<class Opt> |
| 257 | void apply(Opt &O) const { O.setInitialValue(Init); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 260 | template<class Ty> |
| 261 | initializer<Ty> init(const Ty &Val) { |
| 262 | return initializer<Ty>(Val); |
| 263 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 265 | |
| 266 | // location - Allow the user to specify which external variable they want to |
| 267 | // store the results of the command line argument processing into, if they don't |
| 268 | // want to store it in the option itself. |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 269 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 270 | template<class Ty> |
| 271 | struct LocationClass { |
| 272 | Ty &Loc; |
| 273 | LocationClass(Ty &L) : Loc(L) {} |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 275 | template<class Opt> |
| 276 | void apply(Opt &O) const { O.setLocation(O, Loc); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 279 | template<class Ty> |
| 280 | LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 281 | |
| 282 | |
| 283 | //===----------------------------------------------------------------------===// |
| 284 | // Enum valued command line option |
| 285 | // |
Chris Lattner | ae1257a | 2002-07-23 00:44:37 +0000 | [diff] [blame] | 286 | #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC |
| 287 | #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 288 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 289 | // values - For custom data types, allow specifying a group of values together |
| 290 | // as the values that go into the mapping that the option handler uses. Note |
| 291 | // that the values list must always have a 0 at the end of the list to indicate |
| 292 | // that the list has ended. |
| 293 | // |
| 294 | template<class DataType> |
| 295 | class ValuesClass { |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 296 | // Use a vector instead of a map, because the lists should be short, |
| 297 | // the overhead is less, and most importantly, it keeps them in the order |
| 298 | // inserted so we can print our option out nicely. |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 299 | std::vector<std::pair<const char *, std::pair<int, const char *> > > Values; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 300 | void processValues(va_list Vals); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 301 | public: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 302 | ValuesClass(const char *EnumName, DataType Val, const char *Desc, |
| 303 | va_list ValueArgs) { |
| 304 | // Insert the first value, which is required. |
| 305 | Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc))); |
| 306 | |
| 307 | // Process the varargs portion of the values... |
| 308 | while (const char *EnumName = va_arg(ValueArgs, const char *)) { |
Chris Lattner | ae1257a | 2002-07-23 00:44:37 +0000 | [diff] [blame] | 309 | DataType EnumVal = (DataType)va_arg(ValueArgs, int); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 310 | const char *EnumDesc = va_arg(ValueArgs, const char *); |
| 311 | Values.push_back(std::make_pair(EnumName, // Add value to value map |
| 312 | std::make_pair(EnumVal, EnumDesc))); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | template<class Opt> |
| 317 | void apply(Opt &O) const { |
| 318 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 319 | O.getParser().addLiteralOption(Values[i].first, Values[i].second.first, |
| 320 | Values[i].second.second); |
| 321 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 322 | }; |
| 323 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 324 | template<class DataType> |
| 325 | ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc, |
| 326 | ...) { |
| 327 | va_list ValueArgs; |
| 328 | va_start(ValueArgs, Desc); |
| 329 | ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs); |
| 330 | va_end(ValueArgs); |
| 331 | return Vals; |
| 332 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 334 | |
| 335 | //===----------------------------------------------------------------------===// |
| 336 | // parser class - Parameterizable parser for different data types. By default, |
| 337 | // known data types (string, int, bool) have specialized parsers, that do what |
| 338 | // you would expect. The default parser, used for data types that are not |
| 339 | // built-in, uses a mapping table to map specific options to values, which is |
| 340 | // used, among other things, to handle enum types. |
| 341 | |
| 342 | //-------------------------------------------------- |
| 343 | // generic_parser_base - This class holds all the non-generic code that we do |
| 344 | // not need replicated for every instance of the generic parser. This also |
| 345 | // allows us to put stuff into CommandLine.cpp |
| 346 | // |
| 347 | struct generic_parser_base { |
| 348 | virtual ~generic_parser_base() {} // Base class should have virtual-dtor |
| 349 | |
| 350 | // getNumOptions - Virtual function implemented by generic subclass to |
| 351 | // indicate how many entries are in Values. |
| 352 | // |
| 353 | virtual unsigned getNumOptions() const = 0; |
| 354 | |
| 355 | // getOption - Return option name N. |
| 356 | virtual const char *getOption(unsigned N) const = 0; |
| 357 | |
| 358 | // getDescription - Return description N |
| 359 | virtual const char *getDescription(unsigned N) const = 0; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 360 | |
| 361 | // Return the width of the option tag for printing... |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 362 | virtual unsigned getOptionWidth(const Option &O) const; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 363 | |
| 364 | // printOptionInfo - Print out information about this option. The |
| 365 | // to-be-maintained width is specified. |
| 366 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 367 | virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const; |
Chris Lattner | 71fb716 | 2002-05-22 17:03:05 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 369 | void initialize(Option &O) { |
| 370 | // All of the modifiers for the option have been processed by now, so the |
| 371 | // argstr field should be stable, copy it down now. |
| 372 | // |
| 373 | hasArgStr = O.hasArgStr(); |
| 374 | |
| 375 | // If there has been no argstr specified, that means that we need to add an |
| 376 | // argument for every possible option. This ensures that our options are |
| 377 | // vectored to us. |
| 378 | // |
| 379 | if (!hasArgStr) |
| 380 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) |
| 381 | O.addArgument(getOption(i)); |
| 382 | } |
| 383 | |
| 384 | enum ValueExpected getValueExpectedFlagDefault() const { |
| 385 | // If there is an ArgStr specified, then we are of the form: |
| 386 | // |
| 387 | // -opt=O2 or -opt O2 or -optO2 |
| 388 | // |
| 389 | // In which case, the value is required. Otherwise if an arg str has not |
| 390 | // been specified, we are of the form: |
| 391 | // |
Misha Brukman | bc0e998 | 2003-07-14 17:20:40 +0000 | [diff] [blame] | 392 | // -O2 or O2 or -la (where -l and -a are separate options) |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 393 | // |
| 394 | // If this is the case, we cannot allow a value. |
| 395 | // |
| 396 | if (hasArgStr) |
| 397 | return ValueRequired; |
| 398 | else |
| 399 | return ValueDisallowed; |
| 400 | } |
| 401 | |
Chris Lattner | af7e821 | 2002-07-23 17:15:09 +0000 | [diff] [blame] | 402 | // findOption - Return the option number corresponding to the specified |
| 403 | // argument string. If the option is not found, getNumOptions() is returned. |
| 404 | // |
| 405 | unsigned findOption(const char *Name); |
| 406 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 407 | protected: |
| 408 | bool hasArgStr; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 411 | // Default parser implementation - This implementation depends on having a |
| 412 | // mapping of recognized options to values of some sort. In addition to this, |
| 413 | // each entry in the mapping also tracks a help message that is printed with the |
| 414 | // command line option for --help. Because this is a simple mapping parser, the |
| 415 | // data type can be any unsupported type. |
| 416 | // |
| 417 | template <class DataType> |
| 418 | class parser : public generic_parser_base { |
Chris Lattner | af7e821 | 2002-07-23 17:15:09 +0000 | [diff] [blame] | 419 | protected: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 420 | std::vector<std::pair<const char *, |
| 421 | std::pair<DataType, const char *> > > Values; |
Chris Lattner | af7e821 | 2002-07-23 17:15:09 +0000 | [diff] [blame] | 422 | public: |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 423 | typedef DataType parser_data_type; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 424 | |
| 425 | // Implement virtual functions needed by generic_parser_base |
| 426 | unsigned getNumOptions() const { return Values.size(); } |
| 427 | const char *getOption(unsigned N) const { return Values[N].first; } |
| 428 | const char *getDescription(unsigned N) const { |
| 429 | return Values[N].second.second; |
| 430 | } |
| 431 | |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 432 | // parse - Return true on error. |
| 433 | bool parse(Option &O, const char *ArgName, const std::string &Arg, |
| 434 | DataType &V) { |
Chris Lattner | 7f4dd47 | 2002-07-24 22:08:36 +0000 | [diff] [blame] | 435 | std::string ArgVal; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 436 | if (hasArgStr) |
| 437 | ArgVal = Arg; |
| 438 | else |
| 439 | ArgVal = ArgName; |
| 440 | |
| 441 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 442 | if (ArgVal == Values[i].first) { |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 443 | V = Values[i].second.first; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 444 | return false; |
| 445 | } |
| 446 | |
| 447 | return O.error(": Cannot find option named '" + ArgVal + "'!"); |
| 448 | } |
| 449 | |
| 450 | // addLiteralOption - Add an entry to the mapping table... |
| 451 | template <class DT> |
| 452 | void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) { |
Chris Lattner | af7e821 | 2002-07-23 17:15:09 +0000 | [diff] [blame] | 453 | assert(findOption(Name) == Values.size() && "Option already exists!"); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 454 | Values.push_back(std::make_pair(Name, std::make_pair((DataType)V,HelpStr))); |
| 455 | } |
Chris Lattner | af7e821 | 2002-07-23 17:15:09 +0000 | [diff] [blame] | 456 | |
| 457 | // removeLiteralOption - Remove the specified option. |
| 458 | // |
| 459 | void removeLiteralOption(const char *Name) { |
| 460 | unsigned N = findOption(Name); |
| 461 | assert(N != Values.size() && "Option not found!"); |
| 462 | Values.erase(Values.begin()+N); |
| 463 | } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 464 | }; |
| 465 | |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 466 | //-------------------------------------------------- |
| 467 | // basic_parser - Super class of parsers to provide boilerplate code |
| 468 | // |
| 469 | struct basic_parser_impl { // non-template implementation of basic_parser<t> |
| 470 | virtual ~basic_parser_impl() {} |
| 471 | |
| 472 | enum ValueExpected getValueExpectedFlagDefault() const { |
| 473 | return ValueRequired; |
| 474 | } |
| 475 | |
| 476 | void initialize(Option &O) {} |
| 477 | |
| 478 | // Return the width of the option tag for printing... |
| 479 | unsigned getOptionWidth(const Option &O) const; |
| 480 | |
| 481 | // printOptionInfo - Print out information about this option. The |
| 482 | // to-be-maintained width is specified. |
| 483 | // |
| 484 | void printOptionInfo(const Option &O, unsigned GlobalWidth) const; |
| 485 | |
| 486 | |
| 487 | // getValueName - Overload in subclass to provide a better default value. |
| 488 | virtual const char *getValueName() const { return "value"; } |
| 489 | }; |
| 490 | |
| 491 | // basic_parser - The real basic parser is just a template wrapper that provides |
| 492 | // a typedef for the provided data type. |
| 493 | // |
| 494 | template<class DataType> |
| 495 | struct basic_parser : public basic_parser_impl { |
| 496 | typedef DataType parser_data_type; |
| 497 | }; |
| 498 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 499 | |
| 500 | //-------------------------------------------------- |
| 501 | // parser<bool> |
| 502 | // |
| 503 | template<> |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 504 | struct parser<bool> : public basic_parser<bool> { |
| 505 | |
| 506 | // parse - Return true on error. |
| 507 | bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 508 | |
| 509 | enum ValueExpected getValueExpectedFlagDefault() const { |
| 510 | return ValueOptional; |
| 511 | } |
| 512 | |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 513 | // getValueName - Do not print =<value> at all |
| 514 | virtual const char *getValueName() const { return 0; } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 515 | }; |
| 516 | |
| 517 | |
| 518 | //-------------------------------------------------- |
| 519 | // parser<int> |
| 520 | // |
| 521 | template<> |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 522 | struct parser<int> : public basic_parser<int> { |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 523 | |
| 524 | // parse - Return true on error. |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 525 | bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 526 | |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 527 | // getValueName - Overload in subclass to provide a better default value. |
| 528 | virtual const char *getValueName() const { return "int"; } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 529 | }; |
| 530 | |
| 531 | |
| 532 | //-------------------------------------------------- |
Chris Lattner | d2a6fc3 | 2003-06-28 15:47:20 +0000 | [diff] [blame] | 533 | // parser<unsigned> |
| 534 | // |
| 535 | template<> |
| 536 | struct parser<unsigned> : public basic_parser<unsigned> { |
| 537 | |
| 538 | // parse - Return true on error. |
| 539 | bool parse(Option &O, const char *ArgName, const std::string &Arg, |
| 540 | unsigned &Val); |
| 541 | |
| 542 | // getValueName - Overload in subclass to provide a better default value. |
| 543 | virtual const char *getValueName() const { return "uint"; } |
| 544 | }; |
| 545 | |
| 546 | |
| 547 | //-------------------------------------------------- |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 548 | // parser<double> |
| 549 | // |
| 550 | template<> |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 551 | struct parser<double> : public basic_parser<double> { |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 552 | // parse - Return true on error. |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 553 | bool parse(Option &O, const char *AN, const std::string &Arg, double &Val); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 554 | |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 555 | // getValueName - Overload in subclass to provide a better default value. |
| 556 | virtual const char *getValueName() const { return "number"; } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 557 | }; |
| 558 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 559 | |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 560 | //-------------------------------------------------- |
| 561 | // parser<float> |
| 562 | // |
| 563 | template<> |
| 564 | struct parser<float> : public basic_parser<float> { |
| 565 | // parse - Return true on error. |
| 566 | bool parse(Option &O, const char *AN, const std::string &Arg, float &Val); |
| 567 | |
| 568 | // getValueName - Overload in subclass to provide a better default value. |
| 569 | virtual const char *getValueName() const { return "number"; } |
| 570 | }; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 571 | |
| 572 | |
| 573 | //-------------------------------------------------- |
Chris Lattner | 7f4dd47 | 2002-07-24 22:08:36 +0000 | [diff] [blame] | 574 | // parser<std::string> |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 575 | // |
| 576 | template<> |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 577 | struct parser<std::string> : public basic_parser<std::string> { |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 578 | // parse - Return true on error. |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 579 | bool parse(Option &O, const char *ArgName, const std::string &Arg, |
| 580 | std::string &Value) { |
| 581 | Value = Arg; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 582 | return false; |
| 583 | } |
Chris Lattner | 71fb716 | 2002-05-22 17:03:05 +0000 | [diff] [blame] | 584 | |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 585 | // getValueName - Overload in subclass to provide a better default value. |
| 586 | virtual const char *getValueName() const { return "string"; } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 587 | }; |
| 588 | |
Chris Lattner | 71fb716 | 2002-05-22 17:03:05 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 590 | |
| 591 | //===----------------------------------------------------------------------===// |
| 592 | // applicator class - This class is used because we must use partial |
| 593 | // specialization to handle literal string arguments specially (const char* does |
| 594 | // not correctly respond to the apply method). Because the syntax to use this |
| 595 | // is a pain, we have the 'apply' method below to handle the nastiness... |
| 596 | // |
| 597 | template<class Mod> struct applicator { |
| 598 | template<class Opt> |
| 599 | static void opt(const Mod &M, Opt &O) { M.apply(O); } |
| 600 | }; |
| 601 | |
| 602 | // Handle const char* as a special case... |
| 603 | template<unsigned n> struct applicator<char[n]> { |
| 604 | template<class Opt> |
| 605 | static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } |
| 606 | }; |
Chris Lattner | 4042332 | 2002-09-13 14:33:39 +0000 | [diff] [blame] | 607 | template<unsigned n> struct applicator<const char[n]> { |
| 608 | template<class Opt> |
| 609 | static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } |
| 610 | }; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 611 | template<> struct applicator<const char*> { |
| 612 | template<class Opt> |
| 613 | static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } |
| 614 | }; |
| 615 | |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 616 | template<> struct applicator<NumOccurrences> { |
| 617 | static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 618 | }; |
| 619 | template<> struct applicator<ValueExpected> { |
| 620 | static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); } |
| 621 | }; |
| 622 | template<> struct applicator<OptionHidden> { |
| 623 | static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); } |
| 624 | }; |
| 625 | template<> struct applicator<FormattingFlags> { |
| 626 | static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); } |
| 627 | }; |
Chris Lattner | b3b729b | 2003-05-22 20:25:57 +0000 | [diff] [blame] | 628 | template<> struct applicator<MiscFlags> { |
| 629 | static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); } |
| 630 | }; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 631 | |
| 632 | // apply method - Apply a modifier to an option in a type safe way. |
| 633 | template<class Mod, class Opt> |
| 634 | void apply(const Mod &M, Opt *O) { |
| 635 | applicator<Mod>::opt(M, *O); |
| 636 | } |
| 637 | |
| 638 | |
| 639 | //===----------------------------------------------------------------------===// |
| 640 | // opt_storage class |
| 641 | |
| 642 | // Default storage class definition: external storage. This implementation |
| 643 | // assumes the user will specify a variable to store the data into with the |
| 644 | // cl::location(x) modifier. |
| 645 | // |
| 646 | template<class DataType, bool ExternalStorage, bool isClass> |
| 647 | class opt_storage { |
| 648 | DataType *Location; // Where to store the object... |
| 649 | |
| 650 | void check() { |
| 651 | assert(Location != 0 && "cl::location(...) not specified for a command " |
Chris Lattner | 3c7eb1f | 2003-08-19 21:57:00 +0000 | [diff] [blame] | 652 | "line option with external storage, " |
| 653 | "or cl::init specified before cl::location()!!"); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 654 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 655 | public: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 656 | opt_storage() : Location(0) {} |
| 657 | |
| 658 | bool setLocation(Option &O, DataType &L) { |
| 659 | if (Location) |
| 660 | return O.error(": cl::location(x) specified more than once!"); |
| 661 | Location = &L; |
| 662 | return false; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 665 | template<class T> |
| 666 | void setValue(const T &V) { |
| 667 | check(); |
| 668 | *Location = V; |
| 669 | } |
| 670 | |
| 671 | DataType &getValue() { check(); return *Location; } |
| 672 | const DataType &getValue() const { check(); return *Location; } |
| 673 | }; |
| 674 | |
| 675 | |
| 676 | // Define how to hold a class type object, such as a string. Since we can |
| 677 | // inherit from a class, we do so. This makes us exactly compatible with the |
| 678 | // object in all cases that it is used. |
| 679 | // |
| 680 | template<class DataType> |
| 681 | struct opt_storage<DataType,false,true> : public DataType { |
| 682 | |
| 683 | template<class T> |
| 684 | void setValue(const T &V) { DataType::operator=(V); } |
| 685 | |
| 686 | DataType &getValue() { return *this; } |
| 687 | const DataType &getValue() const { return *this; } |
| 688 | }; |
| 689 | |
| 690 | // Define a partial specialization to handle things we cannot inherit from. In |
| 691 | // this case, we store an instance through containment, and overload operators |
| 692 | // to get at the value. |
| 693 | // |
| 694 | template<class DataType> |
| 695 | struct opt_storage<DataType, false, false> { |
| 696 | DataType Value; |
| 697 | |
| 698 | // Make sure we initialize the value with the default constructor for the |
| 699 | // type. |
| 700 | opt_storage() : Value(DataType()) {} |
| 701 | |
| 702 | template<class T> |
| 703 | void setValue(const T &V) { Value = V; } |
| 704 | DataType &getValue() { return Value; } |
| 705 | DataType getValue() const { return Value; } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 706 | }; |
| 707 | |
| 708 | |
| 709 | //===----------------------------------------------------------------------===// |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 710 | // opt - A scalar command line option. |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 711 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 712 | template <class DataType, bool ExternalStorage = false, |
| 713 | class ParserClass = parser<DataType> > |
| 714 | class opt : public Option, |
| 715 | public opt_storage<DataType, ExternalStorage, |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 716 | ::boost::is_class<DataType>::value> { |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 717 | ParserClass Parser; |
| 718 | |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 719 | virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) { |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 720 | typename ParserClass::parser_data_type Val; |
| 721 | if (Parser.parse(*this, ArgName, Arg, Val)) |
| 722 | return true; // Parse error! |
| 723 | setValue(Val); |
| 724 | return false; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | virtual enum ValueExpected getValueExpectedFlagDefault() const { |
| 728 | return Parser.getValueExpectedFlagDefault(); |
| 729 | } |
| 730 | |
| 731 | // Forward printing stuff to the parser... |
| 732 | virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);} |
| 733 | virtual void printOptionInfo(unsigned GlobalWidth) const { |
| 734 | Parser.printOptionInfo(*this, GlobalWidth); |
| 735 | } |
| 736 | |
| 737 | void done() { |
| 738 | addArgument(ArgStr); |
| 739 | Parser.initialize(*this); |
| 740 | } |
| 741 | public: |
| 742 | // setInitialValue - Used by the cl::init modifier... |
Chris Lattner | 31a6db0 | 2003-08-22 14:26:59 +0000 | [diff] [blame] | 743 | void setInitialValue(const DataType &V) { this->setValue(V); } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 744 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 745 | ParserClass &getParser() { return Parser; } |
| 746 | |
Chris Lattner | 31a6db0 | 2003-08-22 14:26:59 +0000 | [diff] [blame] | 747 | operator DataType() const { return this->getValue(); } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 748 | |
| 749 | template<class T> |
Chris Lattner | 31a6db0 | 2003-08-22 14:26:59 +0000 | [diff] [blame] | 750 | DataType &operator=(const T &Val) { |
| 751 | this->setValue(Val); |
| 752 | return this->getValue(); |
| 753 | } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 754 | |
| 755 | // One option... |
| 756 | template<class M0t> |
| 757 | opt(const M0t &M0) { |
| 758 | apply(M0, this); |
| 759 | done(); |
| 760 | } |
| 761 | |
| 762 | // Two options... |
| 763 | template<class M0t, class M1t> |
| 764 | opt(const M0t &M0, const M1t &M1) { |
| 765 | apply(M0, this); apply(M1, this); |
| 766 | done(); |
| 767 | } |
| 768 | |
| 769 | // Three options... |
| 770 | template<class M0t, class M1t, class M2t> |
| 771 | opt(const M0t &M0, const M1t &M1, const M2t &M2) { |
| 772 | apply(M0, this); apply(M1, this); apply(M2, this); |
| 773 | done(); |
| 774 | } |
| 775 | // Four options... |
| 776 | template<class M0t, class M1t, class M2t, class M3t> |
| 777 | opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) { |
| 778 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 779 | done(); |
| 780 | } |
| 781 | // Five options... |
| 782 | template<class M0t, class M1t, class M2t, class M3t, class M4t> |
| 783 | opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, |
| 784 | const M4t &M4) { |
| 785 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 786 | apply(M4, this); |
| 787 | done(); |
| 788 | } |
| 789 | // Six options... |
| 790 | template<class M0t, class M1t, class M2t, class M3t, |
| 791 | class M4t, class M5t> |
| 792 | opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, |
| 793 | const M4t &M4, const M5t &M5) { |
| 794 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 795 | apply(M4, this); apply(M5, this); |
| 796 | done(); |
| 797 | } |
| 798 | // Seven options... |
| 799 | template<class M0t, class M1t, class M2t, class M3t, |
| 800 | class M4t, class M5t, class M6t> |
| 801 | opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, |
| 802 | const M4t &M4, const M5t &M5, const M6t &M6) { |
| 803 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 804 | apply(M4, this); apply(M5, this); apply(M6, this); |
| 805 | done(); |
| 806 | } |
| 807 | // Eight options... |
| 808 | template<class M0t, class M1t, class M2t, class M3t, |
| 809 | class M4t, class M5t, class M6t, class M7t> |
| 810 | opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, |
| 811 | const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) { |
| 812 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 813 | apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); |
| 814 | done(); |
| 815 | } |
| 816 | }; |
| 817 | |
| 818 | //===----------------------------------------------------------------------===// |
| 819 | // list_storage class |
| 820 | |
| 821 | // Default storage class definition: external storage. This implementation |
| 822 | // assumes the user will specify a variable to store the data into with the |
| 823 | // cl::location(x) modifier. |
| 824 | // |
| 825 | template<class DataType, class StorageClass> |
| 826 | class list_storage { |
| 827 | StorageClass *Location; // Where to store the object... |
| 828 | |
| 829 | public: |
| 830 | list_storage() : Location(0) {} |
| 831 | |
| 832 | bool setLocation(Option &O, StorageClass &L) { |
| 833 | if (Location) |
| 834 | return O.error(": cl::location(x) specified more than once!"); |
| 835 | Location = &L; |
| 836 | return false; |
| 837 | } |
| 838 | |
| 839 | template<class T> |
| 840 | void addValue(const T &V) { |
| 841 | assert(Location != 0 && "cl::location(...) not specified for a command " |
| 842 | "line option with external storage!"); |
| 843 | Location->push_back(V); |
| 844 | } |
| 845 | }; |
| 846 | |
| 847 | |
| 848 | // Define how to hold a class type object, such as a string. Since we can |
| 849 | // inherit from a class, we do so. This makes us exactly compatible with the |
| 850 | // object in all cases that it is used. |
| 851 | // |
| 852 | template<class DataType> |
| 853 | struct list_storage<DataType, bool> : public std::vector<DataType> { |
| 854 | |
| 855 | template<class T> |
| 856 | void addValue(const T &V) { push_back(V); } |
| 857 | }; |
| 858 | |
| 859 | |
| 860 | //===----------------------------------------------------------------------===// |
| 861 | // list - A list of command line options. |
| 862 | // |
| 863 | template <class DataType, class Storage = bool, |
| 864 | class ParserClass = parser<DataType> > |
| 865 | class list : public Option, public list_storage<DataType, Storage> { |
| 866 | ParserClass Parser; |
| 867 | |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 868 | virtual enum NumOccurrences getNumOccurrencesFlagDefault() const { |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 869 | return ZeroOrMore; |
| 870 | } |
| 871 | virtual enum ValueExpected getValueExpectedFlagDefault() const { |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 872 | return Parser.getValueExpectedFlagDefault(); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 873 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 874 | |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 875 | virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) { |
Chris Lattner | d23a35b | 2002-08-07 18:36:27 +0000 | [diff] [blame] | 876 | typename ParserClass::parser_data_type Val; |
| 877 | if (Parser.parse(*this, ArgName, Arg, Val)) |
| 878 | return true; // Parse Error! |
| 879 | addValue(Val); |
| 880 | return false; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 881 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 882 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 883 | // Forward printing stuff to the parser... |
| 884 | virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);} |
| 885 | virtual void printOptionInfo(unsigned GlobalWidth) const { |
| 886 | Parser.printOptionInfo(*this, GlobalWidth); |
| 887 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 888 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 889 | void done() { |
| 890 | addArgument(ArgStr); |
| 891 | Parser.initialize(*this); |
| 892 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 893 | public: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 894 | ParserClass &getParser() { return Parser; } |
| 895 | |
| 896 | // One option... |
| 897 | template<class M0t> |
| 898 | list(const M0t &M0) { |
| 899 | apply(M0, this); |
| 900 | done(); |
| 901 | } |
| 902 | // Two options... |
| 903 | template<class M0t, class M1t> |
| 904 | list(const M0t &M0, const M1t &M1) { |
| 905 | apply(M0, this); apply(M1, this); |
| 906 | done(); |
| 907 | } |
| 908 | // Three options... |
| 909 | template<class M0t, class M1t, class M2t> |
| 910 | list(const M0t &M0, const M1t &M1, const M2t &M2) { |
| 911 | apply(M0, this); apply(M1, this); apply(M2, this); |
| 912 | done(); |
| 913 | } |
| 914 | // Four options... |
| 915 | template<class M0t, class M1t, class M2t, class M3t> |
| 916 | list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) { |
| 917 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 918 | done(); |
| 919 | } |
| 920 | // Five options... |
| 921 | template<class M0t, class M1t, class M2t, class M3t, class M4t> |
| 922 | list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, |
| 923 | const M4t &M4) { |
| 924 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 925 | apply(M4, this); |
| 926 | done(); |
| 927 | } |
| 928 | // Six options... |
| 929 | template<class M0t, class M1t, class M2t, class M3t, |
| 930 | class M4t, class M5t> |
| 931 | list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, |
| 932 | const M4t &M4, const M5t &M5) { |
| 933 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 934 | apply(M4, this); apply(M5, this); |
| 935 | done(); |
| 936 | } |
| 937 | // Seven options... |
| 938 | template<class M0t, class M1t, class M2t, class M3t, |
| 939 | class M4t, class M5t, class M6t> |
| 940 | list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, |
| 941 | const M4t &M4, const M5t &M5, const M6t &M6) { |
| 942 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 943 | apply(M4, this); apply(M5, this); apply(M6, this); |
| 944 | done(); |
| 945 | } |
| 946 | // Eight options... |
| 947 | template<class M0t, class M1t, class M2t, class M3t, |
| 948 | class M4t, class M5t, class M6t, class M7t> |
| 949 | list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, |
| 950 | const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) { |
| 951 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 952 | apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); |
| 953 | done(); |
| 954 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 955 | }; |
| 956 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 957 | |
| 958 | |
| 959 | //===----------------------------------------------------------------------===// |
| 960 | // Aliased command line option (alias this name to a preexisting name) |
| 961 | // |
| 962 | |
| 963 | class alias : public Option { |
| 964 | Option *AliasFor; |
Misha Brukman | dd6cb6a | 2003-07-10 16:49:51 +0000 | [diff] [blame] | 965 | virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) { |
| 966 | return AliasFor->handleOccurrence(AliasFor->ArgStr, Arg); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 967 | } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 968 | // Aliases default to be hidden... |
| 969 | virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;} |
| 970 | |
| 971 | // Handle printing stuff... |
| 972 | virtual unsigned getOptionWidth() const; |
| 973 | virtual void printOptionInfo(unsigned GlobalWidth) const; |
| 974 | |
| 975 | void done() { |
| 976 | if (!hasArgStr()) |
| 977 | error(": cl::alias must have argument name specified!"); |
| 978 | if (AliasFor == 0) |
| 979 | error(": cl::alias must have an cl::aliasopt(option) specified!"); |
| 980 | addArgument(ArgStr); |
| 981 | } |
| 982 | public: |
| 983 | void setAliasFor(Option &O) { |
| 984 | if (AliasFor) |
| 985 | error(": cl::alias must only have one cl::aliasopt(...) specified!"); |
| 986 | AliasFor = &O; |
| 987 | } |
| 988 | |
| 989 | // One option... |
| 990 | template<class M0t> |
| 991 | alias(const M0t &M0) : AliasFor(0) { |
| 992 | apply(M0, this); |
| 993 | done(); |
| 994 | } |
| 995 | // Two options... |
| 996 | template<class M0t, class M1t> |
| 997 | alias(const M0t &M0, const M1t &M1) : AliasFor(0) { |
| 998 | apply(M0, this); apply(M1, this); |
| 999 | done(); |
| 1000 | } |
| 1001 | // Three options... |
| 1002 | template<class M0t, class M1t, class M2t> |
| 1003 | alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) { |
| 1004 | apply(M0, this); apply(M1, this); apply(M2, this); |
| 1005 | done(); |
| 1006 | } |
| 1007 | // Four options... |
| 1008 | template<class M0t, class M1t, class M2t, class M3t> |
| 1009 | alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) |
| 1010 | : AliasFor(0) { |
| 1011 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 1012 | done(); |
| 1013 | } |
| 1014 | }; |
| 1015 | |
| 1016 | // aliasfor - Modifier to set the option an alias aliases. |
| 1017 | struct aliasopt { |
| 1018 | Option &Opt; |
| 1019 | aliasopt(Option &O) : Opt(O) {} |
| 1020 | void apply(alias &A) const { A.setAliasFor(Opt); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 1021 | }; |
| 1022 | |
| 1023 | } // End namespace cl |
| 1024 | |
| 1025 | #endif |