[llvm] r359522 - [CommandLine} Wire-up cl::list::setDefault() so it will work correctly with cl::ResetAllOptionOccurrences() in unittests. Part 2 of 5
Don Hinton via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 29 17:09:49 PDT 2019
Author: dhinton
Date: Mon Apr 29 17:09:49 2019
New Revision: 359522
URL: http://llvm.org/viewvc/llvm-project?rev=359522&view=rev
Log:
[CommandLine} Wire-up cl::list::setDefault() so it will work correctly with cl::ResetAllOptionOccurrences() in unittests. Part 2 of 5
Summary:
With this change, cl::ResetAllOptionOccurrences() clears
cl::list just like cl::opt, allowing users to call
cl::ParseCommandLineOptions() multiple times without interference from
previous calls.
Reviewers: rnk
Reviewed By: rnk
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61234
Modified:
llvm/trunk/include/llvm/Support/CommandLine.h
llvm/trunk/unittests/Support/CommandLineTest.cpp
Modified: llvm/trunk/include/llvm/Support/CommandLine.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/CommandLine.h?rev=359522&r1=359521&r2=359522&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/CommandLine.h (original)
+++ llvm/trunk/include/llvm/Support/CommandLine.h Mon Apr 29 17:09:49 2019
@@ -1425,6 +1425,8 @@ template <class DataType, class StorageC
public:
list_storage() = default;
+ void clear() {}
+
bool setLocation(Option &O, StorageClass &L) {
if (Location)
return O.error("cl::location(x) specified more than once!");
@@ -1476,6 +1478,10 @@ public:
reference operator[](size_type pos) { return Storage[pos]; }
const_reference operator[](size_type pos) const { return Storage[pos]; }
+ void clear() {
+ Storage.clear();
+ }
+
iterator erase(const_iterator pos) { return Storage.erase(pos); }
iterator erase(const_iterator first, const_iterator last) {
return Storage.erase(first, last);
@@ -1553,7 +1559,10 @@ class list : public Option, public list_
void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
}
- void setDefault() override {}
+ void setDefault() override {
+ Positions.clear();
+ list_storage<DataType, StorageClass>::clear();
+ }
void done() {
addArgument();
Modified: llvm/trunk/unittests/Support/CommandLineTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CommandLineTest.cpp?rev=359522&r1=359521&r2=359522&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/CommandLineTest.cpp (original)
+++ llvm/trunk/unittests/Support/CommandLineTest.cpp Mon Apr 29 17:09:49 2019
@@ -944,13 +944,21 @@ TEST(CommandLineTest, ReadConfigFile) {
}
TEST(CommandLineTest, PositionalEatArgsError) {
+ cl::ResetCommandLineParser();
+
StackOption<std::string, cl::list<std::string>> PosEatArgs(
"positional-eat-args", cl::Positional, cl::desc("<arguments>..."),
cl::ZeroOrMore, cl::PositionalEatsArgs);
+ StackOption<std::string, cl::list<std::string>> PosEatArgs2(
+ "positional-eat-args2", cl::Positional, cl::desc("Some strings"),
+ cl::ZeroOrMore, cl::PositionalEatsArgs);
const char *args[] = {"prog", "-positional-eat-args=XXXX"};
const char *args2[] = {"prog", "-positional-eat-args=XXXX", "-foo"};
const char *args3[] = {"prog", "-positional-eat-args", "-foo"};
+ const char *args4[] = {"prog", "-positional-eat-args",
+ "-foo", "-positional-eat-args2",
+ "-bar", "foo"};
std::string Errs;
raw_string_ostream OS(Errs);
@@ -959,6 +967,12 @@ TEST(CommandLineTest, PositionalEatArgsE
EXPECT_FALSE(cl::ParseCommandLineOptions(3, args2, StringRef(), &OS)); OS.flush();
EXPECT_FALSE(Errs.empty()); Errs.clear();
EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), &OS)); OS.flush();
+ EXPECT_TRUE(Errs.empty()); Errs.clear();
+
+ cl::ResetAllOptionOccurrences();
+ EXPECT_TRUE(cl::ParseCommandLineOptions(6, args4, StringRef(), &OS)); OS.flush();
+ EXPECT_TRUE(PosEatArgs.size() == 1);
+ EXPECT_TRUE(PosEatArgs2.size() == 2);
EXPECT_TRUE(Errs.empty());
}
More information about the llvm-commits
mailing list