CommandLine.h revision 27e9d457d4047cf0051e17d9ca39740875d11440
1//===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===// 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//===----------------------------------------------------------------------===// 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// 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 17// 18//===----------------------------------------------------------------------===// 19 20#ifndef LLVM_SUPPORT_COMMANDLINE_H 21#define LLVM_SUPPORT_COMMANDLINE_H 22 23#include "llvm/Support/type_traits.h" 24#include "llvm/Support/DataTypes.h" 25#include "llvm/Support/Compiler.h" 26#include "llvm/ADT/SmallVector.h" 27#include <string> 28#include <vector> 29#include <utility> 30#include <cstdarg> 31#include <cassert> 32 33namespace llvm { 34 35/// cl Namespace - This namespace contains all of the command line option 36/// processing machinery. It is intentionally a short name to make qualified 37/// usage concise. 38namespace cl { 39 40//===----------------------------------------------------------------------===// 41// ParseCommandLineOptions - Command line option processing entry point. 42// 43void ParseCommandLineOptions(int &argc, char **argv, 44 const char *Overview = 0); 45 46//===----------------------------------------------------------------------===// 47// ParseEnvironmentOptions - Environment variable option processing alternate 48// entry point. 49// 50void ParseEnvironmentOptions(const char *progName, const char *envvar, 51 const char *Overview = 0); 52 53///===---------------------------------------------------------------------===// 54/// SetVersionPrinter - Override the default (LLVM specific) version printer 55/// used to print out the version when --version is given 56/// on the command line. This gives other systems using the 57/// CommandLine utilities to print their own version string. 58void SetVersionPrinter(void (*func)()); 59 60 61// MarkOptionsChanged - Internal helper function. 62void MarkOptionsChanged(); 63 64//===----------------------------------------------------------------------===// 65// Flags permitted to be passed to command line arguments 66// 67 68enum NumOccurrences { // Flags for the number of occurrences allowed 69 Optional = 0x01, // Zero or One occurrence 70 ZeroOrMore = 0x02, // Zero or more occurrences allowed 71 Required = 0x03, // One occurrence required 72 OneOrMore = 0x04, // One or more occurrences required 73 74 // ConsumeAfter - Indicates that this option is fed anything that follows the 75 // last positional argument required by the application (it is an error if 76 // there are zero positional arguments, and a ConsumeAfter option is used). 77 // Thus, for example, all arguments to LLI are processed until a filename is 78 // found. Once a filename is found, all of the succeeding arguments are 79 // passed, unprocessed, to the ConsumeAfter option. 80 // 81 ConsumeAfter = 0x05, 82 83 OccurrencesMask = 0x07 84}; 85 86enum ValueExpected { // Is a value required for the option? 87 ValueOptional = 0x08, // The value can appear... or not 88 ValueRequired = 0x10, // The value is required to appear! 89 ValueDisallowed = 0x18, // A value may not be specified (for flags) 90 ValueMask = 0x18 91}; 92 93enum OptionHidden { // Control whether -help shows this option 94 NotHidden = 0x20, // Option included in --help & --help-hidden 95 Hidden = 0x40, // -help doesn't, but --help-hidden does 96 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg 97 HiddenMask = 0x60 98}; 99 100// Formatting flags - This controls special features that the option might have 101// that cause it to be parsed differently... 102// 103// Prefix - This option allows arguments that are otherwise unrecognized to be 104// matched by options that are a prefix of the actual value. This is useful for 105// cases like a linker, where options are typically of the form '-lfoo' or 106// '-L../../include' where -l or -L are the actual flags. When prefix is 107// enabled, and used, the value for the flag comes from the suffix of the 108// argument. 109// 110// Grouping - With this option enabled, multiple letter options are allowed to 111// bunch together with only a single hyphen for the whole group. This allows 112// emulation of the behavior that ls uses for example: ls -la === ls -l -a 113// 114 115enum FormattingFlags { 116 NormalFormatting = 0x000, // Nothing special 117 Positional = 0x080, // Is a positional argument, no '-' required 118 Prefix = 0x100, // Can this option directly prefix its value? 119 Grouping = 0x180, // Can this option group with other options? 120 FormattingMask = 0x180 // Union of the above flags. 121}; 122 123enum MiscFlags { // Miscellaneous flags to adjust argument 124 CommaSeparated = 0x200, // Should this cl::list split between commas? 125 PositionalEatsArgs = 0x400, // Should this positional cl::list eat -args? 126 MiscMask = 0x600 // Union of the above flags. 127}; 128 129 130 131//===----------------------------------------------------------------------===// 132// Option Base class 133// 134class alias; 135class Option { 136 friend void cl::ParseCommandLineOptions(int &, char **, const char *); 137 friend class alias; 138 139 // handleOccurrences - Overriden by subclasses to handle the value passed into 140 // an argument. Should return true if there was an error processing the 141 // argument and the program should exit. 142 // 143 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 144 const std::string &Arg) = 0; 145 146 virtual enum ValueExpected getValueExpectedFlagDefault() const { 147 return ValueOptional; 148 } 149 150 // Out of line virtual function to provide home for the class. 151 virtual void anchor(); 152 153 int NumOccurrences; // The number of times specified 154 int Flags; // Flags for the argument 155 unsigned Position; // Position of last occurrence of the option 156 Option *NextRegistered; // Singly linked list of registered options. 157public: 158 const char *ArgStr; // The argument string itself (ex: "help", "o") 159 const char *HelpStr; // The descriptive text message for --help 160 const char *ValueStr; // String describing what the value of this option is 161 162 inline enum NumOccurrences getNumOccurrencesFlag() const { 163 return static_cast<enum NumOccurrences>(Flags & OccurrencesMask); 164 } 165 inline enum ValueExpected getValueExpectedFlag() const { 166 int VE = Flags & ValueMask; 167 return VE ? static_cast<enum ValueExpected>(VE) 168 : getValueExpectedFlagDefault(); 169 } 170 inline enum OptionHidden getOptionHiddenFlag() const { 171 return static_cast<enum OptionHidden>(Flags & HiddenMask); 172 } 173 inline enum FormattingFlags getFormattingFlag() const { 174 return static_cast<enum FormattingFlags>(Flags & FormattingMask); 175 } 176 inline unsigned getMiscFlags() const { 177 return Flags & MiscMask; 178 } 179 inline unsigned getPosition() const { return Position; } 180 181 // hasArgStr - Return true if the argstr != "" 182 bool hasArgStr() const { return ArgStr[0] != 0; } 183 184 //-------------------------------------------------------------------------=== 185 // Accessor functions set by OptionModifiers 186 // 187 void setArgStr(const char *S) { ArgStr = S; } 188 void setDescription(const char *S) { HelpStr = S; } 189 void setValueStr(const char *S) { ValueStr = S; } 190 191 void setFlag(unsigned Flag, unsigned FlagMask) { 192 Flags &= ~FlagMask; 193 Flags |= Flag; 194 } 195 196 void setNumOccurrencesFlag(enum NumOccurrences Val) { 197 setFlag(Val, OccurrencesMask); 198 } 199 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); } 200 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); } 201 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); } 202 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); } 203 void setPosition(unsigned pos) { Position = pos; } 204protected: 205 explicit Option(unsigned DefaultFlags) 206 : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0), 207 NextRegistered(0), ArgStr(""), HelpStr(""), ValueStr("") { 208 assert(getNumOccurrencesFlag() != 0 && 209 getOptionHiddenFlag() != 0 && "Not all default flags specified!"); 210 } 211 212public: 213 // addArgument - Register this argument with the commandline system. 214 // 215 void addArgument(); 216 217 Option *getNextRegisteredOption() const { return NextRegistered; } 218 219 // Return the width of the option tag for printing... 220 virtual unsigned getOptionWidth() const = 0; 221 222 // printOptionInfo - Print out information about this option. The 223 // to-be-maintained width is specified. 224 // 225 virtual void printOptionInfo(unsigned GlobalWidth) const = 0; 226 227 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {} 228 229 // addOccurrence - Wrapper around handleOccurrence that enforces Flags 230 // 231 bool addOccurrence(unsigned pos, const char *ArgName, 232 const std::string &Value); 233 234 // Prints option name followed by message. Always returns true. 235 bool error(std::string Message, const char *ArgName = 0); 236 237public: 238 inline int getNumOccurrences() const { return NumOccurrences; } 239 virtual ~Option() {} 240}; 241 242 243//===----------------------------------------------------------------------===// 244// Command line option modifiers that can be used to modify the behavior of 245// command line option parsers... 246// 247 248// desc - Modifier to set the description shown in the --help output... 249struct desc { 250 const char *Desc; 251 desc(const char *Str) : Desc(Str) {} 252 void apply(Option &O) const { O.setDescription(Desc); } 253}; 254 255// value_desc - Modifier to set the value description shown in the --help 256// output... 257struct value_desc { 258 const char *Desc; 259 value_desc(const char *Str) : Desc(Str) {} 260 void apply(Option &O) const { O.setValueStr(Desc); } 261}; 262 263// init - Specify a default (initial) value for the command line argument, if 264// the default constructor for the argument type does not give you what you 265// want. This is only valid on "opt" arguments, not on "list" arguments. 266// 267template<class Ty> 268struct initializer { 269 const Ty &Init; 270 initializer(const Ty &Val) : Init(Val) {} 271 272 template<class Opt> 273 void apply(Opt &O) const { O.setInitialValue(Init); } 274}; 275 276template<class Ty> 277initializer<Ty> init(const Ty &Val) { 278 return initializer<Ty>(Val); 279} 280 281 282// location - Allow the user to specify which external variable they want to 283// store the results of the command line argument processing into, if they don't 284// want to store it in the option itself. 285// 286template<class Ty> 287struct LocationClass { 288 Ty &Loc; 289 LocationClass(Ty &L) : Loc(L) {} 290 291 template<class Opt> 292 void apply(Opt &O) const { O.setLocation(O, Loc); } 293}; 294 295template<class Ty> 296LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); } 297 298 299//===----------------------------------------------------------------------===// 300// Enum valued command line option 301// 302#define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC 303#define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC 304#define clEnumValEnd (reinterpret_cast<void*>(0)) 305 306// values - For custom data types, allow specifying a group of values together 307// as the values that go into the mapping that the option handler uses. Note 308// that the values list must always have a 0 at the end of the list to indicate 309// that the list has ended. 310// 311template<class DataType> 312class ValuesClass { 313 // Use a vector instead of a map, because the lists should be short, 314 // the overhead is less, and most importantly, it keeps them in the order 315 // inserted so we can print our option out nicely. 316 SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values; 317 void processValues(va_list Vals); 318public: 319 ValuesClass(const char *EnumName, DataType Val, const char *Desc, 320 va_list ValueArgs) { 321 // Insert the first value, which is required. 322 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc))); 323 324 // Process the varargs portion of the values... 325 while (const char *EnumName = va_arg(ValueArgs, const char *)) { 326 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int)); 327 const char *EnumDesc = va_arg(ValueArgs, const char *); 328 Values.push_back(std::make_pair(EnumName, // Add value to value map 329 std::make_pair(EnumVal, EnumDesc))); 330 } 331 } 332 333 template<class Opt> 334 void apply(Opt &O) const { 335 for (unsigned i = 0, e = Values.size(); i != e; ++i) 336 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first, 337 Values[i].second.second); 338 } 339}; 340 341template<class DataType> 342ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val, 343 const char *Desc, ...) { 344 va_list ValueArgs; 345 va_start(ValueArgs, Desc); 346 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs); 347 va_end(ValueArgs); 348 return Vals; 349} 350 351 352//===----------------------------------------------------------------------===// 353// parser class - Parameterizable parser for different data types. By default, 354// known data types (string, int, bool) have specialized parsers, that do what 355// you would expect. The default parser, used for data types that are not 356// built-in, uses a mapping table to map specific options to values, which is 357// used, among other things, to handle enum types. 358 359//-------------------------------------------------- 360// generic_parser_base - This class holds all the non-generic code that we do 361// not need replicated for every instance of the generic parser. This also 362// allows us to put stuff into CommandLine.cpp 363// 364struct generic_parser_base { 365 virtual ~generic_parser_base() {} // Base class should have virtual-dtor 366 367 // getNumOptions - Virtual function implemented by generic subclass to 368 // indicate how many entries are in Values. 369 // 370 virtual unsigned getNumOptions() const = 0; 371 372 // getOption - Return option name N. 373 virtual const char *getOption(unsigned N) const = 0; 374 375 // getDescription - Return description N 376 virtual const char *getDescription(unsigned N) const = 0; 377 378 // Return the width of the option tag for printing... 379 virtual unsigned getOptionWidth(const Option &O) const; 380 381 // printOptionInfo - Print out information about this option. The 382 // to-be-maintained width is specified. 383 // 384 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const; 385 386 void initialize(Option &O) { 387 // All of the modifiers for the option have been processed by now, so the 388 // argstr field should be stable, copy it down now. 389 // 390 hasArgStr = O.hasArgStr(); 391 } 392 393 void getExtraOptionNames(std::vector<const char*> &OptionNames) { 394 // If there has been no argstr specified, that means that we need to add an 395 // argument for every possible option. This ensures that our options are 396 // vectored to us. 397 if (!hasArgStr) 398 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) 399 OptionNames.push_back(getOption(i)); 400 } 401 402 403 enum ValueExpected getValueExpectedFlagDefault() const { 404 // If there is an ArgStr specified, then we are of the form: 405 // 406 // -opt=O2 or -opt O2 or -optO2 407 // 408 // In which case, the value is required. Otherwise if an arg str has not 409 // been specified, we are of the form: 410 // 411 // -O2 or O2 or -la (where -l and -a are separate options) 412 // 413 // If this is the case, we cannot allow a value. 414 // 415 if (hasArgStr) 416 return ValueRequired; 417 else 418 return ValueDisallowed; 419 } 420 421 // findOption - Return the option number corresponding to the specified 422 // argument string. If the option is not found, getNumOptions() is returned. 423 // 424 unsigned findOption(const char *Name); 425 426protected: 427 bool hasArgStr; 428}; 429 430// Default parser implementation - This implementation depends on having a 431// mapping of recognized options to values of some sort. In addition to this, 432// each entry in the mapping also tracks a help message that is printed with the 433// command line option for --help. Because this is a simple mapping parser, the 434// data type can be any unsupported type. 435// 436template <class DataType> 437class parser : public generic_parser_base { 438protected: 439 SmallVector<std::pair<const char *, 440 std::pair<DataType, const char *> >, 8> Values; 441public: 442 typedef DataType parser_data_type; 443 444 // Implement virtual functions needed by generic_parser_base 445 unsigned getNumOptions() const { return unsigned(Values.size()); } 446 const char *getOption(unsigned N) const { return Values[N].first; } 447 const char *getDescription(unsigned N) const { 448 return Values[N].second.second; 449 } 450 451 // parse - Return true on error. 452 bool parse(Option &O, const char *ArgName, const std::string &Arg, 453 DataType &V) { 454 std::string ArgVal; 455 if (hasArgStr) 456 ArgVal = Arg; 457 else 458 ArgVal = ArgName; 459 460 for (unsigned i = 0, e = Values.size(); i != e; ++i) 461 if (ArgVal == Values[i].first) { 462 V = Values[i].second.first; 463 return false; 464 } 465 466 return O.error(": Cannot find option named '" + ArgVal + "'!"); 467 } 468 469 /// addLiteralOption - Add an entry to the mapping table. 470 /// 471 template <class DT> 472 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) { 473 assert(findOption(Name) == Values.size() && "Option already exists!"); 474 Values.push_back(std::make_pair(Name, 475 std::make_pair(static_cast<DataType>(V),HelpStr))); 476 MarkOptionsChanged(); 477 } 478 479 /// removeLiteralOption - Remove the specified option. 480 /// 481 void removeLiteralOption(const char *Name) { 482 unsigned N = findOption(Name); 483 assert(N != Values.size() && "Option not found!"); 484 Values.erase(Values.begin()+N); 485 } 486}; 487 488//-------------------------------------------------- 489// basic_parser - Super class of parsers to provide boilerplate code 490// 491struct basic_parser_impl { // non-template implementation of basic_parser<t> 492 virtual ~basic_parser_impl() {} 493 494 enum ValueExpected getValueExpectedFlagDefault() const { 495 return ValueRequired; 496 } 497 498 void getExtraOptionNames(std::vector<const char*> &OptionNames) {} 499 500 void initialize(Option &O) {} 501 502 // Return the width of the option tag for printing... 503 unsigned getOptionWidth(const Option &O) const; 504 505 // printOptionInfo - Print out information about this option. The 506 // to-be-maintained width is specified. 507 // 508 void printOptionInfo(const Option &O, unsigned GlobalWidth) const; 509 510 // getValueName - Overload in subclass to provide a better default value. 511 virtual const char *getValueName() const { return "value"; } 512 513 // An out-of-line virtual method to provide a 'home' for this class. 514 virtual void anchor(); 515}; 516 517// basic_parser - The real basic parser is just a template wrapper that provides 518// a typedef for the provided data type. 519// 520template<class DataType> 521struct basic_parser : public basic_parser_impl { 522 typedef DataType parser_data_type; 523}; 524 525//-------------------------------------------------- 526// parser<bool> 527// 528template<> 529class parser<bool> : public basic_parser<bool> { 530public: 531 // parse - Return true on error. 532 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val); 533 534 enum ValueExpected getValueExpectedFlagDefault() const { 535 return ValueOptional; 536 } 537 538 // getValueName - Do not print =<value> at all. 539 virtual const char *getValueName() const { return 0; } 540 541 // An out-of-line virtual method to provide a 'home' for this class. 542 virtual void anchor(); 543}; 544 545EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>); 546 547//-------------------------------------------------- 548// parser<boolOrDefault> 549enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE }; 550template<> 551class parser<boolOrDefault> : public basic_parser<boolOrDefault> { 552public: 553 // parse - Return true on error. 554 bool parse(Option &O, const char *ArgName, const std::string &Arg, 555 boolOrDefault &Val); 556 557 enum ValueExpected getValueExpectedFlagDefault() const { 558 return ValueOptional; 559 } 560 561 // getValueName - Do not print =<value> at all. 562 virtual const char *getValueName() const { return 0; } 563 564 // An out-of-line virtual method to provide a 'home' for this class. 565 virtual void anchor(); 566}; 567 568EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>); 569 570//-------------------------------------------------- 571// parser<int> 572// 573template<> 574class parser<int> : public basic_parser<int> { 575public: 576 // parse - Return true on error. 577 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val); 578 579 // getValueName - Overload in subclass to provide a better default value. 580 virtual const char *getValueName() const { return "int"; } 581 582 // An out-of-line virtual method to provide a 'home' for this class. 583 virtual void anchor(); 584}; 585 586EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>); 587 588 589//-------------------------------------------------- 590// parser<unsigned> 591// 592template<> 593class parser<unsigned> : public basic_parser<unsigned> { 594public: 595 // parse - Return true on error. 596 bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val); 597 598 // getValueName - Overload in subclass to provide a better default value. 599 virtual const char *getValueName() const { return "uint"; } 600 601 // An out-of-line virtual method to provide a 'home' for this class. 602 virtual void anchor(); 603}; 604 605EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>); 606 607//-------------------------------------------------- 608// parser<double> 609// 610template<> 611class parser<double> : public basic_parser<double> { 612public: 613 // parse - Return true on error. 614 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val); 615 616 // getValueName - Overload in subclass to provide a better default value. 617 virtual const char *getValueName() const { return "number"; } 618 619 // An out-of-line virtual method to provide a 'home' for this class. 620 virtual void anchor(); 621}; 622 623EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>); 624 625//-------------------------------------------------- 626// parser<float> 627// 628template<> 629class parser<float> : public basic_parser<float> { 630public: 631 // parse - Return true on error. 632 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val); 633 634 // getValueName - Overload in subclass to provide a better default value. 635 virtual const char *getValueName() const { return "number"; } 636 637 // An out-of-line virtual method to provide a 'home' for this class. 638 virtual void anchor(); 639}; 640 641EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>); 642 643//-------------------------------------------------- 644// parser<std::string> 645// 646template<> 647class parser<std::string> : public basic_parser<std::string> { 648public: 649 // parse - Return true on error. 650 bool parse(Option &O, const char *AN, const std::string &Arg, 651 std::string &Value) { 652 Value = Arg; 653 return false; 654 } 655 656 // getValueName - Overload in subclass to provide a better default value. 657 virtual const char *getValueName() const { return "string"; } 658 659 // An out-of-line virtual method to provide a 'home' for this class. 660 virtual void anchor(); 661}; 662 663EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>); 664 665//===----------------------------------------------------------------------===// 666// applicator class - This class is used because we must use partial 667// specialization to handle literal string arguments specially (const char* does 668// not correctly respond to the apply method). Because the syntax to use this 669// is a pain, we have the 'apply' method below to handle the nastiness... 670// 671template<class Mod> struct applicator { 672 template<class Opt> 673 static void opt(const Mod &M, Opt &O) { M.apply(O); } 674}; 675 676// Handle const char* as a special case... 677template<unsigned n> struct applicator<char[n]> { 678 template<class Opt> 679 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 680}; 681template<unsigned n> struct applicator<const char[n]> { 682 template<class Opt> 683 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 684}; 685template<> struct applicator<const char*> { 686 template<class Opt> 687 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 688}; 689 690template<> struct applicator<NumOccurrences> { 691 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); } 692}; 693template<> struct applicator<ValueExpected> { 694 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); } 695}; 696template<> struct applicator<OptionHidden> { 697 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); } 698}; 699template<> struct applicator<FormattingFlags> { 700 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); } 701}; 702template<> struct applicator<MiscFlags> { 703 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); } 704}; 705 706// apply method - Apply a modifier to an option in a type safe way. 707template<class Mod, class Opt> 708void apply(const Mod &M, Opt *O) { 709 applicator<Mod>::opt(M, *O); 710} 711 712 713//===----------------------------------------------------------------------===// 714// opt_storage class 715 716// Default storage class definition: external storage. This implementation 717// assumes the user will specify a variable to store the data into with the 718// cl::location(x) modifier. 719// 720template<class DataType, bool ExternalStorage, bool isClass> 721class opt_storage { 722 DataType *Location; // Where to store the object... 723 724 void check() { 725 assert(Location != 0 && "cl::location(...) not specified for a command " 726 "line option with external storage, " 727 "or cl::init specified before cl::location()!!"); 728 } 729public: 730 opt_storage() : Location(0) {} 731 732 bool setLocation(Option &O, DataType &L) { 733 if (Location) 734 return O.error(": cl::location(x) specified more than once!"); 735 Location = &L; 736 return false; 737 } 738 739 template<class T> 740 void setValue(const T &V) { 741 check(); 742 *Location = V; 743 } 744 745 DataType &getValue() { check(); return *Location; } 746 const DataType &getValue() const { check(); return *Location; } 747}; 748 749 750// Define how to hold a class type object, such as a string. Since we can 751// inherit from a class, we do so. This makes us exactly compatible with the 752// object in all cases that it is used. 753// 754template<class DataType> 755class opt_storage<DataType,false,true> : public DataType { 756public: 757 template<class T> 758 void setValue(const T &V) { DataType::operator=(V); } 759 760 DataType &getValue() { return *this; } 761 const DataType &getValue() const { return *this; } 762}; 763 764// Define a partial specialization to handle things we cannot inherit from. In 765// this case, we store an instance through containment, and overload operators 766// to get at the value. 767// 768template<class DataType> 769class opt_storage<DataType, false, false> { 770public: 771 DataType Value; 772 773 // Make sure we initialize the value with the default constructor for the 774 // type. 775 opt_storage() : Value(DataType()) {} 776 777 template<class T> 778 void setValue(const T &V) { Value = V; } 779 DataType &getValue() { return Value; } 780 DataType getValue() const { return Value; } 781 782 // If the datatype is a pointer, support -> on it. 783 DataType operator->() const { return Value; } 784}; 785 786 787//===----------------------------------------------------------------------===// 788// opt - A scalar command line option. 789// 790template <class DataType, bool ExternalStorage = false, 791 class ParserClass = parser<DataType> > 792class opt : public Option, 793 public opt_storage<DataType, ExternalStorage, 794 is_class<DataType>::value> { 795 ParserClass Parser; 796 797 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 798 const std::string &Arg) { 799 typename ParserClass::parser_data_type Val = 800 typename ParserClass::parser_data_type(); 801 if (Parser.parse(*this, ArgName, Arg, Val)) 802 return true; // Parse error! 803 setValue(Val); 804 setPosition(pos); 805 return false; 806 } 807 808 virtual enum ValueExpected getValueExpectedFlagDefault() const { 809 return Parser.getValueExpectedFlagDefault(); 810 } 811 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) { 812 return Parser.getExtraOptionNames(OptionNames); 813 } 814 815 // Forward printing stuff to the parser... 816 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);} 817 virtual void printOptionInfo(unsigned GlobalWidth) const { 818 Parser.printOptionInfo(*this, GlobalWidth); 819 } 820 821 void done() { 822 addArgument(); 823 Parser.initialize(*this); 824 } 825public: 826 // setInitialValue - Used by the cl::init modifier... 827 void setInitialValue(const DataType &V) { this->setValue(V); } 828 829 ParserClass &getParser() { return Parser; } 830 831 operator DataType() const { return this->getValue(); } 832 833 template<class T> 834 DataType &operator=(const T &Val) { 835 this->setValue(Val); 836 return this->getValue(); 837 } 838 839 // One option... 840 template<class M0t> 841 opt(const M0t &M0) : Option(Optional | NotHidden) { 842 apply(M0, this); 843 done(); 844 } 845 846 // Two options... 847 template<class M0t, class M1t> 848 opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) { 849 apply(M0, this); apply(M1, this); 850 done(); 851 } 852 853 // Three options... 854 template<class M0t, class M1t, class M2t> 855 opt(const M0t &M0, const M1t &M1, 856 const M2t &M2) : Option(Optional | NotHidden) { 857 apply(M0, this); apply(M1, this); apply(M2, this); 858 done(); 859 } 860 // Four options... 861 template<class M0t, class M1t, class M2t, class M3t> 862 opt(const M0t &M0, const M1t &M1, const M2t &M2, 863 const M3t &M3) : Option(Optional | NotHidden) { 864 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 865 done(); 866 } 867 // Five options... 868 template<class M0t, class M1t, class M2t, class M3t, class M4t> 869 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 870 const M4t &M4) : Option(Optional | NotHidden) { 871 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 872 apply(M4, this); 873 done(); 874 } 875 // Six options... 876 template<class M0t, class M1t, class M2t, class M3t, 877 class M4t, class M5t> 878 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 879 const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) { 880 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 881 apply(M4, this); apply(M5, this); 882 done(); 883 } 884 // Seven options... 885 template<class M0t, class M1t, class M2t, class M3t, 886 class M4t, class M5t, class M6t> 887 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 888 const M4t &M4, const M5t &M5, 889 const M6t &M6) : Option(Optional | NotHidden) { 890 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 891 apply(M4, this); apply(M5, this); apply(M6, this); 892 done(); 893 } 894 // Eight options... 895 template<class M0t, class M1t, class M2t, class M3t, 896 class M4t, class M5t, class M6t, class M7t> 897 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 898 const M4t &M4, const M5t &M5, const M6t &M6, 899 const M7t &M7) : Option(Optional | NotHidden) { 900 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 901 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 902 done(); 903 } 904}; 905 906EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>); 907EXTERN_TEMPLATE_INSTANTIATION(class opt<int>); 908EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>); 909EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>); 910 911//===----------------------------------------------------------------------===// 912// list_storage class 913 914// Default storage class definition: external storage. This implementation 915// assumes the user will specify a variable to store the data into with the 916// cl::location(x) modifier. 917// 918template<class DataType, class StorageClass> 919class list_storage { 920 StorageClass *Location; // Where to store the object... 921 922public: 923 list_storage() : Location(0) {} 924 925 bool setLocation(Option &O, StorageClass &L) { 926 if (Location) 927 return O.error(": cl::location(x) specified more than once!"); 928 Location = &L; 929 return false; 930 } 931 932 template<class T> 933 void addValue(const T &V) { 934 assert(Location != 0 && "cl::location(...) not specified for a command " 935 "line option with external storage!"); 936 Location->push_back(V); 937 } 938}; 939 940 941// Define how to hold a class type object, such as a string. Since we can 942// inherit from a class, we do so. This makes us exactly compatible with the 943// object in all cases that it is used. 944// 945template<class DataType> 946class list_storage<DataType, bool> : public std::vector<DataType> { 947public: 948 template<class T> 949 void addValue(const T &V) { push_back(V); } 950}; 951 952 953//===----------------------------------------------------------------------===// 954// list - A list of command line options. 955// 956template <class DataType, class Storage = bool, 957 class ParserClass = parser<DataType> > 958class list : public Option, public list_storage<DataType, Storage> { 959 std::vector<unsigned> Positions; 960 ParserClass Parser; 961 962 virtual enum ValueExpected getValueExpectedFlagDefault() const { 963 return Parser.getValueExpectedFlagDefault(); 964 } 965 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) { 966 return Parser.getExtraOptionNames(OptionNames); 967 } 968 969 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 970 const std::string &Arg) { 971 typename ParserClass::parser_data_type Val = 972 typename ParserClass::parser_data_type(); 973 if (Parser.parse(*this, ArgName, Arg, Val)) 974 return true; // Parse Error! 975 addValue(Val); 976 setPosition(pos); 977 Positions.push_back(pos); 978 return false; 979 } 980 981 // Forward printing stuff to the parser... 982 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);} 983 virtual void printOptionInfo(unsigned GlobalWidth) const { 984 Parser.printOptionInfo(*this, GlobalWidth); 985 } 986 987 void done() { 988 addArgument(); 989 Parser.initialize(*this); 990 } 991public: 992 ParserClass &getParser() { return Parser; } 993 994 unsigned getPosition(unsigned optnum) const { 995 assert(optnum < this->size() && "Invalid option index"); 996 return Positions[optnum]; 997 } 998 999 // One option... 1000 template<class M0t> 1001 list(const M0t &M0) : Option(ZeroOrMore | NotHidden) { 1002 apply(M0, this); 1003 done(); 1004 } 1005 // Two options... 1006 template<class M0t, class M1t> 1007 list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) { 1008 apply(M0, this); apply(M1, this); 1009 done(); 1010 } 1011 // Three options... 1012 template<class M0t, class M1t, class M2t> 1013 list(const M0t &M0, const M1t &M1, const M2t &M2) 1014 : Option(ZeroOrMore | NotHidden) { 1015 apply(M0, this); apply(M1, this); apply(M2, this); 1016 done(); 1017 } 1018 // Four options... 1019 template<class M0t, class M1t, class M2t, class M3t> 1020 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 1021 : Option(ZeroOrMore | NotHidden) { 1022 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1023 done(); 1024 } 1025 // Five options... 1026 template<class M0t, class M1t, class M2t, class M3t, class M4t> 1027 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1028 const M4t &M4) : Option(ZeroOrMore | NotHidden) { 1029 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1030 apply(M4, this); 1031 done(); 1032 } 1033 // Six options... 1034 template<class M0t, class M1t, class M2t, class M3t, 1035 class M4t, class M5t> 1036 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1037 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) { 1038 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1039 apply(M4, this); apply(M5, this); 1040 done(); 1041 } 1042 // Seven options... 1043 template<class M0t, class M1t, class M2t, class M3t, 1044 class M4t, class M5t, class M6t> 1045 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1046 const M4t &M4, const M5t &M5, const M6t &M6) 1047 : Option(ZeroOrMore | NotHidden) { 1048 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1049 apply(M4, this); apply(M5, this); apply(M6, this); 1050 done(); 1051 } 1052 // Eight options... 1053 template<class M0t, class M1t, class M2t, class M3t, 1054 class M4t, class M5t, class M6t, class M7t> 1055 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1056 const M4t &M4, const M5t &M5, const M6t &M6, 1057 const M7t &M7) : Option(ZeroOrMore | NotHidden) { 1058 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1059 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 1060 done(); 1061 } 1062}; 1063 1064//===----------------------------------------------------------------------===// 1065// bits_storage class 1066 1067// Default storage class definition: external storage. This implementation 1068// assumes the user will specify a variable to store the data into with the 1069// cl::location(x) modifier. 1070// 1071template<class DataType, class StorageClass> 1072class bits_storage { 1073 unsigned *Location; // Where to store the bits... 1074 1075 template<class T> 1076 static unsigned Bit(const T &V) { 1077 unsigned BitPos = reinterpret_cast<unsigned>(V); 1078 assert(BitPos < sizeof(unsigned) * 8 && 1079 "enum exceeds width of bit vector!"); 1080 return 1 << BitPos; 1081 } 1082 1083public: 1084 bits_storage() : Location(0) {} 1085 1086 bool setLocation(Option &O, unsigned &L) { 1087 if (Location) 1088 return O.error(": cl::location(x) specified more than once!"); 1089 Location = &L; 1090 return false; 1091 } 1092 1093 template<class T> 1094 void addValue(const T &V) { 1095 assert(Location != 0 && "cl::location(...) not specified for a command " 1096 "line option with external storage!"); 1097 *Location |= Bit(V); 1098 } 1099 1100 unsigned getBits() { return *Location; } 1101 1102 template<class T> 1103 bool isSet(const T &V) { 1104 return (*Location & Bit(V)) != 0; 1105 } 1106}; 1107 1108 1109// Define how to hold bits. Since we can inherit from a class, we do so. 1110// This makes us exactly compatible with the bits in all cases that it is used. 1111// 1112template<class DataType> 1113class bits_storage<DataType, bool> { 1114 unsigned Bits; // Where to store the bits... 1115 1116 template<class T> 1117 static unsigned Bit(const T &V) { 1118 unsigned BitPos = reinterpret_cast<unsigned>(V); 1119 assert(BitPos < sizeof(unsigned) * 8 && 1120 "enum exceeds width of bit vector!"); 1121 return 1 << BitPos; 1122 } 1123 1124public: 1125 template<class T> 1126 void addValue(const T &V) { 1127 Bits |= Bit(V); 1128 } 1129 1130 unsigned getBits() { return Bits; } 1131 1132 template<class T> 1133 bool isSet(const T &V) { 1134 return (Bits & Bit(V)) != 0; 1135 } 1136}; 1137 1138 1139//===----------------------------------------------------------------------===// 1140// bits - A bit vector of command options. 1141// 1142template <class DataType, class Storage = bool, 1143 class ParserClass = parser<DataType> > 1144class bits : public Option, public bits_storage<DataType, Storage> { 1145 std::vector<unsigned> Positions; 1146 ParserClass Parser; 1147 1148 virtual enum ValueExpected getValueExpectedFlagDefault() const { 1149 return Parser.getValueExpectedFlagDefault(); 1150 } 1151 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) { 1152 return Parser.getExtraOptionNames(OptionNames); 1153 } 1154 1155 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 1156 const std::string &Arg) { 1157 typename ParserClass::parser_data_type Val = 1158 typename ParserClass::parser_data_type(); 1159 if (Parser.parse(*this, ArgName, Arg, Val)) 1160 return true; // Parse Error! 1161 addValue(Val); 1162 setPosition(pos); 1163 Positions.push_back(pos); 1164 return false; 1165 } 1166 1167 // Forward printing stuff to the parser... 1168 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);} 1169 virtual void printOptionInfo(unsigned GlobalWidth) const { 1170 Parser.printOptionInfo(*this, GlobalWidth); 1171 } 1172 1173 void done() { 1174 addArgument(); 1175 Parser.initialize(*this); 1176 } 1177public: 1178 ParserClass &getParser() { return Parser; } 1179 1180 unsigned getPosition(unsigned optnum) const { 1181 assert(optnum < this->size() && "Invalid option index"); 1182 return Positions[optnum]; 1183 } 1184 1185 // One option... 1186 template<class M0t> 1187 bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) { 1188 apply(M0, this); 1189 done(); 1190 } 1191 // Two options... 1192 template<class M0t, class M1t> 1193 bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) { 1194 apply(M0, this); apply(M1, this); 1195 done(); 1196 } 1197 // Three options... 1198 template<class M0t, class M1t, class M2t> 1199 bits(const M0t &M0, const M1t &M1, const M2t &M2) 1200 : Option(ZeroOrMore | NotHidden) { 1201 apply(M0, this); apply(M1, this); apply(M2, this); 1202 done(); 1203 } 1204 // Four options... 1205 template<class M0t, class M1t, class M2t, class M3t> 1206 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 1207 : Option(ZeroOrMore | NotHidden) { 1208 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1209 done(); 1210 } 1211 // Five options... 1212 template<class M0t, class M1t, class M2t, class M3t, class M4t> 1213 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1214 const M4t &M4) : Option(ZeroOrMore | NotHidden) { 1215 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1216 apply(M4, this); 1217 done(); 1218 } 1219 // Six options... 1220 template<class M0t, class M1t, class M2t, class M3t, 1221 class M4t, class M5t> 1222 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1223 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) { 1224 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1225 apply(M4, this); apply(M5, this); 1226 done(); 1227 } 1228 // Seven options... 1229 template<class M0t, class M1t, class M2t, class M3t, 1230 class M4t, class M5t, class M6t> 1231 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1232 const M4t &M4, const M5t &M5, const M6t &M6) 1233 : Option(ZeroOrMore | NotHidden) { 1234 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1235 apply(M4, this); apply(M5, this); apply(M6, this); 1236 done(); 1237 } 1238 // Eight options... 1239 template<class M0t, class M1t, class M2t, class M3t, 1240 class M4t, class M5t, class M6t, class M7t> 1241 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1242 const M4t &M4, const M5t &M5, const M6t &M6, 1243 const M7t &M7) : Option(ZeroOrMore | NotHidden) { 1244 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1245 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 1246 done(); 1247 } 1248}; 1249 1250//===----------------------------------------------------------------------===// 1251// Aliased command line option (alias this name to a preexisting name) 1252// 1253 1254class alias : public Option { 1255 Option *AliasFor; 1256 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 1257 const std::string &Arg) { 1258 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg); 1259 } 1260 // Handle printing stuff... 1261 virtual unsigned getOptionWidth() const; 1262 virtual void printOptionInfo(unsigned GlobalWidth) const; 1263 1264 void done() { 1265 if (!hasArgStr()) 1266 error(": cl::alias must have argument name specified!"); 1267 if (AliasFor == 0) 1268 error(": cl::alias must have an cl::aliasopt(option) specified!"); 1269 addArgument(); 1270 } 1271public: 1272 void setAliasFor(Option &O) { 1273 if (AliasFor) 1274 error(": cl::alias must only have one cl::aliasopt(...) specified!"); 1275 AliasFor = &O; 1276 } 1277 1278 // One option... 1279 template<class M0t> 1280 alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) { 1281 apply(M0, this); 1282 done(); 1283 } 1284 // Two options... 1285 template<class M0t, class M1t> 1286 alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) { 1287 apply(M0, this); apply(M1, this); 1288 done(); 1289 } 1290 // Three options... 1291 template<class M0t, class M1t, class M2t> 1292 alias(const M0t &M0, const M1t &M1, const M2t &M2) 1293 : Option(Optional | Hidden), AliasFor(0) { 1294 apply(M0, this); apply(M1, this); apply(M2, this); 1295 done(); 1296 } 1297 // Four options... 1298 template<class M0t, class M1t, class M2t, class M3t> 1299 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 1300 : Option(Optional | Hidden), AliasFor(0) { 1301 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1302 done(); 1303 } 1304}; 1305 1306// aliasfor - Modifier to set the option an alias aliases. 1307struct aliasopt { 1308 Option &Opt; 1309 aliasopt(Option &O) : Opt(O) {} 1310 void apply(alias &A) const { A.setAliasFor(Opt); } 1311}; 1312 1313// extrahelp - provide additional help at the end of the normal help 1314// output. All occurrences of cl::extrahelp will be accumulated and 1315// printed to std::cerr at the end of the regular help, just before 1316// exit is called. 1317struct extrahelp { 1318 const char * morehelp; 1319 extrahelp(const char* help); 1320}; 1321 1322void PrintVersionMessage(); 1323// This function just prints the help message, exactly the same way as if the 1324// --help option had been given on the command line. 1325// NOTE: THIS FUNCTION TERMINATES THE PROGRAM! 1326void PrintHelpMessage(); 1327 1328} // End namespace cl 1329 1330} // End namespace llvm 1331 1332#endif 1333