blob: 7ff358a57e043bf03326d969a157d9364b649c1f [file] [log] [blame]
Michael J. Spencer41ee0412012-12-05 00:29:32 +00001//===--- ArgList.cpp - Argument List Management ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Option/ArgList.h"
David Blaikief70b21a2014-04-20 22:37:46 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000012#include "llvm/ADT/SmallString.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000013#include "llvm/ADT/Twine.h"
14#include "llvm/Option/Arg.h"
15#include "llvm/Option/Option.h"
Eric Christopher9a8b5e72015-12-18 18:55:26 +000016#include "llvm/Support/Debug.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000017#include "llvm/Support/raw_ostream.h"
18
19using namespace llvm;
20using namespace llvm::opt;
21
22void arg_iterator::SkipToNextArg() {
23 for (; Current != Args.end(); ++Current) {
24 // Done if there are no filters.
25 if (!Id0.isValid())
26 break;
27
28 // Otherwise require a match.
29 const Option &O = (*Current)->getOption();
30 if (O.matches(Id0) ||
31 (Id1.isValid() && O.matches(Id1)) ||
32 (Id2.isValid() && O.matches(Id2)))
33 break;
34 }
35}
36
Michael J. Spencer41ee0412012-12-05 00:29:32 +000037void ArgList::append(Arg *A) {
38 Args.push_back(A);
39}
40
41void ArgList::eraseArg(OptSpecifier Id) {
David Majnemerc7004902016-08-12 04:32:37 +000042 Args.erase(
43 remove_if(*this, [=](Arg *A) { return A->getOption().matches(Id); }),
44 end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +000045}
46
47Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const {
48 // FIXME: Make search efficient?
49 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
50 if ((*it)->getOption().matches(Id))
51 return *it;
Craig Topper2617dcc2014-04-15 06:32:26 +000052 return nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000053}
54
Ehsan Akhgari63e3a292014-09-12 19:42:53 +000055Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1) const {
56 // FIXME: Make search efficient?
57 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
58 if ((*it)->getOption().matches(Id0) ||
59 (*it)->getOption().matches(Id1))
60 return *it;
61 return nullptr;
62}
63
Filipe Cabecinhas008067a2015-03-20 23:32:58 +000064Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1,
65 OptSpecifier Id2) const {
66 // FIXME: Make search efficient?
67 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
68 if ((*it)->getOption().matches(Id0) || (*it)->getOption().matches(Id1) ||
69 (*it)->getOption().matches(Id2))
70 return *it;
71 return nullptr;
72}
73
74Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1,
75 OptSpecifier Id2, OptSpecifier Id3) const {
76 // FIXME: Make search efficient?
77 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
78 if ((*it)->getOption().matches(Id0) || (*it)->getOption().matches(Id1) ||
79 (*it)->getOption().matches(Id2) || (*it)->getOption().matches(Id3))
80 return *it;
81 return nullptr;
82}
83
Michael J. Spencer41ee0412012-12-05 00:29:32 +000084Arg *ArgList::getLastArg(OptSpecifier Id) const {
Craig Topper2617dcc2014-04-15 06:32:26 +000085 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000086 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
87 if ((*it)->getOption().matches(Id)) {
88 Res = *it;
89 Res->claim();
90 }
91 }
92
93 return Res;
94}
95
96Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
Craig Topper2617dcc2014-04-15 06:32:26 +000097 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000098 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
99 if ((*it)->getOption().matches(Id0) ||
100 (*it)->getOption().matches(Id1)) {
101 Res = *it;
102 Res->claim();
103
104 }
105 }
106
107 return Res;
108}
109
110Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
111 OptSpecifier Id2) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000112 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000113 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
114 if ((*it)->getOption().matches(Id0) ||
115 (*it)->getOption().matches(Id1) ||
116 (*it)->getOption().matches(Id2)) {
117 Res = *it;
118 Res->claim();
119 }
120 }
121
122 return Res;
123}
124
125Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
126 OptSpecifier Id2, OptSpecifier Id3) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000127 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000128 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
129 if ((*it)->getOption().matches(Id0) ||
130 (*it)->getOption().matches(Id1) ||
131 (*it)->getOption().matches(Id2) ||
132 (*it)->getOption().matches(Id3)) {
133 Res = *it;
134 Res->claim();
135 }
136 }
137
138 return Res;
139}
140
141Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
142 OptSpecifier Id2, OptSpecifier Id3,
143 OptSpecifier Id4) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000144 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000145 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
146 if ((*it)->getOption().matches(Id0) ||
147 (*it)->getOption().matches(Id1) ||
148 (*it)->getOption().matches(Id2) ||
149 (*it)->getOption().matches(Id3) ||
150 (*it)->getOption().matches(Id4)) {
151 Res = *it;
152 Res->claim();
153 }
154 }
155
156 return Res;
157}
158
159Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
160 OptSpecifier Id2, OptSpecifier Id3,
161 OptSpecifier Id4, OptSpecifier Id5) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000162 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000163 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
164 if ((*it)->getOption().matches(Id0) ||
165 (*it)->getOption().matches(Id1) ||
166 (*it)->getOption().matches(Id2) ||
167 (*it)->getOption().matches(Id3) ||
168 (*it)->getOption().matches(Id4) ||
169 (*it)->getOption().matches(Id5)) {
170 Res = *it;
171 Res->claim();
172 }
173 }
174
175 return Res;
176}
177
178Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
179 OptSpecifier Id2, OptSpecifier Id3,
180 OptSpecifier Id4, OptSpecifier Id5,
181 OptSpecifier Id6) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000182 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000183 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
184 if ((*it)->getOption().matches(Id0) ||
185 (*it)->getOption().matches(Id1) ||
186 (*it)->getOption().matches(Id2) ||
187 (*it)->getOption().matches(Id3) ||
188 (*it)->getOption().matches(Id4) ||
189 (*it)->getOption().matches(Id5) ||
190 (*it)->getOption().matches(Id6)) {
191 Res = *it;
192 Res->claim();
193 }
194 }
195
196 return Res;
197}
198
199Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
200 OptSpecifier Id2, OptSpecifier Id3,
201 OptSpecifier Id4, OptSpecifier Id5,
202 OptSpecifier Id6, OptSpecifier Id7) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000203 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000204 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
205 if ((*it)->getOption().matches(Id0) ||
206 (*it)->getOption().matches(Id1) ||
207 (*it)->getOption().matches(Id2) ||
208 (*it)->getOption().matches(Id3) ||
209 (*it)->getOption().matches(Id4) ||
210 (*it)->getOption().matches(Id5) ||
211 (*it)->getOption().matches(Id6) ||
212 (*it)->getOption().matches(Id7)) {
213 Res = *it;
214 Res->claim();
215 }
216 }
217
218 return Res;
219}
220
221bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
222 if (Arg *A = getLastArg(Pos, Neg))
223 return A->getOption().matches(Pos);
224 return Default;
225}
226
Reid Kleckner12e03322013-06-13 18:12:12 +0000227bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
228 bool Default) const {
229 if (Arg *A = getLastArg(Pos, PosAlias, Neg))
230 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
231 return Default;
232}
233
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000234StringRef ArgList::getLastArgValue(OptSpecifier Id,
235 StringRef Default) const {
236 if (Arg *A = getLastArg(Id))
237 return A->getValue();
238 return Default;
239}
240
241std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
242 SmallVector<const char *, 16> Values;
243 AddAllArgValues(Values, Id);
244 return std::vector<std::string>(Values.begin(), Values.end());
245}
246
247void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
248 if (Arg *A = getLastArg(Id)) {
249 A->claim();
250 A->render(*this, Output);
251 }
252}
253
Reid Kleckner12e03322013-06-13 18:12:12 +0000254void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
255 OptSpecifier Id1) const {
256 if (Arg *A = getLastArg(Id0, Id1)) {
257 A->claim();
258 A->render(*this, Output);
259 }
260}
261
Douglas Katzman93a9a832016-09-29 19:47:58 +0000262void ArgList::AddAllArgsExcept(ArgStringList &Output,
263 ArrayRef<OptSpecifier> Ids,
264 ArrayRef<OptSpecifier> ExcludeIds) const {
Douglas Katzmanf2b96082015-07-29 17:34:41 +0000265 for (const Arg *Arg : Args) {
Douglas Katzman93a9a832016-09-29 19:47:58 +0000266 bool Excluded = false;
267 for (OptSpecifier Id : ExcludeIds) {
Douglas Katzmanf2b96082015-07-29 17:34:41 +0000268 if (Arg->getOption().matches(Id)) {
Douglas Katzman93a9a832016-09-29 19:47:58 +0000269 Excluded = true;
Douglas Katzmanf2b96082015-07-29 17:34:41 +0000270 break;
271 }
272 }
Douglas Katzman93a9a832016-09-29 19:47:58 +0000273 if (!Excluded) {
274 for (OptSpecifier Id : Ids) {
275 if (Arg->getOption().matches(Id)) {
276 Arg->claim();
277 Arg->render(*this, Output);
278 break;
279 }
280 }
281 }
Douglas Katzmanf2b96082015-07-29 17:34:41 +0000282 }
283}
284
Douglas Katzman93a9a832016-09-29 19:47:58 +0000285/// This is a nicer interface when you don't have a list of Ids to exclude.
286void ArgList::AddAllArgs(ArgStringList &Output,
287 ArrayRef<OptSpecifier> Ids) const {
288 ArrayRef<OptSpecifier> Exclude = None;
289 AddAllArgsExcept(Output, Ids, Exclude);
290}
291
Douglas Katzmanf2b96082015-07-29 17:34:41 +0000292/// This 3-opt variant of AddAllArgs could be eliminated in favor of one
293/// that accepts a single specifier, given the above which accepts any number.
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000294void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
295 OptSpecifier Id1, OptSpecifier Id2) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000296 for (auto Arg: filtered(Id0, Id1, Id2)) {
297 Arg->claim();
298 Arg->render(*this, Output);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000299 }
300}
301
302void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
303 OptSpecifier Id1, OptSpecifier Id2) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000304 for (auto Arg : filtered(Id0, Id1, Id2)) {
305 Arg->claim();
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000306 const auto &Values = Arg->getValues();
307 Output.append(Values.begin(), Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000308 }
309}
310
311void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
312 const char *Translation,
313 bool Joined) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000314 for (auto Arg: filtered(Id0)) {
315 Arg->claim();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000316
317 if (Joined) {
318 Output.push_back(MakeArgString(StringRef(Translation) +
Tim Northoverac002d32014-07-09 13:03:37 +0000319 Arg->getValue(0)));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000320 } else {
321 Output.push_back(Translation);
Tim Northoverac002d32014-07-09 13:03:37 +0000322 Output.push_back(Arg->getValue(0));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000323 }
324 }
325}
326
327void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000328 for (auto Arg : filtered(Id0))
329 Arg->claim();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000330}
331
332void ArgList::ClaimAllArgs() const {
333 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
334 if (!(*it)->isClaimed())
335 (*it)->claim();
336}
337
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000338const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
339 StringRef LHS,
340 StringRef RHS) const {
341 StringRef Cur = getArgString(Index);
342 if (Cur.size() == LHS.size() + RHS.size() &&
343 Cur.startswith(LHS) && Cur.endswith(RHS))
344 return Cur.data();
345
346 return MakeArgString(LHS + RHS);
347}
348
Eric Christopher9a8b5e72015-12-18 18:55:26 +0000349void ArgList::print(raw_ostream &O) const {
Eric Christopher42b56ee2015-12-18 18:55:22 +0000350 for (Arg *A : *this) {
Eric Christopher9a8b5e72015-12-18 18:55:26 +0000351 O << "* ";
352 A->print(O);
Eric Christopher42b56ee2015-12-18 18:55:22 +0000353 }
354}
355
Matthias Braun8c209aa2017-01-28 02:02:38 +0000356#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eric Christopher9a8b5e72015-12-18 18:55:26 +0000357LLVM_DUMP_METHOD void ArgList::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000358#endif
Eric Christopher9a8b5e72015-12-18 18:55:26 +0000359
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000360//
361
Benjamin Kramer49381be2015-06-23 15:28:10 +0000362void InputArgList::releaseMemory() {
363 // An InputArgList always owns its arguments.
364 for (Arg *A : *this)
365 delete A;
366}
367
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000368InputArgList::InputArgList(const char* const *ArgBegin,
369 const char* const *ArgEnd)
370 : NumInputArgStrings(ArgEnd - ArgBegin) {
371 ArgStrings.append(ArgBegin, ArgEnd);
372}
373
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000374unsigned InputArgList::MakeIndex(StringRef String0) const {
375 unsigned Index = ArgStrings.size();
376
377 // Tuck away so we have a reliable const char *.
Reid Klecknera75eba92013-07-15 16:40:52 +0000378 SynthesizedStrings.push_back(String0);
379 ArgStrings.push_back(SynthesizedStrings.back().c_str());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000380
381 return Index;
382}
383
384unsigned InputArgList::MakeIndex(StringRef String0,
385 StringRef String1) const {
386 unsigned Index0 = MakeIndex(String0);
387 unsigned Index1 = MakeIndex(String1);
388 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
389 (void) Index1;
390 return Index0;
391}
392
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000393const char *InputArgList::MakeArgStringRef(StringRef Str) const {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000394 return getArgString(MakeIndex(Str));
395}
396
397//
398
David Blaikie9f380a32015-03-16 18:06:57 +0000399DerivedArgList::DerivedArgList(const InputArgList &BaseArgs)
400 : BaseArgs(BaseArgs) {}
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000401
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000402const char *DerivedArgList::MakeArgStringRef(StringRef Str) const {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000403 return BaseArgs.MakeArgString(Str);
404}
405
David Blaikief70b21a2014-04-20 22:37:46 +0000406void DerivedArgList::AddSynthesizedArg(Arg *A) {
407 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
408}
409
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000410Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
Justin Bogner6f070462014-06-20 04:36:29 +0000411 SynthesizedArgs.push_back(
412 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
413 BaseArgs.MakeIndex(Opt.getName()), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000414 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000415}
416
417Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
418 StringRef Value) const {
419 unsigned Index = BaseArgs.MakeIndex(Value);
Justin Bogner6f070462014-06-20 04:36:29 +0000420 SynthesizedArgs.push_back(
421 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
422 Index, BaseArgs.getArgString(Index), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000423 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000424}
425
426Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
427 StringRef Value) const {
428 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
Justin Bogner6f070462014-06-20 04:36:29 +0000429 SynthesizedArgs.push_back(
430 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
431 Index, BaseArgs.getArgString(Index + 1), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000432 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000433}
434
435Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
436 StringRef Value) const {
Yaron Keren075759a2015-03-30 15:42:36 +0000437 unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
David Blaikief70b21a2014-04-20 22:37:46 +0000438 SynthesizedArgs.push_back(make_unique<Arg>(
Justin Bogner6f070462014-06-20 04:36:29 +0000439 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
440 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000441 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000442}