X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FOption%2FOptTable.cpp;h=ef9a3e75a155d4f8b60dc1d06be843c9cdaf1c44;hb=0e9c68e6bc8768143308b0162e900ba8bd10dc01;hp=8f21a4ff24cf1ba17a3d05d75e404ac20f208657;hpb=1997734e37775a68182b3fd508a52e0c28ff36f8;p=oota-llvm.git diff --git a/lib/Option/OptTable.cpp b/lib/Option/OptTable.cpp index 8f21a4ff24c..ef9a3e75a15 100644 --- a/lib/Option/OptTable.cpp +++ b/lib/Option/OptTable.cpp @@ -14,26 +14,27 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/raw_ostream.h" #include +#include #include using namespace llvm; using namespace llvm::opt; -// Ordering on Info. The ordering is *almost* lexicographic, with two -// exceptions. First, '\0' comes at the end of the alphabet instead of -// 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. +namespace llvm { +namespace opt { -static int StrCmpOptionName(const char *A, const char *B) { - char a = *A, b = *B; +// Ordering on Info. The ordering is *almost* case-insensitive lexicographic, +// 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) { if (a == '\0') return 0; - a = *++A; - b = *++B; + a = tolower(*++X); + b = tolower(*++Y); } if (a == '\0') // A is a prefix of B. @@ -45,21 +46,25 @@ static int StrCmpOptionName(const char *A, const char *B) { return (a < b) ? -1 : 1; } -namespace llvm { -namespace opt { +#ifndef NDEBUG +static int StrCmpOptionName(const char *A, const char *B) { + if (int N = StrCmpOptionNameIgnoreCase(A, B)) + return N; + return strcmp(A, B); +} static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) { if (&A == &B) return false; if (int N = StrCmpOptionName(A.Name, B.Name)) - return N == -1; + return N < 0; for (const char * const *APre = A.Prefixes, * const *BPre = B.Prefixes; - *APre != 0 && *BPre != 0; ++APre, ++BPre) { + *APre != nullptr && *BPre != nullptr; ++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 @@ -68,22 +73,22 @@ static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) { "Unexpected classes for options with same name."); return B.Kind == Option::JoinedClass; } +#endif // Support lower_bound between info and an option name. static inline bool operator<(const OptTable::Info &I, const char *Name) { - return StrCmpOptionName(I.Name, Name) == -1; -} -static inline bool operator<(const char *Name, const OptTable::Info &I) { - return StrCmpOptionName(Name, I.Name) == -1; + return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0; } } } 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), NumOptionInfos(_NumOptionInfos), + IgnoreCase(_IgnoreCase), TheInputOptionID(0), TheUnknownOptionID(0), FirstSearchableIndex(0) @@ -131,7 +136,7 @@ OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos) for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions() + 1; i != e; ++i) { if (const char *const *P = getInfo(i).Prefixes) { - for (; *P != 0; ++P) { + for (; *P != nullptr; ++P) { PrefixesUnion.insert(*P); } } @@ -155,7 +160,7 @@ OptTable::~OptTable() { const Option OptTable::getOption(OptSpecifier Opt) const { unsigned id = Opt.getID(); if (id == 0) - return Option(0, 0); + return Option(nullptr, nullptr); assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID."); return Option(&getInfo(id), this); } @@ -171,11 +176,18 @@ static bool isInput(const llvm::StringSet<> &Prefixes, StringRef Arg) { } /// \returns Matched size. 0 means no match. -static unsigned matchOption(const OptTable::Info *I, StringRef Str) { - for (const char * const *Pre = I->Prefixes; *Pre != 0; ++Pre) { +static unsigned matchOption(const OptTable::Info *I, StringRef Str, + bool IgnoreCase) { + for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) { StringRef Prefix(*Pre); - if (Str.startswith(Prefix) && Str.substr(Prefix.size()).startswith(I->Name)) - return Prefix.size() + StringRef(I->Name).size(); + if (Str.startswith(Prefix)) { + StringRef Rest = Str.substr(Prefix.size()); + bool Matched = IgnoreCase + ? Rest.startswith_lower(I->Name) + : Rest.startswith(I->Name); + if (Matched) + return Prefix.size() + StringRef(I->Name).size(); + } } return 0; } @@ -210,7 +222,7 @@ Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index, unsigned ArgSize = 0; // Scan for first option which is a proper prefix. for (; Start != End; ++Start) - if ((ArgSize = matchOption(Start, Str))) + if ((ArgSize = matchOption(Start, Str, IgnoreCase))) break; if (Start == End) break; @@ -228,7 +240,7 @@ Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index, // Otherwise, see if this argument was missing values. if (Prev != Index) - return 0; + return nullptr; } // If we failed to find an option and this arg started with /, then it's @@ -288,7 +300,18 @@ static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) { llvm_unreachable("Invalid option with help text."); case Option::MultiArgClass: - llvm_unreachable("Cannot print metavar for this kind of option."); + if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) { + // For MultiArgs, metavar is full list of all argument names. + Name += ' '; + Name += MetaVarName; + } + else { + // For MultiArgs, if metavar not supplied, print N times. + for (unsigned i=0, e=O.getNumArgs(); i< e; ++i) { + Name += " "; + } + } + break; case Option::FlagClass: break;