Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1686 lines
56 KiB

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