]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Driver/ArgList.cpp
Merge wpa_supplicant and hostapd 0.7.3.
[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
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/raw_ostream.h"
18
19 using namespace clang;
20 using namespace clang::driver;
21
22 void arg_iterator::SkipToNextArg() {
23   for (; Current != Args.end(); ++Current) {
24     // Done if there are no filters.
25     if (!Id0.isValid())
26       break;
27
28     // Otherwise require a match.
29     const Option &O = (*Current)->getOption();
30     if (O.matches(Id0) ||
31         (Id1.isValid() && O.matches(Id1)) ||
32         (Id2.isValid() && O.matches(Id2)))
33       break;
34   }
35 }
36
37 //
38
39 ArgList::ArgList() {
40 }
41
42 ArgList::~ArgList() {
43 }
44
45 void ArgList::append(Arg *A) {
46   Args.push_back(A);
47 }
48
49 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 0;
55 }
56
57 Arg *ArgList::getLastArg(OptSpecifier Id) const {
58   Arg *A = getLastArgNoClaim(Id);
59   if (A)
60     A->claim();
61   return A;
62 }
63
64 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
65   Arg *Res = 0;
66   for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) {
67     if ((*it)->getOption().matches(Id0) ||
68         (*it)->getOption().matches(Id1)) {
69       Res = *it;
70       break;
71     }
72   }
73
74   if (Res)
75     Res->claim();
76
77   return Res;
78 }
79
80 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
81                          OptSpecifier Id2) const {
82   Arg *Res = 0;
83   for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) {
84     if ((*it)->getOption().matches(Id0) ||
85         (*it)->getOption().matches(Id1) ||
86         (*it)->getOption().matches(Id2)) {
87       Res = *it;
88       break;
89     }
90   }
91
92   if (Res)
93     Res->claim();
94
95   return Res;
96 }
97
98 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
99                          OptSpecifier Id2, OptSpecifier Id3) const {
100   Arg *Res = 0;
101   for (const_reverse_iterator it = rbegin(), ie = rend(); 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       break;
108     }
109   }
110
111   if (Res)
112     Res->claim();
113
114   return Res;
115 }
116
117 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
118   if (Arg *A = getLastArg(Pos, Neg))
119     return A->getOption().matches(Pos);
120   return Default;
121 }
122
123 llvm::StringRef ArgList::getLastArgValue(OptSpecifier Id,
124                                          llvm::StringRef Default) const {
125   if (Arg *A = getLastArg(Id))
126     return A->getValue(*this);
127   return Default;
128 }
129
130 int ArgList::getLastArgIntValue(OptSpecifier Id, int Default,
131                                 clang::Diagnostic &Diags) const {
132   int Res = Default;
133
134   if (Arg *A = getLastArg(Id)) {
135     if (llvm::StringRef(A->getValue(*this)).getAsInteger(10, Res))
136       Diags.Report(diag::err_drv_invalid_int_value)
137         << A->getAsString(*this) << A->getValue(*this);
138   }
139
140   return Res;
141 }
142
143 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
144   llvm::SmallVector<const char *, 16> Values;
145   AddAllArgValues(Values, Id);
146   return std::vector<std::string>(Values.begin(), Values.end());
147 }
148
149 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
150   if (Arg *A = getLastArg(Id)) {
151     A->claim();
152     A->render(*this, Output);
153   }
154 }
155
156 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
157                          OptSpecifier Id1, OptSpecifier Id2) const {
158   for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
159          ie = filtered_end(); it != ie; ++it) {
160     (*it)->claim();
161     (*it)->render(*this, Output);
162   }
163 }
164
165 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
166                               OptSpecifier Id1, OptSpecifier Id2) const {
167   for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
168          ie = filtered_end(); it != ie; ++it) {
169     (*it)->claim();
170     for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
171       Output.push_back((*it)->getValue(*this, i));
172   }
173 }
174
175 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
176                                    const char *Translation,
177                                    bool Joined) const {
178   for (arg_iterator it = filtered_begin(Id0),
179          ie = filtered_end(); it != ie; ++it) {
180     (*it)->claim();
181
182     if (Joined) {
183       Output.push_back(MakeArgString(llvm::StringRef(Translation) +
184                                      (*it)->getValue(*this, 0)));
185     } else {
186       Output.push_back(Translation);
187       Output.push_back((*it)->getValue(*this, 0));
188     }
189   }
190 }
191
192 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
193   for (arg_iterator it = filtered_begin(Id0),
194          ie = filtered_end(); it != ie; ++it)
195     (*it)->claim();
196 }
197
198 const char *ArgList::MakeArgString(const llvm::Twine &T) const {
199   llvm::SmallString<256> Str;
200   T.toVector(Str);
201   return MakeArgString(Str.str());
202 }
203
204 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
205                                               llvm::StringRef LHS,
206                                               llvm::StringRef RHS) const {
207   llvm::StringRef Cur = getArgString(Index);
208   if (Cur.size() == LHS.size() + RHS.size() &&
209       Cur.startswith(LHS) && Cur.endswith(RHS))
210     return Cur.data();
211
212   return MakeArgString(LHS + RHS);
213 }
214
215 //
216
217 InputArgList::InputArgList(const char **ArgBegin, const char **ArgEnd)
218   : NumInputArgStrings(ArgEnd - ArgBegin) {
219   ArgStrings.append(ArgBegin, ArgEnd);
220 }
221
222 InputArgList::~InputArgList() {
223   // An InputArgList always owns its arguments.
224   for (iterator it = begin(), ie = end(); it != ie; ++it)
225     delete *it;
226 }
227
228 unsigned InputArgList::MakeIndex(llvm::StringRef String0) const {
229   unsigned Index = ArgStrings.size();
230
231   // Tuck away so we have a reliable const char *.
232   SynthesizedStrings.push_back(String0);
233   ArgStrings.push_back(SynthesizedStrings.back().c_str());
234
235   return Index;
236 }
237
238 unsigned InputArgList::MakeIndex(llvm::StringRef String0,
239                                  llvm::StringRef String1) const {
240   unsigned Index0 = MakeIndex(String0);
241   unsigned Index1 = MakeIndex(String1);
242   assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
243   (void) Index1;
244   return Index0;
245 }
246
247 const char *InputArgList::MakeArgString(llvm::StringRef Str) const {
248   return getArgString(MakeIndex(Str));
249 }
250
251 //
252
253 DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
254   : BaseArgs(_BaseArgs) {
255 }
256
257 DerivedArgList::~DerivedArgList() {
258   // We only own the arguments we explicitly synthesized.
259   for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
260        it != ie; ++it)
261     delete *it;
262 }
263
264 const char *DerivedArgList::MakeArgString(llvm::StringRef Str) const {
265   return BaseArgs.MakeArgString(Str);
266 }
267
268 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
269   Arg *A = new Arg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
270   SynthesizedArgs.push_back(A);
271   return A;
272 }
273
274 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
275                                        llvm::StringRef Value) const {
276   unsigned Index = BaseArgs.MakeIndex(Value);
277   Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index), BaseArg);
278   SynthesizedArgs.push_back(A);
279   return A;
280 }
281
282 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
283                                      llvm::StringRef Value) const {
284   unsigned Index = BaseArgs.MakeIndex(Opt->getName(), Value);
285   Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index + 1), BaseArg);
286   SynthesizedArgs.push_back(A);
287   return A;
288 }
289
290 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
291                                    llvm::StringRef Value) const {
292   unsigned Index = BaseArgs.MakeIndex(Opt->getName() + Value.str());
293   Arg *A = new Arg(Opt, Index,
294                    BaseArgs.getArgString(Index) + strlen(Opt->getName()),
295                    BaseArg);
296   SynthesizedArgs.push_back(A);
297   return A;
298 }