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