[llvm] 54cfc69 - [CommandLine] Fix cl::ConsumeAfter support with more than one positional argument
Yi-Hong Lyu via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 17 02:19:55 PDT 2020
Author: Yi-Hong Lyu
Date: 2020-04-17T02:12:54-07:00
New Revision: 54cfc6944e2669d7a41a164fc4f3d923a71e701d
URL: https://github.com/llvm/llvm-project/commit/54cfc6944e2669d7a41a164fc4f3d923a71e701d
DIFF: https://github.com/llvm/llvm-project/commit/54cfc6944e2669d7a41a164fc4f3d923a71e701d.diff
LOG: [CommandLine] Fix cl::ConsumeAfter support with more than one positional argument
Summary:
Currently, cl::ConsumeAfter only works for the case that has exactly one
positional argument. Without the fix, it skip fulfilling first positional
argument and put that additional positional argument in interpreter arguments.
Reviewers: bkramer, Mordante, rnk, lattner, beanz, craig.topper
Reviewed By: rnk
Subscribers: JosephTremoulet, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77242
Added:
Modified:
llvm/lib/Support/CommandLine.cpp
llvm/unittests/Support/CommandLineTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index f78c4bfbb311..0025806ca235 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -1581,9 +1581,9 @@ bool CommandLineParser::ParseCommandLineOptions(int argc,
} else {
assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
unsigned ValNo = 0;
- for (size_t j = 1, e = PositionalOpts.size(); j != e; ++j)
- if (RequiresValue(PositionalOpts[j])) {
- ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
+ for (size_t J = 0, E = PositionalOpts.size(); J != E; ++J)
+ if (RequiresValue(PositionalOpts[J])) {
+ ErrorParsing |= ProvidePositionalOption(PositionalOpts[J],
PositionalVals[ValNo].first,
PositionalVals[ValNo].second);
ValNo++;
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp
index b6fc699cf37f..a6b26b310b97 100644
--- a/llvm/unittests/Support/CommandLineTest.cpp
+++ b/llvm/unittests/Support/CommandLineTest.cpp
@@ -1793,4 +1793,49 @@ static cl::bits<Enum> ExampleBits(
clEnumValN(Val1, "bits-val1", "The Val1 value"),
clEnumValN(Val1, "bits-val2", "The Val2 value")));
+TEST(CommandLineTest, ConsumeAfterOnePositional) {
+ cl::ResetCommandLineParser();
+
+ // input [args]
+ StackOption<std::string, cl::opt<std::string>> Input(cl::Positional,
+ cl::Required);
+ StackOption<std::string, cl::list<std::string>> ExtraArgs(cl::ConsumeAfter);
+
+ const char *Args[] = {"prog", "input", "arg1", "arg2"};
+
+ std::string Errs;
+ raw_string_ostream OS(Errs);
+ EXPECT_TRUE(cl::ParseCommandLineOptions(4, Args, StringRef(), &OS));
+ OS.flush();
+ EXPECT_EQ("input", Input);
+ EXPECT_TRUE(ExtraArgs.size() == 2);
+ EXPECT_TRUE(ExtraArgs[0] == "arg1");
+ EXPECT_TRUE(ExtraArgs[1] == "arg2");
+ EXPECT_TRUE(Errs.empty());
+}
+
+TEST(CommandLineTest, ConsumeAfterTwoPositionals) {
+ cl::ResetCommandLineParser();
+
+ // input1 input2 [args]
+ StackOption<std::string, cl::opt<std::string>> Input1(cl::Positional,
+ cl::Required);
+ StackOption<std::string, cl::opt<std::string>> Input2(cl::Positional,
+ cl::Required);
+ StackOption<std::string, cl::list<std::string>> ExtraArgs(cl::ConsumeAfter);
+
+ const char *Args[] = {"prog", "input1", "input2", "arg1", "arg2"};
+
+ std::string Errs;
+ raw_string_ostream OS(Errs);
+ EXPECT_TRUE(cl::ParseCommandLineOptions(5, Args, StringRef(), &OS));
+ OS.flush();
+ EXPECT_EQ("input1", Input1);
+ EXPECT_EQ("input2", Input2);
+ EXPECT_TRUE(ExtraArgs.size() == 2);
+ EXPECT_TRUE(ExtraArgs[0] == "arg1");
+ EXPECT_TRUE(ExtraArgs[1] == "arg2");
+ EXPECT_TRUE(Errs.empty());
+}
+
} // anonymous namespace
More information about the llvm-commits
mailing list