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