diff options
author | Igor Kudrin <ikudrin@accesssoftek.com> | 2019-03-01 09:20:56 +0000 |
---|---|---|
committer | Igor Kudrin <ikudrin@accesssoftek.com> | 2019-03-01 09:20:56 +0000 |
commit | 875f05828d95251abe7c943d79399a3b1c80db12 (patch) | |
tree | bda5a205e962c01a076fe5ef6c2dfe7a84319a14 /llvm/unittests/Support/CommandLineTest.cpp | |
parent | 88c643abf1df624a0402658c74359b6b17f6a86a (diff) | |
download | llvm-875f05828d95251abe7c943d79399a3b1c80db12.zip llvm-875f05828d95251abe7c943d79399a3b1c80db12.tar.gz llvm-875f05828d95251abe7c943d79399a3b1c80db12.tar.bz2 |
[CommandLine] Do not crash if an option has both ValueRequired and Grouping.
If an option, which requires a value, has a `cl::Grouping` formatting
modifier, it works well as far as it is used at the end of a group,
or as a separate argument. However, if the option appears accidentally
in the middle of a group, the program just crashes. This patch prints
an error message instead.
Differential Revision: https://reviews.llvm.org/D58499
llvm-svn: 355184
Diffstat (limited to 'llvm/unittests/Support/CommandLineTest.cpp')
-rw-r--r-- | llvm/unittests/Support/CommandLineTest.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp index f9841f5..4839a31 100644 --- a/llvm/unittests/Support/CommandLineTest.cpp +++ b/llvm/unittests/Support/CommandLineTest.cpp @@ -1128,4 +1128,26 @@ TEST(CommandLineTest, PrefixOptions) { 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 |