]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Driver/ArgList.cpp
Upgrade our copy of llvm/clang to trunk r178860, in preparation of the
[FreeBSD/FreeBSD.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 StringRef ArgList::getLastArgValue(OptSpecifier Id,
210                                          StringRef Default) const {
211   if (Arg *A = getLastArg(Id))
212     return A->getValue();
213   return Default;
214 }
215
216 int ArgList::getLastArgIntValue(OptSpecifier Id, int Default,
217                                 clang::DiagnosticsEngine *Diags) const {
218   int Res = Default;
219
220   if (Arg *A = getLastArg(Id)) {
221     if (StringRef(A->getValue()).getAsInteger(10, Res)) {
222       if (Diags)
223         Diags->Report(diag::err_drv_invalid_int_value)
224           << A->getAsString(*this) << A->getValue();
225     }
226   }
227
228   return Res;
229 }
230
231 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
232   SmallVector<const char *, 16> Values;
233   AddAllArgValues(Values, Id);
234   return std::vector<std::string>(Values.begin(), Values.end());
235 }
236
237 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
238   if (Arg *A = getLastArg(Id)) {
239     A->claim();
240     A->render(*this, Output);
241   }
242 }
243
244 void 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
253 void 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
263 void 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
280 void 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
286 void ArgList::ClaimAllArgs() const {
287   for (const_iterator it = begin(), ie = end(); it != ie; ++it)
288     if (!(*it)->isClaimed())
289       (*it)->claim();
290 }
291
292 const char *ArgList::MakeArgString(const Twine &T) const {
293   SmallString<256> Str;
294   T.toVector(Str);
295   return MakeArgString(Str.str());
296 }
297
298 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
299                                               StringRef LHS,
300                                               StringRef RHS) const {
301   StringRef Cur = getArgString(Index);
302   if (Cur.size() == LHS.size() + RHS.size() &&
303       Cur.startswith(LHS) && Cur.endswith(RHS))
304     return Cur.data();
305
306   return MakeArgString(LHS + RHS);
307 }
308
309 void ArgList::dump() {
310   llvm::errs() << "ArgList:";
311   for (iterator it = begin(), ie = end(); it != ie; ++it) {
312     llvm::errs() << " " << (*it)->getSpelling();
313   }
314   llvm::errs() << "\n";
315 }
316
317 //
318
319 InputArgList::InputArgList(const char* const *ArgBegin,
320                            const char* const *ArgEnd)
321   : NumInputArgStrings(ArgEnd - ArgBegin) {
322   ArgStrings.append(ArgBegin, ArgEnd);
323 }
324
325 InputArgList::~InputArgList() {
326   // An InputArgList always owns its arguments.
327   for (iterator it = begin(), ie = end(); it != ie; ++it)
328     delete *it;
329 }
330
331 unsigned InputArgList::MakeIndex(StringRef String0) const {
332   unsigned Index = ArgStrings.size();
333
334   // Tuck away so we have a reliable const char *.
335   SynthesizedStrings.push_back(String0);
336   ArgStrings.push_back(SynthesizedStrings.back().c_str());
337
338   return Index;
339 }
340
341 unsigned InputArgList::MakeIndex(StringRef String0,
342                                  StringRef String1) const {
343   unsigned Index0 = MakeIndex(String0);
344   unsigned Index1 = MakeIndex(String1);
345   assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
346   (void) Index1;
347   return Index0;
348 }
349
350 const char *InputArgList::MakeArgString(StringRef Str) const {
351   return getArgString(MakeIndex(Str));
352 }
353
354 //
355
356 DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
357   : BaseArgs(_BaseArgs) {
358 }
359
360 DerivedArgList::~DerivedArgList() {
361   // We only own the arguments we explicitly synthesized.
362   for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
363        it != ie; ++it)
364     delete *it;
365 }
366
367 const char *DerivedArgList::MakeArgString(StringRef Str) const {
368   return BaseArgs.MakeArgString(Str);
369 }
370
371 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
372   Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
373                                                Twine(Opt.getName())),
374                    BaseArgs.MakeIndex(Opt.getName()), BaseArg);
375   SynthesizedArgs.push_back(A);
376   return A;
377 }
378
379 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
380                                        StringRef Value) const {
381   unsigned Index = BaseArgs.MakeIndex(Value);
382   Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
383                                                Twine(Opt.getName())),
384                    Index, BaseArgs.getArgString(Index), BaseArg);
385   SynthesizedArgs.push_back(A);
386   return A;
387 }
388
389 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
390                                      StringRef Value) const {
391   unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
392   Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
393                                                Twine(Opt.getName())),
394                    Index, BaseArgs.getArgString(Index + 1), BaseArg);
395   SynthesizedArgs.push_back(A);
396   return A;
397 }
398
399 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
400                                    StringRef Value) const {
401   unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str());
402   Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
403                                                Twine(Opt.getName())), Index,
404                    BaseArgs.getArgString(Index) + Opt.getName().size(),
405                    BaseArg);
406   SynthesizedArgs.push_back(A);
407   return A;
408 }