[llvm-commits] [llvm] r82345 - in /llvm/trunk: include/llvm/Support/CommandLine.h lib/Support/CommandLine.cpp
Chris Lattner
sabre at nondot.org
Sat Sep 19 17:40:49 PDT 2009
Author: lattner
Date: Sat Sep 19 19:40:49 2009
New Revision: 82345
URL: http://llvm.org/viewvc/llvm-project?rev=82345&view=rev
Log:
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.
Modified:
llvm/trunk/include/llvm/Support/CommandLine.h
llvm/trunk/lib/Support/CommandLine.cpp
Modified: llvm/trunk/include/llvm/Support/CommandLine.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/CommandLine.h?rev=82345&r1=82344&r2=82345&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/CommandLine.h (original)
+++ llvm/trunk/include/llvm/Support/CommandLine.h Sat Sep 19 19:40:49 2009
@@ -142,7 +142,7 @@
// 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 @@
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 @@
}
// 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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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);
}
Modified: llvm/trunk/lib/Support/CommandLine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CommandLine.cpp?rev=82345&r1=82344&r2=82345&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CommandLine.cpp (original)
+++ llvm/trunk/lib/Support/CommandLine.cpp Sat Sep 19 19:40:49 2009
@@ -171,7 +171,7 @@
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 @@
// 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 @@
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 @@
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 @@
//
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 @@
// 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 @@
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 @@
// 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 @@
// 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 @@
// 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 @@
// 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 @@
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))
More information about the llvm-commits
mailing list