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)))
36 void ArgList::append(Arg *A) {
40 void ArgList::eraseArg(OptSpecifier Id) {
41 Args.erase(std::remove_if(begin(), end(),
42 [=](Arg *A) { return A->getOption().matches(Id); }),
46 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const {
47 // FIXME: Make search efficient?
48 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
49 if ((*it)->getOption().matches(Id))
54 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1) const {
55 // FIXME: Make search efficient?
56 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
57 if ((*it)->getOption().matches(Id0) ||
58 (*it)->getOption().matches(Id1))
63 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1,
64 OptSpecifier Id2) const {
65 // FIXME: Make search efficient?
66 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
67 if ((*it)->getOption().matches(Id0) || (*it)->getOption().matches(Id1) ||
68 (*it)->getOption().matches(Id2))
73 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1,
74 OptSpecifier Id2, OptSpecifier Id3) const {
75 // FIXME: Make search efficient?
76 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
77 if ((*it)->getOption().matches(Id0) || (*it)->getOption().matches(Id1) ||
78 (*it)->getOption().matches(Id2) || (*it)->getOption().matches(Id3))
83 Arg *ArgList::getLastArg(OptSpecifier Id) const {
85 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
86 if ((*it)->getOption().matches(Id)) {
95 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
97 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
98 if ((*it)->getOption().matches(Id0) ||
99 (*it)->getOption().matches(Id1)) {
109 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
110 OptSpecifier Id2) const {
112 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
113 if ((*it)->getOption().matches(Id0) ||
114 (*it)->getOption().matches(Id1) ||
115 (*it)->getOption().matches(Id2)) {
124 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
125 OptSpecifier Id2, OptSpecifier Id3) 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)) {
140 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
141 OptSpecifier Id2, OptSpecifier Id3,
142 OptSpecifier Id4) const {
144 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
145 if ((*it)->getOption().matches(Id0) ||
146 (*it)->getOption().matches(Id1) ||
147 (*it)->getOption().matches(Id2) ||
148 (*it)->getOption().matches(Id3) ||
149 (*it)->getOption().matches(Id4)) {
158 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
159 OptSpecifier Id2, OptSpecifier Id3,
160 OptSpecifier Id4, OptSpecifier Id5) const {
162 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
163 if ((*it)->getOption().matches(Id0) ||
164 (*it)->getOption().matches(Id1) ||
165 (*it)->getOption().matches(Id2) ||
166 (*it)->getOption().matches(Id3) ||
167 (*it)->getOption().matches(Id4) ||
168 (*it)->getOption().matches(Id5)) {
177 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
178 OptSpecifier Id2, OptSpecifier Id3,
179 OptSpecifier Id4, OptSpecifier Id5,
180 OptSpecifier Id6) const {
182 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
183 if ((*it)->getOption().matches(Id0) ||
184 (*it)->getOption().matches(Id1) ||
185 (*it)->getOption().matches(Id2) ||
186 (*it)->getOption().matches(Id3) ||
187 (*it)->getOption().matches(Id4) ||
188 (*it)->getOption().matches(Id5) ||
189 (*it)->getOption().matches(Id6)) {
198 Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
199 OptSpecifier Id2, OptSpecifier Id3,
200 OptSpecifier Id4, OptSpecifier Id5,
201 OptSpecifier Id6, OptSpecifier Id7) const {
203 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
204 if ((*it)->getOption().matches(Id0) ||
205 (*it)->getOption().matches(Id1) ||
206 (*it)->getOption().matches(Id2) ||
207 (*it)->getOption().matches(Id3) ||
208 (*it)->getOption().matches(Id4) ||
209 (*it)->getOption().matches(Id5) ||
210 (*it)->getOption().matches(Id6) ||
211 (*it)->getOption().matches(Id7)) {
220 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
221 if (Arg *A = getLastArg(Pos, Neg))
222 return A->getOption().matches(Pos);
226 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
227 bool Default) const {
228 if (Arg *A = getLastArg(Pos, PosAlias, Neg))
229 return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
233 StringRef ArgList::getLastArgValue(OptSpecifier Id,
234 StringRef Default) const {
235 if (Arg *A = getLastArg(Id))
236 return A->getValue();
240 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
241 SmallVector<const char *, 16> Values;
242 AddAllArgValues(Values, Id);
243 return std::vector<std::string>(Values.begin(), Values.end());
246 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
247 if (Arg *A = getLastArg(Id)) {
249 A->render(*this, Output);
253 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
254 OptSpecifier Id1) const {
255 if (Arg *A = getLastArg(Id0, Id1)) {
257 A->render(*this, Output);
261 void ArgList::AddAllArgs(ArgStringList &Output,
262 ArrayRef<OptSpecifier> Ids) const {
263 for (const Arg *Arg : Args) {
264 for (OptSpecifier Id : Ids) {
265 if (Arg->getOption().matches(Id)) {
267 Arg->render(*this, Output);
274 /// This 3-opt variant of AddAllArgs could be eliminated in favor of one
275 /// that accepts a single specifier, given the above which accepts any number.
276 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
277 OptSpecifier Id1, OptSpecifier Id2) const {
278 for (auto Arg: filtered(Id0, Id1, Id2)) {
280 Arg->render(*this, Output);
284 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
285 OptSpecifier Id1, OptSpecifier Id2) const {
286 for (auto Arg : filtered(Id0, Id1, Id2)) {
288 const auto &Values = Arg->getValues();
289 Output.append(Values.begin(), Values.end());
293 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
294 const char *Translation,
296 for (auto Arg: filtered(Id0)) {
300 Output.push_back(MakeArgString(StringRef(Translation) +
303 Output.push_back(Translation);
304 Output.push_back(Arg->getValue(0));
309 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
310 for (auto Arg : filtered(Id0))
314 void ArgList::ClaimAllArgs() const {
315 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
316 if (!(*it)->isClaimed())
320 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
322 StringRef RHS) const {
323 StringRef Cur = getArgString(Index);
324 if (Cur.size() == LHS.size() + RHS.size() &&
325 Cur.startswith(LHS) && Cur.endswith(RHS))
328 return MakeArgString(LHS + RHS);
333 void InputArgList::releaseMemory() {
334 // An InputArgList always owns its arguments.
339 InputArgList::InputArgList(const char* const *ArgBegin,
340 const char* const *ArgEnd)
341 : NumInputArgStrings(ArgEnd - ArgBegin) {
342 ArgStrings.append(ArgBegin, ArgEnd);
345 unsigned InputArgList::MakeIndex(StringRef String0) const {
346 unsigned Index = ArgStrings.size();
348 // Tuck away so we have a reliable const char *.
349 SynthesizedStrings.push_back(String0);
350 ArgStrings.push_back(SynthesizedStrings.back().c_str());
355 unsigned InputArgList::MakeIndex(StringRef String0,
356 StringRef String1) const {
357 unsigned Index0 = MakeIndex(String0);
358 unsigned Index1 = MakeIndex(String1);
359 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
364 const char *InputArgList::MakeArgStringRef(StringRef Str) const {
365 return getArgString(MakeIndex(Str));
370 DerivedArgList::DerivedArgList(const InputArgList &BaseArgs)
371 : BaseArgs(BaseArgs) {}
373 const char *DerivedArgList::MakeArgStringRef(StringRef Str) const {
374 return BaseArgs.MakeArgString(Str);
377 void DerivedArgList::AddSynthesizedArg(Arg *A) {
378 SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
381 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
382 SynthesizedArgs.push_back(
383 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
384 BaseArgs.MakeIndex(Opt.getName()), BaseArg));
385 return SynthesizedArgs.back().get();
388 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
389 StringRef Value) const {
390 unsigned Index = BaseArgs.MakeIndex(Value);
391 SynthesizedArgs.push_back(
392 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
393 Index, BaseArgs.getArgString(Index), BaseArg));
394 return SynthesizedArgs.back().get();
397 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
398 StringRef Value) const {
399 unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
400 SynthesizedArgs.push_back(
401 make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
402 Index, BaseArgs.getArgString(Index + 1), BaseArg));
403 return SynthesizedArgs.back().get();
406 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
407 StringRef Value) const {
408 unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
409 SynthesizedArgs.push_back(make_unique<Arg>(
410 Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
411 BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
412 return SynthesizedArgs.back().get();