Option parsing: properly handle flag aliases for joined options (PR23394)

A joined option always needs to have an argument, even if it's an empty one.

Clang would previously assert when trying to use --extra-warnings, which is
a flag alias for -W, which is a joined option.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@236434 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Hans Wennborg 2015-05-04 18:00:13 +00:00
parent e6cd0c73a7
commit 375079a549
3 changed files with 21 additions and 0 deletions

View File

@ -125,6 +125,11 @@ Arg *Option::accept(const ArgList &Args,
Val += strlen(Val) + 1;
}
}
if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs())
// A Flag alias for a Joined option must provide an argument.
A->getValues().push_back("");
return A;
}
case JoinedClass: {

View File

@ -209,3 +209,17 @@ TEST(Option, Slurp) {
EXPECT_EQ(AL->getAllArgValues(OPT_Slurp)[1], "--");
EXPECT_EQ(AL->getAllArgValues(OPT_Slurp)[2], "foo");
}
TEST(Option, FlagAliasToJoined) {
TestOptTable T;
unsigned MAI, MAC;
// Check that a flag alias provides an empty argument to a joined option.
const char *MyArgs[] = { "-K" };
std::unique_ptr<InputArgList> AL(
T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC));
EXPECT_EQ(AL->size(), 1U);
EXPECT_TRUE(AL->hasArg(OPT_B));
EXPECT_EQ(AL->getAllArgValues(OPT_B).size(), 1U);
EXPECT_EQ(AL->getAllArgValues(OPT_B)[0], "");
}

View File

@ -23,4 +23,6 @@ def I : Flag<["-"], "I">, Alias<H>, Group<my_group>;
def J : Flag<["-"], "J">, Alias<B>, AliasArgs<["foo"]>;
def Joo : Flag<["-"], "Joo">, Alias<B>, AliasArgs<["bar"]>;
def K : Flag<["-"], "K">, Alias<B>;
def Slurp : Option<["-"], "slurp", KIND_REMAINING_ARGS>;