2013-04-08 18:41:23 +00: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 "llvm/Option/Option.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::opt;
|
|
|
|
|
|
|
|
Option::Option(const OptTable::Info *info, const OptTable *owner)
|
|
|
|
: Info(info), Owner(owner) {
|
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
// Multi-level aliases are not supported. This just simplifies option
|
|
|
|
// tracking, it is not an inherent limitation.
|
|
|
|
assert((!Info || !getAlias().isValid() || !getAlias().getAlias().isValid()) &&
|
|
|
|
"Multi-level aliases are not supported.");
|
|
|
|
|
|
|
|
if (Info && getAliasArgs()) {
|
|
|
|
assert(getAlias().isValid() && "Only alias options can have alias args.");
|
|
|
|
assert(getKind() == FlagClass && "Only Flag aliases can have alias args.");
|
|
|
|
assert(getAlias().getKind() != FlagClass &&
|
|
|
|
"Cannot provide alias args to a flag option.");
|
|
|
|
}
|
2013-04-08 18:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Option::~Option() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Option::dump() const {
|
|
|
|
llvm::errs() << "<";
|
|
|
|
switch (getKind()) {
|
|
|
|
#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);
|
2013-12-22 00:04:03 +00:00
|
|
|
P(RemainingArgsClass);
|
2013-04-08 18:41:23 +00:00
|
|
|
#undef P
|
|
|
|
}
|
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
if (Info->Prefixes) {
|
|
|
|
llvm::errs() << " Prefixes:[";
|
2014-11-24 09:08:18 +00:00
|
|
|
for (const char * const *Pre = Info->Prefixes; *Pre != nullptr; ++Pre) {
|
|
|
|
llvm::errs() << '"' << *Pre << (*(Pre + 1) == nullptr ? "\"" : "\", ");
|
2013-12-22 00:04:03 +00:00
|
|
|
}
|
|
|
|
llvm::errs() << ']';
|
2013-04-08 18:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::errs() << " Name:\"" << getName() << '"';
|
|
|
|
|
|
|
|
const Option Group = getGroup();
|
|
|
|
if (Group.isValid()) {
|
|
|
|
llvm::errs() << " Group:";
|
|
|
|
Group.dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Option Alias = getAlias();
|
|
|
|
if (Alias.isValid()) {
|
|
|
|
llvm::errs() << " Alias:";
|
|
|
|
Alias.dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getKind() == MultiArgClass)
|
|
|
|
llvm::errs() << " NumArgs:" << getNumArgs();
|
|
|
|
|
|
|
|
llvm::errs() << ">\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Option::matches(OptSpecifier Opt) const {
|
|
|
|
// Aliases are never considered in matching, look through them.
|
|
|
|
const Option Alias = getAlias();
|
|
|
|
if (Alias.isValid())
|
|
|
|
return Alias.matches(Opt);
|
|
|
|
|
|
|
|
// Check exact match.
|
|
|
|
if (getID() == Opt.getID())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const Option Group = getGroup();
|
|
|
|
if (Group.isValid())
|
|
|
|
return Group.matches(Opt);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg *Option::accept(const ArgList &Args,
|
|
|
|
unsigned &Index,
|
|
|
|
unsigned ArgSize) const {
|
|
|
|
const Option &UnaliasedOption = getUnaliasedOption();
|
|
|
|
StringRef Spelling;
|
|
|
|
// If the option was an alias, get the spelling from the unaliased one.
|
|
|
|
if (getID() == UnaliasedOption.getID()) {
|
|
|
|
Spelling = StringRef(Args.getArgString(Index), ArgSize);
|
|
|
|
} else {
|
|
|
|
Spelling = Args.MakeArgString(Twine(UnaliasedOption.getPrefix()) +
|
|
|
|
Twine(UnaliasedOption.getName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (getKind()) {
|
2013-12-22 00:04:03 +00:00
|
|
|
case FlagClass: {
|
2013-04-08 18:41:23 +00:00
|
|
|
if (ArgSize != strlen(Args.getArgString(Index)))
|
2014-11-24 09:08:18 +00:00
|
|
|
return nullptr;
|
2013-04-08 18:41:23 +00:00
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
|
|
|
|
if (getAliasArgs()) {
|
|
|
|
const char *Val = getAliasArgs();
|
|
|
|
while (*Val != '\0') {
|
|
|
|
A->getValues().push_back(Val);
|
|
|
|
|
|
|
|
// Move past the '\0' to the next argument.
|
|
|
|
Val += strlen(Val) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return A;
|
|
|
|
}
|
2013-04-08 18:41:23 +00:00
|
|
|
case JoinedClass: {
|
|
|
|
const char *Value = Args.getArgString(Index) + ArgSize;
|
|
|
|
return new Arg(UnaliasedOption, Spelling, Index++, Value);
|
|
|
|
}
|
|
|
|
case CommaJoinedClass: {
|
|
|
|
// Always matches.
|
|
|
|
const char *Str = Args.getArgString(Index) + ArgSize;
|
|
|
|
Arg *A = new Arg(UnaliasedOption, Spelling, 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);
|
|
|
|
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
case SeparateClass:
|
|
|
|
// Matches iff this is an exact match.
|
|
|
|
// FIXME: Avoid strlen.
|
|
|
|
if (ArgSize != strlen(Args.getArgString(Index)))
|
2014-11-24 09:08:18 +00:00
|
|
|
return nullptr;
|
2013-04-08 18:41:23 +00:00
|
|
|
|
|
|
|
Index += 2;
|
|
|
|
if (Index > Args.getNumInputArgStrings())
|
2014-11-24 09:08:18 +00:00
|
|
|
return nullptr;
|
2013-04-08 18:41:23 +00:00
|
|
|
|
|
|
|
return new Arg(UnaliasedOption, Spelling,
|
|
|
|
Index - 2, Args.getArgString(Index - 1));
|
|
|
|
case MultiArgClass: {
|
|
|
|
// Matches iff this is an exact match.
|
|
|
|
// FIXME: Avoid strlen.
|
|
|
|
if (ArgSize != strlen(Args.getArgString(Index)))
|
2014-11-24 09:08:18 +00:00
|
|
|
return nullptr;
|
2013-04-08 18:41:23 +00:00
|
|
|
|
|
|
|
Index += 1 + getNumArgs();
|
|
|
|
if (Index > Args.getNumInputArgStrings())
|
2014-11-24 09:08:18 +00:00
|
|
|
return nullptr;
|
2013-04-08 18:41:23 +00:00
|
|
|
|
|
|
|
Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(),
|
|
|
|
Args.getArgString(Index - getNumArgs()));
|
|
|
|
for (unsigned i = 1; i != getNumArgs(); ++i)
|
|
|
|
A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
case JoinedOrSeparateClass: {
|
|
|
|
// If this is not an exact match, it is a joined arg.
|
|
|
|
// FIXME: Avoid strlen.
|
|
|
|
if (ArgSize != strlen(Args.getArgString(Index))) {
|
|
|
|
const char *Value = Args.getArgString(Index) + ArgSize;
|
|
|
|
return new Arg(*this, Spelling, Index++, Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise it must be separate.
|
|
|
|
Index += 2;
|
|
|
|
if (Index > Args.getNumInputArgStrings())
|
2014-11-24 09:08:18 +00:00
|
|
|
return nullptr;
|
2013-04-08 18:41:23 +00:00
|
|
|
|
|
|
|
return new Arg(UnaliasedOption, Spelling,
|
|
|
|
Index - 2, Args.getArgString(Index - 1));
|
|
|
|
}
|
|
|
|
case JoinedAndSeparateClass:
|
|
|
|
// Always matches.
|
|
|
|
Index += 2;
|
|
|
|
if (Index > Args.getNumInputArgStrings())
|
2014-11-24 09:08:18 +00:00
|
|
|
return nullptr;
|
2013-04-08 18:41:23 +00:00
|
|
|
|
|
|
|
return new Arg(UnaliasedOption, Spelling, Index - 2,
|
|
|
|
Args.getArgString(Index - 2) + ArgSize,
|
|
|
|
Args.getArgString(Index - 1));
|
2013-12-22 00:04:03 +00:00
|
|
|
case RemainingArgsClass: {
|
|
|
|
// Matches iff this is an exact match.
|
|
|
|
// FIXME: Avoid strlen.
|
|
|
|
if (ArgSize != strlen(Args.getArgString(Index)))
|
2014-11-24 09:08:18 +00:00
|
|
|
return nullptr;
|
2013-12-22 00:04:03 +00:00
|
|
|
Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
|
|
|
|
while (Index < Args.getNumInputArgStrings())
|
|
|
|
A->getValues().push_back(Args.getArgString(Index++));
|
|
|
|
return A;
|
|
|
|
}
|
2013-04-08 18:41:23 +00:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid option kind!");
|
|
|
|
}
|
|
|
|
}
|