65cb51959eb9d1c99bcf49c10c12e9dc34d9161f
[oota-llvm.git] / lib / Option / ArgList.cpp
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
12 #include "llvm/ADT/SmallString.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
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
36 //
37
38 ArgList::ArgList() {
39 }
40
41 ArgList::~ArgList() {
42 }
43
44 void ArgList::append(Arg *A) {
45   Args.push_back(A);
46 }
47
48 void ArgList::eraseArg(OptSpecifier Id) {
49   for (iterator it = begin(), ie = end(); it != ie; ) {
50     if ((*it)->getOption().matches(Id)) {
51       it = Args.erase(it);
52       ie = end();
53     } else {
54       ++it;
55     }
56   }
57 }
58
59 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const {
60   // FIXME: Make search efficient?
61   for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
62     if ((*it)->getOption().matches(Id))
63       return *it;
64   return 0;
65 }
66
67 Arg *ArgList::getLastArg(OptSpecifier Id) const {
68   Arg *Res = 0;
69   for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
70     if ((*it)->getOption().matches(Id)) {
71       Res = *it;
72       Res->claim();
73     }
74   }
75
76   return Res;
77 }
78
79 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
80   Arg *Res = 0;
81   for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
82     if ((*it)->getOption().matches(Id0) ||
83         (*it)->getOption().matches(Id1)) {
84       Res = *it;
85       Res->claim();
86
87     }
88   }
89
90   return Res;
91 }
92
93 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
94                          OptSpecifier Id2) const {
95   Arg *Res = 0;
96   for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
97     if ((*it)->getOption().matches(Id0) ||
98         (*it)->getOption().matches(Id1) ||
99         (*it)->getOption().matches(Id2)) {
100       Res = *it;
101       Res->claim();
102     }
103   }
104
105   return Res;
106 }
107
108 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
109                          OptSpecifier Id2, OptSpecifier Id3) const {
110   Arg *Res = 0;
111   for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
112     if ((*it)->getOption().matches(Id0) ||
113         (*it)->getOption().matches(Id1) ||
114         (*it)->getOption().matches(Id2) ||
115         (*it)->getOption().matches(Id3)) {
116       Res = *it;
117       Res->claim();
118     }
119   }
120
121   return Res;
122 }
123
124 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
125                          OptSpecifier Id2, OptSpecifier Id3,
126                          OptSpecifier Id4) const {
127   Arg *Res = 0;
128   for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
129     if ((*it)->getOption().matches(Id0) ||
130         (*it)->getOption().matches(Id1) ||
131         (*it)->getOption().matches(Id2) ||
132         (*it)->getOption().matches(Id3) ||
133         (*it)->getOption().matches(Id4)) {
134       Res = *it;
135       Res->claim();
136     }
137   }
138
139   return Res;
140 }
141
142 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
143                          OptSpecifier Id2, OptSpecifier Id3,
144                          OptSpecifier Id4, OptSpecifier Id5) const {
145   Arg *Res = 0;
146   for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
147     if ((*it)->getOption().matches(Id0) ||
148         (*it)->getOption().matches(Id1) ||
149         (*it)->getOption().matches(Id2) ||
150         (*it)->getOption().matches(Id3) ||
151         (*it)->getOption().matches(Id4) ||
152         (*it)->getOption().matches(Id5)) {
153       Res = *it;
154       Res->claim();
155     }
156   }
157
158   return Res;
159 }
160
161 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
162                          OptSpecifier Id2, OptSpecifier Id3,
163                          OptSpecifier Id4, OptSpecifier Id5,
164                          OptSpecifier Id6) const {
165   Arg *Res = 0;
166   for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
167     if ((*it)->getOption().matches(Id0) ||
168         (*it)->getOption().matches(Id1) ||
169         (*it)->getOption().matches(Id2) ||
170         (*it)->getOption().matches(Id3) ||
171         (*it)->getOption().matches(Id4) ||
172         (*it)->getOption().matches(Id5) ||
173         (*it)->getOption().matches(Id6)) {
174       Res = *it;
175       Res->claim();
176     }
177   }
178
179   return Res;
180 }
181
182 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
183                          OptSpecifier Id2, OptSpecifier Id3,
184                          OptSpecifier Id4, OptSpecifier Id5,
185                          OptSpecifier Id6, OptSpecifier Id7) const {
186   Arg *Res = 0;
187   for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
188     if ((*it)->getOption().matches(Id0) ||
189         (*it)->getOption().matches(Id1) ||
190         (*it)->getOption().matches(Id2) ||
191         (*it)->getOption().matches(Id3) ||
192         (*it)->getOption().matches(Id4) ||
193         (*it)->getOption().matches(Id5) ||
194         (*it)->getOption().matches(Id6) ||
195         (*it)->getOption().matches(Id7)) {
196       Res = *it;
197       Res->claim();
198     }
199   }
200
201   return Res;
202 }
203
204 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
205   if (Arg *A = getLastArg(Pos, Neg))
206     return A->getOption().matches(Pos);
207   return Default;
208 }
209
210 StringRef ArgList::getLastArgValue(OptSpecifier Id,
211                                          StringRef Default) const {
212   if (Arg *A = getLastArg(Id))
213     return A->getValue();
214   return Default;
215 }
216
217 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
218   SmallVector<const char *, 16> Values;
219   AddAllArgValues(Values, Id);
220   return std::vector<std::string>(Values.begin(), Values.end());
221 }
222
223 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
224   if (Arg *A = getLastArg(Id)) {
225     A->claim();
226     A->render(*this, Output);
227   }
228 }
229
230 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
231                          OptSpecifier Id1, OptSpecifier Id2) const {
232   for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
233          ie = filtered_end(); it != ie; ++it) {
234     (*it)->claim();
235     (*it)->render(*this, Output);
236   }
237 }
238
239 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
240                               OptSpecifier Id1, OptSpecifier Id2) const {
241   for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
242          ie = filtered_end(); it != ie; ++it) {
243     (*it)->claim();
244     for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
245       Output.push_back((*it)->getValue(i));
246   }
247 }
248
249 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
250                                    const char *Translation,
251                                    bool Joined) const {
252   for (arg_iterator it = filtered_begin(Id0),
253          ie = filtered_end(); it != ie; ++it) {
254     (*it)->claim();
255
256     if (Joined) {
257       Output.push_back(MakeArgString(StringRef(Translation) +
258                                      (*it)->getValue(0)));
259     } else {
260       Output.push_back(Translation);
261       Output.push_back((*it)->getValue(0));
262     }
263   }
264 }
265
266 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
267   for (arg_iterator it = filtered_begin(Id0),
268          ie = filtered_end(); it != ie; ++it)
269     (*it)->claim();
270 }
271
272 void ArgList::ClaimAllArgs() const {
273   for (const_iterator it = begin(), ie = end(); it != ie; ++it)
274     if (!(*it)->isClaimed())
275       (*it)->claim();
276 }
277
278 const char *ArgList::MakeArgString(const Twine &T) const {
279   SmallString<256> Str;
280   T.toVector(Str);
281   return MakeArgString(Str.str());
282 }
283
284 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
297 InputArgList::InputArgList(const char* const *ArgBegin,
298                            const char* const *ArgEnd)
299   : NumInputArgStrings(ArgEnd - ArgBegin) {
300   ArgStrings.append(ArgBegin, ArgEnd);
301 }
302
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
309 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
319 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
328 const char *InputArgList::MakeArgString(StringRef Str) const {
329   return getArgString(MakeIndex(Str));
330 }
331
332 //
333
334 DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
335   : BaseArgs(_BaseArgs) {
336 }
337
338 DerivedArgList::~DerivedArgList() {
339   // We only own the arguments we explicitly synthesized.
340   for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
341        it != ie; ++it)
342     delete *it;
343 }
344
345 const char *DerivedArgList::MakeArgString(StringRef Str) const {
346   return BaseArgs.MakeArgString(Str);
347 }
348
349 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
350   Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
351                                                Twine(Opt.getName())),
352                    BaseArgs.MakeIndex(Opt.getName()), BaseArg);
353   SynthesizedArgs.push_back(A);
354   return A;
355 }
356
357 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
358                                        StringRef Value) const {
359   unsigned Index = BaseArgs.MakeIndex(Value);
360   Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
361                                                Twine(Opt.getName())),
362                    Index, BaseArgs.getArgString(Index), BaseArg);
363   SynthesizedArgs.push_back(A);
364   return A;
365 }
366
367 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
368                                      StringRef Value) const {
369   unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
370   Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
371                                                Twine(Opt.getName())),
372                    Index, BaseArgs.getArgString(Index + 1), BaseArg);
373   SynthesizedArgs.push_back(A);
374   return A;
375 }
376
377 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
378                                    StringRef Value) const {
379   unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str());
380   Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
381                                                Twine(Opt.getName())), Index,
382                    BaseArgs.getArgString(Index) + Opt.getName().size(),
383                    BaseArg);
384   SynthesizedArgs.push_back(A);
385   return A;
386 }