#ifndef CLANG_DRIVER_ARG_H_
#define CLANG_DRIVER_ARG_H_
-#include "llvm/Support/Casting.h"
-using llvm::isa;
-using llvm::cast;
-using llvm::cast_or_null;
-using llvm::dyn_cast;
-using llvm::dyn_cast_or_null;
-
#include "Util.h"
#include "llvm/ADT/SmallVector.h"
#include <vector>
/// ArgList to provide efficient iteration over all instances of a
/// particular option.
class Arg {
- public:
- enum ArgClass {
- FlagClass = 0,
- PositionalClass,
- JoinedClass,
- SeparateClass,
- CommaJoinedClass,
- JoinedAndSeparateClass
- };
+ Arg(const Arg &); // DO NOT IMPLEMENT
+ void operator=(const Arg &); // DO NOT IMPLEMENT
private:
- ArgClass Kind;
-
/// The option this argument is an instance of.
const Option *Opt;
/// The argument values, as C strings.
llvm::SmallVector<const char *, 2> Values;
- protected:
- Arg(ArgClass Kind, const Option *Opt, unsigned Index,
- const Arg *BaseArg = 0);
-
public:
- Arg(const Arg &);
- virtual ~Arg();
+ Arg(const Option *Opt, unsigned Index, const Arg *BaseArg = 0);
+ Arg(const Option *Opt, unsigned Index,
+ const char *Value0, const Arg *BaseArg = 0);
+ Arg(const Option *Opt, unsigned Index,
+ const char *Value0, const char *Value1, const Arg *BaseArg = 0);
+ ~Arg();
- ArgClass getKind() const { return Kind; }
const Option &getOption() const { return *Opt; }
unsigned getIndex() const { return Index; }
std::string getAsString(const ArgList &Args) const;
};
- /// FlagArg - An argument with no value.
- class FlagArg : public Arg {
- public:
- FlagArg(const Option *Opt, unsigned Index, const Arg *BaseArg = 0);
-
- static bool classof(const Arg *A) {
- return A->getKind() == Arg::FlagClass;
- }
- static bool classof(const FlagArg *) { return true; }
- };
-
- /// PositionalArg - A simple positional argument.
- class PositionalArg : public Arg {
- public:
- PositionalArg(const Option *Opt, unsigned Index, const char *Value,
- const Arg *BaseArg = 0);
-
- static bool classof(const Arg *A) {
- return A->getKind() == Arg::PositionalClass;
- }
- static bool classof(const PositionalArg *) { return true; }
- };
-
- /// JoinedArg - A single value argument where the value is joined
- /// (suffixed) to the option.
- class JoinedArg : public Arg {
- public:
- JoinedArg(const Option *Opt, unsigned Index, const char *Value,
- const Arg *BaseArg = 0);
-
- static bool classof(const Arg *A) {
- return A->getKind() == Arg::JoinedClass;
- }
- static bool classof(const JoinedArg *) { return true; }
- };
-
- /// SeparateArg - An argument where one or more values follow the
- /// option specifier immediately in the argument vector.
- class SeparateArg : public Arg {
- public:
- SeparateArg(const Option *Opt, unsigned Index, const char *Value,
- const Arg *BaseArg = 0);
-
- static bool classof(const Arg *A) {
- return A->getKind() == Arg::SeparateClass;
- }
- static bool classof(const SeparateArg *) { return true; }
- };
-
- /// CommaJoinedArg - An argument with multiple values joined by
- /// commas and joined (suffixed) to the option specifier.
- ///
- /// The key point of this arg is that it renders its values into
- /// separate arguments, which allows it to be used as a generic
- /// mechanism for passing arguments through to tools.
- class CommaJoinedArg : public Arg {
- public:
- CommaJoinedArg(const Option *Opt, unsigned Index, const char *Str,
- const Arg *BaseArg = 0);
-
- static bool classof(const Arg *A) {
- return A->getKind() == Arg::CommaJoinedClass;
- }
- static bool classof(const CommaJoinedArg *) { return true; }
- };
-
- /// JoinedAndSeparateArg - An argument with both joined and separate
- /// values.
- class JoinedAndSeparateArg : public Arg {
- public:
- JoinedAndSeparateArg(const Option *Opt, unsigned Index,
- const char *Value0, const char *Value1,
- const Arg *BaseArg = 0);
-
- static bool classof(const Arg *A) {
- return A->getKind() == Arg::JoinedAndSeparateClass;
- }
- static bool classof(const JoinedAndSeparateArg *) { return true; }
- };
} // end namespace driver
} // end namespace clang
using namespace clang::driver;
-Arg::Arg(ArgClass _Kind, const Option *_Opt, unsigned _Index,
- const Arg *_BaseArg)
- : Kind(_Kind), Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
+Arg::Arg(const Option *_Opt, unsigned _Index, const Arg *_BaseArg)
+ : Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
Claimed(false), OwnsValues(false) {
}
+Arg::Arg(const Option *_Opt, unsigned _Index,
+ const char *Value0, const Arg *_BaseArg)
+ : Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
+ Claimed(false), OwnsValues(false) {
+ Values.push_back(Value0);
+}
+
+Arg::Arg(const Option *_Opt, unsigned _Index,
+ const char *Value0, const char *Value1, const Arg *_BaseArg)
+ : Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
+ Claimed(false), OwnsValues(false) {
+ Values.push_back(Value0);
+ Values.push_back(Value1);
+}
+
Arg::~Arg() {
if (OwnsValues) {
for (unsigned i = 0, e = Values.size(); i != e; ++i)
void Arg::dump() const {
llvm::errs() << "<";
- switch (Kind) {
- default:
- assert(0 && "Invalid kind");
-#define P(N) case N: llvm::errs() << #N; break
- P(FlagClass);
- P(PositionalClass);
- P(JoinedClass);
- P(SeparateClass);
- P(CommaJoinedClass);
- P(JoinedAndSeparateClass);
-#undef P
- }
llvm::errs() << " Opt:";
Opt->dump();
llvm::errs() << " Index:" << Index;
- if (isa<CommaJoinedArg>(this) || isa<SeparateArg>(this))
- llvm::errs() << " NumValues:" << getNumValues();
+ llvm::errs() << " Values: [";
+ for (unsigned i = 0, e = Values.size(); i != e; ++i) {
+ if (i) llvm::errs() << ", ";
+ llvm::errs() << "'" << Values[i] << "'";
+ }
- llvm::errs() << ">\n";
+ llvm::errs() << "]>\n";
}
std::string Arg::getAsString(const ArgList &Args) const {
break;
}
}
-
-FlagArg::FlagArg(const Option *Opt, unsigned Index, const Arg *BaseArg)
- : Arg(FlagClass, Opt, Index, BaseArg) {
-}
-
-PositionalArg::PositionalArg(const Option *Opt, unsigned Index,
- const char *Value0, const Arg *BaseArg)
- : Arg(PositionalClass, Opt, Index, BaseArg) {
- getValues().push_back(Value0);
-}
-
-JoinedArg::JoinedArg(const Option *Opt, unsigned Index, const char *Value0,
- const Arg *BaseArg)
- : Arg(JoinedClass, Opt, Index, BaseArg) {
- getValues().push_back(Value0);
-}
-
-CommaJoinedArg::CommaJoinedArg(const Option *Opt, unsigned Index,
- const char *Str, const Arg *BaseArg)
- : Arg(CommaJoinedClass, Opt, Index, BaseArg) {
- const char *Prev = Str;
- for (;; ++Str) {
- char c = *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);
-}
-
-SeparateArg::SeparateArg(const Option *Opt, unsigned Index, const char *Value0,
- const Arg *BaseArg)
- : Arg(SeparateClass, Opt, Index, BaseArg) {
- getValues().push_back(Value0);
-}
-
-JoinedAndSeparateArg::JoinedAndSeparateArg(const Option *Opt, unsigned Index,
- const char *Value0,
- const char *Value1,
- const Arg *BaseArg)
- : Arg(JoinedAndSeparateClass, Opt, Index, BaseArg) {
- getValues().push_back(Value0);
- getValues().push_back(Value1);
-}
}
Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
- Arg *A = new FlagArg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
+ Arg *A = new Arg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
SynthesizedArgs.push_back(A);
return A;
}
Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
llvm::StringRef Value) const {
unsigned Index = BaseArgs.MakeIndex(Value);
- Arg *A = new PositionalArg(Opt, Index, BaseArgs.getArgString(Index), BaseArg);
+ Arg *A = new Arg(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 {
unsigned Index = BaseArgs.MakeIndex(Opt->getName(), Value);
- Arg *A = new SeparateArg(Opt, Index, BaseArgs.getArgString(Index), BaseArg);
+ Arg *A = new Arg(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 {
unsigned Index = BaseArgs.MakeIndex(Opt->getName() + Value.str());
- Arg *A = new JoinedArg(Opt, Index,
- BaseArgs.getArgString(Index) + strlen(Opt->getName()),
- BaseArg);
+ Arg *A = new Arg(Opt, Index,
+ BaseArgs.getArgString(Index) + strlen(Opt->getName()),
+ BaseArg);
SynthesizedArgs.push_back(A);
return A;
}
// just using Args was better?
const Arg &Input = IA->getInputArg();
Input.claim();
- if (isa<PositionalArg>(Input)) {
+ if (Input.getOption().matches(options::OPT_INPUT)) {
const char *Name = Input.getValue(C.getArgs());
Result = InputInfo(Name, A->getType(), Name);
} else
// Anything that doesn't start with '-' is an input, as is '-' itself.
if (Str[0] != '-' || Str[1] == '\0')
- return new PositionalArg(TheInputOption, Index++, Str);
+ return new Arg(TheInputOption, Index++, Str);
const Info *Start = OptionInfos + FirstSearchableIndex;
const Info *End = OptionInfos + getNumOptions();
return 0;
}
- return new PositionalArg(TheUnknownOption, Index++, Str);
+ return new Arg(TheUnknownOption, Index++, Str);
}
InputArgList *OptTable::ParseArgs(const char **ArgBegin, const char **ArgEnd,
if (strlen(getName()) != strlen(Args.getArgString(Index)))
return 0;
- return new FlagArg(this, Index++);
+ return new Arg(this, Index++);
}
JoinedOption::JoinedOption(OptSpecifier ID, const char *Name,
Arg *JoinedOption::accept(const InputArgList &Args, unsigned &Index) const {
// Always matches.
const char *Value = Args.getArgString(Index) + strlen(getName());
- return new JoinedArg(this, Index++, Value);
+ return new Arg(this, Index++, Value);
}
CommaJoinedOption::CommaJoinedOption(OptSpecifier ID, const char *Name,
Arg *CommaJoinedOption::accept(const InputArgList &Args,
unsigned &Index) const {
- // Always matches. We count the commas now so we can answer
- // getNumValues easily.
+ // Always matches.
+ const char *Str = Args.getArgString(Index) + strlen(getName());
+ Arg *A = new Arg(this, Index++);
+
+ // Parse out the comma separated values.
+ const char *Prev = Str;
+ for (;; ++Str) {
+ char c = *Str;
+
+ if (!c || c == ',') {
+ if (Prev != Str) {
+ char *Value = new char[Str - Prev + 1];
+ memcpy(Value, Prev, Str - Prev);
+ Value[Str - Prev] = '\0';
+ A->getValues().push_back(Value);
+ }
+
+ if (!c)
+ break;
+
+ Prev = Str + 1;
+ }
+ }
+ A->setOwnsValues(true);
- // Get the suffix string.
- // FIXME: Avoid strlen, and move to helper method?
- const char *Suffix = Args.getArgString(Index) + strlen(getName());
- return new CommaJoinedArg(this, Index++, Suffix);
+ return A;
}
SeparateOption::SeparateOption(OptSpecifier ID, const char *Name,
if (Index > Args.getNumInputArgStrings())
return 0;
- return new SeparateArg(this, Index - 2, Args.getArgString(Index - 1));
+ return new Arg(this, Index - 2, Args.getArgString(Index - 1));
}
MultiArgOption::MultiArgOption(OptSpecifier ID, const char *Name,
if (Index > Args.getNumInputArgStrings())
return 0;
- Arg *A = new SeparateArg(this, Index - 1 - NumArgs,
- Args.getArgString(Index - NumArgs));
+ Arg *A = new Arg(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;
// FIXME: Avoid strlen.
if (strlen(getName()) != strlen(Args.getArgString(Index))) {
const char *Value = Args.getArgString(Index) + strlen(getName());
- return new JoinedArg(this, Index++, Value);
+ return new Arg(this, Index++, Value);
}
// Otherwise it must be separate.
if (Index > Args.getNumInputArgStrings())
return 0;
- return new SeparateArg(this, Index - 2,
- Args.getArgString(Index - 1));
+ return new Arg(this, Index - 2, Args.getArgString(Index - 1));
}
JoinedAndSeparateOption::JoinedAndSeparateOption(OptSpecifier ID,
if (Index > Args.getNumInputArgStrings())
return 0;
- return new JoinedAndSeparateArg(this, Index - 2,
- Args.getArgString(Index-2)+strlen(getName()),
- Args.getArgString(Index-1));
+ return new Arg(this, Index - 2, Args.getArgString(Index-2)+strlen(getName())
+ , Args.getArgString(Index-1));
}