diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp --- a/llvm/lib/Support/CommandLine.cpp +++ b/llvm/lib/Support/CommandLine.cpp @@ -1325,6 +1325,12 @@ for (auto *SC : RegisteredSubCommands) { for (auto &O : SC->OptionsMap) O.second->reset(); + for (Option *O : SC->PositionalOpts) + O->reset(); + for (Option *O : SC->SinkOpts) + O->reset(); + if (SC->ConsumeAfterOpt) + SC->ConsumeAfterOpt->reset(); } } 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 @@ -1894,4 +1894,34 @@ EXPECT_TRUE(Errs.empty()); } +TEST(CommandLineTest, ResetAllOptionOccurrences) { + cl::ResetCommandLineParser(); + + // -option [sink] input [args] + StackOption Option("option"); + StackOption> Sink(cl::Sink); + StackOption Input(cl::Positional); + StackOption> ExtraArgs(cl::ConsumeAfter); + + const char *Args[] = {"prog", "-option", "-unknown", "input", "-arg"}; + + std::string Errs; + raw_string_ostream OS(Errs); + EXPECT_TRUE(cl::ParseCommandLineOptions(5, Args, StringRef(), &OS)); + EXPECT_TRUE(OS.str().empty()); + + EXPECT_TRUE(Option); + EXPECT_EQ(1, Sink.size()); + EXPECT_EQ("-unknown", Sink[0]); + EXPECT_EQ("input", Input); + EXPECT_EQ(1, ExtraArgs.size()); + EXPECT_EQ("-arg", ExtraArgs[0]); + + cl::ResetAllOptionOccurrences(); + EXPECT_FALSE(Option); + EXPECT_EQ(0, Sink.size()); + EXPECT_EQ(0, Input.getNumOccurrences()); + EXPECT_EQ(0, ExtraArgs.size()); +} + } // anonymous namespace