2009-03-03 13:55:11 +08:00
|
|
|
//===--- Option.cpp - Abstract Driver Options ---------------------------*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Driver/Option.h"
|
2009-03-05 07:22:02 +08:00
|
|
|
|
|
|
|
#include "clang/Driver/Arg.h"
|
|
|
|
#include "clang/Driver/ArgList.h"
|
2009-03-04 16:33:23 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-03-03 13:55:11 +08:00
|
|
|
#include <cassert>
|
2009-03-05 07:22:02 +08:00
|
|
|
#include <algorithm>
|
2009-03-03 13:55:11 +08:00
|
|
|
using namespace clang::driver;
|
|
|
|
|
2009-03-05 05:53:04 +08:00
|
|
|
Option::Option(OptionClass _Kind, options::ID _ID, const char *_Name,
|
2009-03-04 16:33:23 +08:00
|
|
|
const OptionGroup *_Group, const Option *_Alias)
|
2009-03-05 05:53:04 +08:00
|
|
|
: Kind(_Kind), ID(_ID), Name(_Name), Group(_Group), Alias(_Alias),
|
2009-03-05 05:05:23 +08:00
|
|
|
Unsupported(false), LinkerInput(false), NoOptAsInput(false),
|
2009-03-18 16:01:15 +08:00
|
|
|
ForceSeparateRender(false), ForceJoinedRender(false),
|
|
|
|
ForwardToGCC(true)
|
2009-03-05 05:05:23 +08:00
|
|
|
{
|
2009-03-03 13:55:11 +08:00
|
|
|
|
|
|
|
// Multi-level aliases are not supported, and alias options cannot
|
|
|
|
// have groups. This just simplifies option tracking, it is not an
|
|
|
|
// inherent limitation.
|
|
|
|
assert((!Alias || (!Alias->Alias && !Group)) &&
|
|
|
|
"Multi-level aliases and aliases with groups are unsupported.");
|
|
|
|
}
|
|
|
|
|
2009-03-04 16:33:23 +08:00
|
|
|
Option::~Option() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Option::dump() const {
|
|
|
|
llvm::errs() << "<";
|
|
|
|
switch (Kind) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Invalid kind");
|
|
|
|
#define P(N) case N: llvm::errs() << #N; break
|
|
|
|
P(GroupClass);
|
|
|
|
P(InputClass);
|
|
|
|
P(UnknownClass);
|
|
|
|
P(FlagClass);
|
|
|
|
P(JoinedClass);
|
|
|
|
P(SeparateClass);
|
|
|
|
P(CommaJoinedClass);
|
|
|
|
P(MultiArgClass);
|
|
|
|
P(JoinedOrSeparateClass);
|
|
|
|
P(JoinedAndSeparateClass);
|
|
|
|
#undef P
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::errs() << " Name:\"" << Name << '"';
|
|
|
|
|
|
|
|
if (Group) {
|
|
|
|
llvm::errs() << " Group:";
|
|
|
|
Group->dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Alias) {
|
|
|
|
llvm::errs() << " Alias:";
|
|
|
|
Alias->dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const MultiArgOption *MOA = dyn_cast<MultiArgOption>(this))
|
|
|
|
llvm::errs() << " NumArgs:" << MOA->getNumArgs();
|
|
|
|
|
|
|
|
llvm::errs() << ">\n";
|
|
|
|
}
|
|
|
|
|
2009-03-03 13:55:11 +08:00
|
|
|
bool Option::matches(const Option *Opt) const {
|
|
|
|
// Aliases are never considered in matching.
|
|
|
|
if (Opt->getAlias())
|
|
|
|
return matches(Opt->getAlias());
|
|
|
|
if (Alias)
|
|
|
|
return Alias->matches(Opt);
|
|
|
|
|
|
|
|
if (this == Opt)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Group)
|
|
|
|
return Group->matches(Opt);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-12 09:34:20 +08:00
|
|
|
bool Option::matches(options::ID Id) const {
|
|
|
|
// FIXME: Decide what to do here; we should either pull out the
|
|
|
|
// handling of alias on the option for Id from the other matches, or
|
|
|
|
// find some other solution (which hopefully doesn't require using
|
|
|
|
// the option table).
|
|
|
|
if (Alias)
|
|
|
|
return Alias->matches(Id);
|
|
|
|
|
|
|
|
if (ID == Id)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Group)
|
|
|
|
return Group->matches(Id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-05 05:53:04 +08:00
|
|
|
OptionGroup::OptionGroup(options::ID ID, const char *Name,
|
|
|
|
const OptionGroup *Group)
|
|
|
|
: Option(Option::GroupClass, ID, Name, Group, 0) {
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:53:00 +08:00
|
|
|
Arg *OptionGroup::accept(const ArgList &Args, unsigned &Index) const {
|
2009-03-23 07:26:43 +08:00
|
|
|
assert(0 && "accept() should never be called on an OptionGroup");
|
2009-03-04 16:33:23 +08:00
|
|
|
return 0;
|
2009-03-03 13:55:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
InputOption::InputOption()
|
2009-03-12 11:42:54 +08:00
|
|
|
: Option(Option::InputClass, options::OPT_INPUT, "<input>", 0, 0) {
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:53:00 +08:00
|
|
|
Arg *InputOption::accept(const ArgList &Args, unsigned &Index) const {
|
2009-03-23 07:26:43 +08:00
|
|
|
assert(0 && "accept() should never be called on an InputOption");
|
2009-03-04 16:33:23 +08:00
|
|
|
return 0;
|
2009-03-03 13:55:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UnknownOption::UnknownOption()
|
2009-03-12 11:42:54 +08:00
|
|
|
: Option(Option::UnknownClass, options::OPT_UNKNOWN, "<unknown>", 0, 0) {
|
2009-03-03 13:55:11 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:53:00 +08:00
|
|
|
Arg *UnknownOption::accept(const ArgList &Args, unsigned &Index) const {
|
2009-03-23 07:26:43 +08:00
|
|
|
assert(0 && "accept() should never be called on an UnknownOption");
|
2009-03-04 16:33:23 +08:00
|
|
|
return 0;
|
2009-03-03 13:55:11 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 05:53:04 +08:00
|
|
|
FlagOption::FlagOption(options::ID ID, const char *Name,
|
|
|
|
const OptionGroup *Group, const Option *Alias)
|
|
|
|
: Option(Option::FlagClass, ID, Name, Group, Alias) {
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
2009-03-03 13:55:11 +08:00
|
|
|
|
2009-03-05 04:53:00 +08:00
|
|
|
Arg *FlagOption::accept(const ArgList &Args, unsigned &Index) const {
|
2009-03-05 07:22:02 +08:00
|
|
|
// Matches iff this is an exact match.
|
|
|
|
// FIXME: Avoid strlen.
|
|
|
|
if (strlen(getName()) != strlen(Args.getArgString(Index)))
|
|
|
|
return 0;
|
|
|
|
|
2009-03-05 14:38:47 +08:00
|
|
|
return new FlagArg(this, Index++);
|
2009-03-03 13:55:11 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 05:53:04 +08:00
|
|
|
JoinedOption::JoinedOption(options::ID ID, const char *Name,
|
|
|
|
const OptionGroup *Group, const Option *Alias)
|
|
|
|
: Option(Option::JoinedClass, ID, Name, Group, Alias) {
|
2009-03-03 13:55:11 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:53:00 +08:00
|
|
|
Arg *JoinedOption::accept(const ArgList &Args, unsigned &Index) const {
|
2009-03-05 07:22:02 +08:00
|
|
|
// Always matches.
|
|
|
|
return new JoinedArg(this, Index++);
|
2009-03-03 13:55:11 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 05:53:04 +08:00
|
|
|
CommaJoinedOption::CommaJoinedOption(options::ID ID, const char *Name,
|
|
|
|
const OptionGroup *Group,
|
2009-03-04 16:33:23 +08:00
|
|
|
const Option *Alias)
|
2009-03-05 05:53:04 +08:00
|
|
|
: Option(Option::CommaJoinedClass, ID, Name, Group, Alias) {
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:53:00 +08:00
|
|
|
Arg *CommaJoinedOption::accept(const ArgList &Args, unsigned &Index) const {
|
2009-03-05 07:22:02 +08:00
|
|
|
// 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());
|
2009-03-05 14:38:47 +08:00
|
|
|
return new CommaJoinedArg(this, Index++, Suffix);
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 05:53:04 +08:00
|
|
|
SeparateOption::SeparateOption(options::ID ID, const char *Name,
|
|
|
|
const OptionGroup *Group, const Option *Alias)
|
|
|
|
: Option(Option::SeparateClass, ID, Name, Group, Alias) {
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:53:00 +08:00
|
|
|
Arg *SeparateOption::accept(const ArgList &Args, unsigned &Index) const {
|
2009-03-05 07:22:02 +08:00
|
|
|
// Matches iff this is an exact match.
|
|
|
|
// FIXME: Avoid strlen.
|
|
|
|
if (strlen(getName()) != strlen(Args.getArgString(Index)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Index += 2;
|
2009-03-23 07:26:43 +08:00
|
|
|
if (Index > Args.getNumInputArgStrings())
|
|
|
|
return 0;
|
|
|
|
|
2009-03-05 07:22:02 +08:00
|
|
|
return new SeparateArg(this, Index - 2, 1);
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 05:53:04 +08:00
|
|
|
MultiArgOption::MultiArgOption(options::ID ID, const char *Name,
|
|
|
|
const OptionGroup *Group, const Option *Alias,
|
|
|
|
unsigned _NumArgs)
|
|
|
|
: Option(Option::MultiArgClass, ID, Name, Group, Alias), NumArgs(_NumArgs) {
|
2009-03-12 13:46:32 +08:00
|
|
|
assert(NumArgs > 1 && "Invalid MultiArgOption!");
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:53:00 +08:00
|
|
|
Arg *MultiArgOption::accept(const ArgList &Args, unsigned &Index) const {
|
2009-03-05 07:22:02 +08:00
|
|
|
// Matches iff this is an exact match.
|
|
|
|
// FIXME: Avoid strlen.
|
|
|
|
if (strlen(getName()) != strlen(Args.getArgString(Index)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Index += 1 + NumArgs;
|
2009-03-23 07:26:43 +08:00
|
|
|
if (Index > Args.getNumInputArgStrings())
|
|
|
|
return 0;
|
|
|
|
|
2009-03-05 07:22:02 +08:00
|
|
|
return new SeparateArg(this, Index - 1 - NumArgs, NumArgs);
|
2009-03-03 13:55:11 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 05:53:04 +08:00
|
|
|
JoinedOrSeparateOption::JoinedOrSeparateOption(options::ID ID, const char *Name,
|
2009-03-04 16:33:23 +08:00
|
|
|
const OptionGroup *Group,
|
|
|
|
const Option *Alias)
|
2009-03-05 05:53:04 +08:00
|
|
|
: Option(Option::JoinedOrSeparateClass, ID, Name, Group, Alias) {
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
2009-03-23 07:26:43 +08:00
|
|
|
Arg *JoinedOrSeparateOption::accept(const ArgList &Args,
|
|
|
|
unsigned &Index) const {
|
2009-03-05 07:22:02 +08:00
|
|
|
// 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.
|
|
|
|
Index += 2;
|
2009-03-23 07:26:43 +08:00
|
|
|
if (Index > Args.getNumInputArgStrings())
|
|
|
|
return 0;
|
|
|
|
|
2009-03-05 07:22:02 +08:00
|
|
|
return new SeparateArg(this, Index - 2, 1);
|
2009-03-03 13:55:11 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 05:53:04 +08:00
|
|
|
JoinedAndSeparateOption::JoinedAndSeparateOption(options::ID ID,
|
|
|
|
const char *Name,
|
2009-03-04 16:33:23 +08:00
|
|
|
const OptionGroup *Group,
|
|
|
|
const Option *Alias)
|
2009-03-05 05:53:04 +08:00
|
|
|
: Option(Option::JoinedAndSeparateClass, ID, Name, Group, Alias) {
|
2009-03-03 13:55:11 +08:00
|
|
|
}
|
|
|
|
|
2009-03-23 07:26:43 +08:00
|
|
|
Arg *JoinedAndSeparateOption::accept(const ArgList &Args,
|
|
|
|
unsigned &Index) const {
|
2009-03-05 07:22:02 +08:00
|
|
|
// Always matches.
|
|
|
|
|
|
|
|
Index += 2;
|
2009-03-23 07:26:43 +08:00
|
|
|
if (Index > Args.getNumInputArgStrings())
|
|
|
|
return 0;
|
|
|
|
|
2009-03-05 07:22:02 +08:00
|
|
|
return new JoinedAndSeparateArg(this, Index - 2);
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
2009-03-03 13:55:11 +08:00
|
|
|
|