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