1 //===--- ArgList.cpp - Argument List Management ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Option/ArgList.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Option/Arg.h"
15 #include "llvm/Option/Option.h"
16 #include "llvm/Support/raw_ostream.h"
19 using namespace llvm::opt;
21 void arg_iterator::SkipToNextArg() {
22 for (; Current != Args.end(); ++Current) {
23 // Done if there are no filters.
27 // Otherwise require a match.
28 const Option &O = (*Current)->getOption();
30 (Id1.isValid() && O.matches(Id1)) ||
31 (Id2.isValid() && O.matches(Id2)))
39 void ArgList::append(Arg *A) {
43 void ArgList::eraseArg(OptSpecifier Id) {
44 Args.erase(std::remove_if(begin(), end(),
45 [=](Arg *A) { return A->getOption().matches(Id); }),
49 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const {
50 // FIXME: Make search efficient?
51 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
52 if ((*it)->getOption().matches(Id))
57 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1) const {
58 // FIXME: Make search efficient?
59 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
60 if ((*it)->getOption().matches(Id0) ||
61 (*it)->getOption().matches(Id1))
66 Arg *ArgList::getLastArg(OptSpecifier Id) const {
68 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
69 if ((*it)->getOption().matches(Id)) {
78 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
80 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
81 if ((*it)->getOption().matches(Id0) ||
82 (*it)->getOption().matches(Id1)) {
92 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
93 OptSpecifier Id2) const {
95 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
96 if ((*it)->getOption().matches(Id0) ||
97 (*it)->getOption().matches(Id1) ||
98 (*it)->getOption().matches(Id2)) {
107 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
108 OptSpecifier Id2, OptSpecifier Id3) const {
110 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
111 if ((*it)->getOption().matches(Id0) ||
112 (*it)->getOption().matches(Id1) ||
113 (*it)->getOption().matches(Id2) ||
114 (*it)->getOption().matches(Id3)) {
123 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
124 OptSpecifier Id2, OptSpecifier Id3,
125 OptSpecifier Id4) const {
127 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
128 if ((*it)->getOption().matches(Id0) ||
129 (*it)->getOption().matches(Id1) ||
130 (*it)->getOption().matches(Id2) ||
131 (*it)->getOption().matches(Id3) ||
132 (*it)->getOption().matches(Id4)) {
141 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
142 OptSpecifier Id2, OptSpecifier Id3,
143 OptSpecifier Id4, OptSpecifier Id5) const {
145 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
146 if ((*it)->getOption().matches(Id0) ||
147 (*it)->getOption().matches(Id1) ||
148 (*it)->getOption().matches(Id2) ||
149 (*it)->getOption().matches(Id3) ||
150 (*it)->getOption().matches(Id4) ||
151 (*it)->getOption().matches(Id5)) {
160 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
161 OptSpecifier Id2, OptSpecifier Id3,
162 OptSpecifier Id4, OptSpecifier Id5,
163 OptSpecifier Id6) const {
165 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
166 if ((*it)->getOption().matches(Id0) ||
167 (*it)->getOption().matches(Id1) ||
168 (*it)->getOption().matches(Id2) ||
169 (*it)->getOption().matches(Id3) ||
170 (*it)->getOption().matches(Id4) ||
171 (*it)->getOption().matches(Id5) ||
172 (*it)->getOption().matches(Id6)) {
181 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
182 OptSpecifier Id2, OptSpecifier Id3,
183 OptSpecifier Id4, OptSpecifier Id5,
184 OptSpecifier Id6, OptSpecifier Id7) const {
186 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
187 if ((*it)->getOption().matches(Id0) ||
188 (*it)->getOption().matches(Id1) ||
189 (*it)->getOption().matches(Id2) ||
190 (*it)->getOption().matches(Id3) ||
191 (*it)->getOption().matches(Id4) ||
192 (*it)->getOption().matches(Id5) ||
193 (*it)->getOption().matches(Id6) ||
194 (*it)->getOption().matches(Id7)) {
203 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
204 if (Arg *A = getLastArg(Pos, Neg))
205 return A->getOption().matches(Pos);
209 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
210 bool Default) const {
211 if (Arg *A = getLastArg(Pos, PosAlias, Neg))
212 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
216 StringRef ArgList::getLastArgValue(OptSpecifier Id,
217 StringRef Default) const {
218 if (Arg *A = getLastArg(Id))
219 return A->getValue();
223 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
224 SmallVector<const char *, 16> Values;
225 AddAllArgValues(Values, Id);
226 return std::vector<std::string>(Values.begin(), Values.end());
229 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
230 if (Arg *A = getLastArg(Id)) {
232 A->render(*this, Output);
236 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
237 OptSpecifier Id1) const {
238 if (Arg *A = getLastArg(Id0, Id1)) {
240 A->render(*this, Output);
244 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
245 OptSpecifier Id1, OptSpecifier Id2) const {
246 for (auto Arg: filtered(Id0, Id1, Id2)) {
248 Arg->render(*this, Output);
252 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
253 OptSpecifier Id1, OptSpecifier Id2) const {
254 for (auto Arg : filtered(Id0, Id1, Id2)) {
256 for (unsigned i = 0, e = Arg->getNumValues(); i != e; ++i)
257 Output.push_back(Arg->getValue(i));
261 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
262 const char *Translation,
264 for (auto Arg: filtered(Id0)) {
268 Output.push_back(MakeArgString(StringRef(Translation) +
271 Output.push_back(Translation);
272 Output.push_back(Arg->getValue(0));
277 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
278 for (auto Arg : filtered(Id0))
282 void ArgList::ClaimAllArgs() const {
283 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
284 if (!(*it)->isClaimed())
288 const char *ArgList::MakeArgString(const Twine &T) const {
289 SmallString<256> Str;
290 return MakeArgString(T.toStringRef(Str));
293 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
295 StringRef RHS) const {
296 StringRef Cur = getArgString(Index);
297 if (Cur.size() == LHS.size() + RHS.size() &&
298 Cur.startswith(LHS) && Cur.endswith(RHS))
301 return MakeArgString(LHS + RHS);
306 InputArgList::InputArgList(const char* const *ArgBegin,
307 const char* const *ArgEnd)
308 : NumInputArgStrings(ArgEnd - ArgBegin) {
309 ArgStrings.append(ArgBegin, ArgEnd);
312 InputArgList::~InputArgList() {
313 // An InputArgList always owns its arguments.
314 for (iterator it = begin(), ie = end(); it != ie; ++it)
318 unsigned InputArgList::MakeIndex(StringRef String0) const {
319 unsigned Index = ArgStrings.size();
321 // Tuck away so we have a reliable const char *.
322 SynthesizedStrings.push_back(String0);
323 ArgStrings.push_back(SynthesizedStrings.back().c_str());
328 unsigned InputArgList::MakeIndex(StringRef String0,
329 StringRef String1) const {
330 unsigned Index0 = MakeIndex(String0);
331 unsigned Index1 = MakeIndex(String1);
332 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
337 const char *InputArgList::MakeArgString(StringRef Str) const {
338 return getArgString(MakeIndex(Str));
343 DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
344 : BaseArgs(_BaseArgs) {
347 DerivedArgList::~DerivedArgList() {}
349 const char *DerivedArgList::MakeArgString(StringRef Str) const {
350 return BaseArgs.MakeArgString(Str);
353 void DerivedArgList::AddSynthesizedArg(Arg *A) {
354 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
357 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
358 SynthesizedArgs.push_back(
359 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
360 BaseArgs.MakeIndex(Opt.getName()), BaseArg));
361 return SynthesizedArgs.back().get();
364 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
365 StringRef Value) const {
366 unsigned Index = BaseArgs.MakeIndex(Value);
367 SynthesizedArgs.push_back(
368 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
369 Index, BaseArgs.getArgString(Index), BaseArg));
370 return SynthesizedArgs.back().get();
373 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
374 StringRef Value) const {
375 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
376 SynthesizedArgs.push_back(
377 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
378 Index, BaseArgs.getArgString(Index + 1), BaseArg));
379 return SynthesizedArgs.back().get();
382 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
383 StringRef Value) const {
384 unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str());
385 SynthesizedArgs.push_back(make_unique<Arg>(
386 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
387 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
388 return SynthesizedArgs.back().get();