blob: 5848bb11bfa1cb9dac24c2321dcdaa8a5991566f [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"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000011#include "llvm/ADT/SmallString.h"
David Blaikief70b21a2014-04-20 22:37:46 +000012#include "llvm/ADT/STLExtras.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
57Arg *ArgList::getLastArg(OptSpecifier Id) const {
Craig Topper2617dcc2014-04-15 06:32:26 +000058 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000059 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
60 if ((*it)->getOption().matches(Id)) {
61 Res = *it;
62 Res->claim();
63 }
64 }
65
66 return Res;
67}
68
69Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
Craig Topper2617dcc2014-04-15 06:32:26 +000070 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000071 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
72 if ((*it)->getOption().matches(Id0) ||
73 (*it)->getOption().matches(Id1)) {
74 Res = *it;
75 Res->claim();
76
77 }
78 }
79
80 return Res;
81}
82
83Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
84 OptSpecifier Id2) 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(Id0) ||
88 (*it)->getOption().matches(Id1) ||
89 (*it)->getOption().matches(Id2)) {
90 Res = *it;
91 Res->claim();
92 }
93 }
94
95 return Res;
96}
97
98Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
99 OptSpecifier Id2, OptSpecifier Id3) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000100 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000101 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
102 if ((*it)->getOption().matches(Id0) ||
103 (*it)->getOption().matches(Id1) ||
104 (*it)->getOption().matches(Id2) ||
105 (*it)->getOption().matches(Id3)) {
106 Res = *it;
107 Res->claim();
108 }
109 }
110
111 return Res;
112}
113
114Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
115 OptSpecifier Id2, OptSpecifier Id3,
116 OptSpecifier Id4) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000117 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000118 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
119 if ((*it)->getOption().matches(Id0) ||
120 (*it)->getOption().matches(Id1) ||
121 (*it)->getOption().matches(Id2) ||
122 (*it)->getOption().matches(Id3) ||
123 (*it)->getOption().matches(Id4)) {
124 Res = *it;
125 Res->claim();
126 }
127 }
128
129 return Res;
130}
131
132Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
133 OptSpecifier Id2, OptSpecifier Id3,
134 OptSpecifier Id4, OptSpecifier Id5) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000135 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000136 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
137 if ((*it)->getOption().matches(Id0) ||
138 (*it)->getOption().matches(Id1) ||
139 (*it)->getOption().matches(Id2) ||
140 (*it)->getOption().matches(Id3) ||
141 (*it)->getOption().matches(Id4) ||
142 (*it)->getOption().matches(Id5)) {
143 Res = *it;
144 Res->claim();
145 }
146 }
147
148 return Res;
149}
150
151Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
152 OptSpecifier Id2, OptSpecifier Id3,
153 OptSpecifier Id4, OptSpecifier Id5,
154 OptSpecifier Id6) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000155 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000156 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
157 if ((*it)->getOption().matches(Id0) ||
158 (*it)->getOption().matches(Id1) ||
159 (*it)->getOption().matches(Id2) ||
160 (*it)->getOption().matches(Id3) ||
161 (*it)->getOption().matches(Id4) ||
162 (*it)->getOption().matches(Id5) ||
163 (*it)->getOption().matches(Id6)) {
164 Res = *it;
165 Res->claim();
166 }
167 }
168
169 return Res;
170}
171
172Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
173 OptSpecifier Id2, OptSpecifier Id3,
174 OptSpecifier Id4, OptSpecifier Id5,
175 OptSpecifier Id6, OptSpecifier Id7) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000176 Arg *Res = nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000177 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
178 if ((*it)->getOption().matches(Id0) ||
179 (*it)->getOption().matches(Id1) ||
180 (*it)->getOption().matches(Id2) ||
181 (*it)->getOption().matches(Id3) ||
182 (*it)->getOption().matches(Id4) ||
183 (*it)->getOption().matches(Id5) ||
184 (*it)->getOption().matches(Id6) ||
185 (*it)->getOption().matches(Id7)) {
186 Res = *it;
187 Res->claim();
188 }
189 }
190
191 return Res;
192}
193
194bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
195 if (Arg *A = getLastArg(Pos, Neg))
196 return A->getOption().matches(Pos);
197 return Default;
198}
199
Reid Kleckner12e03322013-06-13 18:12:12 +0000200bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
201 bool Default) const {
202 if (Arg *A = getLastArg(Pos, PosAlias, Neg))
203 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
204 return Default;
205}
206
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000207StringRef ArgList::getLastArgValue(OptSpecifier Id,
208 StringRef Default) const {
209 if (Arg *A = getLastArg(Id))
210 return A->getValue();
211 return Default;
212}
213
214std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
215 SmallVector<const char *, 16> Values;
216 AddAllArgValues(Values, Id);
217 return std::vector<std::string>(Values.begin(), Values.end());
218}
219
220void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
221 if (Arg *A = getLastArg(Id)) {
222 A->claim();
223 A->render(*this, Output);
224 }
225}
226
Reid Kleckner12e03322013-06-13 18:12:12 +0000227void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
228 OptSpecifier Id1) const {
229 if (Arg *A = getLastArg(Id0, Id1)) {
230 A->claim();
231 A->render(*this, Output);
232 }
233}
234
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000235void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
236 OptSpecifier Id1, OptSpecifier Id2) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000237 for (auto Arg: filtered(Id0, Id1, Id2)) {
238 Arg->claim();
239 Arg->render(*this, Output);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000240 }
241}
242
243void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
244 OptSpecifier Id1, OptSpecifier Id2) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000245 for (auto Arg : filtered(Id0, Id1, Id2)) {
246 Arg->claim();
247 for (unsigned i = 0, e = Arg->getNumValues(); i != e; ++i)
248 Output.push_back(Arg->getValue(i));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000249 }
250}
251
252void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
253 const char *Translation,
254 bool Joined) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000255 for (auto Arg: filtered(Id0)) {
256 Arg->claim();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000257
258 if (Joined) {
259 Output.push_back(MakeArgString(StringRef(Translation) +
Tim Northoverac002d32014-07-09 13:03:37 +0000260 Arg->getValue(0)));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000261 } else {
262 Output.push_back(Translation);
Tim Northoverac002d32014-07-09 13:03:37 +0000263 Output.push_back(Arg->getValue(0));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000264 }
265 }
266}
267
268void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
Tim Northoverac002d32014-07-09 13:03:37 +0000269 for (auto Arg : filtered(Id0))
270 Arg->claim();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000271}
272
273void ArgList::ClaimAllArgs() const {
274 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
275 if (!(*it)->isClaimed())
276 (*it)->claim();
277}
278
279const char *ArgList::MakeArgString(const Twine &T) const {
280 SmallString<256> Str;
David Blaikie8e5283a2013-12-03 18:18:28 +0000281 return MakeArgString(T.toStringRef(Str));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000282}
283
284const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
285 StringRef LHS,
286 StringRef RHS) const {
287 StringRef Cur = getArgString(Index);
288 if (Cur.size() == LHS.size() + RHS.size() &&
289 Cur.startswith(LHS) && Cur.endswith(RHS))
290 return Cur.data();
291
292 return MakeArgString(LHS + RHS);
293}
294
295//
296
297InputArgList::InputArgList(const char* const *ArgBegin,
298 const char* const *ArgEnd)
299 : NumInputArgStrings(ArgEnd - ArgBegin) {
300 ArgStrings.append(ArgBegin, ArgEnd);
301}
302
303InputArgList::~InputArgList() {
304 // An InputArgList always owns its arguments.
305 for (iterator it = begin(), ie = end(); it != ie; ++it)
306 delete *it;
307}
308
309unsigned InputArgList::MakeIndex(StringRef String0) const {
310 unsigned Index = ArgStrings.size();
311
312 // Tuck away so we have a reliable const char *.
Reid Klecknera75eba92013-07-15 16:40:52 +0000313 SynthesizedStrings.push_back(String0);
314 ArgStrings.push_back(SynthesizedStrings.back().c_str());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000315
316 return Index;
317}
318
319unsigned InputArgList::MakeIndex(StringRef String0,
320 StringRef String1) const {
321 unsigned Index0 = MakeIndex(String0);
322 unsigned Index1 = MakeIndex(String1);
323 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
324 (void) Index1;
325 return Index0;
326}
327
328const char *InputArgList::MakeArgString(StringRef Str) const {
329 return getArgString(MakeIndex(Str));
330}
331
332//
333
334DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
335 : BaseArgs(_BaseArgs) {
336}
337
David Blaikief70b21a2014-04-20 22:37:46 +0000338DerivedArgList::~DerivedArgList() {}
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000339
340const char *DerivedArgList::MakeArgString(StringRef Str) const {
341 return BaseArgs.MakeArgString(Str);
342}
343
David Blaikief70b21a2014-04-20 22:37:46 +0000344void DerivedArgList::AddSynthesizedArg(Arg *A) {
345 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
346}
347
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000348Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
Justin Bogner6f070462014-06-20 04:36:29 +0000349 SynthesizedArgs.push_back(
350 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
351 BaseArgs.MakeIndex(Opt.getName()), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000352 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000353}
354
355Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
356 StringRef Value) const {
357 unsigned Index = BaseArgs.MakeIndex(Value);
Justin Bogner6f070462014-06-20 04:36:29 +0000358 SynthesizedArgs.push_back(
359 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
360 Index, BaseArgs.getArgString(Index), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000361 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000362}
363
364Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
365 StringRef Value) const {
366 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
Justin Bogner6f070462014-06-20 04:36:29 +0000367 SynthesizedArgs.push_back(
368 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
369 Index, BaseArgs.getArgString(Index + 1), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000370 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000371}
372
373Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
374 StringRef Value) const {
375 unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str());
David Blaikief70b21a2014-04-20 22:37:46 +0000376 SynthesizedArgs.push_back(make_unique<Arg>(
Justin Bogner6f070462014-06-20 04:36:29 +0000377 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
378 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
David Blaikief70b21a2014-04-20 22:37:46 +0000379 return SynthesizedArgs.back().get();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000380}