LLVM API Documentation
00001 //===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This class implements a command line argument processor that is useful when 00011 // creating a tool. It provides a simple, minimalistic interface that is easily 00012 // extensible and supports nonlocal (library) command line options. 00013 // 00014 // Note that rather than trying to figure out what this code does, you should 00015 // read the library documentation located in docs/CommandLine.html or looks at 00016 // the many example usages in tools/*/*.cpp 00017 // 00018 //===----------------------------------------------------------------------===// 00019 00020 #ifndef LLVM_SUPPORT_COMMANDLINE_H 00021 #define LLVM_SUPPORT_COMMANDLINE_H 00022 00023 #include "llvm/Support/type_traits.h" 00024 #include "llvm/Support/DataTypes.h" 00025 #include <string> 00026 #include <vector> 00027 #include <utility> 00028 #include <cstdarg> 00029 #include <cassert> 00030 00031 namespace llvm { 00032 00033 /// cl Namespace - This namespace contains all of the command line option 00034 /// processing machinery. It is intentionally a short name to make qualified 00035 /// usage concise. 00036 namespace cl { 00037 00038 //===----------------------------------------------------------------------===// 00039 // ParseCommandLineOptions - Command line option processing entry point. 00040 // 00041 void ParseCommandLineOptions(int &argc, char **argv, 00042 const char *Overview = 0); 00043 00044 //===----------------------------------------------------------------------===// 00045 // ParseEnvironmentOptions - Environment variable option processing alternate 00046 // entry point. 00047 // 00048 void ParseEnvironmentOptions(const char *progName, const char *envvar, 00049 const char *Overview = 0); 00050 00051 //===----------------------------------------------------------------------===// 00052 // Flags permitted to be passed to command line arguments 00053 // 00054 00055 enum NumOccurrences { // Flags for the number of occurrences allowed 00056 Optional = 0x01, // Zero or One occurrence 00057 ZeroOrMore = 0x02, // Zero or more occurrences allowed 00058 Required = 0x03, // One occurrence required 00059 OneOrMore = 0x04, // One or more occurrences required 00060 00061 // ConsumeAfter - Indicates that this option is fed anything that follows the 00062 // last positional argument required by the application (it is an error if 00063 // there are zero positional arguments, and a ConsumeAfter option is used). 00064 // Thus, for example, all arguments to LLI are processed until a filename is 00065 // found. Once a filename is found, all of the succeeding arguments are 00066 // passed, unprocessed, to the ConsumeAfter option. 00067 // 00068 ConsumeAfter = 0x05, 00069 00070 OccurrencesMask = 0x07 00071 }; 00072 00073 enum ValueExpected { // Is a value required for the option? 00074 ValueOptional = 0x08, // The value can appear... or not 00075 ValueRequired = 0x10, // The value is required to appear! 00076 ValueDisallowed = 0x18, // A value may not be specified (for flags) 00077 ValueMask = 0x18 00078 }; 00079 00080 enum OptionHidden { // Control whether -help shows this option 00081 NotHidden = 0x20, // Option included in --help & --help-hidden 00082 Hidden = 0x40, // -help doesn't, but --help-hidden does 00083 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg 00084 HiddenMask = 0x60 00085 }; 00086 00087 // Formatting flags - This controls special features that the option might have 00088 // that cause it to be parsed differently... 00089 // 00090 // Prefix - This option allows arguments that are otherwise unrecognized to be 00091 // matched by options that are a prefix of the actual value. This is useful for 00092 // cases like a linker, where options are typically of the form '-lfoo' or 00093 // '-L../../include' where -l or -L are the actual flags. When prefix is 00094 // enabled, and used, the value for the flag comes from the suffix of the 00095 // argument. 00096 // 00097 // Grouping - With this option enabled, multiple letter options are allowed to 00098 // bunch together with only a single hyphen for the whole group. This allows 00099 // emulation of the behavior that ls uses for example: ls -la === ls -l -a 00100 // 00101 00102 enum FormattingFlags { 00103 NormalFormatting = 0x000, // Nothing special 00104 Positional = 0x080, // Is a positional argument, no '-' required 00105 Prefix = 0x100, // Can this option directly prefix its value? 00106 Grouping = 0x180, // Can this option group with other options? 00107 FormattingMask = 0x180 // Union of the above flags. 00108 }; 00109 00110 enum MiscFlags { // Miscellaneous flags to adjust argument 00111 CommaSeparated = 0x200, // Should this cl::list split between commas? 00112 PositionalEatsArgs = 0x400, // Should this positional cl::list eat -args? 00113 MiscMask = 0x600 // Union of the above flags. 00114 }; 00115 00116 00117 00118 //===----------------------------------------------------------------------===// 00119 // Option Base class 00120 // 00121 class alias; 00122 class Option { 00123 friend void cl::ParseCommandLineOptions(int &, char **, const char *); 00124 friend class alias; 00125 00126 // handleOccurrences - Overriden by subclasses to handle the value passed into 00127 // an argument. Should return true if there was an error processing the 00128 // argument and the program should exit. 00129 // 00130 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 00131 const std::string &Arg) = 0; 00132 00133 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const { 00134 return Optional; 00135 } 00136 virtual enum ValueExpected getValueExpectedFlagDefault() const { 00137 return ValueOptional; 00138 } 00139 virtual enum OptionHidden getOptionHiddenFlagDefault() const { 00140 return NotHidden; 00141 } 00142 virtual enum FormattingFlags getFormattingFlagDefault() const { 00143 return NormalFormatting; 00144 } 00145 00146 int NumOccurrences; // The number of times specified 00147 int Flags; // Flags for the argument 00148 unsigned Position; // Position of last occurrence of the option 00149 public: 00150 const char *ArgStr; // The argument string itself (ex: "help", "o") 00151 const char *HelpStr; // The descriptive text message for --help 00152 const char *ValueStr; // String describing what the value of this option is 00153 00154 inline enum NumOccurrences getNumOccurrencesFlag() const { 00155 int NO = Flags & OccurrencesMask; 00156 return NO ? static_cast<enum NumOccurrences>(NO) 00157 : getNumOccurrencesFlagDefault(); 00158 } 00159 inline enum ValueExpected getValueExpectedFlag() const { 00160 int VE = Flags & ValueMask; 00161 return VE ? static_cast<enum ValueExpected>(VE) 00162 : getValueExpectedFlagDefault(); 00163 } 00164 inline enum OptionHidden getOptionHiddenFlag() const { 00165 int OH = Flags & HiddenMask; 00166 return OH ? static_cast<enum OptionHidden>(OH) 00167 : getOptionHiddenFlagDefault(); 00168 } 00169 inline enum FormattingFlags getFormattingFlag() const { 00170 int OH = Flags & FormattingMask; 00171 return OH ? static_cast<enum FormattingFlags>(OH) 00172 : getFormattingFlagDefault(); 00173 } 00174 inline unsigned getMiscFlags() const { 00175 return Flags & MiscMask; 00176 } 00177 inline unsigned getPosition() const { return Position; } 00178 00179 // hasArgStr - Return true if the argstr != "" 00180 bool hasArgStr() const { return ArgStr[0] != 0; } 00181 00182 //-------------------------------------------------------------------------=== 00183 // Accessor functions set by OptionModifiers 00184 // 00185 void setArgStr(const char *S) { ArgStr = S; } 00186 void setDescription(const char *S) { HelpStr = S; } 00187 void setValueStr(const char *S) { ValueStr = S; } 00188 00189 void setFlag(unsigned Flag, unsigned FlagMask) { 00190 if (Flags & FlagMask) { 00191 error(": Specified two settings for the same option!"); 00192 exit(1); 00193 } 00194 00195 Flags |= Flag; 00196 } 00197 00198 void setNumOccurrencesFlag(enum NumOccurrences Val) { 00199 setFlag(Val, OccurrencesMask); 00200 } 00201 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); } 00202 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); } 00203 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); } 00204 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); } 00205 void setPosition(unsigned pos) { Position = pos; } 00206 protected: 00207 Option() : NumOccurrences(0), Flags(0), Position(0), 00208 ArgStr(""), HelpStr(""), ValueStr("") {} 00209 00210 public: 00211 // addArgument - Tell the system that this Option subclass will handle all 00212 // occurrences of -ArgStr on the command line. 00213 // 00214 void addArgument(const char *ArgStr); 00215 void removeArgument(const char *ArgStr); 00216 00217 // Return the width of the option tag for printing... 00218 virtual unsigned getOptionWidth() const = 0; 00219 00220 // printOptionInfo - Print out information about this option. The 00221 // to-be-maintained width is specified. 00222 // 00223 virtual void printOptionInfo(unsigned GlobalWidth) const = 0; 00224 00225 // addOccurrence - Wrapper around handleOccurrence that enforces Flags 00226 // 00227 bool addOccurrence(unsigned pos, const char *ArgName, 00228 const std::string &Value); 00229 00230 // Prints option name followed by message. Always returns true. 00231 bool error(std::string Message, const char *ArgName = 0); 00232 00233 public: 00234 inline int getNumOccurrences() const { return NumOccurrences; } 00235 virtual ~Option() {} 00236 }; 00237 00238 00239 //===----------------------------------------------------------------------===// 00240 // Command line option modifiers that can be used to modify the behavior of 00241 // command line option parsers... 00242 // 00243 00244 // desc - Modifier to set the description shown in the --help output... 00245 struct desc { 00246 const char *Desc; 00247 desc(const char *Str) : Desc(Str) {} 00248 void apply(Option &O) const { O.setDescription(Desc); } 00249 }; 00250 00251 // value_desc - Modifier to set the value description shown in the --help 00252 // output... 00253 struct value_desc { 00254 const char *Desc; 00255 value_desc(const char *Str) : Desc(Str) {} 00256 void apply(Option &O) const { O.setValueStr(Desc); } 00257 }; 00258 00259 // init - Specify a default (initial) value for the command line argument, if 00260 // the default constructor for the argument type does not give you what you 00261 // want. This is only valid on "opt" arguments, not on "list" arguments. 00262 // 00263 template<class Ty> 00264 struct initializer { 00265 const Ty &Init; 00266 initializer(const Ty &Val) : Init(Val) {} 00267 00268 template<class Opt> 00269 void apply(Opt &O) const { O.setInitialValue(Init); } 00270 }; 00271 00272 template<class Ty> 00273 initializer<Ty> init(const Ty &Val) { 00274 return initializer<Ty>(Val); 00275 } 00276 00277 00278 // location - Allow the user to specify which external variable they want to 00279 // store the results of the command line argument processing into, if they don't 00280 // want to store it in the option itself. 00281 // 00282 template<class Ty> 00283 struct LocationClass { 00284 Ty &Loc; 00285 LocationClass(Ty &L) : Loc(L) {} 00286 00287 template<class Opt> 00288 void apply(Opt &O) const { O.setLocation(O, Loc); } 00289 }; 00290 00291 template<class Ty> 00292 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); } 00293 00294 00295 //===----------------------------------------------------------------------===// 00296 // Enum valued command line option 00297 // 00298 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC 00299 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC 00300 #define clEnumValEnd (reinterpret_cast<void*>(0)) 00301 00302 // values - For custom data types, allow specifying a group of values together 00303 // as the values that go into the mapping that the option handler uses. Note 00304 // that the values list must always have a 0 at the end of the list to indicate 00305 // that the list has ended. 00306 // 00307 template<class DataType> 00308 class ValuesClass { 00309 // Use a vector instead of a map, because the lists should be short, 00310 // the overhead is less, and most importantly, it keeps them in the order 00311 // inserted so we can print our option out nicely. 00312 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values; 00313 void processValues(va_list Vals); 00314 public: 00315 ValuesClass(const char *EnumName, DataType Val, const char *Desc, 00316 va_list ValueArgs) { 00317 // Insert the first value, which is required. 00318 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc))); 00319 00320 // Process the varargs portion of the values... 00321 while (const char *EnumName = va_arg(ValueArgs, const char *)) { 00322 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int)); 00323 const char *EnumDesc = va_arg(ValueArgs, const char *); 00324 Values.push_back(std::make_pair(EnumName, // Add value to value map 00325 std::make_pair(EnumVal, EnumDesc))); 00326 } 00327 } 00328 00329 template<class Opt> 00330 void apply(Opt &O) const { 00331 for (unsigned i = 0, e = Values.size(); i != e; ++i) 00332 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first, 00333 Values[i].second.second); 00334 } 00335 }; 00336 00337 template<class DataType> 00338 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val, 00339 const char *Desc, ...) { 00340 va_list ValueArgs; 00341 va_start(ValueArgs, Desc); 00342 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs); 00343 va_end(ValueArgs); 00344 return Vals; 00345 } 00346 00347 00348 //===----------------------------------------------------------------------===// 00349 // parser class - Parameterizable parser for different data types. By default, 00350 // known data types (string, int, bool) have specialized parsers, that do what 00351 // you would expect. The default parser, used for data types that are not 00352 // built-in, uses a mapping table to map specific options to values, which is 00353 // used, among other things, to handle enum types. 00354 00355 //-------------------------------------------------- 00356 // generic_parser_base - This class holds all the non-generic code that we do 00357 // not need replicated for every instance of the generic parser. This also 00358 // allows us to put stuff into CommandLine.cpp 00359 // 00360 struct generic_parser_base { 00361 virtual ~generic_parser_base() {} // Base class should have virtual-dtor 00362 00363 // getNumOptions - Virtual function implemented by generic subclass to 00364 // indicate how many entries are in Values. 00365 // 00366 virtual unsigned getNumOptions() const = 0; 00367 00368 // getOption - Return option name N. 00369 virtual const char *getOption(unsigned N) const = 0; 00370 00371 // getDescription - Return description N 00372 virtual const char *getDescription(unsigned N) const = 0; 00373 00374 // Return the width of the option tag for printing... 00375 virtual unsigned getOptionWidth(const Option &O) const; 00376 00377 // printOptionInfo - Print out information about this option. The 00378 // to-be-maintained width is specified. 00379 // 00380 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const; 00381 00382 void initialize(Option &O) { 00383 // All of the modifiers for the option have been processed by now, so the 00384 // argstr field should be stable, copy it down now. 00385 // 00386 hasArgStr = O.hasArgStr(); 00387 00388 // If there has been no argstr specified, that means that we need to add an 00389 // argument for every possible option. This ensures that our options are 00390 // vectored to us. 00391 // 00392 if (!hasArgStr) 00393 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) 00394 O.addArgument(getOption(i)); 00395 } 00396 00397 enum ValueExpected getValueExpectedFlagDefault() const { 00398 // If there is an ArgStr specified, then we are of the form: 00399 // 00400 // -opt=O2 or -opt O2 or -optO2 00401 // 00402 // In which case, the value is required. Otherwise if an arg str has not 00403 // been specified, we are of the form: 00404 // 00405 // -O2 or O2 or -la (where -l and -a are separate options) 00406 // 00407 // If this is the case, we cannot allow a value. 00408 // 00409 if (hasArgStr) 00410 return ValueRequired; 00411 else 00412 return ValueDisallowed; 00413 } 00414 00415 // findOption - Return the option number corresponding to the specified 00416 // argument string. If the option is not found, getNumOptions() is returned. 00417 // 00418 unsigned findOption(const char *Name); 00419 00420 protected: 00421 bool hasArgStr; 00422 }; 00423 00424 // Default parser implementation - This implementation depends on having a 00425 // mapping of recognized options to values of some sort. In addition to this, 00426 // each entry in the mapping also tracks a help message that is printed with the 00427 // command line option for --help. Because this is a simple mapping parser, the 00428 // data type can be any unsupported type. 00429 // 00430 template <class DataType> 00431 class parser : public generic_parser_base { 00432 protected: 00433 std::vector<std::pair<const char *, 00434 std::pair<DataType, const char *> > > Values; 00435 public: 00436 typedef DataType parser_data_type; 00437 00438 // Implement virtual functions needed by generic_parser_base 00439 unsigned getNumOptions() const { return unsigned(Values.size()); } 00440 const char *getOption(unsigned N) const { return Values[N].first; } 00441 const char *getDescription(unsigned N) const { 00442 return Values[N].second.second; 00443 } 00444 00445 // parse - Return true on error. 00446 bool parse(Option &O, const char *ArgName, const std::string &Arg, 00447 DataType &V) { 00448 std::string ArgVal; 00449 if (hasArgStr) 00450 ArgVal = Arg; 00451 else 00452 ArgVal = ArgName; 00453 00454 for (unsigned i = 0, e = Values.size(); i != e; ++i) 00455 if (ArgVal == Values[i].first) { 00456 V = Values[i].second.first; 00457 return false; 00458 } 00459 00460 return O.error(": Cannot find option named '" + ArgVal + "'!"); 00461 } 00462 00463 // addLiteralOption - Add an entry to the mapping table... 00464 template <class DT> 00465 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) { 00466 assert(findOption(Name) == Values.size() && "Option already exists!"); 00467 Values.push_back(std::make_pair(Name, 00468 std::make_pair(static_cast<DataType>(V),HelpStr))); 00469 } 00470 00471 // removeLiteralOption - Remove the specified option. 00472 // 00473 void removeLiteralOption(const char *Name) { 00474 unsigned N = findOption(Name); 00475 assert(N != Values.size() && "Option not found!"); 00476 Values.erase(Values.begin()+N); 00477 } 00478 }; 00479 00480 //-------------------------------------------------- 00481 // basic_parser - Super class of parsers to provide boilerplate code 00482 // 00483 struct basic_parser_impl { // non-template implementation of basic_parser<t> 00484 virtual ~basic_parser_impl() {} 00485 00486 enum ValueExpected getValueExpectedFlagDefault() const { 00487 return ValueRequired; 00488 } 00489 00490 void initialize(Option &O) {} 00491 00492 // Return the width of the option tag for printing... 00493 unsigned getOptionWidth(const Option &O) const; 00494 00495 // printOptionInfo - Print out information about this option. The 00496 // to-be-maintained width is specified. 00497 // 00498 void printOptionInfo(const Option &O, unsigned GlobalWidth) const; 00499 00500 // getValueName - Overload in subclass to provide a better default value. 00501 virtual const char *getValueName() const { return "value"; } 00502 }; 00503 00504 // basic_parser - The real basic parser is just a template wrapper that provides 00505 // a typedef for the provided data type. 00506 // 00507 template<class DataType> 00508 struct basic_parser : public basic_parser_impl { 00509 typedef DataType parser_data_type; 00510 }; 00511 00512 00513 //-------------------------------------------------- 00514 // parser<bool> 00515 // 00516 template<> 00517 class parser<bool> : public basic_parser<bool> { 00518 public: 00519 // parse - Return true on error. 00520 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val); 00521 00522 enum ValueExpected getValueExpectedFlagDefault() const { 00523 return ValueOptional; 00524 } 00525 00526 // getValueName - Do not print =<value> at all 00527 virtual const char *getValueName() const { return 0; } 00528 }; 00529 00530 00531 //-------------------------------------------------- 00532 // parser<int> 00533 // 00534 template<> 00535 class parser<int> : public basic_parser<int> { 00536 public: 00537 // parse - Return true on error. 00538 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val); 00539 00540 // getValueName - Overload in subclass to provide a better default value. 00541 virtual const char *getValueName() const { return "int"; } 00542 }; 00543 00544 00545 //-------------------------------------------------- 00546 // parser<unsigned> 00547 // 00548 template<> 00549 class parser<unsigned> : public basic_parser<unsigned> { 00550 public: 00551 // parse - Return true on error. 00552 bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val); 00553 00554 // getValueName - Overload in subclass to provide a better default value. 00555 virtual const char *getValueName() const { return "uint"; } 00556 }; 00557 00558 00559 //-------------------------------------------------- 00560 // parser<double> 00561 // 00562 template<> 00563 class parser<double> : public basic_parser<double> { 00564 public: 00565 // parse - Return true on error. 00566 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val); 00567 00568 // getValueName - Overload in subclass to provide a better default value. 00569 virtual const char *getValueName() const { return "number"; } 00570 }; 00571 00572 00573 //-------------------------------------------------- 00574 // parser<float> 00575 // 00576 template<> 00577 class parser<float> : public basic_parser<float> { 00578 public: 00579 // parse - Return true on error. 00580 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val); 00581 00582 // getValueName - Overload in subclass to provide a better default value. 00583 virtual const char *getValueName() const { return "number"; } 00584 }; 00585 00586 00587 //-------------------------------------------------- 00588 // parser<std::string> 00589 // 00590 template<> 00591 class parser<std::string> : public basic_parser<std::string> { 00592 public: 00593 // parse - Return true on error. 00594 bool parse(Option &O, const char *AN, const std::string &Arg, 00595 std::string &Value) { 00596 Value = Arg; 00597 return false; 00598 } 00599 00600 // getValueName - Overload in subclass to provide a better default value. 00601 virtual const char *getValueName() const { return "string"; } 00602 }; 00603 00604 //===----------------------------------------------------------------------===// 00605 // applicator class - This class is used because we must use partial 00606 // specialization to handle literal string arguments specially (const char* does 00607 // not correctly respond to the apply method). Because the syntax to use this 00608 // is a pain, we have the 'apply' method below to handle the nastiness... 00609 // 00610 template<class Mod> struct applicator { 00611 template<class Opt> 00612 static void opt(const Mod &M, Opt &O) { M.apply(O); } 00613 }; 00614 00615 // Handle const char* as a special case... 00616 template<unsigned n> struct applicator<char[n]> { 00617 template<class Opt> 00618 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 00619 }; 00620 template<unsigned n> struct applicator<const char[n]> { 00621 template<class Opt> 00622 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 00623 }; 00624 template<> struct applicator<const char*> { 00625 template<class Opt> 00626 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 00627 }; 00628 00629 template<> struct applicator<NumOccurrences> { 00630 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); } 00631 }; 00632 template<> struct applicator<ValueExpected> { 00633 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); } 00634 }; 00635 template<> struct applicator<OptionHidden> { 00636 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); } 00637 }; 00638 template<> struct applicator<FormattingFlags> { 00639 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); } 00640 }; 00641 template<> struct applicator<MiscFlags> { 00642 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); } 00643 }; 00644 00645 // apply method - Apply a modifier to an option in a type safe way. 00646 template<class Mod, class Opt> 00647 void apply(const Mod &M, Opt *O) { 00648 applicator<Mod>::opt(M, *O); 00649 } 00650 00651 00652 //===----------------------------------------------------------------------===// 00653 // opt_storage class 00654 00655 // Default storage class definition: external storage. This implementation 00656 // assumes the user will specify a variable to store the data into with the 00657 // cl::location(x) modifier. 00658 // 00659 template<class DataType, bool ExternalStorage, bool isClass> 00660 class opt_storage { 00661 DataType *Location; // Where to store the object... 00662 00663 void check() { 00664 assert(Location != 0 && "cl::location(...) not specified for a command " 00665 "line option with external storage, " 00666 "or cl::init specified before cl::location()!!"); 00667 } 00668 public: 00669 opt_storage() : Location(0) {} 00670 00671 bool setLocation(Option &O, DataType &L) { 00672 if (Location) 00673 return O.error(": cl::location(x) specified more than once!"); 00674 Location = &L; 00675 return false; 00676 } 00677 00678 template<class T> 00679 void setValue(const T &V) { 00680 check(); 00681 *Location = V; 00682 } 00683 00684 DataType &getValue() { check(); return *Location; } 00685 const DataType &getValue() const { check(); return *Location; } 00686 }; 00687 00688 00689 // Define how to hold a class type object, such as a string. Since we can 00690 // inherit from a class, we do so. This makes us exactly compatible with the 00691 // object in all cases that it is used. 00692 // 00693 template<class DataType> 00694 class opt_storage<DataType,false,true> : public DataType { 00695 public: 00696 template<class T> 00697 void setValue(const T &V) { DataType::operator=(V); } 00698 00699 DataType &getValue() { return *this; } 00700 const DataType &getValue() const { return *this; } 00701 }; 00702 00703 // Define a partial specialization to handle things we cannot inherit from. In 00704 // this case, we store an instance through containment, and overload operators 00705 // to get at the value. 00706 // 00707 template<class DataType> 00708 class opt_storage<DataType, false, false> { 00709 public: 00710 DataType Value; 00711 00712 // Make sure we initialize the value with the default constructor for the 00713 // type. 00714 opt_storage() : Value(DataType()) {} 00715 00716 template<class T> 00717 void setValue(const T &V) { Value = V; } 00718 DataType &getValue() { return Value; } 00719 DataType getValue() const { return Value; } 00720 00721 // If the datatype is a pointer, support -> on it. 00722 DataType operator->() const { return Value; } 00723 }; 00724 00725 00726 //===----------------------------------------------------------------------===// 00727 // opt - A scalar command line option. 00728 // 00729 template <class DataType, bool ExternalStorage = false, 00730 class ParserClass = parser<DataType> > 00731 class opt : public Option, 00732 public opt_storage<DataType, ExternalStorage, 00733 is_class<DataType>::value> { 00734 ParserClass Parser; 00735 00736 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 00737 const std::string &Arg) { 00738 typename ParserClass::parser_data_type Val = 00739 typename ParserClass::parser_data_type(); 00740 if (Parser.parse(*this, ArgName, Arg, Val)) 00741 return true; // Parse error! 00742 setValue(Val); 00743 setPosition(pos); 00744 return false; 00745 } 00746 00747 virtual enum ValueExpected getValueExpectedFlagDefault() const { 00748 return Parser.getValueExpectedFlagDefault(); 00749 } 00750 00751 // Forward printing stuff to the parser... 00752 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);} 00753 virtual void printOptionInfo(unsigned GlobalWidth) const { 00754 Parser.printOptionInfo(*this, GlobalWidth); 00755 } 00756 00757 void done() { 00758 addArgument(ArgStr); 00759 Parser.initialize(*this); 00760 } 00761 public: 00762 // setInitialValue - Used by the cl::init modifier... 00763 void setInitialValue(const DataType &V) { this->setValue(V); } 00764 00765 ParserClass &getParser() { return Parser; } 00766 00767 operator DataType() const { return this->getValue(); } 00768 00769 template<class T> 00770 DataType &operator=(const T &Val) { 00771 this->setValue(Val); 00772 return this->getValue(); 00773 } 00774 00775 // One option... 00776 template<class M0t> 00777 opt(const M0t &M0) { 00778 apply(M0, this); 00779 done(); 00780 } 00781 00782 // Two options... 00783 template<class M0t, class M1t> 00784 opt(const M0t &M0, const M1t &M1) { 00785 apply(M0, this); apply(M1, this); 00786 done(); 00787 } 00788 00789 // Three options... 00790 template<class M0t, class M1t, class M2t> 00791 opt(const M0t &M0, const M1t &M1, const M2t &M2) { 00792 apply(M0, this); apply(M1, this); apply(M2, this); 00793 done(); 00794 } 00795 // Four options... 00796 template<class M0t, class M1t, class M2t, class M3t> 00797 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) { 00798 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00799 done(); 00800 } 00801 // Five options... 00802 template<class M0t, class M1t, class M2t, class M3t, class M4t> 00803 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 00804 const M4t &M4) { 00805 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00806 apply(M4, this); 00807 done(); 00808 } 00809 // Six options... 00810 template<class M0t, class M1t, class M2t, class M3t, 00811 class M4t, class M5t> 00812 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 00813 const M4t &M4, const M5t &M5) { 00814 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00815 apply(M4, this); apply(M5, this); 00816 done(); 00817 } 00818 // Seven options... 00819 template<class M0t, class M1t, class M2t, class M3t, 00820 class M4t, class M5t, class M6t> 00821 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 00822 const M4t &M4, const M5t &M5, const M6t &M6) { 00823 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00824 apply(M4, this); apply(M5, this); apply(M6, this); 00825 done(); 00826 } 00827 // Eight options... 00828 template<class M0t, class M1t, class M2t, class M3t, 00829 class M4t, class M5t, class M6t, class M7t> 00830 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 00831 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) { 00832 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00833 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 00834 done(); 00835 } 00836 }; 00837 00838 //===----------------------------------------------------------------------===// 00839 // list_storage class 00840 00841 // Default storage class definition: external storage. This implementation 00842 // assumes the user will specify a variable to store the data into with the 00843 // cl::location(x) modifier. 00844 // 00845 template<class DataType, class StorageClass> 00846 class list_storage { 00847 StorageClass *Location; // Where to store the object... 00848 00849 public: 00850 list_storage() : Location(0) {} 00851 00852 bool setLocation(Option &O, StorageClass &L) { 00853 if (Location) 00854 return O.error(": cl::location(x) specified more than once!"); 00855 Location = &L; 00856 return false; 00857 } 00858 00859 template<class T> 00860 void addValue(const T &V) { 00861 assert(Location != 0 && "cl::location(...) not specified for a command " 00862 "line option with external storage!"); 00863 Location->push_back(V); 00864 } 00865 }; 00866 00867 00868 // Define how to hold a class type object, such as a string. Since we can 00869 // inherit from a class, we do so. This makes us exactly compatible with the 00870 // object in all cases that it is used. 00871 // 00872 template<class DataType> 00873 class list_storage<DataType, bool> : public std::vector<DataType> { 00874 public: 00875 template<class T> 00876 void addValue(const T &V) { push_back(V); } 00877 }; 00878 00879 00880 //===----------------------------------------------------------------------===// 00881 // list - A list of command line options. 00882 // 00883 template <class DataType, class Storage = bool, 00884 class ParserClass = parser<DataType> > 00885 class list : public Option, public list_storage<DataType, Storage> { 00886 std::vector<unsigned> Positions; 00887 ParserClass Parser; 00888 00889 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const { 00890 return ZeroOrMore; 00891 } 00892 virtual enum ValueExpected getValueExpectedFlagDefault() const { 00893 return Parser.getValueExpectedFlagDefault(); 00894 } 00895 00896 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 00897 const std::string &Arg) { 00898 typename ParserClass::parser_data_type Val = 00899 typename ParserClass::parser_data_type(); 00900 if (Parser.parse(*this, ArgName, Arg, Val)) 00901 return true; // Parse Error! 00902 addValue(Val); 00903 setPosition(pos); 00904 Positions.push_back(pos); 00905 return false; 00906 } 00907 00908 // Forward printing stuff to the parser... 00909 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);} 00910 virtual void printOptionInfo(unsigned GlobalWidth) const { 00911 Parser.printOptionInfo(*this, GlobalWidth); 00912 } 00913 00914 void done() { 00915 addArgument(ArgStr); 00916 Parser.initialize(*this); 00917 } 00918 public: 00919 ParserClass &getParser() { return Parser; } 00920 00921 unsigned getPosition(unsigned optnum) const { 00922 assert(optnum < this->size() && "Invalid option index"); 00923 return Positions[optnum]; 00924 } 00925 00926 // One option... 00927 template<class M0t> 00928 list(const M0t &M0) { 00929 apply(M0, this); 00930 done(); 00931 } 00932 // Two options... 00933 template<class M0t, class M1t> 00934 list(const M0t &M0, const M1t &M1) { 00935 apply(M0, this); apply(M1, this); 00936 done(); 00937 } 00938 // Three options... 00939 template<class M0t, class M1t, class M2t> 00940 list(const M0t &M0, const M1t &M1, const M2t &M2) { 00941 apply(M0, this); apply(M1, this); apply(M2, this); 00942 done(); 00943 } 00944 // Four options... 00945 template<class M0t, class M1t, class M2t, class M3t> 00946 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) { 00947 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00948 done(); 00949 } 00950 // Five options... 00951 template<class M0t, class M1t, class M2t, class M3t, class M4t> 00952 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 00953 const M4t &M4) { 00954 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00955 apply(M4, this); 00956 done(); 00957 } 00958 // Six options... 00959 template<class M0t, class M1t, class M2t, class M3t, 00960 class M4t, class M5t> 00961 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 00962 const M4t &M4, const M5t &M5) { 00963 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00964 apply(M4, this); apply(M5, this); 00965 done(); 00966 } 00967 // Seven options... 00968 template<class M0t, class M1t, class M2t, class M3t, 00969 class M4t, class M5t, class M6t> 00970 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 00971 const M4t &M4, const M5t &M5, const M6t &M6) { 00972 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00973 apply(M4, this); apply(M5, this); apply(M6, this); 00974 done(); 00975 } 00976 // Eight options... 00977 template<class M0t, class M1t, class M2t, class M3t, 00978 class M4t, class M5t, class M6t, class M7t> 00979 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 00980 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) { 00981 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00982 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 00983 done(); 00984 } 00985 }; 00986 00987 //===----------------------------------------------------------------------===// 00988 // bits_storage class 00989 00990 // Default storage class definition: external storage. This implementation 00991 // assumes the user will specify a variable to store the data into with the 00992 // cl::location(x) modifier. 00993 // 00994 template<class DataType, class StorageClass> 00995 class bits_storage { 00996 unsigned *Location; // Where to store the bits... 00997 00998 template<class T> 00999 static unsigned Bit(const T &V) { 01000 unsigned BitPos = (unsigned)V; 01001 assert(BitPos < sizeof(unsigned) * 8 && 01002 "enum exceeds width of bit vector!"); 01003 return 1 << BitPos; 01004 } 01005 01006 public: 01007 bits_storage() : Location(0) {} 01008 01009 bool setLocation(Option &O, unsigned &L) { 01010 if (Location) 01011 return O.error(": cl::location(x) specified more than once!"); 01012 Location = &L; 01013 return false; 01014 } 01015 01016 template<class T> 01017 void addValue(const T &V) { 01018 assert(Location != 0 && "cl::location(...) not specified for a command " 01019 "line option with external storage!"); 01020 *Location |= Bit(V); 01021 } 01022 01023 unsigned getBits() { return *Location; } 01024 01025 template<class T> 01026 bool isSet(const T &V) { 01027 return (*Location & Bit(V)) != 0; 01028 } 01029 }; 01030 01031 01032 // Define how to hold bits. Since we can inherit from a class, we do so. 01033 // This makes us exactly compatible with the bits in all cases that it is used. 01034 // 01035 template<class DataType> 01036 class bits_storage<DataType, bool> { 01037 unsigned Bits; // Where to store the bits... 01038 01039 template<class T> 01040 static unsigned Bit(const T &V) { 01041 unsigned BitPos = (unsigned)V; 01042 assert(BitPos < sizeof(unsigned) * 8 && 01043 "enum exceeds width of bit vector!"); 01044 return 1 << BitPos; 01045 } 01046 01047 public: 01048 template<class T> 01049 void addValue(const T &V) { 01050 Bits |= Bit(V); 01051 } 01052 01053 unsigned getBits() { return Bits; } 01054 01055 template<class T> 01056 bool isSet(const T &V) { 01057 return (Bits & Bit(V)) != 0; 01058 } 01059 }; 01060 01061 01062 //===----------------------------------------------------------------------===// 01063 // bits - A bit vector of command options. 01064 // 01065 template <class DataType, class Storage = bool, 01066 class ParserClass = parser<DataType> > 01067 class bits : public Option, public bits_storage<DataType, Storage> { 01068 std::vector<unsigned> Positions; 01069 ParserClass Parser; 01070 01071 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const { 01072 return ZeroOrMore; 01073 } 01074 virtual enum ValueExpected getValueExpectedFlagDefault() const { 01075 return Parser.getValueExpectedFlagDefault(); 01076 } 01077 01078 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 01079 const std::string &Arg) { 01080 typename ParserClass::parser_data_type Val = 01081 typename ParserClass::parser_data_type(); 01082 if (Parser.parse(*this, ArgName, Arg, Val)) 01083 return true; // Parse Error! 01084 addValue(Val); 01085 setPosition(pos); 01086 Positions.push_back(pos); 01087 return false; 01088 } 01089 01090 // Forward printing stuff to the parser... 01091 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);} 01092 virtual void printOptionInfo(unsigned GlobalWidth) const { 01093 Parser.printOptionInfo(*this, GlobalWidth); 01094 } 01095 01096 void done() { 01097 addArgument(ArgStr); 01098 Parser.initialize(*this); 01099 } 01100 public: 01101 ParserClass &getParser() { return Parser; } 01102 01103 unsigned getPosition(unsigned optnum) const { 01104 assert(optnum < this->size() && "Invalid option index"); 01105 return Positions[optnum]; 01106 } 01107 01108 // One option... 01109 template<class M0t> 01110 bits(const M0t &M0) { 01111 apply(M0, this); 01112 done(); 01113 } 01114 // Two options... 01115 template<class M0t, class M1t> 01116 bits(const M0t &M0, const M1t &M1) { 01117 apply(M0, this); apply(M1, this); 01118 done(); 01119 } 01120 // Three options... 01121 template<class M0t, class M1t, class M2t> 01122 bits(const M0t &M0, const M1t &M1, const M2t &M2) { 01123 apply(M0, this); apply(M1, this); apply(M2, this); 01124 done(); 01125 } 01126 // Four options... 01127 template<class M0t, class M1t, class M2t, class M3t> 01128 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) { 01129 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01130 done(); 01131 } 01132 // Five options... 01133 template<class M0t, class M1t, class M2t, class M3t, class M4t> 01134 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01135 const M4t &M4) { 01136 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01137 apply(M4, this); 01138 done(); 01139 } 01140 // Six options... 01141 template<class M0t, class M1t, class M2t, class M3t, 01142 class M4t, class M5t> 01143 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01144 const M4t &M4, const M5t &M5) { 01145 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01146 apply(M4, this); apply(M5, this); 01147 done(); 01148 } 01149 // Seven options... 01150 template<class M0t, class M1t, class M2t, class M3t, 01151 class M4t, class M5t, class M6t> 01152 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01153 const M4t &M4, const M5t &M5, const M6t &M6) { 01154 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01155 apply(M4, this); apply(M5, this); apply(M6, this); 01156 done(); 01157 } 01158 // Eight options... 01159 template<class M0t, class M1t, class M2t, class M3t, 01160 class M4t, class M5t, class M6t, class M7t> 01161 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01162 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) { 01163 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01164 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 01165 done(); 01166 } 01167 }; 01168 01169 //===----------------------------------------------------------------------===// 01170 // Aliased command line option (alias this name to a preexisting name) 01171 // 01172 01173 class alias : public Option { 01174 Option *AliasFor; 01175 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 01176 const std::string &Arg) { 01177 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg); 01178 } 01179 // Aliases default to be hidden... 01180 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;} 01181 01182 // Handle printing stuff... 01183 virtual unsigned getOptionWidth() const; 01184 virtual void printOptionInfo(unsigned GlobalWidth) const; 01185 01186 void done() { 01187 if (!hasArgStr()) 01188 error(": cl::alias must have argument name specified!"); 01189 if (AliasFor == 0) 01190 error(": cl::alias must have an cl::aliasopt(option) specified!"); 01191 addArgument(ArgStr); 01192 } 01193 public: 01194 void setAliasFor(Option &O) { 01195 if (AliasFor) 01196 error(": cl::alias must only have one cl::aliasopt(...) specified!"); 01197 AliasFor = &O; 01198 } 01199 01200 // One option... 01201 template<class M0t> 01202 alias(const M0t &M0) : AliasFor(0) { 01203 apply(M0, this); 01204 done(); 01205 } 01206 // Two options... 01207 template<class M0t, class M1t> 01208 alias(const M0t &M0, const M1t &M1) : AliasFor(0) { 01209 apply(M0, this); apply(M1, this); 01210 done(); 01211 } 01212 // Three options... 01213 template<class M0t, class M1t, class M2t> 01214 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) { 01215 apply(M0, this); apply(M1, this); apply(M2, this); 01216 done(); 01217 } 01218 // Four options... 01219 template<class M0t, class M1t, class M2t, class M3t> 01220 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 01221 : AliasFor(0) { 01222 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01223 done(); 01224 } 01225 }; 01226 01227 // aliasfor - Modifier to set the option an alias aliases. 01228 struct aliasopt { 01229 Option &Opt; 01230 aliasopt(Option &O) : Opt(O) {} 01231 void apply(alias &A) const { A.setAliasFor(Opt); } 01232 }; 01233 01234 // extrahelp - provide additional help at the end of the normal help 01235 // output. All occurrences of cl::extrahelp will be accumulated and 01236 // printed to std::cerr at the end of the regular help, just before 01237 // exit is called. 01238 struct extrahelp { 01239 const char * morehelp; 01240 extrahelp(const char* help); 01241 }; 01242 01243 // This function just prints the help message, exactly the same way as if the 01244 // --help option had been given on the command line. 01245 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM! 01246 void PrintHelpMessage(); 01247 01248 } // End namespace cl 01249 01250 } // End namespace llvm 01251 01252 #endif