LLVM  6.0.0svn
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/ADT/ArrayRef.h"
11 #include "llvm/ADT/None.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Option/Arg.h"
17 #include "llvm/Option/ArgList.h"
18 #include "llvm/Option/Option.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Debug.h"
23 #include <algorithm>
24 #include <cassert>
25 #include <memory>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 using namespace llvm;
31 using namespace llvm::opt;
32 
33 void ArgList::append(Arg *A) {
34  Args.push_back(A);
35 
36  // Update ranges for the option and all of its groups.
37  for (Option O = A->getOption().getUnaliasedOption(); O.isValid();
38  O = O.getGroup()) {
39  auto &R =
40  OptRanges.insert(std::make_pair(O.getID(), emptyRange())).first->second;
41  R.first = std::min<unsigned>(R.first, Args.size() - 1);
42  R.second = Args.size();
43  }
44 }
45 
47  // Zero out the removed entries but keep them around so that we don't
48  // need to invalidate OptRanges.
49  for (Arg *const &A : filtered(Id)) {
50  // Avoid the need for a non-const filtered iterator variant.
51  Arg **ArgsBegin = Args.data();
52  ArgsBegin[&A - ArgsBegin] = nullptr;
53  }
54  OptRanges.erase(Id.getID());
55 }
56 
57 ArgList::OptRange
58 ArgList::getRange(std::initializer_list<OptSpecifier> Ids) const {
59  OptRange R = emptyRange();
60  for (auto Id : Ids) {
61  auto I = OptRanges.find(Id.getID());
62  if (I != OptRanges.end()) {
63  R.first = std::min(R.first, I->second.first);
64  R.second = std::max(R.second, I->second.second);
65  }
66  }
67  // Map an empty {-1, 0} range to {0, 0} so it can be used to form iterators.
68  if (R.first == -1u)
69  R.first = 0;
70  return R;
71 }
72 
73 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
74  if (Arg *A = getLastArg(Pos, Neg))
75  return A->getOption().matches(Pos);
76  return Default;
77 }
78 
80  bool Default) const {
81  if (Arg *A = getLastArg(Pos, PosAlias, Neg))
82  return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
83  return Default;
84 }
85 
87  if (Arg *A = getLastArg(Id))
88  return A->getValue();
89  return Default;
90 }
91 
92 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
94  AddAllArgValues(Values, Id);
95  return std::vector<std::string>(Values.begin(), Values.end());
96 }
97 
99  if (Arg *A = getLastArg(Id)) {
100  A->claim();
101  A->render(*this, Output);
102  }
103 }
104 
106  OptSpecifier Id1) const {
107  if (Arg *A = getLastArg(Id0, Id1)) {
108  A->claim();
109  A->render(*this, Output);
110  }
111 }
112 
115  ArrayRef<OptSpecifier> ExcludeIds) const {
116  for (const Arg *Arg : *this) {
117  bool Excluded = false;
118  for (OptSpecifier Id : ExcludeIds) {
119  if (Arg->getOption().matches(Id)) {
120  Excluded = true;
121  break;
122  }
123  }
124  if (!Excluded) {
125  for (OptSpecifier Id : Ids) {
126  if (Arg->getOption().matches(Id)) {
127  Arg->claim();
128  Arg->render(*this, Output);
129  break;
130  }
131  }
132  }
133  }
134 }
135 
136 /// This is a nicer interface when you don't have a list of Ids to exclude.
138  ArrayRef<OptSpecifier> Ids) const {
139  ArrayRef<OptSpecifier> Exclude = None;
140  AddAllArgsExcept(Output, Ids, Exclude);
141 }
142 
143 /// This 3-opt variant of AddAllArgs could be eliminated in favor of one
144 /// that accepts a single specifier, given the above which accepts any number.
146  OptSpecifier Id1, OptSpecifier Id2) const {
147  for (auto Arg: filtered(Id0, Id1, Id2)) {
148  Arg->claim();
149  Arg->render(*this, Output);
150  }
151 }
152 
154  OptSpecifier Id1, OptSpecifier Id2) const {
155  for (auto Arg : filtered(Id0, Id1, Id2)) {
156  Arg->claim();
157  const auto &Values = Arg->getValues();
158  Output.append(Values.begin(), Values.end());
159  }
160 }
161 
163  const char *Translation,
164  bool Joined) const {
165  for (auto Arg: filtered(Id0)) {
166  Arg->claim();
167 
168  if (Joined) {
169  Output.push_back(MakeArgString(StringRef(Translation) +
170  Arg->getValue(0)));
171  } else {
172  Output.push_back(Translation);
173  Output.push_back(Arg->getValue(0));
174  }
175  }
176 }
177 
179  for (auto *Arg : filtered(Id0))
180  Arg->claim();
181 }
182 
183 void ArgList::ClaimAllArgs() const {
184  for (auto *Arg : *this)
185  if (!Arg->isClaimed())
186  Arg->claim();
187 }
188 
189 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
190  StringRef LHS,
191  StringRef RHS) const {
192  StringRef Cur = getArgString(Index);
193  if (Cur.size() == LHS.size() + RHS.size() &&
194  Cur.startswith(LHS) && Cur.endswith(RHS))
195  return Cur.data();
196 
197  return MakeArgString(LHS + RHS);
198 }
199 
201  for (Arg *A : *this) {
202  O << "* ";
203  A->print(O);
204  }
205 }
206 
207 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
209 #endif
210 
211 void InputArgList::releaseMemory() {
212  // An InputArgList always owns its arguments.
213  for (Arg *A : *this)
214  delete A;
215 }
216 
217 InputArgList::InputArgList(const char* const *ArgBegin,
218  const char* const *ArgEnd)
219  : NumInputArgStrings(ArgEnd - ArgBegin) {
220  ArgStrings.append(ArgBegin, ArgEnd);
221 }
222 
223 unsigned InputArgList::MakeIndex(StringRef String0) const {
224  unsigned Index = ArgStrings.size();
225 
226  // Tuck away so we have a reliable const char *.
227  SynthesizedStrings.push_back(String0);
228  ArgStrings.push_back(SynthesizedStrings.back().c_str());
229 
230  return Index;
231 }
232 
234  StringRef String1) const {
235  unsigned Index0 = MakeIndex(String0);
236  unsigned Index1 = MakeIndex(String1);
237  assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
238  (void) Index1;
239  return Index0;
240 }
241 
243  return getArgString(MakeIndex(Str));
244 }
245 
247  : BaseArgs(BaseArgs) {}
248 
250  return BaseArgs.MakeArgString(Str);
251 }
252 
254  SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
255 }
256 
257 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
258  SynthesizedArgs.push_back(
259  make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
260  BaseArgs.MakeIndex(Opt.getName()), BaseArg));
261  return SynthesizedArgs.back().get();
262 }
263 
264 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
265  StringRef Value) const {
266  unsigned Index = BaseArgs.MakeIndex(Value);
267  SynthesizedArgs.push_back(
268  make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
269  Index, BaseArgs.getArgString(Index), BaseArg));
270  return SynthesizedArgs.back().get();
271 }
272 
273 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
274  StringRef Value) const {
275  unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
276  SynthesizedArgs.push_back(
277  make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
278  Index, BaseArgs.getArgString(Index + 1), BaseArg));
279  return SynthesizedArgs.back().get();
280 }
281 
282 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
283  StringRef Value) const {
284  unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
285  SynthesizedArgs.push_back(make_unique<Arg>(
286  Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
287  BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
288  return SynthesizedArgs.back().get();
289 }
const NoneType None
Definition: None.h:24
void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const
AddLastArg - Render only the last argument match Id0, if present.
Definition: ArgList.cpp:98
void push_back(const T &Elt)
Definition: SmallVector.h:212
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
StringRef getPrefix() const
Get the default prefix for this option.
Definition: Option.h:127
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:449
void print(raw_ostream &O) const
Definition: Arg.cpp:47
const Option getUnaliasedOption() const
getUnaliasedOption - Return the final option this option aliases (itself, if the option has no alias)...
Definition: Option.h:177
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
iterator_range< filtered_iterator< sizeof...(OptSpecifiers)> > filtered(OptSpecifiers ...Ids) const
Definition: ArgList.h:209
void eraseArg(OptSpecifier Id)
eraseArg - Remove any option matching Id.
Definition: ArgList.cpp:46
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:191
const char * getArgString(unsigned Index) const override
getArgString - Return the input argument string at Index.
Definition: ArgList.h:411
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:279
const Option & getOption() const
Definition: Arg.h:73
Arg * MakeFlagArg(const Arg *BaseArg, const Option Opt) const
MakeFlagArg - Construct a new FlagArg for the given option Id.
Definition: ArgList.cpp:257
unsigned getID() const
Definition: OptSpecifier.h:30
SmallVectorImpl< const char * > & getValues()
Definition: Arg.h:100
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
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:162
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:282
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:146
void AddSynthesizedArg(Arg *A)
AddSynthesizedArg - Add a argument to the list of synthesized arguments (to be freed).
Definition: ArgList.cpp:253
bool erase(const KeyT &Val)
Definition: DenseMap.h:268
const char * MakeArgStringRef(StringRef Str) const override
Construct a constant string pointer whose lifetime will match that of the ArgList.
Definition: ArgList.cpp:249
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:52
Definition: Arg.h:27
StringRef getName() const
Get the name of this option without any prefix.
Definition: Option.h:99
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:93
void AddAllArgs(ArgStringList &Output, ArrayRef< OptSpecifier > Ids) const
AddAllArgs - Render all arguments matching any of the given ids.
Definition: ArgList.cpp:137
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:273
A concrete instance of a particular driver option.
Definition: Arg.h:35
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:73
DerivedArgList(const InputArgList &BaseArgs)
Construct a new derived arg list from BaseArgs.
Definition: ArgList.cpp:246
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:116
const char * MakeArgString(const Twine &Str) const
Definition: ArgList.h:354
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:189
Arg * getLastArg(OptSpecifiers ...Ids) const
Return the last argument matching Id, or null.
Definition: ArgList.h:254
void claim() const
Set the Arg claimed bit.
Definition: Arg.h:92
virtual const char * getArgString(unsigned Index) const =0
getArgString - Return the input argument string at Index.
unsigned MakeIndex(StringRef String0) const
MakeIndex - Get an index for the given string(s).
Definition: ArgList.cpp:223
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:264
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Defines the llvm::Arg class for parsed arguments.
std::vector< std::string > getAllArgValues(OptSpecifier Id) const
getAllArgValues - Get the values of all instances of the given argument as strings.
Definition: ArgList.cpp:92
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:398
StringRef getLastArgValue(OptSpecifier Id, StringRef Default="") const
getLastArgValue - Return the value of the last argument, or a default.
Definition: ArgList.cpp:86
void append(Arg *A)
append - Append A to the arg list.
Definition: ArgList.cpp:33
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:120
OptSpecifier - Wrapper class for abstracting references to option IDs.
Definition: OptSpecifier.h:19
void dump() const
Definition: ArgList.cpp:208
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:143
#define I(x, y, z)
Definition: MD5.cpp:58
iterator end()
Definition: DenseMap.h:79
InputArgList(const char *const *ArgBegin, const char *const *ArgEnd)
Definition: ArgList.cpp:217
void AddAllArgsExcept(ArgStringList &Output, ArrayRef< OptSpecifier > Ids, ArrayRef< OptSpecifier > ExcludeIds) const
AddAllArgsExcept - Render all arguments matching any of the given ids and not matching any of the exc...
Definition: ArgList.cpp:113
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:153
void render(const ArgList &Args, ArgStringList &Output) const
Append the argument onto the given array as strings.
Definition: Arg.cpp:93
void ClaimAllArgs() const
ClaimAllArgs - Claim all arguments.
Definition: ArgList.cpp:183
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
const char * MakeArgStringRef(StringRef Str) const override
Construct a constant string pointer whose lifetime will match that of the ArgList.
Definition: ArgList.cpp:242
const char * getValue(unsigned N=0) const
Definition: Arg.h:96
void print(raw_ostream &O) const
Definition: ArgList.cpp:200
bool isClaimed() const
Definition: Arg.h:89