LLVM API Documentation
00001 //===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This class implements a command line argument processor that is useful when 00011 // creating a tool. It provides a simple, minimalistic interface that is easily 00012 // extensible and supports nonlocal (library) command line options. 00013 // 00014 // Note that rather than trying to figure out what this code does, you should 00015 // read the library documentation located in docs/CommandLine.html or looks at 00016 // the many example usages in tools/*/*.cpp 00017 // 00018 //===----------------------------------------------------------------------===// 00019 00020 #ifndef LLVM_SUPPORT_COMMANDLINE_H 00021 #define LLVM_SUPPORT_COMMANDLINE_H 00022 00023 #include "llvm/ADT/SmallVector.h" 00024 #include "llvm/ADT/Twine.h" 00025 #include "llvm/ADT/StringMap.h" 00026 #include "llvm/Support/Compiler.h" 00027 #include "llvm/Support/type_traits.h" 00028 #include <cassert> 00029 #include <climits> 00030 #include <cstdarg> 00031 #include <utility> 00032 #include <vector> 00033 00034 namespace llvm { 00035 00036 /// cl Namespace - This namespace contains all of the command line option 00037 /// processing machinery. It is intentionally a short name to make qualified 00038 /// usage concise. 00039 namespace cl { 00040 00041 //===----------------------------------------------------------------------===// 00042 // ParseCommandLineOptions - Command line option processing entry point. 00043 // 00044 void ParseCommandLineOptions(int argc, const char * const *argv, 00045 const char *Overview = 0); 00046 00047 //===----------------------------------------------------------------------===// 00048 // ParseEnvironmentOptions - Environment variable option processing alternate 00049 // entry point. 00050 // 00051 void ParseEnvironmentOptions(const char *progName, const char *envvar, 00052 const char *Overview = 0); 00053 00054 ///===---------------------------------------------------------------------===// 00055 /// SetVersionPrinter - Override the default (LLVM specific) version printer 00056 /// used to print out the version when --version is given 00057 /// on the command line. This allows other systems using the 00058 /// CommandLine utilities to print their own version string. 00059 void SetVersionPrinter(void (*func)()); 00060 00061 ///===---------------------------------------------------------------------===// 00062 /// AddExtraVersionPrinter - Add an extra printer to use in addition to the 00063 /// default one. This can be called multiple times, 00064 /// and each time it adds a new function to the list 00065 /// which will be called after the basic LLVM version 00066 /// printing is complete. Each can then add additional 00067 /// information specific to the tool. 00068 void AddExtraVersionPrinter(void (*func)()); 00069 00070 00071 // PrintOptionValues - Print option values. 00072 // With -print-options print the difference between option values and defaults. 00073 // With -print-all-options print all option values. 00074 // (Currently not perfect, but best-effort.) 00075 void PrintOptionValues(); 00076 00077 // MarkOptionsChanged - Internal helper function. 00078 void MarkOptionsChanged(); 00079 00080 //===----------------------------------------------------------------------===// 00081 // Flags permitted to be passed to command line arguments 00082 // 00083 00084 enum NumOccurrencesFlag { // Flags for the number of occurrences allowed 00085 Optional = 0x00, // Zero or One occurrence 00086 ZeroOrMore = 0x01, // Zero or more occurrences allowed 00087 Required = 0x02, // One occurrence required 00088 OneOrMore = 0x03, // One or more occurrences required 00089 00090 // ConsumeAfter - Indicates that this option is fed anything that follows the 00091 // last positional argument required by the application (it is an error if 00092 // there are zero positional arguments, and a ConsumeAfter option is used). 00093 // Thus, for example, all arguments to LLI are processed until a filename is 00094 // found. Once a filename is found, all of the succeeding arguments are 00095 // passed, unprocessed, to the ConsumeAfter option. 00096 // 00097 ConsumeAfter = 0x04 00098 }; 00099 00100 enum ValueExpected { // Is a value required for the option? 00101 // zero reserved for the unspecified value 00102 ValueOptional = 0x01, // The value can appear... or not 00103 ValueRequired = 0x02, // The value is required to appear! 00104 ValueDisallowed = 0x03 // A value may not be specified (for flags) 00105 }; 00106 00107 enum OptionHidden { // Control whether -help shows this option 00108 NotHidden = 0x00, // Option included in -help & -help-hidden 00109 Hidden = 0x01, // -help doesn't, but -help-hidden does 00110 ReallyHidden = 0x02 // Neither -help nor -help-hidden show this arg 00111 }; 00112 00113 // Formatting flags - This controls special features that the option might have 00114 // that cause it to be parsed differently... 00115 // 00116 // Prefix - This option allows arguments that are otherwise unrecognized to be 00117 // matched by options that are a prefix of the actual value. This is useful for 00118 // cases like a linker, where options are typically of the form '-lfoo' or 00119 // '-L../../include' where -l or -L are the actual flags. When prefix is 00120 // enabled, and used, the value for the flag comes from the suffix of the 00121 // argument. 00122 // 00123 // Grouping - With this option enabled, multiple letter options are allowed to 00124 // bunch together with only a single hyphen for the whole group. This allows 00125 // emulation of the behavior that ls uses for example: ls -la === ls -l -a 00126 // 00127 00128 enum FormattingFlags { 00129 NormalFormatting = 0x00, // Nothing special 00130 Positional = 0x01, // Is a positional argument, no '-' required 00131 Prefix = 0x02, // Can this option directly prefix its value? 00132 Grouping = 0x03 // Can this option group with other options? 00133 }; 00134 00135 enum MiscFlags { // Miscellaneous flags to adjust argument 00136 CommaSeparated = 0x01, // Should this cl::list split between commas? 00137 PositionalEatsArgs = 0x02, // Should this positional cl::list eat -args? 00138 Sink = 0x04 // Should this cl::list eat all unknown options? 00139 }; 00140 00141 //===----------------------------------------------------------------------===// 00142 // Option Category class 00143 // 00144 class OptionCategory { 00145 private: 00146 const char *const Name; 00147 const char *const Description; 00148 void registerCategory(); 00149 public: 00150 OptionCategory(const char *const Name, const char *const Description = 0) 00151 : Name(Name), Description(Description) { registerCategory(); } 00152 const char *getName() { return Name; } 00153 const char *getDescription() { return Description; } 00154 }; 00155 00156 // The general Option Category (used as default category). 00157 extern OptionCategory GeneralCategory; 00158 00159 //===----------------------------------------------------------------------===// 00160 // Option Base class 00161 // 00162 class alias; 00163 class Option { 00164 friend class alias; 00165 00166 // handleOccurrences - Overriden by subclasses to handle the value passed into 00167 // an argument. Should return true if there was an error processing the 00168 // argument and the program should exit. 00169 // 00170 virtual bool handleOccurrence(unsigned pos, StringRef ArgName, 00171 StringRef Arg) = 0; 00172 00173 virtual enum ValueExpected getValueExpectedFlagDefault() const { 00174 return ValueOptional; 00175 } 00176 00177 // Out of line virtual function to provide home for the class. 00178 virtual void anchor(); 00179 00180 int NumOccurrences; // The number of times specified 00181 // Occurrences, HiddenFlag, and Formatting are all enum types but to avoid 00182 // problems with signed enums in bitfields. 00183 unsigned Occurrences : 3; // enum NumOccurrencesFlag 00184 // not using the enum type for 'Value' because zero is an implementation 00185 // detail representing the non-value 00186 unsigned Value : 2; 00187 unsigned HiddenFlag : 2; // enum OptionHidden 00188 unsigned Formatting : 2; // enum FormattingFlags 00189 unsigned Misc : 3; 00190 unsigned Position; // Position of last occurrence of the option 00191 unsigned AdditionalVals;// Greater than 0 for multi-valued option. 00192 Option *NextRegistered; // Singly linked list of registered options. 00193 00194 public: 00195 const char *ArgStr; // The argument string itself (ex: "help", "o") 00196 const char *HelpStr; // The descriptive text message for -help 00197 const char *ValueStr; // String describing what the value of this option is 00198 OptionCategory *Category; // The Category this option belongs to 00199 00200 inline enum NumOccurrencesFlag getNumOccurrencesFlag() const { 00201 return (enum NumOccurrencesFlag)Occurrences; 00202 } 00203 inline enum ValueExpected getValueExpectedFlag() const { 00204 return Value ? ((enum ValueExpected)Value) 00205 : getValueExpectedFlagDefault(); 00206 } 00207 inline enum OptionHidden getOptionHiddenFlag() const { 00208 return (enum OptionHidden)HiddenFlag; 00209 } 00210 inline enum FormattingFlags getFormattingFlag() const { 00211 return (enum FormattingFlags)Formatting; 00212 } 00213 inline unsigned getMiscFlags() const { 00214 return Misc; 00215 } 00216 inline unsigned getPosition() const { return Position; } 00217 inline unsigned getNumAdditionalVals() const { return AdditionalVals; } 00218 00219 // hasArgStr - Return true if the argstr != "" 00220 bool hasArgStr() const { return ArgStr[0] != 0; } 00221 00222 //-------------------------------------------------------------------------=== 00223 // Accessor functions set by OptionModifiers 00224 // 00225 void setArgStr(const char *S) { ArgStr = S; } 00226 void setDescription(const char *S) { HelpStr = S; } 00227 void setValueStr(const char *S) { ValueStr = S; } 00228 void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { 00229 Occurrences = Val; 00230 } 00231 void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; } 00232 void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; } 00233 void setFormattingFlag(enum FormattingFlags V) { Formatting = V; } 00234 void setMiscFlag(enum MiscFlags M) { Misc |= M; } 00235 void setPosition(unsigned pos) { Position = pos; } 00236 void setCategory(OptionCategory &C) { Category = &C; } 00237 protected: 00238 explicit Option(enum NumOccurrencesFlag OccurrencesFlag, 00239 enum OptionHidden Hidden) 00240 : NumOccurrences(0), Occurrences(OccurrencesFlag), Value(0), 00241 HiddenFlag(Hidden), Formatting(NormalFormatting), Misc(0), 00242 Position(0), AdditionalVals(0), NextRegistered(0), 00243 ArgStr(""), HelpStr(""), ValueStr(""), Category(&GeneralCategory) { 00244 } 00245 00246 inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; } 00247 public: 00248 // addArgument - Register this argument with the commandline system. 00249 // 00250 void addArgument(); 00251 00252 Option *getNextRegisteredOption() const { return NextRegistered; } 00253 00254 // Return the width of the option tag for printing... 00255 virtual size_t getOptionWidth() const = 0; 00256 00257 // printOptionInfo - Print out information about this option. The 00258 // to-be-maintained width is specified. 00259 // 00260 virtual void printOptionInfo(size_t GlobalWidth) const = 0; 00261 00262 virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0; 00263 00264 virtual void getExtraOptionNames(SmallVectorImpl<const char*> &) {} 00265 00266 // addOccurrence - Wrapper around handleOccurrence that enforces Flags. 00267 // 00268 bool addOccurrence(unsigned pos, StringRef ArgName, 00269 StringRef Value, bool MultiArg = false); 00270 00271 // Prints option name followed by message. Always returns true. 00272 bool error(const Twine &Message, StringRef ArgName = StringRef()); 00273 00274 public: 00275 inline int getNumOccurrences() const { return NumOccurrences; } 00276 virtual ~Option() {} 00277 }; 00278 00279 00280 //===----------------------------------------------------------------------===// 00281 // Command line option modifiers that can be used to modify the behavior of 00282 // command line option parsers... 00283 // 00284 00285 // desc - Modifier to set the description shown in the -help output... 00286 struct desc { 00287 const char *Desc; 00288 desc(const char *Str) : Desc(Str) {} 00289 void apply(Option &O) const { O.setDescription(Desc); } 00290 }; 00291 00292 // value_desc - Modifier to set the value description shown in the -help 00293 // output... 00294 struct value_desc { 00295 const char *Desc; 00296 value_desc(const char *Str) : Desc(Str) {} 00297 void apply(Option &O) const { O.setValueStr(Desc); } 00298 }; 00299 00300 // init - Specify a default (initial) value for the command line argument, if 00301 // the default constructor for the argument type does not give you what you 00302 // want. This is only valid on "opt" arguments, not on "list" arguments. 00303 // 00304 template<class Ty> 00305 struct initializer { 00306 const Ty &Init; 00307 initializer(const Ty &Val) : Init(Val) {} 00308 00309 template<class Opt> 00310 void apply(Opt &O) const { O.setInitialValue(Init); } 00311 }; 00312 00313 template<class Ty> 00314 initializer<Ty> init(const Ty &Val) { 00315 return initializer<Ty>(Val); 00316 } 00317 00318 00319 // location - Allow the user to specify which external variable they want to 00320 // store the results of the command line argument processing into, if they don't 00321 // want to store it in the option itself. 00322 // 00323 template<class Ty> 00324 struct LocationClass { 00325 Ty &Loc; 00326 LocationClass(Ty &L) : Loc(L) {} 00327 00328 template<class Opt> 00329 void apply(Opt &O) const { O.setLocation(O, Loc); } 00330 }; 00331 00332 template<class Ty> 00333 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); } 00334 00335 // cat - Specifiy the Option category for the command line argument to belong 00336 // to. 00337 struct cat { 00338 OptionCategory &Category; 00339 cat(OptionCategory &c) : Category(c) {} 00340 00341 template<class Opt> 00342 void apply(Opt &O) const { O.setCategory(Category); } 00343 }; 00344 00345 00346 //===----------------------------------------------------------------------===// 00347 // OptionValue class 00348 00349 // Support value comparison outside the template. 00350 struct GenericOptionValue { 00351 virtual ~GenericOptionValue() {} 00352 virtual bool compare(const GenericOptionValue &V) const = 0; 00353 private: 00354 virtual void anchor(); 00355 }; 00356 00357 template<class DataType> struct OptionValue; 00358 00359 // The default value safely does nothing. Option value printing is only 00360 // best-effort. 00361 template<class DataType, bool isClass> 00362 struct OptionValueBase : public GenericOptionValue { 00363 // Temporary storage for argument passing. 00364 typedef OptionValue<DataType> WrapperType; 00365 00366 bool hasValue() const { return false; } 00367 00368 const DataType &getValue() const { llvm_unreachable("no default value"); } 00369 00370 // Some options may take their value from a different data type. 00371 template<class DT> 00372 void setValue(const DT& /*V*/) {} 00373 00374 bool compare(const DataType &/*V*/) const { return false; } 00375 00376 virtual bool compare(const GenericOptionValue& /*V*/) const { return false; } 00377 }; 00378 00379 // Simple copy of the option value. 00380 template<class DataType> 00381 class OptionValueCopy : public GenericOptionValue { 00382 DataType Value; 00383 bool Valid; 00384 public: 00385 OptionValueCopy() : Valid(false) {} 00386 00387 bool hasValue() const { return Valid; } 00388 00389 const DataType &getValue() const { 00390 assert(Valid && "invalid option value"); 00391 return Value; 00392 } 00393 00394 void setValue(const DataType &V) { Valid = true; Value = V; } 00395 00396 bool compare(const DataType &V) const { 00397 return Valid && (Value != V); 00398 } 00399 00400 virtual bool compare(const GenericOptionValue &V) const { 00401 const OptionValueCopy<DataType> &VC = 00402 static_cast< const OptionValueCopy<DataType>& >(V); 00403 if (!VC.hasValue()) return false; 00404 return compare(VC.getValue()); 00405 } 00406 }; 00407 00408 // Non-class option values. 00409 template<class DataType> 00410 struct OptionValueBase<DataType, false> : OptionValueCopy<DataType> { 00411 typedef DataType WrapperType; 00412 }; 00413 00414 // Top-level option class. 00415 template<class DataType> 00416 struct OptionValue : OptionValueBase<DataType, is_class<DataType>::value> { 00417 OptionValue() {} 00418 00419 OptionValue(const DataType& V) { 00420 this->setValue(V); 00421 } 00422 // Some options may take their value from a different data type. 00423 template<class DT> 00424 OptionValue<DataType> &operator=(const DT& V) { 00425 this->setValue(V); 00426 return *this; 00427 } 00428 }; 00429 00430 // Other safe-to-copy-by-value common option types. 00431 enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE }; 00432 template<> 00433 struct OptionValue<cl::boolOrDefault> : OptionValueCopy<cl::boolOrDefault> { 00434 typedef cl::boolOrDefault WrapperType; 00435 00436 OptionValue() {} 00437 00438 OptionValue(const cl::boolOrDefault& V) { 00439 this->setValue(V); 00440 } 00441 OptionValue<cl::boolOrDefault> &operator=(const cl::boolOrDefault& V) { 00442 setValue(V); 00443 return *this; 00444 } 00445 private: 00446 virtual void anchor(); 00447 }; 00448 00449 template<> 00450 struct OptionValue<std::string> : OptionValueCopy<std::string> { 00451 typedef StringRef WrapperType; 00452 00453 OptionValue() {} 00454 00455 OptionValue(const std::string& V) { 00456 this->setValue(V); 00457 } 00458 OptionValue<std::string> &operator=(const std::string& V) { 00459 setValue(V); 00460 return *this; 00461 } 00462 private: 00463 virtual void anchor(); 00464 }; 00465 00466 //===----------------------------------------------------------------------===// 00467 // Enum valued command line option 00468 // 00469 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC 00470 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC 00471 #define clEnumValEnd (reinterpret_cast<void*>(0)) 00472 00473 // values - For custom data types, allow specifying a group of values together 00474 // as the values that go into the mapping that the option handler uses. Note 00475 // that the values list must always have a 0 at the end of the list to indicate 00476 // that the list has ended. 00477 // 00478 template<class DataType> 00479 class ValuesClass { 00480 // Use a vector instead of a map, because the lists should be short, 00481 // the overhead is less, and most importantly, it keeps them in the order 00482 // inserted so we can print our option out nicely. 00483 SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values; 00484 void processValues(va_list Vals); 00485 public: 00486 ValuesClass(const char *EnumName, DataType Val, const char *Desc, 00487 va_list ValueArgs) { 00488 // Insert the first value, which is required. 00489 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc))); 00490 00491 // Process the varargs portion of the values... 00492 while (const char *enumName = va_arg(ValueArgs, const char *)) { 00493 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int)); 00494 const char *EnumDesc = va_arg(ValueArgs, const char *); 00495 Values.push_back(std::make_pair(enumName, // Add value to value map 00496 std::make_pair(EnumVal, EnumDesc))); 00497 } 00498 } 00499 00500 template<class Opt> 00501 void apply(Opt &O) const { 00502 for (size_t i = 0, e = Values.size(); i != e; ++i) 00503 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first, 00504 Values[i].second.second); 00505 } 00506 }; 00507 00508 template<class DataType> 00509 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val, 00510 const char *Desc, ...) { 00511 va_list ValueArgs; 00512 va_start(ValueArgs, Desc); 00513 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs); 00514 va_end(ValueArgs); 00515 return Vals; 00516 } 00517 00518 //===----------------------------------------------------------------------===// 00519 // parser class - Parameterizable parser for different data types. By default, 00520 // known data types (string, int, bool) have specialized parsers, that do what 00521 // you would expect. The default parser, used for data types that are not 00522 // built-in, uses a mapping table to map specific options to values, which is 00523 // used, among other things, to handle enum types. 00524 00525 //-------------------------------------------------- 00526 // generic_parser_base - This class holds all the non-generic code that we do 00527 // not need replicated for every instance of the generic parser. This also 00528 // allows us to put stuff into CommandLine.cpp 00529 // 00530 class generic_parser_base { 00531 protected: 00532 class GenericOptionInfo { 00533 public: 00534 GenericOptionInfo(const char *name, const char *helpStr) : 00535 Name(name), HelpStr(helpStr) {} 00536 const char *Name; 00537 const char *HelpStr; 00538 }; 00539 public: 00540 virtual ~generic_parser_base() {} // Base class should have virtual-dtor 00541 00542 // getNumOptions - Virtual function implemented by generic subclass to 00543 // indicate how many entries are in Values. 00544 // 00545 virtual unsigned getNumOptions() const = 0; 00546 00547 // getOption - Return option name N. 00548 virtual const char *getOption(unsigned N) const = 0; 00549 00550 // getDescription - Return description N 00551 virtual const char *getDescription(unsigned N) const = 0; 00552 00553 // Return the width of the option tag for printing... 00554 virtual size_t getOptionWidth(const Option &O) const; 00555 00556 virtual const GenericOptionValue &getOptionValue(unsigned N) const = 0; 00557 00558 // printOptionInfo - Print out information about this option. The 00559 // to-be-maintained width is specified. 00560 // 00561 virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const; 00562 00563 void printGenericOptionDiff(const Option &O, const GenericOptionValue &V, 00564 const GenericOptionValue &Default, 00565 size_t GlobalWidth) const; 00566 00567 // printOptionDiff - print the value of an option and it's default. 00568 // 00569 // Template definition ensures that the option and default have the same 00570 // DataType (via the same AnyOptionValue). 00571 template<class AnyOptionValue> 00572 void printOptionDiff(const Option &O, const AnyOptionValue &V, 00573 const AnyOptionValue &Default, 00574 size_t GlobalWidth) const { 00575 printGenericOptionDiff(O, V, Default, GlobalWidth); 00576 } 00577 00578 void initialize(Option &O) { 00579 // All of the modifiers for the option have been processed by now, so the 00580 // argstr field should be stable, copy it down now. 00581 // 00582 hasArgStr = O.hasArgStr(); 00583 } 00584 00585 void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) { 00586 // If there has been no argstr specified, that means that we need to add an 00587 // argument for every possible option. This ensures that our options are 00588 // vectored to us. 00589 if (!hasArgStr) 00590 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) 00591 OptionNames.push_back(getOption(i)); 00592 } 00593 00594 00595 enum ValueExpected getValueExpectedFlagDefault() const { 00596 // If there is an ArgStr specified, then we are of the form: 00597 // 00598 // -opt=O2 or -opt O2 or -optO2 00599 // 00600 // In which case, the value is required. Otherwise if an arg str has not 00601 // been specified, we are of the form: 00602 // 00603 // -O2 or O2 or -la (where -l and -a are separate options) 00604 // 00605 // If this is the case, we cannot allow a value. 00606 // 00607 if (hasArgStr) 00608 return ValueRequired; 00609 else 00610 return ValueDisallowed; 00611 } 00612 00613 // findOption - Return the option number corresponding to the specified 00614 // argument string. If the option is not found, getNumOptions() is returned. 00615 // 00616 unsigned findOption(const char *Name); 00617 00618 protected: 00619 bool hasArgStr; 00620 }; 00621 00622 // Default parser implementation - This implementation depends on having a 00623 // mapping of recognized options to values of some sort. In addition to this, 00624 // each entry in the mapping also tracks a help message that is printed with the 00625 // command line option for -help. Because this is a simple mapping parser, the 00626 // data type can be any unsupported type. 00627 // 00628 template <class DataType> 00629 class parser : public generic_parser_base { 00630 protected: 00631 class OptionInfo : public GenericOptionInfo { 00632 public: 00633 OptionInfo(const char *name, DataType v, const char *helpStr) : 00634 GenericOptionInfo(name, helpStr), V(v) {} 00635 OptionValue<DataType> V; 00636 }; 00637 SmallVector<OptionInfo, 8> Values; 00638 public: 00639 typedef DataType parser_data_type; 00640 00641 // Implement virtual functions needed by generic_parser_base 00642 unsigned getNumOptions() const { return unsigned(Values.size()); } 00643 const char *getOption(unsigned N) const { return Values[N].Name; } 00644 const char *getDescription(unsigned N) const { 00645 return Values[N].HelpStr; 00646 } 00647 00648 // getOptionValue - Return the value of option name N. 00649 virtual const GenericOptionValue &getOptionValue(unsigned N) const { 00650 return Values[N].V; 00651 } 00652 00653 // parse - Return true on error. 00654 bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) { 00655 StringRef ArgVal; 00656 if (hasArgStr) 00657 ArgVal = Arg; 00658 else 00659 ArgVal = ArgName; 00660 00661 for (size_t i = 0, e = Values.size(); i != e; ++i) 00662 if (Values[i].Name == ArgVal) { 00663 V = Values[i].V.getValue(); 00664 return false; 00665 } 00666 00667 return O.error("Cannot find option named '" + ArgVal + "'!"); 00668 } 00669 00670 /// addLiteralOption - Add an entry to the mapping table. 00671 /// 00672 template <class DT> 00673 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) { 00674 assert(findOption(Name) == Values.size() && "Option already exists!"); 00675 OptionInfo X(Name, static_cast<DataType>(V), HelpStr); 00676 Values.push_back(X); 00677 MarkOptionsChanged(); 00678 } 00679 00680 /// removeLiteralOption - Remove the specified option. 00681 /// 00682 void removeLiteralOption(const char *Name) { 00683 unsigned N = findOption(Name); 00684 assert(N != Values.size() && "Option not found!"); 00685 Values.erase(Values.begin()+N); 00686 } 00687 }; 00688 00689 //-------------------------------------------------- 00690 // basic_parser - Super class of parsers to provide boilerplate code 00691 // 00692 class basic_parser_impl { // non-template implementation of basic_parser<t> 00693 public: 00694 virtual ~basic_parser_impl() {} 00695 00696 enum ValueExpected getValueExpectedFlagDefault() const { 00697 return ValueRequired; 00698 } 00699 00700 void getExtraOptionNames(SmallVectorImpl<const char*> &) {} 00701 00702 void initialize(Option &) {} 00703 00704 // Return the width of the option tag for printing... 00705 size_t getOptionWidth(const Option &O) const; 00706 00707 // printOptionInfo - Print out information about this option. The 00708 // to-be-maintained width is specified. 00709 // 00710 void printOptionInfo(const Option &O, size_t GlobalWidth) const; 00711 00712 // printOptionNoValue - Print a placeholder for options that don't yet support 00713 // printOptionDiff(). 00714 void printOptionNoValue(const Option &O, size_t GlobalWidth) const; 00715 00716 // getValueName - Overload in subclass to provide a better default value. 00717 virtual const char *getValueName() const { return "value"; } 00718 00719 // An out-of-line virtual method to provide a 'home' for this class. 00720 virtual void anchor(); 00721 00722 protected: 00723 // A helper for basic_parser::printOptionDiff. 00724 void printOptionName(const Option &O, size_t GlobalWidth) const; 00725 }; 00726 00727 // basic_parser - The real basic parser is just a template wrapper that provides 00728 // a typedef for the provided data type. 00729 // 00730 template<class DataType> 00731 class basic_parser : public basic_parser_impl { 00732 public: 00733 typedef DataType parser_data_type; 00734 typedef OptionValue<DataType> OptVal; 00735 }; 00736 00737 //-------------------------------------------------- 00738 // parser<bool> 00739 // 00740 template<> 00741 class parser<bool> : public basic_parser<bool> { 00742 const char *ArgStr; 00743 public: 00744 00745 // parse - Return true on error. 00746 bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val); 00747 00748 template <class Opt> 00749 void initialize(Opt &O) { 00750 ArgStr = O.ArgStr; 00751 } 00752 00753 enum ValueExpected getValueExpectedFlagDefault() const { 00754 return ValueOptional; 00755 } 00756 00757 // getValueName - Do not print =<value> at all. 00758 virtual const char *getValueName() const { return 0; } 00759 00760 void printOptionDiff(const Option &O, bool V, OptVal Default, 00761 size_t GlobalWidth) const; 00762 00763 // An out-of-line virtual method to provide a 'home' for this class. 00764 virtual void anchor(); 00765 }; 00766 00767 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>); 00768 00769 //-------------------------------------------------- 00770 // parser<boolOrDefault> 00771 template<> 00772 class parser<boolOrDefault> : public basic_parser<boolOrDefault> { 00773 public: 00774 // parse - Return true on error. 00775 bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val); 00776 00777 enum ValueExpected getValueExpectedFlagDefault() const { 00778 return ValueOptional; 00779 } 00780 00781 // getValueName - Do not print =<value> at all. 00782 virtual const char *getValueName() const { return 0; } 00783 00784 void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default, 00785 size_t GlobalWidth) const; 00786 00787 // An out-of-line virtual method to provide a 'home' for this class. 00788 virtual void anchor(); 00789 }; 00790 00791 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>); 00792 00793 //-------------------------------------------------- 00794 // parser<int> 00795 // 00796 template<> 00797 class parser<int> : public basic_parser<int> { 00798 public: 00799 // parse - Return true on error. 00800 bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val); 00801 00802 // getValueName - Overload in subclass to provide a better default value. 00803 virtual const char *getValueName() const { return "int"; } 00804 00805 void printOptionDiff(const Option &O, int V, OptVal Default, 00806 size_t GlobalWidth) const; 00807 00808 // An out-of-line virtual method to provide a 'home' for this class. 00809 virtual void anchor(); 00810 }; 00811 00812 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>); 00813 00814 00815 //-------------------------------------------------- 00816 // parser<unsigned> 00817 // 00818 template<> 00819 class parser<unsigned> : public basic_parser<unsigned> { 00820 public: 00821 // parse - Return true on error. 00822 bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val); 00823 00824 // getValueName - Overload in subclass to provide a better default value. 00825 virtual const char *getValueName() const { return "uint"; } 00826 00827 void printOptionDiff(const Option &O, unsigned V, OptVal Default, 00828 size_t GlobalWidth) const; 00829 00830 // An out-of-line virtual method to provide a 'home' for this class. 00831 virtual void anchor(); 00832 }; 00833 00834 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>); 00835 00836 //-------------------------------------------------- 00837 // parser<unsigned long long> 00838 // 00839 template<> 00840 class parser<unsigned long long> : public basic_parser<unsigned long long> { 00841 public: 00842 // parse - Return true on error. 00843 bool parse(Option &O, StringRef ArgName, StringRef Arg, 00844 unsigned long long &Val); 00845 00846 // getValueName - Overload in subclass to provide a better default value. 00847 virtual const char *getValueName() const { return "uint"; } 00848 00849 void printOptionDiff(const Option &O, unsigned long long V, OptVal Default, 00850 size_t GlobalWidth) const; 00851 00852 // An out-of-line virtual method to provide a 'home' for this class. 00853 virtual void anchor(); 00854 }; 00855 00856 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned long long>); 00857 00858 //-------------------------------------------------- 00859 // parser<double> 00860 // 00861 template<> 00862 class parser<double> : public basic_parser<double> { 00863 public: 00864 // parse - Return true on error. 00865 bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val); 00866 00867 // getValueName - Overload in subclass to provide a better default value. 00868 virtual const char *getValueName() const { return "number"; } 00869 00870 void printOptionDiff(const Option &O, double V, OptVal Default, 00871 size_t GlobalWidth) const; 00872 00873 // An out-of-line virtual method to provide a 'home' for this class. 00874 virtual void anchor(); 00875 }; 00876 00877 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>); 00878 00879 //-------------------------------------------------- 00880 // parser<float> 00881 // 00882 template<> 00883 class parser<float> : public basic_parser<float> { 00884 public: 00885 // parse - Return true on error. 00886 bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val); 00887 00888 // getValueName - Overload in subclass to provide a better default value. 00889 virtual const char *getValueName() const { return "number"; } 00890 00891 void printOptionDiff(const Option &O, float V, OptVal Default, 00892 size_t GlobalWidth) const; 00893 00894 // An out-of-line virtual method to provide a 'home' for this class. 00895 virtual void anchor(); 00896 }; 00897 00898 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>); 00899 00900 //-------------------------------------------------- 00901 // parser<std::string> 00902 // 00903 template<> 00904 class parser<std::string> : public basic_parser<std::string> { 00905 public: 00906 // parse - Return true on error. 00907 bool parse(Option &, StringRef, StringRef Arg, std::string &Value) { 00908 Value = Arg.str(); 00909 return false; 00910 } 00911 00912 // getValueName - Overload in subclass to provide a better default value. 00913 virtual const char *getValueName() const { return "string"; } 00914 00915 void printOptionDiff(const Option &O, StringRef V, OptVal Default, 00916 size_t GlobalWidth) const; 00917 00918 // An out-of-line virtual method to provide a 'home' for this class. 00919 virtual void anchor(); 00920 }; 00921 00922 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>); 00923 00924 //-------------------------------------------------- 00925 // parser<char> 00926 // 00927 template<> 00928 class parser<char> : public basic_parser<char> { 00929 public: 00930 // parse - Return true on error. 00931 bool parse(Option &, StringRef, StringRef Arg, char &Value) { 00932 Value = Arg[0]; 00933 return false; 00934 } 00935 00936 // getValueName - Overload in subclass to provide a better default value. 00937 virtual const char *getValueName() const { return "char"; } 00938 00939 void printOptionDiff(const Option &O, char V, OptVal Default, 00940 size_t GlobalWidth) const; 00941 00942 // An out-of-line virtual method to provide a 'home' for this class. 00943 virtual void anchor(); 00944 }; 00945 00946 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<char>); 00947 00948 //-------------------------------------------------- 00949 // PrintOptionDiff 00950 // 00951 // This collection of wrappers is the intermediary between class opt and class 00952 // parser to handle all the template nastiness. 00953 00954 // This overloaded function is selected by the generic parser. 00955 template<class ParserClass, class DT> 00956 void printOptionDiff(const Option &O, const generic_parser_base &P, const DT &V, 00957 const OptionValue<DT> &Default, size_t GlobalWidth) { 00958 OptionValue<DT> OV = V; 00959 P.printOptionDiff(O, OV, Default, GlobalWidth); 00960 } 00961 00962 // This is instantiated for basic parsers when the parsed value has a different 00963 // type than the option value. e.g. HelpPrinter. 00964 template<class ParserDT, class ValDT> 00965 struct OptionDiffPrinter { 00966 void print(const Option &O, const parser<ParserDT> P, const ValDT &/*V*/, 00967 const OptionValue<ValDT> &/*Default*/, size_t GlobalWidth) { 00968 P.printOptionNoValue(O, GlobalWidth); 00969 } 00970 }; 00971 00972 // This is instantiated for basic parsers when the parsed value has the same 00973 // type as the option value. 00974 template<class DT> 00975 struct OptionDiffPrinter<DT, DT> { 00976 void print(const Option &O, const parser<DT> P, const DT &V, 00977 const OptionValue<DT> &Default, size_t GlobalWidth) { 00978 P.printOptionDiff(O, V, Default, GlobalWidth); 00979 } 00980 }; 00981 00982 // This overloaded function is selected by the basic parser, which may parse a 00983 // different type than the option type. 00984 template<class ParserClass, class ValDT> 00985 void printOptionDiff( 00986 const Option &O, 00987 const basic_parser<typename ParserClass::parser_data_type> &P, 00988 const ValDT &V, const OptionValue<ValDT> &Default, 00989 size_t GlobalWidth) { 00990 00991 OptionDiffPrinter<typename ParserClass::parser_data_type, ValDT> printer; 00992 printer.print(O, static_cast<const ParserClass&>(P), V, Default, 00993 GlobalWidth); 00994 } 00995 00996 //===----------------------------------------------------------------------===// 00997 // applicator class - This class is used because we must use partial 00998 // specialization to handle literal string arguments specially (const char* does 00999 // not correctly respond to the apply method). Because the syntax to use this 01000 // is a pain, we have the 'apply' method below to handle the nastiness... 01001 // 01002 template<class Mod> struct applicator { 01003 template<class Opt> 01004 static void opt(const Mod &M, Opt &O) { M.apply(O); } 01005 }; 01006 01007 // Handle const char* as a special case... 01008 template<unsigned n> struct applicator<char[n]> { 01009 template<class Opt> 01010 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 01011 }; 01012 template<unsigned n> struct applicator<const char[n]> { 01013 template<class Opt> 01014 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 01015 }; 01016 template<> struct applicator<const char*> { 01017 template<class Opt> 01018 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 01019 }; 01020 01021 template<> struct applicator<NumOccurrencesFlag> { 01022 static void opt(NumOccurrencesFlag NO, Option &O) { 01023 O.setNumOccurrencesFlag(NO); 01024 } 01025 }; 01026 template<> struct applicator<ValueExpected> { 01027 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); } 01028 }; 01029 template<> struct applicator<OptionHidden> { 01030 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); } 01031 }; 01032 template<> struct applicator<FormattingFlags> { 01033 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); } 01034 }; 01035 template<> struct applicator<MiscFlags> { 01036 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); } 01037 }; 01038 01039 // apply method - Apply a modifier to an option in a type safe way. 01040 template<class Mod, class Opt> 01041 void apply(const Mod &M, Opt *O) { 01042 applicator<Mod>::opt(M, *O); 01043 } 01044 01045 //===----------------------------------------------------------------------===// 01046 // opt_storage class 01047 01048 // Default storage class definition: external storage. This implementation 01049 // assumes the user will specify a variable to store the data into with the 01050 // cl::location(x) modifier. 01051 // 01052 template<class DataType, bool ExternalStorage, bool isClass> 01053 class opt_storage { 01054 DataType *Location; // Where to store the object... 01055 OptionValue<DataType> Default; 01056 01057 void check() const { 01058 assert(Location != 0 && "cl::location(...) not specified for a command " 01059 "line option with external storage, " 01060 "or cl::init specified before cl::location()!!"); 01061 } 01062 public: 01063 opt_storage() : Location(0) {} 01064 01065 bool setLocation(Option &O, DataType &L) { 01066 if (Location) 01067 return O.error("cl::location(x) specified more than once!"); 01068 Location = &L; 01069 Default = L; 01070 return false; 01071 } 01072 01073 template<class T> 01074 void setValue(const T &V, bool initial = false) { 01075 check(); 01076 *Location = V; 01077 if (initial) 01078 Default = V; 01079 } 01080 01081 DataType &getValue() { check(); return *Location; } 01082 const DataType &getValue() const { check(); return *Location; } 01083 01084 operator DataType() const { return this->getValue(); } 01085 01086 const OptionValue<DataType> &getDefault() const { return Default; } 01087 }; 01088 01089 // Define how to hold a class type object, such as a string. Since we can 01090 // inherit from a class, we do so. This makes us exactly compatible with the 01091 // object in all cases that it is used. 01092 // 01093 template<class DataType> 01094 class opt_storage<DataType,false,true> : public DataType { 01095 public: 01096 OptionValue<DataType> Default; 01097 01098 template<class T> 01099 void setValue(const T &V, bool initial = false) { 01100 DataType::operator=(V); 01101 if (initial) 01102 Default = V; 01103 } 01104 01105 DataType &getValue() { return *this; } 01106 const DataType &getValue() const { return *this; } 01107 01108 const OptionValue<DataType> &getDefault() const { return Default; } 01109 }; 01110 01111 // Define a partial specialization to handle things we cannot inherit from. In 01112 // this case, we store an instance through containment, and overload operators 01113 // to get at the value. 01114 // 01115 template<class DataType> 01116 class opt_storage<DataType, false, false> { 01117 public: 01118 DataType Value; 01119 OptionValue<DataType> Default; 01120 01121 // Make sure we initialize the value with the default constructor for the 01122 // type. 01123 opt_storage() : Value(DataType()), Default(DataType()) {} 01124 01125 template<class T> 01126 void setValue(const T &V, bool initial = false) { 01127 Value = V; 01128 if (initial) 01129 Default = V; 01130 } 01131 DataType &getValue() { return Value; } 01132 DataType getValue() const { return Value; } 01133 01134 const OptionValue<DataType> &getDefault() const { return Default; } 01135 01136 operator DataType() const { return getValue(); } 01137 01138 // If the datatype is a pointer, support -> on it. 01139 DataType operator->() const { return Value; } 01140 }; 01141 01142 01143 //===----------------------------------------------------------------------===// 01144 // opt - A scalar command line option. 01145 // 01146 template <class DataType, bool ExternalStorage = false, 01147 class ParserClass = parser<DataType> > 01148 class opt : public Option, 01149 public opt_storage<DataType, ExternalStorage, 01150 is_class<DataType>::value> { 01151 ParserClass Parser; 01152 01153 virtual bool handleOccurrence(unsigned pos, StringRef ArgName, 01154 StringRef Arg) { 01155 typename ParserClass::parser_data_type Val = 01156 typename ParserClass::parser_data_type(); 01157 if (Parser.parse(*this, ArgName, Arg, Val)) 01158 return true; // Parse error! 01159 this->setValue(Val); 01160 this->setPosition(pos); 01161 return false; 01162 } 01163 01164 virtual enum ValueExpected getValueExpectedFlagDefault() const { 01165 return Parser.getValueExpectedFlagDefault(); 01166 } 01167 virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) { 01168 return Parser.getExtraOptionNames(OptionNames); 01169 } 01170 01171 // Forward printing stuff to the parser... 01172 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);} 01173 virtual void printOptionInfo(size_t GlobalWidth) const { 01174 Parser.printOptionInfo(*this, GlobalWidth); 01175 } 01176 01177 virtual void printOptionValue(size_t GlobalWidth, bool Force) const { 01178 if (Force || this->getDefault().compare(this->getValue())) { 01179 cl::printOptionDiff<ParserClass>( 01180 *this, Parser, this->getValue(), this->getDefault(), GlobalWidth); 01181 } 01182 } 01183 01184 void done() { 01185 addArgument(); 01186 Parser.initialize(*this); 01187 } 01188 public: 01189 // setInitialValue - Used by the cl::init modifier... 01190 void setInitialValue(const DataType &V) { this->setValue(V, true); } 01191 01192 ParserClass &getParser() { return Parser; } 01193 01194 template<class T> 01195 DataType &operator=(const T &Val) { 01196 this->setValue(Val); 01197 return this->getValue(); 01198 } 01199 01200 // One option... 01201 template<class M0t> 01202 explicit opt(const M0t &M0) : Option(Optional, NotHidden) { 01203 apply(M0, this); 01204 done(); 01205 } 01206 01207 // Two options... 01208 template<class M0t, class M1t> 01209 opt(const M0t &M0, const M1t &M1) : Option(Optional, NotHidden) { 01210 apply(M0, this); apply(M1, this); 01211 done(); 01212 } 01213 01214 // Three options... 01215 template<class M0t, class M1t, class M2t> 01216 opt(const M0t &M0, const M1t &M1, 01217 const M2t &M2) : Option(Optional, NotHidden) { 01218 apply(M0, this); apply(M1, this); apply(M2, this); 01219 done(); 01220 } 01221 // Four options... 01222 template<class M0t, class M1t, class M2t, class M3t> 01223 opt(const M0t &M0, const M1t &M1, const M2t &M2, 01224 const M3t &M3) : Option(Optional, NotHidden) { 01225 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01226 done(); 01227 } 01228 // Five options... 01229 template<class M0t, class M1t, class M2t, class M3t, class M4t> 01230 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01231 const M4t &M4) : Option(Optional, NotHidden) { 01232 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01233 apply(M4, this); 01234 done(); 01235 } 01236 // Six options... 01237 template<class M0t, class M1t, class M2t, class M3t, 01238 class M4t, class M5t> 01239 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01240 const M4t &M4, const M5t &M5) : Option(Optional, NotHidden) { 01241 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01242 apply(M4, this); apply(M5, this); 01243 done(); 01244 } 01245 // Seven options... 01246 template<class M0t, class M1t, class M2t, class M3t, 01247 class M4t, class M5t, class M6t> 01248 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01249 const M4t &M4, const M5t &M5, 01250 const M6t &M6) : Option(Optional, NotHidden) { 01251 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01252 apply(M4, this); apply(M5, this); apply(M6, this); 01253 done(); 01254 } 01255 // Eight options... 01256 template<class M0t, class M1t, class M2t, class M3t, 01257 class M4t, class M5t, class M6t, class M7t> 01258 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01259 const M4t &M4, const M5t &M5, const M6t &M6, 01260 const M7t &M7) : Option(Optional, NotHidden) { 01261 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01262 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 01263 done(); 01264 } 01265 }; 01266 01267 EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>); 01268 EXTERN_TEMPLATE_INSTANTIATION(class opt<int>); 01269 EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>); 01270 EXTERN_TEMPLATE_INSTANTIATION(class opt<char>); 01271 EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>); 01272 01273 //===----------------------------------------------------------------------===// 01274 // list_storage class 01275 01276 // Default storage class definition: external storage. This implementation 01277 // assumes the user will specify a variable to store the data into with the 01278 // cl::location(x) modifier. 01279 // 01280 template<class DataType, class StorageClass> 01281 class list_storage { 01282 StorageClass *Location; // Where to store the object... 01283 01284 public: 01285 list_storage() : Location(0) {} 01286 01287 bool setLocation(Option &O, StorageClass &L) { 01288 if (Location) 01289 return O.error("cl::location(x) specified more than once!"); 01290 Location = &L; 01291 return false; 01292 } 01293 01294 template<class T> 01295 void addValue(const T &V) { 01296 assert(Location != 0 && "cl::location(...) not specified for a command " 01297 "line option with external storage!"); 01298 Location->push_back(V); 01299 } 01300 }; 01301 01302 01303 // Define how to hold a class type object, such as a string. Since we can 01304 // inherit from a class, we do so. This makes us exactly compatible with the 01305 // object in all cases that it is used. 01306 // 01307 template<class DataType> 01308 class list_storage<DataType, bool> : public std::vector<DataType> { 01309 public: 01310 template<class T> 01311 void addValue(const T &V) { std::vector<DataType>::push_back(V); } 01312 }; 01313 01314 01315 //===----------------------------------------------------------------------===// 01316 // list - A list of command line options. 01317 // 01318 template <class DataType, class Storage = bool, 01319 class ParserClass = parser<DataType> > 01320 class list : public Option, public list_storage<DataType, Storage> { 01321 std::vector<unsigned> Positions; 01322 ParserClass Parser; 01323 01324 virtual enum ValueExpected getValueExpectedFlagDefault() const { 01325 return Parser.getValueExpectedFlagDefault(); 01326 } 01327 virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) { 01328 return Parser.getExtraOptionNames(OptionNames); 01329 } 01330 01331 virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg){ 01332 typename ParserClass::parser_data_type Val = 01333 typename ParserClass::parser_data_type(); 01334 if (Parser.parse(*this, ArgName, Arg, Val)) 01335 return true; // Parse Error! 01336 list_storage<DataType, Storage>::addValue(Val); 01337 setPosition(pos); 01338 Positions.push_back(pos); 01339 return false; 01340 } 01341 01342 // Forward printing stuff to the parser... 01343 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);} 01344 virtual void printOptionInfo(size_t GlobalWidth) const { 01345 Parser.printOptionInfo(*this, GlobalWidth); 01346 } 01347 01348 // Unimplemented: list options don't currently store their default value. 01349 virtual void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const {} 01350 01351 void done() { 01352 addArgument(); 01353 Parser.initialize(*this); 01354 } 01355 public: 01356 ParserClass &getParser() { return Parser; } 01357 01358 unsigned getPosition(unsigned optnum) const { 01359 assert(optnum < this->size() && "Invalid option index"); 01360 return Positions[optnum]; 01361 } 01362 01363 void setNumAdditionalVals(unsigned n) { 01364 Option::setNumAdditionalVals(n); 01365 } 01366 01367 // One option... 01368 template<class M0t> 01369 explicit list(const M0t &M0) : Option(ZeroOrMore, NotHidden) { 01370 apply(M0, this); 01371 done(); 01372 } 01373 // Two options... 01374 template<class M0t, class M1t> 01375 list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore, NotHidden) { 01376 apply(M0, this); apply(M1, this); 01377 done(); 01378 } 01379 // Three options... 01380 template<class M0t, class M1t, class M2t> 01381 list(const M0t &M0, const M1t &M1, const M2t &M2) 01382 : Option(ZeroOrMore, NotHidden) { 01383 apply(M0, this); apply(M1, this); apply(M2, this); 01384 done(); 01385 } 01386 // Four options... 01387 template<class M0t, class M1t, class M2t, class M3t> 01388 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 01389 : Option(ZeroOrMore, NotHidden) { 01390 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01391 done(); 01392 } 01393 // Five options... 01394 template<class M0t, class M1t, class M2t, class M3t, class M4t> 01395 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01396 const M4t &M4) : Option(ZeroOrMore, NotHidden) { 01397 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01398 apply(M4, this); 01399 done(); 01400 } 01401 // Six options... 01402 template<class M0t, class M1t, class M2t, class M3t, 01403 class M4t, class M5t> 01404 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01405 const M4t &M4, const M5t &M5) : Option(ZeroOrMore, NotHidden) { 01406 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01407 apply(M4, this); apply(M5, this); 01408 done(); 01409 } 01410 // Seven options... 01411 template<class M0t, class M1t, class M2t, class M3t, 01412 class M4t, class M5t, class M6t> 01413 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01414 const M4t &M4, const M5t &M5, const M6t &M6) 01415 : Option(ZeroOrMore, NotHidden) { 01416 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01417 apply(M4, this); apply(M5, this); apply(M6, this); 01418 done(); 01419 } 01420 // Eight options... 01421 template<class M0t, class M1t, class M2t, class M3t, 01422 class M4t, class M5t, class M6t, class M7t> 01423 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01424 const M4t &M4, const M5t &M5, const M6t &M6, 01425 const M7t &M7) : Option(ZeroOrMore, NotHidden) { 01426 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01427 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 01428 done(); 01429 } 01430 }; 01431 01432 // multi_val - Modifier to set the number of additional values. 01433 struct multi_val { 01434 unsigned AdditionalVals; 01435 explicit multi_val(unsigned N) : AdditionalVals(N) {} 01436 01437 template <typename D, typename S, typename P> 01438 void apply(list<D, S, P> &L) const { L.setNumAdditionalVals(AdditionalVals); } 01439 }; 01440 01441 01442 //===----------------------------------------------------------------------===// 01443 // bits_storage class 01444 01445 // Default storage class definition: external storage. This implementation 01446 // assumes the user will specify a variable to store the data into with the 01447 // cl::location(x) modifier. 01448 // 01449 template<class DataType, class StorageClass> 01450 class bits_storage { 01451 unsigned *Location; // Where to store the bits... 01452 01453 template<class T> 01454 static unsigned Bit(const T &V) { 01455 unsigned BitPos = reinterpret_cast<unsigned>(V); 01456 assert(BitPos < sizeof(unsigned) * CHAR_BIT && 01457 "enum exceeds width of bit vector!"); 01458 return 1 << BitPos; 01459 } 01460 01461 public: 01462 bits_storage() : Location(0) {} 01463 01464 bool setLocation(Option &O, unsigned &L) { 01465 if (Location) 01466 return O.error("cl::location(x) specified more than once!"); 01467 Location = &L; 01468 return false; 01469 } 01470 01471 template<class T> 01472 void addValue(const T &V) { 01473 assert(Location != 0 && "cl::location(...) not specified for a command " 01474 "line option with external storage!"); 01475 *Location |= Bit(V); 01476 } 01477 01478 unsigned getBits() { return *Location; } 01479 01480 template<class T> 01481 bool isSet(const T &V) { 01482 return (*Location & Bit(V)) != 0; 01483 } 01484 }; 01485 01486 01487 // Define how to hold bits. Since we can inherit from a class, we do so. 01488 // This makes us exactly compatible with the bits in all cases that it is used. 01489 // 01490 template<class DataType> 01491 class bits_storage<DataType, bool> { 01492 unsigned Bits; // Where to store the bits... 01493 01494 template<class T> 01495 static unsigned Bit(const T &V) { 01496 unsigned BitPos = (unsigned)V; 01497 assert(BitPos < sizeof(unsigned) * CHAR_BIT && 01498 "enum exceeds width of bit vector!"); 01499 return 1 << BitPos; 01500 } 01501 01502 public: 01503 template<class T> 01504 void addValue(const T &V) { 01505 Bits |= Bit(V); 01506 } 01507 01508 unsigned getBits() { return Bits; } 01509 01510 template<class T> 01511 bool isSet(const T &V) { 01512 return (Bits & Bit(V)) != 0; 01513 } 01514 }; 01515 01516 01517 //===----------------------------------------------------------------------===// 01518 // bits - A bit vector of command options. 01519 // 01520 template <class DataType, class Storage = bool, 01521 class ParserClass = parser<DataType> > 01522 class bits : public Option, public bits_storage<DataType, Storage> { 01523 std::vector<unsigned> Positions; 01524 ParserClass Parser; 01525 01526 virtual enum ValueExpected getValueExpectedFlagDefault() const { 01527 return Parser.getValueExpectedFlagDefault(); 01528 } 01529 virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) { 01530 return Parser.getExtraOptionNames(OptionNames); 01531 } 01532 01533 virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg){ 01534 typename ParserClass::parser_data_type Val = 01535 typename ParserClass::parser_data_type(); 01536 if (Parser.parse(*this, ArgName, Arg, Val)) 01537 return true; // Parse Error! 01538 this->addValue(Val); 01539 setPosition(pos); 01540 Positions.push_back(pos); 01541 return false; 01542 } 01543 01544 // Forward printing stuff to the parser... 01545 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);} 01546 virtual void printOptionInfo(size_t GlobalWidth) const { 01547 Parser.printOptionInfo(*this, GlobalWidth); 01548 } 01549 01550 // Unimplemented: bits options don't currently store their default values. 01551 virtual void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const {} 01552 01553 void done() { 01554 addArgument(); 01555 Parser.initialize(*this); 01556 } 01557 public: 01558 ParserClass &getParser() { return Parser; } 01559 01560 unsigned getPosition(unsigned optnum) const { 01561 assert(optnum < this->size() && "Invalid option index"); 01562 return Positions[optnum]; 01563 } 01564 01565 // One option... 01566 template<class M0t> 01567 explicit bits(const M0t &M0) : Option(ZeroOrMore, NotHidden) { 01568 apply(M0, this); 01569 done(); 01570 } 01571 // Two options... 01572 template<class M0t, class M1t> 01573 bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore, NotHidden) { 01574 apply(M0, this); apply(M1, this); 01575 done(); 01576 } 01577 // Three options... 01578 template<class M0t, class M1t, class M2t> 01579 bits(const M0t &M0, const M1t &M1, const M2t &M2) 01580 : Option(ZeroOrMore, NotHidden) { 01581 apply(M0, this); apply(M1, this); apply(M2, this); 01582 done(); 01583 } 01584 // Four options... 01585 template<class M0t, class M1t, class M2t, class M3t> 01586 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 01587 : Option(ZeroOrMore, NotHidden) { 01588 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01589 done(); 01590 } 01591 // Five options... 01592 template<class M0t, class M1t, class M2t, class M3t, class M4t> 01593 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01594 const M4t &M4) : Option(ZeroOrMore, NotHidden) { 01595 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01596 apply(M4, this); 01597 done(); 01598 } 01599 // Six options... 01600 template<class M0t, class M1t, class M2t, class M3t, 01601 class M4t, class M5t> 01602 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01603 const M4t &M4, const M5t &M5) : Option(ZeroOrMore, NotHidden) { 01604 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01605 apply(M4, this); apply(M5, this); 01606 done(); 01607 } 01608 // Seven options... 01609 template<class M0t, class M1t, class M2t, class M3t, 01610 class M4t, class M5t, class M6t> 01611 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01612 const M4t &M4, const M5t &M5, const M6t &M6) 01613 : Option(ZeroOrMore, NotHidden) { 01614 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01615 apply(M4, this); apply(M5, this); apply(M6, this); 01616 done(); 01617 } 01618 // Eight options... 01619 template<class M0t, class M1t, class M2t, class M3t, 01620 class M4t, class M5t, class M6t, class M7t> 01621 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01622 const M4t &M4, const M5t &M5, const M6t &M6, 01623 const M7t &M7) : Option(ZeroOrMore, NotHidden) { 01624 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01625 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 01626 done(); 01627 } 01628 }; 01629 01630 //===----------------------------------------------------------------------===// 01631 // Aliased command line option (alias this name to a preexisting name) 01632 // 01633 01634 class alias : public Option { 01635 Option *AliasFor; 01636 virtual bool handleOccurrence(unsigned pos, StringRef /*ArgName*/, 01637 StringRef Arg) LLVM_OVERRIDE { 01638 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg); 01639 } 01640 // Handle printing stuff... 01641 virtual size_t getOptionWidth() const LLVM_OVERRIDE; 01642 virtual void printOptionInfo(size_t GlobalWidth) const LLVM_OVERRIDE; 01643 01644 // Aliases do not need to print their values. 01645 virtual void printOptionValue(size_t /*GlobalWidth*/, 01646 bool /*Force*/) const LLVM_OVERRIDE {} 01647 01648 void done() { 01649 if (!hasArgStr()) 01650 error("cl::alias must have argument name specified!"); 01651 if (AliasFor == 0) 01652 error("cl::alias must have an cl::aliasopt(option) specified!"); 01653 addArgument(); 01654 } 01655 public: 01656 void setAliasFor(Option &O) { 01657 if (AliasFor) 01658 error("cl::alias must only have one cl::aliasopt(...) specified!"); 01659 AliasFor = &O; 01660 } 01661 01662 // One option... 01663 template<class M0t> 01664 explicit alias(const M0t &M0) : Option(Optional, Hidden), AliasFor(0) { 01665 apply(M0, this); 01666 done(); 01667 } 01668 // Two options... 01669 template<class M0t, class M1t> 01670 alias(const M0t &M0, const M1t &M1) : Option(Optional, Hidden), AliasFor(0) { 01671 apply(M0, this); apply(M1, this); 01672 done(); 01673 } 01674 // Three options... 01675 template<class M0t, class M1t, class M2t> 01676 alias(const M0t &M0, const M1t &M1, const M2t &M2) 01677 : Option(Optional, Hidden), AliasFor(0) { 01678 apply(M0, this); apply(M1, this); apply(M2, this); 01679 done(); 01680 } 01681 // Four options... 01682 template<class M0t, class M1t, class M2t, class M3t> 01683 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 01684 : Option(Optional, Hidden), AliasFor(0) { 01685 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01686 done(); 01687 } 01688 }; 01689 01690 // aliasfor - Modifier to set the option an alias aliases. 01691 struct aliasopt { 01692 Option &Opt; 01693 explicit aliasopt(Option &O) : Opt(O) {} 01694 void apply(alias &A) const { A.setAliasFor(Opt); } 01695 }; 01696 01697 // extrahelp - provide additional help at the end of the normal help 01698 // output. All occurrences of cl::extrahelp will be accumulated and 01699 // printed to stderr at the end of the regular help, just before 01700 // exit is called. 01701 struct extrahelp { 01702 const char * morehelp; 01703 explicit extrahelp(const char* help); 01704 }; 01705 01706 void PrintVersionMessage(); 01707 01708 /// This function just prints the help message, exactly the same way as if the 01709 /// -help or -help-hidden option had been given on the command line. 01710 /// 01711 /// NOTE: THIS FUNCTION TERMINATES THE PROGRAM! 01712 /// 01713 /// \param Hidden if true will print hidden options 01714 /// \param Categorized if true print options in categories 01715 void PrintHelpMessage(bool Hidden=false, bool Categorized=false); 01716 01717 01718 //===----------------------------------------------------------------------===// 01719 // Public interface for accessing registered options. 01720 // 01721 01722 /// \brief Use this to get a StringMap to all registered named options 01723 /// (e.g. -help). Note \p Map Should be an empty StringMap. 01724 /// 01725 /// \param [out] Map will be filled with mappings where the key is the 01726 /// Option argument string (e.g. "help") and value is the corresponding 01727 /// Option*. 01728 /// 01729 /// Access to unnamed arguments (i.e. positional) are not provided because 01730 /// it is expected that the client already has access to these. 01731 /// 01732 /// Typical usage: 01733 /// \code 01734 /// main(int argc,char* argv[]) { 01735 /// StringMap<llvm::cl::Option*> opts; 01736 /// llvm::cl::getRegisteredOptions(opts); 01737 /// assert(opts.count("help") == 1) 01738 /// opts["help"]->setDescription("Show alphabetical help information") 01739 /// // More code 01740 /// llvm::cl::ParseCommandLineOptions(argc,argv); 01741 /// //More code 01742 /// } 01743 /// \endcode 01744 /// 01745 /// This interface is useful for modifying options in libraries that are out of 01746 /// the control of the client. The options should be modified before calling 01747 /// llvm::cl::ParseCommandLineOptions(). 01748 void getRegisteredOptions(StringMap<Option*> &Map); 01749 01750 } // End namespace cl 01751 01752 } // End namespace llvm 01753 01754 #endif