]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/clang/lib/Driver/ArgList.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / tools / clang / lib / Driver / 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 "clang/Driver/ArgList.h"
11 #include "clang/Driver/Arg.h"
12 #include "clang/Driver/DriverDiagnostic.h"
13 #include "clang/Driver/Option.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/raw_ostream.h"
17
18 using namespace clang;
19 using namespace clang::driver;
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   return Res;
90 }
91
92 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
93                          OptSpecifier Id2) const {
94   Arg *Res = 0;
95   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
107 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
108                          OptSpecifier Id2, OptSpecifier Id3) const {
109   Arg *Res = 0;
110   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
123 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
124                          OptSpecifier Id2, OptSpecifier Id3,
125                          OptSpecifier Id4) const {
126   Arg *Res = 0;
127   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
141 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
142                          OptSpecifier Id2, OptSpecifier Id3,
143                          OptSpecifier Id4, OptSpecifier Id5) const {
144   Arg *Res = 0;
145   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
160 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
161                          OptSpecifier Id2, OptSpecifier Id3,
162                          OptSpecifier Id4, OptSpecifier Id5,
163                          OptSpecifier Id6) const {
164   Arg *Res = 0;
165   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
181 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
182                          OptSpecifier Id2, OptSpecifier Id3,
183                          OptSpecifier Id4, OptSpecifier Id5,
184                          OptSpecifier Id6, OptSpecifier Id7) const {
185   Arg *Res = 0;
186   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
203 bool 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
209 bool 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
216 StringRef ArgList::getLastArgValue(OptSpecifier Id,
217                                          StringRef Default) const {
218   if (Arg *A = getLastArg(Id))
219     return A->getValue();
220   return Default;
221 }
222
223 int ArgList::getLastArgIntValue(OptSpecifier Id, int Default,
224                                 clang::DiagnosticsEngine *Diags) const {
225   int Res = Default;
226
227   if (Arg *A = getLastArg(Id)) {
228     if (StringRef(A->getValue()).getAsInteger(10, Res)) {
229       if (Diags)
230         Diags->Report(diag::err_drv_invalid_int_value)
231           << A->getAsString(*this) << A->getValue();
232     }
233   }
234
235   return Res;
236 }
237
238 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
239   SmallVector<const char *, 16> Values;
240   AddAllArgValues(Values, Id);
241   return std::vector<std::string>(Values.begin(), Values.end());
242 }
243
244 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
245   if (Arg *A = getLastArg(Id)) {
246     A->claim();
247     A->render(*this, Output);
248   }
249 }
250
251 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
252                          OptSpecifier Id1) const {
253   if (Arg *A = getLastArg(Id0, Id1)) {
254     A->claim();
255     A->render(*this, Output);
256   }
257 }
258
259 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
260                          OptSpecifier Id1, OptSpecifier Id2) const {
261   for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
262          ie = filtered_end(); it != ie; ++it) {
263     (*it)->claim();
264     (*it)->render(*this, Output);
265   }
266 }
267
268 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
269                               OptSpecifier Id1, OptSpecifier Id2) const {
270   for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
271          ie = filtered_end(); it != ie; ++it) {
272     (*it)->claim();
273     for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
274       Output.push_back((*it)->getValue(i));
275   }
276 }
277
278 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
279                                    const char *Translation,
280                                    bool Joined) const {
281   for (arg_iterator it = filtered_begin(Id0),
282          ie = filtered_end(); it != ie; ++it) {
283     (*it)->claim();
284
285     if (Joined) {
286       Output.push_back(MakeArgString(StringRef(Translation) +
287                                      (*it)->getValue(0)));
288     } else {
289       Output.push_back(Translation);
290       Output.push_back((*it)->getValue(0));
291     }
292   }
293 }
294
295 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
296   for (arg_iterator it = filtered_begin(Id0),
297          ie = filtered_end(); it != ie; ++it)
298     (*it)->claim();
299 }
300
301 void ArgList::ClaimAllArgs() const {
302   for (const_iterator it = begin(), ie = end(); it != ie; ++it)
303     if (!(*it)->isClaimed())
304       (*it)->claim();
305 }
306
307 const char *ArgList::MakeArgString(const Twine &T) const {
308   SmallString<256> Str;
309   T.toVector(Str);
310   return MakeArgString(Str.str());
311 }
312
313 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
314                                               StringRef LHS,
315                                               StringRef RHS) const {
316   StringRef Cur = getArgString(Index);
317   if (Cur.size() == LHS.size() + RHS.size() &&
318       Cur.startswith(LHS) && Cur.endswith(RHS))
319     return Cur.data();
320
321   return MakeArgString(LHS + RHS);
322 }
323
324 void ArgList::dump() {
325   llvm::errs() << "ArgList:";
326   for (iterator it = begin(), ie = end(); it != ie; ++it) {
327     llvm::errs() << " " << (*it)->getSpelling();
328   }
329   llvm::errs() << "\n";
330 }
331
332 //
333
334 InputArgList::InputArgList(const char* const *ArgBegin,
335                            const char* const *ArgEnd)
336   : NumInputArgStrings(ArgEnd - ArgBegin) {
337   ArgStrings.append(ArgBegin, ArgEnd);
338 }
339
340 InputArgList::~InputArgList() {
341   // An InputArgList always owns its arguments.
342   for (iterator it = begin(), ie = end(); it != ie; ++it)
343     delete *it;
344 }
345
346 unsigned InputArgList::MakeIndex(StringRef String0) const {
347   unsigned Index = ArgStrings.size();
348
349   // Tuck away so we have a reliable const char *.
350   SynthesizedStrings.push_back(String0);
351   ArgStrings.push_back(SynthesizedStrings.back().c_str());
352
353   return Index;
354 }
355
356 unsigned InputArgList::MakeIndex(StringRef String0,
357                                  StringRef String1) const {
358   unsigned Index0 = MakeIndex(String0);
359   unsigned Index1 = MakeIndex(String1);
360   assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
361   (void) Index1;
362   return Index0;
363 }
364
365 const char *InputArgList::MakeArgString(StringRef Str) const {
366   return getArgString(MakeIndex(Str));
367 }
368
369 //
370
371 DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
372   : BaseArgs(_BaseArgs) {
373 }
374
375 DerivedArgList::~DerivedArgList() {
376   // We only own the arguments we explicitly synthesized.
377   for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
378        it != ie; ++it)
379     delete *it;
380 }
381
382 const char *DerivedArgList::MakeArgString(StringRef Str) const {
383   return BaseArgs.MakeArgString(Str);
384 }
385
386 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
387   Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
388                                                Twine(Opt.getName())),
389                    BaseArgs.MakeIndex(Opt.getName()), BaseArg);
390   SynthesizedArgs.push_back(A);
391   return A;
392 }
393
394 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
395                                        StringRef Value) const {
396   unsigned Index = BaseArgs.MakeIndex(Value);
397   Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
398                                                Twine(Opt.getName())),
399                    Index, BaseArgs.getArgString(Index), BaseArg);
400   SynthesizedArgs.push_back(A);
401   return A;
402 }
403
404 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
405                                      StringRef Value) const {
406   unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
407   Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
408                                                Twine(Opt.getName())),
409                    Index, BaseArgs.getArgString(Index + 1), BaseArg);
410   SynthesizedArgs.push_back(A);
411   return A;
412 }
413
414 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
415                                    StringRef Value) const {
416   unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str());
417   Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
418                                                Twine(Opt.getName())), Index,
419                    BaseArgs.getArgString(Index) + Opt.getName().size(),
420                    BaseArg);
421   SynthesizedArgs.push_back(A);
422   return A;
423 }