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