[llvm] r266394 - Option parser: class for consuming a joined arg in addition to all remaining args
Hans Wennborg via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 14 17:23:30 PDT 2016
Author: hans
Date: Thu Apr 14 19:23:30 2016
New Revision: 266394
URL: http://llvm.org/viewvc/llvm-project?rev=266394&view=rev
Log:
Option parser: class for consuming a joined arg in addition to all remaining args
Modified:
llvm/trunk/include/llvm/Option/OptParser.td
llvm/trunk/include/llvm/Option/Option.h
llvm/trunk/lib/Option/OptTable.cpp
llvm/trunk/lib/Option/Option.cpp
llvm/trunk/unittests/Option/OptionParsingTest.cpp
llvm/trunk/unittests/Option/Opts.td
Modified: llvm/trunk/include/llvm/Option/OptParser.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Option/OptParser.td?rev=266394&r1=266393&r2=266394&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Option/OptParser.td (original)
+++ llvm/trunk/include/llvm/Option/OptParser.td Thu Apr 14 19:23:30 2016
@@ -46,6 +46,9 @@ def KIND_JOINED_OR_SEPARATE : OptionKind
def KIND_JOINED_AND_SEPARATE : OptionKind<"JoinedAndSeparate">;
// An option which consumes all remaining arguments if there are any.
def KIND_REMAINING_ARGS : OptionKind<"RemainingArgs">;
+// An option which consumes an optional joined argument and any other remaining
+// arguments.
+def KIND_REMAINING_ARGS_JOINED : OptionKind<"RemainingArgsJoined">;
// Define the option flags.
Modified: llvm/trunk/include/llvm/Option/Option.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Option/Option.h?rev=266394&r1=266393&r2=266394&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Option/Option.h (original)
+++ llvm/trunk/include/llvm/Option/Option.h Thu Apr 14 19:23:30 2016
@@ -51,6 +51,7 @@ public:
JoinedClass,
SeparateClass,
RemainingArgsClass,
+ RemainingArgsJoinedClass,
CommaJoinedClass,
MultiArgClass,
JoinedOrSeparateClass,
@@ -150,6 +151,7 @@ public:
case MultiArgClass:
case JoinedOrSeparateClass:
case RemainingArgsClass:
+ case RemainingArgsJoinedClass:
return RenderSeparateStyle;
}
llvm_unreachable("Unexpected kind!");
Modified: llvm/trunk/lib/Option/OptTable.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Option/OptTable.cpp?rev=266394&r1=266393&r2=266394&view=diff
==============================================================================
--- llvm/trunk/lib/Option/OptTable.cpp (original)
+++ llvm/trunk/lib/Option/OptTable.cpp Thu Apr 14 19:23:30 2016
@@ -315,7 +315,7 @@ static std::string getOptionHelpName(con
break;
case Option::SeparateClass: case Option::JoinedOrSeparateClass:
- case Option::RemainingArgsClass:
+ case Option::RemainingArgsClass: case Option::RemainingArgsJoinedClass:
Name += ' ';
// FALLTHROUGH
case Option::JoinedClass: case Option::CommaJoinedClass:
Modified: llvm/trunk/lib/Option/Option.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Option/Option.cpp?rev=266394&r1=266393&r2=266394&view=diff
==============================================================================
--- llvm/trunk/lib/Option/Option.cpp (original)
+++ llvm/trunk/lib/Option/Option.cpp Thu Apr 14 19:23:30 2016
@@ -51,6 +51,7 @@ void Option::print(raw_ostream &O) const
P(JoinedOrSeparateClass);
P(JoinedAndSeparateClass);
P(RemainingArgsClass);
+ P(RemainingArgsJoinedClass);
#undef P
}
@@ -234,6 +235,19 @@ Arg *Option::accept(const ArgList &Args,
A->getValues().push_back(Args.getArgString(Index++));
return A;
}
+ case RemainingArgsJoinedClass: {
+ Arg *A = new Arg(UnaliasedOption, Spelling, Index);
+ if (ArgSize != strlen(Args.getArgString(Index))) {
+ // An inexact match means there is a joined arg.
+ A->getValues().push_back(Args.getArgString(Index) + ArgSize);
+ }
+ Index++;
+ while (Index < Args.getNumInputArgStrings() &&
+ Args.getArgString(Index) != nullptr)
+ A->getValues().push_back(Args.getArgString(Index++));
+ return A;
+ }
+
default:
llvm_unreachable("Invalid option kind!");
}
Modified: llvm/trunk/unittests/Option/OptionParsingTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Option/OptionParsingTest.cpp?rev=266394&r1=266393&r2=266394&view=diff
==============================================================================
--- llvm/trunk/unittests/Option/OptionParsingTest.cpp (original)
+++ llvm/trunk/unittests/Option/OptionParsingTest.cpp Thu Apr 14 19:23:30 2016
@@ -202,6 +202,57 @@ TEST(Option, Slurp) {
EXPECT_EQ("foo", AL.getAllArgValues(OPT_Slurp)[2]);
}
+TEST(Option, SlurpJoinedEmpty) {
+ TestOptTable T;
+ unsigned MAI, MAC;
+
+ const char *MyArgs[] = { "-A", "-slurpjoined" };
+ InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
+ EXPECT_TRUE(AL.hasArg(OPT_A));
+ EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
+ EXPECT_EQ(AL.getAllArgValues(OPT_SlurpJoined).size(), 0U);
+}
+
+TEST(Option, SlurpJoinedOneJoined) {
+ TestOptTable T;
+ unsigned MAI, MAC;
+
+ const char *MyArgs[] = { "-A", "-slurpjoinedfoo" };
+ InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
+ EXPECT_TRUE(AL.hasArg(OPT_A));
+ EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
+ EXPECT_EQ(AL.getAllArgValues(OPT_SlurpJoined).size(), 1U);
+ EXPECT_EQ(AL.getAllArgValues(OPT_SlurpJoined)[0], "foo");
+}
+
+TEST(Option, SlurpJoinedAndSeparate) {
+ TestOptTable T;
+ unsigned MAI, MAC;
+
+ const char *MyArgs[] = { "-A", "-slurpjoinedfoo", "bar", "baz" };
+ InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
+ EXPECT_TRUE(AL.hasArg(OPT_A));
+ EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
+ EXPECT_EQ(3U, AL.getAllArgValues(OPT_SlurpJoined).size());
+ EXPECT_EQ("foo", AL.getAllArgValues(OPT_SlurpJoined)[0]);
+ EXPECT_EQ("bar", AL.getAllArgValues(OPT_SlurpJoined)[1]);
+ EXPECT_EQ("baz", AL.getAllArgValues(OPT_SlurpJoined)[2]);
+}
+
+TEST(Option, SlurpJoinedButSeparate) {
+ TestOptTable T;
+ unsigned MAI, MAC;
+
+ const char *MyArgs[] = { "-A", "-slurpjoined", "foo", "bar", "baz" };
+ InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
+ EXPECT_TRUE(AL.hasArg(OPT_A));
+ EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
+ EXPECT_EQ(3U, AL.getAllArgValues(OPT_SlurpJoined).size());
+ EXPECT_EQ("foo", AL.getAllArgValues(OPT_SlurpJoined)[0]);
+ EXPECT_EQ("bar", AL.getAllArgValues(OPT_SlurpJoined)[1]);
+ EXPECT_EQ("baz", AL.getAllArgValues(OPT_SlurpJoined)[2]);
+}
+
TEST(Option, FlagAliasToJoined) {
TestOptTable T;
unsigned MAI, MAC;
Modified: llvm/trunk/unittests/Option/Opts.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Option/Opts.td?rev=266394&r1=266393&r2=266394&view=diff
==============================================================================
--- llvm/trunk/unittests/Option/Opts.td (original)
+++ llvm/trunk/unittests/Option/Opts.td Thu Apr 14 19:23:30 2016
@@ -26,3 +26,5 @@ def Joo : Flag<["-"], "Joo">, Alias<B>,
def K : Flag<["-"], "K">, Alias<B>;
def Slurp : Option<["-"], "slurp", KIND_REMAINING_ARGS>;
+
+def SlurpJoined : Option<["-"], "slurpjoined", KIND_REMAINING_ARGS_JOINED>;
More information about the llvm-commits
mailing list