blob: 2de55ce73ddd5b342b17846992ee311a3fc33b2d [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
Michael J. Spencer41ee0412012-12-05 00:29:32 +000066Arg *ArgList::getLastArg(OptSpecifier Id) const {
Craig Topper2617dcc2014-04-15 06:32:26 +000067 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000068 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
69 if ((*it)->getOption().matches(Id)) {
70 Res = *it;
71 Res->claim();
72 }
73 }
74
75 return Res;
76}
77
78Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
Craig Topper2617dcc2014-04-15 06:32:26 +000079 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000080 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
81 if ((*it)->getOption().matches(Id0) ||
82 (*it)->getOption().matches(Id1)) {
83 Res = *it;
84 Res->claim();
85
86 }
87 }
88
89 return Res;
90}
91
92Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
93 OptSpecifier Id2) const {
Craig Topper2617dcc2014-04-15 06:32:26 +000094 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000095 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
96 if ((*it)->getOption().matches(Id0) ||
97 (*it)->getOption().matches(Id1) ||
98 (*it)->getOption().matches(Id2)) {
99 Res = *it;
100 Res->claim();
101 }
102 }
103
104 return Res;
105}
106
107Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
108 OptSpecifier Id2, OptSpecifier Id3) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000109 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000110 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
111 if ((*it)->getOption().matches(Id0) ||
112 (*it)->getOption().matches(Id1) ||
113 (*it)->getOption().matches(Id2) ||
114 (*it)->getOption().matches(Id3)) {
115 Res = *it;
116 Res->claim();
117 }
118 }
119
120 return Res;
121}
122
123Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
124 OptSpecifier Id2, OptSpecifier Id3,
125 OptSpecifier Id4) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000126 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000127 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
128 if ((*it)->getOption().matches(Id0) ||
129 (*it)->getOption().matches(Id1) ||
130 (*it)->getOption().matches(Id2) ||
131 (*it)->getOption().matches(Id3) ||
132 (*it)->getOption().matches(Id4)) {
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, OptSpecifier Id5) 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 (*it)->getOption().matches(Id5)) {
152 Res = *it;
153 Res->claim();
154 }
155 }
156
157 return Res;
158}
159
160Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
161 OptSpecifier Id2, OptSpecifier Id3,
162 OptSpecifier Id4, OptSpecifier Id5,
163 OptSpecifier Id6) 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 (*it)->getOption().matches(Id6)) {
173 Res = *it;
174 Res->claim();
175 }
176 }
177
178 return Res;
179}
180
181Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
182 OptSpecifier Id2, OptSpecifier Id3,
183 OptSpecifier Id4, OptSpecifier Id5,
184 OptSpecifier Id6, OptSpecifier Id7) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000185 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000186 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
187 if ((*it)->getOption().matches(Id0) ||
188 (*it)->getOption().matches(Id1) ||
189 (*it)->getOption().matches(Id2) ||
190 (*it)->getOption().matches(Id3) ||
191 (*it)->getOption().matches(Id4) ||
192 (*it)->getOption().matches(Id5) ||
193 (*it)->getOption().matches(Id6) ||
194 (*it)->getOption().matches(Id7)) {
195 Res = *it;
196 Res->claim();
197 }
198 }
199
200 return Res;
201}
202
203bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
204 if (Arg *A = getLastArg(Pos, Neg))
205 return A->getOption().matches(Pos);
206 return Default;
207}
208
Reid Kleckner12e03322013-06-13 18:12:12 +0000209bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
210 bool Default) const {
211 if (Arg *A = getLastArg(Pos, PosAlias, Neg))
212 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
213 return Default;
214}
215
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000216StringRef ArgList::getLastArgValue(OptSpecifier Id,
217 StringRef Default) const {
218 if (Arg *A = getLastArg(Id))
219 return A->getValue();
220 return Default;
221}
222
223std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
224 SmallVector<const char *, 16> Values;
225 AddAllArgValues(Values, Id);
226 return std::vector<std::string>(Values.begin(), Values.end());
227}
228
229void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
230 if (Arg *A = getLastArg(Id)) {
231 A->claim();
232 A->render(*this, Output);
233 }
234}
235
Reid Kleckner12e03322013-06-13 18:12:12 +0000236void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
237 OptSpecifier Id1) const {
238 if (Arg *A = getLastArg(Id0, Id1)) {
239 A->claim();
240 A->render(*this, Output);
241 }
242}
243
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000244void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
245 OptSpecifier Id1, OptSpecifier Id2) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000246 for (auto Arg: filtered(Id0, Id1, Id2)) {
247 Arg->claim();
248 Arg->render(*this, Output);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000249 }
250}
251
252void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
253 OptSpecifier Id1, OptSpecifier Id2) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000254 for (auto Arg : filtered(Id0, Id1, Id2)) {
255 Arg->claim();
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000256 const auto &Values = Arg->getValues();
257 Output.append(Values.begin(), Values.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000258 }
259}
260
261void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
262 const char *Translation,
263 bool Joined) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000264 for (auto Arg: filtered(Id0)) {
265 Arg->claim();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000266
267 if (Joined) {
268 Output.push_back(MakeArgString(StringRef(Translation) +
Tim Northoverac002d32014-07-09 13:03:37 +0000269 Arg->getValue(0)));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000270 } else {
271 Output.push_back(Translation);
Tim Northoverac002d32014-07-09 13:03:37 +0000272 Output.push_back(Arg->getValue(0));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000273 }
274 }
275}
276
277void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000278 for (auto Arg : filtered(Id0))
279 Arg->claim();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000280}
281
282void ArgList::ClaimAllArgs() const {
283 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
284 if (!(*it)->isClaimed())
285 (*it)->claim();
286}
287
288const char *ArgList::MakeArgString(const Twine &T) const {
289 SmallString<256> Str;
David Blaikie8e5283a2013-12-03 18:18:28 +0000290 return MakeArgString(T.toStringRef(Str));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000291}
292
293const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
294 StringRef LHS,
295 StringRef RHS) const {
296 StringRef Cur = getArgString(Index);
297 if (Cur.size() == LHS.size() + RHS.size() &&
298 Cur.startswith(LHS) && Cur.endswith(RHS))
299 return Cur.data();
300
301 return MakeArgString(LHS + RHS);
302}
303
304//
305
306InputArgList::InputArgList(const char* const *ArgBegin,
307 const char* const *ArgEnd)
308 : NumInputArgStrings(ArgEnd - ArgBegin) {
309 ArgStrings.append(ArgBegin, ArgEnd);
310}
311
312InputArgList::~InputArgList() {
313 // An InputArgList always owns its arguments.
314 for (iterator it = begin(), ie = end(); it != ie; ++it)
315 delete *it;
316}
317
318unsigned InputArgList::MakeIndex(StringRef String0) const {
319 unsigned Index = ArgStrings.size();
320
321 // Tuck away so we have a reliable const char *.
Reid Klecknera75eba92013-07-15 16:40:52 +0000322 SynthesizedStrings.push_back(String0);
323 ArgStrings.push_back(SynthesizedStrings.back().c_str());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000324
325 return Index;
326}
327
328unsigned InputArgList::MakeIndex(StringRef String0,
329 StringRef String1) const {
330 unsigned Index0 = MakeIndex(String0);
331 unsigned Index1 = MakeIndex(String1);
332 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
333 (void) Index1;
334 return Index0;
335}
336
337const char *InputArgList::MakeArgString(StringRef Str) const {
338 return getArgString(MakeIndex(Str));
339}
340
341//
342
David Blaikie9f380a32015-03-16 18:06:57 +0000343DerivedArgList::DerivedArgList(const InputArgList &BaseArgs)
344 : BaseArgs(BaseArgs) {}
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000345
David Blaikief70b21a2014-04-20 22:37:46 +0000346DerivedArgList::~DerivedArgList() {}
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000347
348const char *DerivedArgList::MakeArgString(StringRef Str) const {
349 return BaseArgs.MakeArgString(Str);
350}
351
David Blaikief70b21a2014-04-20 22:37:46 +0000352void DerivedArgList::AddSynthesizedArg(Arg *A) {
353 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
354}
355
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000356Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
Justin Bogner6f070462014-06-20 04:36:29 +0000357 SynthesizedArgs.push_back(
358 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
359 BaseArgs.MakeIndex(Opt.getName()), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000360 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000361}
362
363Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
364 StringRef Value) const {
365 unsigned Index = BaseArgs.MakeIndex(Value);
Justin Bogner6f070462014-06-20 04:36:29 +0000366 SynthesizedArgs.push_back(
367 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
368 Index, BaseArgs.getArgString(Index), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000369 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000370}
371
372Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
373 StringRef Value) const {
374 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
Justin Bogner6f070462014-06-20 04:36:29 +0000375 SynthesizedArgs.push_back(
376 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
377 Index, BaseArgs.getArgString(Index + 1), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000378 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000379}
380
381Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
382 StringRef Value) const {
383 unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str());
David Blaikief70b21a2014-04-20 22:37:46 +0000384 SynthesizedArgs.push_back(make_unique<Arg>(
Justin Bogner6f070462014-06-20 04:36:29 +0000385 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
386 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000387 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000388}