[llvm-commits] [llvm] r82318 - in /llvm/trunk: include/llvm/Support/CommandLine.h lib/Support/CommandLine.cpp
Chris Lattner
sabre at nondot.org
Sat Sep 19 11:55:05 PDT 2009
Author: lattner
Date: Sat Sep 19 13:55:05 2009
New Revision: 82318
URL: http://llvm.org/viewvc/llvm-project?rev=82318&view=rev
Log:
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.
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=82318&r1=82317&r2=82318&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/CommandLine.h (original)
+++ llvm/trunk/include/llvm/Support/CommandLine.h Sat Sep 19 13:55:05 2009
@@ -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 @@
// 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 @@
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 @@
// 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 @@
}
// 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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
}
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 @@
}
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 @@
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...
Modified: llvm/trunk/lib/Support/CommandLine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CommandLine.cpp?rev=82318&r1=82317&r2=82318&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CommandLine.cpp (original)
+++ llvm/trunk/lib/Support/CommandLine.cpp Sat Sep 19 13:55:05 2009
@@ -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 @@
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 @@
}
// 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;
-
- if (Value) {
- if (Handler->addOccurrence(i, ArgName, Value, MultiArg))
- return true;
- --NumAdditionalVals;
- MultiArg = true;
- }
- while (NumAdditionalVals > 0) {
+ // If it is, run the handle several times.
+ bool MultiArg = false;
- 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 @@
// 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::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 @@
// 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") {
+ return false;
+ }
+
+ 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;
+ 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") {
+ return false;
+ }
+ 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;
+
+ 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 @@
// 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 @@
// 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 @@
}
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;
More information about the llvm-commits
mailing list