blob: 1b013f2c02a09374c3b3abfe95e6ccb35d8178d8 [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"
12#include "llvm/ADT/Twine.h"
13#include "llvm/Option/Arg.h"
14#include "llvm/Option/Option.h"
15#include "llvm/Support/raw_ostream.h"
16
17using namespace llvm;
18using namespace llvm::opt;
19
20void arg_iterator::SkipToNextArg() {
21 for (; Current != Args.end(); ++Current) {
22 // Done if there are no filters.
23 if (!Id0.isValid())
24 break;
25
26 // Otherwise require a match.
27 const Option &O = (*Current)->getOption();
28 if (O.matches(Id0) ||
29 (Id1.isValid() && O.matches(Id1)) ||
30 (Id2.isValid() && O.matches(Id2)))
31 break;
32 }
33}
34
35//
36
37ArgList::ArgList() {
38}
39
40ArgList::~ArgList() {
41}
42
43void ArgList::append(Arg *A) {
44 Args.push_back(A);
45}
46
47void ArgList::eraseArg(OptSpecifier Id) {
48 for (iterator it = begin(), ie = end(); it != ie; ) {
49 if ((*it)->getOption().matches(Id)) {
50 it = Args.erase(it);
51 ie = end();
52 } else {
53 ++it;
54 }
55 }
56}
57
58Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const {
59 // FIXME: Make search efficient?
60 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
61 if ((*it)->getOption().matches(Id))
62 return *it;
Craig Topper2617dcc2014-04-15 06:32:26 +000063 return nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000064}
65
66Arg *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 {
246 for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
247 ie = filtered_end(); it != ie; ++it) {
248 (*it)->claim();
249 (*it)->render(*this, Output);
250 }
251}
252
253void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
254 OptSpecifier Id1, OptSpecifier Id2) const {
255 for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
256 ie = filtered_end(); it != ie; ++it) {
257 (*it)->claim();
258 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
259 Output.push_back((*it)->getValue(i));
260 }
261}
262
263void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
264 const char *Translation,
265 bool Joined) const {
266 for (arg_iterator it = filtered_begin(Id0),
267 ie = filtered_end(); it != ie; ++it) {
268 (*it)->claim();
269
270 if (Joined) {
271 Output.push_back(MakeArgString(StringRef(Translation) +
272 (*it)->getValue(0)));
273 } else {
274 Output.push_back(Translation);
275 Output.push_back((*it)->getValue(0));
276 }
277 }
278}
279
280void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
281 for (arg_iterator it = filtered_begin(Id0),
282 ie = filtered_end(); it != ie; ++it)
283 (*it)->claim();
284}
285
286void ArgList::ClaimAllArgs() const {
287 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
288 if (!(*it)->isClaimed())
289 (*it)->claim();
290}
291
292const char *ArgList::MakeArgString(const Twine &T) const {
293 SmallString<256> Str;
David Blaikie8e5283a2013-12-03 18:18:28 +0000294 return MakeArgString(T.toStringRef(Str));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000295}
296
297const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
298 StringRef LHS,
299 StringRef RHS) const {
300 StringRef Cur = getArgString(Index);
301 if (Cur.size() == LHS.size() + RHS.size() &&
302 Cur.startswith(LHS) && Cur.endswith(RHS))
303 return Cur.data();
304
305 return MakeArgString(LHS + RHS);
306}
307
308//
309
310InputArgList::InputArgList(const char* const *ArgBegin,
311 const char* const *ArgEnd)
312 : NumInputArgStrings(ArgEnd - ArgBegin) {
313 ArgStrings.append(ArgBegin, ArgEnd);
314}
315
316InputArgList::~InputArgList() {
317 // An InputArgList always owns its arguments.
318 for (iterator it = begin(), ie = end(); it != ie; ++it)
319 delete *it;
320}
321
322unsigned InputArgList::MakeIndex(StringRef String0) const {
323 unsigned Index = ArgStrings.size();
324
325 // Tuck away so we have a reliable const char *.
Reid Klecknera75eba92013-07-15 16:40:52 +0000326 SynthesizedStrings.push_back(String0);
327 ArgStrings.push_back(SynthesizedStrings.back().c_str());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000328
329 return Index;
330}
331
332unsigned InputArgList::MakeIndex(StringRef String0,
333 StringRef String1) const {
334 unsigned Index0 = MakeIndex(String0);
335 unsigned Index1 = MakeIndex(String1);
336 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
337 (void) Index1;
338 return Index0;
339}
340
341const char *InputArgList::MakeArgString(StringRef Str) const {
342 return getArgString(MakeIndex(Str));
343}
344
345//
346
347DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
348 : BaseArgs(_BaseArgs) {
349}
350
351DerivedArgList::~DerivedArgList() {
352 // We only own the arguments we explicitly synthesized.
353 for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
354 it != ie; ++it)
355 delete *it;
356}
357
358const char *DerivedArgList::MakeArgString(StringRef Str) const {
359 return BaseArgs.MakeArgString(Str);
360}
361
362Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
363 Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
364 Twine(Opt.getName())),
365 BaseArgs.MakeIndex(Opt.getName()), BaseArg);
366 SynthesizedArgs.push_back(A);
367 return A;
368}
369
370Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
371 StringRef Value) const {
372 unsigned Index = BaseArgs.MakeIndex(Value);
373 Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
374 Twine(Opt.getName())),
375 Index, BaseArgs.getArgString(Index), BaseArg);
376 SynthesizedArgs.push_back(A);
377 return A;
378}
379
380Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
381 StringRef Value) const {
382 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
383 Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
384 Twine(Opt.getName())),
385 Index, BaseArgs.getArgString(Index + 1), BaseArg);
386 SynthesizedArgs.push_back(A);
387 return A;
388}
389
390Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
391 StringRef Value) const {
392 unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str());
393 Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
394 Twine(Opt.getName())), Index,
395 BaseArgs.getArgString(Index) + Opt.getName().size(),
396 BaseArg);
397 SynthesizedArgs.push_back(A);
398 return A;
399}