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
|
|
|
|
|
|
|
using namespace clang::driver;
|
|
|
|
|
2009-03-23 07:26:43 +08:00
|
|
|
ArgList::ArgList(const char **ArgBegin, const char **ArgEnd)
|
|
|
|
: NumInputArgStrings(ArgEnd - ArgBegin)
|
|
|
|
{
|
2009-03-05 01:10:42 +08:00
|
|
|
ArgStrings.append(ArgBegin, ArgEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArgList::~ArgList() {
|
2009-03-18 04:44:29 +08:00
|
|
|
for (iterator it = begin(), ie = end(); it != ie; ++it)
|
2009-03-05 01:10:42 +08:00
|
|
|
delete *it;
|
|
|
|
}
|
2009-03-05 06:40:08 +08:00
|
|
|
|
|
|
|
void ArgList::append(Arg *A) {
|
|
|
|
if (A->getOption().isUnsupported()) {
|
|
|
|
assert(0 && "FIXME: unsupported unsupported.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Args.push_back(A);
|
|
|
|
}
|
2009-03-12 09:36:44 +08:00
|
|
|
|
2009-03-15 08:48:16 +08:00
|
|
|
Arg *ArgList::getLastArg(options::ID Id, bool Claim) const {
|
2009-03-12 09:36:44 +08:00
|
|
|
// FIXME: Make search efficient?
|
|
|
|
|
|
|
|
// FIXME: This needs to not require loading of the option.
|
2009-03-15 08:48:16 +08:00
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
|
|
|
|
if ((*it)->getOption().matches(Id)) {
|
|
|
|
if (Claim) (*it)->claim();
|
2009-03-13 00:03:38 +08:00
|
|
|
return *it;
|
2009-03-15 08:48:16 +08:00
|
|
|
}
|
|
|
|
}
|
2009-03-12 09:36:44 +08:00
|
|
|
|
2009-03-13 00:03:38 +08:00
|
|
|
return 0;
|
2009-03-12 09:36:44 +08:00
|
|
|
}
|
2009-03-13 02:20:18 +08:00
|
|
|
|
2009-03-18 02:51:42 +08:00
|
|
|
Arg *ArgList::getLastArg(options::ID Id0, options::ID Id1, bool Claim) const {
|
|
|
|
Arg *Res, *A0 = getLastArg(Id0, false), *A1 = getLastArg(Id1, false);
|
|
|
|
|
|
|
|
if (A0 && A1)
|
|
|
|
Res = A0->getIndex() > A1->getIndex() ? A0 : A1;
|
|
|
|
else
|
|
|
|
Res = A0 ? A0 : A1;
|
|
|
|
|
|
|
|
if (Claim && Res)
|
|
|
|
Res->claim();
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2009-03-18 17:29:36 +08:00
|
|
|
bool ArgList::hasFlag(options::ID Pos, options::ID Neg, bool Default) const {
|
|
|
|
Arg *PosA = getLastArg(Pos);
|
|
|
|
Arg *NegA = getLastArg(Pos);
|
|
|
|
if (PosA && NegA)
|
|
|
|
return NegA->getIndex() < PosA->getIndex();
|
|
|
|
if (PosA) return true;
|
|
|
|
if (NegA) return false;
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
2009-03-18 01:51:18 +08:00
|
|
|
unsigned ArgList::MakeIndex(const char *String0) const {
|
2009-03-13 02:20:18 +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-03-18 01:51:18 +08:00
|
|
|
unsigned ArgList::MakeIndex(const char *String0, const char *String1) const {
|
2009-03-13 02:20:18 +08:00
|
|
|
unsigned Index0 = MakeIndex(String0);
|
|
|
|
unsigned Index1 = MakeIndex(String1);
|
2009-03-14 01:25:24 +08:00
|
|
|
assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
|
2009-03-13 02:20:18 +08:00
|
|
|
(void) Index1;
|
|
|
|
return Index0;
|
|
|
|
}
|
|
|
|
|
2009-03-18 01:51:18 +08:00
|
|
|
const char *ArgList::MakeArgString(const char *Str) const {
|
|
|
|
return getArgString(MakeIndex(Str));
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg *ArgList::MakeFlagArg(const Option *Opt) const {
|
2009-03-13 02:20:18 +08:00
|
|
|
return new FlagArg(Opt, MakeIndex(Opt->getName()));
|
|
|
|
}
|
|
|
|
|
2009-03-18 01:51:18 +08:00
|
|
|
Arg *ArgList::MakePositionalArg(const Option *Opt, const char *Value) const {
|
2009-03-13 02:20:18 +08:00
|
|
|
return new PositionalArg(Opt, MakeIndex(Value));
|
|
|
|
}
|
|
|
|
|
2009-03-18 01:51:18 +08:00
|
|
|
Arg *ArgList::MakeSeparateArg(const Option *Opt, const char *Value) const {
|
2009-03-13 02:20:18 +08:00
|
|
|
return new SeparateArg(Opt, MakeIndex(Opt->getName(), Value), 1);
|
|
|
|
}
|
|
|
|
|
2009-03-18 01:51:18 +08:00
|
|
|
Arg *ArgList::MakeJoinedArg(const Option *Opt, const char *Value) const {
|
2009-03-13 02:20:18 +08:00
|
|
|
std::string Joined(Opt->getName());
|
|
|
|
Joined += Value;
|
|
|
|
return new JoinedArg(Opt, MakeIndex(Joined.c_str()));
|
|
|
|
}
|
2009-03-18 17:29:36 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
|
|
|
|
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
|
|
|
|
|
|
|
void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0,
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|