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