LLVM  3.7.0
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/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Option/Arg.h"
15 #include "llvm/Option/Option.h"
17 
18 using namespace llvm;
19 using namespace llvm::opt;
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 
37  Args.push_back(A);
38 }
39 
41  Args.erase(std::remove_if(begin(), end(),
42  [=](Arg *A) { return A->getOption().matches(Id); }),
43  end());
44 }
45 
47  // FIXME: Make search efficient?
48  for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
49  if ((*it)->getOption().matches(Id))
50  return *it;
51  return nullptr;
52 }
53 
55  // FIXME: Make search efficient?
56  for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
57  if ((*it)->getOption().matches(Id0) ||
58  (*it)->getOption().matches(Id1))
59  return *it;
60  return nullptr;
61 }
62 
64  OptSpecifier Id2) const {
65  // FIXME: Make search efficient?
66  for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
67  if ((*it)->getOption().matches(Id0) || (*it)->getOption().matches(Id1) ||
68  (*it)->getOption().matches(Id2))
69  return *it;
70  return nullptr;
71 }
72 
74  OptSpecifier Id2, OptSpecifier Id3) const {
75  // FIXME: Make search efficient?
76  for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
77  if ((*it)->getOption().matches(Id0) || (*it)->getOption().matches(Id1) ||
78  (*it)->getOption().matches(Id2) || (*it)->getOption().matches(Id3))
79  return *it;
80  return nullptr;
81 }
82 
84  Arg *Res = nullptr;
85  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
86  if ((*it)->getOption().matches(Id)) {
87  Res = *it;
88  Res->claim();
89  }
90  }
91 
92  return Res;
93 }
94 
96  Arg *Res = nullptr;
97  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
98  if ((*it)->getOption().matches(Id0) ||
99  (*it)->getOption().matches(Id1)) {
100  Res = *it;
101  Res->claim();
102 
103  }
104  }
105 
106  return Res;
107 }
108 
110  OptSpecifier Id2) const {
111  Arg *Res = nullptr;
112  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
113  if ((*it)->getOption().matches(Id0) ||
114  (*it)->getOption().matches(Id1) ||
115  (*it)->getOption().matches(Id2)) {
116  Res = *it;
117  Res->claim();
118  }
119  }
120 
121  return Res;
122 }
123 
125  OptSpecifier Id2, OptSpecifier Id3) const {
126  Arg *Res = nullptr;
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  Res = *it;
133  Res->claim();
134  }
135  }
136 
137  return Res;
138 }
139 
141  OptSpecifier Id2, OptSpecifier Id3,
142  OptSpecifier Id4) const {
143  Arg *Res = nullptr;
144  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
145  if ((*it)->getOption().matches(Id0) ||
146  (*it)->getOption().matches(Id1) ||
147  (*it)->getOption().matches(Id2) ||
148  (*it)->getOption().matches(Id3) ||
149  (*it)->getOption().matches(Id4)) {
150  Res = *it;
151  Res->claim();
152  }
153  }
154 
155  return Res;
156 }
157 
159  OptSpecifier Id2, OptSpecifier Id3,
160  OptSpecifier Id4, OptSpecifier Id5) const {
161  Arg *Res = nullptr;
162  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
163  if ((*it)->getOption().matches(Id0) ||
164  (*it)->getOption().matches(Id1) ||
165  (*it)->getOption().matches(Id2) ||
166  (*it)->getOption().matches(Id3) ||
167  (*it)->getOption().matches(Id4) ||
168  (*it)->getOption().matches(Id5)) {
169  Res = *it;
170  Res->claim();
171  }
172  }
173 
174  return Res;
175 }
176 
178  OptSpecifier Id2, OptSpecifier Id3,
179  OptSpecifier Id4, OptSpecifier Id5,
180  OptSpecifier Id6) const {
181  Arg *Res = nullptr;
182  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
183  if ((*it)->getOption().matches(Id0) ||
184  (*it)->getOption().matches(Id1) ||
185  (*it)->getOption().matches(Id2) ||
186  (*it)->getOption().matches(Id3) ||
187  (*it)->getOption().matches(Id4) ||
188  (*it)->getOption().matches(Id5) ||
189  (*it)->getOption().matches(Id6)) {
190  Res = *it;
191  Res->claim();
192  }
193  }
194 
195  return Res;
196 }
197 
199  OptSpecifier Id2, OptSpecifier Id3,
200  OptSpecifier Id4, OptSpecifier Id5,
201  OptSpecifier Id6, OptSpecifier Id7) const {
202  Arg *Res = nullptr;
203  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
204  if ((*it)->getOption().matches(Id0) ||
205  (*it)->getOption().matches(Id1) ||
206  (*it)->getOption().matches(Id2) ||
207  (*it)->getOption().matches(Id3) ||
208  (*it)->getOption().matches(Id4) ||
209  (*it)->getOption().matches(Id5) ||
210  (*it)->getOption().matches(Id6) ||
211  (*it)->getOption().matches(Id7)) {
212  Res = *it;
213  Res->claim();
214  }
215  }
216 
217  return Res;
218 }
219 
220 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
221  if (Arg *A = getLastArg(Pos, Neg))
222  return A->getOption().matches(Pos);
223  return Default;
224 }
225 
227  bool Default) const {
228  if (Arg *A = getLastArg(Pos, PosAlias, Neg))
229  return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
230  return Default;
231 }
232 
234  StringRef Default) const {
235  if (Arg *A = getLastArg(Id))
236  return A->getValue();
237  return Default;
238 }
239 
240 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
242  AddAllArgValues(Values, Id);
243  return std::vector<std::string>(Values.begin(), Values.end());
244 }
245 
247  if (Arg *A = getLastArg(Id)) {
248  A->claim();
249  A->render(*this, Output);
250  }
251 }
252 
254  OptSpecifier Id1) const {
255  if (Arg *A = getLastArg(Id0, Id1)) {
256  A->claim();
257  A->render(*this, Output);
258  }
259 }
260 
262  OptSpecifier Id1, OptSpecifier Id2) const {
263  for (auto Arg: filtered(Id0, Id1, Id2)) {
264  Arg->claim();
265  Arg->render(*this, Output);
266  }
267 }
268 
270  OptSpecifier Id1, OptSpecifier Id2) const {
271  for (auto Arg : filtered(Id0, Id1, Id2)) {
272  Arg->claim();
273  const auto &Values = Arg->getValues();
274  Output.append(Values.begin(), Values.end());
275  }
276 }
277 
279  const char *Translation,
280  bool Joined) const {
281  for (auto Arg: filtered(Id0)) {
282  Arg->claim();
283 
284  if (Joined) {
285  Output.push_back(MakeArgString(StringRef(Translation) +
286  Arg->getValue(0)));
287  } else {
288  Output.push_back(Translation);
289  Output.push_back(Arg->getValue(0));
290  }
291  }
292 }
293 
295  for (auto Arg : filtered(Id0))
296  Arg->claim();
297 }
298 
299 void ArgList::ClaimAllArgs() const {
300  for (const_iterator it = begin(), ie = end(); it != ie; ++it)
301  if (!(*it)->isClaimed())
302  (*it)->claim();
303 }
304 
305 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
306  StringRef LHS,
307  StringRef RHS) const {
308  StringRef Cur = getArgString(Index);
309  if (Cur.size() == LHS.size() + RHS.size() &&
310  Cur.startswith(LHS) && Cur.endswith(RHS))
311  return Cur.data();
312 
313  return MakeArgString(LHS + RHS);
314 }
315 
316 //
317 
318 void InputArgList::releaseMemory() {
319  // An InputArgList always owns its arguments.
320  for (Arg *A : *this)
321  delete A;
322 }
323 
324 InputArgList::InputArgList(const char* const *ArgBegin,
325  const char* const *ArgEnd)
326  : NumInputArgStrings(ArgEnd - ArgBegin) {
327  ArgStrings.append(ArgBegin, ArgEnd);
328 }
329 
330 unsigned InputArgList::MakeIndex(StringRef String0) const {
331  unsigned Index = ArgStrings.size();
332 
333  // Tuck away so we have a reliable const char *.
334  SynthesizedStrings.push_back(String0);
335  ArgStrings.push_back(SynthesizedStrings.back().c_str());
336 
337  return Index;
338 }
339 
341  StringRef String1) const {
342  unsigned Index0 = MakeIndex(String0);
343  unsigned Index1 = MakeIndex(String1);
344  assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
345  (void) Index1;
346  return Index0;
347 }
348 
350  return getArgString(MakeIndex(Str));
351 }
352 
353 //
354 
356  : BaseArgs(BaseArgs) {}
357 
359  return BaseArgs.MakeArgString(Str);
360 }
361 
363  SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
364 }
365 
366 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
367  SynthesizedArgs.push_back(
368  make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
369  BaseArgs.MakeIndex(Opt.getName()), BaseArg));
370  return SynthesizedArgs.back().get();
371 }
372 
373 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
374  StringRef Value) const {
375  unsigned Index = BaseArgs.MakeIndex(Value);
376  SynthesizedArgs.push_back(
377  make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
378  Index, BaseArgs.getArgString(Index), BaseArg));
379  return SynthesizedArgs.back().get();
380 }
381 
382 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
383  StringRef Value) const {
384  unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
385  SynthesizedArgs.push_back(
386  make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
387  Index, BaseArgs.getArgString(Index + 1), BaseArg));
388  return SynthesizedArgs.back().get();
389 }
390 
391 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
392  StringRef Value) const {
393  unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
394  SynthesizedArgs.push_back(make_unique<Arg>(
395  Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
396  BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
397  return SynthesizedArgs.back().get();
398 }
void push_back(const T &Elt)
Definition: SmallVector.h:222
bool matches(OptSpecifier ID) const
matches - Predicate for whether this option is part of the given option (which may be a group)...
Definition: Option.cpp:84
const Option & getOption() const
Definition: Arg.h:70
iterator begin()
Definition: ArgList.h:142
StringRef getName() const
Get the name of this option without any prefix.
Definition: Option.h:89
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0, const char *Translation, bool Joined=false) const
AddAllArgsTranslated - Render all the arguments matching the given ids, but forced to separate args a...
Definition: ArgList.cpp:278
bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:224
Arg * MakeFlagArg(const Arg *BaseArg, const Option Opt) const
MakeFlagArg - Construct a new FlagArg for the given option Id.
Definition: ArgList.cpp:366
void AddAllArgs(ArgStringList &Output, OptSpecifier Id0, OptSpecifier Id1=0U, OptSpecifier Id2=0U) const
AddAllArgs - Render all arguments matching the given ids.
Definition: ArgList.cpp:261
arglist_type::const_reverse_iterator const_reverse_iterator
Definition: ArgList.h:100
void eraseArg(OptSpecifier Id)
eraseArg - Remove any option matching Id.
Definition: ArgList.cpp:40
const char * getValue(unsigned N=0) const
Definition: Arg.h:92
reverse_iterator rbegin()
Definition: ArgList.h:145
const char * getArgString(unsigned Index) const override
getArgString - Return the input argument string at Index.
Definition: ArgList.h:347
unsigned MakeIndex(StringRef String0) const
MakeIndex - Get an index for the given string(s).
Definition: ArgList.cpp:330
SmallVectorImpl< const char * > & getValues()
Definition: Arg.h:96
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:107
void AddSynthesizedArg(Arg *A)
AddSynthesizedArg - Add a argument to the list of synthesized arguments (to be freed).
Definition: ArgList.cpp:362
Arg * getLastArgNoClaim(OptSpecifier Id) const
getLastArg - Return the last argument matching Id, or null.
Definition: ArgList.cpp:46
const char * MakeArgStringRef(StringRef Str) const override
Construct a constant string pointer whose lifetime will match that of the ArgList.
Definition: ArgList.cpp:358
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:44
iterator end()
Definition: ArgList.h:143
A concrete instance of a particular driver option.
Definition: Arg.h:31
DerivedArgList(const InputArgList &BaseArgs)
Construct a new derived arg list from BaseArgs.
Definition: ArgList.cpp:355
void claim() const
Set the Arg claimed bit.
Definition: Arg.h:89
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:305
bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const
hasFlag - Given an option Pos and its negative form Neg, return true if the option is present...
Definition: ArgList.cpp:220
const char * MakeArgString(const Twine &Str) const
Definition: ArgList.h:296
StringRef getPrefix() const
Get the default prefix for this option.
Definition: Option.h:117
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:416
iterator erase(iterator I)
Definition: SmallVector.h:455
reverse_iterator rend()
Definition: ArgList.h:146
iterator_range< arg_iterator > filtered(OptSpecifier Id0=0U, OptSpecifier Id1=0U, OptSpecifier Id2=0U) const
Definition: ArgList.h:162
void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const
AddLastArg - Render only the last argument match Id0, if present.
Definition: ArgList.cpp:246
void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0, OptSpecifier Id1=0U, OptSpecifier Id2=0U) const
AddAllArgValues - Render the argument values of all arguments matching the given ids.
Definition: ArgList.cpp:269
void render(const ArgList &Args, ArgStringList &Output) const
Append the argument onto the given array as strings.
Definition: Arg.cpp:88
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:215
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
MakeJoinedArg - Construct a new Positional arg for the given option Id, with the provided Value...
Definition: ArgList.cpp:391
Defines the llvm::Arg class for parsed arguments.
Arg * MakeSeparateArg(const Arg *BaseArg, const Option Opt, StringRef Value) const
MakeSeparateArg - Construct a new Positional arg for the given option Id, with the provided Value...
Definition: ArgList.cpp:382
Arg * MakePositionalArg(const Arg *BaseArg, const Option Opt, StringRef Value) const
MakePositionalArg - Construct a new Positional arg for the given option Id, with the provided Value...
Definition: ArgList.cpp:373
void ClaimAllArgs() const
ClaimAllArgs - Claim all arguments.
Definition: ArgList.cpp:299
void append(Arg *A)
append - Append A to the arg list.
Definition: ArgList.cpp:36
OptSpecifier - Wrapper class for abstracting references to option IDs.
Definition: OptSpecifier.h:20
Arg * getLastArg(OptSpecifier Id) const
Definition: ArgList.cpp:83
InputArgList(const char *const *ArgBegin, const char *const *ArgEnd)
Definition: ArgList.cpp:324
std::vector< std::string > getAllArgValues(OptSpecifier Id) const
getAllArgValues - Get the values of all instances of the given argument as strings.
Definition: ArgList.cpp:240
LLVM Value Representation.
Definition: Value.h:69
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
bool isValid() const
Definition: OptSpecifier.h:31
const char * MakeArgStringRef(StringRef Str) const override
Construct a constant string pointer whose lifetime will match that of the ArgList.
Definition: ArgList.cpp:349
StringRef getLastArgValue(OptSpecifier Id, StringRef Default="") const
getLastArgValue - Return the value of the last argument, or a default.
Definition: ArgList.cpp:233