mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-26 12:43:36 +01:00
Option parsing: support case-insensitive option matching.
Re-submitting r189416 with fix for Windows build on where strcasecmp is not defined. llvm-svn: 189501
This commit is contained in:
parent
a22a21165f
commit
3ab487af0b
@ -51,6 +51,7 @@ private:
|
|||||||
/// \brief The static option information table.
|
/// \brief The static option information table.
|
||||||
const Info *OptionInfos;
|
const Info *OptionInfos;
|
||||||
unsigned NumOptionInfos;
|
unsigned NumOptionInfos;
|
||||||
|
bool IgnoreCase;
|
||||||
|
|
||||||
unsigned TheInputOptionID;
|
unsigned TheInputOptionID;
|
||||||
unsigned TheUnknownOptionID;
|
unsigned TheUnknownOptionID;
|
||||||
@ -72,7 +73,8 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos);
|
OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos,
|
||||||
|
bool _IgnoreCase = false);
|
||||||
public:
|
public:
|
||||||
~OptTable();
|
~OptTable();
|
||||||
|
|
||||||
|
@ -14,26 +14,27 @@
|
|||||||
#include "llvm/Support/ErrorHandling.h"
|
#include "llvm/Support/ErrorHandling.h"
|
||||||
#include "llvm/Support/raw_ostream.h"
|
#include "llvm/Support/raw_ostream.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
using namespace llvm::opt;
|
using namespace llvm::opt;
|
||||||
|
|
||||||
// Ordering on Info. The ordering is *almost* lexicographic, with two
|
namespace llvm {
|
||||||
// exceptions. First, '\0' comes at the end of the alphabet instead of
|
namespace opt {
|
||||||
// the beginning (thus options precede any other options which prefix
|
|
||||||
// them). Second, for options with the same name, the less permissive
|
|
||||||
// version should come first; a Flag option should precede a Joined
|
|
||||||
// option, for example.
|
|
||||||
|
|
||||||
static int StrCmpOptionName(const char *A, const char *B) {
|
// Ordering on Info. The ordering is *almost* case-insensitive lexicographic,
|
||||||
char a = *A, b = *B;
|
// with an exceptions. '\0' comes at the end of the alphabet instead of the
|
||||||
|
// beginning (thus options precede any other options which prefix them).
|
||||||
|
static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) {
|
||||||
|
const char *X = A, *Y = B;
|
||||||
|
char a = tolower(*A), b = tolower(*B);
|
||||||
while (a == b) {
|
while (a == b) {
|
||||||
if (a == '\0')
|
if (a == '\0')
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
a = *++A;
|
a = tolower(*++X);
|
||||||
b = *++B;
|
b = tolower(*++Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a == '\0') // A is a prefix of B.
|
if (a == '\0') // A is a prefix of B.
|
||||||
@ -45,21 +46,24 @@ static int StrCmpOptionName(const char *A, const char *B) {
|
|||||||
return (a < b) ? -1 : 1;
|
return (a < b) ? -1 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace llvm {
|
static int StrCmpOptionName(const char *A, const char *B) {
|
||||||
namespace opt {
|
if (int N = StrCmpOptionNameIgnoreCase(A, B))
|
||||||
|
return N;
|
||||||
|
return strcmp(A, B);
|
||||||
|
}
|
||||||
|
|
||||||
static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
|
static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
|
||||||
if (&A == &B)
|
if (&A == &B)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (int N = StrCmpOptionName(A.Name, B.Name))
|
if (int N = StrCmpOptionName(A.Name, B.Name))
|
||||||
return N == -1;
|
return N < 0;
|
||||||
|
|
||||||
for (const char * const *APre = A.Prefixes,
|
for (const char * const *APre = A.Prefixes,
|
||||||
* const *BPre = B.Prefixes;
|
* const *BPre = B.Prefixes;
|
||||||
*APre != 0 && *BPre != 0; ++APre, ++BPre) {
|
*APre != 0 && *BPre != 0; ++APre, ++BPre) {
|
||||||
if (int N = StrCmpOptionName(*APre, *BPre))
|
if (int N = StrCmpOptionName(*APre, *BPre))
|
||||||
return N == -1;
|
return N < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Names are the same, check that classes are in order; exactly one
|
// Names are the same, check that classes are in order; exactly one
|
||||||
@ -71,19 +75,21 @@ static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
|
|||||||
|
|
||||||
// Support lower_bound between info and an option name.
|
// Support lower_bound between info and an option name.
|
||||||
static inline bool operator<(const OptTable::Info &I, const char *Name) {
|
static inline bool operator<(const OptTable::Info &I, const char *Name) {
|
||||||
return StrCmpOptionName(I.Name, Name) == -1;
|
return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0;
|
||||||
}
|
}
|
||||||
static inline bool operator<(const char *Name, const OptTable::Info &I) {
|
static inline bool operator<(const char *Name, const OptTable::Info &I) {
|
||||||
return StrCmpOptionName(Name, I.Name) == -1;
|
return StrCmpOptionNameIgnoreCase(Name, I.Name) < 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
|
OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
|
||||||
|
|
||||||
OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
|
OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos,
|
||||||
|
bool _IgnoreCase)
|
||||||
: OptionInfos(_OptionInfos),
|
: OptionInfos(_OptionInfos),
|
||||||
NumOptionInfos(_NumOptionInfos),
|
NumOptionInfos(_NumOptionInfos),
|
||||||
|
IgnoreCase(_IgnoreCase),
|
||||||
TheInputOptionID(0),
|
TheInputOptionID(0),
|
||||||
TheUnknownOptionID(0),
|
TheUnknownOptionID(0),
|
||||||
FirstSearchableIndex(0)
|
FirstSearchableIndex(0)
|
||||||
@ -170,12 +176,26 @@ static bool isInput(const llvm::StringSet<> &Prefixes, StringRef Arg) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns true if X starts with Y, ignoring case.
|
||||||
|
static bool startsWithIgnoreCase(StringRef X, StringRef Y) {
|
||||||
|
if (X.size() < Y.size())
|
||||||
|
return false;
|
||||||
|
return X.substr(0, Y.size()).equals_lower(Y);
|
||||||
|
}
|
||||||
|
|
||||||
/// \returns Matched size. 0 means no match.
|
/// \returns Matched size. 0 means no match.
|
||||||
static unsigned matchOption(const OptTable::Info *I, StringRef Str) {
|
static unsigned matchOption(const OptTable::Info *I, StringRef Str,
|
||||||
|
bool IgnoreCase) {
|
||||||
for (const char * const *Pre = I->Prefixes; *Pre != 0; ++Pre) {
|
for (const char * const *Pre = I->Prefixes; *Pre != 0; ++Pre) {
|
||||||
StringRef Prefix(*Pre);
|
StringRef Prefix(*Pre);
|
||||||
if (Str.startswith(Prefix) && Str.substr(Prefix.size()).startswith(I->Name))
|
if (Str.startswith(Prefix)) {
|
||||||
return Prefix.size() + StringRef(I->Name).size();
|
StringRef Rest = Str.substr(Prefix.size());
|
||||||
|
bool Matched = IgnoreCase
|
||||||
|
? startsWithIgnoreCase(Rest, I->Name)
|
||||||
|
: Rest.startswith(I->Name);
|
||||||
|
if (Matched)
|
||||||
|
return Prefix.size() + StringRef(I->Name).size();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -210,7 +230,7 @@ Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
|
|||||||
unsigned ArgSize = 0;
|
unsigned ArgSize = 0;
|
||||||
// Scan for first option which is a proper prefix.
|
// Scan for first option which is a proper prefix.
|
||||||
for (; Start != End; ++Start)
|
for (; Start != End; ++Start)
|
||||||
if ((ArgSize = matchOption(Start, Str)))
|
if ((ArgSize = matchOption(Start, Str, IgnoreCase)))
|
||||||
break;
|
break;
|
||||||
if (Start == End)
|
if (Start == End)
|
||||||
break;
|
break;
|
||||||
|
@ -20,7 +20,7 @@ using namespace llvm::opt;
|
|||||||
enum ID {
|
enum ID {
|
||||||
OPT_INVALID = 0, // This is not an option ID.
|
OPT_INVALID = 0, // This is not an option ID.
|
||||||
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
||||||
HELPTEXT, METAVAR) OPT_##ID,
|
HELPTEXT, METAVAR) OPT_##ID,
|
||||||
#include "Opts.inc"
|
#include "Opts.inc"
|
||||||
LastOption
|
LastOption
|
||||||
#undef OPTION
|
#undef OPTION
|
||||||
@ -48,8 +48,8 @@ static const OptTable::Info InfoTable[] = {
|
|||||||
namespace {
|
namespace {
|
||||||
class TestOptTable : public OptTable {
|
class TestOptTable : public OptTable {
|
||||||
public:
|
public:
|
||||||
TestOptTable()
|
TestOptTable(bool IgnoreCase = false)
|
||||||
: OptTable(InfoTable, array_lengthof(InfoTable)) {}
|
: OptTable(InfoTable, array_lengthof(InfoTable), IgnoreCase) {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,6 +157,26 @@ TEST(Option, AliasArgs) {
|
|||||||
EXPECT_EQ(AL->getAllArgValues(OPT_B)[1], "bar");
|
EXPECT_EQ(AL->getAllArgValues(OPT_B)[1], "bar");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(Option, IgnoreCase) {
|
||||||
|
TestOptTable T(true);
|
||||||
|
unsigned MAI, MAC;
|
||||||
|
|
||||||
|
const char *MyArgs[] = { "-a", "-joo" };
|
||||||
|
OwningPtr<InputArgList> AL(T.ParseArgs(MyArgs, array_endof(MyArgs), MAI, MAC));
|
||||||
|
EXPECT_TRUE(AL->hasArg(OPT_A));
|
||||||
|
EXPECT_TRUE(AL->hasArg(OPT_B));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Option, DoNotIgnoreCase) {
|
||||||
|
TestOptTable T;
|
||||||
|
unsigned MAI, MAC;
|
||||||
|
|
||||||
|
const char *MyArgs[] = { "-a", "-joo" };
|
||||||
|
OwningPtr<InputArgList> AL(T.ParseArgs(MyArgs, array_endof(MyArgs), MAI, MAC));
|
||||||
|
EXPECT_FALSE(AL->hasArg(OPT_A));
|
||||||
|
EXPECT_FALSE(AL->hasArg(OPT_B));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(Option, SlurpEmpty) {
|
TEST(Option, SlurpEmpty) {
|
||||||
TestOptTable T;
|
TestOptTable T;
|
||||||
unsigned MAI, MAC;
|
unsigned MAI, MAC;
|
||||||
|
@ -13,18 +13,23 @@
|
|||||||
#include "llvm/ADT/Twine.h"
|
#include "llvm/ADT/Twine.h"
|
||||||
#include "llvm/TableGen/Record.h"
|
#include "llvm/TableGen/Record.h"
|
||||||
#include "llvm/TableGen/TableGenBackend.h"
|
#include "llvm/TableGen/TableGenBackend.h"
|
||||||
|
#include <cstring>
|
||||||
|
#include <cctype>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
|
// Ordering on Info. The logic should match with the consumer-side function in
|
||||||
|
// llvm/Option/OptTable.h.
|
||||||
static int StrCmpOptionName(const char *A, const char *B) {
|
static int StrCmpOptionName(const char *A, const char *B) {
|
||||||
char a = *A, b = *B;
|
const char *X = A, *Y = B;
|
||||||
|
char a = tolower(*A), b = tolower(*B);
|
||||||
while (a == b) {
|
while (a == b) {
|
||||||
if (a == '\0')
|
if (a == '\0')
|
||||||
return 0;
|
return strcmp(A, B);
|
||||||
|
|
||||||
a = *++A;
|
a = tolower(*++X);
|
||||||
b = *++B;
|
b = tolower(*++Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a == '\0') // A is a prefix of B.
|
if (a == '\0') // A is a prefix of B.
|
||||||
@ -50,7 +55,7 @@ static int CompareOptionRecords(const void *Av, const void *Bv) {
|
|||||||
if (!ASent)
|
if (!ASent)
|
||||||
if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
|
if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
|
||||||
B->getValueAsString("Name").c_str()))
|
B->getValueAsString("Name").c_str()))
|
||||||
return Cmp;
|
return Cmp;
|
||||||
|
|
||||||
if (!ASent) {
|
if (!ASent) {
|
||||||
std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes");
|
std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes");
|
||||||
|
Loading…
Reference in New Issue
Block a user