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/SmallString.h"
12 #include "llvm/ADT/STLExtras.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::getLastArg(OptSpecifier Id) const {
59 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
60 if ((*it)->getOption().matches(Id)) {
69 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
71 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
72 if ((*it)->getOption().matches(Id0) ||
73 (*it)->getOption().matches(Id1)) {
83 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
84 OptSpecifier Id2) const {
86 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
87 if ((*it)->getOption().matches(Id0) ||
88 (*it)->getOption().matches(Id1) ||
89 (*it)->getOption().matches(Id2)) {
98 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
99 OptSpecifier Id2, OptSpecifier Id3) const {
101 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
102 if ((*it)->getOption().matches(Id0) ||
103 (*it)->getOption().matches(Id1) ||
104 (*it)->getOption().matches(Id2) ||
105 (*it)->getOption().matches(Id3)) {
114 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
115 OptSpecifier Id2, OptSpecifier Id3,
116 OptSpecifier Id4) const {
118 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
119 if ((*it)->getOption().matches(Id0) ||
120 (*it)->getOption().matches(Id1) ||
121 (*it)->getOption().matches(Id2) ||
122 (*it)->getOption().matches(Id3) ||
123 (*it)->getOption().matches(Id4)) {
132 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
133 OptSpecifier Id2, OptSpecifier Id3,
134 OptSpecifier Id4, OptSpecifier Id5) const {
136 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
137 if ((*it)->getOption().matches(Id0) ||
138 (*it)->getOption().matches(Id1) ||
139 (*it)->getOption().matches(Id2) ||
140 (*it)->getOption().matches(Id3) ||
141 (*it)->getOption().matches(Id4) ||
142 (*it)->getOption().matches(Id5)) {
151 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
152 OptSpecifier Id2, OptSpecifier Id3,
153 OptSpecifier Id4, OptSpecifier Id5,
154 OptSpecifier Id6) const {
156 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
157 if ((*it)->getOption().matches(Id0) ||
158 (*it)->getOption().matches(Id1) ||
159 (*it)->getOption().matches(Id2) ||
160 (*it)->getOption().matches(Id3) ||
161 (*it)->getOption().matches(Id4) ||
162 (*it)->getOption().matches(Id5) ||
163 (*it)->getOption().matches(Id6)) {
172 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
173 OptSpecifier Id2, OptSpecifier Id3,
174 OptSpecifier Id4, OptSpecifier Id5,
175 OptSpecifier Id6, OptSpecifier Id7) const {
177 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
178 if ((*it)->getOption().matches(Id0) ||
179 (*it)->getOption().matches(Id1) ||
180 (*it)->getOption().matches(Id2) ||
181 (*it)->getOption().matches(Id3) ||
182 (*it)->getOption().matches(Id4) ||
183 (*it)->getOption().matches(Id5) ||
184 (*it)->getOption().matches(Id6) ||
185 (*it)->getOption().matches(Id7)) {
194 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
195 if (Arg *A = getLastArg(Pos, Neg))
196 return A->getOption().matches(Pos);
200 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
201 bool Default) const {
202 if (Arg *A = getLastArg(Pos, PosAlias, Neg))
203 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
207 StringRef ArgList::getLastArgValue(OptSpecifier Id,
208 StringRef Default) const {
209 if (Arg *A = getLastArg(Id))
210 return A->getValue();
214 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
215 SmallVector<const char *, 16> Values;
216 AddAllArgValues(Values, Id);
217 return std::vector<std::string>(Values.begin(), Values.end());
220 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
221 if (Arg *A = getLastArg(Id)) {
223 A->render(*this, Output);
227 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
228 OptSpecifier Id1) const {
229 if (Arg *A = getLastArg(Id0, Id1)) {
231 A->render(*this, Output);
235 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
236 OptSpecifier Id1, OptSpecifier Id2) const {
237 for (auto Arg: filtered(Id0, Id1, Id2)) {
239 Arg->render(*this, Output);
243 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
244 OptSpecifier Id1, OptSpecifier Id2) const {
245 for (auto Arg : filtered(Id0, Id1, Id2)) {
247 for (unsigned i = 0, e = Arg->getNumValues(); i != e; ++i)
248 Output.push_back(Arg->getValue(i));
252 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
253 const char *Translation,
255 for (auto Arg: filtered(Id0)) {
259 Output.push_back(MakeArgString(StringRef(Translation) +
262 Output.push_back(Translation);
263 Output.push_back(Arg->getValue(0));
268 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
269 for (auto Arg : filtered(Id0))
273 void ArgList::ClaimAllArgs() const {
274 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
275 if (!(*it)->isClaimed())
279 const char *ArgList::MakeArgString(const Twine &T) const {
280 SmallString<256> Str;
281 return MakeArgString(T.toStringRef(Str));
284 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
286 StringRef RHS) const {
287 StringRef Cur = getArgString(Index);
288 if (Cur.size() == LHS.size() + RHS.size() &&
289 Cur.startswith(LHS) && Cur.endswith(RHS))
292 return MakeArgString(LHS + RHS);
297 InputArgList::InputArgList(const char* const *ArgBegin,
298 const char* const *ArgEnd)
299 : NumInputArgStrings(ArgEnd - ArgBegin) {
300 ArgStrings.append(ArgBegin, ArgEnd);
303 InputArgList::~InputArgList() {
304 // An InputArgList always owns its arguments.
305 for (iterator it = begin(), ie = end(); it != ie; ++it)
309 unsigned InputArgList::MakeIndex(StringRef String0) const {
310 unsigned Index = ArgStrings.size();
312 // Tuck away so we have a reliable const char *.
313 SynthesizedStrings.push_back(String0);
314 ArgStrings.push_back(SynthesizedStrings.back().c_str());
319 unsigned InputArgList::MakeIndex(StringRef String0,
320 StringRef String1) const {
321 unsigned Index0 = MakeIndex(String0);
322 unsigned Index1 = MakeIndex(String1);
323 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
328 const char *InputArgList::MakeArgString(StringRef Str) const {
329 return getArgString(MakeIndex(Str));
334 DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
335 : BaseArgs(_BaseArgs) {
338 DerivedArgList::~DerivedArgList() {}
340 const char *DerivedArgList::MakeArgString(StringRef Str) const {
341 return BaseArgs.MakeArgString(Str);
344 void DerivedArgList::AddSynthesizedArg(Arg *A) {
345 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
348 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
349 SynthesizedArgs.push_back(
350 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
351 BaseArgs.MakeIndex(Opt.getName()), BaseArg));
352 return SynthesizedArgs.back().get();
355 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
356 StringRef Value) const {
357 unsigned Index = BaseArgs.MakeIndex(Value);
358 SynthesizedArgs.push_back(
359 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
360 Index, BaseArgs.getArgString(Index), BaseArg));
361 return SynthesizedArgs.back().get();
364 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
365 StringRef Value) const {
366 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
367 SynthesizedArgs.push_back(
368 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
369 Index, BaseArgs.getArgString(Index + 1), BaseArg));
370 return SynthesizedArgs.back().get();
373 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
374 StringRef Value) const {
375 unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str());
376 SynthesizedArgs.push_back(make_unique<Arg>(
377 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
378 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
379 return SynthesizedArgs.back().get();