convert a bunch more stuff to use StringRef. The ArgName arguments are now

stringref because they may not be nul terminated.  For options like -Lfoo
this now avoids a O(n)  temporary std::strings where N is the length of 
the string after -L.

llvm-svn: 82345
This commit is contained in:
Chris Lattner 2009-09-20 00:40:49 +00:00
parent 2cdc7b8e76
commit 85478e8cd1
2 changed files with 46 additions and 47 deletions

View File

@ -142,7 +142,7 @@ class Option {
// an argument. Should return true if there was an error processing the
// argument and the program should exit.
//
virtual bool handleOccurrence(unsigned pos, const char *ArgName,
virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
StringRef Arg) = 0;
virtual enum ValueExpected getValueExpectedFlagDefault() const {
@ -232,13 +232,13 @@ public:
virtual void getExtraOptionNames(std::vector<const char*> &) {}
// addOccurrence - Wrapper around handleOccurrence that enforces Flags
// addOccurrence - Wrapper around handleOccurrence that enforces Flags.
//
bool addOccurrence(unsigned pos, const char *ArgName,
bool addOccurrence(unsigned pos, StringRef ArgName,
StringRef Value, bool MultiArg = false);
// Prints option name followed by message. Always returns true.
bool error(const Twine &Message, const char *ArgName = 0);
bool error(const Twine &Message, StringRef ArgName = StringRef());
public:
inline int getNumOccurrences() const { return NumOccurrences; }
@ -456,7 +456,7 @@ public:
}
// parse - Return true on error.
bool parse(Option &O, const char *ArgName, StringRef Arg, DataType &V) {
bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) {
StringRef ArgVal;
if (hasArgStr)
ArgVal = Arg;
@ -538,7 +538,7 @@ class parser<bool> : public basic_parser<bool> {
public:
// parse - Return true on error.
bool parse(Option &O, const char *ArgName, StringRef Arg, bool &Val);
bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val);
template <class Opt>
void initialize(Opt &O) {
@ -565,7 +565,7 @@ template<>
class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
public:
// parse - Return true on error.
bool parse(Option &O, const char *ArgName, StringRef Arg, boolOrDefault &Val);
bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val);
enum ValueExpected getValueExpectedFlagDefault() const {
return ValueOptional;
@ -587,7 +587,7 @@ template<>
class parser<int> : public basic_parser<int> {
public:
// parse - Return true on error.
bool parse(Option &O, const char *ArgName, StringRef Arg, int &Val);
bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
// getValueName - Overload in subclass to provide a better default value.
virtual const char *getValueName() const { return "int"; }
@ -606,7 +606,7 @@ template<>
class parser<unsigned> : public basic_parser<unsigned> {
public:
// parse - Return true on error.
bool parse(Option &O, const char *AN, StringRef Arg, unsigned &Val);
bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
// getValueName - Overload in subclass to provide a better default value.
virtual const char *getValueName() const { return "uint"; }
@ -624,7 +624,7 @@ template<>
class parser<double> : public basic_parser<double> {
public:
// parse - Return true on error.
bool parse(Option &O, const char *AN, StringRef Arg, double &Val);
bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
// getValueName - Overload in subclass to provide a better default value.
virtual const char *getValueName() const { return "number"; }
@ -642,7 +642,7 @@ template<>
class parser<float> : public basic_parser<float> {
public:
// parse - Return true on error.
bool parse(Option &O, const char *AN, StringRef Arg, float &Val);
bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
// getValueName - Overload in subclass to provide a better default value.
virtual const char *getValueName() const { return "number"; }
@ -660,7 +660,7 @@ template<>
class parser<std::string> : public basic_parser<std::string> {
public:
// parse - Return true on error.
bool parse(Option &, const char *, StringRef Arg, std::string &Value) {
bool parse(Option &, StringRef ArgName, StringRef Arg, std::string &Value) {
Value = Arg.str();
return false;
}
@ -681,7 +681,7 @@ template<>
class parser<char> : public basic_parser<char> {
public:
// parse - Return true on error.
bool parse(Option &, const char *, StringRef Arg, char &Value) {
bool parse(Option &, StringRef ArgName, StringRef Arg, char &Value) {
Value = Arg[0];
return false;
}
@ -827,7 +827,7 @@ class opt : public Option,
is_class<DataType>::value> {
ParserClass Parser;
virtual bool handleOccurrence(unsigned pos, const char *ArgName,
virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
StringRef Arg) {
typename ParserClass::parser_data_type Val =
typename ParserClass::parser_data_type();
@ -1000,8 +1000,7 @@ class list : public Option, public list_storage<DataType, Storage> {
return Parser.getExtraOptionNames(OptionNames);
}
virtual bool handleOccurrence(unsigned pos, const char *ArgName,
StringRef Arg) {
virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg){
typename ParserClass::parser_data_type Val =
typename ParserClass::parser_data_type();
if (Parser.parse(*this, ArgName, Arg, Val))
@ -1200,8 +1199,7 @@ class bits : public Option, public bits_storage<DataType, Storage> {
return Parser.getExtraOptionNames(OptionNames);
}
virtual bool handleOccurrence(unsigned pos, const char *ArgName,
StringRef Arg) {
virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg){
typename ParserClass::parser_data_type Val =
typename ParserClass::parser_data_type();
if (Parser.parse(*this, ArgName, Arg, Val))
@ -1301,7 +1299,7 @@ public:
class alias : public Option {
Option *AliasFor;
virtual bool handleOccurrence(unsigned pos, const char * /*ArgName*/,
virtual bool handleOccurrence(unsigned pos, StringRef /*ArgName*/,
StringRef Arg) {
return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
}

View File

@ -171,7 +171,7 @@ static Option *LookupOption(const char *&Arg, const char *&Value,
return I != OptionsMap.end() ? I->second : 0;
}
static inline bool ProvideOption(Option *Handler, const char *ArgName,
static inline bool ProvideOption(Option *Handler, StringRef ArgName,
const char *Value, int argc, char **argv,
int &i) {
// Is this a multi-argument option?
@ -253,28 +253,28 @@ static inline bool isPrefixedOrGrouping(const Option *O) {
// see if there options that satisfy the predicate. If we find one, return it,
// otherwise return null.
//
static Option *getOptionPred(std::string Name, size_t &Length,
static Option *getOptionPred(StringRef Name, size_t &Length,
bool (*Pred)(const Option*),
StringMap<Option*> &OptionsMap) {
StringMap<Option*>::iterator OMI = OptionsMap.find(Name);
if (OMI != OptionsMap.end() && Pred(OMI->second)) {
Length = Name.length();
Length = Name.size();
return OMI->second;
}
if (Name.size() == 1) return 0;
do {
Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Name = Name.substr(0, Name.size()-1); // Chop off the last character.
OMI = OptionsMap.find(Name);
// Loop while we haven't found an option and Name still has at least two
// characters in it (so that the next iteration will not be the empty
// string...
// string.
} while ((OMI == OptionsMap.end() || !Pred(OMI->second)) && Name.size() > 1);
if (OMI != OptionsMap.end() && Pred(OMI->second)) {
Length = Name.length();
Length = Name.size();
return OMI->second; // Found one!
}
return 0; // No option found!
@ -517,7 +517,9 @@ void cl::ParseCommandLineOptions(int argc, char **argv,
if (ActivePositionalArg) {
ProvidePositionalOption(ActivePositionalArg, argv[i], i);
continue; // We are done!
} else if (!PositionalOpts.empty()) {
}
if (!PositionalOpts.empty()) {
PositionalVals.push_back(std::make_pair(argv[i],i));
// All of the positional arguments have been fulfulled, give the rest to
@ -549,13 +551,13 @@ void cl::ParseCommandLineOptions(int argc, char **argv,
continue; // We are done!
}
} else { // We start with a '-', must be an argument...
} else { // We start with a '-', must be an argument.
ArgName = argv[i]+1;
Handler = LookupOption(ArgName, Value, Opts);
// Check to see if this "option" is really a prefixed or grouped argument.
if (Handler == 0) {
std::string RealName(ArgName);
StringRef RealName(ArgName);
if (RealName.size() > 1) {
size_t Length = 0;
Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping,
@ -567,29 +569,28 @@ void cl::ParseCommandLineOptions(int argc, char **argv,
//
if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
Value = ArgName+Length;
assert(Opts.find(std::string(ArgName, Value)) != Opts.end() &&
Opts.find(std::string(ArgName, Value))->second == PGOpt);
assert(Opts.count(StringRef(ArgName, Length)) &&
Opts[StringRef(ArgName, Length)] == PGOpt);
Handler = PGOpt;
} else if (PGOpt) {
// This must be a grouped option... handle them now.
assert(isGrouping(PGOpt) && "Broken getOptionPred!");
do {
// Move current arg name out of RealName into RealArgName...
std::string RealArgName(RealName.begin(),
RealName.begin() + Length);
RealName.erase(RealName.begin(), RealName.begin() + Length);
// Move current arg name out of RealName into RealArgName.
StringRef RealArgName = RealName.substr(0, Length);
RealName = RealName.substr(Length);
// Because ValueRequired is an invalid flag for grouped arguments,
// we don't need to pass argc/argv in...
// we don't need to pass argc/argv in.
//
assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
"Option can not be cl::Grouping AND cl::ValueRequired!");
int Dummy;
ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(),
ErrorParsing |= ProvideOption(PGOpt, RealArgName,
0, 0, 0, Dummy);
// Get the next grouping option...
// Get the next grouping option.
PGOpt = getOptionPred(RealName, Length, isGrouping, Opts);
} while (PGOpt && Length != RealName.size());
@ -760,9 +761,9 @@ void cl::ParseCommandLineOptions(int argc, char **argv,
// Option Base class implementation
//
bool Option::error(const Twine &Message, const char *ArgName) {
if (ArgName == 0) ArgName = ArgStr;
if (ArgName[0] == 0)
bool Option::error(const Twine &Message, StringRef ArgName) {
if (ArgName.data() == 0) ArgName = ArgStr;
if (ArgName.empty())
errs() << HelpStr; // Be nice for positional arguments
else
errs() << ProgramName << ": for the -" << ArgName;
@ -771,7 +772,7 @@ bool Option::error(const Twine &Message, const char *ArgName) {
return true;
}
bool Option::addOccurrence(unsigned pos, const char *ArgName,
bool Option::addOccurrence(unsigned pos, StringRef ArgName,
StringRef Value, bool MultiArg) {
if (!MultiArg)
NumOccurrences++; // Increment the number of times we have been seen
@ -855,7 +856,7 @@ void basic_parser_impl::printOptionInfo(const Option &O,
// parser<bool> implementation
//
bool parser<bool>::parse(Option &O, const char *ArgName,
bool parser<bool>::parse(Option &O, StringRef ArgName,
StringRef Arg, bool &Value) {
if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
Arg == "1") {
@ -873,7 +874,7 @@ bool parser<bool>::parse(Option &O, const char *ArgName,
// parser<boolOrDefault> implementation
//
bool parser<boolOrDefault>::parse(Option &O, const char *ArgName,
bool parser<boolOrDefault>::parse(Option &O, StringRef ArgName,
StringRef Arg, boolOrDefault &Value) {
if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
Arg == "1") {
@ -891,7 +892,7 @@ bool parser<boolOrDefault>::parse(Option &O, const char *ArgName,
// parser<int> implementation
//
bool parser<int>::parse(Option &O, const char *ArgName,
bool parser<int>::parse(Option &O, StringRef ArgName,
StringRef Arg, int &Value) {
if (Arg.getAsInteger(0, Value))
return O.error("'" + Arg + "' value invalid for integer argument!");
@ -900,7 +901,7 @@ bool parser<int>::parse(Option &O, const char *ArgName,
// parser<unsigned> implementation
//
bool parser<unsigned>::parse(Option &O, const char *ArgName,
bool parser<unsigned>::parse(Option &O, StringRef ArgName,
StringRef Arg, unsigned &Value) {
if (Arg.getAsInteger(0, Value))
@ -920,12 +921,12 @@ static bool parseDouble(Option &O, StringRef Arg, double &Value) {
return false;
}
bool parser<double>::parse(Option &O, const char *AN,
bool parser<double>::parse(Option &O, StringRef ArgName,
StringRef Arg, double &Val) {
return parseDouble(O, Arg, Val);
}
bool parser<float>::parse(Option &O, const char *AN,
bool parser<float>::parse(Option &O, StringRef ArgName,
StringRef Arg, float &Val) {
double dVal;
if (parseDouble(O, Arg, dVal))