blob: b771a18a106e8c7835892193786a341b1fcbbdb4 [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"
16#include "llvm/Support/raw_ostream.h"
17
18using namespace llvm;
19using namespace llvm::opt;
20
21void arg_iterator::SkipToNextArg() {
22 for (; Current != Args.end(); ++Current) {
23 // Done if there are no filters.
24 if (!Id0.isValid())
25 break;
26
27 // Otherwise require a match.
28 const Option &O = (*Current)->getOption();
29 if (O.matches(Id0) ||
30 (Id1.isValid() && O.matches(Id1)) ||
31 (Id2.isValid() && O.matches(Id2)))
32 break;
33 }
34}
35
Michael J. Spencer41ee0412012-12-05 00:29:32 +000036ArgList::~ArgList() {
37}
38
39void ArgList::append(Arg *A) {
40 Args.push_back(A);
41}
42
43void ArgList::eraseArg(OptSpecifier Id) {
Benjamin Kramerf9d2d512014-05-18 15:14:13 +000044 Args.erase(std::remove_if(begin(), end(),
45 [=](Arg *A) { return A->getOption().matches(Id); }),
46 end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +000047}
48
49Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const {
50 // FIXME: Make search efficient?
51 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
52 if ((*it)->getOption().matches(Id))
53 return *it;
Craig Topper2617dcc2014-04-15 06:32:26 +000054 return nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000055}
56
Ehsan Akhgari63e3a292014-09-12 19:42:53 +000057Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1) const {
58 // FIXME: Make search efficient?
59 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
60 if ((*it)->getOption().matches(Id0) ||
61 (*it)->getOption().matches(Id1))
62 return *it;
63 return nullptr;
64}
65
Filipe Cabecinhas008067a2015-03-20 23:32:58 +000066Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1,
67 OptSpecifier Id2) const {
68 // FIXME: Make search efficient?
69 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
70 if ((*it)->getOption().matches(Id0) || (*it)->getOption().matches(Id1) ||
71 (*it)->getOption().matches(Id2))
72 return *it;
73 return nullptr;
74}
75
76Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1,
77 OptSpecifier Id2, OptSpecifier Id3) const {
78 // FIXME: Make search efficient?
79 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
80 if ((*it)->getOption().matches(Id0) || (*it)->getOption().matches(Id1) ||
81 (*it)->getOption().matches(Id2) || (*it)->getOption().matches(Id3))
82 return *it;
83 return nullptr;
84}
85
Michael J. Spencer41ee0412012-12-05 00:29:32 +000086Arg *ArgList::getLastArg(OptSpecifier Id) const {
Craig Topper2617dcc2014-04-15 06:32:26 +000087 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000088 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
89 if ((*it)->getOption().matches(Id)) {
90 Res = *it;
91 Res->claim();
92 }
93 }
94
95 return Res;
96}
97
98Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
Craig Topper2617dcc2014-04-15 06:32:26 +000099 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000100 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
101 if ((*it)->getOption().matches(Id0) ||
102 (*it)->getOption().matches(Id1)) {
103 Res = *it;
104 Res->claim();
105
106 }
107 }
108
109 return Res;
110}
111
112Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
113 OptSpecifier Id2) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000114 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000115 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
116 if ((*it)->getOption().matches(Id0) ||
117 (*it)->getOption().matches(Id1) ||
118 (*it)->getOption().matches(Id2)) {
119 Res = *it;
120 Res->claim();
121 }
122 }
123
124 return Res;
125}
126
127Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
128 OptSpecifier Id2, OptSpecifier Id3) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000129 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000130 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
131 if ((*it)->getOption().matches(Id0) ||
132 (*it)->getOption().matches(Id1) ||
133 (*it)->getOption().matches(Id2) ||
134 (*it)->getOption().matches(Id3)) {
135 Res = *it;
136 Res->claim();
137 }
138 }
139
140 return Res;
141}
142
143Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
144 OptSpecifier Id2, OptSpecifier Id3,
145 OptSpecifier Id4) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000146 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000147 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
148 if ((*it)->getOption().matches(Id0) ||
149 (*it)->getOption().matches(Id1) ||
150 (*it)->getOption().matches(Id2) ||
151 (*it)->getOption().matches(Id3) ||
152 (*it)->getOption().matches(Id4)) {
153 Res = *it;
154 Res->claim();
155 }
156 }
157
158 return Res;
159}
160
161Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
162 OptSpecifier Id2, OptSpecifier Id3,
163 OptSpecifier Id4, OptSpecifier Id5) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000164 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000165 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
166 if ((*it)->getOption().matches(Id0) ||
167 (*it)->getOption().matches(Id1) ||
168 (*it)->getOption().matches(Id2) ||
169 (*it)->getOption().matches(Id3) ||
170 (*it)->getOption().matches(Id4) ||
171 (*it)->getOption().matches(Id5)) {
172 Res = *it;
173 Res->claim();
174 }
175 }
176
177 return Res;
178}
179
180Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
181 OptSpecifier Id2, OptSpecifier Id3,
182 OptSpecifier Id4, OptSpecifier Id5,
183 OptSpecifier Id6) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000184 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000185 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
186 if ((*it)->getOption().matches(Id0) ||
187 (*it)->getOption().matches(Id1) ||
188 (*it)->getOption().matches(Id2) ||
189 (*it)->getOption().matches(Id3) ||
190 (*it)->getOption().matches(Id4) ||
191 (*it)->getOption().matches(Id5) ||
192 (*it)->getOption().matches(Id6)) {
193 Res = *it;
194 Res->claim();
195 }
196 }
197
198 return Res;
199}
200
201Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
202 OptSpecifier Id2, OptSpecifier Id3,
203 OptSpecifier Id4, OptSpecifier Id5,
204 OptSpecifier Id6, OptSpecifier Id7) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000205 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000206 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
207 if ((*it)->getOption().matches(Id0) ||
208 (*it)->getOption().matches(Id1) ||
209 (*it)->getOption().matches(Id2) ||
210 (*it)->getOption().matches(Id3) ||
211 (*it)->getOption().matches(Id4) ||
212 (*it)->getOption().matches(Id5) ||
213 (*it)->getOption().matches(Id6) ||
214 (*it)->getOption().matches(Id7)) {
215 Res = *it;
216 Res->claim();
217 }
218 }
219
220 return Res;
221}
222
223bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
224 if (Arg *A = getLastArg(Pos, Neg))
225 return A->getOption().matches(Pos);
226 return Default;
227}
228
Reid Kleckner12e03322013-06-13 18:12:12 +0000229bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
230 bool Default) const {
231 if (Arg *A = getLastArg(Pos, PosAlias, Neg))
232 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
233 return Default;
234}
235
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000236StringRef ArgList::getLastArgValue(OptSpecifier Id,
237 StringRef Default) const {
238 if (Arg *A = getLastArg(Id))
239 return A->getValue();
240 return Default;
241}
242
243std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
244 SmallVector<const char *, 16> Values;
245 AddAllArgValues(Values, Id);
246 return std::vector<std::string>(Values.begin(), Values.end());
247}
248
249void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
250 if (Arg *A = getLastArg(Id)) {
251 A->claim();
252 A->render(*this, Output);
253 }
254}
255
Reid Kleckner12e03322013-06-13 18:12:12 +0000256void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
257 OptSpecifier Id1) const {
258 if (Arg *A = getLastArg(Id0, Id1)) {
259 A->claim();
260 A->render(*this, Output);
261 }
262}
263
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000264void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
265 OptSpecifier Id1, OptSpecifier Id2) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000266 for (auto Arg: filtered(Id0, Id1, Id2)) {
267 Arg->claim();
268 Arg->render(*this, Output);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000269 }
270}
271
272void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
273 OptSpecifier Id1, OptSpecifier Id2) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000274 for (auto Arg : filtered(Id0, Id1, Id2)) {
275 Arg->claim();
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000276 const auto &Values = Arg->getValues();
277 Output.append(Values.begin(), Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000278 }
279}
280
281void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
282 const char *Translation,
283 bool Joined) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000284 for (auto Arg: filtered(Id0)) {
285 Arg->claim();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000286
287 if (Joined) {
288 Output.push_back(MakeArgString(StringRef(Translation) +
Tim Northoverac002d32014-07-09 13:03:37 +0000289 Arg->getValue(0)));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000290 } else {
291 Output.push_back(Translation);
Tim Northoverac002d32014-07-09 13:03:37 +0000292 Output.push_back(Arg->getValue(0));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000293 }
294 }
295}
296
297void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000298 for (auto Arg : filtered(Id0))
299 Arg->claim();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000300}
301
302void ArgList::ClaimAllArgs() const {
303 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
304 if (!(*it)->isClaimed())
305 (*it)->claim();
306}
307
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000308const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
309 StringRef LHS,
310 StringRef RHS) const {
311 StringRef Cur = getArgString(Index);
312 if (Cur.size() == LHS.size() + RHS.size() &&
313 Cur.startswith(LHS) && Cur.endswith(RHS))
314 return Cur.data();
315
316 return MakeArgString(LHS + RHS);
317}
318
319//
320
321InputArgList::InputArgList(const char* const *ArgBegin,
322 const char* const *ArgEnd)
323 : NumInputArgStrings(ArgEnd - ArgBegin) {
324 ArgStrings.append(ArgBegin, ArgEnd);
325}
326
327InputArgList::~InputArgList() {
328 // An InputArgList always owns its arguments.
329 for (iterator it = begin(), ie = end(); it != ie; ++it)
330 delete *it;
331}
332
333unsigned InputArgList::MakeIndex(StringRef String0) const {
334 unsigned Index = ArgStrings.size();
335
336 // Tuck away so we have a reliable const char *.
Reid Klecknera75eba92013-07-15 16:40:52 +0000337 SynthesizedStrings.push_back(String0);
338 ArgStrings.push_back(SynthesizedStrings.back().c_str());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000339
340 return Index;
341}
342
343unsigned InputArgList::MakeIndex(StringRef String0,
344 StringRef String1) const {
345 unsigned Index0 = MakeIndex(String0);
346 unsigned Index1 = MakeIndex(String1);
347 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
348 (void) Index1;
349 return Index0;
350}
351
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000352const char *InputArgList::MakeArgStringRef(StringRef Str) const {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000353 return getArgString(MakeIndex(Str));
354}
355
356//
357
David Blaikie9f380a32015-03-16 18:06:57 +0000358DerivedArgList::DerivedArgList(const InputArgList &BaseArgs)
359 : BaseArgs(BaseArgs) {}
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000360
David Blaikief70b21a2014-04-20 22:37:46 +0000361DerivedArgList::~DerivedArgList() {}
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000362
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000363const char *DerivedArgList::MakeArgStringRef(StringRef Str) const {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000364 return BaseArgs.MakeArgString(Str);
365}
366
David Blaikief70b21a2014-04-20 22:37:46 +0000367void DerivedArgList::AddSynthesizedArg(Arg *A) {
368 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
369}
370
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000371Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
Justin Bogner6f070462014-06-20 04:36:29 +0000372 SynthesizedArgs.push_back(
373 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
374 BaseArgs.MakeIndex(Opt.getName()), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000375 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000376}
377
378Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
379 StringRef Value) const {
380 unsigned Index = BaseArgs.MakeIndex(Value);
Justin Bogner6f070462014-06-20 04:36:29 +0000381 SynthesizedArgs.push_back(
382 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
383 Index, BaseArgs.getArgString(Index), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000384 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000385}
386
387Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
388 StringRef Value) const {
389 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
Justin Bogner6f070462014-06-20 04:36:29 +0000390 SynthesizedArgs.push_back(
391 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
392 Index, BaseArgs.getArgString(Index + 1), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000393 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000394}
395
396Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
397 StringRef Value) const {
Yaron Keren075759a2015-03-30 15:42:36 +0000398 unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
David Blaikief70b21a2014-04-20 22:37:46 +0000399 SynthesizedArgs.push_back(make_unique<Arg>(
Justin Bogner6f070462014-06-20 04:36:29 +0000400 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
401 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000402 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000403}