[llvm] r283007 - Use StringRef in CommandLine Options handling (NFC)
Mehdi Amini via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 30 20:43:21 PDT 2016
Author: mehdi_amini
Date: Fri Sep 30 22:43:20 2016
New Revision: 283007
URL: http://llvm.org/viewvc/llvm-project?rev=283007&view=rev
Log:
Use StringRef in CommandLine Options handling (NFC)
Modified:
llvm/trunk/include/llvm/CodeGen/MachinePassRegistry.h
llvm/trunk/include/llvm/Support/CommandLine.h
llvm/trunk/include/llvm/Support/Options.h
llvm/trunk/lib/Support/CommandLine.cpp
llvm/trunk/tools/llvm-stress/llvm-stress.cpp
llvm/trunk/unittests/Support/CommandLineTest.cpp
Modified: llvm/trunk/include/llvm/CodeGen/MachinePassRegistry.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachinePassRegistry.h?rev=283007&r1=283006&r2=283007&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/MachinePassRegistry.h (original)
+++ llvm/trunk/include/llvm/CodeGen/MachinePassRegistry.h Fri Sep 30 22:43:20 2016
@@ -132,9 +132,9 @@ public:
// Add existing passes to option.
for (RegistryClass *Node = RegistryClass::getList();
Node; Node = Node->getNext()) {
- this->addLiteralOption(Node->getName().data(),
+ this->addLiteralOption(Node->getName(),
(typename RegistryClass::FunctionPassCtor)Node->getCtor(),
- Node->getDescription().data());
+ Node->getDescription());
}
// Make sure we listen for list changes.
@@ -144,12 +144,10 @@ public:
// Implement the MachinePassRegistryListener callbacks.
//
void NotifyAdd(StringRef N, MachinePassCtor C, StringRef D) override {
- this->addLiteralOption(N.data(),
- (typename RegistryClass::FunctionPassCtor)C,
- D.data());
+ this->addLiteralOption(N, (typename RegistryClass::FunctionPassCtor)C, D);
}
void NotifyRemove(StringRef N) override {
- this->removeLiteralOption(N.data());
+ this->removeLiteralOption(N);
}
};
Modified: llvm/trunk/include/llvm/Support/CommandLine.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/CommandLine.h?rev=283007&r1=283006&r2=283007&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/CommandLine.h (original)
+++ llvm/trunk/include/llvm/Support/CommandLine.h Fri Sep 30 22:43:20 2016
@@ -47,7 +47,7 @@ namespace cl {
// ParseCommandLineOptions - Command line option processing entry point.
//
bool ParseCommandLineOptions(int argc, const char *const *argv,
- const char *Overview = nullptr,
+ StringRef Overview = "",
bool IgnoreErrors = false);
//===----------------------------------------------------------------------===//
@@ -55,7 +55,7 @@ bool ParseCommandLineOptions(int argc, c
// entry point.
//
void ParseEnvironmentOptions(const char *progName, const char *envvar,
- const char *Overview = nullptr);
+ const char *Overview = "");
///===---------------------------------------------------------------------===//
/// SetVersionPrinter - Override the default (LLVM specific) version printer
@@ -89,7 +89,7 @@ class Option;
///
/// Literal options are used by some parsers to register special option values.
/// This is how the PassNameParser registers pass names for opt.
-void AddLiteralOption(Option &O, const char *Name);
+void AddLiteralOption(Option &O, StringRef Name);
//===----------------------------------------------------------------------===//
// Flags permitted to be passed to command line arguments
@@ -157,18 +157,18 @@ enum MiscFlags { // Miscella
//
class OptionCategory {
private:
- const char *const Name;
- const char *const Description;
+ StringRef const Name;
+ StringRef const Description;
void registerCategory();
public:
- OptionCategory(const char *const Name,
- const char *const Description = nullptr)
+ OptionCategory(StringRef const Name,
+ StringRef const Description = "")
: Name(Name), Description(Description) {
registerCategory();
}
- const char *getName() const { return Name; }
- const char *getDescription() const { return Description; }
+ StringRef getName() const { return Name; }
+ StringRef getDescription() const { return Description; }
};
// The general Option Category (used as default category).
@@ -179,17 +179,17 @@ extern OptionCategory GeneralCategory;
//
class SubCommand {
private:
- const char *const Name = nullptr;
- const char *const Description = nullptr;
+ StringRef Name = "";
+ StringRef Description = "";
protected:
void registerSubCommand();
void unregisterSubCommand();
public:
- SubCommand(const char *const Name, const char *const Description = nullptr)
+ SubCommand(StringRef Name, StringRef Description = "")
: Name(Name), Description(Description) {
- registerSubCommand();
+ registerSubCommand();
}
SubCommand() {}
@@ -197,8 +197,8 @@ public:
operator bool() const;
- const char *getName() const { return Name; }
- const char *getDescription() const { return Description; }
+ StringRef getName() const { return Name; }
+ StringRef getDescription() const { return Description; }
SmallVector<Option *, 4> PositionalOpts;
SmallVector<Option *, 4> SinkOpts;
@@ -353,16 +353,16 @@ public:
// desc - Modifier to set the description shown in the -help output...
struct desc {
- const char *Desc;
- desc(const char *Str) : Desc(Str) {}
+ StringRef Desc;
+ desc(StringRef Str) : Desc(Str) {}
void apply(Option &O) const { O.setDescription(Desc); }
};
// value_desc - Modifier to set the value description shown in the -help
// output...
struct value_desc {
- const char *Desc;
- value_desc(const char *Str) : Desc(Str) {}
+ StringRef Desc;
+ value_desc(StringRef Str) : Desc(Str) {}
void apply(Option &O) const { O.setValueStr(Desc); }
};
@@ -569,20 +569,20 @@ template <class DataType> class ValuesCl
// Use a vector instead of a map, because the lists should be short,
// the overhead is less, and most importantly, it keeps them in the order
// inserted so we can print our option out nicely.
- SmallVector<std::pair<const char *, std::pair<int, const char *>>, 4> Values;
+ SmallVector<std::pair<StringRef , std::pair<int, StringRef >>, 4> Values;
void processValues(va_list Vals);
public:
- ValuesClass(const char *EnumName, DataType Val, const char *Desc,
+ ValuesClass(StringRef EnumName, DataType Val, StringRef Desc,
va_list ValueArgs) {
// Insert the first value, which is required.
Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
// Process the varargs portion of the values...
- while (const char *enumName = va_arg(ValueArgs, const char *)) {
+ while (const char *enumName = va_arg(ValueArgs, const char * )) {
DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
- const char *EnumDesc = va_arg(ValueArgs, const char *);
- Values.push_back(std::make_pair(enumName, // Add value to value map
+ auto EnumDesc = StringRef(va_arg(ValueArgs, const char * ));
+ Values.push_back(std::make_pair(StringRef(enumName), // Add value to value map
std::make_pair(EnumVal, EnumDesc)));
}
}
@@ -596,7 +596,7 @@ public:
template <class DataType>
ValuesClass<DataType> LLVM_END_WITH_NULL
-values(const char *Arg, DataType Val, const char *Desc, ...) {
+values(StringRef Arg, DataType Val, StringRef Desc, ...) {
va_list ValueArgs;
va_start(ValueArgs, Desc);
ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
@@ -620,10 +620,10 @@ class generic_parser_base {
protected:
class GenericOptionInfo {
public:
- GenericOptionInfo(const char *name, const char *helpStr)
+ GenericOptionInfo(StringRef name, StringRef helpStr)
: Name(name), HelpStr(helpStr) {}
- const char *Name;
- const char *HelpStr;
+ StringRef Name;
+ StringRef HelpStr;
};
public:
@@ -637,10 +637,10 @@ public:
virtual unsigned getNumOptions() const = 0;
// getOption - Return option name N.
- virtual const char *getOption(unsigned N) const = 0;
+ virtual StringRef getOption(unsigned N) const = 0;
// getDescription - Return description N
- virtual const char *getDescription(unsigned N) const = 0;
+ virtual StringRef getDescription(unsigned N) const = 0;
// Return the width of the option tag for printing...
virtual size_t getOptionWidth(const Option &O) const;
@@ -699,7 +699,7 @@ public:
// findOption - Return the option number corresponding to the specified
// argument string. If the option is not found, getNumOptions() is returned.
//
- unsigned findOption(const char *Name);
+ unsigned findOption(StringRef Name);
protected:
Option &Owner;
@@ -715,7 +715,7 @@ template <class DataType> class parser :
protected:
class OptionInfo : public GenericOptionInfo {
public:
- OptionInfo(const char *name, DataType v, const char *helpStr)
+ OptionInfo(StringRef name, DataType v, StringRef helpStr)
: GenericOptionInfo(name, helpStr), V(v) {}
OptionValue<DataType> V;
};
@@ -727,8 +727,8 @@ public:
// Implement virtual functions needed by generic_parser_base
unsigned getNumOptions() const override { return unsigned(Values.size()); }
- const char *getOption(unsigned N) const override { return Values[N].Name; }
- const char *getDescription(unsigned N) const override {
+ StringRef getOption(unsigned N) const override { return Values[N].Name; }
+ StringRef getDescription(unsigned N) const override {
return Values[N].HelpStr;
}
@@ -757,7 +757,7 @@ public:
/// addLiteralOption - Add an entry to the mapping table.
///
template <class DT>
- void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
+ void addLiteralOption(StringRef Name, const DT &V, StringRef HelpStr) {
assert(findOption(Name) == Values.size() && "Option already exists!");
OptionInfo X(Name, static_cast<DataType>(V), HelpStr);
Values.push_back(X);
@@ -766,7 +766,7 @@ public:
/// removeLiteralOption - Remove the specified option.
///
- void removeLiteralOption(const char *Name) {
+ void removeLiteralOption(StringRef Name) {
unsigned N = findOption(Name);
assert(N != Values.size() && "Option not found!");
Values.erase(Values.begin() + N);
@@ -802,7 +802,7 @@ public:
void printOptionNoValue(const Option &O, size_t GlobalWidth) const;
// getValueName - Overload in subclass to provide a better default value.
- virtual const char *getValueName() const { return "value"; }
+ virtual StringRef getValueName() const { return "value"; }
// An out-of-line virtual method to provide a 'home' for this class.
virtual void anchor();
@@ -844,7 +844,7 @@ public:
}
// getValueName - Do not print =<value> at all.
- const char *getValueName() const override { return nullptr; }
+ StringRef getValueName() const override { return StringRef(); }
void printOptionDiff(const Option &O, bool V, OptVal Default,
size_t GlobalWidth) const;
@@ -870,7 +870,7 @@ public:
}
// getValueName - Do not print =<value> at all.
- const char *getValueName() const override { return nullptr; }
+ StringRef getValueName() const override { return StringRef(); }
void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default,
size_t GlobalWidth) const;
@@ -892,7 +892,7 @@ public:
bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
// getValueName - Overload in subclass to provide a better default value.
- const char *getValueName() const override { return "int"; }
+ StringRef getValueName() const override { return "int"; }
void printOptionDiff(const Option &O, int V, OptVal Default,
size_t GlobalWidth) const;
@@ -914,7 +914,7 @@ public:
bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
// getValueName - Overload in subclass to provide a better default value.
- const char *getValueName() const override { return "uint"; }
+ StringRef getValueName() const override { return "uint"; }
void printOptionDiff(const Option &O, unsigned V, OptVal Default,
size_t GlobalWidth) const;
@@ -939,7 +939,7 @@ public:
unsigned long long &Val);
// getValueName - Overload in subclass to provide a better default value.
- const char *getValueName() const override { return "uint"; }
+ StringRef getValueName() const override { return "uint"; }
void printOptionDiff(const Option &O, unsigned long long V, OptVal Default,
size_t GlobalWidth) const;
@@ -961,7 +961,7 @@ public:
bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
// getValueName - Overload in subclass to provide a better default value.
- const char *getValueName() const override { return "number"; }
+ StringRef getValueName() const override { return "number"; }
void printOptionDiff(const Option &O, double V, OptVal Default,
size_t GlobalWidth) const;
@@ -983,7 +983,7 @@ public:
bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
// getValueName - Overload in subclass to provide a better default value.
- const char *getValueName() const override { return "number"; }
+ StringRef getValueName() const override { return "number"; }
void printOptionDiff(const Option &O, float V, OptVal Default,
size_t GlobalWidth) const;
@@ -1008,7 +1008,7 @@ public:
}
// getValueName - Overload in subclass to provide a better default value.
- const char *getValueName() const override { return "string"; }
+ StringRef getValueName() const override { return "string"; }
void printOptionDiff(const Option &O, StringRef V, const OptVal &Default,
size_t GlobalWidth) const;
@@ -1033,7 +1033,7 @@ public:
}
// getValueName - Overload in subclass to provide a better default value.
- const char *getValueName() const override { return "char"; }
+ StringRef getValueName() const override { return "char"; }
void printOptionDiff(const Option &O, char V, OptVal Default,
size_t GlobalWidth) const;
@@ -1101,17 +1101,17 @@ template <class Mod> struct applicator {
// Handle const char* as a special case...
template <unsigned n> struct applicator<char[n]> {
- template <class Opt> static void opt(const char *Str, Opt &O) {
+ template <class Opt> static void opt(StringRef Str, Opt &O) {
O.setArgStr(Str);
}
};
template <unsigned n> struct applicator<const char[n]> {
- template <class Opt> static void opt(const char *Str, Opt &O) {
+ template <class Opt> static void opt(StringRef Str, Opt &O) {
O.setArgStr(Str);
}
};
-template <> struct applicator<const char *> {
- template <class Opt> static void opt(const char *Str, Opt &O) {
+template <> struct applicator<StringRef > {
+ template <class Opt> static void opt(StringRef Str, Opt &O) {
O.setArgStr(Str);
}
};
@@ -1688,8 +1688,8 @@ struct aliasopt {
// printed to stderr at the end of the regular help, just before
// exit is called.
struct extrahelp {
- const char *morehelp;
- explicit extrahelp(const char *help);
+ StringRef morehelp;
+ explicit extrahelp(StringRef help);
};
void PrintVersionMessage();
Modified: llvm/trunk/include/llvm/Support/Options.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Options.h?rev=283007&r1=283006&r2=283007&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Options.h (original)
+++ llvm/trunk/include/llvm/Support/Options.h Fri Sep 30 22:43:20 2016
@@ -93,7 +93,7 @@ public:
/// option stores (\p ValT), the class that will read the option (\p Base),
/// and the member that the class will store the data into (\p Mem).
template <typename ValT, typename Base, ValT(Base::*Mem)>
- static void registerOption(const char *ArgStr, const char *Desc,
+ static void registerOption(StringRef ArgStr, StringRef Desc,
const ValT &InitValue) {
cl::opt<ValT> *Option = new cl::opt<ValT>(ArgStr, cl::desc(Desc),
cl::Hidden, cl::init(InitValue));
Modified: llvm/trunk/lib/Support/CommandLine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CommandLine.cpp?rev=283007&r1=283006&r2=283007&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CommandLine.cpp (original)
+++ llvm/trunk/lib/Support/CommandLine.cpp Fri Sep 30 22:43:20 2016
@@ -92,10 +92,10 @@ public:
// Globals for name and overview of program. Program name is not a string to
// avoid static ctor/dtor issues.
std::string ProgramName;
- const char *ProgramOverview;
+ StringRef ProgramOverview;
// This collects additional help to be printed.
- std::vector<const char *> MoreHelp;
+ std::vector<StringRef> MoreHelp;
// This collects the different option categories that have been registered.
SmallPtrSet<OptionCategory *, 16> RegisteredOptionCategories;
@@ -103,7 +103,7 @@ public:
// This collects the different subcommands that have been registered.
SmallPtrSet<SubCommand *, 4> RegisteredSubCommands;
- CommandLineParser() : ProgramOverview(nullptr), ActiveSubCommand(nullptr) {
+ CommandLineParser() : ActiveSubCommand(nullptr) {
registerSubCommand(&*TopLevelSubCommand);
registerSubCommand(&*AllSubCommands);
}
@@ -111,9 +111,9 @@ public:
void ResetAllOptionOccurrences();
bool ParseCommandLineOptions(int argc, const char *const *argv,
- const char *Overview, bool IgnoreErrors);
+ StringRef Overview, bool IgnoreErrors);
- void addLiteralOption(Option &Opt, SubCommand *SC, const char *Name) {
+ void addLiteralOption(Option &Opt, SubCommand *SC, StringRef Name) {
if (Opt.hasArgStr())
return;
if (!SC->OptionsMap.insert(std::make_pair(Name, &Opt)).second) {
@@ -133,7 +133,7 @@ public:
}
}
- void addLiteralOption(Option &Opt, const char *Name) {
+ void addLiteralOption(Option &Opt, StringRef Name) {
if (Opt.Subs.empty())
addLiteralOption(Opt, &*TopLevelSubCommand, Name);
else {
@@ -285,7 +285,7 @@ public:
void registerSubCommand(SubCommand *sub) {
assert(count_if(RegisteredSubCommands,
[sub](const SubCommand *Sub) {
- return (sub->getName() != nullptr) &&
+ return (!sub->getName().empty()) &&
(Sub->getName() == sub->getName());
}) == 0 &&
"Duplicate subcommands");
@@ -300,7 +300,7 @@ public:
O->hasArgStr())
addOption(O, sub);
else
- addLiteralOption(*O, sub, E.first().str().c_str());
+ addLiteralOption(*O, sub, E.first());
}
}
}
@@ -312,7 +312,7 @@ public:
void reset() {
ActiveSubCommand = nullptr;
ProgramName.clear();
- ProgramOverview = nullptr;
+ ProgramOverview = StringRef();
MoreHelp.clear();
RegisteredOptionCategories.clear();
@@ -330,18 +330,18 @@ private:
SubCommand *ActiveSubCommand;
Option *LookupOption(SubCommand &Sub, StringRef &Arg, StringRef &Value);
- SubCommand *LookupSubCommand(const char *Name);
+ SubCommand *LookupSubCommand(StringRef Name);
};
} // namespace
static ManagedStatic<CommandLineParser> GlobalParser;
-void cl::AddLiteralOption(Option &O, const char *Name) {
+void cl::AddLiteralOption(Option &O, StringRef Name) {
GlobalParser->addLiteralOption(O, Name);
}
-extrahelp::extrahelp(const char *Help) : morehelp(Help) {
+extrahelp::extrahelp(StringRef Help) : morehelp(Help) {
GlobalParser->MoreHelp.push_back(Help);
}
@@ -428,13 +428,13 @@ Option *CommandLineParser::LookupOption(
return I->second;
}
-SubCommand *CommandLineParser::LookupSubCommand(const char *Name) {
- if (Name == nullptr)
+SubCommand *CommandLineParser::LookupSubCommand(StringRef Name) {
+ if (Name.empty())
return &*TopLevelSubCommand;
for (auto S : RegisteredSubCommands) {
if (S == &*AllSubCommands)
continue;
- if (S->getName() == nullptr)
+ if (S->getName().empty())
continue;
if (StringRef(S->getName()) == StringRef(Name))
@@ -534,7 +534,7 @@ static inline bool ProvideOption(Option
return Handler->error("requires a value!");
// Steal the next argument, like for '-o filename'
assert(argv && "null check");
- Value = argv[++i];
+ Value = StringRef(argv[++i]);
}
break;
case ValueDisallowed:
@@ -568,7 +568,7 @@ static inline bool ProvideOption(Option
if (i + 1 >= argc)
return Handler->error("not enough values!");
assert(argv && "null check");
- Value = argv[++i];
+ Value = StringRef(argv[++i]);
if (CommaSeparateAndAddOccurrence(Handler, i, ArgName, Value, MultiArg))
return true;
@@ -726,7 +726,7 @@ void cl::TokenizeGNUCommandLine(StringRe
// End the token if this is whitespace.
if (isWhitespace(Src[I])) {
if (!Token.empty())
- NewArgv.push_back(Saver.save(Token.c_str()));
+ NewArgv.push_back(Saver.save(StringRef(Token)));
Token.clear();
continue;
}
@@ -737,7 +737,7 @@ void cl::TokenizeGNUCommandLine(StringRe
// Append the last token after hitting EOF with no whitespace.
if (!Token.empty())
- NewArgv.push_back(Saver.save(Token.c_str()));
+ NewArgv.push_back(Saver.save(StringRef(Token)));
// Mark the end of response files
if (MarkEOLs)
NewArgv.push_back(nullptr);
@@ -818,7 +818,7 @@ void cl::TokenizeWindowsCommandLine(Stri
if (State == UNQUOTED) {
// Whitespace means the end of the token.
if (isWhitespace(Src[I])) {
- NewArgv.push_back(Saver.save(Token.c_str()));
+ NewArgv.push_back(Saver.save(StringRef(Token)));
Token.clear();
State = INIT;
// Mark the end of lines in response files
@@ -853,7 +853,7 @@ void cl::TokenizeWindowsCommandLine(Stri
}
// Append the last token after hitting EOF with no whitespace.
if (!Token.empty())
- NewArgv.push_back(Saver.save(Token.c_str()));
+ NewArgv.push_back(Saver.save(StringRef(Token)));
// Mark the end of response files
if (MarkEOLs)
NewArgv.push_back(nullptr);
@@ -953,7 +953,7 @@ void cl::ParseEnvironmentOptions(const c
assert(envVar && "Environment variable name missing");
// Get the environment variable they want us to parse options out of.
- llvm::Optional<std::string> envValue = sys::Process::GetEnv(envVar);
+ llvm::Optional<std::string> envValue = sys::Process::GetEnv(StringRef(envVar));
if (!envValue)
return;
@@ -968,11 +968,11 @@ void cl::ParseEnvironmentOptions(const c
// and hand it off to ParseCommandLineOptions().
TokenizeGNUCommandLine(*envValue, Saver, newArgv);
int newArgc = static_cast<int>(newArgv.size());
- ParseCommandLineOptions(newArgc, &newArgv[0], Overview);
+ ParseCommandLineOptions(newArgc, &newArgv[0], StringRef(Overview));
}
bool cl::ParseCommandLineOptions(int argc, const char *const *argv,
- const char *Overview, bool IgnoreErrors) {
+ StringRef Overview, bool IgnoreErrors) {
return GlobalParser->ParseCommandLineOptions(argc, argv, Overview,
IgnoreErrors);
}
@@ -988,7 +988,7 @@ void CommandLineParser::ResetAllOptionOc
bool CommandLineParser::ParseCommandLineOptions(int argc,
const char *const *argv,
- const char *Overview,
+ StringRef Overview,
bool IgnoreErrors) {
assert(hasOptions() && "No options specified!");
@@ -1001,7 +1001,7 @@ bool CommandLineParser::ParseCommandLine
argc = static_cast<int>(newArgv.size());
// Copy the program name into ProgName, making sure not to overflow it.
- ProgramName = sys::path::filename(argv[0]);
+ ProgramName = sys::path::filename(StringRef(argv[0]));
ProgramOverview = Overview;
bool ErrorParsing = false;
@@ -1017,7 +1017,7 @@ bool CommandLineParser::ParseCommandLine
if (argc >= 2 && argv[FirstArg][0] != '-') {
// If the first argument specifies a valid subcommand, start processing
// options from the second argument.
- ChosenSubCommand = LookupSubCommand(argv[FirstArg]);
+ ChosenSubCommand = LookupSubCommand(StringRef(argv[FirstArg]));
if (ChosenSubCommand != &*TopLevelSubCommand)
FirstArg = 2;
}
@@ -1098,19 +1098,19 @@ bool CommandLineParser::ParseCommandLine
if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
// Positional argument!
if (ActivePositionalArg) {
- ProvidePositionalOption(ActivePositionalArg, argv[i], i);
+ ProvidePositionalOption(ActivePositionalArg, StringRef(argv[i]), i);
continue; // We are done!
}
if (!PositionalOpts.empty()) {
- PositionalVals.push_back(std::make_pair(argv[i], i));
+ PositionalVals.push_back(std::make_pair(StringRef(argv[i]), i));
// All of the positional arguments have been fulfulled, give the rest to
// the consume after option... if it's specified...
//
if (PositionalVals.size() >= NumPositionalRequired && ConsumeAfterOpt) {
for (++i; i < argc; ++i)
- PositionalVals.push_back(std::make_pair(argv[i], i));
+ PositionalVals.push_back(std::make_pair(StringRef(argv[i]), i));
break; // Handle outside of the argument processing loop...
}
@@ -1126,19 +1126,19 @@ bool CommandLineParser::ParseCommandLine
// If there is a positional argument eating options, check to see if this
// option is another positional argument. If so, treat it as an argument,
// otherwise feed it to the eating positional.
- ArgName = argv[i] + 1;
+ ArgName = StringRef(argv[i] + 1);
// Eat leading dashes.
while (!ArgName.empty() && ArgName[0] == '-')
ArgName = ArgName.substr(1);
Handler = LookupOption(*ChosenSubCommand, ArgName, Value);
if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
- ProvidePositionalOption(ActivePositionalArg, argv[i], i);
+ ProvidePositionalOption(ActivePositionalArg, StringRef(argv[i]), i);
continue; // We are done!
}
} else { // We start with a '-', must be an argument.
- ArgName = argv[i] + 1;
+ ArgName = StringRef(argv[i] + 1);
// Eat leading dashes.
while (!ArgName.empty() && ArgName[0] == '-')
ArgName = ArgName.substr(1);
@@ -1175,7 +1175,7 @@ bool CommandLineParser::ParseCommandLine
for (SmallVectorImpl<Option *>::iterator I = SinkOpts.begin(),
E = SinkOpts.end();
I != E; ++I)
- (*I)->addOccurrence(i, "", argv[i]);
+ (*I)->addOccurrence(i, "", StringRef(argv[i]));
}
continue;
}
@@ -1389,7 +1389,8 @@ void alias::printOptionInfo(size_t Globa
// Return the width of the option tag for printing...
size_t basic_parser_impl::getOptionWidth(const Option &O) const {
size_t Len = O.ArgStr.size();
- if (const char *ValName = getValueName())
+ auto ValName = getValueName();
+ if (!ValName.empty())
Len += getValueStr(O, ValName).size() + 3;
return Len + 6;
@@ -1402,7 +1403,8 @@ void basic_parser_impl::printOptionInfo(
size_t GlobalWidth) const {
outs() << " -" << O.ArgStr;
- if (const char *ValName = getValueName())
+ auto ValName = getValueName();
+ if (!ValName.empty())
outs() << "=<" << getValueStr(O, ValName) << '>';
printHelpStr(O.HelpStr, GlobalWidth, getOptionWidth(O));
@@ -1512,11 +1514,11 @@ bool parser<float>::parse(Option &O, Str
// findOption - Return the option number corresponding to the specified
// argument string. If the option is not found, getNumOptions() is returned.
//
-unsigned generic_parser_base::findOption(const char *Name) {
+unsigned generic_parser_base::findOption(StringRef Name) {
unsigned e = getNumOptions();
for (unsigned i = 0; i != e; ++i) {
- if (strcmp(getOption(i), Name) == 0)
+ if (getOption(i) == Name)
return i;
}
return e;
@@ -1527,12 +1529,12 @@ size_t generic_parser_base::getOptionWid
if (O.hasArgStr()) {
size_t Size = O.ArgStr.size() + 6;
for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
- Size = std::max(Size, std::strlen(getOption(i)) + 8);
+ Size = std::max(Size, getOption(i).size() + 8);
return Size;
} else {
size_t BaseSize = 0;
for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
- BaseSize = std::max(BaseSize, std::strlen(getOption(i)) + 8);
+ BaseSize = std::max(BaseSize, getOption(i).size() + 8);
return BaseSize;
}
}
@@ -1547,7 +1549,7 @@ void generic_parser_base::printOptionInf
printHelpStr(O.HelpStr, GlobalWidth, O.ArgStr.size() + 6);
for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
- size_t NumSpaces = GlobalWidth - strlen(getOption(i)) - 8;
+ size_t NumSpaces = GlobalWidth - getOption(i).size() - 8;
outs() << " =" << getOption(i);
outs().indent(NumSpaces) << " - " << getDescription(i) << '\n';
}
@@ -1555,9 +1557,9 @@ void generic_parser_base::printOptionInf
if (!O.HelpStr.empty())
outs() << " " << O.HelpStr << '\n';
for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
- const char *Option = getOption(i);
+ auto Option = getOption(i);
outs() << " -" << Option;
- printHelpStr(getDescription(i), GlobalWidth, std::strlen(Option) + 8);
+ printHelpStr(getDescription(i), GlobalWidth, Option.size() + 8);
}
}
}
@@ -1579,7 +1581,7 @@ void generic_parser_base::printGenericOp
continue;
outs() << "= " << getOption(i);
- size_t L = std::strlen(getOption(i));
+ size_t L = getOption(i).size();
size_t NumSpaces = MaxOptWidth > L ? MaxOptWidth - L : 0;
outs().indent(NumSpaces) << " (default: ";
for (unsigned j = 0; j != NumOpts; ++j) {
@@ -1692,9 +1694,9 @@ static void
sortSubCommands(const SmallPtrSetImpl<SubCommand *> &SubMap,
SmallVectorImpl<std::pair<const char *, SubCommand *>> &Subs) {
for (const auto &S : SubMap) {
- if (S->getName() == nullptr)
+ if (S->getName().empty())
continue;
- Subs.push_back(std::make_pair(S->getName(), S));
+ Subs.push_back(std::make_pair(S->getName().data(), S));
}
array_pod_sort(Subs.begin(), Subs.end(), SubNameCompare);
}
@@ -1717,7 +1719,7 @@ protected:
void printSubCommands(StrSubCommandPairVector &Subs, size_t MaxSubLen) {
for (const auto &S : Subs) {
outs() << " " << S.first;
- if (S.second->getDescription()) {
+ if (!S.second->getDescription().empty()) {
outs().indent(MaxSubLen - strlen(S.first));
outs() << " - " << S.second->getDescription();
}
@@ -1745,14 +1747,14 @@ public:
StrSubCommandPairVector Subs;
sortSubCommands(GlobalParser->RegisteredSubCommands, Subs);
- if (GlobalParser->ProgramOverview)
+ if (!GlobalParser->ProgramOverview.empty())
outs() << "OVERVIEW: " << GlobalParser->ProgramOverview << "\n";
if (Sub == &*TopLevelSubCommand)
outs() << "USAGE: " << GlobalParser->ProgramName
<< " [subcommand] [options]";
else {
- if (Sub->getDescription() != nullptr) {
+ if (!Sub->getDescription().empty()) {
outs() << "SUBCOMMAND '" << Sub->getName()
<< "': " << Sub->getDescription() << "\n\n";
}
@@ -1814,7 +1816,7 @@ public:
// ordered before B's name. It returns a value greater equal zero otherwise.
static int OptionCategoryCompare(OptionCategory *const *A,
OptionCategory *const *B) {
- return strcmp((*A)->getName(), (*B)->getName());
+ return (*A)->getName() == (*B)->getName();
}
// Make sure we inherit our base class's operator=()
@@ -1871,7 +1873,7 @@ protected:
outs() << (*Category)->getName() << ":\n";
// Check if description is set.
- if ((*Category)->getDescription() != nullptr)
+ if (!(*Category)->getDescription().empty())
outs() << (*Category)->getDescription() << "\n\n";
else
outs() << "\n";
@@ -2132,5 +2134,5 @@ void cl::ResetAllOptionOccurrences() {
void LLVMParseCommandLineOptions(int argc, const char *const *argv,
const char *Overview) {
- llvm::cl::ParseCommandLineOptions(argc, argv, Overview, true);
+ llvm::cl::ParseCommandLineOptions(argc, argv, StringRef(Overview), true);
}
Modified: llvm/trunk/tools/llvm-stress/llvm-stress.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-stress/llvm-stress.cpp?rev=283007&r1=283006&r2=283007&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-stress/llvm-stress.cpp (original)
+++ llvm/trunk/tools/llvm-stress/llvm-stress.cpp Fri Sep 30 22:43:20 2016
@@ -67,7 +67,7 @@ public:
return false;
}
- const char *getValueName() const override { return "IR scalar type"; }
+ StringRef getValueName() const override { return "IR scalar type"; }
};
}
Modified: llvm/trunk/unittests/Support/CommandLineTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CommandLineTest.cpp?rev=283007&r1=283006&r2=283007&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/CommandLineTest.cpp (original)
+++ llvm/trunk/unittests/Support/CommandLineTest.cpp Fri Sep 30 22:43:20 2016
@@ -76,8 +76,8 @@ public:
class StackSubCommand : public cl::SubCommand {
public:
- StackSubCommand(const char *const Name,
- const char *const Description = nullptr)
+ StackSubCommand(StringRef Name,
+ StringRef Description = StringRef())
: SubCommand(Name, Description) {}
StackSubCommand() : SubCommand() {}
@@ -168,7 +168,7 @@ typedef void ParserFunction(StringRef So
SmallVectorImpl<const char *> &NewArgv,
bool MarkEOLs);
-void testCommandLineTokenizer(ParserFunction *parse, const char *Input,
+void testCommandLineTokenizer(ParserFunction *parse, StringRef Input,
const char *const Output[], size_t OutputSize) {
SmallVector<const char *, 0> Actual;
BumpPtrAllocator A;
@@ -182,7 +182,7 @@ void testCommandLineTokenizer(ParserFunc
}
TEST(CommandLineTest, TokenizeGNUCommandLine) {
- const char *Input =
+ const char Input[] =
"foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' -DFOO=bar\\(\\) "
"foo\"bar\"baz C:\\\\src\\\\foo.cpp \"C:\\src\\foo.cpp\"";
const char *const Output[] = {
@@ -193,7 +193,7 @@ TEST(CommandLineTest, TokenizeGNUCommand
}
TEST(CommandLineTest, TokenizeWindowsCommandLine) {
- const char *Input = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
+ const char Input[] = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
"\"st \\\"u\" \\v";
const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k",
"lmn", "o", "pqr", "st \"u", "\\v" };
@@ -299,7 +299,7 @@ TEST(CommandLineTest, SetValueInSubcateg
EXPECT_FALSE(SC1Opt);
EXPECT_FALSE(SC2Opt);
const char *args[] = {"prog", "-top-level"};
- EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
EXPECT_TRUE(TopLevelOpt);
EXPECT_FALSE(SC1Opt);
EXPECT_FALSE(SC2Opt);
@@ -311,7 +311,7 @@ TEST(CommandLineTest, SetValueInSubcateg
EXPECT_FALSE(SC1Opt);
EXPECT_FALSE(SC2Opt);
const char *args2[] = {"prog", "sc1", "-sc1"};
- EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
EXPECT_FALSE(TopLevelOpt);
EXPECT_TRUE(SC1Opt);
EXPECT_FALSE(SC2Opt);
@@ -323,7 +323,7 @@ TEST(CommandLineTest, SetValueInSubcateg
EXPECT_FALSE(SC1Opt);
EXPECT_FALSE(SC2Opt);
const char *args3[] = {"prog", "sc2", "-sc2"};
- EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), true));
EXPECT_FALSE(TopLevelOpt);
EXPECT_FALSE(SC1Opt);
EXPECT_TRUE(SC2Opt);
@@ -339,7 +339,7 @@ TEST(CommandLineTest, LookupFailsInWrong
StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false));
const char *args[] = {"prog", "sc1", "-sc2"};
- EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, nullptr, true));
+ EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), true));
}
TEST(CommandLineTest, AddToAllSubCommands) {
@@ -355,21 +355,21 @@ TEST(CommandLineTest, AddToAllSubCommand
const char *args3[] = {"prog", "sc2", "-everywhere"};
EXPECT_FALSE(AllOpt);
- EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
EXPECT_TRUE(AllOpt);
AllOpt = false;
cl::ResetAllOptionOccurrences();
EXPECT_FALSE(AllOpt);
- EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
EXPECT_TRUE(AllOpt);
AllOpt = false;
cl::ResetAllOptionOccurrences();
EXPECT_FALSE(AllOpt);
- EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), true));
EXPECT_TRUE(AllOpt);
}
@@ -382,14 +382,14 @@ TEST(CommandLineTest, ReparseCommandLine
const char *args[] = {"prog", "-top-level"};
EXPECT_FALSE(TopLevelOpt);
- EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
EXPECT_TRUE(TopLevelOpt);
TopLevelOpt = false;
cl::ResetAllOptionOccurrences();
EXPECT_FALSE(TopLevelOpt);
- EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
EXPECT_TRUE(TopLevelOpt);
}
@@ -403,13 +403,13 @@ TEST(CommandLineTest, RemoveFromRegularS
const char *args[] = {"prog", "sc", "-remove-option"};
EXPECT_FALSE(RemoveOption);
- EXPECT_TRUE(cl::ParseCommandLineOptions(3, args, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(3, args, StringRef(), true));
EXPECT_TRUE(RemoveOption);
RemoveOption.removeArgument();
cl::ResetAllOptionOccurrences();
- EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, nullptr, true));
+ EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), true));
}
TEST(CommandLineTest, RemoveFromTopLevelSubCommand) {
@@ -423,13 +423,13 @@ TEST(CommandLineTest, RemoveFromTopLevel
const char *args[] = {"prog", "-top-level-remove"};
EXPECT_FALSE(TopLevelRemove);
- EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
EXPECT_TRUE(TopLevelRemove);
TopLevelRemove.removeArgument();
cl::ResetAllOptionOccurrences();
- EXPECT_FALSE(cl::ParseCommandLineOptions(2, args, nullptr, true));
+ EXPECT_FALSE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
}
TEST(CommandLineTest, RemoveFromAllSubCommands) {
@@ -448,32 +448,32 @@ TEST(CommandLineTest, RemoveFromAllSubCo
// It should work for all subcommands including the top-level.
EXPECT_FALSE(RemoveOption);
- EXPECT_TRUE(cl::ParseCommandLineOptions(2, args0, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args0, StringRef(), true));
EXPECT_TRUE(RemoveOption);
RemoveOption = false;
cl::ResetAllOptionOccurrences();
EXPECT_FALSE(RemoveOption);
- EXPECT_TRUE(cl::ParseCommandLineOptions(3, args1, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(3, args1, StringRef(), true));
EXPECT_TRUE(RemoveOption);
RemoveOption = false;
cl::ResetAllOptionOccurrences();
EXPECT_FALSE(RemoveOption);
- EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, nullptr, true));
+ EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
EXPECT_TRUE(RemoveOption);
RemoveOption.removeArgument();
// It should not work for any subcommands including the top-level.
cl::ResetAllOptionOccurrences();
- EXPECT_FALSE(cl::ParseCommandLineOptions(2, args0, nullptr, true));
+ EXPECT_FALSE(cl::ParseCommandLineOptions(2, args0, StringRef(), true));
cl::ResetAllOptionOccurrences();
- EXPECT_FALSE(cl::ParseCommandLineOptions(3, args1, nullptr, true));
+ EXPECT_FALSE(cl::ParseCommandLineOptions(3, args1, StringRef(), true));
cl::ResetAllOptionOccurrences();
- EXPECT_FALSE(cl::ParseCommandLineOptions(3, args2, nullptr, true));
+ EXPECT_FALSE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
}
} // anonymous namespace
More information about the llvm-commits
mailing list