2012-12-05 08:29:32 +08:00
|
|
|
//===--- ArgList.cpp - Argument List Management ---------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Option/ArgList.h"
|
2014-04-21 06:37:46 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::opt;
|
|
|
|
|
|
|
|
void arg_iterator::SkipToNextArg() {
|
|
|
|
for (; Current != Args.end(); ++Current) {
|
|
|
|
// Done if there are no filters.
|
|
|
|
if (!Id0.isValid())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Otherwise require a match.
|
|
|
|
const Option &O = (*Current)->getOption();
|
|
|
|
if (O.matches(Id0) ||
|
|
|
|
(Id1.isValid() && O.matches(Id1)) ||
|
|
|
|
(Id2.isValid() && O.matches(Id2)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ArgList::~ArgList() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::append(Arg *A) {
|
|
|
|
Args.push_back(A);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::eraseArg(OptSpecifier Id) {
|
2014-05-18 23:14:13 +08:00
|
|
|
Args.erase(std::remove_if(begin(), end(),
|
|
|
|
[=](Arg *A) { return A->getOption().matches(Id); }),
|
|
|
|
end());
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const {
|
|
|
|
// FIXME: Make search efficient?
|
|
|
|
for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
|
|
|
|
if ((*it)->getOption().matches(Id))
|
|
|
|
return *it;
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
2014-09-13 03:42:53 +08:00
|
|
|
Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1) const {
|
|
|
|
// FIXME: Make search efficient?
|
|
|
|
for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
|
|
|
|
if ((*it)->getOption().matches(Id0) ||
|
|
|
|
(*it)->getOption().matches(Id1))
|
|
|
|
return *it;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-21 07:32:58 +08:00
|
|
|
Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1,
|
|
|
|
OptSpecifier Id2) const {
|
|
|
|
// FIXME: Make search efficient?
|
|
|
|
for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
|
|
|
|
if ((*it)->getOption().matches(Id0) || (*it)->getOption().matches(Id1) ||
|
|
|
|
(*it)->getOption().matches(Id2))
|
|
|
|
return *it;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg *ArgList::getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1,
|
|
|
|
OptSpecifier Id2, OptSpecifier Id3) const {
|
|
|
|
// FIXME: Make search efficient?
|
|
|
|
for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
|
|
|
|
if ((*it)->getOption().matches(Id0) || (*it)->getOption().matches(Id1) ||
|
|
|
|
(*it)->getOption().matches(Id2) || (*it)->getOption().matches(Id3))
|
|
|
|
return *it;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-12-05 08:29:32 +08:00
|
|
|
Arg *ArgList::getLastArg(OptSpecifier Id) const {
|
2014-04-15 14:32:26 +08:00
|
|
|
Arg *Res = nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
if ((*it)->getOption().matches(Id)) {
|
|
|
|
Res = *it;
|
|
|
|
Res->claim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
|
2014-04-15 14:32:26 +08:00
|
|
|
Arg *Res = nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
if ((*it)->getOption().matches(Id0) ||
|
|
|
|
(*it)->getOption().matches(Id1)) {
|
|
|
|
Res = *it;
|
|
|
|
Res->claim();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
|
|
|
|
OptSpecifier Id2) const {
|
2014-04-15 14:32:26 +08:00
|
|
|
Arg *Res = nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
if ((*it)->getOption().matches(Id0) ||
|
|
|
|
(*it)->getOption().matches(Id1) ||
|
|
|
|
(*it)->getOption().matches(Id2)) {
|
|
|
|
Res = *it;
|
|
|
|
Res->claim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
|
|
|
|
OptSpecifier Id2, OptSpecifier Id3) const {
|
2014-04-15 14:32:26 +08:00
|
|
|
Arg *Res = nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
if ((*it)->getOption().matches(Id0) ||
|
|
|
|
(*it)->getOption().matches(Id1) ||
|
|
|
|
(*it)->getOption().matches(Id2) ||
|
|
|
|
(*it)->getOption().matches(Id3)) {
|
|
|
|
Res = *it;
|
|
|
|
Res->claim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
|
|
|
|
OptSpecifier Id2, OptSpecifier Id3,
|
|
|
|
OptSpecifier Id4) const {
|
2014-04-15 14:32:26 +08:00
|
|
|
Arg *Res = nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
if ((*it)->getOption().matches(Id0) ||
|
|
|
|
(*it)->getOption().matches(Id1) ||
|
|
|
|
(*it)->getOption().matches(Id2) ||
|
|
|
|
(*it)->getOption().matches(Id3) ||
|
|
|
|
(*it)->getOption().matches(Id4)) {
|
|
|
|
Res = *it;
|
|
|
|
Res->claim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
|
|
|
|
OptSpecifier Id2, OptSpecifier Id3,
|
|
|
|
OptSpecifier Id4, OptSpecifier Id5) const {
|
2014-04-15 14:32:26 +08:00
|
|
|
Arg *Res = nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
if ((*it)->getOption().matches(Id0) ||
|
|
|
|
(*it)->getOption().matches(Id1) ||
|
|
|
|
(*it)->getOption().matches(Id2) ||
|
|
|
|
(*it)->getOption().matches(Id3) ||
|
|
|
|
(*it)->getOption().matches(Id4) ||
|
|
|
|
(*it)->getOption().matches(Id5)) {
|
|
|
|
Res = *it;
|
|
|
|
Res->claim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
|
|
|
|
OptSpecifier Id2, OptSpecifier Id3,
|
|
|
|
OptSpecifier Id4, OptSpecifier Id5,
|
|
|
|
OptSpecifier Id6) const {
|
2014-04-15 14:32:26 +08:00
|
|
|
Arg *Res = nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
if ((*it)->getOption().matches(Id0) ||
|
|
|
|
(*it)->getOption().matches(Id1) ||
|
|
|
|
(*it)->getOption().matches(Id2) ||
|
|
|
|
(*it)->getOption().matches(Id3) ||
|
|
|
|
(*it)->getOption().matches(Id4) ||
|
|
|
|
(*it)->getOption().matches(Id5) ||
|
|
|
|
(*it)->getOption().matches(Id6)) {
|
|
|
|
Res = *it;
|
|
|
|
Res->claim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
|
|
|
|
OptSpecifier Id2, OptSpecifier Id3,
|
|
|
|
OptSpecifier Id4, OptSpecifier Id5,
|
|
|
|
OptSpecifier Id6, OptSpecifier Id7) const {
|
2014-04-15 14:32:26 +08:00
|
|
|
Arg *Res = nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
if ((*it)->getOption().matches(Id0) ||
|
|
|
|
(*it)->getOption().matches(Id1) ||
|
|
|
|
(*it)->getOption().matches(Id2) ||
|
|
|
|
(*it)->getOption().matches(Id3) ||
|
|
|
|
(*it)->getOption().matches(Id4) ||
|
|
|
|
(*it)->getOption().matches(Id5) ||
|
|
|
|
(*it)->getOption().matches(Id6) ||
|
|
|
|
(*it)->getOption().matches(Id7)) {
|
|
|
|
Res = *it;
|
|
|
|
Res->claim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
|
|
|
|
if (Arg *A = getLastArg(Pos, Neg))
|
|
|
|
return A->getOption().matches(Pos);
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
2013-06-14 02:12:12 +08:00
|
|
|
bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
|
|
|
|
bool Default) const {
|
|
|
|
if (Arg *A = getLastArg(Pos, PosAlias, Neg))
|
|
|
|
return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
2012-12-05 08:29:32 +08:00
|
|
|
StringRef ArgList::getLastArgValue(OptSpecifier Id,
|
|
|
|
StringRef Default) const {
|
|
|
|
if (Arg *A = getLastArg(Id))
|
|
|
|
return A->getValue();
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
|
|
|
|
SmallVector<const char *, 16> Values;
|
|
|
|
AddAllArgValues(Values, Id);
|
|
|
|
return std::vector<std::string>(Values.begin(), Values.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
|
|
|
|
if (Arg *A = getLastArg(Id)) {
|
|
|
|
A->claim();
|
|
|
|
A->render(*this, Output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 02:12:12 +08:00
|
|
|
void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
|
|
|
|
OptSpecifier Id1) const {
|
|
|
|
if (Arg *A = getLastArg(Id0, Id1)) {
|
|
|
|
A->claim();
|
|
|
|
A->render(*this, Output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 08:29:32 +08:00
|
|
|
void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
|
|
|
|
OptSpecifier Id1, OptSpecifier Id2) const {
|
2014-07-09 21:03:37 +08:00
|
|
|
for (auto Arg: filtered(Id0, Id1, Id2)) {
|
|
|
|
Arg->claim();
|
|
|
|
Arg->render(*this, Output);
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
|
|
|
|
OptSpecifier Id1, OptSpecifier Id2) const {
|
2014-07-09 21:03:37 +08:00
|
|
|
for (auto Arg : filtered(Id0, Id1, Id2)) {
|
|
|
|
Arg->claim();
|
2015-02-17 23:29:18 +08:00
|
|
|
const auto &Values = Arg->getValues();
|
|
|
|
Output.append(Values.begin(), Values.end());
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
|
|
|
|
const char *Translation,
|
|
|
|
bool Joined) const {
|
2014-07-09 21:03:37 +08:00
|
|
|
for (auto Arg: filtered(Id0)) {
|
|
|
|
Arg->claim();
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
if (Joined) {
|
|
|
|
Output.push_back(MakeArgString(StringRef(Translation) +
|
2014-07-09 21:03:37 +08:00
|
|
|
Arg->getValue(0)));
|
2012-12-05 08:29:32 +08:00
|
|
|
} else {
|
|
|
|
Output.push_back(Translation);
|
2014-07-09 21:03:37 +08:00
|
|
|
Output.push_back(Arg->getValue(0));
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
|
2014-07-09 21:03:37 +08:00
|
|
|
for (auto Arg : filtered(Id0))
|
|
|
|
Arg->claim();
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::ClaimAllArgs() const {
|
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it)
|
|
|
|
if (!(*it)->isClaimed())
|
|
|
|
(*it)->claim();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
|
|
|
|
StringRef LHS,
|
|
|
|
StringRef RHS) const {
|
|
|
|
StringRef Cur = getArgString(Index);
|
|
|
|
if (Cur.size() == LHS.size() + RHS.size() &&
|
|
|
|
Cur.startswith(LHS) && Cur.endswith(RHS))
|
|
|
|
return Cur.data();
|
|
|
|
|
|
|
|
return MakeArgString(LHS + RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
InputArgList::InputArgList(const char* const *ArgBegin,
|
|
|
|
const char* const *ArgEnd)
|
|
|
|
: NumInputArgStrings(ArgEnd - ArgBegin) {
|
|
|
|
ArgStrings.append(ArgBegin, ArgEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
InputArgList::~InputArgList() {
|
|
|
|
// An InputArgList always owns its arguments.
|
|
|
|
for (iterator it = begin(), ie = end(); it != ie; ++it)
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned InputArgList::MakeIndex(StringRef String0) const {
|
|
|
|
unsigned Index = ArgStrings.size();
|
|
|
|
|
|
|
|
// Tuck away so we have a reliable const char *.
|
2013-07-16 00:40:52 +08:00
|
|
|
SynthesizedStrings.push_back(String0);
|
|
|
|
ArgStrings.push_back(SynthesizedStrings.back().c_str());
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned InputArgList::MakeIndex(StringRef String0,
|
|
|
|
StringRef String1) const {
|
|
|
|
unsigned Index0 = MakeIndex(String0);
|
|
|
|
unsigned Index1 = MakeIndex(String1);
|
|
|
|
assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
|
|
|
|
(void) Index1;
|
|
|
|
return Index0;
|
|
|
|
}
|
|
|
|
|
2015-03-17 17:51:17 +08:00
|
|
|
const char *InputArgList::MakeArgStringRef(StringRef Str) const {
|
2012-12-05 08:29:32 +08:00
|
|
|
return getArgString(MakeIndex(Str));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2015-03-17 02:06:57 +08:00
|
|
|
DerivedArgList::DerivedArgList(const InputArgList &BaseArgs)
|
|
|
|
: BaseArgs(BaseArgs) {}
|
2012-12-05 08:29:32 +08:00
|
|
|
|
2014-04-21 06:37:46 +08:00
|
|
|
DerivedArgList::~DerivedArgList() {}
|
2012-12-05 08:29:32 +08:00
|
|
|
|
2015-03-17 17:51:17 +08:00
|
|
|
const char *DerivedArgList::MakeArgStringRef(StringRef Str) const {
|
2012-12-05 08:29:32 +08:00
|
|
|
return BaseArgs.MakeArgString(Str);
|
|
|
|
}
|
|
|
|
|
2014-04-21 06:37:46 +08:00
|
|
|
void DerivedArgList::AddSynthesizedArg(Arg *A) {
|
|
|
|
SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
|
|
|
|
}
|
|
|
|
|
2012-12-05 08:29:32 +08:00
|
|
|
Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
|
2014-06-20 12:36:29 +08:00
|
|
|
SynthesizedArgs.push_back(
|
|
|
|
make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
|
|
|
|
BaseArgs.MakeIndex(Opt.getName()), BaseArg));
|
2014-04-21 06:37:46 +08:00
|
|
|
return SynthesizedArgs.back().get();
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
|
|
|
|
StringRef Value) const {
|
|
|
|
unsigned Index = BaseArgs.MakeIndex(Value);
|
2014-06-20 12:36:29 +08:00
|
|
|
SynthesizedArgs.push_back(
|
|
|
|
make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
|
|
|
|
Index, BaseArgs.getArgString(Index), BaseArg));
|
2014-04-21 06:37:46 +08:00
|
|
|
return SynthesizedArgs.back().get();
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
|
|
|
|
StringRef Value) const {
|
|
|
|
unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
|
2014-06-20 12:36:29 +08:00
|
|
|
SynthesizedArgs.push_back(
|
|
|
|
make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
|
|
|
|
Index, BaseArgs.getArgString(Index + 1), BaseArg));
|
2014-04-21 06:37:46 +08:00
|
|
|
return SynthesizedArgs.back().get();
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
|
|
|
|
StringRef Value) const {
|
2015-03-30 23:42:36 +08:00
|
|
|
unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
|
2014-04-21 06:37:46 +08:00
|
|
|
SynthesizedArgs.push_back(make_unique<Arg>(
|
2014-06-20 12:36:29 +08:00
|
|
|
Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
|
|
|
|
BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
|
2014-04-21 06:37:46 +08:00
|
|
|
return SynthesizedArgs.back().get();
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|