forked from OSchip/llvm-project
convert a bunch of std::strings to use StringRef. This should eliminate
a massive number of temporary strings created when parsing a command line. More still left to eliminate. llvm-svn: 82318
This commit is contained in:
parent
7e6deb1cb4
commit
aecd74d895
|
@ -21,18 +21,17 @@
|
|||
#define LLVM_SUPPORT_COMMANDLINE_H
|
||||
|
||||
#include "llvm/Support/type_traits.h"
|
||||
#include "llvm/Support/DataTypes.h"
|
||||
#include "llvm/Support/Compiler.h"
|
||||
#include "llvm/ADT/SmallVector.h"
|
||||
#include "llvm/ADT/Twine.h"
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
#include <cstdarg>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace llvm {
|
||||
|
||||
|
||||
/// cl Namespace - This namespace contains all of the command line option
|
||||
/// processing machinery. It is intentionally a short name to make qualified
|
||||
/// usage concise.
|
||||
|
@ -144,7 +143,7 @@ class Option {
|
|||
// argument and the program should exit.
|
||||
//
|
||||
virtual bool handleOccurrence(unsigned pos, const char *ArgName,
|
||||
const std::string &Arg) = 0;
|
||||
StringRef Arg) = 0;
|
||||
|
||||
virtual enum ValueExpected getValueExpectedFlagDefault() const {
|
||||
return ValueOptional;
|
||||
|
@ -215,8 +214,7 @@ protected:
|
|||
getOptionHiddenFlag() != 0 && "Not all default flags specified!");
|
||||
}
|
||||
|
||||
inline void setNumAdditionalVals(unsigned n)
|
||||
{ AdditionalVals = n; }
|
||||
inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; }
|
||||
public:
|
||||
// addArgument - Register this argument with the commandline system.
|
||||
//
|
||||
|
@ -237,10 +235,10 @@ public:
|
|||
// addOccurrence - Wrapper around handleOccurrence that enforces Flags
|
||||
//
|
||||
bool addOccurrence(unsigned pos, const char *ArgName,
|
||||
const std::string &Value, bool MultiArg = false);
|
||||
StringRef Value, bool MultiArg = false);
|
||||
|
||||
// Prints option name followed by message. Always returns true.
|
||||
bool error(std::string Message, const char *ArgName = 0);
|
||||
bool error(const Twine &Message, const char *ArgName = 0);
|
||||
|
||||
public:
|
||||
inline int getNumOccurrences() const { return NumOccurrences; }
|
||||
|
@ -458,9 +456,8 @@ public:
|
|||
}
|
||||
|
||||
// parse - Return true on error.
|
||||
bool parse(Option &O, const char *ArgName, const std::string &Arg,
|
||||
DataType &V) {
|
||||
std::string ArgVal;
|
||||
bool parse(Option &O, const char *ArgName, StringRef Arg, DataType &V) {
|
||||
StringRef ArgVal;
|
||||
if (hasArgStr)
|
||||
ArgVal = Arg;
|
||||
else
|
||||
|
@ -468,7 +465,7 @@ public:
|
|||
|
||||
for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
|
||||
i != e; ++i)
|
||||
if (ArgVal == Values[i].first) {
|
||||
if (Values[i].first == ArgVal) {
|
||||
V = Values[i].second.first;
|
||||
return false;
|
||||
}
|
||||
|
@ -541,7 +538,7 @@ class parser<bool> : public basic_parser<bool> {
|
|||
public:
|
||||
|
||||
// parse - Return true on error.
|
||||
bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
|
||||
bool parse(Option &O, const char *ArgName, StringRef Arg, bool &Val);
|
||||
|
||||
template <class Opt>
|
||||
void initialize(Opt &O) {
|
||||
|
@ -568,8 +565,7 @@ template<>
|
|||
class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
|
||||
public:
|
||||
// parse - Return true on error.
|
||||
bool parse(Option &O, const char *ArgName, const std::string &Arg,
|
||||
boolOrDefault &Val);
|
||||
bool parse(Option &O, const char *ArgName, StringRef Arg, boolOrDefault &Val);
|
||||
|
||||
enum ValueExpected getValueExpectedFlagDefault() const {
|
||||
return ValueOptional;
|
||||
|
@ -591,7 +587,7 @@ template<>
|
|||
class parser<int> : public basic_parser<int> {
|
||||
public:
|
||||
// parse - Return true on error.
|
||||
bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
|
||||
bool parse(Option &O, const char *ArgName, StringRef Arg, int &Val);
|
||||
|
||||
// getValueName - Overload in subclass to provide a better default value.
|
||||
virtual const char *getValueName() const { return "int"; }
|
||||
|
@ -610,7 +606,7 @@ template<>
|
|||
class parser<unsigned> : public basic_parser<unsigned> {
|
||||
public:
|
||||
// parse - Return true on error.
|
||||
bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val);
|
||||
bool parse(Option &O, const char *AN, StringRef Arg, unsigned &Val);
|
||||
|
||||
// getValueName - Overload in subclass to provide a better default value.
|
||||
virtual const char *getValueName() const { return "uint"; }
|
||||
|
@ -628,7 +624,7 @@ template<>
|
|||
class parser<double> : public basic_parser<double> {
|
||||
public:
|
||||
// parse - Return true on error.
|
||||
bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
|
||||
bool parse(Option &O, const char *AN, StringRef Arg, double &Val);
|
||||
|
||||
// getValueName - Overload in subclass to provide a better default value.
|
||||
virtual const char *getValueName() const { return "number"; }
|
||||
|
@ -646,7 +642,7 @@ template<>
|
|||
class parser<float> : public basic_parser<float> {
|
||||
public:
|
||||
// parse - Return true on error.
|
||||
bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
|
||||
bool parse(Option &O, const char *AN, StringRef Arg, float &Val);
|
||||
|
||||
// getValueName - Overload in subclass to provide a better default value.
|
||||
virtual const char *getValueName() const { return "number"; }
|
||||
|
@ -664,9 +660,8 @@ template<>
|
|||
class parser<std::string> : public basic_parser<std::string> {
|
||||
public:
|
||||
// parse - Return true on error.
|
||||
bool parse(Option &, const char *, const std::string &Arg,
|
||||
std::string &Value) {
|
||||
Value = Arg;
|
||||
bool parse(Option &, const char *, StringRef Arg, std::string &Value) {
|
||||
Value = Arg.str();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -686,8 +681,7 @@ template<>
|
|||
class parser<char> : public basic_parser<char> {
|
||||
public:
|
||||
// parse - Return true on error.
|
||||
bool parse(Option &, const char *, const std::string &Arg,
|
||||
char &Value) {
|
||||
bool parse(Option &, const char *, StringRef Arg, char &Value) {
|
||||
Value = Arg[0];
|
||||
return false;
|
||||
}
|
||||
|
@ -834,7 +828,7 @@ class opt : public Option,
|
|||
ParserClass Parser;
|
||||
|
||||
virtual bool handleOccurrence(unsigned pos, const char *ArgName,
|
||||
const std::string &Arg) {
|
||||
StringRef Arg) {
|
||||
typename ParserClass::parser_data_type Val =
|
||||
typename ParserClass::parser_data_type();
|
||||
if (Parser.parse(*this, ArgName, Arg, Val))
|
||||
|
@ -1007,7 +1001,7 @@ class list : public Option, public list_storage<DataType, Storage> {
|
|||
}
|
||||
|
||||
virtual bool handleOccurrence(unsigned pos, const char *ArgName,
|
||||
const std::string &Arg) {
|
||||
StringRef Arg) {
|
||||
typename ParserClass::parser_data_type Val =
|
||||
typename ParserClass::parser_data_type();
|
||||
if (Parser.parse(*this, ArgName, Arg, Val))
|
||||
|
@ -1207,7 +1201,7 @@ class bits : public Option, public bits_storage<DataType, Storage> {
|
|||
}
|
||||
|
||||
virtual bool handleOccurrence(unsigned pos, const char *ArgName,
|
||||
const std::string &Arg) {
|
||||
StringRef Arg) {
|
||||
typename ParserClass::parser_data_type Val =
|
||||
typename ParserClass::parser_data_type();
|
||||
if (Parser.parse(*this, ArgName, Arg, Val))
|
||||
|
@ -1308,7 +1302,7 @@ public:
|
|||
class alias : public Option {
|
||||
Option *AliasFor;
|
||||
virtual bool handleOccurrence(unsigned pos, const char * /*ArgName*/,
|
||||
const std::string &Arg) {
|
||||
StringRef Arg) {
|
||||
return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
|
||||
}
|
||||
// Handle printing stuff...
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#include "llvm/System/Path.h"
|
||||
#include "llvm/ADT/OwningPtr.h"
|
||||
#include "llvm/ADT/StringMap.h"
|
||||
#include "llvm/ADT/Twine.h"
|
||||
#include "llvm/Config/config.h"
|
||||
#include <set>
|
||||
#include <cerrno>
|
||||
|
@ -193,7 +194,7 @@ static inline bool ProvideOption(Option *Handler, const char *ArgName,
|
|||
|
||||
if (Value)
|
||||
return Handler->error("does not allow a value! '" +
|
||||
std::string(Value) + "' specified.");
|
||||
Twine(Value) + "' specified.");
|
||||
break;
|
||||
case ValueOptional:
|
||||
break;
|
||||
|
@ -205,34 +206,31 @@ static inline bool ProvideOption(Option *Handler, const char *ArgName,
|
|||
}
|
||||
|
||||
// If this isn't a multi-arg option, just run the handler.
|
||||
if (NumAdditionalVals == 0) {
|
||||
if (NumAdditionalVals == 0)
|
||||
return Handler->addOccurrence(i, ArgName, Value ? Value : "");
|
||||
}
|
||||
|
||||
// If it is, run the handle several times.
|
||||
else {
|
||||
bool MultiArg = false;
|
||||
bool MultiArg = false;
|
||||
|
||||
if (Value) {
|
||||
if (Handler->addOccurrence(i, ArgName, Value, MultiArg))
|
||||
return true;
|
||||
--NumAdditionalVals;
|
||||
MultiArg = true;
|
||||
}
|
||||
|
||||
while (NumAdditionalVals > 0) {
|
||||
|
||||
if (i+1 < argc) {
|
||||
Value = argv[++i];
|
||||
} else {
|
||||
return Handler->error("not enough values!");
|
||||
}
|
||||
if (Handler->addOccurrence(i, ArgName, Value, MultiArg))
|
||||
return true;
|
||||
MultiArg = true;
|
||||
--NumAdditionalVals;
|
||||
}
|
||||
return false;
|
||||
if (Value) {
|
||||
if (Handler->addOccurrence(i, ArgName, Value, MultiArg))
|
||||
return true;
|
||||
--NumAdditionalVals;
|
||||
MultiArg = true;
|
||||
}
|
||||
|
||||
while (NumAdditionalVals > 0) {
|
||||
|
||||
if (i+1 >= argc)
|
||||
return Handler->error("not enough values!");
|
||||
Value = argv[++i];
|
||||
|
||||
if (Handler->addOccurrence(i, ArgName, Value, MultiArg))
|
||||
return true;
|
||||
MultiArg = true;
|
||||
--NumAdditionalVals;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool ProvidePositionalOption(Option *Handler, const std::string &Arg,
|
||||
|
@ -763,7 +761,7 @@ void cl::ParseCommandLineOptions(int argc, char **argv,
|
|||
// Option Base class implementation
|
||||
//
|
||||
|
||||
bool Option::error(std::string Message, const char *ArgName) {
|
||||
bool Option::error(const Twine &Message, const char *ArgName) {
|
||||
if (ArgName == 0) ArgName = ArgStr;
|
||||
if (ArgName[0] == 0)
|
||||
errs() << HelpStr; // Be nice for positional arguments
|
||||
|
@ -775,8 +773,7 @@ bool Option::error(std::string Message, const char *ArgName) {
|
|||
}
|
||||
|
||||
bool Option::addOccurrence(unsigned pos, const char *ArgName,
|
||||
const std::string &Value,
|
||||
bool MultiArg) {
|
||||
StringRef Value, bool MultiArg) {
|
||||
if (!MultiArg)
|
||||
NumOccurrences++; // Increment the number of times we have been seen
|
||||
|
||||
|
@ -860,42 +857,47 @@ void basic_parser_impl::printOptionInfo(const Option &O,
|
|||
// parser<bool> implementation
|
||||
//
|
||||
bool parser<bool>::parse(Option &O, const char *ArgName,
|
||||
const std::string &Arg, bool &Value) {
|
||||
StringRef Arg, bool &Value) {
|
||||
if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
|
||||
Arg == "1") {
|
||||
Value = true;
|
||||
} else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
|
||||
Value = false;
|
||||
} else {
|
||||
return O.error("'" + Arg +
|
||||
"' is invalid value for boolean argument! Try 0 or 1");
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
|
||||
Value = false;
|
||||
return false;
|
||||
}
|
||||
return O.error("'" + Arg +
|
||||
"' is invalid value for boolean argument! Try 0 or 1");
|
||||
}
|
||||
|
||||
// parser<boolOrDefault> implementation
|
||||
//
|
||||
bool parser<boolOrDefault>::parse(Option &O, const char *ArgName,
|
||||
const std::string &Arg, boolOrDefault &Value) {
|
||||
StringRef Arg, boolOrDefault &Value) {
|
||||
if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
|
||||
Arg == "1") {
|
||||
Value = BOU_TRUE;
|
||||
} else if (Arg == "false" || Arg == "FALSE"
|
||||
|| Arg == "False" || Arg == "0") {
|
||||
Value = BOU_FALSE;
|
||||
} else {
|
||||
return O.error("'" + Arg +
|
||||
"' is invalid value for boolean argument! Try 0 or 1");
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
|
||||
Value = BOU_FALSE;
|
||||
return false;
|
||||
}
|
||||
|
||||
return O.error("'" + Arg +
|
||||
"' is invalid value for boolean argument! Try 0 or 1");
|
||||
}
|
||||
|
||||
// parser<int> implementation
|
||||
//
|
||||
bool parser<int>::parse(Option &O, const char *ArgName,
|
||||
const std::string &Arg, int &Value) {
|
||||
StringRef Arg, int &Value) {
|
||||
char *End;
|
||||
Value = (int)strtol(Arg.c_str(), &End, 0);
|
||||
// FIXME: Temporary.
|
||||
std::string TMP = Arg.str();
|
||||
Value = (int)strtol(TMP.c_str(), &End, 0);
|
||||
if (*End != 0)
|
||||
return O.error("'" + Arg + "' value invalid for integer argument!");
|
||||
return false;
|
||||
|
@ -904,10 +906,13 @@ bool parser<int>::parse(Option &O, const char *ArgName,
|
|||
// parser<unsigned> implementation
|
||||
//
|
||||
bool parser<unsigned>::parse(Option &O, const char *ArgName,
|
||||
const std::string &Arg, unsigned &Value) {
|
||||
StringRef Arg, unsigned &Value) {
|
||||
char *End;
|
||||
errno = 0;
|
||||
unsigned long V = strtoul(Arg.c_str(), &End, 0);
|
||||
|
||||
// FIXME: Temporary.
|
||||
std::string TMP = Arg.str();
|
||||
unsigned long V = strtoul(TMP.c_str(), &End, 0);
|
||||
Value = (unsigned)V;
|
||||
if (((V == ULONG_MAX) && (errno == ERANGE))
|
||||
|| (*End != 0)
|
||||
|
@ -918,8 +923,11 @@ bool parser<unsigned>::parse(Option &O, const char *ArgName,
|
|||
|
||||
// parser<double>/parser<float> implementation
|
||||
//
|
||||
static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
|
||||
const char *ArgStart = Arg.c_str();
|
||||
static bool parseDouble(Option &O, StringRef Arg, double &Value) {
|
||||
// FIXME: Temporary.
|
||||
std::string TMP = Arg.str();
|
||||
|
||||
const char *ArgStart = TMP.c_str();
|
||||
char *End;
|
||||
Value = strtod(ArgStart, &End);
|
||||
if (*End != 0)
|
||||
|
@ -928,12 +936,12 @@ static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
|
|||
}
|
||||
|
||||
bool parser<double>::parse(Option &O, const char *AN,
|
||||
const std::string &Arg, double &Val) {
|
||||
StringRef Arg, double &Val) {
|
||||
return parseDouble(O, Arg, Val);
|
||||
}
|
||||
|
||||
bool parser<float>::parse(Option &O, const char *AN,
|
||||
const std::string &Arg, float &Val) {
|
||||
StringRef Arg, float &Val) {
|
||||
double dVal;
|
||||
if (parseDouble(O, Arg, dVal))
|
||||
return true;
|
||||
|
|
Loading…
Reference in New Issue