diff --git a/include/Support/CommandLine.h b/include/Support/CommandLine.h index ed2559bd2b9..97b223c7381 100644 --- a/include/Support/CommandLine.h +++ b/include/Support/CommandLine.h @@ -515,6 +515,21 @@ struct parser : public basic_parser { }; +//-------------------------------------------------- +// parser +// +template<> +struct parser : public basic_parser { + + // parse - Return true on error. + bool parse(Option &O, const char *ArgName, const std::string &Arg, + unsigned &Val); + + // getValueName - Overload in subclass to provide a better default value. + virtual const char *getValueName() const { return "uint"; } +}; + + //-------------------------------------------------- // parser // diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index a0eca7a8ed4..bbf996af7b1 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -575,14 +575,25 @@ bool parser::parse(Option &O, const char *ArgName, // bool parser::parse(Option &O, const char *ArgName, const std::string &Arg, int &Value) { - const char *ArgStart = Arg.c_str(); char *End; - Value = (int)strtol(ArgStart, &End, 0); + Value = (int)strtol(Arg.c_str(), &End, 0); if (*End != 0) return O.error(": '" + Arg + "' value invalid for integer argument!"); return false; } +// parser implementation +// +bool parser::parse(Option &O, const char *ArgName, + const std::string &Arg, unsigned &Value) { + char *End; + long long int V = strtoll(Arg.c_str(), &End, 0); + Value = (unsigned)V; + if (*End != 0 || V < 0 || Value != V) + return O.error(": '" + Arg + "' value invalid for uint argument!"); + return false; +} + // parser/parser implementation // static bool parseDouble(Option &O, const std::string &Arg, double &Value) { diff --git a/support/lib/Support/CommandLine.cpp b/support/lib/Support/CommandLine.cpp index a0eca7a8ed4..bbf996af7b1 100644 --- a/support/lib/Support/CommandLine.cpp +++ b/support/lib/Support/CommandLine.cpp @@ -575,14 +575,25 @@ bool parser::parse(Option &O, const char *ArgName, // bool parser::parse(Option &O, const char *ArgName, const std::string &Arg, int &Value) { - const char *ArgStart = Arg.c_str(); char *End; - Value = (int)strtol(ArgStart, &End, 0); + Value = (int)strtol(Arg.c_str(), &End, 0); if (*End != 0) return O.error(": '" + Arg + "' value invalid for integer argument!"); return false; } +// parser implementation +// +bool parser::parse(Option &O, const char *ArgName, + const std::string &Arg, unsigned &Value) { + char *End; + long long int V = strtoll(Arg.c_str(), &End, 0); + Value = (unsigned)V; + if (*End != 0 || V < 0 || Value != V) + return O.error(": '" + Arg + "' value invalid for uint argument!"); + return false; +} + // parser/parser implementation // static bool parseDouble(Option &O, const std::string &Arg, double &Value) {