Mercurial > hg > CbC > CbC_llvm
comparison lib/Option/ArgList.cpp @ 3:9ad51c7bc036
1st commit. remove git dir and add all files.
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 15 May 2013 06:43:32 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 3:9ad51c7bc036 |
---|---|
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/Twine.h" | |
13 #include "llvm/Option/Arg.h" | |
14 #include "llvm/Option/Option.h" | |
15 #include "llvm/Support/raw_ostream.h" | |
16 | |
17 using namespace llvm; | |
18 using namespace llvm::opt; | |
19 | |
20 void arg_iterator::SkipToNextArg() { | |
21 for (; Current != Args.end(); ++Current) { | |
22 // Done if there are no filters. | |
23 if (!Id0.isValid()) | |
24 break; | |
25 | |
26 // Otherwise require a match. | |
27 const Option &O = (*Current)->getOption(); | |
28 if (O.matches(Id0) || | |
29 (Id1.isValid() && O.matches(Id1)) || | |
30 (Id2.isValid() && O.matches(Id2))) | |
31 break; | |
32 } | |
33 } | |
34 | |
35 // | |
36 | |
37 ArgList::ArgList() { | |
38 } | |
39 | |
40 ArgList::~ArgList() { | |
41 } | |
42 | |
43 void ArgList::append(Arg *A) { | |
44 Args.push_back(A); | |
45 } | |
46 | |
47 void ArgList::eraseArg(OptSpecifier Id) { | |
48 for (iterator it = begin(), ie = end(); it != ie; ) { | |
49 if ((*it)->getOption().matches(Id)) { | |
50 it = Args.erase(it); | |
51 ie = end(); | |
52 } else { | |
53 ++it; | |
54 } | |
55 } | |
56 } | |
57 | |
58 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const { | |
59 // FIXME: Make search efficient? | |
60 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) | |
61 if ((*it)->getOption().matches(Id)) | |
62 return *it; | |
63 return 0; | |
64 } | |
65 | |
66 Arg *ArgList::getLastArg(OptSpecifier Id) const { | |
67 Arg *Res = 0; | |
68 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { | |
69 if ((*it)->getOption().matches(Id)) { | |
70 Res = *it; | |
71 Res->claim(); | |
72 } | |
73 } | |
74 | |
75 return Res; | |
76 } | |
77 | |
78 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const { | |
79 Arg *Res = 0; | |
80 for (const_iterator it = begin(), ie = end(); it != ie; ++it) { | |
81 if ((*it)->getOption().matches(Id0) || | |
82 (*it)->getOption().matches(Id1)) { | |
83 Res = *it; | |
84 Res->claim(); | |
85 | |
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 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const { | |
217 SmallVector<const char *, 16> Values; | |
218 AddAllArgValues(Values, Id); | |
219 return std::vector<std::string>(Values.begin(), Values.end()); | |
220 } | |
221 | |
222 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const { | |
223 if (Arg *A = getLastArg(Id)) { | |
224 A->claim(); | |
225 A->render(*this, Output); | |
226 } | |
227 } | |
228 | |
229 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0, | |
230 OptSpecifier Id1, OptSpecifier Id2) const { | |
231 for (arg_iterator it = filtered_begin(Id0, Id1, Id2), | |
232 ie = filtered_end(); it != ie; ++it) { | |
233 (*it)->claim(); | |
234 (*it)->render(*this, Output); | |
235 } | |
236 } | |
237 | |
238 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0, | |
239 OptSpecifier Id1, OptSpecifier Id2) const { | |
240 for (arg_iterator it = filtered_begin(Id0, Id1, Id2), | |
241 ie = filtered_end(); it != ie; ++it) { | |
242 (*it)->claim(); | |
243 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i) | |
244 Output.push_back((*it)->getValue(i)); | |
245 } | |
246 } | |
247 | |
248 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0, | |
249 const char *Translation, | |
250 bool Joined) const { | |
251 for (arg_iterator it = filtered_begin(Id0), | |
252 ie = filtered_end(); it != ie; ++it) { | |
253 (*it)->claim(); | |
254 | |
255 if (Joined) { | |
256 Output.push_back(MakeArgString(StringRef(Translation) + | |
257 (*it)->getValue(0))); | |
258 } else { | |
259 Output.push_back(Translation); | |
260 Output.push_back((*it)->getValue(0)); | |
261 } | |
262 } | |
263 } | |
264 | |
265 void ArgList::ClaimAllArgs(OptSpecifier Id0) const { | |
266 for (arg_iterator it = filtered_begin(Id0), | |
267 ie = filtered_end(); it != ie; ++it) | |
268 (*it)->claim(); | |
269 } | |
270 | |
271 void ArgList::ClaimAllArgs() const { | |
272 for (const_iterator it = begin(), ie = end(); it != ie; ++it) | |
273 if (!(*it)->isClaimed()) | |
274 (*it)->claim(); | |
275 } | |
276 | |
277 const char *ArgList::MakeArgString(const Twine &T) const { | |
278 SmallString<256> Str; | |
279 T.toVector(Str); | |
280 return MakeArgString(Str.str()); | |
281 } | |
282 | |
283 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index, | |
284 StringRef LHS, | |
285 StringRef RHS) const { | |
286 StringRef Cur = getArgString(Index); | |
287 if (Cur.size() == LHS.size() + RHS.size() && | |
288 Cur.startswith(LHS) && Cur.endswith(RHS)) | |
289 return Cur.data(); | |
290 | |
291 return MakeArgString(LHS + RHS); | |
292 } | |
293 | |
294 // | |
295 | |
296 InputArgList::InputArgList(const char* const *ArgBegin, | |
297 const char* const *ArgEnd) | |
298 : NumInputArgStrings(ArgEnd - ArgBegin) { | |
299 ArgStrings.append(ArgBegin, ArgEnd); | |
300 } | |
301 | |
302 InputArgList::~InputArgList() { | |
303 // An InputArgList always owns its arguments. | |
304 for (iterator it = begin(), ie = end(); it != ie; ++it) | |
305 delete *it; | |
306 } | |
307 | |
308 unsigned InputArgList::MakeIndex(StringRef String0) const { | |
309 unsigned Index = ArgStrings.size(); | |
310 | |
311 // Tuck away so we have a reliable const char *. | |
312 SynthesizedStrings.push_back(String0); | |
313 ArgStrings.push_back(SynthesizedStrings.back().c_str()); | |
314 | |
315 return Index; | |
316 } | |
317 | |
318 unsigned InputArgList::MakeIndex(StringRef String0, | |
319 StringRef String1) const { | |
320 unsigned Index0 = MakeIndex(String0); | |
321 unsigned Index1 = MakeIndex(String1); | |
322 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!"); | |
323 (void) Index1; | |
324 return Index0; | |
325 } | |
326 | |
327 const char *InputArgList::MakeArgString(StringRef Str) const { | |
328 return getArgString(MakeIndex(Str)); | |
329 } | |
330 | |
331 // | |
332 | |
333 DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs) | |
334 : BaseArgs(_BaseArgs) { | |
335 } | |
336 | |
337 DerivedArgList::~DerivedArgList() { | |
338 // We only own the arguments we explicitly synthesized. | |
339 for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end(); | |
340 it != ie; ++it) | |
341 delete *it; | |
342 } | |
343 | |
344 const char *DerivedArgList::MakeArgString(StringRef Str) const { | |
345 return BaseArgs.MakeArgString(Str); | |
346 } | |
347 | |
348 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const { | |
349 Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) + | |
350 Twine(Opt.getName())), | |
351 BaseArgs.MakeIndex(Opt.getName()), BaseArg); | |
352 SynthesizedArgs.push_back(A); | |
353 return A; | |
354 } | |
355 | |
356 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt, | |
357 StringRef Value) const { | |
358 unsigned Index = BaseArgs.MakeIndex(Value); | |
359 Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) + | |
360 Twine(Opt.getName())), | |
361 Index, BaseArgs.getArgString(Index), BaseArg); | |
362 SynthesizedArgs.push_back(A); | |
363 return A; | |
364 } | |
365 | |
366 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt, | |
367 StringRef Value) const { | |
368 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value); | |
369 Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) + | |
370 Twine(Opt.getName())), | |
371 Index, BaseArgs.getArgString(Index + 1), BaseArg); | |
372 SynthesizedArgs.push_back(A); | |
373 return A; | |
374 } | |
375 | |
376 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt, | |
377 StringRef Value) const { | |
378 unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str()); | |
379 Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) + | |
380 Twine(Opt.getName())), Index, | |
381 BaseArgs.getArgString(Index) + Opt.getName().size(), | |
382 BaseArg); | |
383 SynthesizedArgs.push_back(A); | |
384 return A; | |
385 } |