1193323Sed//===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This class implements a command line argument processor that is useful when 11193323Sed// creating a tool. It provides a simple, minimalistic interface that is easily 12193323Sed// extensible and supports nonlocal (library) command line options. 13193323Sed// 14193323Sed// Note that rather than trying to figure out what this code does, you should 15193323Sed// read the library documentation located in docs/CommandLine.html or looks at 16193323Sed// the many example usages in tools/*/*.cpp 17193323Sed// 18193323Sed//===----------------------------------------------------------------------===// 19193323Sed 20193323Sed#ifndef LLVM_SUPPORT_COMMANDLINE_H 21193323Sed#define LLVM_SUPPORT_COMMANDLINE_H 22193323Sed 23193323Sed#include "llvm/ADT/SmallVector.h" 24198090Srdivacky#include "llvm/ADT/Twine.h" 25251662Sdim#include "llvm/ADT/StringMap.h" 26249423Sdim#include "llvm/Support/Compiler.h" 27249423Sdim#include "llvm/Support/type_traits.h" 28193323Sed#include <cassert> 29193323Sed#include <climits> 30193323Sed#include <cstdarg> 31193323Sed#include <utility> 32193323Sed#include <vector> 33193323Sed 34193323Sednamespace llvm { 35212904Sdim 36193323Sed/// cl Namespace - This namespace contains all of the command line option 37193323Sed/// processing machinery. It is intentionally a short name to make qualified 38193323Sed/// usage concise. 39193323Sednamespace cl { 40193323Sed 41193323Sed//===----------------------------------------------------------------------===// 42193323Sed// ParseCommandLineOptions - Command line option processing entry point. 43193323Sed// 44234353Sdimvoid ParseCommandLineOptions(int argc, const char * const *argv, 45243830Sdim const char *Overview = 0); 46193323Sed 47193323Sed//===----------------------------------------------------------------------===// 48193323Sed// ParseEnvironmentOptions - Environment variable option processing alternate 49193323Sed// entry point. 50193323Sed// 51193323Sedvoid ParseEnvironmentOptions(const char *progName, const char *envvar, 52243830Sdim const char *Overview = 0); 53193323Sed 54193323Sed///===---------------------------------------------------------------------===// 55193323Sed/// SetVersionPrinter - Override the default (LLVM specific) version printer 56193323Sed/// used to print out the version when --version is given 57193323Sed/// on the command line. This allows other systems using the 58193323Sed/// CommandLine utilities to print their own version string. 59193323Sedvoid SetVersionPrinter(void (*func)()); 60193323Sed 61226633Sdim///===---------------------------------------------------------------------===// 62226633Sdim/// AddExtraVersionPrinter - Add an extra printer to use in addition to the 63226633Sdim/// default one. This can be called multiple times, 64226633Sdim/// and each time it adds a new function to the list 65226633Sdim/// which will be called after the basic LLVM version 66226633Sdim/// printing is complete. Each can then add additional 67226633Sdim/// information specific to the tool. 68226633Sdimvoid AddExtraVersionPrinter(void (*func)()); 69193323Sed 70226633Sdim 71221345Sdim// PrintOptionValues - Print option values. 72221345Sdim// With -print-options print the difference between option values and defaults. 73221345Sdim// With -print-all-options print all option values. 74221345Sdim// (Currently not perfect, but best-effort.) 75221345Sdimvoid PrintOptionValues(); 76221345Sdim 77193323Sed// MarkOptionsChanged - Internal helper function. 78193323Sedvoid MarkOptionsChanged(); 79193323Sed 80193323Sed//===----------------------------------------------------------------------===// 81193323Sed// Flags permitted to be passed to command line arguments 82193323Sed// 83193323Sed 84198090Srdivackyenum NumOccurrencesFlag { // Flags for the number of occurrences allowed 85234353Sdim Optional = 0x00, // Zero or One occurrence 86234353Sdim ZeroOrMore = 0x01, // Zero or more occurrences allowed 87234353Sdim Required = 0x02, // One occurrence required 88234353Sdim OneOrMore = 0x03, // One or more occurrences required 89193323Sed 90193323Sed // ConsumeAfter - Indicates that this option is fed anything that follows the 91193323Sed // last positional argument required by the application (it is an error if 92193323Sed // there are zero positional arguments, and a ConsumeAfter option is used). 93193323Sed // Thus, for example, all arguments to LLI are processed until a filename is 94193323Sed // found. Once a filename is found, all of the succeeding arguments are 95193323Sed // passed, unprocessed, to the ConsumeAfter option. 96193323Sed // 97234353Sdim ConsumeAfter = 0x04 98193323Sed}; 99193323Sed 100193323Sedenum ValueExpected { // Is a value required for the option? 101234353Sdim // zero reserved for the unspecified value 102234353Sdim ValueOptional = 0x01, // The value can appear... or not 103234353Sdim ValueRequired = 0x02, // The value is required to appear! 104234353Sdim ValueDisallowed = 0x03 // A value may not be specified (for flags) 105193323Sed}; 106193323Sed 107193323Sedenum OptionHidden { // Control whether -help shows this option 108234353Sdim NotHidden = 0x00, // Option included in -help & -help-hidden 109234353Sdim Hidden = 0x01, // -help doesn't, but -help-hidden does 110234353Sdim ReallyHidden = 0x02 // Neither -help nor -help-hidden show this arg 111193323Sed}; 112193323Sed 113193323Sed// Formatting flags - This controls special features that the option might have 114193323Sed// that cause it to be parsed differently... 115193323Sed// 116193323Sed// Prefix - This option allows arguments that are otherwise unrecognized to be 117193323Sed// matched by options that are a prefix of the actual value. This is useful for 118193323Sed// cases like a linker, where options are typically of the form '-lfoo' or 119193323Sed// '-L../../include' where -l or -L are the actual flags. When prefix is 120193323Sed// enabled, and used, the value for the flag comes from the suffix of the 121193323Sed// argument. 122193323Sed// 123193323Sed// Grouping - With this option enabled, multiple letter options are allowed to 124193323Sed// bunch together with only a single hyphen for the whole group. This allows 125193323Sed// emulation of the behavior that ls uses for example: ls -la === ls -l -a 126193323Sed// 127193323Sed 128193323Sedenum FormattingFlags { 129234353Sdim NormalFormatting = 0x00, // Nothing special 130234353Sdim Positional = 0x01, // Is a positional argument, no '-' required 131234353Sdim Prefix = 0x02, // Can this option directly prefix its value? 132234353Sdim Grouping = 0x03 // Can this option group with other options? 133193323Sed}; 134193323Sed 135193323Sedenum MiscFlags { // Miscellaneous flags to adjust argument 136234353Sdim CommaSeparated = 0x01, // Should this cl::list split between commas? 137234353Sdim PositionalEatsArgs = 0x02, // Should this positional cl::list eat -args? 138234353Sdim Sink = 0x04 // Should this cl::list eat all unknown options? 139193323Sed}; 140193323Sed 141251662Sdim//===----------------------------------------------------------------------===// 142251662Sdim// Option Category class 143251662Sdim// 144251662Sdimclass OptionCategory { 145251662Sdimprivate: 146251662Sdim const char *const Name; 147251662Sdim const char *const Description; 148251662Sdim void registerCategory(); 149251662Sdimpublic: 150251662Sdim OptionCategory(const char *const Name, const char *const Description = 0) 151251662Sdim : Name(Name), Description(Description) { registerCategory(); } 152251662Sdim const char *getName() { return Name; } 153251662Sdim const char *getDescription() { return Description; } 154251662Sdim}; 155193323Sed 156251662Sdim// The general Option Category (used as default category). 157251662Sdimextern OptionCategory GeneralCategory; 158193323Sed 159193323Sed//===----------------------------------------------------------------------===// 160193323Sed// Option Base class 161193323Sed// 162193323Sedclass alias; 163193323Sedclass Option { 164193323Sed friend class alias; 165193323Sed 166193323Sed // handleOccurrences - Overriden by subclasses to handle the value passed into 167193323Sed // an argument. Should return true if there was an error processing the 168193323Sed // argument and the program should exit. 169193323Sed // 170198090Srdivacky virtual bool handleOccurrence(unsigned pos, StringRef ArgName, 171198090Srdivacky StringRef Arg) = 0; 172193323Sed 173193323Sed virtual enum ValueExpected getValueExpectedFlagDefault() const { 174193323Sed return ValueOptional; 175193323Sed } 176193323Sed 177193323Sed // Out of line virtual function to provide home for the class. 178193323Sed virtual void anchor(); 179193323Sed 180193323Sed int NumOccurrences; // The number of times specified 181234353Sdim // Occurrences, HiddenFlag, and Formatting are all enum types but to avoid 182234353Sdim // problems with signed enums in bitfields. 183234353Sdim unsigned Occurrences : 3; // enum NumOccurrencesFlag 184234353Sdim // not using the enum type for 'Value' because zero is an implementation 185234353Sdim // detail representing the non-value 186234353Sdim unsigned Value : 2; 187234353Sdim unsigned HiddenFlag : 2; // enum OptionHidden 188234353Sdim unsigned Formatting : 2; // enum FormattingFlags 189234353Sdim unsigned Misc : 3; 190193323Sed unsigned Position; // Position of last occurrence of the option 191193323Sed unsigned AdditionalVals;// Greater than 0 for multi-valued option. 192193323Sed Option *NextRegistered; // Singly linked list of registered options. 193251662Sdim 194193323Sedpublic: 195251662Sdim const char *ArgStr; // The argument string itself (ex: "help", "o") 196251662Sdim const char *HelpStr; // The descriptive text message for -help 197251662Sdim const char *ValueStr; // String describing what the value of this option is 198251662Sdim OptionCategory *Category; // The Category this option belongs to 199193323Sed 200198090Srdivacky inline enum NumOccurrencesFlag getNumOccurrencesFlag() const { 201234353Sdim return (enum NumOccurrencesFlag)Occurrences; 202193323Sed } 203193323Sed inline enum ValueExpected getValueExpectedFlag() const { 204234353Sdim return Value ? ((enum ValueExpected)Value) 205193323Sed : getValueExpectedFlagDefault(); 206193323Sed } 207193323Sed inline enum OptionHidden getOptionHiddenFlag() const { 208234353Sdim return (enum OptionHidden)HiddenFlag; 209193323Sed } 210193323Sed inline enum FormattingFlags getFormattingFlag() const { 211234353Sdim return (enum FormattingFlags)Formatting; 212193323Sed } 213193323Sed inline unsigned getMiscFlags() const { 214234353Sdim return Misc; 215193323Sed } 216193323Sed inline unsigned getPosition() const { return Position; } 217193323Sed inline unsigned getNumAdditionalVals() const { return AdditionalVals; } 218193323Sed 219193323Sed // hasArgStr - Return true if the argstr != "" 220193323Sed bool hasArgStr() const { return ArgStr[0] != 0; } 221193323Sed 222193323Sed //-------------------------------------------------------------------------=== 223193323Sed // Accessor functions set by OptionModifiers 224193323Sed // 225193323Sed void setArgStr(const char *S) { ArgStr = S; } 226193323Sed void setDescription(const char *S) { HelpStr = S; } 227193323Sed void setValueStr(const char *S) { ValueStr = S; } 228198090Srdivacky void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { 229234353Sdim Occurrences = Val; 230193323Sed } 231234353Sdim void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; } 232234353Sdim void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; } 233234353Sdim void setFormattingFlag(enum FormattingFlags V) { Formatting = V; } 234234353Sdim void setMiscFlag(enum MiscFlags M) { Misc |= M; } 235193323Sed void setPosition(unsigned pos) { Position = pos; } 236251662Sdim void setCategory(OptionCategory &C) { Category = &C; } 237193323Sedprotected: 238239462Sdim explicit Option(enum NumOccurrencesFlag OccurrencesFlag, 239234353Sdim enum OptionHidden Hidden) 240239462Sdim : NumOccurrences(0), Occurrences(OccurrencesFlag), Value(0), 241239462Sdim HiddenFlag(Hidden), Formatting(NormalFormatting), Misc(0), 242239462Sdim Position(0), AdditionalVals(0), NextRegistered(0), 243251662Sdim ArgStr(""), HelpStr(""), ValueStr(""), Category(&GeneralCategory) { 244193323Sed } 245193323Sed 246198090Srdivacky inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; } 247193323Sedpublic: 248193323Sed // addArgument - Register this argument with the commandline system. 249193323Sed // 250193323Sed void addArgument(); 251193323Sed 252193323Sed Option *getNextRegisteredOption() const { return NextRegistered; } 253193323Sed 254193323Sed // Return the width of the option tag for printing... 255193323Sed virtual size_t getOptionWidth() const = 0; 256193323Sed 257193323Sed // printOptionInfo - Print out information about this option. The 258193323Sed // to-be-maintained width is specified. 259193323Sed // 260193323Sed virtual void printOptionInfo(size_t GlobalWidth) const = 0; 261193323Sed 262221345Sdim virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0; 263221345Sdim 264198090Srdivacky virtual void getExtraOptionNames(SmallVectorImpl<const char*> &) {} 265193323Sed 266198090Srdivacky // addOccurrence - Wrapper around handleOccurrence that enforces Flags. 267193323Sed // 268198090Srdivacky bool addOccurrence(unsigned pos, StringRef ArgName, 269198090Srdivacky StringRef Value, bool MultiArg = false); 270193323Sed 271193323Sed // Prints option name followed by message. Always returns true. 272198090Srdivacky bool error(const Twine &Message, StringRef ArgName = StringRef()); 273193323Sed 274193323Sedpublic: 275193323Sed inline int getNumOccurrences() const { return NumOccurrences; } 276193323Sed virtual ~Option() {} 277193323Sed}; 278193323Sed 279193323Sed 280193323Sed//===----------------------------------------------------------------------===// 281193323Sed// Command line option modifiers that can be used to modify the behavior of 282193323Sed// command line option parsers... 283193323Sed// 284193323Sed 285204642Srdivacky// desc - Modifier to set the description shown in the -help output... 286193323Sedstruct desc { 287193323Sed const char *Desc; 288193323Sed desc(const char *Str) : Desc(Str) {} 289193323Sed void apply(Option &O) const { O.setDescription(Desc); } 290193323Sed}; 291193323Sed 292204642Srdivacky// value_desc - Modifier to set the value description shown in the -help 293193323Sed// output... 294193323Sedstruct value_desc { 295193323Sed const char *Desc; 296193323Sed value_desc(const char *Str) : Desc(Str) {} 297193323Sed void apply(Option &O) const { O.setValueStr(Desc); } 298193323Sed}; 299193323Sed 300193323Sed// init - Specify a default (initial) value for the command line argument, if 301193323Sed// the default constructor for the argument type does not give you what you 302193323Sed// want. This is only valid on "opt" arguments, not on "list" arguments. 303193323Sed// 304193323Sedtemplate<class Ty> 305193323Sedstruct initializer { 306193323Sed const Ty &Init; 307193323Sed initializer(const Ty &Val) : Init(Val) {} 308193323Sed 309193323Sed template<class Opt> 310193323Sed void apply(Opt &O) const { O.setInitialValue(Init); } 311193323Sed}; 312193323Sed 313193323Sedtemplate<class Ty> 314193323Sedinitializer<Ty> init(const Ty &Val) { 315193323Sed return initializer<Ty>(Val); 316193323Sed} 317193323Sed 318193323Sed 319193323Sed// location - Allow the user to specify which external variable they want to 320193323Sed// store the results of the command line argument processing into, if they don't 321193323Sed// want to store it in the option itself. 322193323Sed// 323193323Sedtemplate<class Ty> 324193323Sedstruct LocationClass { 325193323Sed Ty &Loc; 326193323Sed LocationClass(Ty &L) : Loc(L) {} 327193323Sed 328193323Sed template<class Opt> 329193323Sed void apply(Opt &O) const { O.setLocation(O, Loc); } 330193323Sed}; 331193323Sed 332193323Sedtemplate<class Ty> 333193323SedLocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); } 334193323Sed 335251662Sdim// cat - Specifiy the Option category for the command line argument to belong 336251662Sdim// to. 337251662Sdimstruct cat { 338251662Sdim OptionCategory &Category; 339251662Sdim cat(OptionCategory &c) : Category(c) {} 340193323Sed 341251662Sdim template<class Opt> 342251662Sdim void apply(Opt &O) const { O.setCategory(Category); } 343251662Sdim}; 344251662Sdim 345251662Sdim 346193323Sed//===----------------------------------------------------------------------===// 347221345Sdim// OptionValue class 348221345Sdim 349221345Sdim// Support value comparison outside the template. 350221345Sdimstruct GenericOptionValue { 351221345Sdim virtual ~GenericOptionValue() {} 352221345Sdim virtual bool compare(const GenericOptionValue &V) const = 0; 353263508Sdim 354234353Sdimprivate: 355234353Sdim virtual void anchor(); 356221345Sdim}; 357221345Sdim 358221345Sdimtemplate<class DataType> struct OptionValue; 359221345Sdim 360221345Sdim// The default value safely does nothing. Option value printing is only 361221345Sdim// best-effort. 362221345Sdimtemplate<class DataType, bool isClass> 363221345Sdimstruct OptionValueBase : public GenericOptionValue { 364221345Sdim // Temporary storage for argument passing. 365221345Sdim typedef OptionValue<DataType> WrapperType; 366221345Sdim 367221345Sdim bool hasValue() const { return false; } 368221345Sdim 369234353Sdim const DataType &getValue() const { llvm_unreachable("no default value"); } 370221345Sdim 371221345Sdim // Some options may take their value from a different data type. 372221345Sdim template<class DT> 373221345Sdim void setValue(const DT& /*V*/) {} 374221345Sdim 375221345Sdim bool compare(const DataType &/*V*/) const { return false; } 376221345Sdim 377221345Sdim virtual bool compare(const GenericOptionValue& /*V*/) const { return false; } 378221345Sdim}; 379221345Sdim 380221345Sdim// Simple copy of the option value. 381221345Sdimtemplate<class DataType> 382221345Sdimclass OptionValueCopy : public GenericOptionValue { 383221345Sdim DataType Value; 384221345Sdim bool Valid; 385221345Sdimpublic: 386221345Sdim OptionValueCopy() : Valid(false) {} 387221345Sdim 388221345Sdim bool hasValue() const { return Valid; } 389221345Sdim 390221345Sdim const DataType &getValue() const { 391221345Sdim assert(Valid && "invalid option value"); 392221345Sdim return Value; 393221345Sdim } 394221345Sdim 395221345Sdim void setValue(const DataType &V) { Valid = true; Value = V; } 396221345Sdim 397221345Sdim bool compare(const DataType &V) const { 398221345Sdim return Valid && (Value != V); 399221345Sdim } 400221345Sdim 401221345Sdim virtual bool compare(const GenericOptionValue &V) const { 402221345Sdim const OptionValueCopy<DataType> &VC = 403221345Sdim static_cast< const OptionValueCopy<DataType>& >(V); 404221345Sdim if (!VC.hasValue()) return false; 405221345Sdim return compare(VC.getValue()); 406221345Sdim } 407221345Sdim}; 408221345Sdim 409221345Sdim// Non-class option values. 410221345Sdimtemplate<class DataType> 411221345Sdimstruct OptionValueBase<DataType, false> : OptionValueCopy<DataType> { 412221345Sdim typedef DataType WrapperType; 413221345Sdim}; 414221345Sdim 415221345Sdim// Top-level option class. 416221345Sdimtemplate<class DataType> 417221345Sdimstruct OptionValue : OptionValueBase<DataType, is_class<DataType>::value> { 418221345Sdim OptionValue() {} 419221345Sdim 420221345Sdim OptionValue(const DataType& V) { 421221345Sdim this->setValue(V); 422221345Sdim } 423221345Sdim // Some options may take their value from a different data type. 424221345Sdim template<class DT> 425221345Sdim OptionValue<DataType> &operator=(const DT& V) { 426221345Sdim this->setValue(V); 427221345Sdim return *this; 428221345Sdim } 429221345Sdim}; 430221345Sdim 431221345Sdim// Other safe-to-copy-by-value common option types. 432221345Sdimenum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE }; 433221345Sdimtemplate<> 434221345Sdimstruct OptionValue<cl::boolOrDefault> : OptionValueCopy<cl::boolOrDefault> { 435221345Sdim typedef cl::boolOrDefault WrapperType; 436221345Sdim 437221345Sdim OptionValue() {} 438221345Sdim 439221345Sdim OptionValue(const cl::boolOrDefault& V) { 440221345Sdim this->setValue(V); 441221345Sdim } 442221345Sdim OptionValue<cl::boolOrDefault> &operator=(const cl::boolOrDefault& V) { 443221345Sdim setValue(V); 444221345Sdim return *this; 445221345Sdim } 446234353Sdimprivate: 447234353Sdim virtual void anchor(); 448221345Sdim}; 449221345Sdim 450221345Sdimtemplate<> 451221345Sdimstruct OptionValue<std::string> : OptionValueCopy<std::string> { 452221345Sdim typedef StringRef WrapperType; 453221345Sdim 454221345Sdim OptionValue() {} 455221345Sdim 456221345Sdim OptionValue(const std::string& V) { 457221345Sdim this->setValue(V); 458221345Sdim } 459221345Sdim OptionValue<std::string> &operator=(const std::string& V) { 460221345Sdim setValue(V); 461221345Sdim return *this; 462221345Sdim } 463234353Sdimprivate: 464234353Sdim virtual void anchor(); 465221345Sdim}; 466221345Sdim 467221345Sdim//===----------------------------------------------------------------------===// 468193323Sed// Enum valued command line option 469193323Sed// 470193323Sed#define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC 471193323Sed#define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC 472193323Sed#define clEnumValEnd (reinterpret_cast<void*>(0)) 473193323Sed 474193323Sed// values - For custom data types, allow specifying a group of values together 475193323Sed// as the values that go into the mapping that the option handler uses. Note 476193323Sed// that the values list must always have a 0 at the end of the list to indicate 477193323Sed// that the list has ended. 478193323Sed// 479193323Sedtemplate<class DataType> 480193323Sedclass ValuesClass { 481193323Sed // Use a vector instead of a map, because the lists should be short, 482193323Sed // the overhead is less, and most importantly, it keeps them in the order 483193323Sed // inserted so we can print our option out nicely. 484193323Sed SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values; 485193323Sed void processValues(va_list Vals); 486193323Sedpublic: 487193323Sed ValuesClass(const char *EnumName, DataType Val, const char *Desc, 488193323Sed va_list ValueArgs) { 489193323Sed // Insert the first value, which is required. 490193323Sed Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc))); 491193323Sed 492193323Sed // Process the varargs portion of the values... 493193323Sed while (const char *enumName = va_arg(ValueArgs, const char *)) { 494193323Sed DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int)); 495193323Sed const char *EnumDesc = va_arg(ValueArgs, const char *); 496193323Sed Values.push_back(std::make_pair(enumName, // Add value to value map 497193323Sed std::make_pair(EnumVal, EnumDesc))); 498193323Sed } 499193323Sed } 500193323Sed 501193323Sed template<class Opt> 502193323Sed void apply(Opt &O) const { 503249423Sdim for (size_t i = 0, e = Values.size(); i != e; ++i) 504193323Sed O.getParser().addLiteralOption(Values[i].first, Values[i].second.first, 505193323Sed Values[i].second.second); 506193323Sed } 507193323Sed}; 508193323Sed 509193323Sedtemplate<class DataType> 510193323SedValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val, 511193323Sed const char *Desc, ...) { 512193323Sed va_list ValueArgs; 513193323Sed va_start(ValueArgs, Desc); 514193323Sed ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs); 515193323Sed va_end(ValueArgs); 516193323Sed return Vals; 517193323Sed} 518193323Sed 519193323Sed//===----------------------------------------------------------------------===// 520193323Sed// parser class - Parameterizable parser for different data types. By default, 521193323Sed// known data types (string, int, bool) have specialized parsers, that do what 522193323Sed// you would expect. The default parser, used for data types that are not 523193323Sed// built-in, uses a mapping table to map specific options to values, which is 524193323Sed// used, among other things, to handle enum types. 525193323Sed 526193323Sed//-------------------------------------------------- 527193323Sed// generic_parser_base - This class holds all the non-generic code that we do 528193323Sed// not need replicated for every instance of the generic parser. This also 529193323Sed// allows us to put stuff into CommandLine.cpp 530193323Sed// 531221345Sdimclass generic_parser_base { 532221345Sdimprotected: 533221345Sdim class GenericOptionInfo { 534221345Sdim public: 535221345Sdim GenericOptionInfo(const char *name, const char *helpStr) : 536221345Sdim Name(name), HelpStr(helpStr) {} 537221345Sdim const char *Name; 538221345Sdim const char *HelpStr; 539221345Sdim }; 540221345Sdimpublic: 541193323Sed virtual ~generic_parser_base() {} // Base class should have virtual-dtor 542193323Sed 543193323Sed // getNumOptions - Virtual function implemented by generic subclass to 544193323Sed // indicate how many entries are in Values. 545193323Sed // 546193323Sed virtual unsigned getNumOptions() const = 0; 547193323Sed 548193323Sed // getOption - Return option name N. 549193323Sed virtual const char *getOption(unsigned N) const = 0; 550193323Sed 551193323Sed // getDescription - Return description N 552193323Sed virtual const char *getDescription(unsigned N) const = 0; 553193323Sed 554193323Sed // Return the width of the option tag for printing... 555193323Sed virtual size_t getOptionWidth(const Option &O) const; 556193323Sed 557221345Sdim virtual const GenericOptionValue &getOptionValue(unsigned N) const = 0; 558221345Sdim 559193323Sed // printOptionInfo - Print out information about this option. The 560193323Sed // to-be-maintained width is specified. 561193323Sed // 562193323Sed virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const; 563193323Sed 564221345Sdim void printGenericOptionDiff(const Option &O, const GenericOptionValue &V, 565221345Sdim const GenericOptionValue &Default, 566221345Sdim size_t GlobalWidth) const; 567221345Sdim 568221345Sdim // printOptionDiff - print the value of an option and it's default. 569221345Sdim // 570221345Sdim // Template definition ensures that the option and default have the same 571221345Sdim // DataType (via the same AnyOptionValue). 572221345Sdim template<class AnyOptionValue> 573221345Sdim void printOptionDiff(const Option &O, const AnyOptionValue &V, 574221345Sdim const AnyOptionValue &Default, 575221345Sdim size_t GlobalWidth) const { 576221345Sdim printGenericOptionDiff(O, V, Default, GlobalWidth); 577221345Sdim } 578221345Sdim 579193323Sed void initialize(Option &O) { 580193323Sed // All of the modifiers for the option have been processed by now, so the 581193323Sed // argstr field should be stable, copy it down now. 582193323Sed // 583193323Sed hasArgStr = O.hasArgStr(); 584193323Sed } 585193323Sed 586198090Srdivacky void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) { 587193323Sed // If there has been no argstr specified, that means that we need to add an 588193323Sed // argument for every possible option. This ensures that our options are 589193323Sed // vectored to us. 590193323Sed if (!hasArgStr) 591193323Sed for (unsigned i = 0, e = getNumOptions(); i != e; ++i) 592193323Sed OptionNames.push_back(getOption(i)); 593193323Sed } 594193323Sed 595193323Sed 596193323Sed enum ValueExpected getValueExpectedFlagDefault() const { 597193323Sed // If there is an ArgStr specified, then we are of the form: 598193323Sed // 599193323Sed // -opt=O2 or -opt O2 or -optO2 600193323Sed // 601193323Sed // In which case, the value is required. Otherwise if an arg str has not 602193323Sed // been specified, we are of the form: 603193323Sed // 604193323Sed // -O2 or O2 or -la (where -l and -a are separate options) 605193323Sed // 606193323Sed // If this is the case, we cannot allow a value. 607193323Sed // 608193323Sed if (hasArgStr) 609193323Sed return ValueRequired; 610193323Sed else 611193323Sed return ValueDisallowed; 612193323Sed } 613193323Sed 614193323Sed // findOption - Return the option number corresponding to the specified 615193323Sed // argument string. If the option is not found, getNumOptions() is returned. 616193323Sed // 617193323Sed unsigned findOption(const char *Name); 618193323Sed 619193323Sedprotected: 620193323Sed bool hasArgStr; 621193323Sed}; 622193323Sed 623193323Sed// Default parser implementation - This implementation depends on having a 624193323Sed// mapping of recognized options to values of some sort. In addition to this, 625193323Sed// each entry in the mapping also tracks a help message that is printed with the 626204642Srdivacky// command line option for -help. Because this is a simple mapping parser, the 627193323Sed// data type can be any unsupported type. 628193323Sed// 629193323Sedtemplate <class DataType> 630193323Sedclass parser : public generic_parser_base { 631193323Sedprotected: 632221345Sdim class OptionInfo : public GenericOptionInfo { 633212904Sdim public: 634212904Sdim OptionInfo(const char *name, DataType v, const char *helpStr) : 635221345Sdim GenericOptionInfo(name, helpStr), V(v) {} 636221345Sdim OptionValue<DataType> V; 637212904Sdim }; 638212904Sdim SmallVector<OptionInfo, 8> Values; 639193323Sedpublic: 640193323Sed typedef DataType parser_data_type; 641193323Sed 642193323Sed // Implement virtual functions needed by generic_parser_base 643193323Sed unsigned getNumOptions() const { return unsigned(Values.size()); } 644212904Sdim const char *getOption(unsigned N) const { return Values[N].Name; } 645193323Sed const char *getDescription(unsigned N) const { 646212904Sdim return Values[N].HelpStr; 647193323Sed } 648193323Sed 649221345Sdim // getOptionValue - Return the value of option name N. 650221345Sdim virtual const GenericOptionValue &getOptionValue(unsigned N) const { 651221345Sdim return Values[N].V; 652221345Sdim } 653221345Sdim 654193323Sed // parse - Return true on error. 655198090Srdivacky bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) { 656198090Srdivacky StringRef ArgVal; 657193323Sed if (hasArgStr) 658193323Sed ArgVal = Arg; 659193323Sed else 660193323Sed ArgVal = ArgName; 661193323Sed 662249423Sdim for (size_t i = 0, e = Values.size(); i != e; ++i) 663212904Sdim if (Values[i].Name == ArgVal) { 664221345Sdim V = Values[i].V.getValue(); 665193323Sed return false; 666193323Sed } 667193323Sed 668198090Srdivacky return O.error("Cannot find option named '" + ArgVal + "'!"); 669193323Sed } 670193323Sed 671193323Sed /// addLiteralOption - Add an entry to the mapping table. 672193323Sed /// 673193323Sed template <class DT> 674193323Sed void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) { 675193323Sed assert(findOption(Name) == Values.size() && "Option already exists!"); 676212904Sdim OptionInfo X(Name, static_cast<DataType>(V), HelpStr); 677212904Sdim Values.push_back(X); 678193323Sed MarkOptionsChanged(); 679193323Sed } 680193323Sed 681193323Sed /// removeLiteralOption - Remove the specified option. 682193323Sed /// 683193323Sed void removeLiteralOption(const char *Name) { 684193323Sed unsigned N = findOption(Name); 685193323Sed assert(N != Values.size() && "Option not found!"); 686193323Sed Values.erase(Values.begin()+N); 687193323Sed } 688193323Sed}; 689193323Sed 690193323Sed//-------------------------------------------------- 691193323Sed// basic_parser - Super class of parsers to provide boilerplate code 692193323Sed// 693199481Srdivackyclass basic_parser_impl { // non-template implementation of basic_parser<t> 694199481Srdivackypublic: 695193323Sed virtual ~basic_parser_impl() {} 696193323Sed 697193323Sed enum ValueExpected getValueExpectedFlagDefault() const { 698193323Sed return ValueRequired; 699193323Sed } 700193323Sed 701198090Srdivacky void getExtraOptionNames(SmallVectorImpl<const char*> &) {} 702193323Sed 703193323Sed void initialize(Option &) {} 704193323Sed 705193323Sed // Return the width of the option tag for printing... 706193323Sed size_t getOptionWidth(const Option &O) const; 707193323Sed 708193323Sed // printOptionInfo - Print out information about this option. The 709193323Sed // to-be-maintained width is specified. 710193323Sed // 711193323Sed void printOptionInfo(const Option &O, size_t GlobalWidth) const; 712193323Sed 713221345Sdim // printOptionNoValue - Print a placeholder for options that don't yet support 714221345Sdim // printOptionDiff(). 715221345Sdim void printOptionNoValue(const Option &O, size_t GlobalWidth) const; 716221345Sdim 717193323Sed // getValueName - Overload in subclass to provide a better default value. 718193323Sed virtual const char *getValueName() const { return "value"; } 719193323Sed 720193323Sed // An out-of-line virtual method to provide a 'home' for this class. 721193323Sed virtual void anchor(); 722221345Sdim 723221345Sdimprotected: 724221345Sdim // A helper for basic_parser::printOptionDiff. 725221345Sdim void printOptionName(const Option &O, size_t GlobalWidth) const; 726193323Sed}; 727193323Sed 728193323Sed// basic_parser - The real basic parser is just a template wrapper that provides 729193323Sed// a typedef for the provided data type. 730193323Sed// 731193323Sedtemplate<class DataType> 732199481Srdivackyclass basic_parser : public basic_parser_impl { 733199481Srdivackypublic: 734193323Sed typedef DataType parser_data_type; 735221345Sdim typedef OptionValue<DataType> OptVal; 736193323Sed}; 737193323Sed 738193323Sed//-------------------------------------------------- 739193323Sed// parser<bool> 740193323Sed// 741193323Sedtemplate<> 742193323Sedclass parser<bool> : public basic_parser<bool> { 743193323Sed const char *ArgStr; 744193323Sedpublic: 745194612Sed 746193323Sed // parse - Return true on error. 747198090Srdivacky bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val); 748193323Sed 749193323Sed template <class Opt> 750193323Sed void initialize(Opt &O) { 751193323Sed ArgStr = O.ArgStr; 752193323Sed } 753193323Sed 754193323Sed enum ValueExpected getValueExpectedFlagDefault() const { 755193323Sed return ValueOptional; 756193323Sed } 757193323Sed 758193323Sed // getValueName - Do not print =<value> at all. 759193323Sed virtual const char *getValueName() const { return 0; } 760193323Sed 761221345Sdim void printOptionDiff(const Option &O, bool V, OptVal Default, 762221345Sdim size_t GlobalWidth) const; 763221345Sdim 764193323Sed // An out-of-line virtual method to provide a 'home' for this class. 765193323Sed virtual void anchor(); 766193323Sed}; 767193323Sed 768193323SedEXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>); 769193323Sed 770193323Sed//-------------------------------------------------- 771193323Sed// parser<boolOrDefault> 772193323Sedtemplate<> 773193323Sedclass parser<boolOrDefault> : public basic_parser<boolOrDefault> { 774193323Sedpublic: 775193323Sed // parse - Return true on error. 776198090Srdivacky bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val); 777193323Sed 778193323Sed enum ValueExpected getValueExpectedFlagDefault() const { 779193323Sed return ValueOptional; 780193323Sed } 781193323Sed 782193323Sed // getValueName - Do not print =<value> at all. 783193323Sed virtual const char *getValueName() const { return 0; } 784193323Sed 785221345Sdim void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default, 786221345Sdim size_t GlobalWidth) const; 787221345Sdim 788193323Sed // An out-of-line virtual method to provide a 'home' for this class. 789193323Sed virtual void anchor(); 790193323Sed}; 791193323Sed 792193323SedEXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>); 793193323Sed 794193323Sed//-------------------------------------------------- 795193323Sed// parser<int> 796193323Sed// 797193323Sedtemplate<> 798193323Sedclass parser<int> : public basic_parser<int> { 799193323Sedpublic: 800193323Sed // parse - Return true on error. 801198090Srdivacky bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val); 802193323Sed 803193323Sed // getValueName - Overload in subclass to provide a better default value. 804193323Sed virtual const char *getValueName() const { return "int"; } 805193323Sed 806221345Sdim void printOptionDiff(const Option &O, int V, OptVal Default, 807221345Sdim size_t GlobalWidth) const; 808221345Sdim 809193323Sed // An out-of-line virtual method to provide a 'home' for this class. 810193323Sed virtual void anchor(); 811193323Sed}; 812193323Sed 813193323SedEXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>); 814193323Sed 815193323Sed 816193323Sed//-------------------------------------------------- 817193323Sed// parser<unsigned> 818193323Sed// 819193323Sedtemplate<> 820193323Sedclass parser<unsigned> : public basic_parser<unsigned> { 821193323Sedpublic: 822193323Sed // parse - Return true on error. 823198090Srdivacky bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val); 824193323Sed 825193323Sed // getValueName - Overload in subclass to provide a better default value. 826193323Sed virtual const char *getValueName() const { return "uint"; } 827193323Sed 828221345Sdim void printOptionDiff(const Option &O, unsigned V, OptVal Default, 829221345Sdim size_t GlobalWidth) const; 830221345Sdim 831193323Sed // An out-of-line virtual method to provide a 'home' for this class. 832193323Sed virtual void anchor(); 833193323Sed}; 834193323Sed 835193323SedEXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>); 836193323Sed 837193323Sed//-------------------------------------------------- 838226633Sdim// parser<unsigned long long> 839226633Sdim// 840226633Sdimtemplate<> 841226633Sdimclass parser<unsigned long long> : public basic_parser<unsigned long long> { 842226633Sdimpublic: 843226633Sdim // parse - Return true on error. 844226633Sdim bool parse(Option &O, StringRef ArgName, StringRef Arg, 845226633Sdim unsigned long long &Val); 846226633Sdim 847226633Sdim // getValueName - Overload in subclass to provide a better default value. 848226633Sdim virtual const char *getValueName() const { return "uint"; } 849226633Sdim 850226633Sdim void printOptionDiff(const Option &O, unsigned long long V, OptVal Default, 851226633Sdim size_t GlobalWidth) const; 852226633Sdim 853226633Sdim // An out-of-line virtual method to provide a 'home' for this class. 854226633Sdim virtual void anchor(); 855226633Sdim}; 856226633Sdim 857226633SdimEXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned long long>); 858226633Sdim 859226633Sdim//-------------------------------------------------- 860193323Sed// parser<double> 861193323Sed// 862193323Sedtemplate<> 863193323Sedclass parser<double> : public basic_parser<double> { 864193323Sedpublic: 865193323Sed // parse - Return true on error. 866198090Srdivacky bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val); 867193323Sed 868193323Sed // getValueName - Overload in subclass to provide a better default value. 869193323Sed virtual const char *getValueName() const { return "number"; } 870193323Sed 871221345Sdim void printOptionDiff(const Option &O, double V, OptVal Default, 872221345Sdim size_t GlobalWidth) const; 873221345Sdim 874193323Sed // An out-of-line virtual method to provide a 'home' for this class. 875193323Sed virtual void anchor(); 876193323Sed}; 877193323Sed 878193323SedEXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>); 879193323Sed 880193323Sed//-------------------------------------------------- 881193323Sed// parser<float> 882193323Sed// 883193323Sedtemplate<> 884193323Sedclass parser<float> : public basic_parser<float> { 885193323Sedpublic: 886193323Sed // parse - Return true on error. 887198090Srdivacky bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val); 888193323Sed 889193323Sed // getValueName - Overload in subclass to provide a better default value. 890193323Sed virtual const char *getValueName() const { return "number"; } 891193323Sed 892221345Sdim void printOptionDiff(const Option &O, float V, OptVal Default, 893221345Sdim size_t GlobalWidth) const; 894221345Sdim 895193323Sed // An out-of-line virtual method to provide a 'home' for this class. 896193323Sed virtual void anchor(); 897193323Sed}; 898193323Sed 899193323SedEXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>); 900193323Sed 901193323Sed//-------------------------------------------------- 902193323Sed// parser<std::string> 903193323Sed// 904193323Sedtemplate<> 905193323Sedclass parser<std::string> : public basic_parser<std::string> { 906193323Sedpublic: 907193323Sed // parse - Return true on error. 908198113Srdivacky bool parse(Option &, StringRef, StringRef Arg, std::string &Value) { 909198090Srdivacky Value = Arg.str(); 910193323Sed return false; 911193323Sed } 912193323Sed 913193323Sed // getValueName - Overload in subclass to provide a better default value. 914193323Sed virtual const char *getValueName() const { return "string"; } 915193323Sed 916221345Sdim void printOptionDiff(const Option &O, StringRef V, OptVal Default, 917221345Sdim size_t GlobalWidth) const; 918221345Sdim 919193323Sed // An out-of-line virtual method to provide a 'home' for this class. 920193323Sed virtual void anchor(); 921193323Sed}; 922193323Sed 923193323SedEXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>); 924193323Sed 925193323Sed//-------------------------------------------------- 926193323Sed// parser<char> 927193323Sed// 928193323Sedtemplate<> 929193323Sedclass parser<char> : public basic_parser<char> { 930193323Sedpublic: 931193323Sed // parse - Return true on error. 932198113Srdivacky bool parse(Option &, StringRef, StringRef Arg, char &Value) { 933193323Sed Value = Arg[0]; 934193323Sed return false; 935193323Sed } 936193323Sed 937193323Sed // getValueName - Overload in subclass to provide a better default value. 938193323Sed virtual const char *getValueName() const { return "char"; } 939193323Sed 940221345Sdim void printOptionDiff(const Option &O, char V, OptVal Default, 941221345Sdim size_t GlobalWidth) const; 942221345Sdim 943193323Sed // An out-of-line virtual method to provide a 'home' for this class. 944193323Sed virtual void anchor(); 945193323Sed}; 946193323Sed 947193323SedEXTERN_TEMPLATE_INSTANTIATION(class basic_parser<char>); 948193323Sed 949221345Sdim//-------------------------------------------------- 950221345Sdim// PrintOptionDiff 951221345Sdim// 952221345Sdim// This collection of wrappers is the intermediary between class opt and class 953221345Sdim// parser to handle all the template nastiness. 954221345Sdim 955221345Sdim// This overloaded function is selected by the generic parser. 956221345Sdimtemplate<class ParserClass, class DT> 957221345Sdimvoid printOptionDiff(const Option &O, const generic_parser_base &P, const DT &V, 958221345Sdim const OptionValue<DT> &Default, size_t GlobalWidth) { 959221345Sdim OptionValue<DT> OV = V; 960221345Sdim P.printOptionDiff(O, OV, Default, GlobalWidth); 961221345Sdim} 962221345Sdim 963221345Sdim// This is instantiated for basic parsers when the parsed value has a different 964221345Sdim// type than the option value. e.g. HelpPrinter. 965221345Sdimtemplate<class ParserDT, class ValDT> 966221345Sdimstruct OptionDiffPrinter { 967221345Sdim void print(const Option &O, const parser<ParserDT> P, const ValDT &/*V*/, 968221345Sdim const OptionValue<ValDT> &/*Default*/, size_t GlobalWidth) { 969221345Sdim P.printOptionNoValue(O, GlobalWidth); 970221345Sdim } 971221345Sdim}; 972221345Sdim 973221345Sdim// This is instantiated for basic parsers when the parsed value has the same 974221345Sdim// type as the option value. 975221345Sdimtemplate<class DT> 976221345Sdimstruct OptionDiffPrinter<DT, DT> { 977221345Sdim void print(const Option &O, const parser<DT> P, const DT &V, 978221345Sdim const OptionValue<DT> &Default, size_t GlobalWidth) { 979221345Sdim P.printOptionDiff(O, V, Default, GlobalWidth); 980221345Sdim } 981221345Sdim}; 982221345Sdim 983221345Sdim// This overloaded function is selected by the basic parser, which may parse a 984221345Sdim// different type than the option type. 985221345Sdimtemplate<class ParserClass, class ValDT> 986221345Sdimvoid printOptionDiff( 987221345Sdim const Option &O, 988221345Sdim const basic_parser<typename ParserClass::parser_data_type> &P, 989221345Sdim const ValDT &V, const OptionValue<ValDT> &Default, 990221345Sdim size_t GlobalWidth) { 991221345Sdim 992221345Sdim OptionDiffPrinter<typename ParserClass::parser_data_type, ValDT> printer; 993221345Sdim printer.print(O, static_cast<const ParserClass&>(P), V, Default, 994221345Sdim GlobalWidth); 995221345Sdim} 996221345Sdim 997193323Sed//===----------------------------------------------------------------------===// 998193323Sed// applicator class - This class is used because we must use partial 999193323Sed// specialization to handle literal string arguments specially (const char* does 1000193323Sed// not correctly respond to the apply method). Because the syntax to use this 1001193323Sed// is a pain, we have the 'apply' method below to handle the nastiness... 1002193323Sed// 1003193323Sedtemplate<class Mod> struct applicator { 1004193323Sed template<class Opt> 1005193323Sed static void opt(const Mod &M, Opt &O) { M.apply(O); } 1006193323Sed}; 1007193323Sed 1008193323Sed// Handle const char* as a special case... 1009193323Sedtemplate<unsigned n> struct applicator<char[n]> { 1010193323Sed template<class Opt> 1011193323Sed static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 1012193323Sed}; 1013193323Sedtemplate<unsigned n> struct applicator<const char[n]> { 1014193323Sed template<class Opt> 1015193323Sed static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 1016193323Sed}; 1017193323Sedtemplate<> struct applicator<const char*> { 1018193323Sed template<class Opt> 1019193323Sed static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 1020193323Sed}; 1021193323Sed 1022198090Srdivackytemplate<> struct applicator<NumOccurrencesFlag> { 1023198090Srdivacky static void opt(NumOccurrencesFlag NO, Option &O) { 1024198090Srdivacky O.setNumOccurrencesFlag(NO); 1025198090Srdivacky } 1026193323Sed}; 1027193323Sedtemplate<> struct applicator<ValueExpected> { 1028193323Sed static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); } 1029193323Sed}; 1030193323Sedtemplate<> struct applicator<OptionHidden> { 1031193323Sed static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); } 1032193323Sed}; 1033193323Sedtemplate<> struct applicator<FormattingFlags> { 1034193323Sed static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); } 1035193323Sed}; 1036193323Sedtemplate<> struct applicator<MiscFlags> { 1037193323Sed static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); } 1038193323Sed}; 1039193323Sed 1040193323Sed// apply method - Apply a modifier to an option in a type safe way. 1041193323Sedtemplate<class Mod, class Opt> 1042193323Sedvoid apply(const Mod &M, Opt *O) { 1043193323Sed applicator<Mod>::opt(M, *O); 1044193323Sed} 1045193323Sed 1046193323Sed//===----------------------------------------------------------------------===// 1047193323Sed// opt_storage class 1048193323Sed 1049193323Sed// Default storage class definition: external storage. This implementation 1050193323Sed// assumes the user will specify a variable to store the data into with the 1051193323Sed// cl::location(x) modifier. 1052193323Sed// 1053193323Sedtemplate<class DataType, bool ExternalStorage, bool isClass> 1054193323Sedclass opt_storage { 1055193323Sed DataType *Location; // Where to store the object... 1056221345Sdim OptionValue<DataType> Default; 1057193323Sed 1058193323Sed void check() const { 1059193323Sed assert(Location != 0 && "cl::location(...) not specified for a command " 1060193323Sed "line option with external storage, " 1061193323Sed "or cl::init specified before cl::location()!!"); 1062193323Sed } 1063193323Sedpublic: 1064193323Sed opt_storage() : Location(0) {} 1065193323Sed 1066193323Sed bool setLocation(Option &O, DataType &L) { 1067193323Sed if (Location) 1068198090Srdivacky return O.error("cl::location(x) specified more than once!"); 1069193323Sed Location = &L; 1070221345Sdim Default = L; 1071193323Sed return false; 1072193323Sed } 1073193323Sed 1074193323Sed template<class T> 1075221345Sdim void setValue(const T &V, bool initial = false) { 1076193323Sed check(); 1077193323Sed *Location = V; 1078221345Sdim if (initial) 1079221345Sdim Default = V; 1080193323Sed } 1081193323Sed 1082193323Sed DataType &getValue() { check(); return *Location; } 1083193323Sed const DataType &getValue() const { check(); return *Location; } 1084212904Sdim 1085199481Srdivacky operator DataType() const { return this->getValue(); } 1086221345Sdim 1087221345Sdim const OptionValue<DataType> &getDefault() const { return Default; } 1088193323Sed}; 1089193323Sed 1090193323Sed// Define how to hold a class type object, such as a string. Since we can 1091193323Sed// inherit from a class, we do so. This makes us exactly compatible with the 1092193323Sed// object in all cases that it is used. 1093193323Sed// 1094193323Sedtemplate<class DataType> 1095193323Sedclass opt_storage<DataType,false,true> : public DataType { 1096193323Sedpublic: 1097221345Sdim OptionValue<DataType> Default; 1098221345Sdim 1099193323Sed template<class T> 1100221345Sdim void setValue(const T &V, bool initial = false) { 1101221345Sdim DataType::operator=(V); 1102221345Sdim if (initial) 1103221345Sdim Default = V; 1104221345Sdim } 1105193323Sed 1106193323Sed DataType &getValue() { return *this; } 1107193323Sed const DataType &getValue() const { return *this; } 1108221345Sdim 1109221345Sdim const OptionValue<DataType> &getDefault() const { return Default; } 1110193323Sed}; 1111193323Sed 1112193323Sed// Define a partial specialization to handle things we cannot inherit from. In 1113193323Sed// this case, we store an instance through containment, and overload operators 1114193323Sed// to get at the value. 1115193323Sed// 1116193323Sedtemplate<class DataType> 1117193323Sedclass opt_storage<DataType, false, false> { 1118193323Sedpublic: 1119193323Sed DataType Value; 1120221345Sdim OptionValue<DataType> Default; 1121193323Sed 1122193323Sed // Make sure we initialize the value with the default constructor for the 1123193323Sed // type. 1124249423Sdim opt_storage() : Value(DataType()), Default(DataType()) {} 1125193323Sed 1126193323Sed template<class T> 1127221345Sdim void setValue(const T &V, bool initial = false) { 1128221345Sdim Value = V; 1129221345Sdim if (initial) 1130221345Sdim Default = V; 1131221345Sdim } 1132193323Sed DataType &getValue() { return Value; } 1133193323Sed DataType getValue() const { return Value; } 1134193323Sed 1135221345Sdim const OptionValue<DataType> &getDefault() const { return Default; } 1136221345Sdim 1137199481Srdivacky operator DataType() const { return getValue(); } 1138199481Srdivacky 1139193323Sed // If the datatype is a pointer, support -> on it. 1140193323Sed DataType operator->() const { return Value; } 1141193323Sed}; 1142193323Sed 1143193323Sed 1144193323Sed//===----------------------------------------------------------------------===// 1145193323Sed// opt - A scalar command line option. 1146193323Sed// 1147193323Sedtemplate <class DataType, bool ExternalStorage = false, 1148193323Sed class ParserClass = parser<DataType> > 1149193323Sedclass opt : public Option, 1150193323Sed public opt_storage<DataType, ExternalStorage, 1151193323Sed is_class<DataType>::value> { 1152193323Sed ParserClass Parser; 1153193323Sed 1154198090Srdivacky virtual bool handleOccurrence(unsigned pos, StringRef ArgName, 1155198090Srdivacky StringRef Arg) { 1156193323Sed typename ParserClass::parser_data_type Val = 1157193323Sed typename ParserClass::parser_data_type(); 1158193323Sed if (Parser.parse(*this, ArgName, Arg, Val)) 1159193323Sed return true; // Parse error! 1160193323Sed this->setValue(Val); 1161193323Sed this->setPosition(pos); 1162193323Sed return false; 1163193323Sed } 1164193323Sed 1165193323Sed virtual enum ValueExpected getValueExpectedFlagDefault() const { 1166193323Sed return Parser.getValueExpectedFlagDefault(); 1167193323Sed } 1168198090Srdivacky virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) { 1169193323Sed return Parser.getExtraOptionNames(OptionNames); 1170193323Sed } 1171193323Sed 1172193323Sed // Forward printing stuff to the parser... 1173193323Sed virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);} 1174193323Sed virtual void printOptionInfo(size_t GlobalWidth) const { 1175193323Sed Parser.printOptionInfo(*this, GlobalWidth); 1176193323Sed } 1177193323Sed 1178221345Sdim virtual void printOptionValue(size_t GlobalWidth, bool Force) const { 1179221345Sdim if (Force || this->getDefault().compare(this->getValue())) { 1180221345Sdim cl::printOptionDiff<ParserClass>( 1181221345Sdim *this, Parser, this->getValue(), this->getDefault(), GlobalWidth); 1182221345Sdim } 1183221345Sdim } 1184221345Sdim 1185193323Sed void done() { 1186193323Sed addArgument(); 1187193323Sed Parser.initialize(*this); 1188193323Sed } 1189193323Sedpublic: 1190193323Sed // setInitialValue - Used by the cl::init modifier... 1191221345Sdim void setInitialValue(const DataType &V) { this->setValue(V, true); } 1192193323Sed 1193193323Sed ParserClass &getParser() { return Parser; } 1194193323Sed 1195193323Sed template<class T> 1196193323Sed DataType &operator=(const T &Val) { 1197193323Sed this->setValue(Val); 1198193323Sed return this->getValue(); 1199193323Sed } 1200193323Sed 1201193323Sed // One option... 1202193323Sed template<class M0t> 1203234353Sdim explicit opt(const M0t &M0) : Option(Optional, NotHidden) { 1204193323Sed apply(M0, this); 1205193323Sed done(); 1206193323Sed } 1207193323Sed 1208193323Sed // Two options... 1209193323Sed template<class M0t, class M1t> 1210234353Sdim opt(const M0t &M0, const M1t &M1) : Option(Optional, NotHidden) { 1211193323Sed apply(M0, this); apply(M1, this); 1212193323Sed done(); 1213193323Sed } 1214193323Sed 1215193323Sed // Three options... 1216193323Sed template<class M0t, class M1t, class M2t> 1217193323Sed opt(const M0t &M0, const M1t &M1, 1218234353Sdim const M2t &M2) : Option(Optional, NotHidden) { 1219193323Sed apply(M0, this); apply(M1, this); apply(M2, this); 1220193323Sed done(); 1221193323Sed } 1222193323Sed // Four options... 1223193323Sed template<class M0t, class M1t, class M2t, class M3t> 1224193323Sed opt(const M0t &M0, const M1t &M1, const M2t &M2, 1225234353Sdim const M3t &M3) : Option(Optional, NotHidden) { 1226193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1227193323Sed done(); 1228193323Sed } 1229193323Sed // Five options... 1230193323Sed template<class M0t, class M1t, class M2t, class M3t, class M4t> 1231193323Sed opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1232234353Sdim const M4t &M4) : Option(Optional, NotHidden) { 1233193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1234193323Sed apply(M4, this); 1235193323Sed done(); 1236193323Sed } 1237193323Sed // Six options... 1238193323Sed template<class M0t, class M1t, class M2t, class M3t, 1239193323Sed class M4t, class M5t> 1240193323Sed opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1241234353Sdim const M4t &M4, const M5t &M5) : Option(Optional, NotHidden) { 1242193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1243193323Sed apply(M4, this); apply(M5, this); 1244193323Sed done(); 1245193323Sed } 1246193323Sed // Seven options... 1247193323Sed template<class M0t, class M1t, class M2t, class M3t, 1248193323Sed class M4t, class M5t, class M6t> 1249193323Sed opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1250193323Sed const M4t &M4, const M5t &M5, 1251234353Sdim const M6t &M6) : Option(Optional, NotHidden) { 1252193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1253193323Sed apply(M4, this); apply(M5, this); apply(M6, this); 1254193323Sed done(); 1255193323Sed } 1256193323Sed // Eight options... 1257193323Sed template<class M0t, class M1t, class M2t, class M3t, 1258193323Sed class M4t, class M5t, class M6t, class M7t> 1259193323Sed opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1260193323Sed const M4t &M4, const M5t &M5, const M6t &M6, 1261234353Sdim const M7t &M7) : Option(Optional, NotHidden) { 1262193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1263193323Sed apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 1264193323Sed done(); 1265193323Sed } 1266193323Sed}; 1267193323Sed 1268193323SedEXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>); 1269193323SedEXTERN_TEMPLATE_INSTANTIATION(class opt<int>); 1270193323SedEXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>); 1271193323SedEXTERN_TEMPLATE_INSTANTIATION(class opt<char>); 1272193323SedEXTERN_TEMPLATE_INSTANTIATION(class opt<bool>); 1273193323Sed 1274193323Sed//===----------------------------------------------------------------------===// 1275193323Sed// list_storage class 1276193323Sed 1277193323Sed// Default storage class definition: external storage. This implementation 1278193323Sed// assumes the user will specify a variable to store the data into with the 1279193323Sed// cl::location(x) modifier. 1280193323Sed// 1281193323Sedtemplate<class DataType, class StorageClass> 1282193323Sedclass list_storage { 1283193323Sed StorageClass *Location; // Where to store the object... 1284193323Sed 1285193323Sedpublic: 1286193323Sed list_storage() : Location(0) {} 1287193323Sed 1288193323Sed bool setLocation(Option &O, StorageClass &L) { 1289193323Sed if (Location) 1290198090Srdivacky return O.error("cl::location(x) specified more than once!"); 1291193323Sed Location = &L; 1292193323Sed return false; 1293193323Sed } 1294193323Sed 1295193323Sed template<class T> 1296193323Sed void addValue(const T &V) { 1297193323Sed assert(Location != 0 && "cl::location(...) not specified for a command " 1298193323Sed "line option with external storage!"); 1299193323Sed Location->push_back(V); 1300193323Sed } 1301193323Sed}; 1302193323Sed 1303193323Sed 1304193323Sed// Define how to hold a class type object, such as a string. Since we can 1305193323Sed// inherit from a class, we do so. This makes us exactly compatible with the 1306193323Sed// object in all cases that it is used. 1307193323Sed// 1308193323Sedtemplate<class DataType> 1309193323Sedclass list_storage<DataType, bool> : public std::vector<DataType> { 1310193323Sedpublic: 1311193323Sed template<class T> 1312200581Srdivacky void addValue(const T &V) { std::vector<DataType>::push_back(V); } 1313193323Sed}; 1314193323Sed 1315193323Sed 1316193323Sed//===----------------------------------------------------------------------===// 1317193323Sed// list - A list of command line options. 1318193323Sed// 1319193323Sedtemplate <class DataType, class Storage = bool, 1320193323Sed class ParserClass = parser<DataType> > 1321193323Sedclass list : public Option, public list_storage<DataType, Storage> { 1322193323Sed std::vector<unsigned> Positions; 1323193323Sed ParserClass Parser; 1324193323Sed 1325193323Sed virtual enum ValueExpected getValueExpectedFlagDefault() const { 1326193323Sed return Parser.getValueExpectedFlagDefault(); 1327193323Sed } 1328198090Srdivacky virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) { 1329193323Sed return Parser.getExtraOptionNames(OptionNames); 1330193323Sed } 1331193323Sed 1332198090Srdivacky virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg){ 1333193323Sed typename ParserClass::parser_data_type Val = 1334193323Sed typename ParserClass::parser_data_type(); 1335193323Sed if (Parser.parse(*this, ArgName, Arg, Val)) 1336193323Sed return true; // Parse Error! 1337200581Srdivacky list_storage<DataType, Storage>::addValue(Val); 1338193323Sed setPosition(pos); 1339193323Sed Positions.push_back(pos); 1340193323Sed return false; 1341193323Sed } 1342193323Sed 1343193323Sed // Forward printing stuff to the parser... 1344193323Sed virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);} 1345193323Sed virtual void printOptionInfo(size_t GlobalWidth) const { 1346193323Sed Parser.printOptionInfo(*this, GlobalWidth); 1347193323Sed } 1348193323Sed 1349221345Sdim // Unimplemented: list options don't currently store their default value. 1350221345Sdim virtual void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const {} 1351221345Sdim 1352193323Sed void done() { 1353193323Sed addArgument(); 1354193323Sed Parser.initialize(*this); 1355193323Sed } 1356193323Sedpublic: 1357193323Sed ParserClass &getParser() { return Parser; } 1358193323Sed 1359193323Sed unsigned getPosition(unsigned optnum) const { 1360193323Sed assert(optnum < this->size() && "Invalid option index"); 1361193323Sed return Positions[optnum]; 1362193323Sed } 1363193323Sed 1364193323Sed void setNumAdditionalVals(unsigned n) { 1365193323Sed Option::setNumAdditionalVals(n); 1366193323Sed } 1367193323Sed 1368193323Sed // One option... 1369193323Sed template<class M0t> 1370234353Sdim explicit list(const M0t &M0) : Option(ZeroOrMore, NotHidden) { 1371193323Sed apply(M0, this); 1372193323Sed done(); 1373193323Sed } 1374193323Sed // Two options... 1375193323Sed template<class M0t, class M1t> 1376234353Sdim list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore, NotHidden) { 1377193323Sed apply(M0, this); apply(M1, this); 1378193323Sed done(); 1379193323Sed } 1380193323Sed // Three options... 1381193323Sed template<class M0t, class M1t, class M2t> 1382193323Sed list(const M0t &M0, const M1t &M1, const M2t &M2) 1383234353Sdim : Option(ZeroOrMore, NotHidden) { 1384193323Sed apply(M0, this); apply(M1, this); apply(M2, this); 1385193323Sed done(); 1386193323Sed } 1387193323Sed // Four options... 1388193323Sed template<class M0t, class M1t, class M2t, class M3t> 1389193323Sed list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 1390234353Sdim : Option(ZeroOrMore, NotHidden) { 1391193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1392193323Sed done(); 1393193323Sed } 1394193323Sed // Five options... 1395193323Sed template<class M0t, class M1t, class M2t, class M3t, class M4t> 1396193323Sed list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1397234353Sdim const M4t &M4) : Option(ZeroOrMore, NotHidden) { 1398193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1399193323Sed apply(M4, this); 1400193323Sed done(); 1401193323Sed } 1402193323Sed // Six options... 1403193323Sed template<class M0t, class M1t, class M2t, class M3t, 1404193323Sed class M4t, class M5t> 1405193323Sed list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1406234353Sdim const M4t &M4, const M5t &M5) : Option(ZeroOrMore, NotHidden) { 1407193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1408193323Sed apply(M4, this); apply(M5, this); 1409193323Sed done(); 1410193323Sed } 1411193323Sed // Seven options... 1412193323Sed template<class M0t, class M1t, class M2t, class M3t, 1413193323Sed class M4t, class M5t, class M6t> 1414193323Sed list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1415193323Sed const M4t &M4, const M5t &M5, const M6t &M6) 1416234353Sdim : Option(ZeroOrMore, NotHidden) { 1417193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1418193323Sed apply(M4, this); apply(M5, this); apply(M6, this); 1419193323Sed done(); 1420193323Sed } 1421193323Sed // Eight options... 1422193323Sed template<class M0t, class M1t, class M2t, class M3t, 1423193323Sed class M4t, class M5t, class M6t, class M7t> 1424193323Sed list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1425193323Sed const M4t &M4, const M5t &M5, const M6t &M6, 1426234353Sdim const M7t &M7) : Option(ZeroOrMore, NotHidden) { 1427193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1428193323Sed apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 1429193323Sed done(); 1430193323Sed } 1431193323Sed}; 1432193323Sed 1433194612Sed// multi_val - Modifier to set the number of additional values. 1434193323Sedstruct multi_val { 1435193323Sed unsigned AdditionalVals; 1436193323Sed explicit multi_val(unsigned N) : AdditionalVals(N) {} 1437193323Sed 1438193323Sed template <typename D, typename S, typename P> 1439193323Sed void apply(list<D, S, P> &L) const { L.setNumAdditionalVals(AdditionalVals); } 1440193323Sed}; 1441193323Sed 1442193323Sed 1443193323Sed//===----------------------------------------------------------------------===// 1444193323Sed// bits_storage class 1445193323Sed 1446193323Sed// Default storage class definition: external storage. This implementation 1447193323Sed// assumes the user will specify a variable to store the data into with the 1448193323Sed// cl::location(x) modifier. 1449193323Sed// 1450193323Sedtemplate<class DataType, class StorageClass> 1451193323Sedclass bits_storage { 1452193323Sed unsigned *Location; // Where to store the bits... 1453193323Sed 1454193323Sed template<class T> 1455193323Sed static unsigned Bit(const T &V) { 1456193323Sed unsigned BitPos = reinterpret_cast<unsigned>(V); 1457193323Sed assert(BitPos < sizeof(unsigned) * CHAR_BIT && 1458193323Sed "enum exceeds width of bit vector!"); 1459193323Sed return 1 << BitPos; 1460193323Sed } 1461193323Sed 1462193323Sedpublic: 1463193323Sed bits_storage() : Location(0) {} 1464193323Sed 1465193323Sed bool setLocation(Option &O, unsigned &L) { 1466193323Sed if (Location) 1467198090Srdivacky return O.error("cl::location(x) specified more than once!"); 1468193323Sed Location = &L; 1469193323Sed return false; 1470193323Sed } 1471193323Sed 1472193323Sed template<class T> 1473193323Sed void addValue(const T &V) { 1474193323Sed assert(Location != 0 && "cl::location(...) not specified for a command " 1475193323Sed "line option with external storage!"); 1476193323Sed *Location |= Bit(V); 1477193323Sed } 1478193323Sed 1479193323Sed unsigned getBits() { return *Location; } 1480193323Sed 1481193323Sed template<class T> 1482193323Sed bool isSet(const T &V) { 1483193323Sed return (*Location & Bit(V)) != 0; 1484193323Sed } 1485193323Sed}; 1486193323Sed 1487193323Sed 1488193323Sed// Define how to hold bits. Since we can inherit from a class, we do so. 1489193323Sed// This makes us exactly compatible with the bits in all cases that it is used. 1490193323Sed// 1491193323Sedtemplate<class DataType> 1492193323Sedclass bits_storage<DataType, bool> { 1493193323Sed unsigned Bits; // Where to store the bits... 1494193323Sed 1495193323Sed template<class T> 1496193323Sed static unsigned Bit(const T &V) { 1497203954Srdivacky unsigned BitPos = (unsigned)V; 1498193323Sed assert(BitPos < sizeof(unsigned) * CHAR_BIT && 1499193323Sed "enum exceeds width of bit vector!"); 1500193323Sed return 1 << BitPos; 1501193323Sed } 1502193323Sed 1503193323Sedpublic: 1504193323Sed template<class T> 1505193323Sed void addValue(const T &V) { 1506193323Sed Bits |= Bit(V); 1507193323Sed } 1508193323Sed 1509193323Sed unsigned getBits() { return Bits; } 1510193323Sed 1511193323Sed template<class T> 1512193323Sed bool isSet(const T &V) { 1513193323Sed return (Bits & Bit(V)) != 0; 1514193323Sed } 1515193323Sed}; 1516193323Sed 1517193323Sed 1518193323Sed//===----------------------------------------------------------------------===// 1519193323Sed// bits - A bit vector of command options. 1520193323Sed// 1521193323Sedtemplate <class DataType, class Storage = bool, 1522193323Sed class ParserClass = parser<DataType> > 1523193323Sedclass bits : public Option, public bits_storage<DataType, Storage> { 1524193323Sed std::vector<unsigned> Positions; 1525193323Sed ParserClass Parser; 1526193323Sed 1527193323Sed virtual enum ValueExpected getValueExpectedFlagDefault() const { 1528193323Sed return Parser.getValueExpectedFlagDefault(); 1529193323Sed } 1530198090Srdivacky virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) { 1531193323Sed return Parser.getExtraOptionNames(OptionNames); 1532193323Sed } 1533193323Sed 1534198090Srdivacky virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg){ 1535193323Sed typename ParserClass::parser_data_type Val = 1536193323Sed typename ParserClass::parser_data_type(); 1537193323Sed if (Parser.parse(*this, ArgName, Arg, Val)) 1538193323Sed return true; // Parse Error! 1539243830Sdim this->addValue(Val); 1540193323Sed setPosition(pos); 1541193323Sed Positions.push_back(pos); 1542193323Sed return false; 1543193323Sed } 1544193323Sed 1545193323Sed // Forward printing stuff to the parser... 1546193323Sed virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);} 1547193323Sed virtual void printOptionInfo(size_t GlobalWidth) const { 1548193323Sed Parser.printOptionInfo(*this, GlobalWidth); 1549193323Sed } 1550193323Sed 1551221345Sdim // Unimplemented: bits options don't currently store their default values. 1552221345Sdim virtual void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const {} 1553221345Sdim 1554193323Sed void done() { 1555193323Sed addArgument(); 1556193323Sed Parser.initialize(*this); 1557193323Sed } 1558193323Sedpublic: 1559193323Sed ParserClass &getParser() { return Parser; } 1560193323Sed 1561193323Sed unsigned getPosition(unsigned optnum) const { 1562193323Sed assert(optnum < this->size() && "Invalid option index"); 1563193323Sed return Positions[optnum]; 1564193323Sed } 1565193323Sed 1566193323Sed // One option... 1567193323Sed template<class M0t> 1568234353Sdim explicit bits(const M0t &M0) : Option(ZeroOrMore, NotHidden) { 1569193323Sed apply(M0, this); 1570193323Sed done(); 1571193323Sed } 1572193323Sed // Two options... 1573193323Sed template<class M0t, class M1t> 1574234353Sdim bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore, NotHidden) { 1575193323Sed apply(M0, this); apply(M1, this); 1576193323Sed done(); 1577193323Sed } 1578193323Sed // Three options... 1579193323Sed template<class M0t, class M1t, class M2t> 1580193323Sed bits(const M0t &M0, const M1t &M1, const M2t &M2) 1581234353Sdim : Option(ZeroOrMore, NotHidden) { 1582193323Sed apply(M0, this); apply(M1, this); apply(M2, this); 1583193323Sed done(); 1584193323Sed } 1585193323Sed // Four options... 1586193323Sed template<class M0t, class M1t, class M2t, class M3t> 1587193323Sed bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 1588234353Sdim : Option(ZeroOrMore, NotHidden) { 1589193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1590193323Sed done(); 1591193323Sed } 1592193323Sed // Five options... 1593193323Sed template<class M0t, class M1t, class M2t, class M3t, class M4t> 1594193323Sed bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1595234353Sdim const M4t &M4) : Option(ZeroOrMore, NotHidden) { 1596193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1597193323Sed apply(M4, this); 1598193323Sed done(); 1599193323Sed } 1600193323Sed // Six options... 1601193323Sed template<class M0t, class M1t, class M2t, class M3t, 1602193323Sed class M4t, class M5t> 1603193323Sed bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1604234353Sdim const M4t &M4, const M5t &M5) : Option(ZeroOrMore, NotHidden) { 1605193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1606193323Sed apply(M4, this); apply(M5, this); 1607193323Sed done(); 1608193323Sed } 1609193323Sed // Seven options... 1610193323Sed template<class M0t, class M1t, class M2t, class M3t, 1611193323Sed class M4t, class M5t, class M6t> 1612193323Sed bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1613193323Sed const M4t &M4, const M5t &M5, const M6t &M6) 1614234353Sdim : Option(ZeroOrMore, NotHidden) { 1615193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1616193323Sed apply(M4, this); apply(M5, this); apply(M6, this); 1617193323Sed done(); 1618193323Sed } 1619193323Sed // Eight options... 1620193323Sed template<class M0t, class M1t, class M2t, class M3t, 1621193323Sed class M4t, class M5t, class M6t, class M7t> 1622193323Sed bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 1623193323Sed const M4t &M4, const M5t &M5, const M6t &M6, 1624234353Sdim const M7t &M7) : Option(ZeroOrMore, NotHidden) { 1625193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1626193323Sed apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 1627193323Sed done(); 1628193323Sed } 1629193323Sed}; 1630193323Sed 1631193323Sed//===----------------------------------------------------------------------===// 1632193323Sed// Aliased command line option (alias this name to a preexisting name) 1633193323Sed// 1634193323Sed 1635193323Sedclass alias : public Option { 1636193323Sed Option *AliasFor; 1637198090Srdivacky virtual bool handleOccurrence(unsigned pos, StringRef /*ArgName*/, 1638243830Sdim StringRef Arg) LLVM_OVERRIDE { 1639193323Sed return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg); 1640193323Sed } 1641193323Sed // Handle printing stuff... 1642243830Sdim virtual size_t getOptionWidth() const LLVM_OVERRIDE; 1643243830Sdim virtual void printOptionInfo(size_t GlobalWidth) const LLVM_OVERRIDE; 1644193323Sed 1645221345Sdim // Aliases do not need to print their values. 1646243830Sdim virtual void printOptionValue(size_t /*GlobalWidth*/, 1647243830Sdim bool /*Force*/) const LLVM_OVERRIDE {} 1648221345Sdim 1649193323Sed void done() { 1650193323Sed if (!hasArgStr()) 1651198090Srdivacky error("cl::alias must have argument name specified!"); 1652193323Sed if (AliasFor == 0) 1653198090Srdivacky error("cl::alias must have an cl::aliasopt(option) specified!"); 1654193323Sed addArgument(); 1655193323Sed } 1656193323Sedpublic: 1657193323Sed void setAliasFor(Option &O) { 1658193323Sed if (AliasFor) 1659198090Srdivacky error("cl::alias must only have one cl::aliasopt(...) specified!"); 1660193323Sed AliasFor = &O; 1661193323Sed } 1662193323Sed 1663193323Sed // One option... 1664193323Sed template<class M0t> 1665234353Sdim explicit alias(const M0t &M0) : Option(Optional, Hidden), AliasFor(0) { 1666193323Sed apply(M0, this); 1667193323Sed done(); 1668193323Sed } 1669193323Sed // Two options... 1670193323Sed template<class M0t, class M1t> 1671234353Sdim alias(const M0t &M0, const M1t &M1) : Option(Optional, Hidden), AliasFor(0) { 1672193323Sed apply(M0, this); apply(M1, this); 1673193323Sed done(); 1674193323Sed } 1675193323Sed // Three options... 1676193323Sed template<class M0t, class M1t, class M2t> 1677193323Sed alias(const M0t &M0, const M1t &M1, const M2t &M2) 1678234353Sdim : Option(Optional, Hidden), AliasFor(0) { 1679193323Sed apply(M0, this); apply(M1, this); apply(M2, this); 1680193323Sed done(); 1681193323Sed } 1682193323Sed // Four options... 1683193323Sed template<class M0t, class M1t, class M2t, class M3t> 1684193323Sed alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 1685234353Sdim : Option(Optional, Hidden), AliasFor(0) { 1686193323Sed apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 1687193323Sed done(); 1688193323Sed } 1689193323Sed}; 1690193323Sed 1691193323Sed// aliasfor - Modifier to set the option an alias aliases. 1692193323Sedstruct aliasopt { 1693193323Sed Option &Opt; 1694193323Sed explicit aliasopt(Option &O) : Opt(O) {} 1695193323Sed void apply(alias &A) const { A.setAliasFor(Opt); } 1696193323Sed}; 1697193323Sed 1698193323Sed// extrahelp - provide additional help at the end of the normal help 1699193323Sed// output. All occurrences of cl::extrahelp will be accumulated and 1700198090Srdivacky// printed to stderr at the end of the regular help, just before 1701193323Sed// exit is called. 1702193323Sedstruct extrahelp { 1703193323Sed const char * morehelp; 1704193323Sed explicit extrahelp(const char* help); 1705193323Sed}; 1706193323Sed 1707193323Sedvoid PrintVersionMessage(); 1708193323Sed 1709251662Sdim/// This function just prints the help message, exactly the same way as if the 1710251662Sdim/// -help or -help-hidden option had been given on the command line. 1711251662Sdim/// 1712251662Sdim/// NOTE: THIS FUNCTION TERMINATES THE PROGRAM! 1713251662Sdim/// 1714263508Sdim/// \param Hidden if true will print hidden options 1715263508Sdim/// \param Categorized if true print options in categories 1716251662Sdimvoid PrintHelpMessage(bool Hidden=false, bool Categorized=false); 1717251662Sdim 1718251662Sdim 1719251662Sdim//===----------------------------------------------------------------------===// 1720251662Sdim// Public interface for accessing registered options. 1721251662Sdim// 1722251662Sdim 1723251662Sdim/// \brief Use this to get a StringMap to all registered named options 1724251662Sdim/// (e.g. -help). Note \p Map Should be an empty StringMap. 1725251662Sdim/// 1726263508Sdim/// \param [out] Map will be filled with mappings where the key is the 1727251662Sdim/// Option argument string (e.g. "help") and value is the corresponding 1728251662Sdim/// Option*. 1729251662Sdim/// 1730251662Sdim/// Access to unnamed arguments (i.e. positional) are not provided because 1731251662Sdim/// it is expected that the client already has access to these. 1732251662Sdim/// 1733251662Sdim/// Typical usage: 1734251662Sdim/// \code 1735251662Sdim/// main(int argc,char* argv[]) { 1736251662Sdim/// StringMap<llvm::cl::Option*> opts; 1737251662Sdim/// llvm::cl::getRegisteredOptions(opts); 1738251662Sdim/// assert(opts.count("help") == 1) 1739251662Sdim/// opts["help"]->setDescription("Show alphabetical help information") 1740251662Sdim/// // More code 1741251662Sdim/// llvm::cl::ParseCommandLineOptions(argc,argv); 1742251662Sdim/// //More code 1743251662Sdim/// } 1744251662Sdim/// \endcode 1745251662Sdim/// 1746251662Sdim/// This interface is useful for modifying options in libraries that are out of 1747251662Sdim/// the control of the client. The options should be modified before calling 1748251662Sdim/// llvm::cl::ParseCommandLineOptions(). 1749251662Sdimvoid getRegisteredOptions(StringMap<Option*> &Map); 1750251662Sdim 1751263508Sdim//===----------------------------------------------------------------------===// 1752263508Sdim// Standalone command line processing utilities. 1753263508Sdim// 1754263508Sdim 1755263508Sdim/// \brief Saves strings in the inheritor's stable storage and returns a stable 1756263508Sdim/// raw character pointer. 1757263508Sdimclass StringSaver { 1758263508Sdim virtual void anchor(); 1759263508Sdimpublic: 1760263508Sdim virtual const char *SaveString(const char *Str) = 0; 1761263508Sdim virtual ~StringSaver() {}; // Pacify -Wnon-virtual-dtor. 1762263508Sdim}; 1763263508Sdim 1764263508Sdim/// \brief Tokenizes a command line that can contain escapes and quotes. 1765263508Sdim// 1766263508Sdim/// The quoting rules match those used by GCC and other tools that use 1767263508Sdim/// libiberty's buildargv() or expandargv() utilities, and do not match bash. 1768263508Sdim/// They differ from buildargv() on treatment of backslashes that do not escape 1769263508Sdim/// a special character to make it possible to accept most Windows file paths. 1770263508Sdim/// 1771263508Sdim/// \param [in] Source The string to be split on whitespace with quotes. 1772263508Sdim/// \param [in] Saver Delegates back to the caller for saving parsed strings. 1773263508Sdim/// \param [out] NewArgv All parsed strings are appended to NewArgv. 1774263508Sdimvoid TokenizeGNUCommandLine(StringRef Source, StringSaver &Saver, 1775263508Sdim SmallVectorImpl<const char *> &NewArgv); 1776263508Sdim 1777263508Sdim/// \brief Tokenizes a Windows command line which may contain quotes and escaped 1778263508Sdim/// quotes. 1779263508Sdim/// 1780263508Sdim/// See MSDN docs for CommandLineToArgvW for information on the quoting rules. 1781263508Sdim/// http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx 1782263508Sdim/// 1783263508Sdim/// \param [in] Source The string to be split on whitespace with quotes. 1784263508Sdim/// \param [in] Saver Delegates back to the caller for saving parsed strings. 1785263508Sdim/// \param [out] NewArgv All parsed strings are appended to NewArgv. 1786263508Sdimvoid TokenizeWindowsCommandLine(StringRef Source, StringSaver &Saver, 1787263508Sdim SmallVectorImpl<const char *> &NewArgv); 1788263508Sdim 1789263508Sdim/// \brief String tokenization function type. Should be compatible with either 1790263508Sdim/// Windows or Unix command line tokenizers. 1791263508Sdimtypedef void (*TokenizerCallback)(StringRef Source, StringSaver &Saver, 1792263508Sdim SmallVectorImpl<const char *> &NewArgv); 1793263508Sdim 1794263508Sdim/// \brief Expand response files on a command line recursively using the given 1795263508Sdim/// StringSaver and tokenization strategy. Argv should contain the command line 1796263508Sdim/// before expansion and will be modified in place. 1797263508Sdim/// 1798263508Sdim/// \param [in] Saver Delegates back to the caller for saving parsed strings. 1799263508Sdim/// \param [in] Tokenizer Tokenization strategy. Typically Unix or Windows. 1800263508Sdim/// \param [in,out] Argv Command line into which to expand response files. 1801263508Sdim/// \return true if all @files were expanded successfully or there were none. 1802263508Sdimbool ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, 1803263508Sdim SmallVectorImpl<const char *> &Argv); 1804263508Sdim 1805193323Sed} // End namespace cl 1806193323Sed 1807193323Sed} // End namespace llvm 1808193323Sed 1809193323Sed#endif 1810