Driver: Change Arg to just hold the values directly, instead of implicitly

deriving them from the Arg type.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@105760 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2010-06-09 22:31:00 +00:00
Родитель bfbb39deab
Коммит 4465a776a5
5 изменённых файлов: 91 добавлений и 107 удалений

Просмотреть файл

@ -18,6 +18,7 @@ using llvm::dyn_cast;
using llvm::dyn_cast_or_null;
#include "Util.h"
#include "llvm/ADT/SmallVector.h"
#include <vector>
#include <string>
@ -58,10 +59,15 @@ namespace driver {
/// ArgList.
unsigned Index;
/// Flag indicating whether this argument was used to effect
/// compilation; used for generating "argument unused"
/// diagnostics.
mutable bool Claimed;
/// Was this argument used to effect compilation; used for generating
/// "argument unused" diagnostics.
mutable unsigned Claimed : 1;
/// Does this argument own its values.
mutable unsigned OwnsValues : 1;
/// The argument values, as C strings.
llvm::SmallVector<const char *, 2> Values;
protected:
Arg(ArgClass Kind, const Option *Opt, unsigned Index,
@ -85,16 +91,22 @@ namespace driver {
BaseArg = _BaseArg;
}
bool getOwnsValues() const { return OwnsValues; }
void setOwnsValues(bool Value) const { OwnsValues = Value; }
bool isClaimed() const { return getBaseArg().Claimed; }
/// claim - Set the Arg claimed bit.
// FIXME: We need to deal with derived arguments and set the bit
// in the original argument; not the derived one.
void claim() const { getBaseArg().Claimed = true; }
virtual unsigned getNumValues() const = 0;
virtual const char *getValue(const ArgList &Args, unsigned N=0) const = 0;
unsigned getNumValues() const { return Values.size(); }
const char *getValue(const ArgList &Args, unsigned N=0) const {
return Values[N];
}
llvm::SmallVectorImpl<const char*> &getValues() {
return Values;
}
/// render - Append the argument onto the given array as strings.
virtual void render(const ArgList &Args, ArgStringList &Output) const = 0;
@ -121,9 +133,6 @@ namespace driver {
virtual void render(const ArgList &Args, ArgStringList &Output) const;
virtual unsigned getNumValues() const { return 0; }
virtual const char *getValue(const ArgList &Args, unsigned N=0) const;
static bool classof(const Arg *A) {
return A->getKind() == Arg::FlagClass;
}
@ -133,13 +142,11 @@ namespace driver {
/// PositionalArg - A simple positional argument.
class PositionalArg : public Arg {
public:
PositionalArg(const Option *Opt, unsigned Index, const Arg *BaseArg = 0);
PositionalArg(const Option *Opt, unsigned Index, const char *Value,
const Arg *BaseArg = 0);
virtual void render(const ArgList &Args, ArgStringList &Output) const;
virtual unsigned getNumValues() const { return 1; }
virtual const char *getValue(const ArgList &Args, unsigned N=0) const;
static bool classof(const Arg *A) {
return A->getKind() == Arg::PositionalClass;
}
@ -149,18 +156,12 @@ namespace driver {
/// JoinedArg - A single value argument where the value is joined
/// (suffixed) to the option.
class JoinedArg : public Arg {
/// The offset of the joined argument value.
unsigned Offset;
public:
JoinedArg(const Option *Opt, unsigned Index, unsigned Offset,
JoinedArg(const Option *Opt, unsigned Index, const char *Value,
const Arg *BaseArg = 0);
virtual void render(const ArgList &Args, ArgStringList &Output) const;
virtual unsigned getNumValues() const { return 1; }
virtual const char *getValue(const ArgList &Args, unsigned N=0) const;
static bool classof(const Arg *A) {
return A->getKind() == Arg::JoinedClass;
}
@ -170,17 +171,12 @@ namespace driver {
/// SeparateArg - An argument where one or more values follow the
/// option specifier immediately in the argument vector.
class SeparateArg : public Arg {
unsigned NumValues;
public:
SeparateArg(const Option *Opt, unsigned Index, unsigned NumValues,
SeparateArg(const Option *Opt, unsigned Index, const char *Value,
const Arg *BaseArg = 0);
virtual void render(const ArgList &Args, ArgStringList &Output) const;
virtual unsigned getNumValues() const { return NumValues; }
virtual const char *getValue(const ArgList &Args, unsigned N=0) const;
static bool classof(const Arg *A) {
return A->getKind() == Arg::SeparateClass;
}
@ -194,17 +190,12 @@ namespace driver {
/// separate arguments, which allows it to be used as a generic
/// mechanism for passing arguments through to tools.
class CommaJoinedArg : public Arg {
std::vector<std::string> Values;
public:
CommaJoinedArg(const Option *Opt, unsigned Index, const char *Str,
const Arg *BaseArg = 0);
virtual void render(const ArgList &Args, ArgStringList &Output) const;
virtual unsigned getNumValues() const { return Values.size(); }
virtual const char *getValue(const ArgList &Args, unsigned N=0) const;
static bool classof(const Arg *A) {
return A->getKind() == Arg::CommaJoinedClass;
}
@ -214,18 +205,13 @@ namespace driver {
/// JoinedAndSeparateArg - An argument with both joined and separate
/// values.
class JoinedAndSeparateArg : public Arg {
/// The offset of the joined argument value.
unsigned Offset;
public:
JoinedAndSeparateArg(const Option *Opt, unsigned Index,
unsigned Offset, const Arg *BaseArg = 0);
const char *Value0, const char *Value1,
const Arg *BaseArg = 0);
virtual void render(const ArgList &Args, ArgStringList &Output) const;
virtual unsigned getNumValues() const { return 2; }
virtual const char *getValue(const ArgList &Args, unsigned N=0) const;
static bool classof(const Arg *A) {
return A->getKind() == Arg::JoinedAndSeparateClass;
}

Просмотреть файл

@ -17,10 +17,16 @@ using namespace clang::driver;
Arg::Arg(ArgClass _Kind, const Option *_Opt, unsigned _Index,
const Arg *_BaseArg)
: Kind(_Kind), Opt(_Opt), BaseArg(_BaseArg), Index(_Index), Claimed(false) {
: Kind(_Kind), Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
Claimed(false), OwnsValues(false) {
}
Arg::~Arg() { }
Arg::~Arg() {
if (OwnsValues) {
for (unsigned i = 0, e = Values.size(); i != e; ++i)
delete[] Values[i];
}
}
void Arg::dump() const {
llvm::errs() << "<";
@ -82,28 +88,20 @@ void FlagArg::render(const ArgList &Args, ArgStringList &Output) const {
Output.push_back(getOption().getName());
}
const char *FlagArg::getValue(const ArgList &Args, unsigned N) const {
assert(0 && "Invalid index.");
return 0;
}
PositionalArg::PositionalArg(const Option *Opt, unsigned Index,
const Arg *BaseArg)
const char *Value0, const Arg *BaseArg)
: Arg(PositionalClass, Opt, Index, BaseArg) {
getValues().push_back(Value0);
}
void PositionalArg::render(const ArgList &Args, ArgStringList &Output) const {
Output.push_back(Args.getArgString(getIndex()));
}
const char *PositionalArg::getValue(const ArgList &Args, unsigned N) const {
assert(N < getNumValues() && "Invalid index.");
return Args.getArgString(getIndex());
}
JoinedArg::JoinedArg(const Option *Opt, unsigned Index, unsigned _Offset,
JoinedArg::JoinedArg(const Option *Opt, unsigned Index, const char *Value0,
const Arg *BaseArg)
: Arg(JoinedClass, Opt, Index, BaseArg), Offset(_Offset) {
: Arg(JoinedClass, Opt, Index, BaseArg) {
getValues().push_back(Value0);
}
void JoinedArg::render(const ArgList &Args, ArgStringList &Output) const {
@ -116,11 +114,6 @@ void JoinedArg::render(const ArgList &Args, ArgStringList &Output) const {
}
}
const char *JoinedArg::getValue(const ArgList &Args, unsigned N) const {
assert(N < getNumValues() && "Invalid index.");
return Args.getArgString(getIndex()) + Offset;
}
CommaJoinedArg::CommaJoinedArg(const Option *Opt, unsigned Index,
const char *Str, const Arg *BaseArg)
: Arg(CommaJoinedClass, Opt, Index, BaseArg) {
@ -128,30 +121,32 @@ CommaJoinedArg::CommaJoinedArg(const Option *Opt, unsigned Index,
for (;; ++Str) {
char c = *Str;
if (!c) {
if (Prev != Str)
Values.push_back(std::string(Prev, Str));
break;
} else if (c == ',') {
if (Prev != Str)
Values.push_back(std::string(Prev, Str));
if (!c || c == ',') {
if (Prev != Str) {
char *Value = new char[Str - Prev + 1];
memcpy(Value, Prev, Str - Prev);
Value[Str - Prev] = '\0';
getValues().push_back(Value);
}
if (!c)
break;
Prev = Str + 1;
}
}
setOwnsValues(true);
}
void CommaJoinedArg::render(const ArgList &Args, ArgStringList &Output) const {
Output.push_back(Args.getArgString(getIndex()));
}
const char *CommaJoinedArg::getValue(const ArgList &Args, unsigned N) const {
assert(N < getNumValues() && "Invalid index.");
return Values[N].c_str();
}
SeparateArg::SeparateArg(const Option *Opt, unsigned Index, unsigned _NumValues,
SeparateArg::SeparateArg(const Option *Opt, unsigned Index, const char *Value0,
const Arg *BaseArg)
: Arg(SeparateClass, Opt, Index, BaseArg), NumValues(_NumValues) {
: Arg(SeparateClass, Opt, Index, BaseArg) {
getValues().push_back(Value0);
}
void SeparateArg::render(const ArgList &Args, ArgStringList &Output) const {
@ -161,32 +156,23 @@ void SeparateArg::render(const ArgList &Args, ArgStringList &Output) const {
getValue(Args, 0)));
} else {
Output.push_back(getOption().getName());
for (unsigned i = 0; i < NumValues; ++i)
for (unsigned i = 0; i != getNumValues(); ++i)
Output.push_back(getValue(Args, i));
}
}
const char *SeparateArg::getValue(const ArgList &Args, unsigned N) const {
assert(N < getNumValues() && "Invalid index.");
return Args.getArgString(getIndex() + 1 + N);
}
JoinedAndSeparateArg::JoinedAndSeparateArg(const Option *Opt, unsigned Index,
unsigned _Offset, const Arg *BaseArg)
: Arg(JoinedAndSeparateClass, Opt, Index, BaseArg), Offset(_Offset) {
const char *Value0,
const char *Value1,
const Arg *BaseArg)
: Arg(JoinedAndSeparateClass, Opt, Index, BaseArg) {
getValues().push_back(Value0);
getValues().push_back(Value1);
}
void JoinedAndSeparateArg::render(const ArgList &Args,
ArgStringList &Output) const {
Output.push_back(Args.GetOrMakeJoinedArgString(
getIndex(), getOption().getName(), getValue(Args, 0)));
Output.push_back(Args.getArgString(getIndex() + 1));
}
const char *JoinedAndSeparateArg::getValue(const ArgList &Args,
unsigned N) const {
assert(N < getNumValues() && "Invalid index.");
if (N == 0)
return Args.getArgString(getIndex()) + Offset;
return Args.getArgString(getIndex() + 1);
Output.push_back(getValue(Args, 1));
}

Просмотреть файл

@ -264,23 +264,26 @@ Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
llvm::StringRef Value) const {
Arg *A = new PositionalArg(Opt, BaseArgs.MakeIndex(Value), BaseArg);
unsigned Index = BaseArgs.MakeIndex(Value);
Arg *A = new PositionalArg(Opt, Index, BaseArgs.getArgString(Index), BaseArg);
SynthesizedArgs.push_back(A);
return A;
}
Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
llvm::StringRef Value) const {
Arg *A = new SeparateArg(Opt, BaseArgs.MakeIndex(Opt->getName(), Value), 1,
BaseArg);
unsigned Index = BaseArgs.MakeIndex(Opt->getName(), Value);
Arg *A = new SeparateArg(Opt, Index, BaseArgs.getArgString(Index), BaseArg);
SynthesizedArgs.push_back(A);
return A;
}
Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
llvm::StringRef Value) const {
Arg *A = new JoinedArg(Opt, BaseArgs.MakeIndex(Opt->getName() + Value.str()),
strlen(Opt->getName()), BaseArg);
unsigned Index = BaseArgs.MakeIndex(Opt->getName() + Value.str());
Arg *A = new JoinedArg(Opt, Index,
BaseArgs.getArgString(Index) + strlen(Opt->getName()),
BaseArg);
SynthesizedArgs.push_back(A);
return A;
}

Просмотреть файл

@ -188,7 +188,7 @@ Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const {
// Anything that doesn't start with '-' is an input, as is '-' itself.
if (Str[0] != '-' || Str[1] == '\0')
return new PositionalArg(TheInputOption, Index++);
return new PositionalArg(TheInputOption, Index++, Str);
const Info *Start = OptionInfos + FirstSearchableIndex;
const Info *End = OptionInfos + getNumOptions();
@ -221,7 +221,7 @@ Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const {
return 0;
}
return new PositionalArg(TheUnknownOption, Index++);
return new PositionalArg(TheUnknownOption, Index++, Str);
}
InputArgList *OptTable::ParseArgs(const char **ArgBegin, const char **ArgEnd,

Просмотреть файл

@ -133,7 +133,8 @@ JoinedOption::JoinedOption(OptSpecifier ID, const char *Name,
Arg *JoinedOption::accept(const InputArgList &Args, unsigned &Index) const {
// Always matches.
return new JoinedArg(this, Index++, strlen(getName()));
const char *Value = Args.getArgString(Index) + strlen(getName());
return new JoinedArg(this, Index++, Value);
}
CommaJoinedOption::CommaJoinedOption(OptSpecifier ID, const char *Name,
@ -168,7 +169,7 @@ Arg *SeparateOption::accept(const InputArgList &Args, unsigned &Index) const {
if (Index > Args.getNumInputArgStrings())
return 0;
return new SeparateArg(this, Index - 2, 1);
return new SeparateArg(this, Index - 2, Args.getArgString(Index - 1));
}
MultiArgOption::MultiArgOption(OptSpecifier ID, const char *Name,
@ -188,7 +189,11 @@ Arg *MultiArgOption::accept(const InputArgList &Args, unsigned &Index) const {
if (Index > Args.getNumInputArgStrings())
return 0;
return new SeparateArg(this, Index - 1 - NumArgs, NumArgs);
Arg *A = new SeparateArg(this, Index - 1 - NumArgs,
Args.getArgString(Index - NumArgs));
for (unsigned i = 1; i != NumArgs; ++i)
A->getValues().push_back(Args.getArgString(Index - NumArgs + i));
return A;
}
JoinedOrSeparateOption::JoinedOrSeparateOption(OptSpecifier ID,
@ -202,15 +207,18 @@ Arg *JoinedOrSeparateOption::accept(const InputArgList &Args,
unsigned &Index) const {
// If this is not an exact match, it is a joined arg.
// FIXME: Avoid strlen.
if (strlen(getName()) != strlen(Args.getArgString(Index)))
return new JoinedArg(this, Index++, strlen(getName()));
if (strlen(getName()) != strlen(Args.getArgString(Index))) {
const char *Value = Args.getArgString(Index) + strlen(getName());
return new JoinedArg(this, Index++, Value);
}
// Otherwise it must be separate.
Index += 2;
if (Index > Args.getNumInputArgStrings())
return 0;
return new SeparateArg(this, Index - 2, 1);
return new SeparateArg(this, Index - 2,
Args.getArgString(Index - 1));
}
JoinedAndSeparateOption::JoinedAndSeparateOption(OptSpecifier ID,
@ -228,6 +236,7 @@ Arg *JoinedAndSeparateOption::accept(const InputArgList &Args,
if (Index > Args.getNumInputArgStrings())
return 0;
return new JoinedAndSeparateArg(this, Index - 2, strlen(getName()));
return new JoinedAndSeparateArg(this, Index - 2,
Args.getArgString(Index-2)+strlen(getName()),
Args.getArgString(Index-1));
}