llvm.org GIT mirror llvm / af9e355
Options: Add new option kind that consumes remaining arguments This adds KIND_REMAINING_ARGS, a class of options that consume all remaining arguments on the command line. This will be used to support /link in clang-cl, which is used to forward all remaining arguments to the linker. It also allows us to remove the hard-coded handling of "--", allowing clients (clang and lld) to implement that functionality themselves with this new option class. Differential Revision: http://llvm-reviews.chandlerc.com/D1387 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188314 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 6 years ago
6 changed file(s) with 47 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
4343 // An option which is both joined to its (first) value, and followed by its
4444 // (second) value.
4545 def KIND_JOINED_AND_SEPARATE : OptionKind<"JoinedAndSeparate">;
46 // An option which consumes all remaining arguments if there are any.
47 def KIND_REMAINING_ARGS : OptionKind<"RemainingArgs">;
4648
4749 // Define the option flags.
4850
4949 FlagClass,
5050 JoinedClass,
5151 SeparateClass,
52 RemainingArgsClass,
5253 CommaJoinedClass,
5354 MultiArgClass,
5455 JoinedOrSeparateClass,
148149 case SeparateClass:
149150 case MultiArgClass:
150151 case JoinedOrSeparateClass:
152 case RemainingArgsClass:
151153 return RenderSeparateStyle;
152154 }
153155 llvm_unreachable("Unexpected kind!");
258258 continue;
259259 }
260260
261 // FIXME: Remove once clients are updated to use a KIND_REMAINING_ARGS
262 // option to handle this explicitly instead.
261263 if (Str == "--") {
262264 // Everything after -- is a filename.
263265 ++Index;
307309 break;
308310
309311 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
312 case Option::RemainingArgsClass:
310313 Name += ' ';
311314 // FALLTHROUGH
312315 case Option::JoinedClass: case Option::CommaJoinedClass:
5151 P(MultiArgClass);
5252 P(JoinedOrSeparateClass);
5353 P(JoinedAndSeparateClass);
54 P(RemainingArgsClass);
5455 #undef P
5556 }
5657
213214 return new Arg(UnaliasedOption, Spelling, Index - 2,
214215 Args.getArgString(Index - 2) + ArgSize,
215216 Args.getArgString(Index - 1));
217 case RemainingArgsClass: {
218 // Matches iff this is an exact match.
219 // FIXME: Avoid strlen.
220 if (ArgSize != strlen(Args.getArgString(Index)))
221 return 0;
222 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
223 while (Index < Args.getNumInputArgStrings())
224 A->getValues().push_back(Args.getArgString(Index++));
225 return A;
226 }
216227 default:
217228 llvm_unreachable("Invalid option kind!");
218229 }
168168 EXPECT_EQ(AL->getAllArgValues(OPT_INPUT)[0], "-B");
169169 EXPECT_EQ(AL->getAllArgValues(OPT_INPUT)[1], "--");
170170 }
171
172 TEST(Option, SlurpEmpty) {
173 TestOptTable T;
174 unsigned MAI, MAC;
175
176 const char *MyArgs[] = { "-A", "-slurp" };
177 OwningPtr AL(T.ParseArgs(MyArgs, array_endof(MyArgs), MAI, MAC));
178 EXPECT_TRUE(AL->hasArg(OPT_A));
179 EXPECT_TRUE(AL->hasArg(OPT_Slurp));
180 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp).size(), 0);
181 }
182
183 TEST(Option, Slurp) {
184 TestOptTable T;
185 unsigned MAI, MAC;
186
187 const char *MyArgs[] = { "-A", "-slurp", "-B", "--", "foo" };
188 OwningPtr AL(T.ParseArgs(MyArgs, array_endof(MyArgs), MAI, MAC));
189 EXPECT_EQ(AL->size(), 2U);
190 EXPECT_TRUE(AL->hasArg(OPT_A));
191 EXPECT_FALSE(AL->hasArg(OPT_B));
192 EXPECT_TRUE(AL->hasArg(OPT_Slurp));
193 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp).size(), 3U);
194 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp)[0], "-B");
195 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp)[1], "--");
196 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp)[2], "foo");
197 }
2121
2222 def J : Flag<["-"], "J">, Alias, AliasArgs<["foo"]>;
2323 def Joo : Flag<["-"], "Joo">, Alias, AliasArgs<["bar"]>;
24
25 def Slurp : Option<["-"], "slurp", KIND_REMAINING_ARGS>;