From 375079a549c584bd14dad501ecc4575738cea3c1 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Mon, 4 May 2015 18:00:13 +0000 Subject: [PATCH] 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 --- lib/Option/Option.cpp | 5 +++++ unittests/Option/OptionParsingTest.cpp | 14 ++++++++++++++ unittests/Option/Opts.td | 2 ++ 3 files changed, 21 insertions(+) diff --git a/lib/Option/Option.cpp b/lib/Option/Option.cpp index e29d6491c7f..221414d79e7 100644 --- a/lib/Option/Option.cpp +++ b/lib/Option/Option.cpp @@ -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: { diff --git a/unittests/Option/OptionParsingTest.cpp b/unittests/Option/OptionParsingTest.cpp index aa32da3a595..521009a9e66 100644 --- a/unittests/Option/OptionParsingTest.cpp +++ b/unittests/Option/OptionParsingTest.cpp @@ -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 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], ""); +} diff --git a/unittests/Option/Opts.td b/unittests/Option/Opts.td index aaed6b2101e..c96774a68e0 100644 --- a/unittests/Option/Opts.td +++ b/unittests/Option/Opts.td @@ -23,4 +23,6 @@ def I : Flag<["-"], "I">, Alias, Group; def J : Flag<["-"], "J">, Alias, AliasArgs<["foo"]>; def Joo : Flag<["-"], "Joo">, Alias, AliasArgs<["bar"]>; +def K : Flag<["-"], "K">, Alias; + def Slurp : Option<["-"], "slurp", KIND_REMAINING_ARGS>; -- 2.34.1