1 //===- unittest/Support/OptionParsingTest.cpp - OptTable tests ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/Option/Arg.h"
12 #include "llvm/Option/ArgList.h"
13 #include "llvm/Option/Option.h"
14 #include "gtest/gtest.h"
17 using namespace llvm::opt;
20 OPT_INVALID = 0, // This is not an option ID.
21 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
22 HELPTEXT, METAVAR) OPT_##ID,
28 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
38 static const OptTable::Info InfoTable[] = {
39 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
41 { PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, PARAM, \
42 FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS },
48 class TestOptTable : public OptTable {
50 TestOptTable(bool IgnoreCase = false)
51 : OptTable(InfoTable, array_lengthof(InfoTable), IgnoreCase) {}
55 const char *Args[] = {
61 "-E", "apple", "bloom",
67 TEST(Option, OptionParsing) {
70 std::unique_ptr<InputArgList> AL(
71 T.ParseArgs(Args, array_endof(Args), MAI, MAC));
73 // Check they all exist.
74 EXPECT_TRUE(AL->hasArg(OPT_A));
75 EXPECT_TRUE(AL->hasArg(OPT_B));
76 EXPECT_TRUE(AL->hasArg(OPT_C));
77 EXPECT_TRUE(AL->hasArg(OPT_D));
78 EXPECT_TRUE(AL->hasArg(OPT_E));
79 EXPECT_TRUE(AL->hasArg(OPT_F));
80 EXPECT_TRUE(AL->hasArg(OPT_G));
83 EXPECT_EQ(AL->getLastArgValue(OPT_B), "hi");
84 EXPECT_EQ(AL->getLastArgValue(OPT_C), "bye");
85 EXPECT_EQ(AL->getLastArgValue(OPT_D), "adena");
86 std::vector<std::string> Es = AL->getAllArgValues(OPT_E);
87 EXPECT_EQ(Es[0], "apple");
88 EXPECT_EQ(Es[1], "bloom");
89 EXPECT_EQ(AL->getLastArgValue(OPT_F), "42");
90 std::vector<std::string> Gs = AL->getAllArgValues(OPT_G);
91 EXPECT_EQ(Gs[0], "chuu");
92 EXPECT_EQ(Gs[1], "2");
94 // Check the help text.
96 raw_string_ostream RSO(Help);
97 T.PrintHelp(RSO, "test", "title!");
98 EXPECT_NE(Help.find("-A"), std::string::npos);
101 arg_iterator Cs = AL->filtered_begin(OPT_C);
102 ASSERT_NE(Cs, AL->filtered_end());
103 EXPECT_EQ(StringRef((*Cs)->getValue()), "desu");
105 (*Cs)->render(*AL, ASL);
106 ASSERT_EQ(ASL.size(), 2u);
107 EXPECT_EQ(StringRef(ASL[0]), "-C");
108 EXPECT_EQ(StringRef(ASL[1]), "desu");
111 TEST(Option, ParseWithFlagExclusions) {
114 std::unique_ptr<InputArgList> AL;
116 // Exclude flag3 to avoid parsing as OPT_SLASH_C.
117 AL.reset(T.ParseArgs(Args, array_endof(Args), MAI, MAC,
118 /*FlagsToInclude=*/0,
119 /*FlagsToExclude=*/OptFlag3));
120 EXPECT_TRUE(AL->hasArg(OPT_A));
121 EXPECT_TRUE(AL->hasArg(OPT_C));
122 EXPECT_FALSE(AL->hasArg(OPT_SLASH_C));
124 // Exclude flag1 to avoid parsing as OPT_C.
125 AL.reset(T.ParseArgs(Args, array_endof(Args), MAI, MAC,
126 /*FlagsToInclude=*/0,
127 /*FlagsToExclude=*/OptFlag1));
128 EXPECT_TRUE(AL->hasArg(OPT_B));
129 EXPECT_FALSE(AL->hasArg(OPT_C));
130 EXPECT_TRUE(AL->hasArg(OPT_SLASH_C));
132 const char *NewArgs[] = { "/C", "foo", "--C=bar" };
133 AL.reset(T.ParseArgs(NewArgs, array_endof(NewArgs), MAI, MAC));
134 EXPECT_TRUE(AL->hasArg(OPT_SLASH_C));
135 EXPECT_TRUE(AL->hasArg(OPT_C));
136 EXPECT_EQ(AL->getLastArgValue(OPT_SLASH_C), "foo");
137 EXPECT_EQ(AL->getLastArgValue(OPT_C), "bar");
140 TEST(Option, ParseAliasInGroup) {
144 const char *MyArgs[] = { "-I" };
145 std::unique_ptr<InputArgList> AL(
146 T.ParseArgs(MyArgs, array_endof(MyArgs), MAI, MAC));
147 EXPECT_TRUE(AL->hasArg(OPT_H));
150 TEST(Option, AliasArgs) {
154 const char *MyArgs[] = { "-J", "-Joo" };
155 std::unique_ptr<InputArgList> AL(
156 T.ParseArgs(MyArgs, array_endof(MyArgs), MAI, MAC));
157 EXPECT_TRUE(AL->hasArg(OPT_B));
158 EXPECT_EQ(AL->getAllArgValues(OPT_B)[0], "foo");
159 EXPECT_EQ(AL->getAllArgValues(OPT_B)[1], "bar");
162 TEST(Option, IgnoreCase) {
163 TestOptTable T(true);
166 const char *MyArgs[] = { "-a", "-joo" };
167 std::unique_ptr<InputArgList> AL(
168 T.ParseArgs(MyArgs, array_endof(MyArgs), MAI, MAC));
169 EXPECT_TRUE(AL->hasArg(OPT_A));
170 EXPECT_TRUE(AL->hasArg(OPT_B));
173 TEST(Option, DoNotIgnoreCase) {
177 const char *MyArgs[] = { "-a", "-joo" };
178 std::unique_ptr<InputArgList> AL(
179 T.ParseArgs(MyArgs, array_endof(MyArgs), MAI, MAC));
180 EXPECT_FALSE(AL->hasArg(OPT_A));
181 EXPECT_FALSE(AL->hasArg(OPT_B));
184 TEST(Option, SlurpEmpty) {
188 const char *MyArgs[] = { "-A", "-slurp" };
189 std::unique_ptr<InputArgList> AL(
190 T.ParseArgs(MyArgs, array_endof(MyArgs), MAI, MAC));
191 EXPECT_TRUE(AL->hasArg(OPT_A));
192 EXPECT_TRUE(AL->hasArg(OPT_Slurp));
193 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp).size(), 0U);
196 TEST(Option, Slurp) {
200 const char *MyArgs[] = { "-A", "-slurp", "-B", "--", "foo" };
201 std::unique_ptr<InputArgList> AL(
202 T.ParseArgs(MyArgs, array_endof(MyArgs), MAI, MAC));
203 EXPECT_EQ(AL->size(), 2U);
204 EXPECT_TRUE(AL->hasArg(OPT_A));
205 EXPECT_FALSE(AL->hasArg(OPT_B));
206 EXPECT_TRUE(AL->hasArg(OPT_Slurp));
207 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp).size(), 3U);
208 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp)[0], "-B");
209 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp)[1], "--");
210 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp)[2], "foo");