Skip to content

Commit

Permalink
Option parsing: properly handle flag aliases for joined options (PR23…
Browse files Browse the repository at this point in the history
…394)

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
  • Loading branch information
zmodem committed May 4, 2015
1 parent e6cd0c7 commit 375079a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/Option/Option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
14 changes: 14 additions & 0 deletions unittests/Option/OptionParsingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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], "");
}
2 changes: 2 additions & 0 deletions unittests/Option/Opts.td
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

0 comments on commit 375079a

Please sign in to comment.