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