[PATCH] D115433: [CommandLine] Reset option to its default if its Default field is undefined

Yevgeny Rouban via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 9 01:24:28 PST 2021


yrouban updated this revision to Diff 393067.
yrouban added a comment.

added context lines


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115433/new/

https://reviews.llvm.org/D115433

Files:
  llvm/include/llvm/Support/CommandLine.h
  llvm/unittests/Support/CommandLineTest.cpp


Index: llvm/unittests/Support/CommandLineTest.cpp
===================================================================
--- llvm/unittests/Support/CommandLineTest.cpp
+++ llvm/unittests/Support/CommandLineTest.cpp
@@ -1903,20 +1903,22 @@
 TEST(CommandLineTest, ResetAllOptionOccurrences) {
   cl::ResetCommandLineParser();
 
-  // -option [sink] input [args]
+  // -option -str [sink] input [args]
   StackOption<bool> Option("option");
+  StackOption<std::string> Str("str");
   StackOption<std::string, cl::list<std::string>> Sink(cl::Sink);
   StackOption<std::string> Input(cl::Positional);
   StackOption<std::string, cl::list<std::string>> ExtraArgs(cl::ConsumeAfter);
 
-  const char *Args[] = {"prog", "-option", "-unknown", "input", "-arg"};
+  const char *Args[] = {"prog", "-option", "-str=STR", "-unknown", "input", "-arg"};
 
   std::string Errs;
   raw_string_ostream OS(Errs);
-  EXPECT_TRUE(cl::ParseCommandLineOptions(5, Args, StringRef(), &OS));
+  EXPECT_TRUE(cl::ParseCommandLineOptions(6, Args, StringRef(), &OS));
   EXPECT_TRUE(OS.str().empty());
 
   EXPECT_TRUE(Option);
+  EXPECT_EQ("STR", Str);
   EXPECT_EQ(1, (int)Sink.size());
   EXPECT_EQ("-unknown", Sink[0]);
   EXPECT_EQ("input", Input);
@@ -1925,6 +1927,7 @@
 
   cl::ResetAllOptionOccurrences();
   EXPECT_FALSE(Option);
+  EXPECT_EQ("", Str);
   EXPECT_EQ(0, (int)Sink.size());
   EXPECT_EQ(0, Input.getNumOccurrences());
   EXPECT_EQ(0, (int)ExtraArgs.size());
Index: llvm/include/llvm/Support/CommandLine.h
===================================================================
--- llvm/include/llvm/Support/CommandLine.h
+++ llvm/include/llvm/Support/CommandLine.h
@@ -1476,6 +1476,8 @@
     const OptionValue<DataType> &V = this->getDefault();
     if (V.hasValue())
       this->setValue(V.getValue());
+    else
+      this->setValue(T());
   }
 
   template <class T,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115433.393067.patch
Type: text/x-patch
Size: 1857 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211209/98780ba1/attachment.bin>


More information about the llvm-commits mailing list