llvm.org GIT mirror llvm / 006bce7
ArrayRef-ify ParseArgs git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240233 91177308-0d34-0410-b5e6-96231b3b80d8 David Blaikie 4 years ago
4 changed file(s) with 19 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
99 #ifndef LLVM_OPTION_OPTTABLE_H
1010 #define LLVM_OPTION_OPTTABLE_H
1111
12 #include "llvm/ADT/ArrayRef.h"
1213 #include "llvm/ADT/StringSet.h"
1314 #include "llvm/Option/OptSpecifier.h"
1415
151152 /// is the default and means exclude nothing.
152153 /// \return An InputArgList; on error this will contain all the options
153154 /// which could be parsed.
154 InputArgList *ParseArgs(const char* const *ArgBegin,
155 const char* const *ArgEnd,
156 unsigned &MissingArgIndex,
157 unsigned &MissingArgCount,
155 InputArgList *ParseArgs(ArrayRef Args,
156 unsigned &MissingArgIndex, unsigned &MissingArgCount,
158157 unsigned FlagsToInclude = 0,
159158 unsigned FlagsToExclude = 0) const;
160159
113113 LibOptTable Table;
114114 unsigned MissingIndex;
115115 unsigned MissingCount;
116 std::unique_ptr Args(
117 Table.ParseArgs(&Argv[1], &Argv[Argc], MissingIndex, MissingCount));
116 std::unique_ptr Args(Table.ParseArgs(
117 makeArrayRef(Argv, Argc).slice(1), MissingIndex, MissingCount));
118118 if (MissingCount) {
119119 llvm::errs() << "missing arg value for \""
120120 << Args->getArgString(MissingIndex)
246246 return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str);
247247 }
248248
249 InputArgList *OptTable::ParseArgs(const char *const *ArgBegin,
250 const char *const *ArgEnd,
249 InputArgList *OptTable::ParseArgs(ArrayRef ArgArr,
251250 unsigned &MissingArgIndex,
252251 unsigned &MissingArgCount,
253252 unsigned FlagsToInclude,
254253 unsigned FlagsToExclude) const {
255 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
254 InputArgList *Args = new InputArgList(ArgArr.begin(), ArgArr.end());
256255
257256 // FIXME: Handle '@' args (or at least error on them).
258257
259258 MissingArgIndex = MissingArgCount = 0;
260 unsigned Index = 0, End = ArgEnd - ArgBegin;
259 unsigned Index = 0, End = ArgArr.size();
261260 while (Index < End) {
262261 // Ingore nullptrs, they are response file's EOL markers
263262 if (Args->getArgString(Index) == nullptr) {
6666 TEST(Option, OptionParsing) {
6767 TestOptTable T;
6868 unsigned MAI, MAC;
69 std::unique_ptr AL(
70 T.ParseArgs(std::begin(Args), std::end(Args), MAI, MAC));
69 std::unique_ptr AL(T.ParseArgs(Args, MAI, MAC));
7170
7271 // Check they all exist.
7372 EXPECT_TRUE(AL->hasArg(OPT_A));
113112 std::unique_ptr AL;
114113
115114 // Exclude flag3 to avoid parsing as OPT_SLASH_C.
116 AL.reset(T.ParseArgs(std::begin(Args), std::end(Args), MAI, MAC,
115 AL.reset(T.ParseArgs(Args, MAI, MAC,
117116 /*FlagsToInclude=*/0,
118117 /*FlagsToExclude=*/OptFlag3));
119118 EXPECT_TRUE(AL->hasArg(OPT_A));
121120 EXPECT_FALSE(AL->hasArg(OPT_SLASH_C));
122121
123122 // Exclude flag1 to avoid parsing as OPT_C.
124 AL.reset(T.ParseArgs(std::begin(Args), std::end(Args), MAI, MAC,
123 AL.reset(T.ParseArgs(Args, MAI, MAC,
125124 /*FlagsToInclude=*/0,
126125 /*FlagsToExclude=*/OptFlag1));
127126 EXPECT_TRUE(AL->hasArg(OPT_B));
129128 EXPECT_TRUE(AL->hasArg(OPT_SLASH_C));
130129
131130 const char *NewArgs[] = { "/C", "foo", "--C=bar" };
132 AL.reset(T.ParseArgs(std::begin(NewArgs), std::end(NewArgs), MAI, MAC));
131 AL.reset(T.ParseArgs(NewArgs, MAI, MAC));
133132 EXPECT_TRUE(AL->hasArg(OPT_SLASH_C));
134133 EXPECT_TRUE(AL->hasArg(OPT_C));
135134 EXPECT_EQ(AL->getLastArgValue(OPT_SLASH_C), "foo");
141140 unsigned MAI, MAC;
142141
143142 const char *MyArgs[] = { "-I" };
144 std::unique_ptr AL(
145 T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC));
143 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
146144 EXPECT_TRUE(AL->hasArg(OPT_H));
147145 }
148146
151149 unsigned MAI, MAC;
152150
153151 const char *MyArgs[] = { "-J", "-Joo" };
154 std::unique_ptr AL(
155 T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC));
152 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
156153 EXPECT_TRUE(AL->hasArg(OPT_B));
157154 EXPECT_EQ(AL->getAllArgValues(OPT_B)[0], "foo");
158155 EXPECT_EQ(AL->getAllArgValues(OPT_B)[1], "bar");
163160 unsigned MAI, MAC;
164161
165162 const char *MyArgs[] = { "-a", "-joo" };
166 std::unique_ptr AL(
167 T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC));
163 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
168164 EXPECT_TRUE(AL->hasArg(OPT_A));
169165 EXPECT_TRUE(AL->hasArg(OPT_B));
170166 }
174170 unsigned MAI, MAC;
175171
176172 const char *MyArgs[] = { "-a", "-joo" };
177 std::unique_ptr AL(
178 T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC));
173 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
179174 EXPECT_FALSE(AL->hasArg(OPT_A));
180175 EXPECT_FALSE(AL->hasArg(OPT_B));
181176 }
185180 unsigned MAI, MAC;
186181
187182 const char *MyArgs[] = { "-A", "-slurp" };
188 std::unique_ptr AL(
189 T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC));
183 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
190184 EXPECT_TRUE(AL->hasArg(OPT_A));
191185 EXPECT_TRUE(AL->hasArg(OPT_Slurp));
192186 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp).size(), 0U);
197191 unsigned MAI, MAC;
198192
199193 const char *MyArgs[] = { "-A", "-slurp", "-B", "--", "foo" };
200 std::unique_ptr AL(
201 T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC));
194 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
202195 EXPECT_EQ(AL->size(), 2U);
203196 EXPECT_TRUE(AL->hasArg(OPT_A));
204197 EXPECT_FALSE(AL->hasArg(OPT_B));
215208
216209 // Check that a flag alias provides an empty argument to a joined option.
217210 const char *MyArgs[] = { "-K" };
218 std::unique_ptr AL(
219 T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC));
211 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
220212 EXPECT_EQ(AL->size(), 1U);
221213 EXPECT_TRUE(AL->hasArg(OPT_B));
222214 EXPECT_EQ(AL->getAllArgValues(OPT_B).size(), 1U);