From bbf842bb27c6e964359111b6a72a5f948c56fc41 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Wed, 4 Mar 2009 23:22:02 +0000 Subject: [PATCH] Driver: Implement Option::accept methods. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66106 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Driver/Arg.cpp | 4 +-- lib/Driver/OptTable.cpp | 2 +- lib/Driver/Option.cpp | 72 +++++++++++++++++++++++++++++++++-------- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/lib/Driver/Arg.cpp b/lib/Driver/Arg.cpp index 9f0ed7245d..a4581d0e9e 100644 --- a/lib/Driver/Arg.cpp +++ b/lib/Driver/Arg.cpp @@ -41,8 +41,8 @@ void Arg::dump() const { llvm::errs() << " Index:" << Index; - if (const CommaJoinedArg *CJA = dyn_cast(this)) - llvm::errs() << " NumValues:" << CJA->getNumValues(); + if (isa(this) || isa(this)) + llvm::errs() << " NumValues:" << getNumValues(); llvm::errs() << ">\n"; diff --git a/lib/Driver/OptTable.cpp b/lib/Driver/OptTable.cpp index 5de71d7633..3308bc51a9 100644 --- a/lib/Driver/OptTable.cpp +++ b/lib/Driver/OptTable.cpp @@ -126,7 +126,7 @@ Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index, if (Str[0] != '-') return new PositionalArg(getOption(InputOpt), Index++); - for (unsigned j = UnknownOpt + 1; j < getNumOptions(); ++j) { + for (unsigned j = UnknownOpt + 1; j < LastOption; ++j) { const char *OptName = getOptionName((options::ID) j); // Arguments are only accepted by options which prefix them. diff --git a/lib/Driver/Option.cpp b/lib/Driver/Option.cpp index 583650f63b..43d7e9e476 100644 --- a/lib/Driver/Option.cpp +++ b/lib/Driver/Option.cpp @@ -8,8 +8,12 @@ //===----------------------------------------------------------------------===// #include "clang/Driver/Option.h" + +#include "clang/Driver/Arg.h" +#include "clang/Driver/ArgList.h" #include "llvm/Support/raw_ostream.h" #include +#include using namespace clang; using namespace clang::driver; @@ -65,6 +69,8 @@ void Option::dump() const { llvm::errs() << " NumArgs:" << MOA->getNumArgs(); llvm::errs() << ">\n"; + + llvm::errs().flush(); // FIXME } bool Option::matches(const Option *Opt) const { @@ -116,8 +122,12 @@ FlagOption::FlagOption(options::ID ID, const char *Name, } Arg *FlagOption::accept(const ArgList &Args, unsigned &Index) const { - assert(0 && "FIXME"); - return 0; + // Matches iff this is an exact match. + // FIXME: Avoid strlen. + if (strlen(getName()) != strlen(Args.getArgString(Index))) + return 0; + + return new PositionalArg(this, Index++); } JoinedOption::JoinedOption(options::ID ID, const char *Name, @@ -126,8 +136,8 @@ JoinedOption::JoinedOption(options::ID ID, const char *Name, } Arg *JoinedOption::accept(const ArgList &Args, unsigned &Index) const { - assert(0 && "FIXME"); - return 0; + // Always matches. + return new JoinedArg(this, Index++); } CommaJoinedOption::CommaJoinedOption(options::ID ID, const char *Name, @@ -137,8 +147,20 @@ CommaJoinedOption::CommaJoinedOption(options::ID ID, const char *Name, } Arg *CommaJoinedOption::accept(const ArgList &Args, unsigned &Index) const { - assert(0 && "FIXME"); - return 0; + // Always matches. We count the commas now so we can answer + // getNumValues easily. + + // Get the suffix string. + // FIXME: Avoid strlen, and move to helper method? + const char *Suffix = Args.getArgString(Index) + strlen(getName()); + const char *SuffixEnd = Suffix + strlen(Suffix); + + // Degenerate case, exact match has no values. + if (Suffix == SuffixEnd) + return new CommaJoinedArg(this, Index++, 0); + + return new CommaJoinedArg(this, Index++, + std::count(Suffix, SuffixEnd, ',') + 1); } SeparateOption::SeparateOption(options::ID ID, const char *Name, @@ -147,8 +169,14 @@ SeparateOption::SeparateOption(options::ID ID, const char *Name, } Arg *SeparateOption::accept(const ArgList &Args, unsigned &Index) const { - assert(0 && "FIXME"); - return 0; + // Matches iff this is an exact match. + // FIXME: Avoid strlen. + if (strlen(getName()) != strlen(Args.getArgString(Index))) + return 0; + + // FIXME: Missing argument error. + Index += 2; + return new SeparateArg(this, Index - 2, 1); } MultiArgOption::MultiArgOption(options::ID ID, const char *Name, @@ -158,8 +186,14 @@ MultiArgOption::MultiArgOption(options::ID ID, const char *Name, } Arg *MultiArgOption::accept(const ArgList &Args, unsigned &Index) const { - assert(0 && "FIXME"); - return 0; + // Matches iff this is an exact match. + // FIXME: Avoid strlen. + if (strlen(getName()) != strlen(Args.getArgString(Index))) + return 0; + + // FIXME: Missing argument error. + Index += 1 + NumArgs; + return new SeparateArg(this, Index - 1 - NumArgs, NumArgs); } JoinedOrSeparateOption::JoinedOrSeparateOption(options::ID ID, const char *Name, @@ -169,8 +203,15 @@ JoinedOrSeparateOption::JoinedOrSeparateOption(options::ID ID, const char *Name, } Arg *JoinedOrSeparateOption::accept(const ArgList &Args, unsigned &Index) const { - assert(0 && "FIXME"); - return 0; + // 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++); + + // Otherwise it must be separate. + // FIXME: Missing argument error. + Index += 2; + return new SeparateArg(this, Index - 2, 1); } JoinedAndSeparateOption::JoinedAndSeparateOption(options::ID ID, @@ -181,7 +222,10 @@ JoinedAndSeparateOption::JoinedAndSeparateOption(options::ID ID, } Arg *JoinedAndSeparateOption::accept(const ArgList &Args, unsigned &Index) const { - assert(0 && "FIXME"); - return 0; + // Always matches. + + // FIXME: Missing argument error. + Index += 2; + return new JoinedAndSeparateArg(this, Index - 2); } -- 2.40.0