From: Don Hinton Date: Tue, 30 Apr 2019 00:09:49 +0000 (+0000) Subject: [CommandLine} Wire-up cl::list::setDefault() so it will work correctly with cl::Reset... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eb132e02b354305f8f2a6a28e42316a2158f8264;p=llvm [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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359522 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Support/CommandLine.h b/include/llvm/Support/CommandLine.h index a62e2eba0a9..1fc26420fe6 100644 --- a/include/llvm/Support/CommandLine.h +++ b/include/llvm/Support/CommandLine.h @@ -1425,6 +1425,8 @@ template class list_storage { 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_storage { void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override { } - void setDefault() override {} + void setDefault() override { + Positions.clear(); + list_storage::clear(); + } void done() { addArgument(); diff --git a/unittests/Support/CommandLineTest.cpp b/unittests/Support/CommandLineTest.cpp index b648600b908..5ce65410b88 100644 --- a/unittests/Support/CommandLineTest.cpp +++ b/unittests/Support/CommandLineTest.cpp @@ -944,13 +944,21 @@ TEST(CommandLineTest, ReadConfigFile) { } TEST(CommandLineTest, PositionalEatArgsError) { + cl::ResetCommandLineParser(); + StackOption> PosEatArgs( "positional-eat-args", cl::Positional, cl::desc("..."), cl::ZeroOrMore, cl::PositionalEatsArgs); + StackOption> 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, PositionalEatArgsError) { 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()); }