diff options
author | Daniel Dunbar <daniel@zuster.org> | 2010-06-09 22:31:00 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2010-06-09 22:31:00 +0000 |
commit | 4465a776a56de81211ae4672e5782c6bef075135 (patch) | |
tree | 8d13f1e726299f975fdd658cbe3b80d67e0eef7c | |
parent | bfbb39deabba4f7b8c89d69a28653074c8936086 (diff) |
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
-rw-r--r-- | include/clang/Driver/Arg.h | 68 | ||||
-rw-r--r-- | lib/Driver/Arg.cpp | 88 | ||||
-rw-r--r-- | lib/Driver/ArgList.cpp | 13 | ||||
-rw-r--r-- | lib/Driver/OptTable.cpp | 4 | ||||
-rw-r--r-- | lib/Driver/Option.cpp | 25 |
5 files changed, 91 insertions, 107 deletions
diff --git a/include/clang/Driver/Arg.h b/include/clang/Driver/Arg.h index 9415bae8e5..6b07db9ffe 100644 --- a/include/clang/Driver/Arg.h +++ b/include/clang/Driver/Arg.h @@ -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; } diff --git a/lib/Driver/Arg.cpp b/lib/Driver/Arg.cpp index 9b02378d15..24de51f615 100644 --- a/lib/Driver/Arg.cpp +++ b/lib/Driver/Arg.cpp @@ -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)); } diff --git a/lib/Driver/ArgList.cpp b/lib/Driver/ArgList.cpp index ad067f108e..9cf2eaf7c9 100644 --- a/lib/Driver/ArgList.cpp +++ b/lib/Driver/ArgList.cpp @@ -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; } diff --git a/lib/Driver/OptTable.cpp b/lib/Driver/OptTable.cpp index de1e4592b3..1f913e34ba 100644 --- a/lib/Driver/OptTable.cpp +++ b/lib/Driver/OptTable.cpp @@ -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, diff --git a/lib/Driver/Option.cpp b/lib/Driver/Option.cpp index 5a967ea3df..ea3b3bc418 100644 --- a/lib/Driver/Option.cpp +++ b/lib/Driver/Option.cpp @@ -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)); } - |