LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ArgList.cpp
Go to the documentation of this file.
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"
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 
38 }
39 
41 }
42 
44  Args.push_back(A);
45 }
46 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
217  StringRef Default) const {
218  if (Arg *A = getLastArg(Id))
219  return A->getValue();
220  return Default;
221 }
222 
223 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
225  AddAllArgValues(Values, Id);
226  return std::vector<std::string>(Values.begin(), Values.end());
227 }
228 
230  if (Arg *A = getLastArg(Id)) {
231  A->claim();
232  A->render(*this, Output);
233  }
234 }
235 
237  OptSpecifier Id1) const {
238  if (Arg *A = getLastArg(Id0, Id1)) {
239  A->claim();
240  A->render(*this, Output);
241  }
242 }
243 
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 
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 
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 
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 //
310 
311 InputArgList::InputArgList(const char* const *ArgBegin,
312  const char* const *ArgEnd)
313  : NumInputArgStrings(ArgEnd - ArgBegin) {
314  ArgStrings.append(ArgBegin, ArgEnd);
315 }
316 
318  // An InputArgList always owns its arguments.
319  for (iterator it = begin(), ie = end(); it != ie; ++it)
320  delete *it;
321 }
322 
323 unsigned InputArgList::MakeIndex(StringRef String0) const {
324  unsigned Index = ArgStrings.size();
325 
326  // Tuck away so we have a reliable const char *.
327  SynthesizedStrings.push_back(String0);
328  ArgStrings.push_back(SynthesizedStrings.back().c_str());
329 
330  return Index;
331 }
332 
334  StringRef String1) const {
335  unsigned Index0 = MakeIndex(String0);
336  unsigned Index1 = MakeIndex(String1);
337  assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
338  (void) Index1;
339  return Index0;
340 }
341 
342 const char *InputArgList::MakeArgString(StringRef Str) const {
343  return getArgString(MakeIndex(Str));
344 }
345 
346 //
347 
349  : BaseArgs(_BaseArgs) {
350 }
351 
353  // We only own the arguments we explicitly synthesized.
354  for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
355  it != ie; ++it)
356  delete *it;
357 }
358 
359 const char *DerivedArgList::MakeArgString(StringRef Str) const {
360  return BaseArgs.MakeArgString(Str);
361 }
362 
363 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
364  Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
365  Twine(Opt.getName())),
366  BaseArgs.MakeIndex(Opt.getName()), BaseArg);
367  SynthesizedArgs.push_back(A);
368  return A;
369 }
370 
371 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
372  StringRef Value) const {
373  unsigned Index = BaseArgs.MakeIndex(Value);
374  Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
375  Twine(Opt.getName())),
376  Index, BaseArgs.getArgString(Index), BaseArg);
377  SynthesizedArgs.push_back(A);
378  return A;
379 }
380 
381 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
382  StringRef Value) const {
383  unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
384  Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
385  Twine(Opt.getName())),
386  Index, BaseArgs.getArgString(Index + 1), BaseArg);
387  SynthesizedArgs.push_back(A);
388  return A;
389 }
390 
391 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
392  StringRef Value) const {
393  unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str());
394  Arg *A = new Arg(Opt, ArgList::MakeArgString(Twine(Opt.getPrefix()) +
395  Twine(Opt.getName())), Index,
396  BaseArgs.getArgString(Index) + Opt.getName().size(),
397  BaseArg);
398  SynthesizedArgs.push_back(A);
399  return A;
400 }
void toVector(SmallVectorImpl< char > &Out) const
Definition: Twine.cpp:26
virtual const char * MakeArgString(StringRef Str) const
Definition: ArgList.cpp:359
void push_back(const T &Elt)
Definition: SmallVector.h:236
bool matches(OptSpecifier ID) const
Definition: Option.cpp:87
iterator begin()
Definition: ArgList.h:128
StringRef getName() const
Get the name of this option without any prefix.
Definition: Option.h:90
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
virtual const char * MakeArgString(StringRef Str) const =0
void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0, const char *Translation, bool Joined=false) const
Definition: ArgList.cpp:263
bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:217
Arg * MakeFlagArg(const Arg *BaseArg, const Option Opt) const
MakeFlagArg - Construct a new FlagArg for the given option Id.
Definition: ArgList.cpp:363
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:181
void AddAllArgs(ArgStringList &Output, OptSpecifier Id0, OptSpecifier Id1=0U, OptSpecifier Id2=0U) const
AddAllArgs - Render all arguments matching the given ids.
Definition: ArgList.cpp:244
arglist_type::const_reverse_iterator const_reverse_iterator
Definition: ArgList.h:101
void eraseArg(OptSpecifier Id)
eraseArg - Remove any option matching Id.
Definition: ArgList.cpp:47
reverse_iterator rbegin()
Definition: ArgList.h:131
unsigned MakeIndex(StringRef String0) const
MakeIndex - Get an index for the given string(s).
Definition: ArgList.cpp:323
arg_iterator - Iterates through arguments stored inside an ArgList.
Definition: ArgList.h:28
virtual ~ArgList()
Definition: ArgList.cpp:40
const char * data() const
Definition: StringRef.h:107
Arg * getLastArgNoClaim(OptSpecifier Id) const
Definition: ArgList.cpp:58
arg_iterator filtered_end() const
Definition: ArgList.h:144
iterator end()
Definition: ArgList.h:129
A concrete instance of a particular driver option.
Definition: Arg.h:34
DerivedArgList(const InputArgList &BaseArgs)
Construct a new derived arg list from BaseArgs.
Definition: ArgList.cpp:348
void claim() const
Set the Arg claimed bit.
Definition: Arg.h:94
const char * GetOrMakeJoinedArgString(unsigned Index, StringRef LHS, StringRef RHS) const
Create an arg string for (LHS + RHS), reusing the string at Index if possible.
Definition: ArgList.cpp:298
bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const
Definition: ArgList.cpp:203
StringRef getPrefix() const
Get the default prefix for this option.
Definition: Option.h:118
void append(in_iter in_start, in_iter in_end)
Definition: SmallVector.h:445
iterator erase(iterator I)
Definition: SmallVector.h:478
reverse_iterator rend()
Definition: ArgList.h:132
void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const
AddLastArg - Render only the last argument match Id0, if present.
Definition: ArgList.cpp:229
void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0, OptSpecifier Id1=0U, OptSpecifier Id2=0U) const
Definition: ArgList.cpp:253
virtual const char * MakeArgString(StringRef Str) const
Definition: ArgList.cpp:342
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:208
virtual const char * getArgString(unsigned Index) const =0
getArgString - Return the input argument string at Index.
Arg * MakeJoinedArg(const Arg *BaseArg, const Option Opt, StringRef Value) const
Definition: ArgList.cpp:391
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:270
Defines the llvm::Arg class for parsed arguments.
Arg * MakeSeparateArg(const Arg *BaseArg, const Option Opt, StringRef Value) const
Definition: ArgList.cpp:381
Arg * MakePositionalArg(const Arg *BaseArg, const Option Opt, StringRef Value) const
Definition: ArgList.cpp:371
void ClaimAllArgs() const
Definition: ArgList.cpp:286
void append(Arg *A)
append - Append A to the arg list.
Definition: ArgList.cpp:43
OptSpecifier - Wrapper class for abstracting references to option IDs.
Definition: OptSpecifier.h:18
Arg * getLastArg(OptSpecifier Id) const
Definition: ArgList.cpp:66
InputArgList(const char *const *ArgBegin, const char *const *ArgEnd)
Definition: ArgList.cpp:311
std::vector< std::string > getAllArgValues(OptSpecifier Id) const
Definition: ArgList.cpp:223
LLVM Value Representation.
Definition: Value.h:66
virtual const char * getArgString(unsigned Index) const
getArgString - Return the input argument string at Index.
Definition: ArgList.h:310
arg_iterator filtered_begin(OptSpecifier Id0=0U, OptSpecifier Id1=0U, OptSpecifier Id2=0U) const
Definition: ArgList.h:140
bool isValid() const
Definition: OptSpecifier.h:29
StringRef getLastArgValue(OptSpecifier Id, StringRef Default="") const
getLastArgValue - Return the value of the last argument, or a default.
Definition: ArgList.cpp:216