1 //===--- 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"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/STLExtras.h"
13 #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
18 using namespace llvm;
19 using namespace llvm::opt;
20
SkipToNextArg()21 void 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
~ArgList()36 ArgList::~ArgList() {
37 }
38
append(Arg * A)39 void ArgList::append(Arg *A) {
40 Args.push_back(A);
41 }
42
eraseArg(OptSpecifier Id)43 void ArgList::eraseArg(OptSpecifier Id) {
44 Args.erase(std::remove_if(begin(), end(),
45 [=](Arg *A) { return A->getOption().matches(Id); }),
46 end());
47 }
48
getLastArgNoClaim(OptSpecifier Id) const49 Arg *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;
54 return nullptr;
55 }
56
getLastArg(OptSpecifier Id) const57 Arg *ArgList::getLastArg(OptSpecifier Id) const {
58 Arg *Res = nullptr;
59 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
getLastArg(OptSpecifier Id0,OptSpecifier Id1) const69 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
70 Arg *Res = nullptr;
71 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
getLastArg(OptSpecifier Id0,OptSpecifier Id1,OptSpecifier Id2) const83 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
84 OptSpecifier Id2) const {
85 Arg *Res = nullptr;
86 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
getLastArg(OptSpecifier Id0,OptSpecifier Id1,OptSpecifier Id2,OptSpecifier Id3) const98 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
99 OptSpecifier Id2, OptSpecifier Id3) const {
100 Arg *Res = nullptr;
101 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
getLastArg(OptSpecifier Id0,OptSpecifier Id1,OptSpecifier Id2,OptSpecifier Id3,OptSpecifier Id4) const114 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
115 OptSpecifier Id2, OptSpecifier Id3,
116 OptSpecifier Id4) const {
117 Arg *Res = nullptr;
118 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
getLastArg(OptSpecifier Id0,OptSpecifier Id1,OptSpecifier Id2,OptSpecifier Id3,OptSpecifier Id4,OptSpecifier Id5) const132 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
133 OptSpecifier Id2, OptSpecifier Id3,
134 OptSpecifier Id4, OptSpecifier Id5) const {
135 Arg *Res = nullptr;
136 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
getLastArg(OptSpecifier Id0,OptSpecifier Id1,OptSpecifier Id2,OptSpecifier Id3,OptSpecifier Id4,OptSpecifier Id5,OptSpecifier Id6) const151 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
152 OptSpecifier Id2, OptSpecifier Id3,
153 OptSpecifier Id4, OptSpecifier Id5,
154 OptSpecifier Id6) const {
155 Arg *Res = nullptr;
156 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
getLastArg(OptSpecifier Id0,OptSpecifier Id1,OptSpecifier Id2,OptSpecifier Id3,OptSpecifier Id4,OptSpecifier Id5,OptSpecifier Id6,OptSpecifier Id7) const172 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
173 OptSpecifier Id2, OptSpecifier Id3,
174 OptSpecifier Id4, OptSpecifier Id5,
175 OptSpecifier Id6, OptSpecifier Id7) const {
176 Arg *Res = nullptr;
177 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
hasFlag(OptSpecifier Pos,OptSpecifier Neg,bool Default) const194 bool 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
hasFlag(OptSpecifier Pos,OptSpecifier PosAlias,OptSpecifier Neg,bool Default) const200 bool 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
getLastArgValue(OptSpecifier Id,StringRef Default) const207 StringRef ArgList::getLastArgValue(OptSpecifier Id,
208 StringRef Default) const {
209 if (Arg *A = getLastArg(Id))
210 return A->getValue();
211 return Default;
212 }
213
getAllArgValues(OptSpecifier Id) const214 std::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
AddLastArg(ArgStringList & Output,OptSpecifier Id) const220 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
221 if (Arg *A = getLastArg(Id)) {
222 A->claim();
223 A->render(*this, Output);
224 }
225 }
226
AddLastArg(ArgStringList & Output,OptSpecifier Id0,OptSpecifier Id1) const227 void 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
AddAllArgs(ArgStringList & Output,OptSpecifier Id0,OptSpecifier Id1,OptSpecifier Id2) const235 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
236 OptSpecifier Id1, OptSpecifier Id2) const {
237 for (auto Arg: filtered(Id0, Id1, Id2)) {
238 Arg->claim();
239 Arg->render(*this, Output);
240 }
241 }
242
AddAllArgValues(ArgStringList & Output,OptSpecifier Id0,OptSpecifier Id1,OptSpecifier Id2) const243 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
244 OptSpecifier Id1, OptSpecifier Id2) const {
245 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));
249 }
250 }
251
AddAllArgsTranslated(ArgStringList & Output,OptSpecifier Id0,const char * Translation,bool Joined) const252 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
253 const char *Translation,
254 bool Joined) const {
255 for (auto Arg: filtered(Id0)) {
256 Arg->claim();
257
258 if (Joined) {
259 Output.push_back(MakeArgString(StringRef(Translation) +
260 Arg->getValue(0)));
261 } else {
262 Output.push_back(Translation);
263 Output.push_back(Arg->getValue(0));
264 }
265 }
266 }
267
ClaimAllArgs(OptSpecifier Id0) const268 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
269 for (auto Arg : filtered(Id0))
270 Arg->claim();
271 }
272
ClaimAllArgs() const273 void ArgList::ClaimAllArgs() const {
274 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
275 if (!(*it)->isClaimed())
276 (*it)->claim();
277 }
278
MakeArgString(const Twine & T) const279 const char *ArgList::MakeArgString(const Twine &T) const {
280 SmallString<256> Str;
281 return MakeArgString(T.toStringRef(Str));
282 }
283
GetOrMakeJoinedArgString(unsigned Index,StringRef LHS,StringRef RHS) const284 const 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
InputArgList(const char * const * ArgBegin,const char * const * ArgEnd)297 InputArgList::InputArgList(const char* const *ArgBegin,
298 const char* const *ArgEnd)
299 : NumInputArgStrings(ArgEnd - ArgBegin) {
300 ArgStrings.append(ArgBegin, ArgEnd);
301 }
302
~InputArgList()303 InputArgList::~InputArgList() {
304 // An InputArgList always owns its arguments.
305 for (iterator it = begin(), ie = end(); it != ie; ++it)
306 delete *it;
307 }
308
MakeIndex(StringRef String0) const309 unsigned InputArgList::MakeIndex(StringRef String0) const {
310 unsigned Index = ArgStrings.size();
311
312 // Tuck away so we have a reliable const char *.
313 SynthesizedStrings.push_back(String0);
314 ArgStrings.push_back(SynthesizedStrings.back().c_str());
315
316 return Index;
317 }
318
MakeIndex(StringRef String0,StringRef String1) const319 unsigned 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
MakeArgString(StringRef Str) const328 const char *InputArgList::MakeArgString(StringRef Str) const {
329 return getArgString(MakeIndex(Str));
330 }
331
332 //
333
DerivedArgList(const InputArgList & _BaseArgs)334 DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
335 : BaseArgs(_BaseArgs) {
336 }
337
~DerivedArgList()338 DerivedArgList::~DerivedArgList() {}
339
MakeArgString(StringRef Str) const340 const char *DerivedArgList::MakeArgString(StringRef Str) const {
341 return BaseArgs.MakeArgString(Str);
342 }
343
AddSynthesizedArg(Arg * A)344 void DerivedArgList::AddSynthesizedArg(Arg *A) {
345 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
346 }
347
MakeFlagArg(const Arg * BaseArg,const Option Opt) const348 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
349 SynthesizedArgs.push_back(
350 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
351 BaseArgs.MakeIndex(Opt.getName()), BaseArg));
352 return SynthesizedArgs.back().get();
353 }
354
MakePositionalArg(const Arg * BaseArg,const Option Opt,StringRef Value) const355 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
356 StringRef Value) const {
357 unsigned Index = BaseArgs.MakeIndex(Value);
358 SynthesizedArgs.push_back(
359 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
360 Index, BaseArgs.getArgString(Index), BaseArg));
361 return SynthesizedArgs.back().get();
362 }
363
MakeSeparateArg(const Arg * BaseArg,const Option Opt,StringRef Value) const364 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
365 StringRef Value) const {
366 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
367 SynthesizedArgs.push_back(
368 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
369 Index, BaseArgs.getArgString(Index + 1), BaseArg));
370 return SynthesizedArgs.back().get();
371 }
372
MakeJoinedArg(const Arg * BaseArg,const Option Opt,StringRef Value) const373 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
374 StringRef Value) const {
375 unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str());
376 SynthesizedArgs.push_back(make_unique<Arg>(
377 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
378 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
379 return SynthesizedArgs.back().get();
380 }
381