[cfe-commits] r105760 - in /cfe/trunk: include/clang/Driver/Arg.h lib/Driver/Arg.cpp lib/Driver/ArgList.cpp lib/Driver/OptTable.cpp lib/Driver/Option.cpp
Daniel Dunbar
daniel at zuster.org
Wed Jun 9 15:31:00 PDT 2010
Author: ddunbar
Date: Wed Jun 9 17:31:00 2010
New Revision: 105760
URL: http://llvm.org/viewvc/llvm-project?rev=105760&view=rev
Log:
Driver: Change Arg to just hold the values directly, instead of implicitly
deriving them from the Arg type.
Modified:
cfe/trunk/include/clang/Driver/Arg.h
cfe/trunk/lib/Driver/Arg.cpp
cfe/trunk/lib/Driver/ArgList.cpp
cfe/trunk/lib/Driver/OptTable.cpp
cfe/trunk/lib/Driver/Option.cpp
Modified: cfe/trunk/include/clang/Driver/Arg.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Arg.h?rev=105760&r1=105759&r2=105760&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/Arg.h (original)
+++ cfe/trunk/include/clang/Driver/Arg.h Wed Jun 9 17:31:00 2010
@@ -18,6 +18,7 @@
using llvm::dyn_cast_or_null;
#include "Util.h"
+#include "llvm/ADT/SmallVector.h"
#include <vector>
#include <string>
@@ -58,10 +59,15 @@
/// 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 @@
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 @@
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 @@
/// 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 @@
/// 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 @@
/// 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 @@
/// 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 @@
/// 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;
}
Modified: cfe/trunk/lib/Driver/Arg.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Arg.cpp?rev=105760&r1=105759&r2=105760&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Arg.cpp (original)
+++ cfe/trunk/lib/Driver/Arg.cpp Wed Jun 9 17:31:00 2010
@@ -17,10 +17,16 @@
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 @@
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 @@
}
}
-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 @@
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 @@
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));
}
Modified: cfe/trunk/lib/Driver/ArgList.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ArgList.cpp?rev=105760&r1=105759&r2=105760&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ArgList.cpp (original)
+++ cfe/trunk/lib/Driver/ArgList.cpp Wed Jun 9 17:31:00 2010
@@ -264,23 +264,26 @@
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;
}
Modified: cfe/trunk/lib/Driver/OptTable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/OptTable.cpp?rev=105760&r1=105759&r2=105760&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/OptTable.cpp (original)
+++ cfe/trunk/lib/Driver/OptTable.cpp Wed Jun 9 17:31:00 2010
@@ -188,7 +188,7 @@
// 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 @@
return 0;
}
- return new PositionalArg(TheUnknownOption, Index++);
+ return new PositionalArg(TheUnknownOption, Index++, Str);
}
InputArgList *OptTable::ParseArgs(const char **ArgBegin, const char **ArgEnd,
Modified: cfe/trunk/lib/Driver/Option.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Option.cpp?rev=105760&r1=105759&r2=105760&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Option.cpp (original)
+++ cfe/trunk/lib/Driver/Option.cpp Wed Jun 9 17:31:00 2010
@@ -133,7 +133,8 @@
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 @@
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 @@
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 @@
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 @@
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));
}
-
More information about the cfe-commits
mailing list