llvm.org GIT mirror llvm / 0c17f95
Modify ParseArgs to return the InputArgList by value - there's no need for dynamic allocation/ownership here The one caller that does anything other than keep this variable on the stack is the single use of DerivedArgList in Clang, which is a bit more interesting but can probably be cleaned up/simplified a bit further (have DerivedArgList take ownership of the InputArgList rather than needing to reference its Args indirectly) which I'll try to after this. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240345 91177308-0d34-0410-b5e6-96231b3b80d8 David Blaikie 4 years ago
5 changed file(s) with 118 addition(s) and 99 deletion(s). Raw diff Collapse all Expand all
9191 /// check for the presence of Arg instances for a particular Option
9292 /// and to iterate over groups of arguments.
9393 class ArgList {
94 private:
95 ArgList(const ArgList &) = delete;
96 void operator=(const ArgList &) = delete;
97
9894 public:
9995 typedef SmallVector arglist_type;
10096 typedef arglist_type::iterator iterator;
107103 arglist_type Args;
108104
109105 protected:
110 // Default ctor provided explicitly as it is not provided implicitly due to
111 // the presence of the (deleted) copy ctor above.
106 // Make the default special members protected so they won't be used to slice
107 // derived objects, but can still be used by derived objects to implement
108 // their own special members.
112109 ArgList() = default;
110 // Explicit move operations to ensure the container is cleared post-move
111 // otherwise it could lead to a double-delete in the case of moving of an
112 // InputArgList which deletes the contents of the container. If we could fix
113 // up the ownership here (delegate storage/ownership to the derived class so
114 // it can be a container of unique_ptr) this would be simpler.
115 ArgList(ArgList &&RHS) : Args(std::move(RHS.Args)) { RHS.Args.clear(); }
116 ArgList &operator=(ArgList &&RHS) {
117 Args = std::move(RHS.Args);
118 RHS.Args.clear();
119 return *this;
120 }
113121 // Protect the dtor to ensure this type is never destroyed polymorphically.
114122 ~ArgList() = default;
115123
318326
319327 public:
320328 InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
329 // Default move operations implemented for the convenience of MSVC. Nothing
330 // special here.
331 InputArgList(InputArgList &&RHS)
332 : ArgList(std::move(RHS)), ArgStrings(std::move(RHS.ArgStrings)),
333 SynthesizedStrings(std::move(RHS.SynthesizedStrings)),
334 NumInputArgStrings(RHS.NumInputArgStrings) {}
335 InputArgList &operator=(InputArgList &&RHS) {
336 ArgList::operator=(std::move(RHS));
337 ArgStrings = std::move(RHS.ArgStrings);
338 SynthesizedStrings = std::move(RHS.SynthesizedStrings);
339 NumInputArgStrings = RHS.NumInputArgStrings;
340 return *this;
341 }
321342 ~InputArgList();
322343
323344 const char *getArgString(unsigned Index) const override {
150150 /// is the default and means exclude nothing.
151151 /// \return An InputArgList; on error this will contain all the options
152152 /// which could be parsed.
153 InputArgList *ParseArgs(ArrayRef Args,
154 unsigned &MissingArgIndex, unsigned &MissingArgCount,
155 unsigned FlagsToInclude = 0,
156 unsigned FlagsToExclude = 0) const;
153 InputArgList ParseArgs(ArrayRef Args, unsigned &MissingArgIndex,
154 unsigned &MissingArgCount, unsigned FlagsToInclude = 0,
155 unsigned FlagsToExclude = 0) const;
157156
158157 /// \brief Render the help text for an option table.
159158 ///
112112 LibOptTable Table;
113113 unsigned MissingIndex;
114114 unsigned MissingCount;
115 std::unique_ptr Args(
116 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount));
115 llvm::opt::InputArgList Args =
116 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
117117 if (MissingCount) {
118118 llvm::errs() << "missing arg value for \""
119 << Args->getArgString(MissingIndex)
120 << "\", expected " << MissingCount
119 << Args.getArgString(MissingIndex) << "\", expected "
120 << MissingCount
121121 << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
122122 return 1;
123123 }
124 for (auto *Arg : Args->filtered(OPT_UNKNOWN))
124 for (auto *Arg : Args.filtered(OPT_UNKNOWN))
125125 llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
126126
127 if (Args->filtered_begin(OPT_INPUT) == Args->filtered_end()) {
127 if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end()) {
128128 llvm::errs() << "no input files.\n";
129129 return 1;
130130 }
131131
132 std::vector SearchPaths = getSearchPaths(Args.get(), Saver);
132 std::vector SearchPaths = getSearchPaths(&Args, Saver);
133133
134134 std::vector Members;
135 for (auto *Arg : Args->filtered(OPT_INPUT)) {
135 for (auto *Arg : Args.filtered(OPT_INPUT)) {
136136 Optional Path = findInputFile(Arg->getValue(), SearchPaths);
137137 if (!Path.hasValue()) {
138138 llvm::errs() << Arg->getValue() << ": no such file or directory\n";
142142 llvm::sys::path::filename(Arg->getValue()));
143143 }
144144
145 std::pair Result = llvm::writeArchive(
146 getOutputPath(Args.get()), Members, /*WriteSymtab=*/true);
145 std::pair Result =
146 llvm::writeArchive(getOutputPath(&Args), Members, /*WriteSymtab=*/true);
147147 if (Result.second) {
148148 if (Result.first.empty())
149149 Result.first = ArgsArr[0];
246246 return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str);
247247 }
248248
249 InputArgList *OptTable::ParseArgs(ArrayRef ArgArr,
250 unsigned &MissingArgIndex,
251 unsigned &MissingArgCount,
252 unsigned FlagsToInclude,
253 unsigned FlagsToExclude) const {
254 InputArgList *Args = new InputArgList(ArgArr.begin(), ArgArr.end());
249 InputArgList OptTable::ParseArgs(ArrayRef ArgArr,
250 unsigned &MissingArgIndex,
251 unsigned &MissingArgCount,
252 unsigned FlagsToInclude,
253 unsigned FlagsToExclude) const {
254 InputArgList Args(ArgArr.begin(), ArgArr.end());
255255
256256 // FIXME: Handle '@' args (or at least error on them).
257257
259259 unsigned Index = 0, End = ArgArr.size();
260260 while (Index < End) {
261261 // Ingore nullptrs, they are response file's EOL markers
262 if (Args->getArgString(Index) == nullptr) {
262 if (Args.getArgString(Index) == nullptr) {
263263 ++Index;
264264 continue;
265265 }
266266 // Ignore empty arguments (other things may still take them as arguments).
267 StringRef Str = Args->getArgString(Index);
267 StringRef Str = Args.getArgString(Index);
268268 if (Str == "") {
269269 ++Index;
270270 continue;
271271 }
272272
273273 unsigned Prev = Index;
274 Arg *A = ParseOneArg(*Args, Index, FlagsToInclude, FlagsToExclude);
274 Arg *A = ParseOneArg(Args, Index, FlagsToInclude, FlagsToExclude);
275275 assert(Index > Prev && "Parser failed to consume argument.");
276276
277277 // Check for missing argument error.
283283 break;
284284 }
285285
286 Args->append(A);
287 }
288
289 return Args;
286 Args.append(A);
287 }
288
289 return std::move(Args);
290290 }
291291
292292 static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
6666 TEST(Option, OptionParsing) {
6767 TestOptTable T;
6868 unsigned MAI, MAC;
69 std::unique_ptr AL(T.ParseArgs(Args, MAI, MAC));
69 InputArgList AL = T.ParseArgs(Args, MAI, MAC);
7070
7171 // Check they all exist.
72 EXPECT_TRUE(AL->hasArg(OPT_A));
73 EXPECT_TRUE(AL->hasArg(OPT_B));
74 EXPECT_TRUE(AL->hasArg(OPT_C));
75 EXPECT_TRUE(AL->hasArg(OPT_D));
76 EXPECT_TRUE(AL->hasArg(OPT_E));
77 EXPECT_TRUE(AL->hasArg(OPT_F));
78 EXPECT_TRUE(AL->hasArg(OPT_G));
72 EXPECT_TRUE(AL.hasArg(OPT_A));
73 EXPECT_TRUE(AL.hasArg(OPT_B));
74 EXPECT_TRUE(AL.hasArg(OPT_C));
75 EXPECT_TRUE(AL.hasArg(OPT_D));
76 EXPECT_TRUE(AL.hasArg(OPT_E));
77 EXPECT_TRUE(AL.hasArg(OPT_F));
78 EXPECT_TRUE(AL.hasArg(OPT_G));
7979
8080 // Check the values.
81 EXPECT_EQ(AL->getLastArgValue(OPT_B), "hi");
82 EXPECT_EQ(AL->getLastArgValue(OPT_C), "bye");
83 EXPECT_EQ(AL->getLastArgValue(OPT_D), "adena");
84 std::vector Es = AL->getAllArgValues(OPT_E);
81 EXPECT_EQ(AL.getLastArgValue(OPT_B), "hi");
82 EXPECT_EQ(AL.getLastArgValue(OPT_C), "bye");
83 EXPECT_EQ(AL.getLastArgValue(OPT_D), "adena");
84 std::vector Es = AL.getAllArgValues(OPT_E);
8585 EXPECT_EQ(Es[0], "apple");
8686 EXPECT_EQ(Es[1], "bloom");
87 EXPECT_EQ(AL->getLastArgValue(OPT_F), "42");
88 std::vector Gs = AL->getAllArgValues(OPT_G);
87 EXPECT_EQ(AL.getLastArgValue(OPT_F), "42");
88 std::vector Gs = AL.getAllArgValues(OPT_G);
8989 EXPECT_EQ(Gs[0], "chuu");
9090 EXPECT_EQ(Gs[1], "2");
9191
9696 EXPECT_NE(Help.find("-A"), std::string::npos);
9797
9898 // Test aliases.
99 arg_iterator Cs = AL->filtered_begin(OPT_C);
100 ASSERT_NE(Cs, AL->filtered_end());
99 arg_iterator Cs = AL.filtered_begin(OPT_C);
100 ASSERT_NE(Cs, AL.filtered_end());
101101 EXPECT_EQ(StringRef((*Cs)->getValue()), "desu");
102102 ArgStringList ASL;
103 (*Cs)->render(*AL, ASL);
103 (*Cs)->render(AL, ASL);
104104 ASSERT_EQ(ASL.size(), 2u);
105105 EXPECT_EQ(StringRef(ASL[0]), "-C");
106106 EXPECT_EQ(StringRef(ASL[1]), "desu");
109109 TEST(Option, ParseWithFlagExclusions) {
110110 TestOptTable T;
111111 unsigned MAI, MAC;
112 std::unique_ptr AL;
113112
114113 // Exclude flag3 to avoid parsing as OPT_SLASH_C.
115 AL.reset(T.ParseArgs(Args, MAI, MAC,
116 /*FlagsToInclude=*/0,
117 /*FlagsToExclude=*/OptFlag3));
118 EXPECT_TRUE(AL->hasArg(OPT_A));
119 EXPECT_TRUE(AL->hasArg(OPT_C));
120 EXPECT_FALSE(AL->hasArg(OPT_SLASH_C));
114 InputArgList AL = T.ParseArgs(Args, MAI, MAC,
115 /*FlagsToInclude=*/0,
116 /*FlagsToExclude=*/OptFlag3);
117 EXPECT_TRUE(AL.hasArg(OPT_A));
118 EXPECT_TRUE(AL.hasArg(OPT_C));
119 EXPECT_FALSE(AL.hasArg(OPT_SLASH_C));
121120
122121 // Exclude flag1 to avoid parsing as OPT_C.
123 AL.reset(T.ParseArgs(Args, MAI, MAC,
124 /*FlagsToInclude=*/0,
125 /*FlagsToExclude=*/OptFlag1));
126 EXPECT_TRUE(AL->hasArg(OPT_B));
127 EXPECT_FALSE(AL->hasArg(OPT_C));
128 EXPECT_TRUE(AL->hasArg(OPT_SLASH_C));
122 AL = T.ParseArgs(Args, MAI, MAC,
123 /*FlagsToInclude=*/0,
124 /*FlagsToExclude=*/OptFlag1);
125 EXPECT_TRUE(AL.hasArg(OPT_B));
126 EXPECT_FALSE(AL.hasArg(OPT_C));
127 EXPECT_TRUE(AL.hasArg(OPT_SLASH_C));
129128
130129 const char *NewArgs[] = { "/C", "foo", "--C=bar" };
131 AL.reset(T.ParseArgs(NewArgs, MAI, MAC));
132 EXPECT_TRUE(AL->hasArg(OPT_SLASH_C));
133 EXPECT_TRUE(AL->hasArg(OPT_C));
134 EXPECT_EQ(AL->getLastArgValue(OPT_SLASH_C), "foo");
135 EXPECT_EQ(AL->getLastArgValue(OPT_C), "bar");
130 AL = T.ParseArgs(NewArgs, MAI, MAC);
131 EXPECT_TRUE(AL.hasArg(OPT_SLASH_C));
132 EXPECT_TRUE(AL.hasArg(OPT_C));
133 EXPECT_EQ(AL.getLastArgValue(OPT_SLASH_C), "foo");
134 EXPECT_EQ(AL.getLastArgValue(OPT_C), "bar");
136135 }
137136
138137 TEST(Option, ParseAliasInGroup) {
140139 unsigned MAI, MAC;
141140
142141 const char *MyArgs[] = { "-I" };
143 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
144 EXPECT_TRUE(AL->hasArg(OPT_H));
142 InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
143 EXPECT_TRUE(AL.hasArg(OPT_H));
145144 }
146145
147146 TEST(Option, AliasArgs) {
149148 unsigned MAI, MAC;
150149
151150 const char *MyArgs[] = { "-J", "-Joo" };
152 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
153 EXPECT_TRUE(AL->hasArg(OPT_B));
154 EXPECT_EQ(AL->getAllArgValues(OPT_B)[0], "foo");
155 EXPECT_EQ(AL->getAllArgValues(OPT_B)[1], "bar");
151 InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
152 EXPECT_TRUE(AL.hasArg(OPT_B));
153 EXPECT_EQ(AL.getAllArgValues(OPT_B)[0], "foo");
154 EXPECT_EQ(AL.getAllArgValues(OPT_B)[1], "bar");
156155 }
157156
158157 TEST(Option, IgnoreCase) {
160159 unsigned MAI, MAC;
161160
162161 const char *MyArgs[] = { "-a", "-joo" };
163 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
164 EXPECT_TRUE(AL->hasArg(OPT_A));
165 EXPECT_TRUE(AL->hasArg(OPT_B));
162 InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
163 EXPECT_TRUE(AL.hasArg(OPT_A));
164 EXPECT_TRUE(AL.hasArg(OPT_B));
166165 }
167166
168167 TEST(Option, DoNotIgnoreCase) {
170169 unsigned MAI, MAC;
171170
172171 const char *MyArgs[] = { "-a", "-joo" };
173 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
174 EXPECT_FALSE(AL->hasArg(OPT_A));
175 EXPECT_FALSE(AL->hasArg(OPT_B));
172 InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
173 EXPECT_FALSE(AL.hasArg(OPT_A));
174 EXPECT_FALSE(AL.hasArg(OPT_B));
176175 }
177176
178177 TEST(Option, SlurpEmpty) {
180179 unsigned MAI, MAC;
181180
182181 const char *MyArgs[] = { "-A", "-slurp" };
183 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
184 EXPECT_TRUE(AL->hasArg(OPT_A));
185 EXPECT_TRUE(AL->hasArg(OPT_Slurp));
186 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp).size(), 0U);
182 InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
183 EXPECT_TRUE(AL.hasArg(OPT_A));
184 EXPECT_TRUE(AL.hasArg(OPT_Slurp));
185 EXPECT_EQ(AL.getAllArgValues(OPT_Slurp).size(), 0U);
187186 }
188187
189188 TEST(Option, Slurp) {
191190 unsigned MAI, MAC;
192191
193192 const char *MyArgs[] = { "-A", "-slurp", "-B", "--", "foo" };
194 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
195 EXPECT_EQ(AL->size(), 2U);
196 EXPECT_TRUE(AL->hasArg(OPT_A));
197 EXPECT_FALSE(AL->hasArg(OPT_B));
198 EXPECT_TRUE(AL->hasArg(OPT_Slurp));
199 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp).size(), 3U);
200 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp)[0], "-B");
201 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp)[1], "--");
202 EXPECT_EQ(AL->getAllArgValues(OPT_Slurp)[2], "foo");
193 InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
194 EXPECT_EQ(AL.size(), 2U);
195 EXPECT_TRUE(AL.hasArg(OPT_A));
196 EXPECT_FALSE(AL.hasArg(OPT_B));
197 EXPECT_TRUE(AL.hasArg(OPT_Slurp));
198 EXPECT_EQ(AL.getAllArgValues(OPT_Slurp).size(), 3U);
199 EXPECT_EQ(AL.getAllArgValues(OPT_Slurp)[0], "-B");
200 EXPECT_EQ(AL.getAllArgValues(OPT_Slurp)[1], "--");
201 EXPECT_EQ(AL.getAllArgValues(OPT_Slurp)[2], "foo");
203202 }
204203
205204 TEST(Option, FlagAliasToJoined) {
208207
209208 // Check that a flag alias provides an empty argument to a joined option.
210209 const char *MyArgs[] = { "-K" };
211 std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC));
212 EXPECT_EQ(AL->size(), 1U);
213 EXPECT_TRUE(AL->hasArg(OPT_B));
214 EXPECT_EQ(AL->getAllArgValues(OPT_B).size(), 1U);
215 EXPECT_EQ(AL->getAllArgValues(OPT_B)[0], "");
216 }
210 InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
211 EXPECT_EQ(AL.size(), 1U);
212 EXPECT_TRUE(AL.hasArg(OPT_B));
213 EXPECT_EQ(AL.getAllArgValues(OPT_B).size(), 1U);
214 EXPECT_EQ(AL.getAllArgValues(OPT_B)[0], "");
215 }