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