LLVM  16.0.0git
ArgList.cpp
Go to the documentation of this file.
1 //===- ArgList.cpp - Argument List Management -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/ArrayRef.h"
10 #include "llvm/ADT/None.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/Config/llvm-config.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  OptSpecifier Neg) const {
100  if (Arg *A = getLastArg(Pos, Neg))
101  if (A->getOption().matches(Pos))
102  A->render(*this, Output);
103 }
104 
107  ArrayRef<OptSpecifier> ExcludeIds) const {
108  for (const Arg *Arg : *this) {
109  bool Excluded = false;
110  for (OptSpecifier Id : ExcludeIds) {
111  if (Arg->getOption().matches(Id)) {
112  Excluded = true;
113  break;
114  }
115  }
116  if (!Excluded) {
117  for (OptSpecifier Id : Ids) {
118  if (Arg->getOption().matches(Id)) {
119  Arg->claim();
120  Arg->render(*this, Output);
121  break;
122  }
123  }
124  }
125  }
126 }
127 
128 /// This is a nicer interface when you don't have a list of Ids to exclude.
130  ArrayRef<OptSpecifier> Ids) const {
131  ArrayRef<OptSpecifier> Exclude = None;
132  AddAllArgsExcept(Output, Ids, Exclude);
133 }
134 
135 /// This 3-opt variant of AddAllArgs could be eliminated in favor of one
136 /// that accepts a single specifier, given the above which accepts any number.
138  OptSpecifier Id1, OptSpecifier Id2) const {
139  for (auto *Arg : filtered(Id0, Id1, Id2)) {
140  Arg->claim();
141  Arg->render(*this, Output);
142  }
143 }
144 
146  OptSpecifier Id1, OptSpecifier Id2) const {
147  for (auto *Arg : filtered(Id0, Id1, Id2)) {
148  Arg->claim();
149  const auto &Values = Arg->getValues();
150  Output.append(Values.begin(), Values.end());
151  }
152 }
153 
155  const char *Translation,
156  bool Joined) const {
157  for (auto *Arg : filtered(Id0)) {
158  Arg->claim();
159 
160  if (Joined) {
161  Output.push_back(MakeArgString(StringRef(Translation) +
162  Arg->getValue(0)));
163  } else {
164  Output.push_back(Translation);
165  Output.push_back(Arg->getValue(0));
166  }
167  }
168 }
169 
171  for (auto *Arg : filtered(Id0))
172  Arg->claim();
173 }
174 
175 void ArgList::ClaimAllArgs() const {
176  for (auto *Arg : *this)
177  if (!Arg->isClaimed())
178  Arg->claim();
179 }
180 
182  StringRef LHS,
183  StringRef RHS) const {
185  if (Cur.size() == LHS.size() + RHS.size() &&
186  Cur.startswith(LHS) && Cur.endswith(RHS))
187  return Cur.data();
188 
189  return MakeArgString(LHS + RHS);
190 }
191 
193  for (Arg *A : *this) {
194  O << "* ";
195  A->print(O);
196  }
197 }
198 
199 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
201 #endif
202 
203 void InputArgList::releaseMemory() {
204  // An InputArgList always owns its arguments.
205  for (Arg *A : *this)
206  delete A;
207 }
208 
209 InputArgList::InputArgList(const char* const *ArgBegin,
210  const char* const *ArgEnd)
211  : NumInputArgStrings(ArgEnd - ArgBegin) {
212  ArgStrings.append(ArgBegin, ArgEnd);
213 }
214 
215 unsigned InputArgList::MakeIndex(StringRef String0) const {
216  unsigned Index = ArgStrings.size();
217 
218  // Tuck away so we have a reliable const char *.
219  SynthesizedStrings.push_back(std::string(String0));
220  ArgStrings.push_back(SynthesizedStrings.back().c_str());
221 
222  return Index;
223 }
224 
226  StringRef String1) const {
227  unsigned Index0 = MakeIndex(String0);
228  unsigned Index1 = MakeIndex(String1);
229  assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
230  (void) Index1;
231  return Index0;
232 }
233 
235  return getArgString(MakeIndex(Str));
236 }
237 
239  : BaseArgs(BaseArgs) {}
240 
242  return BaseArgs.MakeArgString(Str);
243 }
244 
246  SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
247 }
248 
249 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
250  SynthesizedArgs.push_back(
251  std::make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
252  BaseArgs.MakeIndex(Opt.getName()), BaseArg));
253  return SynthesizedArgs.back().get();
254 }
255 
256 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
257  StringRef Value) const {
258  unsigned Index = BaseArgs.MakeIndex(Value);
259  SynthesizedArgs.push_back(
260  std::make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
261  Index, BaseArgs.getArgString(Index), BaseArg));
262  return SynthesizedArgs.back().get();
263 }
264 
265 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
266  StringRef Value) const {
267  unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
268  SynthesizedArgs.push_back(
269  std::make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
270  Index, BaseArgs.getArgString(Index + 1), BaseArg));
271  return SynthesizedArgs.back().get();
272 }
273 
274 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
275  StringRef Value) const {
276  unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
277  SynthesizedArgs.push_back(std::make_unique<Arg>(
278  Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
279  BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
280  return SynthesizedArgs.back().get();
281 }
Option.h
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:492
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::opt::DerivedArgList::MakePositionalArg
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:256
llvm::opt::InputArgList
Definition: ArgList.h:383
llvm::opt::Arg
A concrete instance of a particular driver option.
Definition: Arg.h:34
StringRef.h
llvm::SmallVector< const char *, 16 >
llvm::opt::ArgList::dump
void dump() const
Definition: ArgList.cpp:200
llvm::opt::DerivedArgList::MakeFlagArg
Arg * MakeFlagArg(const Arg *BaseArg, const Option Opt) const
MakeFlagArg - Construct a new FlagArg for the given option Id.
Definition: ArgList.cpp:249
llvm::opt::Arg::claim
void claim() const
Set the Arg claimed bit.
Definition: Arg.h:109
llvm::opt::ArgList::GetOrMakeJoinedArgString
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:181
llvm::DenseMapBase::erase
bool erase(const KeyT &Val)
Definition: DenseMap.h:302
llvm::opt::ArgList::AddAllArgsExcept
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:105
llvm::opt::ArgList::hasFlag
bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const
hasFlag - Given an option Pos and its negative form Neg, return true if the option is present,...
Definition: ArgList.cpp:73
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
STLExtras.h
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::opt::InputArgList::MakeArgString
const char * MakeArgString(const Twine &Str) const
Definition: ArgList.h:367
llvm::opt::InputArgList::getArgString
const char * getArgString(unsigned Index) const override
getArgString - Return the input argument string at Index.
Definition: ArgList.h:426
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::opt::Arg::getOption
const Option & getOption() const
Definition: Arg.h:77
llvm::opt::Arg::isClaimed
bool isClaimed() const
Definition: Arg.h:106
llvm::StringRef::startswith
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:256
llvm::opt::Arg::getValues
SmallVectorImpl< const char * > & getValues()
Definition: Arg.h:117
Twine.h
llvm::opt::DerivedArgList::MakeSeparateArg
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:265
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
llvm::opt::Option::matches
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
llvm::opt
Definition: Arg.h:26
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::opt::ArgList::getLastArgValue
StringRef getLastArgValue(OptSpecifier Id, StringRef Default="") const
getLastArgValue - Return the value of the last argument, or a default.
Definition: ArgList.cpp:86
llvm::StringRef::data
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
llvm::opt::ArgList::eraseArg
void eraseArg(OptSpecifier Id)
eraseArg - Remove any option matching Id.
Definition: ArgList.cpp:46
llvm::SmallVectorImpl::append
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:669
llvm::opt::Arg::getValue
const char * getValue(unsigned N=0) const
Definition: Arg.h:113
llvm::None
const NoneType None
Definition: None.h:24
llvm::opt::OptSpecifier
OptSpecifier - Wrapper class for abstracting references to option IDs.
Definition: OptSpecifier.h:18
llvm::opt::ArgList::append
void append(Arg *A)
append - Append A to the arg list.
Definition: ArgList.cpp:33
llvm::opt::ArgList::getAllArgValues
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
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:264
Index
uint32_t Index
Definition: ELFObjHandler.cpp:82
llvm::opt::Option::getName
StringRef getName() const
Get the name of this option without any prefix.
Definition: Option.h:98
llvm::opt::ArgList::ClaimAllArgs
void ClaimAllArgs() const
ClaimAllArgs - Claim all arguments.
Definition: ArgList.cpp:175
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::opt::Arg::render
void render(const ArgList &Args, ArgStringList &Output) const
Append the argument onto the given array as strings.
Definition: Arg.cpp:96
llvm::opt::ArgList::AddAllArgValues
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:145
llvm::opt::Option::getPrefix
StringRef getPrefix() const
Get the default prefix for this option.
Definition: Option.h:126
llvm::opt::InputArgList::InputArgList
InputArgList()
Definition: ArgList.h:406
ArrayRef.h
llvm::opt::DerivedArgList::DerivedArgList
DerivedArgList(const InputArgList &BaseArgs)
Construct a new derived arg list from BaseArgs.
Definition: ArgList.cpp:238
llvm::DenseMapBase::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
OptSpecifier.h
Arg.h
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
None.h
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::opt::ArgList::addOptInFlag
void addOptInFlag(ArgStringList &Output, OptSpecifier Pos, OptSpecifier Neg) const
Given an option Pos and its negative form Neg, render the option if Pos is present.
Definition: ArgList.cpp:98
Compiler.h
llvm::opt::ArgList::print
void print(raw_ostream &O) const
Definition: ArgList.cpp:192
llvm::opt::ArgList::AddAllArgsTranslated
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:154
llvm::DenseMapBase::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:207
llvm::StringRef::size
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
ArgList.h
llvm::DenseMapBase::end
iterator end()
Definition: DenseMap.h:84
llvm::StringRef::endswith
bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:265
llvm::opt::Option
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:51
llvm::opt::DerivedArgList::AddSynthesizedArg
void AddSynthesizedArg(Arg *A)
AddSynthesizedArg - Add a argument to the list of synthesized arguments (to be freed).
Definition: ArgList.cpp:245
llvm::opt::ArgList::AddAllArgs
void AddAllArgs(ArgStringList &Output, ArrayRef< OptSpecifier > Ids) const
AddAllArgs - Render all arguments matching any of the given ids.
Definition: ArgList.cpp:129
llvm::opt::ArgList::filtered
iterator_range< filtered_iterator< sizeof...(OptSpecifiers)> > filtered(OptSpecifiers ...Ids) const
Definition: ArgList.h:205
llvm::opt::InputArgList::MakeArgStringRef
const char * MakeArgStringRef(StringRef Str) const override
Construct a constant string pointer whose lifetime will match that of the ArgList.
Definition: ArgList.cpp:234
SmallVector.h
llvm::opt::ArgList::MakeArgString
const char * MakeArgString(const Twine &Str) const
Definition: ArgList.h:367
llvm::opt::InputArgList::MakeIndex
unsigned MakeIndex(StringRef String0) const
MakeIndex - Get an index for the given string(s).
Definition: ArgList.cpp:215
raw_ostream.h
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:241
llvm::opt::DerivedArgList::MakeJoinedArg
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:274
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
Debug.h
llvm::opt::ArgList::getArgString
virtual const char * getArgString(unsigned Index) const =0
getArgString - Return the input argument string at Index.
llvm::opt::DerivedArgList::MakeArgStringRef
const char * MakeArgStringRef(StringRef Str) const override
Construct a constant string pointer whose lifetime will match that of the ArgList.
Definition: ArgList.cpp:241
llvm::opt::ArgList::getLastArg
Arg * getLastArg(OptSpecifiers ...Ids) const
Return the last argument matching Id, or null.
Definition: ArgList.h:256