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/SmallVector.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"
18 #include "llvm/Option/OptSpecifier.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <algorithm>
23 #include <cassert>
24 #include <memory>
25 #include <string>
26 #include <utility>
27 #include <vector>
28
29 using namespace llvm;
30 using namespace llvm::opt;
31
append(Arg * A)32 void ArgList::append(Arg *A) {
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
eraseArg(OptSpecifier Id)45 void ArgList::eraseArg(OptSpecifier Id) {
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
56 ArgList::OptRange
getRange(std::initializer_list<OptSpecifier> Ids) const57 ArgList::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
hasFlag(OptSpecifier Pos,OptSpecifier Neg,bool Default) const72 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
73 if (Arg *A = getLastArg(Pos, Neg))
74 return A->getOption().matches(Pos);
75 return Default;
76 }
77
hasFlag(OptSpecifier Pos,OptSpecifier PosAlias,OptSpecifier Neg,bool Default) const78 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
79 bool Default) const {
80 if (Arg *A = getLastArg(Pos, PosAlias, Neg))
81 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
82 return Default;
83 }
84
getLastArgValue(OptSpecifier Id,StringRef Default) const85 StringRef ArgList::getLastArgValue(OptSpecifier Id, StringRef Default) const {
86 if (Arg *A = getLastArg(Id))
87 return A->getValue();
88 return Default;
89 }
90
getAllArgValues(OptSpecifier Id) const91 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
92 SmallVector<const char *, 16> Values;
93 AddAllArgValues(Values, Id);
94 return std::vector<std::string>(Values.begin(), Values.end());
95 }
96
addOptInFlag(ArgStringList & Output,OptSpecifier Pos,OptSpecifier Neg) const97 void ArgList::addOptInFlag(ArgStringList &Output, OptSpecifier Pos,
98 OptSpecifier Neg) const {
99 if (Arg *A = getLastArg(Pos, Neg))
100 if (A->getOption().matches(Pos))
101 A->render(*this, Output);
102 }
103
AddAllArgsExcept(ArgStringList & Output,ArrayRef<OptSpecifier> Ids,ArrayRef<OptSpecifier> ExcludeIds) const104 void ArgList::AddAllArgsExcept(ArgStringList &Output,
105 ArrayRef<OptSpecifier> Ids,
106 ArrayRef<OptSpecifier> ExcludeIds) const {
107 for (const Arg *Arg : *this) {
108 bool Excluded = false;
109 for (OptSpecifier Id : ExcludeIds) {
110 if (Arg->getOption().matches(Id)) {
111 Excluded = true;
112 break;
113 }
114 }
115 if (!Excluded) {
116 for (OptSpecifier Id : Ids) {
117 if (Arg->getOption().matches(Id)) {
118 Arg->claim();
119 Arg->render(*this, Output);
120 break;
121 }
122 }
123 }
124 }
125 }
126
127 /// This is a nicer interface when you don't have a list of Ids to exclude.
AddAllArgs(ArgStringList & Output,ArrayRef<OptSpecifier> Ids) const128 void ArgList::AddAllArgs(ArgStringList &Output,
129 ArrayRef<OptSpecifier> Ids) const {
130 ArrayRef<OptSpecifier> Exclude = std::nullopt;
131 AddAllArgsExcept(Output, Ids, Exclude);
132 }
133
134 /// This 3-opt variant of AddAllArgs could be eliminated in favor of one
135 /// that accepts a single specifier, given the above which accepts any number.
AddAllArgs(ArgStringList & Output,OptSpecifier Id0,OptSpecifier Id1,OptSpecifier Id2) const136 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
137 OptSpecifier Id1, OptSpecifier Id2) const {
138 for (auto *Arg : filtered(Id0, Id1, Id2)) {
139 Arg->claim();
140 Arg->render(*this, Output);
141 }
142 }
143
AddAllArgValues(ArgStringList & Output,OptSpecifier Id0,OptSpecifier Id1,OptSpecifier Id2) const144 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
145 OptSpecifier Id1, OptSpecifier Id2) const {
146 for (auto *Arg : filtered(Id0, Id1, Id2)) {
147 Arg->claim();
148 const auto &Values = Arg->getValues();
149 Output.append(Values.begin(), Values.end());
150 }
151 }
152
AddAllArgsTranslated(ArgStringList & Output,OptSpecifier Id0,const char * Translation,bool Joined) const153 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
154 const char *Translation,
155 bool Joined) const {
156 for (auto *Arg : filtered(Id0)) {
157 Arg->claim();
158
159 if (Joined) {
160 Output.push_back(MakeArgString(StringRef(Translation) +
161 Arg->getValue(0)));
162 } else {
163 Output.push_back(Translation);
164 Output.push_back(Arg->getValue(0));
165 }
166 }
167 }
168
ClaimAllArgs(OptSpecifier Id0) const169 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
170 for (auto *Arg : filtered(Id0))
171 Arg->claim();
172 }
173
ClaimAllArgs() const174 void ArgList::ClaimAllArgs() const {
175 for (auto *Arg : *this)
176 if (!Arg->isClaimed())
177 Arg->claim();
178 }
179
GetOrMakeJoinedArgString(unsigned Index,StringRef LHS,StringRef RHS) const180 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
181 StringRef LHS,
182 StringRef RHS) const {
183 StringRef Cur = getArgString(Index);
184 if (Cur.size() == LHS.size() + RHS.size() &&
185 Cur.startswith(LHS) && Cur.endswith(RHS))
186 return Cur.data();
187
188 return MakeArgString(LHS + RHS);
189 }
190
print(raw_ostream & O) const191 void ArgList::print(raw_ostream &O) const {
192 for (Arg *A : *this) {
193 O << "* ";
194 A->print(O);
195 }
196 }
197
198 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const199 LLVM_DUMP_METHOD void ArgList::dump() const { print(dbgs()); }
200 #endif
201
releaseMemory()202 void InputArgList::releaseMemory() {
203 // An InputArgList always owns its arguments.
204 for (Arg *A : *this)
205 delete A;
206 }
207
InputArgList(const char * const * ArgBegin,const char * const * ArgEnd)208 InputArgList::InputArgList(const char* const *ArgBegin,
209 const char* const *ArgEnd)
210 : NumInputArgStrings(ArgEnd - ArgBegin) {
211 ArgStrings.append(ArgBegin, ArgEnd);
212 }
213
MakeIndex(StringRef String0) const214 unsigned InputArgList::MakeIndex(StringRef String0) const {
215 unsigned Index = ArgStrings.size();
216
217 // Tuck away so we have a reliable const char *.
218 SynthesizedStrings.push_back(std::string(String0));
219 ArgStrings.push_back(SynthesizedStrings.back().c_str());
220
221 return Index;
222 }
223
MakeIndex(StringRef String0,StringRef String1) const224 unsigned InputArgList::MakeIndex(StringRef String0,
225 StringRef String1) const {
226 unsigned Index0 = MakeIndex(String0);
227 unsigned Index1 = MakeIndex(String1);
228 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
229 (void) Index1;
230 return Index0;
231 }
232
MakeArgStringRef(StringRef Str) const233 const char *InputArgList::MakeArgStringRef(StringRef Str) const {
234 return getArgString(MakeIndex(Str));
235 }
236
DerivedArgList(const InputArgList & BaseArgs)237 DerivedArgList::DerivedArgList(const InputArgList &BaseArgs)
238 : BaseArgs(BaseArgs) {}
239
MakeArgStringRef(StringRef Str) const240 const char *DerivedArgList::MakeArgStringRef(StringRef Str) const {
241 return BaseArgs.MakeArgString(Str);
242 }
243
AddSynthesizedArg(Arg * A)244 void DerivedArgList::AddSynthesizedArg(Arg *A) {
245 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
246 }
247
MakeFlagArg(const Arg * BaseArg,const Option Opt) const248 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
249 SynthesizedArgs.push_back(
250 std::make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
251 BaseArgs.MakeIndex(Opt.getName()), BaseArg));
252 return SynthesizedArgs.back().get();
253 }
254
MakePositionalArg(const Arg * BaseArg,const Option Opt,StringRef Value) const255 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
256 StringRef Value) const {
257 unsigned Index = BaseArgs.MakeIndex(Value);
258 SynthesizedArgs.push_back(
259 std::make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
260 Index, BaseArgs.getArgString(Index), BaseArg));
261 return SynthesizedArgs.back().get();
262 }
263
MakeSeparateArg(const Arg * BaseArg,const Option Opt,StringRef Value) const264 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
265 StringRef Value) const {
266 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
267 SynthesizedArgs.push_back(
268 std::make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
269 Index, BaseArgs.getArgString(Index + 1), BaseArg));
270 return SynthesizedArgs.back().get();
271 }
272
MakeJoinedArg(const Arg * BaseArg,const Option Opt,StringRef Value) const273 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
274 StringRef Value) const {
275 unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
276 SynthesizedArgs.push_back(std::make_unique<Arg>(
277 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
278 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
279 return SynthesizedArgs.back().get();
280 }
281