Index: include/llvm/Support/CommandLine.h =================================================================== --- include/llvm/Support/CommandLine.h +++ include/llvm/Support/CommandLine.h @@ -30,6 +30,7 @@ #include "llvm/ADT/iterator_range.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/raw_ostream.h" #include #include #include @@ -643,8 +644,11 @@ StringRef Name; StringRef HelpStr; }; - + public: + /// arbitrary spacing for printOptionDiff + static const size_t MaxOptWidth = 8; + generic_parser_base(Option &O) : Owner(O) {} virtual ~generic_parser_base() = default; @@ -799,6 +803,9 @@ // class basic_parser_impl { // non-template implementation of basic_parser public: + /// arbitrary spacing for printOptionDiff + static const size_t MaxOptWidth = generic_parser_base::MaxOptWidth; + basic_parser_impl(Option &) {} enum ValueExpected getValueExpectedFlagDefault() const { @@ -840,10 +847,19 @@ template class basic_parser : public basic_parser_impl { public: using parser_data_type = DataType; + using OptVal = OptionValue; + using OptValParam = typename std::conditional::value, + OptVal, const OptVal&>::type; + using ValParam = typename std::conditional::value, + DataType, const DataType&>::type; basic_parser(Option &O) : basic_parser_impl(O) {} + + virtual void printOptionDiff(const Option &O, ValParam V, OptValParam Default, + size_t GlobalWidth) const; + protected: ~basic_parser() = default; }; @@ -867,9 +883,6 @@ // getValueName - Do not print = at all. StringRef getValueName() const override { return StringRef(); } - void printOptionDiff(const Option &O, bool V, OptVal Default, - size_t GlobalWidth) const; - // An out-of-line virtual method to provide a 'home' for this class. void anchor() override; }; @@ -893,9 +906,6 @@ // getValueName - Do not print = at all. StringRef getValueName() const override { return StringRef(); } - void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default, - size_t GlobalWidth) const; - // An out-of-line virtual method to provide a 'home' for this class. void anchor() override; }; @@ -915,9 +925,6 @@ // getValueName - Overload in subclass to provide a better default value. StringRef getValueName() const override { return "int"; } - void printOptionDiff(const Option &O, int V, OptVal Default, - size_t GlobalWidth) const; - // An out-of-line virtual method to provide a 'home' for this class. void anchor() override; }; @@ -937,9 +944,6 @@ // getValueName - Overload in subclass to provide a better default value. StringRef getValueName() const override { return "uint"; } - void printOptionDiff(const Option &O, unsigned V, OptVal Default, - size_t GlobalWidth) const; - // An out-of-line virtual method to provide a 'home' for this class. void anchor() override; }; @@ -962,9 +966,6 @@ // getValueName - Overload in subclass to provide a better default value. StringRef getValueName() const override { return "uint"; } - void printOptionDiff(const Option &O, unsigned long long V, OptVal Default, - size_t GlobalWidth) const; - // An out-of-line virtual method to provide a 'home' for this class. void anchor() override; }; @@ -984,9 +985,6 @@ // getValueName - Overload in subclass to provide a better default value. StringRef getValueName() const override { return "number"; } - void printOptionDiff(const Option &O, double V, OptVal Default, - size_t GlobalWidth) const; - // An out-of-line virtual method to provide a 'home' for this class. void anchor() override; }; @@ -1006,9 +1004,6 @@ // getValueName - Overload in subclass to provide a better default value. StringRef getValueName() const override { return "number"; } - void printOptionDiff(const Option &O, float V, OptVal Default, - size_t GlobalWidth) const; - // An out-of-line virtual method to provide a 'home' for this class. void anchor() override; }; @@ -1031,8 +1026,8 @@ // getValueName - Overload in subclass to provide a better default value. StringRef getValueName() const override { return "string"; } - void printOptionDiff(const Option &O, StringRef V, const OptVal &Default, - size_t GlobalWidth) const; + void printOptionDiff(const Option &O, const std::string &V, const OptVal &Default, + size_t GlobalWidth) const override; // An out-of-line virtual method to provide a 'home' for this class. void anchor() override; @@ -1056,9 +1051,6 @@ // getValueName - Overload in subclass to provide a better default value. StringRef getValueName() const override { return "char"; } - void printOptionDiff(const Option &O, char V, OptVal Default, - size_t GlobalWidth) const; - // An out-of-line virtual method to provide a 'home' for this class. void anchor() override; }; @@ -1082,7 +1074,7 @@ // This is instantiated for basic parsers when the parsed value has a different // type than the option value. e.g. HelpPrinter. template struct OptionDiffPrinter { - void print(const Option &O, const parser &P, const ValDT & /*V*/, + void print(const Option &O, const basic_parser &P, const ValDT & /*V*/, const OptionValue & /*Default*/, size_t GlobalWidth) { P.printOptionNoValue(O, GlobalWidth); } @@ -1091,7 +1083,7 @@ // This is instantiated for basic parsers when the parsed value has the same // type as the option value. template struct OptionDiffPrinter { - void print(const Option &O, const parser
&P, const DT &V, + void print(const Option &O, const basic_parser
&P, const DT &V, const OptionValue
&Default, size_t GlobalWidth) { P.printOptionDiff(O, V, Default, GlobalWidth); } @@ -1104,10 +1096,8 @@ const Option &O, const basic_parser &P, const ValDT &V, const OptionValue &Default, size_t GlobalWidth) { - OptionDiffPrinter printer; - printer.print(O, static_cast(P), V, Default, - GlobalWidth); + printer.print(O, P, V, Default, GlobalWidth); } //===----------------------------------------------------------------------===// Index: lib/Support/CommandLine.cpp =================================================================== --- lib/Support/CommandLine.cpp +++ lib/Support/CommandLine.cpp @@ -1598,8 +1598,6 @@ } } -static const size_t MaxOptWidth = 8; // arbitrary spacing for printOptionDiff - // printGenericOptionDiff - Print the value of this option and it's default. // // "Generic" options have each value mapped to a name. @@ -1630,39 +1628,49 @@ outs() << "= *unknown option value*\n"; } -// printOptionDiff - Specializations for printing basic value types. -// -#define PRINT_OPT_DIFF(T) \ - void parser::printOptionDiff(const Option &O, T V, OptionValue D, \ - size_t GlobalWidth) const { \ - printOptionName(O, GlobalWidth); \ - std::string Str; \ - { \ - raw_string_ostream SS(Str); \ - SS << V; \ - } \ - outs() << "= " << Str; \ - size_t NumSpaces = \ - MaxOptWidth > Str.size() ? MaxOptWidth - Str.size() : 0; \ - outs().indent(NumSpaces) << " (default: "; \ - if (D.hasValue()) \ - outs() << D.getValue(); \ - else \ - outs() << "*no default*"; \ - outs() << ")\n"; \ +// explicit basic_parser::printOptionDiff template instantiations :: + +#define TPLINST_BASIC_PARSER_PRINTOPTDIFF_SCALAR(T) \ + template void basic_parser::printOptionDiff(const Option &O, T V, \ + OptVal D, size_t GW) const; + +#define TPLINST_BASIC_PARSER_PRINTOPTDIFF_NONSCALAR(T) \ + template void basic_parser::printOptionDiff(const Option &O, const T &V, \ + const OptVal &D, \ + size_t GW) const; + +TPLINST_BASIC_PARSER_PRINTOPTDIFF_SCALAR(bool) +TPLINST_BASIC_PARSER_PRINTOPTDIFF_SCALAR(boolOrDefault) +TPLINST_BASIC_PARSER_PRINTOPTDIFF_SCALAR(int) +TPLINST_BASIC_PARSER_PRINTOPTDIFF_SCALAR(unsigned) +TPLINST_BASIC_PARSER_PRINTOPTDIFF_SCALAR(unsigned long long) +TPLINST_BASIC_PARSER_PRINTOPTDIFF_SCALAR(double) +TPLINST_BASIC_PARSER_PRINTOPTDIFF_SCALAR(float) +TPLINST_BASIC_PARSER_PRINTOPTDIFF_NONSCALAR(std::string) +TPLINST_BASIC_PARSER_PRINTOPTDIFF_SCALAR(char) + +template +void basic_parser::printOptionDiff(const Option &O, ValParam V, + OptValParam D, + size_t GlobalWidth) const { + printOptionName(O, GlobalWidth); + std::string Str; + { + raw_string_ostream SS(Str); + SS << V; } + outs() << "= " << Str; + size_t NumSpaces = MaxOptWidth > Str.size() ? MaxOptWidth - Str.size() : 0; + outs().indent(NumSpaces) << " (default: "; + if (D.hasValue()) + outs() << D.getValue(); + else + outs() << "*no default*"; + outs() << ")\n"; +} -PRINT_OPT_DIFF(bool) -PRINT_OPT_DIFF(boolOrDefault) -PRINT_OPT_DIFF(int) -PRINT_OPT_DIFF(unsigned) -PRINT_OPT_DIFF(unsigned long long) -PRINT_OPT_DIFF(double) -PRINT_OPT_DIFF(float) -PRINT_OPT_DIFF(char) - -void parser::printOptionDiff(const Option &O, StringRef V, - const OptionValue &D, +void parser::printOptionDiff(const Option &O, const std::string &V, + const OptVal &D, size_t GlobalWidth) const { printOptionName(O, GlobalWidth); outs() << "= " << V;