[PATCH] D58499: [CommandLine] Do not crash if an option has both ValueRequired and Grouping.

Igor Kudrin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 1 01:22:03 PST 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL355184: [CommandLine] Do not crash if an option has both ValueRequired and Grouping. (authored by ikudrin, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D58499?vs=187944&id=188859#toc

Repository:
  rL LLVM

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

https://reviews.llvm.org/D58499

Files:
  llvm/trunk/docs/CommandLine.rst
  llvm/trunk/lib/Support/CommandLine.cpp
  llvm/trunk/unittests/Support/CommandLineTest.cpp


Index: llvm/trunk/lib/Support/CommandLine.cpp
===================================================================
--- llvm/trunk/lib/Support/CommandLine.cpp
+++ llvm/trunk/lib/Support/CommandLine.cpp
@@ -671,10 +671,13 @@
     StringRef OneArgName = Arg.substr(0, Length);
     Arg = Arg.substr(Length);
 
-    // Because ValueRequired is an invalid flag for grouped arguments,
-    // we don't need to pass argc/argv in.
-    assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
-           "Option can not be cl::Grouping AND cl::ValueRequired!");
+    if (PGOpt->getValueExpectedFlag() == cl::ValueRequired) {
+      ErrorParsing |= PGOpt->error("may not occur within a group!");
+      return nullptr;
+    }
+
+    // Because the value for the option is not required, we don't need to pass
+    // argc/argv in.
     int Dummy = 0;
     ErrorParsing |=
         ProvideOption(PGOpt, OneArgName, StringRef(), 0, nullptr, Dummy);
Index: llvm/trunk/unittests/Support/CommandLineTest.cpp
===================================================================
--- llvm/trunk/unittests/Support/CommandLineTest.cpp
+++ llvm/trunk/unittests/Support/CommandLineTest.cpp
@@ -1128,4 +1128,26 @@
   EXPECT_TRUE(MacroDefs.front().compare("HAVE_FOO") == 0);
 }
 
+TEST(CommandLineTest, GroupingWithValue) {
+  cl::ResetCommandLineParser();
+
+  StackOption<bool> OptF("f", cl::Grouping, cl::desc("Some flag"));
+  StackOption<std::string> OptV("v", cl::Grouping,
+                                cl::desc("Grouping option with a value"));
+
+  // Should be possible to use an option which requires a value
+  // at the end of a group.
+  const char *args1[] = {"prog", "-fv", "val1"};
+  EXPECT_TRUE(
+      cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls()));
+  EXPECT_TRUE(OptF);
+  EXPECT_STREQ("val1", OptV.c_str());
+  cl::ResetAllOptionOccurrences();
+
+  // Should not crash if it is accidentally used elsewhere in the group.
+  const char *args2[] = {"prog", "-vf", "val2"};
+  EXPECT_FALSE(
+      cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
+}
+
 }  // anonymous namespace
Index: llvm/trunk/docs/CommandLine.rst
===================================================================
--- llvm/trunk/docs/CommandLine.rst
+++ llvm/trunk/docs/CommandLine.rst
@@ -1172,7 +1172,8 @@
   ``ls``) that have lots of single letter arguments, but only require a single
   dash.  For example, the '``ls -labF``' command actually enables four different
   options, all of which are single letters.  Note that **cl::Grouping** options
-  cannot have values.
+  can have values only if they are used separately or at the end of the groups.
+  It is a runtime error if such an option is used elsewhere in the group.
 
 The CommandLine library does not restrict how you use the **cl::Prefix** or
 **cl::Grouping** modifiers, but it is possible to specify ambiguous argument


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58499.188859.patch
Type: text/x-patch
Size: 2901 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190301/598e9a62/attachment.bin>


More information about the llvm-commits mailing list