[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
Thu Feb 21 03:34:12 PST 2019
ikudrin created this revision.
ikudrin added a reviewer: jhenderson.
Herald added a subscriber: kristina.
Herald added a project: LLVM.
If an option, which requires a value, has a `cl::Grouping` formatting flag,
it works well as far as it is used at the end of a group (or not in a group).
However, if the option appears accidentally in the middle of a group,
the program just crashes. I believe it is better to show some useful
error message in that case.
Repository:
rL LLVM
https://reviews.llvm.org/D58499
Files:
lib/Support/CommandLine.cpp
unittests/Support/CommandLineTest.cpp
Index: unittests/Support/CommandLineTest.cpp
===================================================================
--- unittests/Support/CommandLineTest.cpp
+++ unittests/Support/CommandLineTest.cpp
@@ -1128,4 +1128,25 @@
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_STREQ("val1", OptV.c_str());
+ cl::ResetAllOptionOccurrences();
+
+ // Should not crash if it is accidentally used in the middle of a group.
+ const char *args2[] = {"prog", "-vf", "val2"};
+ EXPECT_FALSE(
+ cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
+}
+
} // anonymous namespace
Index: lib/Support/CommandLine.cpp
===================================================================
--- lib/Support/CommandLine.cpp
+++ 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);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58499.187753.patch
Type: text/x-patch
Size: 2035 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190221/16d94693/attachment.bin>
More information about the llvm-commits
mailing list