llvm.org GIT mirror llvm / 33943f8
Running clang-format on CommandLine.h and CommandLine.cpp. No functional changes, I'm just going to be doing a lot of work in these files and it would be helpful if they had more current LLVM style. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225817 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 5 years ago
2 changed file(s) with 740 addition(s) and 698 deletion(s). Raw diff Collapse all Expand all
3939 //===----------------------------------------------------------------------===//
4040 // ParseCommandLineOptions - Command line option processing entry point.
4141 //
42 void ParseCommandLineOptions(int argc, const char * const *argv,
42 void ParseCommandLineOptions(int argc, const char *const *argv,
4343 const char *Overview = nullptr);
4444
4545 //===----------------------------------------------------------------------===//
6565 /// information specific to the tool.
6666 void AddExtraVersionPrinter(void (*func)());
6767
68
6968 // PrintOptionValues - Print option values.
7069 // With -print-options print the difference between option values and defaults.
7170 // With -print-all-options print all option values.
7978 // Flags permitted to be passed to command line arguments
8079 //
8180
82 enum NumOccurrencesFlag { // Flags for the number of occurrences allowed
83 Optional = 0x00, // Zero or One occurrence
84 ZeroOrMore = 0x01, // Zero or more occurrences allowed
85 Required = 0x02, // One occurrence required
86 OneOrMore = 0x03, // One or more occurrences required
81 enum NumOccurrencesFlag { // Flags for the number of occurrences allowed
82 Optional = 0x00, // Zero or One occurrence
83 ZeroOrMore = 0x01, // Zero or more occurrences allowed
84 Required = 0x02, // One occurrence required
85 OneOrMore = 0x03, // One or more occurrences required
8786
8887 // ConsumeAfter - Indicates that this option is fed anything that follows the
8988 // last positional argument required by the application (it is an error if
9291 // found. Once a filename is found, all of the succeeding arguments are
9392 // passed, unprocessed, to the ConsumeAfter option.
9493 //
95 ConsumeAfter = 0x04
96 };
97
98 enum ValueExpected { // Is a value required for the option?
94 ConsumeAfter = 0x04
95 };
96
97 enum ValueExpected { // Is a value required for the option?
9998 // zero reserved for the unspecified value
100 ValueOptional = 0x01, // The value can appear... or not
101 ValueRequired = 0x02, // The value is required to appear!
102 ValueDisallowed = 0x03 // A value may not be specified (for flags)
103 };
104
105 enum OptionHidden { // Control whether -help shows this option
106 NotHidden = 0x00, // Option included in -help & -help-hidden
107 Hidden = 0x01, // -help doesn't, but -help-hidden does
108 ReallyHidden = 0x02 // Neither -help nor -help-hidden show this arg
99 ValueOptional = 0x01, // The value can appear... or not
100 ValueRequired = 0x02, // The value is required to appear!
101 ValueDisallowed = 0x03 // A value may not be specified (for flags)
102 };
103
104 enum OptionHidden { // Control whether -help shows this option
105 NotHidden = 0x00, // Option included in -help & -help-hidden
106 Hidden = 0x01, // -help doesn't, but -help-hidden does
107 ReallyHidden = 0x02 // Neither -help nor -help-hidden show this arg
109108 };
110109
111110 // Formatting flags - This controls special features that the option might have
124123 //
125124
126125 enum FormattingFlags {
127 NormalFormatting = 0x00, // Nothing special
128 Positional = 0x01, // Is a positional argument, no '-' required
129 Prefix = 0x02, // Can this option directly prefix its value?
130 Grouping = 0x03 // Can this option group with other options?
131 };
132
133 enum MiscFlags { // Miscellaneous flags to adjust argument
134 CommaSeparated = 0x01, // Should this cl::list split between commas?
135 PositionalEatsArgs = 0x02, // Should this positional cl::list eat -args?
136 Sink = 0x04 // Should this cl::list eat all unknown options?
126 NormalFormatting = 0x00, // Nothing special
127 Positional = 0x01, // Is a positional argument, no '-' required
128 Prefix = 0x02, // Can this option directly prefix its value?
129 Grouping = 0x03 // Can this option group with other options?
130 };
131
132 enum MiscFlags { // Miscellaneous flags to adjust argument
133 CommaSeparated = 0x01, // Should this cl::list split between commas?
134 PositionalEatsArgs = 0x02, // Should this positional cl::list eat -args?
135 Sink = 0x04 // Should this cl::list eat all unknown options?
137136 };
138137
139138 //===----------------------------------------------------------------------===//
144143 const char *const Name;
145144 const char *const Description;
146145 void registerCategory();
147 public:
148 OptionCategory(const char *const Name, const char *const Description = nullptr)
149 : Name(Name), Description(Description) { registerCategory(); }
146
147 public:
148 OptionCategory(const char *const Name,
149 const char *const Description = nullptr)
150 : Name(Name), Description(Description) {
151 registerCategory();
152 }
150153 const char *getName() const { return Name; }
151154 const char *getDescription() const { return Description; }
152155 };
175178 // Out of line virtual function to provide home for the class.
176179 virtual void anchor();
177180
178 int NumOccurrences; // The number of times specified
181 int NumOccurrences; // The number of times specified
179182 // Occurrences, HiddenFlag, and Formatting are all enum types but to avoid
180183 // problems with signed enums in bitfields.
181184 unsigned Occurrences : 3; // enum NumOccurrencesFlag
185188 unsigned HiddenFlag : 2; // enum OptionHidden
186189 unsigned Formatting : 2; // enum FormattingFlags
187190 unsigned Misc : 3;
188 unsigned Position; // Position of last occurrence of the option
189 unsigned AdditionalVals;// Greater than 0 for multi-valued option.
190 Option *NextRegistered; // Singly linked list of registered options.
191 unsigned Position; // Position of last occurrence of the option
192 unsigned AdditionalVals; // Greater than 0 for multi-valued option.
193 Option *NextRegistered; // Singly linked list of registered options.
191194
192195 public:
193196 const char *ArgStr; // The argument string itself (ex: "help", "o")
199202 return (enum NumOccurrencesFlag)Occurrences;
200203 }
201204 inline enum ValueExpected getValueExpectedFlag() const {
202 return Value ? ((enum ValueExpected)Value)
203 : getValueExpectedFlagDefault();
205 return Value ? ((enum ValueExpected)Value) : getValueExpectedFlagDefault();
204206 }
205207 inline enum OptionHidden getOptionHiddenFlag() const {
206208 return (enum OptionHidden)HiddenFlag;
208210 inline enum FormattingFlags getFormattingFlag() const {
209211 return (enum FormattingFlags)Formatting;
210212 }
211 inline unsigned getMiscFlags() const {
212 return Misc;
213 }
213 inline unsigned getMiscFlags() const { return Misc; }
214214 inline unsigned getPosition() const { return Position; }
215215 inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
216216
223223 void setArgStr(const char *S) { ArgStr = S; }
224224 void setDescription(const char *S) { HelpStr = S; }
225225 void setValueStr(const char *S) { ValueStr = S; }
226 void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) {
227 Occurrences = Val;
228 }
226 void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { Occurrences = Val; }
229227 void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; }
230228 void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; }
231229 void setFormattingFlag(enum FormattingFlags V) { Formatting = V; }
232230 void setMiscFlag(enum MiscFlags M) { Misc |= M; }
233231 void setPosition(unsigned pos) { Position = pos; }
234232 void setCategory(OptionCategory &C) { Category = &C; }
233
235234 protected:
236235 explicit Option(enum NumOccurrencesFlag OccurrencesFlag,
237236 enum OptionHidden Hidden)
238 : NumOccurrences(0), Occurrences(OccurrencesFlag), Value(0),
239 HiddenFlag(Hidden), Formatting(NormalFormatting), Misc(0),
240 Position(0), AdditionalVals(0), NextRegistered(nullptr),
241 ArgStr(""), HelpStr(""), ValueStr(""), Category(&GeneralCategory) {
242 }
237 : NumOccurrences(0), Occurrences(OccurrencesFlag), Value(0),
238 HiddenFlag(Hidden), Formatting(NormalFormatting), Misc(0), Position(0),
239 AdditionalVals(0), NextRegistered(nullptr), ArgStr(""), HelpStr(""),
240 ValueStr(""), Category(&GeneralCategory) {}
243241
244242 inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; }
243
245244 public:
246245 // addArgument - Register this argument with the commandline system.
247246 //
265264
266265 virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0;
267266
268 virtual void getExtraOptionNames(SmallVectorImpl*> &) {}
267 virtual void getExtraOptionNames(SmallVectorImpl *> &) {}
269268
270269 // addOccurrence - Wrapper around handleOccurrence that enforces Flags.
271270 //
272 virtual bool addOccurrence(unsigned pos, StringRef ArgName,
273 StringRef Value, bool MultiArg = false);
271 virtual bool addOccurrence(unsigned pos, StringRef ArgName, StringRef Value,
272 bool MultiArg = false);
274273
275274 // Prints option name followed by message. Always returns true.
276275 bool error(const Twine &Message, StringRef ArgName = StringRef());
279278 inline int getNumOccurrences() const { return NumOccurrences; }
280279 virtual ~Option() {}
281280 };
282
283281
284282 //===----------------------------------------------------------------------===//
285283 // Command line option modifiers that can be used to modify the behavior of
305303 // the default constructor for the argument type does not give you what you
306304 // want. This is only valid on "opt" arguments, not on "list" arguments.
307305 //
308 template
309 struct initializer {
306 template struct initializer {
310307 const Ty &Init;
311308 initializer(const Ty &Val) : Init(Val) {}
312309
313 template
314 void apply(Opt &O) const { O.setInitialValue(Init); }
315 };
316
317 template
318 initializer init(const Ty &Val) {
310 template void apply(Opt &O) const { O.setInitialValue(Init); }
311 };
312
313 template initializer init(const Ty &Val) {
319314 return initializer(Val);
320315 }
321
322316
323317 // location - Allow the user to specify which external variable they want to
324318 // store the results of the command line argument processing into, if they don't
325319 // want to store it in the option itself.
326320 //
327 template
328 struct LocationClass {
321 template struct LocationClass {
329322 Ty &Loc;
330323 LocationClass(Ty &L) : Loc(L) {}
331324
332 template
333 void apply(Opt &O) const { O.setLocation(O, Loc); }
334 };
335
336 template
337 LocationClass location(Ty &L) { return LocationClass(L); }
325 template void apply(Opt &O) const { O.setLocation(O, Loc); }
326 };
327
328 template LocationClass location(Ty &L) {
329 return LocationClass(L);
330 }
338331
339332 // cat - Specifiy the Option category for the command line argument to belong
340333 // to.
342335 OptionCategory &Category;
343336 cat(OptionCategory &c) : Category(c) {}
344337
345 template
346 void apply(Opt &O) const { O.setCategory(Category); }
347 };
348
338 template void apply(Opt &O) const { O.setCategory(Category); }
339 };
349340
350341 //===----------------------------------------------------------------------===//
351342 // OptionValue class
359350 virtual void anchor();
360351 };
361352
362 template struct OptionValue;
353 template struct OptionValue;
363354
364355 // The default value safely does nothing. Option value printing is only
365356 // best-effort.
366 template
357 template
367358 struct OptionValueBase : public GenericOptionValue {
368359 // Temporary storage for argument passing.
369360 typedef OptionValue WrapperType;
373364 const DataType &getValue() const { llvm_unreachable("no default value"); }
374365
375366 // Some options may take their value from a different data type.
376 template
377 void setValue(const DT& /*V*/) {}
378
379 bool compare(const DataType &/*V*/) const { return false; }
380
381 bool compare(const GenericOptionValue& /*V*/) const override {
367 template void setValue(const DT & /*V*/) {}
368
369 bool compare(const DataType & /*V*/) const { return false; }
370
371 bool compare(const GenericOptionValue & /*V*/) const override {
382372 return false;
383373 }
384374 };
385375
386376 // Simple copy of the option value.
387 template
388 class OptionValueCopy : public GenericOptionValue {
377 template class OptionValueCopy : public GenericOptionValue {
389378 DataType Value;
390379 bool Valid;
380
391381 public:
392382 OptionValueCopy() : Valid(false) {}
393383
398388 return Value;
399389 }
400390
401 void setValue(const DataType &V) { Valid = true; Value = V; }
402
403 bool compare(const DataType &V) const {
404 return Valid && (Value != V);
405 }
391 void setValue(const DataType &V) {
392 Valid = true;
393 Value = V;
394 }
395
396 bool compare(const DataType &V) const { return Valid && (Value != V); }
406397
407398 bool compare(const GenericOptionValue &V) const override {
408399 const OptionValueCopy &VC =
409 static_cast< const OptionValueCopy& >(V);
410 if (!VC.hasValue()) return false;
400 static_cast &>(V);
401 if (!VC.hasValue())
402 return false;
411403 return compare(VC.getValue());
412404 }
413405 };
414406
415407 // Non-class option values.
416 template
408 template
417409 struct OptionValueBase : OptionValueCopy {
418410 typedef DataType WrapperType;
419411 };
420412
421413 // Top-level option class.
422 template
414 template
423415 struct OptionValue : OptionValueBase::value> {
424416 OptionValue() {}
425417
426 OptionValue(const DataType& V) {
427 this->setValue(V);
428 }
418 OptionValue(const DataType &V) { this->setValue(V); }
429419 // Some options may take their value from a different data type.
430 template
431 OptionValue &operator=(const DT& V) {
420 template OptionValue &operator=(const DT &V) {
432421 this->setValue(V);
433422 return *this;
434423 }
436425
437426 // Other safe-to-copy-by-value common option types.
438427 enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
439 template<>
428 template <>
440429 struct OptionValue : OptionValueCopy {
441430 typedef cl::boolOrDefault WrapperType;
442431
443432 OptionValue() {}
444433
445 OptionValue(const cl::boolOrDefault& V) {
446 this->setValue(V);
447 }
448 OptionValue &operator=(const cl::boolOrDefault& V) {
434 OptionValue(const cl::boolOrDefault &V) { this->setValue(V); }
435 OptionValue &operator=(const cl::boolOrDefault &V) {
449436 setValue(V);
450437 return *this;
451438 }
439
452440 private:
453441 void anchor() override;
454442 };
455443
456 template<>
457 struct OptionValue : OptionValueCopy {
444 template <> struct OptionValue : OptionValueCopy {
458445 typedef StringRef WrapperType;
459446
460447 OptionValue() {}
461448
462 OptionValue(const std::string& V) {
463 this->setValue(V);
464 }
465 OptionValue &operator=(const std::string& V) {
449 OptionValue(const std::string &V) { this->setValue(V); }
450 OptionValue &operator=(const std::string &V) {
466451 setValue(V);
467452 return *this;
468453 }
454
469455 private:
470456 void anchor() override;
471457 };
475461 //
476462 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
477463 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
478 #define clEnumValEnd (reinterpret_cast*>(0))
464 #define clEnumValEnd (reinterpret_cast *>(0))
479465
480466 // values - For custom data types, allow specifying a group of values together
481467 // as the values that go into the mapping that the option handler uses. Note
482468 // that the values list must always have a 0 at the end of the list to indicate
483469 // that the list has ended.
484470 //
485 template
486 class ValuesClass {
471 template class ValuesClass {
487472 // Use a vector instead of a map, because the lists should be short,
488473 // the overhead is less, and most importantly, it keeps them in the order
489474 // inserted so we can print our option out nicely.
490 SmallVector >,4> Values;
475 SmallVector>, 4> Values;
491476 void processValues(va_list Vals);
477
492478 public:
493479 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
494480 va_list ValueArgs) {
499485 while (const char *enumName = va_arg(ValueArgs, const char *)) {
500486 DataType EnumVal = static_cast(va_arg(ValueArgs, int));
501487 const char *EnumDesc = va_arg(ValueArgs, const char *);
502 Values.push_back(std::make_pair(enumName, // Add value to value map
488 Values.push_back(std::make_pair(enumName, // Add value to value map
503489 std::make_pair(EnumVal, EnumDesc)));
504490 }
505491 }
506492
507 template
508 void apply(Opt &O) const {
493 template void apply(Opt &O) const {
509494 for (size_t i = 0, e = Values.size(); i != e; ++i)
510495 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
511496 Values[i].second.second);
515500 template
516501 ValuesClass LLVM_END_WITH_NULL
517502 values(const char *Arg, DataType Val, const char *Desc, ...) {
518 va_list ValueArgs;
519 va_start(ValueArgs, Desc);
520 ValuesClass Vals(Arg, Val, Desc, ValueArgs);
521 va_end(ValueArgs);
522 return Vals;
503 va_list ValueArgs;
504 va_start(ValueArgs, Desc);
505 ValuesClass Vals(Arg, Val, Desc, ValueArgs);
506 va_end(ValueArgs);
507 return Vals;
523508 }
524509
525510 //===----------------------------------------------------------------------===//
538523 protected:
539524 class GenericOptionInfo {
540525 public:
541 GenericOptionInfo(const char *name, const char *helpStr) :
542 Name(name), HelpStr(helpStr) {}
526 GenericOptionInfo(const char *name, const char *helpStr)
527 : Name(name), HelpStr(helpStr) {}
543528 const char *Name;
544529 const char *HelpStr;
545530 };
546 public:
547 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
531
532 public:
533 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
548534
549535 // getNumOptions - Virtual function implemented by generic subclass to
550536 // indicate how many entries are in Values.
575561 //
576562 // Template definition ensures that the option and default have the same
577563 // DataType (via the same AnyOptionValue).
578 template
564 template
579565 void printOptionDiff(const Option &O, const AnyOptionValue &V,
580566 const AnyOptionValue &Default,
581567 size_t GlobalWidth) const {
589575 hasArgStr = O.hasArgStr();
590576 }
591577
592 void getExtraOptionNames(SmallVectorImpl*> &OptionNames) {
578 void getExtraOptionNames(SmallVectorImpl *> &OptionNames) {
593579 // If there has been no argstr specified, that means that we need to add an
594580 // argument for every possible option. This ensures that our options are
595581 // vectored to us.
597583 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
598584 OptionNames.push_back(getOption(i));
599585 }
600
601586
602587 enum ValueExpected getValueExpectedFlagDefault() const {
603588 // If there is an ArgStr specified, then we are of the form:
632617 // command line option for -help. Because this is a simple mapping parser, the
633618 // data type can be any unsupported type.
634619 //
635 template
636 class parser : public generic_parser_base {
620 template class parser : public generic_parser_base {
637621 protected:
638622 class OptionInfo : public GenericOptionInfo {
639623 public:
640 OptionInfo(const char *name, DataType v, const char *helpStr) :
641 GenericOptionInfo(name, helpStr), V(v) {}
624 OptionInfo(const char *name, DataType v, const char *helpStr)
625 : GenericOptionInfo(name, helpStr), V(v) {}
642626 OptionValue V;
643627 };
644628 SmallVector Values;
629
645630 public:
646631 typedef DataType parser_data_type;
647632
689674 void removeLiteralOption(const char *Name) {
690675 unsigned N = findOption(Name);
691676 assert(N != Values.size() && "Option not found!");
692 Values.erase(Values.begin()+N);
677 Values.erase(Values.begin() + N);
693678 }
694679 };
695680
696681 //--------------------------------------------------
697682 // basic_parser - Super class of parsers to provide boilerplate code
698683 //
699 class basic_parser_impl { // non-template implementation of basic_parser
684 class basic_parser_impl { // non-template implementation of basic_parser
700685 public:
701686 virtual ~basic_parser_impl() {}
702687
704689 return ValueRequired;
705690 }
706691
707 void getExtraOptionNames(SmallVectorImpl*> &) {}
692 void getExtraOptionNames(SmallVectorImpl *> &) {}
708693
709694 void initialize(Option &) {}
710695
734719 // basic_parser - The real basic parser is just a template wrapper that provides
735720 // a typedef for the provided data type.
736721 //
737 template
738 class basic_parser : public basic_parser_impl {
722 template class basic_parser : public basic_parser_impl {
739723 public:
740724 typedef DataType parser_data_type;
741725 typedef OptionValue OptVal;
744728 //--------------------------------------------------
745729 // parser
746730 //
747 template<>
748 class parser : public basic_parser {
731 template <> class parser : public basic_parser {
749732 const char *ArgStr;
750 public:
751
733
734 public:
752735 // parse - Return true on error.
753736 bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val);
754737
755 template
756 void initialize(Opt &O) {
757 ArgStr = O.ArgStr;
758 }
738 template void initialize(Opt &O) { ArgStr = O.ArgStr; }
759739
760740 enum ValueExpected getValueExpectedFlagDefault() const {
761741 return ValueOptional;
775755
776756 //--------------------------------------------------
777757 // parser
778 template<>
779 class parser : public basic_parser {
758 template <> class parser : public basic_parser {
780759 public:
781760 // parse - Return true on error.
782761 bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val);
800779 //--------------------------------------------------
801780 // parser
802781 //
803 template<>
804 class parser : public basic_parser {
782 template <> class parser : public basic_parser {
805783 public:
806784 // parse - Return true on error.
807785 bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
818796
819797 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser);
820798
821
822799 //--------------------------------------------------
823800 // parser
824801 //
825 template<>
826 class parser : public basic_parser {
802 template <> class parser : public basic_parser {
827803 public:
828804 // parse - Return true on error.
829805 bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
843819 //--------------------------------------------------
844820 // parser
845821 //
846 template<>
822 template <>
847823 class parser : public basic_parser {
848824 public:
849825 // parse - Return true on error.
865841 //--------------------------------------------------
866842 // parser
867843 //
868 template<>
869 class parser : public basic_parser {
844 template <> class parser : public basic_parser {
870845 public:
871846 // parse - Return true on error.
872847 bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
886861 //--------------------------------------------------
887862 // parser
888863 //
889 template<>
890 class parser : public basic_parser {
864 template <> class parser : public basic_parser {
891865 public:
892866 // parse - Return true on error.
893867 bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
907881 //--------------------------------------------------
908882 // parser
909883 //
910 template<>
911 class parser : public basic_parser {
884 template <> class parser : public basic_parser {
912885 public:
913886 // parse - Return true on error.
914887 bool parse(Option &, StringRef, StringRef Arg, std::string &Value) {
931904 //--------------------------------------------------
932905 // parser
933906 //
934 template<>
935 class parser : public basic_parser {
907 template <> class parser : public basic_parser {
936908 public:
937909 // parse - Return true on error.
938910 bool parse(Option &, StringRef, StringRef Arg, char &Value) {
959931 // parser to handle all the template nastiness.
960932
961933 // This overloaded function is selected by the generic parser.
962 template
934 template
963935 void printOptionDiff(const Option &O, const generic_parser_base &P, const DT &V,
964936 const OptionValue
&Default, size_t GlobalWidth) {
965937 OptionValue
OV = V;
968940
969941 // This is instantiated for basic parsers when the parsed value has a different
970942 // type than the option value. e.g. HelpPrinter.
971 template
972 struct OptionDiffPrinter {
973 void print(const Option &O, const parser P, const ValDT &/*V*/,
974 const OptionValue &/*Default*/, size_t GlobalWidth) {
943 template struct OptionDiffPrinter {
944 void print(const Option &O, const parser P, const ValDT & /*V*/,
945 const OptionValue & /*Default*/, size_t GlobalWidth) {
975946 P.printOptionNoValue(O, GlobalWidth);
976947 }
977948 };
978949
979950 // This is instantiated for basic parsers when the parsed value has the same
980951 // type as the option value.
981 template
982 struct OptionDiffPrinter {
952 template struct OptionDiffPrinter {
983953 void print(const Option &O, const parser
P, const DT &V,
984954 const OptionValue
&Default, size_t GlobalWidth) {
985955 P.printOptionDiff(O, V, Default, GlobalWidth);
988958
989959 // This overloaded function is selected by the basic parser, which may parse a
990960 // different type than the option type.
991 template
961 template
992962 void printOptionDiff(
993 const Option &O,
994 const basic_parser &P,
995 const ValDT &V, const OptionValue &Default,
996 size_t GlobalWidth) {
963 const Option &O,
964 const basic_parser &P,
965 const ValDT &V, const OptionValue &Default, size_t GlobalWidth) {
997966
998967 OptionDiffPrinter printer;
999 printer.print(O, static_cast&>(P), V, Default,
968 printer.print(O, static_cast &>(P), V, Default,
1000969 GlobalWidth);
1001970 }
1002971
1006975 // not correctly respond to the apply method). Because the syntax to use this
1007976 // is a pain, we have the 'apply' method below to handle the nastiness...
1008977 //
1009 template struct applicator {
1010 template
1011 static void opt(const Mod &M, Opt &O) { M.apply(O); }
978 template struct applicator {
979 template static void opt(const Mod &M, Opt &O) { M.apply(O); }
1012980 };
1013981
1014982 // Handle const char* as a special case...
1015 template struct applicator {
1016 template
1017 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
1018 };
1019 template struct applicator {
1020 template
1021 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
1022 };
1023 template<> struct applicator {
1024 template
1025 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
1026 };
1027
1028 template<> struct applicator> {
983 template struct applicator> {
984 template static void opt(const char *Str, Opt &O) {
985 O.setArgStr(Str);
986 }
987 };
988 template struct applicator {
989 template static void opt(const char *Str, Opt &O) {
990 O.setArgStr(Str);
991 }
992 };
993 template <> struct applicator {
994 template static void opt(const char *Str, Opt &O) {
995 O.setArgStr(Str);
996 }
997 };
998
999 template <> struct applicator {
10291000 static void opt(NumOccurrencesFlag N, Option &O) {
10301001 O.setNumOccurrencesFlag(N);
10311002 }
10321003 };
1033 template<> struct applicator {
1004 template <> struct applicator {
10341005 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
10351006 };
1036 template<> struct applicator {
1007 template <> struct applicator {
10371008 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
10381009 };
1039 template<> struct applicator {
1010 template <> struct applicator {
10401011 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
10411012 };
1042 template<> struct applicator {
1013 template <> struct applicator {
10431014 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
10441015 };
10451016
10461017 // apply method - Apply a modifier to an option in a type safe way.
1047 template
1048 void apply(const Mod &M, Opt *O) {
1018 template void apply(const Mod &M, Opt *O) {
10491019 applicator::opt(M, *O);
10501020 }
10511021
10561026 // assumes the user will specify a variable to store the data into with the
10571027 // cl::location(x) modifier.
10581028 //
1059 template
1029 template
10601030 class opt_storage {
1061 DataType *Location; // Where to store the object...
1031 DataType *Location; // Where to store the object...
10621032 OptionValue Default;
10631033
10641034 void check_location() const {
10651035 assert(Location && "cl::location(...) not specified for a command "
1066 "line option with external storage, "
1067 "or cl::init specified before cl::location()!!");
1068 }
1036 "line option with external storage, "
1037 "or cl::init specified before cl::location()!!");
1038 }
1039
10691040 public:
10701041 opt_storage() : Location(nullptr) {}
10711042
10771048 return false;
10781049 }
10791050
1080 template
1081 void setValue(const T &V, bool initial = false) {
1051 template void setValue(const T &V, bool initial = false) {
10821052 check_location();
10831053 *Location = V;
10841054 if (initial)
10851055 Default = V;
10861056 }
10871057
1088 DataType &getValue() { check_location(); return *Location; }
1089 const DataType &getValue() const { check_location(); return *Location; }
1058 DataType &getValue() {
1059 check_location();
1060 return *Location;
1061 }
1062 const DataType &getValue() const {
1063 check_location();
1064 return *Location;
1065 }
10901066
10911067 operator DataType() const { return this->getValue(); }
10921068
10971073 // inherit from a class, we do so. This makes us exactly compatible with the
10981074 // object in all cases that it is used.
10991075 //
1100 template
1101 class opt_storage : public DataType {
1076 template
1077 class opt_storage : public DataType {
11021078 public:
11031079 OptionValue Default;
11041080
1105 template
1106 void setValue(const T &V, bool initial = false) {
1081 template void setValue(const T &V, bool initial = false) {
11071082 DataType::operator=(V);
11081083 if (initial)
11091084 Default = V;
11191094 // this case, we store an instance through containment, and overload operators
11201095 // to get at the value.
11211096 //
1122 template
1123 class opt_storage {
1097 template class opt_storage {
11241098 public:
11251099 DataType Value;
11261100 OptionValue Default;
11291103 // type.
11301104 opt_storage() : Value(DataType()), Default(DataType()) {}
11311105
1132 template
1133 void setValue(const T &V, bool initial = false) {
1106 template void setValue(const T &V, bool initial = false) {
11341107 Value = V;
11351108 if (initial)
11361109 Default = V;
11461119 DataType operator->() const { return Value; }
11471120 };
11481121
1149
11501122 //===----------------------------------------------------------------------===//
11511123 // opt - A scalar command line option.
11521124 //
11531125 template
1154 class ParserClass = parser >
1126 class ParserClass = parser>
11551127 class opt : public Option,
11561128 public opt_storage
11571129 std::is_class::value> {
11601132 bool handleOccurrence(unsigned pos, StringRef ArgName,
11611133 StringRef Arg) override {
11621134 typename ParserClass::parser_data_type Val =
1163 typename ParserClass::parser_data_type();
1135 typename ParserClass::parser_data_type();
11641136 if (Parser.parse(*this, ArgName, Arg, Val))
1165 return true; // Parse error!
1137 return true; // Parse error!
11661138 this->setValue(Val);
11671139 this->setPosition(pos);
11681140 return false;
11711143 enum ValueExpected getValueExpectedFlagDefault() const override {
11721144 return Parser.getValueExpectedFlagDefault();
11731145 }
1174 void getExtraOptionNames(SmallVectorImpl &OptionNames) override {
1146 void
1147 getExtraOptionNames(SmallVectorImpl &OptionNames) override {
11751148 return Parser.getExtraOptionNames(OptionNames);
11761149 }
11771150
11781151 // Forward printing stuff to the parser...
1179 size_t getOptionWidth() const override {return Parser.getOptionWidth(*this);}
1152 size_t getOptionWidth() const override {
1153 return Parser.getOptionWidth(*this);
1154 }
11801155 void printOptionInfo(size_t GlobalWidth) const override {
11811156 Parser.printOptionInfo(*this, GlobalWidth);
11821157 }
11831158
11841159 void printOptionValue(size_t GlobalWidth, bool Force) const override {
11851160 if (Force || this->getDefault().compare(this->getValue())) {
1186 cl::printOptionDiff(
1187 *this, Parser, this->getValue(), this->getDefault(), GlobalWidth);
1161 cl::printOptionDiff(*this, Parser, this->getValue(),
1162 this->getDefault(), GlobalWidth);
11881163 }
11891164 }
11901165
11921167 addArgument();
11931168 Parser.initialize(*this);
11941169 }
1170
11951171 public:
11961172 // setInitialValue - Used by the cl::init modifier...
11971173 void setInitialValue(const DataType &V) { this->setValue(V, true); }
11981174
11991175 ParserClass &getParser() { return Parser; }
12001176
1201 template
1202 DataType &operator=(const T &Val) {
1177 template DataType &operator=(const T &Val) {
12031178 this->setValue(Val);
12041179 return this->getValue();
12051180 }
12061181
12071182 // One option...
1208 template
1209 explicit opt(const M0t &M0) : Option(Optional, NotHidden) {
1183 template
1184 explicit opt(const M0t &M0)
1185 : Option(Optional, NotHidden) {
12101186 apply(M0, this);
12111187 done();
12121188 }
12131189
12141190 // Two options...
1215 template
1216 opt(const M0t &M0, const M1t &M1) : Option(Optional, NotHidden) {
1217 apply(M0, this); apply(M1, this);
1191 template
1192 opt(const M0t &M0, const M1t &M1)
1193 : Option(Optional, NotHidden) {
1194 apply(M0, this);
1195 apply(M1, this);
12181196 done();
12191197 }
12201198
12211199 // Three options...
1222 template
1223 opt(const M0t &M0, const M1t &M1,
1224 const M2t &M2) : Option(Optional, NotHidden) {
1225 apply(M0, this); apply(M1, this); apply(M2, this);
1200 template
1201 opt(const M0t &M0, const M1t &M1, const M2t &M2)
1202 : Option(Optional, NotHidden) {
1203 apply(M0, this);
1204 apply(M1, this);
1205 apply(M2, this);
12261206 done();
12271207 }
12281208 // Four options...
1229 template
1230 opt(const M0t &M0, const M1t &M1, const M2t &M2,
1231 const M3t &M3) : Option(Optional, NotHidden) {
1232 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1209 template
1210 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1211 : Option(Optional, NotHidden) {
1212 apply(M0, this);
1213 apply(M1, this);
1214 apply(M2, this);
1215 apply(M3, this);
12331216 done();
12341217 }
12351218 // Five options...
1236 template
1237 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1238 const M4t &M4) : Option(Optional, NotHidden) {
1239 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1219 template
1220 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, const M4t &M4)
1221 : Option(Optional, NotHidden) {
1222 apply(M0, this);
1223 apply(M1, this);
1224 apply(M2, this);
1225 apply(M3, this);
12401226 apply(M4, this);
12411227 done();
12421228 }
12431229 // Six options...
1244 template
1245 class M4t, class M5t>
1246 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1247 const M4t &M4, const M5t &M5) : Option(Optional, NotHidden) {
1248 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1249 apply(M4, this); apply(M5, this);
1230 template
1231 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, const M4t &M4,
1232 const M5t &M5)
1233 : Option(Optional, NotHidden) {
1234 apply(M0, this);
1235 apply(M1, this);
1236 apply(M2, this);
1237 apply(M3, this);
1238 apply(M4, this);
1239 apply(M5, this);
12501240 done();
12511241 }
12521242 // Seven options...
1253 template
1254 class M4t, class M5t, class M6t>
1255 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1256 const M4t &M4, const M5t &M5,
1257 const M6t &M6) : Option(Optional, NotHidden) {
1258 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1259 apply(M4, this); apply(M5, this); apply(M6, this);
1243 template
1244 class M6t>
1245 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, const M4t &M4,
1246 const M5t &M5, const M6t &M6)
1247 : Option(Optional, NotHidden) {
1248 apply(M0, this);
1249 apply(M1, this);
1250 apply(M2, this);
1251 apply(M3, this);
1252 apply(M4, this);
1253 apply(M5, this);
1254 apply(M6, this);
12601255 done();
12611256 }
12621257 // Eight options...
1263 template
1264 class M4t, class M5t, class M6t, class M7t>
1265 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1266 const M4t &M4, const M5t &M5, const M6t &M6,
1267 const M7t &M7) : Option(Optional, NotHidden) {
1268 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1269 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1258 template
1259 class M6t, class M7t>
1260 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, const M4t &M4,
1261 const M5t &M5, const M6t &M6, const M7t &M7)
1262 : Option(Optional, NotHidden) {
1263 apply(M0, this);
1264 apply(M1, this);
1265 apply(M2, this);
1266 apply(M3, this);
1267 apply(M4, this);
1268 apply(M5, this);
1269 apply(M6, this);
1270 apply(M7, this);
12701271 done();
12711272 }
12721273 };
12841285 // assumes the user will specify a variable to store the data into with the
12851286 // cl::location(x) modifier.
12861287 //
1287 template
1288 class list_storage {
1289 StorageClass *Location; // Where to store the object...
1288 template class list_storage {
1289 StorageClass *Location; // Where to store the object...
12901290
12911291 public:
12921292 list_storage() : Location(0) {}
12981298 return false;
12991299 }
13001300
1301 template
1302 void addValue(const T &V) {
1301 template void addValue(const T &V) {
13031302 assert(Location != 0 && "cl::location(...) not specified for a command "
1304 "line option with external storage!");
1303 "line option with external storage!");
13051304 Location->push_back(V);
13061305 }
13071306 };
1308
13091307
13101308 // Define how to hold a class type object, such as a string. Since we can
13111309 // inherit from a class, we do so. This makes us exactly compatible with the
13121310 // object in all cases that it is used.
13131311 //
1314 template
1312 template
13151313 class list_storage : public std::vector {
13161314 public:
1317 template
1318 void addValue(const T &V) { std::vector::push_back(V); }
1319 };
1320
1315 template void addValue(const T &V) {
1316 std::vector::push_back(V);
1317 }
1318 };
13211319
13221320 //===----------------------------------------------------------------------===//
13231321 // list - A list of command line options.
13241322 //
13251323 template
1326 class ParserClass = parser >
1324 class ParserClass = parser>
13271325 class list : public Option, public list_storage {
13281326 std::vector Positions;
13291327 ParserClass Parser;
13311329 enum ValueExpected getValueExpectedFlagDefault() const override {
13321330 return Parser.getValueExpectedFlagDefault();
13331331 }
1334 void getExtraOptionNames(SmallVectorImpl &OptionNames) override {
1332 void
1333 getExtraOptionNames(SmallVectorImpl &OptionNames) override {
13351334 return Parser.getExtraOptionNames(OptionNames);
13361335 }
13371336
13381337 bool handleOccurrence(unsigned pos, StringRef ArgName,
13391338 StringRef Arg) override {
13401339 typename ParserClass::parser_data_type Val =
1341 typename ParserClass::parser_data_type();
1340 typename ParserClass::parser_data_type();
13421341 if (Parser.parse(*this, ArgName, Arg, Val))
1343 return true; // Parse Error!
1342 return true; // Parse Error!
13441343 list_storage::addValue(Val);
13451344 setPosition(pos);
13461345 Positions.push_back(pos);
13481347 }
13491348
13501349 // Forward printing stuff to the parser...
1351 size_t getOptionWidth() const override {return Parser.getOptionWidth(*this);}
1350 size_t getOptionWidth() const override {
1351 return Parser.getOptionWidth(*this);
1352 }
13521353 void printOptionInfo(size_t GlobalWidth) const override {
13531354 Parser.printOptionInfo(*this, GlobalWidth);
13541355 }
13551356
13561357 // Unimplemented: list options don't currently store their default value.
1357 void printOptionValue(size_t /*GlobalWidth*/,
1358 bool /*Force*/) const override {}
1358 void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
1359 }
13591360
13601361 void done() {
13611362 addArgument();
13621363 Parser.initialize(*this);
13631364 }
1365
13641366 public:
13651367 ParserClass &getParser() { return Parser; }
13661368
13691371 return Positions[optnum];
13701372 }
13711373
1372 void setNumAdditionalVals(unsigned n) {
1373 Option::setNumAdditionalVals(n);
1374 }
1374 void setNumAdditionalVals(unsigned n) { Option::setNumAdditionalVals(n); }
13751375
13761376 // One option...
1377 template
1378 explicit list(const M0t &M0) : Option(ZeroOrMore, NotHidden) {
1377 template
1378 explicit list(const M0t &M0)
1379 : Option(ZeroOrMore, NotHidden) {
13791380 apply(M0, this);
13801381 done();
13811382 }
13821383 // Two options...
1383 template
1384 list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore, NotHidden) {
1385 apply(M0, this); apply(M1, this);
1384 template
1385 list(const M0t &M0, const M1t &M1)
1386 : Option(ZeroOrMore, NotHidden) {
1387 apply(M0, this);
1388 apply(M1, this);
13861389 done();
13871390 }
13881391 // Three options...
1389 template
1392 template
13901393 list(const M0t &M0, const M1t &M1, const M2t &M2)
1391 : Option(ZeroOrMore, NotHidden) {
1392 apply(M0, this); apply(M1, this); apply(M2, this);
1394 : Option(ZeroOrMore, NotHidden) {
1395 apply(M0, this);
1396 apply(M1, this);
1397 apply(M2, this);
13931398 done();
13941399 }
13951400 // Four options...
1396 template
1401 template
13971402 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1398 : Option(ZeroOrMore, NotHidden) {
1399 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1403 : Option(ZeroOrMore, NotHidden) {
1404 apply(M0, this);
1405 apply(M1, this);
1406 apply(M2, this);
1407 apply(M3, this);
14001408 done();
14011409 }
14021410 // Five options...
1403 template
1411 template
14041412 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1405 const M4t &M4) : Option(ZeroOrMore, NotHidden) {
1406 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1413 const M4t &M4)
1414 : Option(ZeroOrMore, NotHidden) {
1415 apply(M0, this);
1416 apply(M1, this);
1417 apply(M2, this);
1418 apply(M3, this);
14071419 apply(M4, this);
14081420 done();
14091421 }
14101422 // Six options...
1411 template
1412 class M4t, class M5t>
1423 template class M4t, class M5t>
14131424 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1414 const M4t &M4, const M5t &M5) : Option(ZeroOrMore, NotHidden) {
1415 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1416 apply(M4, this); apply(M5, this);
1425 const M4t &M4, const M5t &M5)
1426 : Option(ZeroOrMore, NotHidden) {
1427 apply(M0, this);
1428 apply(M1, this);
1429 apply(M2, this);
1430 apply(M3, this);
1431 apply(M4, this);
1432 apply(M5, this);
14171433 done();
14181434 }
14191435 // Seven options...
1420 template
1421 class M4t, class M5t, class M6t>
1436 template
1437 class M6t>
14221438 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
14231439 const M4t &M4, const M5t &M5, const M6t &M6)
1424 : Option(ZeroOrMore, NotHidden) {
1425 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1426 apply(M4, this); apply(M5, this); apply(M6, this);
1440 : Option(ZeroOrMore, NotHidden) {
1441 apply(M0, this);
1442 apply(M1, this);
1443 apply(M2, this);
1444 apply(M3, this);
1445 apply(M4, this);
1446 apply(M5, this);
1447 apply(M6, this);
14271448 done();
14281449 }
14291450 // Eight options...
1430 template
1431 class M4t, class M5t, class M6t, class M7t>
1451 template
1452 class M6t, class M7t>
14321453 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1433 const M4t &M4, const M5t &M5, const M6t &M6,
1434 const M7t &M7) : Option(ZeroOrMore, NotHidden) {
1435 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1436 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1454 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7)
1455 : Option(ZeroOrMore, NotHidden) {
1456 apply(M0, this);
1457 apply(M1, this);
1458 apply(M2, this);
1459 apply(M3, this);
1460 apply(M4, this);
1461 apply(M5, this);
1462 apply(M6, this);
1463 apply(M7, this);
14371464 done();
14381465 }
14391466 };
14441471 explicit multi_val(unsigned N) : AdditionalVals(N) {}
14451472
14461473 template
1447 void apply(list &L) const { L.setNumAdditionalVals(AdditionalVals); }
1448 };
1449
1474 void apply(list &L) const {
1475 L.setNumAdditionalVals(AdditionalVals);
1476 }
1477 };
14501478
14511479 //===----------------------------------------------------------------------===//
14521480 // bits_storage class
14551483 // assumes the user will specify a variable to store the data into with the
14561484 // cl::location(x) modifier.
14571485 //
1458 template
1459 class bits_storage {
1460 unsigned *Location; // Where to store the bits...
1461
1462 template
1463 static unsigned Bit(const T &V) {
1486 template class bits_storage {
1487 unsigned *Location; // Where to store the bits...
1488
1489 template static unsigned Bit(const T &V) {
14641490 unsigned BitPos = reinterpret_cast(V);
14651491 assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1466 "enum exceeds width of bit vector!");
1492 "enum exceeds width of bit vector!");
14671493 return 1 << BitPos;
14681494 }
14691495
14771503 return false;
14781504 }
14791505
1480 template
1481 void addValue(const T &V) {
1506 template void addValue(const T &V) {
14821507 assert(Location != 0 && "cl::location(...) not specified for a command "
1483 "line option with external storage!");
1508 "line option with external storage!");
14841509 *Location |= Bit(V);
14851510 }
14861511
14871512 unsigned getBits() { return *Location; }
14881513
1489 template
1490 bool isSet(const T &V) {
1514 template bool isSet(const T &V) {
14911515 return (*Location & Bit(V)) != 0;
14921516 }
14931517 };
1494
14951518
14961519 // Define how to hold bits. Since we can inherit from a class, we do so.
14971520 // This makes us exactly compatible with the bits in all cases that it is used.
14981521 //
1499 template
1500 class bits_storage {
1501 unsigned Bits; // Where to store the bits...
1502
1503 template
1504 static unsigned Bit(const T &V) {
1522 template class bits_storage {
1523 unsigned Bits; // Where to store the bits...
1524
1525 template static unsigned Bit(const T &V) {
15051526 unsigned BitPos = (unsigned)V;
15061527 assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1507 "enum exceeds width of bit vector!");
1528 "enum exceeds width of bit vector!");
15081529 return 1 << BitPos;
15091530 }
15101531
15111532 public:
1512 template
1513 void addValue(const T &V) {
1514 Bits |= Bit(V);
1515 }
1533 template void addValue(const T &V) { Bits |= Bit(V); }
15161534
15171535 unsigned getBits() { return Bits; }
15181536
1519 template
1520 bool isSet(const T &V) {
1521 return (Bits & Bit(V)) != 0;
1522 }
1523 };
1524
1537 template bool isSet(const T &V) { return (Bits & Bit(V)) != 0; }
1538 };
15251539
15261540 //===----------------------------------------------------------------------===//
15271541 // bits - A bit vector of command options.
15281542 //
15291543 template
1530 class ParserClass = parser >
1544 class ParserClass = parser>
15311545 class bits : public Option, public bits_storage {
15321546 std::vector Positions;
15331547 ParserClass Parser;
15351549 enum ValueExpected getValueExpectedFlagDefault() const override {
15361550 return Parser.getValueExpectedFlagDefault();
15371551 }
1538 void getExtraOptionNames(SmallVectorImpl &OptionNames) override {
1552 void
1553 getExtraOptionNames(SmallVectorImpl &OptionNames) override {
15391554 return Parser.getExtraOptionNames(OptionNames);
15401555 }
15411556
15421557 bool handleOccurrence(unsigned pos, StringRef ArgName,
15431558 StringRef Arg) override {
15441559 typename ParserClass::parser_data_type Val =
1545 typename ParserClass::parser_data_type();
1560 typename ParserClass::parser_data_type();
15461561 if (Parser.parse(*this, ArgName, Arg, Val))
1547 return true; // Parse Error!
1562 return true; // Parse Error!
15481563 this->addValue(Val);
15491564 setPosition(pos);
15501565 Positions.push_back(pos);
15521567 }
15531568
15541569 // Forward printing stuff to the parser...
1555 size_t getOptionWidth() const override {return Parser.getOptionWidth(*this);}
1570 size_t getOptionWidth() const override {
1571 return Parser.getOptionWidth(*this);
1572 }
15561573 void printOptionInfo(size_t GlobalWidth) const override {
15571574 Parser.printOptionInfo(*this, GlobalWidth);
15581575 }
15591576
15601577 // Unimplemented: bits options don't currently store their default values.
1561 void printOptionValue(size_t /*GlobalWidth*/,
1562 bool /*Force*/) const override {}
1578 void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
1579 }
15631580
15641581 void done() {
15651582 addArgument();
15661583 Parser.initialize(*this);
15671584 }
1585
15681586 public:
15691587 ParserClass &getParser() { return Parser; }
15701588
15741592 }
15751593
15761594 // One option...
1577 template
1578 explicit bits(const M0t &M0) : Option(ZeroOrMore, NotHidden) {
1595 template
1596 explicit bits(const M0t &M0)
1597 : Option(ZeroOrMore, NotHidden) {
15791598 apply(M0, this);
15801599 done();
15811600 }
15821601 // Two options...
1583 template
1584 bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore, NotHidden) {
1585 apply(M0, this); apply(M1, this);
1602 template
1603 bits(const M0t &M0, const M1t &M1)
1604 : Option(ZeroOrMore, NotHidden) {
1605 apply(M0, this);
1606 apply(M1, this);
15861607 done();
15871608 }
15881609 // Three options...
1589 template
1610 template
15901611 bits(const M0t &M0, const M1t &M1, const M2t &M2)
1591 : Option(ZeroOrMore, NotHidden) {
1592 apply(M0, this); apply(M1, this); apply(M2, this);
1612 : Option(ZeroOrMore, NotHidden) {
1613 apply(M0, this);
1614 apply(M1, this);
1615 apply(M2, this);
15931616 done();
15941617 }
15951618 // Four options...
1596 template
1619 template
15971620 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1598 : Option(ZeroOrMore, NotHidden) {
1599 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1621 : Option(ZeroOrMore, NotHidden) {
1622 apply(M0, this);
1623 apply(M1, this);
1624 apply(M2, this);
1625 apply(M3, this);
16001626 done();
16011627 }
16021628 // Five options...
1603 template
1629 template
16041630 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1605 const M4t &M4) : Option(ZeroOrMore, NotHidden) {
1606 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1631 const M4t &M4)
1632 : Option(ZeroOrMore, NotHidden) {
1633 apply(M0, this);
1634 apply(M1, this);
1635 apply(M2, this);
1636 apply(M3, this);
16071637 apply(M4, this);
16081638 done();
16091639 }
16101640 // Six options...
1611 template
1612 class M4t, class M5t>
1641 template class M4t, class M5t>
16131642 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1614 const M4t &M4, const M5t &M5) : Option(ZeroOrMore, NotHidden) {
1615 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1616 apply(M4, this); apply(M5, this);
1643 const M4t &M4, const M5t &M5)
1644 : Option(ZeroOrMore, NotHidden) {
1645 apply(M0, this);
1646 apply(M1, this);
1647 apply(M2, this);
1648 apply(M3, this);
1649 apply(M4, this);
1650 apply(M5, this);
16171651 done();
16181652 }
16191653 // Seven options...
1620 template
1621 class M4t, class M5t, class M6t>
1654 template
1655 class M6t>
16221656 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
16231657 const M4t &M4, const M5t &M5, const M6t &M6)
1624 : Option(ZeroOrMore, NotHidden) {
1625 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1626 apply(M4, this); apply(M5, this); apply(M6, this);
1658 : Option(ZeroOrMore, NotHidden) {
1659 apply(M0, this);
1660 apply(M1, this);
1661 apply(M2, this);
1662 apply(M3, this);
1663 apply(M4, this);
1664 apply(M5, this);
1665 apply(M6, this);
16271666 done();
16281667 }
16291668 // Eight options...
1630 template
1631 class M4t, class M5t, class M6t, class M7t>
1669 template
1670 class M6t, class M7t>
16321671 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1633 const M4t &M4, const M5t &M5, const M6t &M6,
1634 const M7t &M7) : Option(ZeroOrMore, NotHidden) {
1635 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1636 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1672 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7)
1673 : Option(ZeroOrMore, NotHidden) {
1674 apply(M0, this);
1675 apply(M1, this);
1676 apply(M2, this);
1677 apply(M3, this);
1678 apply(M4, this);
1679 apply(M5, this);
1680 apply(M6, this);
1681 apply(M7, this);
16371682 done();
16381683 }
16391684 };
16451690 class alias : public Option {
16461691 Option *AliasFor;
16471692 bool handleOccurrence(unsigned pos, StringRef /*ArgName*/,
1648 StringRef Arg) override {
1693 StringRef Arg) override {
16491694 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
16501695 }
1651 bool addOccurrence(unsigned pos, StringRef /*ArgName*/,
1652 StringRef Value, bool MultiArg = false) override {
1696 bool addOccurrence(unsigned pos, StringRef /*ArgName*/, StringRef Value,
1697 bool MultiArg = false) override {
16531698 return AliasFor->addOccurrence(pos, AliasFor->ArgStr, Value, MultiArg);
16541699 }
16551700 // Handle printing stuff...
16571702 void printOptionInfo(size_t GlobalWidth) const override;
16581703
16591704 // Aliases do not need to print their values.
1660 void printOptionValue(size_t /*GlobalWidth*/,
1661 bool /*Force*/) const override {}
1705 void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
1706 }
16621707
16631708 ValueExpected getValueExpectedFlagDefault() const override {
16641709 return AliasFor->getValueExpectedFlag();
16691714 error("cl::alias must have argument name specified!");
16701715 if (!AliasFor)
16711716 error("cl::alias must have an cl::aliasopt(option) specified!");
1672 addArgument();
1673 }
1717 addArgument();
1718 }
1719
16741720 public:
16751721 void setAliasFor(Option &O) {
16761722 if (AliasFor)
16791725 }
16801726
16811727 // One option...
1682 template
1683 explicit alias(const M0t &M0) : Option(Optional, Hidden), AliasFor(nullptr) {
1728 template
1729 explicit alias(const M0t &M0)
1730 : Option(Optional, Hidden), AliasFor(nullptr) {
16841731 apply(M0, this);
16851732 done();
16861733 }
16871734 // Two options...
1688 template
1735 template
16891736 alias(const M0t &M0, const M1t &M1)
1690 : Option(Optional, Hidden), AliasFor(nullptr) {
1691 apply(M0, this); apply(M1, this);
1737 : Option(Optional, Hidden), AliasFor(nullptr) {
1738 apply(M0, this);
1739 apply(M1, this);
16921740 done();
16931741 }
16941742 // Three options...
1695 template
1743 template
16961744 alias(const M0t &M0, const M1t &M1, const M2t &M2)
1697 : Option(Optional, Hidden), AliasFor(nullptr) {
1698 apply(M0, this); apply(M1, this); apply(M2, this);
1745 : Option(Optional, Hidden), AliasFor(nullptr) {
1746 apply(M0, this);
1747 apply(M1, this);
1748 apply(M2, this);
16991749 done();
17001750 }
17011751 // Four options...
1702 template
1752 template
17031753 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1704 : Option(Optional, Hidden), AliasFor(nullptr) {
1705 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1754 : Option(Optional, Hidden), AliasFor(nullptr) {
1755 apply(M0, this);
1756 apply(M1, this);
1757 apply(M2, this);
1758 apply(M3, this);
17061759 done();
17071760 }
17081761 };
17191772 // printed to stderr at the end of the regular help, just before
17201773 // exit is called.
17211774 struct extrahelp {
1722 const char * morehelp;
1723 explicit extrahelp(const char* help);
1775 const char *morehelp;
1776 explicit extrahelp(const char *help);
17241777 };
17251778
17261779 void PrintVersionMessage();
17321785 ///
17331786 /// \param Hidden if true will print hidden options
17341787 /// \param Categorized if true print options in categories
1735 void PrintHelpMessage(bool Hidden=false, bool Categorized=false);
1736
1788 void PrintHelpMessage(bool Hidden = false, bool Categorized = false);
17371789
17381790 //===----------------------------------------------------------------------===//
17391791 // Public interface for accessing registered options.
17651817 /// This interface is useful for modifying options in libraries that are out of
17661818 /// the control of the client. The options should be modified before calling
17671819 /// llvm::cl::ParseCommandLineOptions().
1768 void getRegisteredOptions(StringMap*> &Map);
1820 void getRegisteredOptions(StringMap *> &Map);
17691821
17701822 //===----------------------------------------------------------------------===//
17711823 // Standalone command line processing utilities.
17751827 /// raw character pointer.
17761828 class StringSaver {
17771829 virtual void anchor();
1830
17781831 public:
17791832 virtual const char *SaveString(const char *Str) = 0;
1780 virtual ~StringSaver() {}; // Pacify -Wnon-virtual-dtor.
1833 virtual ~StringSaver(){}; // Pacify -Wnon-virtual-dtor.
17811834 };
17821835
17831836 /// \brief Tokenizes a command line that can contain escapes and quotes.
4343 //===----------------------------------------------------------------------===//
4444 // Template instantiations and anchors.
4545 //
46 namespace llvm { namespace cl {
46 namespace llvm {
47 namespace cl {
4748 TEMPLATE_INSTANTIATION(class basic_parser);
4849 TEMPLATE_INSTANTIATION(class basic_parser);
4950 TEMPLATE_INSTANTIATION(class basic_parser);
5960 TEMPLATE_INSTANTIATION(class opt);
6061 TEMPLATE_INSTANTIATION(class opt);
6162 TEMPLATE_INSTANTIATION(class opt);
62 } } // end namespace llvm::cl
63 }
64 } // end namespace llvm::cl
6365
6466 // Pin the vtables to this file.
6567 void GenericOptionValue::anchor() {}
8688 static const char *ProgramOverview = nullptr;
8789
8890 // This collects additional help to be printed.
89 static ManagedStatic > MoreHelp;
90
91 extrahelp::extrahelp(const char *Help)
92 : morehelp(Help) {
91 static ManagedStatic> MoreHelp;
92
93 extrahelp::extrahelp(const char *Help) : morehelp(Help) {
9394 MoreHelp->push_back(Help);
9495 }
9596
9697 static bool OptionListChanged = false;
9798
9899 // MarkOptionsChanged - Internal helper function.
99 void cl::MarkOptionsChanged() {
100 OptionListChanged = true;
101 }
100 void cl::MarkOptionsChanged() { OptionListChanged = true; }
102101
103102 /// RegisteredOptionList - This is the list of the command line options that
104103 /// have statically constructed themselves.
126125 }
127126
128127 // This collects the different option categories that have been registered.
129 typedef SmallPtrSet*,16> OptionCatSet;
128 typedef SmallPtrSet *, 16> OptionCatSet;
130129 static ManagedStatic RegisteredOptionCategories;
131130
132131 // Initialise the general option category.
137136 RegisteredOptionCategories->end(),
138137 [this](const OptionCategory *Category) {
139138 return getName() == Category->getName();
140 }) == 0 && "Duplicate option categories");
139 }) == 0 &&
140 "Duplicate option categories");
141141
142142 RegisteredOptionCategories->insert(this);
143143 }
148148
149149 /// GetOptionInfo - Scan the list of registered options, turning them into data
150150 /// structures that are easier to handle.
151 static void GetOptionInfo(SmallVectorImpl &PositionalOpts,
152 SmallVectorImpl &SinkOpts,
153 StringMap &OptionsMap) {
151 static void GetOptionInfo(SmallVectorImpl
152 SmallVectorImpl
153 StringMap
154154 bool HadErrors = false;
155 SmallVector OptionNames;
156 Option *CAOpt = nullptr; // The ConsumeAfter option if it exists.
155 SmallVector OptionNames;
156 Option *CAOpt = nullptr; // The ConsumeAfter option if it exists.
157157 for (Option *O = RegisteredOptionList; O; O = O->getNextRegisteredOption()) {
158158 // If this option wants to handle multiple option names, get the full set.
159159 // This handles enum options like "-O1 -O2" etc.
200200 report_fatal_error("inconsistency in registered CommandLine options");
201201 }
202202
203
204203 /// LookupOption - Lookup the option specified by the specified option on the
205204 /// command line. If there is a value specified (after an equal sign) return
206205 /// that as well. This assumes that leading dashes have already been stripped.
207206 static Option *LookupOption(StringRef &Arg, StringRef &Value,
208 const StringMap*> &OptionsMap) {
207 const StringMap *> &OptionsMap) {
209208 // Reject all dashes.
210 if (Arg.empty()) return nullptr;
209 if (Arg.empty())
210 return nullptr;
211211
212212 size_t EqualPos = Arg.find('=');
213213
214214 // If we have an equals sign, remember the value.
215215 if (EqualPos == StringRef::npos) {
216216 // Look up the option.
217 StringMap*>::const_iterator I = OptionsMap.find(Arg);
217 StringMap *>::const_iterator I = OptionsMap.find(Arg);
218218 return I != OptionsMap.end() ? I->second : nullptr;
219219 }
220220
221221 // If the argument before the = is a valid option name, we match. If not,
222222 // return Arg unmolested.
223 StringMap::const_iterator I =
224 OptionsMap.find(Arg.substr(0, EqualPos));
225 if (I == OptionsMap.end()) return nullptr;
226
227 Value = Arg.substr(EqualPos+1);
223 StringMap
224 OptionsMap.find(Arg.substr(0, EqualPos));
225 if (I == OptionsMap.end())
226 return nullptr;
227
228 Value = Arg.substr(EqualPos + 1);
228229 Arg = Arg.substr(0, EqualPos);
229230 return I->second;
230231 }
234235 /// (after an equal sign) return that as well. This assumes that leading dashes
235236 /// have already been stripped.
236237 static Option *LookupNearestOption(StringRef Arg,
237 const StringMap*> &OptionsMap,
238 const StringMap *> &OptionsMap,
238239 std::string &NearestString) {
239240 // Reject all dashes.
240 if (Arg.empty()) return nullptr;
241 if (Arg.empty())
242 return nullptr;
241243
242244 // Split on any equal sign.
243245 std::pair SplitArg = Arg.split('=');
244 StringRef &LHS = SplitArg.first; // LHS == Arg when no '=' is present.
246 StringRef &LHS = SplitArg.first; // LHS == Arg when no '=' is present.
245247 StringRef &RHS = SplitArg.second;
246248
247249 // Find the closest match.
248250 Option *Best = nullptr;
249251 unsigned BestDistance = 0;
250 for (StringMap::const_iterator it = OptionsMap.begin(),
251 ie = OptionsMap.end(); it != ie; ++it) {
252 for (StringMap
253 ie = OptionsMap.end();
254 it != ie; ++it) {
252255 Option *O = it->second;
253 SmallVector*, 16> OptionNames;
256 SmallVector *, 16> OptionNames;
254257 O->getExtraOptionNames(OptionNames);
255258 if (O->ArgStr[0])
256259 OptionNames.push_back(O->ArgStr);
260263 for (size_t i = 0, e = OptionNames.size(); i != e; ++i) {
261264 StringRef Name = OptionNames[i];
262265 unsigned Distance = StringRef(Name).edit_distance(
263 Flag, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance);
266 Flag, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance);
264267 if (!Best || Distance < BestDistance) {
265268 Best = O;
266269 BestDistance = Distance;
291294 if (Handler->addOccurrence(pos, ArgName, Val.substr(0, Pos), MultiArg))
292295 return true;
293296 // Erase the portion before the comma, AND the comma.
294 Val = Val.substr(Pos+1);
295 Value.substr(Pos+1); // Increment the original value pointer as well.
297 Val = Val.substr(Pos + 1);
298 Value.substr(Pos + 1); // Increment the original value pointer as well.
296299 // Check for another comma.
297300 Pos = Val.find(',');
298301 }
319322 switch (Handler->getValueExpectedFlag()) {
320323 case ValueRequired:
321324 if (!Value.data()) { // No value specified?
322 if (i+1 >= argc)
325 if (i + 1 >= argc)
323326 return Handler->error("requires a value!");
324327 // Steal the next argument, like for '-o filename'
325328 assert(argv && "null check");
332335 " with ValueDisallowed modifier!");
333336
334337 if (Value.data())
335 return Handler->error("does not allow a value! '" +
336 Twine(Value) + "' specified.");
338 return Handler->error("does not allow a value! '" + Twine(Value) +
339 "' specified.");
337340 break;
338341 case ValueOptional:
339342 break;
354357 }
355358
356359 while (NumAdditionalVals > 0) {
357 if (i+1 >= argc)
360 if (i + 1 >= argc)
358361 return Handler->error("not enough values!");
359362 assert(argv && "null check");
360363 Value = argv[++i];
371374 int Dummy = i;
372375 return ProvideOption(Handler, Handler->ArgStr, Arg, 0, nullptr, Dummy);
373376 }
374
375377
376378 // Option predicates...
377379 static inline bool isGrouping(const Option *O) {
388390 // otherwise return null.
389391 //
390392 static Option *getOptionPred(StringRef Name, size_t &Length,
391 bool (*Pred)(const Option*),
392 const StringMap &OptionsMap) {
393
394 StringMap::const_iterator OMI = OptionsMap.find(Name);
393 bool (*Pred)(const Option *),
394 const StringMap
395
396 StringMap
395397
396398 // Loop while we haven't found an option and Name still has at least two
397399 // characters in it (so that the next iteration will not be the empty
398400 // string.
399401 while (OMI == OptionsMap.end() && Name.size() > 1) {
400 Name = Name.substr(0, Name.size()-1); // Chop off the last character.
402 Name = Name.substr(0, Name.size() - 1); // Chop off the last character.
401403 OMI = OptionsMap.find(Name);
402404 }
403405
404406 if (OMI != OptionsMap.end() && Pred(OMI->second)) {
405407 Length = Name.size();
406 return OMI->second; // Found one!
407 }
408 return nullptr; // No option found!
408 return OMI->second; // Found one!
409 }
410 return nullptr; // No option found!
409411 }
410412
411413 /// HandlePrefixedOrGroupedOption - The specified argument string (which started
412414 /// with at least one '-') does not fully match an available option. Check to
413415 /// see if this is a prefix or grouped option. If so, split arg into output an
414416 /// Arg/Value pair and return the Option to parse it with.
415 static Option *HandlePrefixedOrGroupedOption(StringRef &Arg, StringRef &Value,
416 bool &ErrorParsing,
417 const StringMap &OptionsMap) {
418 if (Arg.size() == 1) return nullptr;
417 static Option *
418 HandlePrefixedOrGroupedOption(StringRef &Arg, StringRef &Value,
419 bool &ErrorParsing,
420 const StringMap
421 if (Arg.size() == 1)
422 return nullptr;
419423
420424 // Do the lookup!
421425 size_t Length = 0;
422426 Option *PGOpt = getOptionPred(Arg, Length, isPrefixedOrGrouping, OptionsMap);
423 if (!PGOpt) return nullptr;
427 if (!PGOpt)
428 return nullptr;
424429
425430 // If the option is a prefixed option, then the value is simply the
426431 // rest of the name... so fall through to later processing, by
446451 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
447452 "Option can not be cl::Grouping AND cl::ValueRequired!");
448453 int Dummy = 0;
449 ErrorParsing |= ProvideOption(PGOpt, OneArgName,
450 StringRef(), 0, nullptr, Dummy);
454 ErrorParsing |=
455 ProvideOption(PGOpt, OneArgName, StringRef(), 0, nullptr, Dummy);
451456
452457 // Get the next grouping option.
453458 PGOpt = getOptionPred(Arg, Length, isGrouping, OptionsMap);
457462 return PGOpt;
458463 }
459464
460
461
462465 static bool RequiresValue(const Option *O) {
463466 return O->getNumOccurrencesFlag() == cl::Required ||
464467 O->getNumOccurrencesFlag() == cl::OneOrMore;
469472 O->getNumOccurrencesFlag() == cl::OneOrMore;
470473 }
471474
472 static bool isWhitespace(char C) {
473 return strchr(" \t\n\r\f\v", C);
474 }
475
476 static bool isQuote(char C) {
477 return C == '\"' || C == '\'';
478 }
479
480 static bool isGNUSpecial(char C) {
481 return strchr("\\\"\' ", C);
482 }
475 static bool isWhitespace(char C) { return strchr(" \t\n\r\f\v", C); }
476
477 static bool isQuote(char C) { return C == '\"' || C == '\''; }
478
479 static bool isGNUSpecial(char C) { return strchr("\\\"\' ", C); }
483480
484481 void cl::TokenizeGNUCommandLine(StringRef Src, StringSaver &Saver,
485482 SmallVectorImpl &NewArgv,
494491 NewArgv.push_back(nullptr);
495492 ++I;
496493 }
497 if (I == E) break;
494 if (I == E)
495 break;
498496 }
499497
500498 // Backslashes can escape backslashes, spaces, and other quotes. Otherwise
501499 // they are literal. This makes it much easier to read Windows file paths.
502500 if (I + 1 < E && Src[I] == '\\' && isGNUSpecial(Src[I + 1])) {
503 ++I; // Skip the escape.
501 ++I; // Skip the escape.
504502 Token.push_back(Src[I]);
505503 continue;
506504 }
515513 Token.push_back(Src[I]);
516514 ++I;
517515 }
518 if (I == E) break;
516 if (I == E)
517 break;
519518 continue;
520519 }
521520
690689 bool AllExpanded = true;
691690
692691 // Don't cache Argv.size() because it can change.
693 for (unsigned I = 0; I != Argv.size(); ) {
692 for (unsigned I = 0; I != Argv.size();) {
694693 const char *Arg = Argv[I];
695694 // Check if it is an EOL marker
696695 if (Arg == nullptr) {
727726 }
728727
729728 namespace {
730 class StrDupSaver : public StringSaver {
731 std::vector Dups;
732 public:
733 ~StrDupSaver() {
734 for (std::vector::iterator I = Dups.begin(), E = Dups.end();
735 I != E; ++I) {
736 char *Dup = *I;
737 free(Dup);
738 }
739 }
740 const char *SaveString(const char *Str) override {
741 char *Dup = strdup(Str);
742 Dups.push_back(Dup);
743 return Dup;
744 }
745 };
729 class StrDupSaver : public StringSaver {
730 std::vector Dups;
731
732 public:
733 ~StrDupSaver() {
734 for (std::vector::iterator I = Dups.begin(), E = Dups.end(); I != E;
735 ++I) {
736 char *Dup = *I;
737 free(Dup);
738 }
739 }
740 const char *SaveString(const char *Str) override {
741 char *Dup = strdup(Str);
742 Dups.push_back(Dup);
743 return Dup;
744 }
745 };
746746 }
747747
748748 /// ParseEnvironmentOptions - An alternative entry point to the
774774 ParseCommandLineOptions(newArgc, &newArgv[0], Overview);
775775 }
776776
777 void cl::ParseCommandLineOptions(int argc, const char * const *argv,
777 void cl::ParseCommandLineOptions(int argc, const char *const *argv,
778778 const char *Overview) {
779779 // Process all registered options.
780 SmallVector PositionalOpts;
781 SmallVector SinkOpts;
782 StringMap Opts;
780 SmallVectorOpts;
781 SmallVector
782 StringMap
783783 GetOptionInfo(PositionalOpts, SinkOpts, Opts);
784784
785 assert((!Opts.empty() || !PositionalOpts.empty()) &&
786 "No options specified!");
785 assert((!Opts.empty() || !PositionalOpts.empty()) && "No options specified!");
787786
788787 // Expand response files.
789788 SmallVector newArgv;
819818
820819 // Calculate how many positional values are _required_.
821820 bool UnboundedFound = false;
822 for (size_t i = ConsumeAfterOpt ? 1 : 0, e = PositionalOpts.size();
823 i != e; ++i) {
821 for (size_t i = ConsumeAfterOpt ? 1 : 0, e = PositionalOpts.size(); i != e;
822 ++i) {
824823 Option *Opt = PositionalOpts[i];
825824 if (RequiresValue(Opt))
826825 ++NumPositionalRequired;
828827 // ConsumeAfter cannot be combined with "optional" positional options
829828 // unless there is only one positional argument...
830829 if (PositionalOpts.size() > 2)
831 ErrorParsing |=
832 Opt->error("error - this positional option will never be matched, "
833 "because it does not Require a value, and a "
834 "cl::ConsumeAfter option is active!");
830 ErrorParsing |= Opt->error(
831 "error - this positional option will never be matched, "
832 "because it does not Require a value, and a "
833 "cl::ConsumeAfter option is active!");
835834 } else if (UnboundedFound && !Opt->ArgStr[0]) {
836835 // This option does not "require" a value... Make sure this option is
837836 // not specified after an option that eats all extra arguments, or this
850849 // PositionalVals - A vector of "positional" arguments we accumulate into
851850 // the process at the end.
852851 //
853 SmallVectorunsigned>, 4> PositionalVals;
852 SmallVector unsigned>, 4> PositionalVals;
854853
855854 // If the program has named positional arguments, and the name has been run
856855 // across, keep track of which positional argument was named. Otherwise put
858857 Option *ActivePositionalArg = nullptr;
859858
860859 // Loop over all of the arguments... processing them.
861 bool DashDashFound = false; // Have we read '--'?
860 bool DashDashFound = false; // Have we read '--'?
862861 for (int i = 1; i < argc; ++i) {
863862 Option *Handler = nullptr;
864863 Option *NearestHandler = nullptr;
885884 // Positional argument!
886885 if (ActivePositionalArg) {
887886 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
888 continue; // We are done!
887 continue; // We are done!
889888 }
890889
891890 if (!PositionalOpts.empty()) {
892 PositionalVals.push_back(std::make_pair(argv[i],i));
891 PositionalVals.push_back(std::make_pair(argv[i], i));
893892
894893 // All of the positional arguments have been fulfulled, give the rest to
895894 // the consume after option... if it's specified...
896895 //
897896 if (PositionalVals.size() >= NumPositionalRequired && ConsumeAfterOpt) {
898897 for (++i; i < argc; ++i)
899 PositionalVals.push_back(std::make_pair(argv[i],i));
900 break; // Handle outside of the argument processing loop...
898 PositionalVals.push_back(std::make_pair(argv[i], i));
899 break; // Handle outside of the argument processing loop...
901900 }
902901
903902 // Delay processing positional arguments until the end...
905904 }
906905 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
907906 !DashDashFound) {
908 DashDashFound = true; // This is the mythical "--"?
909 continue; // Don't try to process it as an argument itself.
907 DashDashFound = true; // This is the mythical "--"?
908 continue; // Don't try to process it as an argument itself.
910909 } else if (ActivePositionalArg &&
911910 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
912911 // If there is a positional argument eating options, check to see if this
913912 // option is another positional argument. If so, treat it as an argument,
914913 // otherwise feed it to the eating positional.
915 ArgName = argv[i]+1;
914 ArgName = argv[i] + 1;
916915 // Eat leading dashes.
917916 while (!ArgName.empty() && ArgName[0] == '-')
918917 ArgName = ArgName.substr(1);
920919 Handler = LookupOption(ArgName, Value, Opts);
921920 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
922921 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
923 continue; // We are done!
924 }
925
926 } else { // We start with a '-', must be an argument.
927 ArgName = argv[i]+1;
922 continue; // We are done!
923 }
924
925 } else { // We start with a '-', must be an argument.
926 ArgName = argv[i] + 1;
928927 // Eat leading dashes.
929928 while (!ArgName.empty() && ArgName[0] == '-')
930929 ArgName = ArgName.substr(1);
933932
934933 // Check to see if this "option" is really a prefixed or grouped argument.
935934 if (!Handler)
936 Handler = HandlePrefixedOrGroupedOption(ArgName, Value,
937 ErrorParsing, Opts);
935 Handler =
936 HandlePrefixedOrGroupedOption(ArgName, Value, ErrorParsing, Opts);
938937
939938 // Otherwise, look for the closest available option to report to the user
940939 // in the upcoming error.
941940 if (!Handler && SinkOpts.empty())
942 NearestHandler = LookupNearestOption(ArgName, Opts,
943 NearestHandlerString);
941 NearestHandler =
942 LookupNearestOption(ArgName, Opts, NearestHandlerString);
944943 }
945944
946945 if (!Handler) {
947946 if (SinkOpts.empty()) {
948 errs() << ProgramName << ": Unknown command line argument '"
949 << argv[i] << "'. Try: '" << argv[0] << " -help'\n";
947 errs() << ProgramName << ": Unknown command line argument '" << argv[i]
948 << "'. Try: '" << argv[0] << " -help'\n";
950949
951950 if (NearestHandler) {
952951 // If we know a near match, report it as well.
953 errs() << ProgramName << ": Did you mean '-"
954 << NearestHandlerString << "'?\n";
952 errs() << ProgramName << ": Did you mean '-" << NearestHandlerString
953 << "'?\n";
955954 }
956955
957956 ErrorParsing = true;
958957 } else {
959 for (SmallVectorImpl::iterator I = SinkOpts.begin(),
960 E = SinkOpts.end(); I != E ; ++I)
958 for (SmallVectorImpl
959 E = SinkOpts.end();
960 I != E; ++I)
961961 (*I)->addOccurrence(i, "", argv[i]);
962962 }
963963 continue;
974974 // Check and handle positional arguments now...
975975 if (NumPositionalRequired > PositionalVals.size()) {
976976 errs() << ProgramName
977 << ": Not enough positional command line arguments specified!\n"
978 << "Must specify at least " << NumPositionalRequired
979 << " positional arguments: See: " << argv[0] << " -help\n";
977 << ": Not enough positional command line arguments specified!\n"
978 << "Must specify at least " << NumPositionalRequired
979 << " positional arguments: See: " << argv[0] << " -help\n";
980980
981981 ErrorParsing = true;
982982 } else if (!HasUnlimitedPositionals &&
983983 PositionalVals.size() > PositionalOpts.size()) {
984 errs() << ProgramName
985 << ": Too many positional arguments specified!\n"
986 << "Can specify at most " << PositionalOpts.size()
987 << " positional arguments: See: " << argv[0] << " -help\n";
984 errs() << ProgramName << ": Too many positional arguments specified!\n"
985 << "Can specify at most " << PositionalOpts.size()
986 << " positional arguments: See: " << argv[0] << " -help\n";
988987 ErrorParsing = true;
989988
990989 } else if (!ConsumeAfterOpt) {
995994 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo].first,
996995 PositionalVals[ValNo].second);
997996 ValNo++;
998 --NumPositionalRequired; // We fulfilled our duty...
997 --NumPositionalRequired; // We fulfilled our duty...
999998 }
1000999
10011000 // If we _can_ give this option more arguments, do so now, as long as we
10031002 // option even _WANTS_ any more.
10041003 //
10051004 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
1006 while (NumVals-ValNo > NumPositionalRequired && !Done) {
1005 while (NumVals - ValNo > NumPositionalRequired && !Done) {
10071006 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
10081007 case cl::Optional:
1009 Done = true; // Optional arguments want _at most_ one value
1010 // FALL THROUGH
1011 case cl::ZeroOrMore: // Zero or more will take all they can get...
1012 case cl::OneOrMore: // One or more will take all they can get...
1008 Done = true; // Optional arguments want _at most_ one value
1009 // FALL THROUGH
1010 case cl::ZeroOrMore: // Zero or more will take all they can get...
1011 case cl::OneOrMore: // One or more will take all they can get...
10131012 ProvidePositionalOption(PositionalOpts[i],
10141013 PositionalVals[ValNo].first,
10151014 PositionalVals[ValNo].second);
10171016 break;
10181017 default:
10191018 llvm_unreachable("Internal error, unexpected NumOccurrences flag in "
1020 "positional argument processing!");
1019 "positional argument processing!");
10211020 }
10221021 }
10231022 }
10471046 // Handle over all of the rest of the arguments to the
10481047 // cl::ConsumeAfter command line option...
10491048 for (; ValNo != PositionalVals.size(); ++ValNo)
1050 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
1051 PositionalVals[ValNo].first,
1052 PositionalVals[ValNo].second);
1049 ErrorParsing |=
1050 ProvidePositionalOption(ConsumeAfterOpt, PositionalVals[ValNo].first,
1051 PositionalVals[ValNo].second);
10531052 }
10541053
10551054 // Loop over args and make sure all required args are specified!
10611060 Opt.second->error("must be specified at least once!");
10621061 ErrorParsing = true;
10631062 }
1064 // Fall through
1063 // Fall through
10651064 default:
10661065 break;
10671066 }
10711070 // Note that if ReadResponseFiles == true, this must be done before the
10721071 // memory allocated for the expanded command line is free()d below.
10731072 DEBUG(dbgs() << "Args: ";
1074 for (int i = 0; i < argc; ++i)
1075 dbgs() << argv[i] << ' ';
1076 dbgs() << '\n';
1077 );
1073 for (int i = 0; i < argc; ++i) dbgs() << argv[i] << ' ';
1074 dbgs() << '\n';);
10781075
10791076 // Free all of the memory allocated to the map. Command line options may only
10801077 // be processed once!
10831080 MoreHelp->clear();
10841081
10851082 // If we had an error processing our arguments, don't let the program execute
1086 if (ErrorParsing) exit(1);
1083 if (ErrorParsing)
1084 exit(1);
10871085 }
10881086
10891087 //===----------------------------------------------------------------------===//
10911089 //
10921090
10931091 bool Option::error(const Twine &Message, StringRef ArgName) {
1094 if (!ArgName.data()) ArgName = ArgStr;
1092 if (!ArgName.data())
1093 ArgName = ArgStr;
10951094 if (ArgName.empty())
1096 errs() << HelpStr; // Be nice for positional arguments
1095 errs() << HelpStr; // Be nice for positional arguments
10971096 else
10981097 errs() << ProgramName << ": for the -" << ArgName;
10991098
11011100 return true;
11021101 }
11031102
1104 bool Option::addOccurrence(unsigned pos, StringRef ArgName,
1105 StringRef Value, bool MultiArg) {
1103 bool Option::addOccurrence(unsigned pos, StringRef ArgName, StringRef Value,
1104 bool MultiArg) {
11061105 if (!MultiArg)
1107 NumOccurrences++; // Increment the number of times we have been seen
1106 NumOccurrences++; // Increment the number of times we have been seen
11081107
11091108 switch (getNumOccurrencesFlag()) {
11101109 case Optional:
11141113 case Required:
11151114 if (NumOccurrences > 1)
11161115 return error("must occur exactly one time!", ArgName);
1117 // Fall through
1116 // Fall through
11181117 case OneOrMore:
11191118 case ZeroOrMore:
1120 case ConsumeAfter: break;
1119 case ConsumeAfter:
1120 break;
11211121 }
11221122
11231123 return handleOccurrence(pos, ArgName, Value);
11241124 }
1125
11261125
11271126 // getValueStr - Get the value description string, using "DefaultMsg" if nothing
11281127 // has been specified yet.
11291128 //
11301129 static const char *getValueStr(const Option &O, const char *DefaultMsg) {
1131 if (O.ValueStr[0] == 0) return DefaultMsg;
1130 if (O.ValueStr[0] == 0)
1131 return DefaultMsg;
11321132 return O.ValueStr;
11331133 }
11341134
11371137 //
11381138
11391139 // Return the width of the option tag for printing...
1140 size_t alias::getOptionWidth() const {
1141 return std::strlen(ArgStr)+6;
1142 }
1140 size_t alias::getOptionWidth() const { return std::strlen(ArgStr) + 6; }
11431141
11441142 static void printHelpStr(StringRef HelpStr, size_t Indent,
11451143 size_t FirstLineIndentedBy) {
11681166 size_t basic_parser_impl::getOptionWidth(const Option &O) const {
11691167 size_t Len = std::strlen(O.ArgStr);
11701168 if (const char *ValName = getValueName())
1171 Len += std::strlen(getValueStr(O, ValName))+3;
1169 Len += std::strlen(getValueStr(O, ValName)) + 3;
11721170
11731171 return Len + 6;
11741172 }
11891187 void basic_parser_impl::printOptionName(const Option &O,
11901188 size_t GlobalWidth) const {
11911189 outs() << " -" << O.ArgStr;
1192 outs().indent(GlobalWidth-std::strlen(O.ArgStr));
1193 }
1194
1190 outs().indent(GlobalWidth - std::strlen(O.ArgStr));
1191 }
11951192
11961193 // parser implementation
11971194 //
1198 bool parser::parse(Option &O, StringRef ArgName,
1199 StringRef Arg, bool &Value) {
1195 bool parser::parse(Option &O, StringRef ArgName, StringRef Arg,
1196 bool &Value) {
12001197 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
12011198 Arg == "1") {
12021199 Value = true;
12131210
12141211 // parser implementation
12151212 //
1216 bool parser::parse(Option &O, StringRef ArgName,
1217 StringRef Arg, boolOrDefault &Value) {
1213 bool parser::parse(Option &O, StringRef ArgName, StringRef Arg,
1214 boolOrDefault &Value) {
12181215 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
12191216 Arg == "1") {
12201217 Value = BOU_TRUE;
12311228
12321229 // parser implementation
12331230 //
1234 bool parser::parse(Option &O, StringRef ArgName,
1235 StringRef Arg, int &Value) {
1231 bool parser::parse(Option &O, StringRef ArgName, StringRef Arg,
1232 int &Value) {
12361233 if (Arg.getAsInteger(0, Value))
12371234 return O.error("'" + Arg + "' value invalid for integer argument!");
12381235 return false;
12401237
12411238 // parser implementation
12421239 //
1243 bool parser::parse(Option &O, StringRef ArgName,
1244 StringRef Arg, unsigned &Value) {
1240 bool parser::parse(Option &O, StringRef ArgName, StringRef Arg,
1241 unsigned &Value) {
12451242
12461243 if (Arg.getAsInteger(0, Value))
12471244 return O.error("'" + Arg + "' value invalid for uint argument!");
12511248 // parser implementation
12521249 //
12531250 bool parser::parse(Option &O, StringRef ArgName,
1254 StringRef Arg, unsigned long long &Value){
1251 StringRef Arg,
1252 unsigned long long &Value) {
12551253
12561254 if (Arg.getAsInteger(0, Value))
12571255 return O.error("'" + Arg + "' value invalid for uint argument!");
12701268 return false;
12711269 }
12721270
1273 bool parser::parse(Option &O, StringRef ArgName,
1274 StringRef Arg, double &Val) {
1271 bool parser::parse(Option &O, StringRef ArgName, StringRef Arg,
1272 double &Val) {
12751273 return parseDouble(O, Arg, Val);
12761274 }
12771275
1278 bool parser::parse(Option &O, StringRef ArgName,
1279 StringRef Arg, float &Val) {
1276 bool parser::parse(Option &O, StringRef ArgName, StringRef Arg,
1277 float &Val) {
12801278 double dVal;
12811279 if (parseDouble(O, Arg, dVal))
12821280 return true;
12841282 return false;
12851283 }
12861284
1287
1288
12891285 // generic_parser_base implementation
12901286 //
12911287
13021298 return e;
13031299 }
13041300
1305
13061301 // Return the width of the option tag for printing...
13071302 size_t generic_parser_base::getOptionWidth(const Option &O) const {
13081303 if (O.hasArgStr()) {
1309 size_t Size = std::strlen(O.ArgStr)+6;
1304 size_t Size = std::strlen(O.ArgStr) + 6;
13101305 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
1311 Size = std::max(Size, std::strlen(getOption(i))+8);
1306 Size = std::max(Size, std::strlen(getOption(i)) + 8);
13121307 return Size;
13131308 } else {
13141309 size_t BaseSize = 0;
13151310 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
1316 BaseSize = std::max(BaseSize, std::strlen(getOption(i))+8);
1311 BaseSize = std::max(BaseSize, std::strlen(getOption(i)) + 8);
13171312 return BaseSize;
13181313 }
13191314 }
13281323 printHelpStr(O.HelpStr, GlobalWidth, std::strlen(O.ArgStr) + 6);
13291324
13301325 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
1331 size_t NumSpaces = GlobalWidth-strlen(getOption(i))-8;
1326 size_t NumSpaces = GlobalWidth - strlen(getOption(i)) - 8;
13321327 outs() << " =" << getOption(i);
13331328 outs().indent(NumSpaces) << " - " << getDescription(i) << '\n';
13341329 }
13481343 // printGenericOptionDiff - Print the value of this option and it's default.
13491344 //
13501345 // "Generic" options have each value mapped to a name.
1351 void generic_parser_base::
1352 printGenericOptionDiff(const Option &O, const GenericOptionValue &Value,
1353 const GenericOptionValue &Default,
1354 size_t GlobalWidth) const {
1346 void generic_parser_base::printGenericOptionDiff(
1347 const Option &O, const GenericOptionValue &Value,
1348 const GenericOptionValue &Default, size_t GlobalWidth) const {
13551349 outs() << " -" << O.ArgStr;
1356 outs().indent(GlobalWidth-std::strlen(O.ArgStr));
1350 outs().indent(GlobalWidth - std::strlen(O.ArgStr));
13571351
13581352 unsigned NumOpts = getNumOptions();
13591353 for (unsigned i = 0; i != NumOpts; ++i) {
13781372
13791373 // printOptionDiff - Specializations for printing basic value types.
13801374 //
1381 #define PRINT_OPT_DIFF(T) \
1382 void parser:: \
1383 printOptionDiff(const Option &O, T V, OptionValue D, \
1384 size_t GlobalWidth) const { \
1385 printOptionName(O, GlobalWidth); \
1386 std::string Str; \
1387 { \
1388 raw_string_ostream SS(Str); \
1389 SS << V; \
1390 } \
1391 outs() << "= " << Str; \
1392 size_t NumSpaces = MaxOptWidth > Str.size() ? MaxOptWidth - Str.size() : 0;\
1393 outs().indent(NumSpaces) << " (default: "; \
1394 if (D.hasValue()) \
1395 outs() << D.getValue(); \
1396 else \
1397 outs() << "*no default*"; \
1398 outs() << ")\n"; \
1399 } \
1375 #define PRINT_OPT_DIFF(T) \
1376 void parser::printOptionDiff(const Option &O, T V, OptionValue D, \
1377 size_t GlobalWidth) const { \
1378 printOptionName(O, GlobalWidth); \
1379 std::string Str; \
1380 { \
1381 raw_string_ostream SS(Str); \
1382 SS << V; \
1383 } \
1384 outs() << "= " << Str; \
1385 size_t NumSpaces = \
1386 MaxOptWidth > Str.size() ? MaxOptWidth - Str.size() : 0; \
1387 outs().indent(NumSpaces) << " (default: "; \
1388 if (D.hasValue()) \
1389 outs() << D.getValue(); \
1390 else \
1391 outs() << "*no default*"; \
1392 outs() << ")\n"; \
1393 }
14001394
14011395 PRINT_OPT_DIFF(bool)
14021396 PRINT_OPT_DIFF(boolOrDefault)
14071401 PRINT_OPT_DIFF(float)
14081402 PRINT_OPT_DIFF(char)
14091403
1410 void parser::
1411 printOptionDiff(const Option &O, StringRef V, OptionValue D,
1412 size_t GlobalWidth) const {
1404 void parser::printOptionDiff(const Option &O, StringRef V,
1405 OptionValue D,
1406 size_t GlobalWidth) const {
14131407 printOptionName(O, GlobalWidth);
14141408 outs() << "= " << V;
14151409 size_t NumSpaces = MaxOptWidth > V.size() ? MaxOptWidth - V.size() : 0;
14221416 }
14231417
14241418 // Print a placeholder for options that don't yet support printOptionDiff().
1425 void basic_parser_impl::
1426 printOptionNoValue(const Option &O, size_t GlobalWidth) const {
1419 void basic_parser_impl::printOptionNoValue(const Option &O,
1420 size_t GlobalWidth) const {
14271421 printOptionName(O, GlobalWidth);
14281422 outs() << "= *cannot print option value*\n";
14291423 }
14331427 //
14341428
14351429 static int OptNameCompare(const void *LHS, const void *RHS) {
1436 typedef std::pair pair_ty;
1437
1438 return strcmp(((const pair_ty*)LHS)->first, ((const pair_ty*)RHS)->first);
1430 typedef std::pair pair_ty;
1431
1432 return strcmp(((const pair_ty *)LHS)->first, ((const pair_ty *)RHS)->first);
14391433 }
14401434
14411435 // Copy Options into a vector so we can sort them as we like.
1442 static void
1443 sortOpts(StringMap &OptMap,
1444 SmallVectorImpl< std::pair > &Opts,
1445 bool ShowHidden) {
1446 SmallPtrSet OptionSet; // Duplicate option detection.
1447
1448 for (StringMap::iterator I = OptMap.begin(), E = OptMap.end();
1436 static void sortOpts(StringMap
1437 SmallVectorImpl> &Opts,
1438 bool ShowHidden) {
1439 SmallPtrSet
1440
1441 for (StringMap
14491442 I != E; ++I) {
14501443 // Ignore really-hidden options.
14511444 if (I->second->getOptionHiddenFlag() == ReallyHidden)
14591452 if (!OptionSet.insert(I->second).second)
14601453 continue;
14611454
1462 Opts.push_back(std::pair(I->getKey().data(),
1463 I->second));
1455 Opts.push_back(
1456 std::pair(I->getKey().data(), I->second));
14641457 }
14651458
14661459 // Sort the options list alphabetically.
14721465 class HelpPrinter {
14731466 protected:
14741467 const bool ShowHidden;
1475 typedef SmallVector*>,128> StrOptionPairVector;
1468 typedef SmallVector *>, 128>
1469 StrOptionPairVector;
14761470 // Print the options. Opts is assumed to be alphabetically sorted.
14771471 virtual void printOptions(StrOptionPairVector &Opts, size_t MaxArgLen) {
14781472 for (size_t i = 0, e = Opts.size(); i != e; ++i)
14851479
14861480 // Invoke the printer.
14871481 void operator=(bool Value) {
1488 if (Value == false) return;
1482 if (Value == false)
1483 return;
14891484
14901485 // Get all the options.
1491 SmallVector PositionalOpts;
1492 SmallVector SinkOpts;
1493 StringMap OptMap;
1486 SmallVector;
1487 SmallVector
1488 StringMap
14941489 GetOptionInfo(PositionalOpts, SinkOpts, OptMap);
14951490
14961491 StrOptionPairVector Opts;
15021497 outs() << "USAGE: " << ProgramName << " [options]";
15031498
15041499 // Print out the positional options.
1505 Option *CAOpt = nullptr; // The cl::ConsumeAfter option, if it exists...
1500 Option *CAOpt = nullptr; // The cl::ConsumeAfter option, if it exists...
15061501 if (!PositionalOpts.empty() &&
15071502 PositionalOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
15081503 CAOpt = PositionalOpts[0];
15141509 }
15151510
15161511 // Print the consume after option info if it exists...
1517 if (CAOpt) outs() << " " << CAOpt->HelpStr;
1512 if (CAOpt)
1513 outs() << " " << CAOpt->HelpStr;
15181514
15191515 outs() << "\n\n";
15201516
15501546 }
15511547
15521548 // Make sure we inherit our base class's operator=()
1553 using HelpPrinter::operator= ;
1549 using HelpPrinter::operator=;
15541550
15551551 protected:
15561552 void printOptions(StrOptionPairVector &Opts, size_t MaxArgLen) override {
15571553 std::vector SortedCategories;
1558 std::map > CategorizedOptions;
1554 std::map> CategorizedOptions;
15591555
15601556 // Collect registered option categories into vector in preparation for
15611557 // sorting.
16321628
16331629 public:
16341630 explicit HelpPrinterWrapper(HelpPrinter &UncategorizedPrinter,
1635 CategorizedHelpPrinter &CategorizedPrinter) :
1636 UncategorizedPrinter(UncategorizedPrinter),
1637 CategorizedPrinter(CategorizedPrinter) { }
1631 CategorizedHelpPrinter &CategorizedPrinter)
1632 : UncategorizedPrinter(UncategorizedPrinter),
1633 CategorizedPrinter(CategorizedPrinter) {}
16381634
16391635 // Invoke the printer.
16401636 void operator=(bool Value);
16491645 static CategorizedHelpPrinter CategorizedNormalPrinter(false);
16501646 static CategorizedHelpPrinter CategorizedHiddenPrinter(true);
16511647
1652
16531648 // Declare HelpPrinter wrappers that will decide whether or not to invoke
16541649 // a categorizing help printer
16551650 static HelpPrinterWrapper WrappedNormalPrinter(UncategorizedNormalPrinter,
16601655 // Define uncategorized help printers.
16611656 // -help-list is hidden by default because if Option categories are being used
16621657 // then -help behaves the same as -help-list.
1663 static cl::opt >
1664 HLOp("help-list",
1665 cl::desc("Display list of available options (-help-list-hidden for more)"),
1666 cl::location(UncategorizedNormalPrinter), cl::Hidden, cl::ValueDisallowed);
1667
1668 static cl::opt >
1669 HLHOp("help-list-hidden",
1670 cl::desc("Display list of all available options"),
1671 cl::location(UncategorizedHiddenPrinter), cl::Hidden, cl::ValueDisallowed);
1658 static cl::opt> HLOp(
1659 "help-list",
1660 cl::desc("Display list of available options (-help-list-hidden for more)"),
1661 cl::location(UncategorizedNormalPrinter), cl::Hidden, cl::ValueDisallowed);
1662
1663 static cl::opt>
1664 HLHOp("help-list-hidden", cl::desc("Display list of all available options"),
1665 cl::location(UncategorizedHiddenPrinter), cl::Hidden,
1666 cl::ValueDisallowed);
16721667
16731668 // Define uncategorized/categorized help printers. These printers change their
16741669 // behaviour at runtime depending on whether one or more Option categories have
16751670 // been declared.
1676 static cl::opt >
1677 HOp("help", cl::desc("Display available options (-help-hidden for more)"),
1678 cl::location(WrappedNormalPrinter), cl::ValueDisallowed);
1679
1680 static cl::opt >
1681 HHOp("help-hidden", cl::desc("Display all available options"),
1682 cl::location(WrappedHiddenPrinter), cl::Hidden, cl::ValueDisallowed);
1683
1684
1685
1686 static cl::opt
1687 PrintOptions("print-options",
1688 cl::desc("Print non-default options after command line parsing"),
1689 cl::Hidden, cl::init(false));
1690
1691 static cl::opt
1692 PrintAllOptions("print-all-options",
1693 cl::desc("Print all option values after command line parsing"),
1694 cl::Hidden, cl::init(false));
1671 static cl::opt>
1672 HOp("help", cl::desc("Display available options (-help-hidden for more)"),
1673 cl::location(WrappedNormalPrinter), cl::ValueDisallowed);
1674
1675 static cl::opt>
1676 HHOp("help-hidden", cl::desc("Display all available options"),
1677 cl::location(WrappedHiddenPrinter), cl::Hidden, cl::ValueDisallowed);
1678
1679 static cl::opt PrintOptions(
1680 "print-options",
1681 cl::desc("Print non-default options after command line parsing"),
1682 cl::Hidden, cl::init(false));
1683
1684 static cl::opt PrintAllOptions(
1685 "print-all-options",
1686 cl::desc("Print all option values after command line parsing"), cl::Hidden,
1687 cl::init(false));
16951688
16961689 void HelpPrinterWrapper::operator=(bool Value) {
16971690 if (Value == false)
17061699 HLOp.setHiddenFlag(NotHidden);
17071700
17081701 CategorizedPrinter = true; // Invoke categorized printer
1709 }
1710 else
1702 } else
17111703 UncategorizedPrinter = true; // Invoke uncategorized printer
17121704 }
17131705
17141706 // Print the value of each option.
17151707 void cl::PrintOptionValues() {
1716 if (!PrintOptions && !PrintAllOptions) return;
1708 if (!PrintOptions && !PrintAllOptions)
1709 return;
17171710
17181711 // Get all the options.
1719 SmallVector PositionalOpts;
1720 SmallVector SinkOpts;
1721 StringMap OptMap;
1712 SmallVector;
1713 SmallVector
1714 StringMap
17221715 GetOptionInfo(PositionalOpts, SinkOpts, OptMap);
17231716
1724 SmallVector, 128> Opts;
1725 sortOpts(OptMap, Opts, /*ShowHidden*/true);
1717 SmallVector, 128> Opts;
1718 sortOpts(OptMap, Opts, /*ShowHidden*/ true);
17261719
17271720 // Compute the maximum argument length...
17281721 size_t MaxArgLen = 0;
17351728
17361729 static void (*OverrideVersionPrinter)() = nullptr;
17371730
1738 static std::vector* ExtraVersionPrinters = nullptr;
1731 static std::vector *ExtraVersionPrinters = nullptr;
17391732
17401733 namespace {
17411734 class VersionPrinter {
17571750 OS << " with assertions";
17581751 #endif
17591752 std::string CPU = sys::getHostCPUName();
1760 if (CPU == "generic") CPU = "(unknown)";
1753 if (CPU == "generic")
1754 CPU = "(unknown)";
17611755 OS << ".\n"
17621756 #if (ENABLE_TIMESTAMPS == 1)
17631757 << " Built " << __DATE__ << " (" << __TIME__ << ").\n"
17661760 << " Host CPU: " << CPU << '\n';
17671761 }
17681762 void operator=(bool OptionWasSpecified) {
1769 if (!OptionWasSpecified) return;
1763 if (!OptionWasSpecified)
1764 return;
17701765
17711766 if (OverrideVersionPrinter != nullptr) {
17721767 (*OverrideVersionPrinter)();
17891784 };
17901785 } // End anonymous namespace
17911786
1792
17931787 // Define the --version option that prints out the LLVM version for the tool
17941788 static VersionPrinter VersionPrinterInstance;
17951789
1796 static cl::opt >
1797 VersOp("version", cl::desc("Display the version of this program"),
1798 cl::location(VersionPrinterInstance), cl::ValueDisallowed);
1790 static cl::opt>
1791 VersOp("version", cl::desc("Display the version of this program"),
1792 cl::location(VersionPrinterInstance), cl::ValueDisallowed);
17991793
18001794 // Utility function for printing the help message.
18011795 void cl::PrintHelpMessage(bool Hidden, bool Categorized) {
18171811 }
18181812
18191813 /// Utility function for printing version number.
1820 void cl::PrintVersionMessage() {
1821 VersionPrinterInstance.print();
1822 }
1823
1824 void cl::SetVersionPrinter(void (*func)()) {
1825 OverrideVersionPrinter = func;
1826 }
1814 void cl::PrintVersionMessage() { VersionPrinterInstance.print(); }
1815
1816 void cl::SetVersionPrinter(void (*func)()) { OverrideVersionPrinter = func; }
18271817
18281818 void cl::AddExtraVersionPrinter(void (*func)()) {
18291819 if (!ExtraVersionPrinters)
18321822 ExtraVersionPrinters->push_back(func);
18331823 }
18341824
1835 void cl::getRegisteredOptions(StringMap &Map)
1836 {
1825 void cl::getRegisteredOptions(StringMap{
18371826 // Get all the options.
1838 SmallVector PositionalOpts; //NOT USED
1839 SmallVector*, 4> SinkOpts; //NOT USED
1827 SmallVector *, 4> PositionalOpts; // NOT USED
1828 SmallVector
18401829 assert(Map.size() == 0 && "StringMap must be empty");
18411830 GetOptionInfo(PositionalOpts, SinkOpts, Map);
18421831 return;