2017-06-16 08:43:26 +08:00
|
|
|
//===- Option.cpp - Abstract Driver Options -------------------------------===//
|
2012-12-05 08:29:32 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-16 08:43:26 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2018-04-30 22:59:11 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
2017-06-16 08:43:26 +08:00
|
|
|
#include "llvm/Option/Option.h"
|
|
|
|
#include "llvm/Option/OptTable.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2015-12-19 02:55:26 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-01-02 18:22:59 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#include <cassert>
|
2017-06-16 08:43:26 +08:00
|
|
|
#include <cstring>
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::opt;
|
|
|
|
|
|
|
|
Option::Option(const OptTable::Info *info, const OptTable *owner)
|
|
|
|
: Info(info), Owner(owner) {
|
2013-07-23 00:18:13 +08:00
|
|
|
// Multi-level aliases are not supported. This just simplifies option
|
|
|
|
// tracking, it is not an inherent limitation.
|
2013-07-23 05:29:28 +08:00
|
|
|
assert((!Info || !getAlias().isValid() || !getAlias().getAlias().isValid()) &&
|
2013-07-23 00:18:13 +08:00
|
|
|
"Multi-level aliases are not supported.");
|
2013-08-01 06:44:41 +08:00
|
|
|
|
|
|
|
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.");
|
|
|
|
}
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
2015-12-19 02:55:26 +08:00
|
|
|
void Option::print(raw_ostream &O) const {
|
|
|
|
O << "<";
|
2012-12-05 08:29:32 +08:00
|
|
|
switch (getKind()) {
|
2015-12-19 02:55:26 +08:00
|
|
|
#define P(N) case N: O << #N; break
|
2012-12-05 08:29:32 +08:00
|
|
|
P(GroupClass);
|
|
|
|
P(InputClass);
|
|
|
|
P(UnknownClass);
|
|
|
|
P(FlagClass);
|
|
|
|
P(JoinedClass);
|
2017-06-21 00:31:31 +08:00
|
|
|
P(ValuesClass);
|
2012-12-05 08:29:32 +08:00
|
|
|
P(SeparateClass);
|
|
|
|
P(CommaJoinedClass);
|
|
|
|
P(MultiArgClass);
|
|
|
|
P(JoinedOrSeparateClass);
|
|
|
|
P(JoinedAndSeparateClass);
|
2013-08-14 05:09:50 +08:00
|
|
|
P(RemainingArgsClass);
|
2016-04-15 08:23:30 +08:00
|
|
|
P(RemainingArgsJoinedClass);
|
2012-12-05 08:29:32 +08:00
|
|
|
#undef P
|
|
|
|
}
|
|
|
|
|
2013-06-27 06:43:37 +08:00
|
|
|
if (Info->Prefixes) {
|
2015-12-19 02:55:26 +08:00
|
|
|
O << " Prefixes:[";
|
|
|
|
for (const char *const *Pre = Info->Prefixes; *Pre != nullptr; ++Pre) {
|
|
|
|
O << '"' << *Pre << (*(Pre + 1) == nullptr ? "\"" : "\", ");
|
2013-06-27 06:43:37 +08:00
|
|
|
}
|
2015-12-19 02:55:26 +08:00
|
|
|
O << ']';
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
2015-12-19 02:55:26 +08:00
|
|
|
O << " Name:\"" << getName() << '"';
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
const Option Group = getGroup();
|
|
|
|
if (Group.isValid()) {
|
2015-12-19 02:55:26 +08:00
|
|
|
O << " Group:";
|
|
|
|
Group.print(O);
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Option Alias = getAlias();
|
|
|
|
if (Alias.isValid()) {
|
2015-12-19 02:55:26 +08:00
|
|
|
O << " Alias:";
|
|
|
|
Alias.print(O);
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (getKind() == MultiArgClass)
|
2015-12-19 02:55:26 +08:00
|
|
|
O << " NumArgs:" << getNumArgs();
|
2012-12-05 08:29:32 +08:00
|
|
|
|
2015-12-19 02:55:26 +08:00
|
|
|
O << ">\n";
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:32:27 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-01-30 04:50:44 +08:00
|
|
|
LLVM_DUMP_METHOD void Option::dump() const { print(dbgs()); }
|
2017-01-28 10:02:38 +08:00
|
|
|
#endif
|
2015-12-19 02:55:26 +08:00
|
|
|
|
2012-12-05 08:29:32 +08:00
|
|
|
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-08-01 06:44:41 +08:00
|
|
|
case FlagClass: {
|
2012-12-05 08:29:32 +08:00
|
|
|
if (ArgSize != strlen(Args.getArgString(Index)))
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
|
2013-08-01 06:44:41 +08: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;
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 02:00:13 +08:00
|
|
|
|
|
|
|
if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs())
|
|
|
|
// A Flag alias for a Joined option must provide an argument.
|
|
|
|
A->getValues().push_back("");
|
|
|
|
|
2013-08-01 06:44:41 +08:00
|
|
|
return A;
|
|
|
|
}
|
2012-12-05 08:29:32 +08: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-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
Index += 2;
|
2014-08-23 03:29:17 +08:00
|
|
|
if (Index > Args.getNumInputArgStrings() ||
|
|
|
|
Args.getArgString(Index - 1) == nullptr)
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08: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-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
Index += 1 + getNumArgs();
|
|
|
|
if (Index > Args.getNumInputArgStrings())
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08: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;
|
2014-08-23 03:29:17 +08:00
|
|
|
if (Index > Args.getNumInputArgStrings() ||
|
|
|
|
Args.getArgString(Index - 1) == nullptr)
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
return new Arg(UnaliasedOption, Spelling,
|
|
|
|
Index - 2, Args.getArgString(Index - 1));
|
|
|
|
}
|
|
|
|
case JoinedAndSeparateClass:
|
|
|
|
// Always matches.
|
|
|
|
Index += 2;
|
2014-08-23 03:29:17 +08:00
|
|
|
if (Index > Args.getNumInputArgStrings() ||
|
|
|
|
Args.getArgString(Index - 1) == nullptr)
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
return new Arg(UnaliasedOption, Spelling, Index - 2,
|
|
|
|
Args.getArgString(Index - 2) + ArgSize,
|
|
|
|
Args.getArgString(Index - 1));
|
2013-08-14 05:09:50 +08:00
|
|
|
case RemainingArgsClass: {
|
|
|
|
// Matches iff this is an exact match.
|
|
|
|
// FIXME: Avoid strlen.
|
|
|
|
if (ArgSize != strlen(Args.getArgString(Index)))
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2013-08-14 05:09:50 +08:00
|
|
|
Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
|
2014-08-23 03:29:17 +08:00
|
|
|
while (Index < Args.getNumInputArgStrings() &&
|
|
|
|
Args.getArgString(Index) != nullptr)
|
2013-08-14 05:09:50 +08:00
|
|
|
A->getValues().push_back(Args.getArgString(Index++));
|
|
|
|
return A;
|
|
|
|
}
|
2016-04-15 08:23:30 +08:00
|
|
|
case RemainingArgsJoinedClass: {
|
|
|
|
Arg *A = new Arg(UnaliasedOption, Spelling, Index);
|
|
|
|
if (ArgSize != strlen(Args.getArgString(Index))) {
|
|
|
|
// An inexact match means there is a joined arg.
|
|
|
|
A->getValues().push_back(Args.getArgString(Index) + ArgSize);
|
|
|
|
}
|
|
|
|
Index++;
|
|
|
|
while (Index < Args.getNumInputArgStrings() &&
|
|
|
|
Args.getArgString(Index) != nullptr)
|
|
|
|
A->getValues().push_back(Args.getArgString(Index++));
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
|
2012-12-05 08:29:32 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid option kind!");
|
|
|
|
}
|
|
|
|
}
|