llvm.org GIT mirror llvm / 40a2186
Refactoring cl::parser construction and initialization. Summary: Some parsers need references back to the option they are members of. This is used for handling the argument string as well as by the various pass name parsers for making pass names into flags. Making parsers that need to refer back to the option have a reference to the option eliminates some of the members of various parsers, and enables further code cleanup. Reviewers: dexonsmith Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D7131 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226864 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 5 years ago
4 changed file(s) with 69 addition(s) and 53 deletion(s). Raw diff Collapse all Expand all
121121 class RegisterPassParser : public MachinePassRegistryListener,
122122 public cl::parser {
123123 public:
124 RegisterPassParser() {}
124 RegisterPassParser(cl::Option &O)
125 : cl::parser(O) {}
125126 ~RegisterPassParser() { RegistryClass::setListener(nullptr); }
126127
127 void initialize(cl::Option &O) {
128 cl::parser::initialize(O);
128 void initialize() {
129 cl::parser::initialize();
129130
130131 // Add existing passes to option.
131132 for (RegistryClass *Node = RegistryClass::getList();
4040 //
4141 class PassNameParser : public PassRegistrationListener,
4242 public cl::parser {
43 cl::Option *Opt;
4443 public:
45 PassNameParser();
44 PassNameParser(cl::Option &O);
4645 virtual ~PassNameParser();
4746
48 void initialize(cl::Option &O) {
49 Opt = &O;
50 cl::parser::initialize(O);
47 void initialize() {
48 cl::parser::initialize();
5149
5250 // Add all of the passes to the map that got initialized before 'this' did.
5351 enumeratePasses();
6866 // Implement the PassRegistrationListener callbacks used to populate our map
6967 //
7068 void passRegistered(const PassInfo *P) override {
71 if (ignorablePass(P) || !Opt) return;
69 if (ignorablePass(P)) return;
7270 if (findOption(P->getPassArgument()) != getNumOptions()) {
7371 errs() << "Two passes with the same argument (-"
7472 << P->getPassArgument() << ") attempted to be registered!\n";
530530 };
531531
532532 public:
533 generic_parser_base(Option &O) : Owner(O) {}
534
533535 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
534536
535537 // getNumOptions - Virtual function implemented by generic subclass to
568570 printGenericOptionDiff(O, V, Default, GlobalWidth);
569571 }
570572
571 void initialize(Option &O) {
572 // All of the modifiers for the option have been processed by now, so the
573 // argstr field should be stable, copy it down now.
574 //
575 hasArgStr = O.hasArgStr();
576 }
573 void initialize() {}
577574
578575 void getExtraOptionNames(SmallVectorImpl &OptionNames) {
579576 // If there has been no argstr specified, that means that we need to add an
580577 // argument for every possible option. This ensures that our options are
581578 // vectored to us.
582 if (!hasArgStr)
579 if (!Owner.hasArgStr())
583580 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
584581 OptionNames.push_back(getOption(i));
585582 }
596593 //
597594 // If this is the case, we cannot allow a value.
598595 //
599 if (hasArgStr)
596 if (Owner.hasArgStr())
600597 return ValueRequired;
601598 else
602599 return ValueDisallowed;
608605 unsigned findOption(const char *Name);
609606
610607 protected:
611 bool hasArgStr;
608 Option &Owner;
612609 };
613610
614611 // Default parser implementation - This implementation depends on having a
628625 SmallVector Values;
629626
630627 public:
628 parser(Option &O) : generic_parser_base(O) {}
631629 typedef DataType parser_data_type;
632630
633631 // Implement virtual functions needed by generic_parser_base
645643 // parse - Return true on error.
646644 bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) {
647645 StringRef ArgVal;
648 if (hasArgStr)
646 if (Owner.hasArgStr())
649647 ArgVal = Arg;
650648 else
651649 ArgVal = ArgName;
683681 //
684682 class basic_parser_impl { // non-template implementation of basic_parser
685683 public:
684 basic_parser_impl(Option &O) {}
685
686686 virtual ~basic_parser_impl() {}
687687
688688 enum ValueExpected getValueExpectedFlagDefault() const {
691691
692692 void getExtraOptionNames(SmallVectorImpl &) {}
693693
694 void initialize(Option &) {}
694 void initialize() {}
695695
696696 // Return the width of the option tag for printing...
697697 size_t getOptionWidth(const Option &O) const;
721721 //
722722 template class basic_parser : public basic_parser_impl {
723723 public:
724 basic_parser(Option &O) : basic_parser_impl(O) {}
724725 typedef DataType parser_data_type;
725726 typedef OptionValue OptVal;
726727 };
729730 // parser
730731 //
731732 template <> class parser : public basic_parser {
732 const char *ArgStr;
733
734 public:
733 public:
734 parser(Option &O) : basic_parser(O) {}
735
735736 // parse - Return true on error.
736737 bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val);
737738
738 template void initialize(Opt &O) { ArgStr = O.ArgStr; }
739 void initialize() {}
739740
740741 enum ValueExpected getValueExpectedFlagDefault() const {
741742 return ValueOptional;
757758 // parser
758759 template <> class parser : public basic_parser {
759760 public:
761 parser(Option &O) : basic_parser(O) {}
762
760763 // parse - Return true on error.
761764 bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val);
762765
781784 //
782785 template <> class parser : public basic_parser {
783786 public:
787 parser(Option &O) : basic_parser(O) {}
788
784789 // parse - Return true on error.
785790 bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
786791
801806 //
802807 template <> class parser : public basic_parser {
803808 public:
809 parser(Option &O) : basic_parser(O) {}
810
804811 // parse - Return true on error.
805812 bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
806813
822829 template <>
823830 class parser : public basic_parser {
824831 public:
832 parser(Option &O) : basic_parser(O) {}
833
825834 // parse - Return true on error.
826835 bool parse(Option &O, StringRef ArgName, StringRef Arg,
827836 unsigned long long &Val);
843852 //
844853 template <> class parser : public basic_parser {
845854 public:
855 parser(Option &O) : basic_parser(O) {}
856
846857 // parse - Return true on error.
847858 bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
848859
863874 //
864875 template <> class parser : public basic_parser {
865876 public:
877 parser(Option &O) : basic_parser(O) {}
878
866879 // parse - Return true on error.
867880 bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
868881
883896 //
884897 template <> class parser : public basic_parser {
885898 public:
899 parser(Option &O) : basic_parser(O) {}
900
886901 // parse - Return true on error.
887902 bool parse(Option &, StringRef, StringRef Arg, std::string &Value) {
888903 Value = Arg.str();
906921 //
907922 template <> class parser : public basic_parser {
908923 public:
924 parser(Option &O) : basic_parser(O) {}
925
909926 // parse - Return true on error.
910927 bool parse(Option &, StringRef, StringRef Arg, char &Value) {
911928 Value = Arg[0];
11651182
11661183 void done() {
11671184 addArgument();
1168 Parser.initialize(*this);
1185 Parser.initialize();
11691186 }
11701187
11711188 // Command line options should not be copyable
11861203 // One option...
11871204 template
11881205 explicit opt(const M0t &M0)
1189 : Option(Optional, NotHidden) {
1206 : Option(Optional, NotHidden), Parser(*this) {
11901207 apply(M0, this);
11911208 done();
11921209 }
11941211 // Two options...
11951212 template
11961213 opt(const M0t &M0, const M1t &M1)
1197 : Option(Optional, NotHidden) {
1214 : Option(Optional, NotHidden), Parser(*this) {
11981215 apply(M0, this);
11991216 apply(M1, this);
12001217 done();
12031220 // Three options...
12041221 template
12051222 opt(const M0t &M0, const M1t &M1, const M2t &M2)
1206 : Option(Optional, NotHidden) {
1223 : Option(Optional, NotHidden), Parser(*this) {
12071224 apply(M0, this);
12081225 apply(M1, this);
12091226 apply(M2, this);
12121229 // Four options...
12131230 template
12141231 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1215 : Option(Optional, NotHidden) {
1232 : Option(Optional, NotHidden), Parser(*this) {
12161233 apply(M0, this);
12171234 apply(M1, this);
12181235 apply(M2, this);
12221239 // Five options...
12231240 template
12241241 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, const M4t &M4)
1225 : Option(Optional, NotHidden) {
1242 : Option(Optional, NotHidden), Parser(*this) {
12261243 apply(M0, this);
12271244 apply(M1, this);
12281245 apply(M2, this);
12341251 template
12351252 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, const M4t &M4,
12361253 const M5t &M5)
1237 : Option(Optional, NotHidden) {
1254 : Option(Optional, NotHidden), Parser(*this) {
12381255 apply(M0, this);
12391256 apply(M1, this);
12401257 apply(M2, this);
12481265 class M6t>
12491266 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, const M4t &M4,
12501267 const M5t &M5, const M6t &M6)
1251 : Option(Optional, NotHidden) {
1268 : Option(Optional, NotHidden), Parser(*this) {
12521269 apply(M0, this);
12531270 apply(M1, this);
12541271 apply(M2, this);
12631280 class M6t, class M7t>
12641281 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, const M4t &M4,
12651282 const M5t &M5, const M6t &M6, const M7t &M7)
1266 : Option(Optional, NotHidden) {
1283 : Option(Optional, NotHidden), Parser(*this) {
12671284 apply(M0, this);
12681285 apply(M1, this);
12691286 apply(M2, this);
13641381
13651382 void done() {
13661383 addArgument();
1367 Parser.initialize(*this);
1384 Parser.initialize();
13681385 }
13691386
13701387 // Command line options should not be copyable
13841401 // One option...
13851402 template
13861403 explicit list(const M0t &M0)
1387 : Option(ZeroOrMore, NotHidden) {
1404 : Option(ZeroOrMore, NotHidden), Parser(*this) {
13881405 apply(M0, this);
13891406 done();
13901407 }
13911408 // Two options...
13921409 template
13931410 list(const M0t &M0, const M1t &M1)
1394 : Option(ZeroOrMore, NotHidden) {
1411 : Option(ZeroOrMore, NotHidden), Parser(*this) {
13951412 apply(M0, this);
13961413 apply(M1, this);
13971414 done();
13991416 // Three options...
14001417 template
14011418 list(const M0t &M0, const M1t &M1, const M2t &M2)
1402 : Option(ZeroOrMore, NotHidden) {
1419 : Option(ZeroOrMore, NotHidden), Parser(*this) {
14031420 apply(M0, this);
14041421 apply(M1, this);
14051422 apply(M2, this);
14081425 // Four options...
14091426 template
14101427 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1411 : Option(ZeroOrMore, NotHidden) {
1428 : Option(ZeroOrMore, NotHidden), Parser(*this) {
14121429 apply(M0, this);
14131430 apply(M1, this);
14141431 apply(M2, this);
14191436 template
14201437 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
14211438 const M4t &M4)
1422 : Option(ZeroOrMore, NotHidden) {
1439 : Option(ZeroOrMore, NotHidden), Parser(*this) {
14231440 apply(M0, this);
14241441 apply(M1, this);
14251442 apply(M2, this);
14311448 template
14321449 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
14331450 const M4t &M4, const M5t &M5)
1434 : Option(ZeroOrMore, NotHidden) {
1451 : Option(ZeroOrMore, NotHidden), Parser(*this) {
14351452 apply(M0, this);
14361453 apply(M1, this);
14371454 apply(M2, this);
14451462 class M6t>
14461463 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
14471464 const M4t &M4, const M5t &M5, const M6t &M6)
1448 : Option(ZeroOrMore, NotHidden) {
1465 : Option(ZeroOrMore, NotHidden), Parser(*this) {
14491466 apply(M0, this);
14501467 apply(M1, this);
14511468 apply(M2, this);
14601477 class M6t, class M7t>
14611478 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
14621479 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7)
1463 : Option(ZeroOrMore, NotHidden) {
1480 : Option(ZeroOrMore, NotHidden), Parser(*this) {
14641481 apply(M0, this);
14651482 apply(M1, this);
14661483 apply(M2, this);
15881605
15891606 void done() {
15901607 addArgument();
1591 Parser.initialize(*this);
1608 Parser.initialize();
15921609 }
15931610
15941611 // Command line options should not be copyable
16061623 // One option...
16071624 template
16081625 explicit bits(const M0t &M0)
1609 : Option(ZeroOrMore, NotHidden) {
1626 : Option(ZeroOrMore, NotHidden), Parser(*this) {
16101627 apply(M0, this);
16111628 done();
16121629 }
16131630 // Two options...
16141631 template
16151632 bits(const M0t &M0, const M1t &M1)
1616 : Option(ZeroOrMore, NotHidden) {
1633 : Option(ZeroOrMore, NotHidden), Parser(*this) {
16171634 apply(M0, this);
16181635 apply(M1, this);
16191636 done();
16211638 // Three options...
16221639 template
16231640 bits(const M0t &M0, const M1t &M1, const M2t &M2)
1624 : Option(ZeroOrMore, NotHidden) {
1641 : Option(ZeroOrMore, NotHidden), Parser(*this) {
16251642 apply(M0, this);
16261643 apply(M1, this);
16271644 apply(M2, this);
16301647 // Four options...
16311648 template
16321649 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1633 : Option(ZeroOrMore, NotHidden) {
1650 : Option(ZeroOrMore, NotHidden), Parser(*this) {
16341651 apply(M0, this);
16351652 apply(M1, this);
16361653 apply(M2, this);
16411658 template
16421659 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
16431660 const M4t &M4)
1644 : Option(ZeroOrMore, NotHidden) {
1661 : Option(ZeroOrMore, NotHidden), Parser(*this) {
16451662 apply(M0, this);
16461663 apply(M1, this);
16471664 apply(M2, this);
16531670 template
16541671 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
16551672 const M4t &M4, const M5t &M5)
1656 : Option(ZeroOrMore, NotHidden) {
1673 : Option(ZeroOrMore, NotHidden), Parser(*this) {
16571674 apply(M0, this);
16581675 apply(M1, this);
16591676 apply(M2, this);
16671684 class M6t>
16681685 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
16691686 const M4t &M4, const M5t &M5, const M6t &M6)
1670 : Option(ZeroOrMore, NotHidden) {
1687 : Option(ZeroOrMore, NotHidden), Parser(*this) {
16711688 apply(M0, this);
16721689 apply(M1, this);
16731690 apply(M2, this);
16821699 class M6t, class M7t>
16831700 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
16841701 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7)
1685 : Option(ZeroOrMore, NotHidden) {
1702 : Option(ZeroOrMore, NotHidden), Parser(*this) {
16861703 apply(M0, this);
16871704 apply(M1, this);
16881705 apply(M2, this);
222222 PassRegistry::getPassRegistry()->enumerateWith(this);
223223 }
224224
225 PassNameParser::PassNameParser()
226 : Opt(nullptr) {
225 PassNameParser::PassNameParser(cl::Option &O)
226 : cl::parser(O) {
227227 PassRegistry::getPassRegistry()->addRegistrationListener(this);
228228 }
229229