2009-03-05 01:10:42 +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 "clang/Driver/ArgList.h"
|
|
|
|
#include "clang/Driver/Arg.h"
|
2009-03-05 06:40:08 +08:00
|
|
|
#include "clang/Driver/Option.h"
|
2009-03-05 01:10:42 +08:00
|
|
|
|
2009-09-10 06:32:26 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2009-03-05 01:10:42 +08:00
|
|
|
using namespace clang::driver;
|
|
|
|
|
2009-03-25 12:13:45 +08:00
|
|
|
ArgList::ArgList(arglist_type &_Args) : Args(_Args) {
|
2009-03-05 01:10:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ArgList::~ArgList() {
|
|
|
|
}
|
2009-03-05 06:40:08 +08:00
|
|
|
|
|
|
|
void ArgList::append(Arg *A) {
|
|
|
|
Args.push_back(A);
|
|
|
|
}
|
2009-03-12 09:36:44 +08:00
|
|
|
|
2009-11-19 12:00:53 +08:00
|
|
|
Arg *ArgList::getLastArgNoClaim(options::ID Id) const {
|
2009-03-12 09:36:44 +08:00
|
|
|
// FIXME: Make search efficient?
|
2009-11-19 12:00:53 +08:00
|
|
|
for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
|
|
|
|
if ((*it)->getOption().matches(Id))
|
2009-03-13 00:03:38 +08:00
|
|
|
return *it;
|
|
|
|
return 0;
|
2009-03-12 09:36:44 +08:00
|
|
|
}
|
2009-03-13 02:20:18 +08:00
|
|
|
|
2009-11-19 12:00:53 +08:00
|
|
|
Arg *ArgList::getLastArg(options::ID Id) const {
|
|
|
|
Arg *A = getLastArgNoClaim(Id);
|
|
|
|
if (A)
|
|
|
|
A->claim();
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg *ArgList::getLastArg(options::ID Id0, options::ID Id1) const {
|
|
|
|
Arg *Res, *A0 = getLastArgNoClaim(Id0), *A1 = getLastArgNoClaim(Id1);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-18 02:51:42 +08:00
|
|
|
if (A0 && A1)
|
|
|
|
Res = A0->getIndex() > A1->getIndex() ? A0 : A1;
|
|
|
|
else
|
|
|
|
Res = A0 ? A0 : A1;
|
|
|
|
|
2009-11-19 12:00:53 +08:00
|
|
|
if (Res)
|
2009-03-18 02:51:42 +08:00
|
|
|
Res->claim();
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2009-11-19 12:00:53 +08:00
|
|
|
Arg *ArgList::getLastArg(options::ID Id0, options::ID Id1,
|
|
|
|
options::ID Id2) const {
|
2009-06-28 15:36:13 +08:00
|
|
|
Arg *Res = 0;
|
2009-11-19 12:00:53 +08:00
|
|
|
Arg *A0 = getLastArgNoClaim(Id0);
|
|
|
|
Arg *A1 = getLastArgNoClaim(Id1);
|
|
|
|
Arg *A2 = getLastArgNoClaim(Id2);
|
2009-06-28 15:36:13 +08:00
|
|
|
|
|
|
|
int A0Idx = A0 ? A0->getIndex() : -1;
|
|
|
|
int A1Idx = A1 ? A1->getIndex() : -1;
|
|
|
|
int A2Idx = A2 ? A2->getIndex() : -1;
|
|
|
|
|
|
|
|
if (A0Idx > A1Idx) {
|
|
|
|
if (A0Idx > A2Idx)
|
|
|
|
Res = A0;
|
|
|
|
else if (A2Idx != -1)
|
|
|
|
Res = A2;
|
|
|
|
} else {
|
|
|
|
if (A1Idx > A2Idx)
|
|
|
|
Res = A1;
|
|
|
|
else if (A2Idx != -1)
|
|
|
|
Res = A2;
|
|
|
|
}
|
|
|
|
|
2009-11-19 12:00:53 +08:00
|
|
|
if (Res)
|
2009-06-28 15:36:13 +08:00
|
|
|
Res->claim();
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2009-03-18 17:29:36 +08:00
|
|
|
bool ArgList::hasFlag(options::ID Pos, options::ID Neg, bool Default) const {
|
2009-04-08 05:08:57 +08:00
|
|
|
if (Arg *A = getLastArg(Pos, Neg))
|
|
|
|
return A->getOption().matches(Pos);
|
2009-03-18 17:29:36 +08:00
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::AddLastArg(ArgStringList &Output, options::ID Id) const {
|
|
|
|
if (Arg *A = getLastArg(Id)) {
|
|
|
|
A->claim();
|
|
|
|
A->render(*this, Output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0) const {
|
|
|
|
// FIXME: Make fast.
|
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
const Arg *A = *it;
|
|
|
|
if (A->getOption().matches(Id0)) {
|
|
|
|
A->claim();
|
|
|
|
A->render(*this, Output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
|
2009-03-18 17:29:36 +08:00
|
|
|
options::ID Id1) const {
|
|
|
|
// FIXME: Make fast.
|
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
const Arg *A = *it;
|
|
|
|
if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
|
|
|
|
A->claim();
|
|
|
|
A->render(*this, Output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
|
2009-03-18 17:29:36 +08:00
|
|
|
options::ID Id1, options::ID Id2) const {
|
|
|
|
// FIXME: Make fast.
|
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
const Arg *A = *it;
|
|
|
|
if (A->getOption().matches(Id0) || A->getOption().matches(Id1) ||
|
|
|
|
A->getOption().matches(Id2)) {
|
|
|
|
A->claim();
|
|
|
|
A->render(*this, Output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0) const {
|
|
|
|
// FIXME: Make fast.
|
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
const Arg *A = *it;
|
|
|
|
if (A->getOption().matches(Id0)) {
|
|
|
|
A->claim();
|
|
|
|
for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
|
|
|
|
Output.push_back(A->getValue(*this, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-20 23:59:01 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0,
|
2009-03-20 23:59:01 +08:00
|
|
|
options::ID Id1) const {
|
|
|
|
// FIXME: Make fast.
|
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
const Arg *A = *it;
|
|
|
|
if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
|
|
|
|
A->claim();
|
|
|
|
for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
|
|
|
|
Output.push_back(A->getValue(*this, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-25 12:13:45 +08:00
|
|
|
|
2009-03-26 23:39:22 +08:00
|
|
|
void ArgList::AddAllArgsTranslated(ArgStringList &Output, options::ID Id0,
|
2009-04-26 09:07:52 +08:00
|
|
|
const char *Translation,
|
|
|
|
bool Joined) const {
|
2009-03-26 23:39:22 +08:00
|
|
|
// FIXME: Make fast.
|
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
const Arg *A = *it;
|
|
|
|
if (A->getOption().matches(Id0)) {
|
|
|
|
A->claim();
|
2009-04-26 09:07:52 +08:00
|
|
|
|
|
|
|
if (Joined) {
|
|
|
|
std::string Value = Translation;
|
|
|
|
Value += A->getValue(*this, 0);
|
|
|
|
Output.push_back(MakeArgString(Value.c_str()));
|
|
|
|
} else {
|
|
|
|
Output.push_back(Translation);
|
|
|
|
Output.push_back(A->getValue(*this, 0));
|
|
|
|
}
|
2009-03-26 23:39:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-04 04:51:31 +08:00
|
|
|
void ArgList::ClaimAllArgs(options::ID Id0) const {
|
|
|
|
// FIXME: Make fast.
|
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
const Arg *A = *it;
|
|
|
|
if (A->getOption().matches(Id0))
|
|
|
|
A->claim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-10 06:32:26 +08:00
|
|
|
const char *ArgList::MakeArgString(const llvm::Twine &T) const {
|
|
|
|
llvm::SmallString<256> Str;
|
|
|
|
T.toVector(Str);
|
|
|
|
return MakeArgString(Str.str());
|
|
|
|
}
|
|
|
|
|
2009-03-25 12:13:45 +08:00
|
|
|
//
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
InputArgList::InputArgList(const char **ArgBegin, const char **ArgEnd)
|
|
|
|
: ArgList(ActualArgs), NumInputArgStrings(ArgEnd - ArgBegin) {
|
2009-03-25 12:13:45 +08:00
|
|
|
ArgStrings.append(ArgBegin, ArgEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
InputArgList::~InputArgList() {
|
|
|
|
// An InputArgList always owns its arguments.
|
|
|
|
for (iterator it = begin(), ie = end(); it != ie; ++it)
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
|
2009-09-10 06:32:26 +08:00
|
|
|
unsigned InputArgList::MakeIndex(llvm::StringRef String0) const {
|
2009-03-25 12:13:45 +08:00
|
|
|
unsigned Index = ArgStrings.size();
|
|
|
|
|
|
|
|
// Tuck away so we have a reliable const char *.
|
|
|
|
SynthesizedStrings.push_back(String0);
|
|
|
|
ArgStrings.push_back(SynthesizedStrings.back().c_str());
|
|
|
|
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2009-09-10 06:32:26 +08:00
|
|
|
unsigned InputArgList::MakeIndex(llvm::StringRef String0,
|
|
|
|
llvm::StringRef String1) const {
|
2009-03-25 12:13:45 +08:00
|
|
|
unsigned Index0 = MakeIndex(String0);
|
|
|
|
unsigned Index1 = MakeIndex(String1);
|
|
|
|
assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
|
|
|
|
(void) Index1;
|
|
|
|
return Index0;
|
|
|
|
}
|
|
|
|
|
2009-09-10 06:32:26 +08:00
|
|
|
const char *InputArgList::MakeArgString(llvm::StringRef Str) const {
|
2009-03-25 12:13:45 +08:00
|
|
|
return getArgString(MakeIndex(Str));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
DerivedArgList::DerivedArgList(InputArgList &_BaseArgs, bool _OnlyProxy)
|
|
|
|
: ArgList(_OnlyProxy ? _BaseArgs.getArgs() : ActualArgs),
|
2009-09-09 23:08:12 +08:00
|
|
|
BaseArgs(_BaseArgs), OnlyProxy(_OnlyProxy) {
|
2009-03-25 12:13:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DerivedArgList::~DerivedArgList() {
|
|
|
|
// We only own the arguments we explicitly synthesized.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
|
2009-03-25 12:13:45 +08:00
|
|
|
it != ie; ++it)
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
|
2009-09-10 06:32:26 +08:00
|
|
|
const char *DerivedArgList::MakeArgString(llvm::StringRef Str) const {
|
2009-03-25 12:13:45 +08:00
|
|
|
return BaseArgs.MakeArgString(Str);
|
|
|
|
}
|
|
|
|
|
2009-03-30 06:29:05 +08:00
|
|
|
Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
|
|
|
|
return new FlagArg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
|
2009-03-25 12:13:45 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
|
2009-09-10 06:32:26 +08:00
|
|
|
llvm::StringRef Value) const {
|
2009-03-30 06:29:05 +08:00
|
|
|
return new PositionalArg(Opt, BaseArgs.MakeIndex(Value), BaseArg);
|
2009-03-25 12:13:45 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
|
2009-09-10 06:32:26 +08:00
|
|
|
llvm::StringRef Value) const {
|
2009-09-09 23:08:12 +08:00
|
|
|
return new SeparateArg(Opt, BaseArgs.MakeIndex(Opt->getName(), Value), 1,
|
2009-03-30 06:29:05 +08:00
|
|
|
BaseArg);
|
2009-03-25 12:13:45 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
|
2009-09-10 06:32:26 +08:00
|
|
|
llvm::StringRef Value) const {
|
2009-03-25 12:13:45 +08:00
|
|
|
std::string Joined(Opt->getName());
|
|
|
|
Joined += Value;
|
2009-03-30 06:29:05 +08:00
|
|
|
return new JoinedArg(Opt, BaseArgs.MakeIndex(Joined.c_str()), BaseArg);
|
2009-03-25 12:13:45 +08:00
|
|
|
}
|