diff --git a/llvm/include/llvm/Support/CommandLine.h b/llvm/include/llvm/Support/CommandLine.h --- a/llvm/include/llvm/Support/CommandLine.h +++ b/llvm/include/llvm/Support/CommandLine.h @@ -1425,6 +1425,8 @@ 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 @@ 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 @@ 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/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp --- a/llvm/unittests/Support/CommandLineTest.cpp +++ b/llvm/unittests/Support/CommandLineTest.cpp @@ -944,13 +944,21 @@ } 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 @@ 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()); }