[llvm] r189424 - Revert "Option parsing: support case-insensitive option matching." as it broke Windows buildbot.

Rui Ueyama ruiu at google.com
Tue Aug 27 17:02:06 PDT 2013


Author: ruiu
Date: Tue Aug 27 19:02:06 2013
New Revision: 189424

URL: http://llvm.org/viewvc/llvm-project?rev=189424&view=rev
Log:
Revert "Option parsing: support case-insensitive option matching." as it broke Windows buildbot.

This reverts r189416.

Modified:
    llvm/trunk/include/llvm/Option/OptTable.h
    llvm/trunk/lib/Option/OptTable.cpp
    llvm/trunk/unittests/Option/OptionParsingTest.cpp
    llvm/trunk/utils/TableGen/OptParserEmitter.cpp

Modified: llvm/trunk/include/llvm/Option/OptTable.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Option/OptTable.h?rev=189424&r1=189423&r2=189424&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Option/OptTable.h (original)
+++ llvm/trunk/include/llvm/Option/OptTable.h Tue Aug 27 19:02:06 2013
@@ -51,7 +51,6 @@ private:
   /// \brief The static option information table.
   const Info *OptionInfos;
   unsigned NumOptionInfos;
-  bool IgnoreCase;
 
   unsigned TheInputOptionID;
   unsigned TheUnknownOptionID;
@@ -73,8 +72,7 @@ private:
   }
 
 protected:
-  OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos,
-           bool _IgnoreCase = false);
+  OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos);
 public:
   ~OptTable();
 

Modified: llvm/trunk/lib/Option/OptTable.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Option/OptTable.cpp?rev=189424&r1=189423&r2=189424&view=diff
==============================================================================
--- llvm/trunk/lib/Option/OptTable.cpp (original)
+++ llvm/trunk/lib/Option/OptTable.cpp Tue Aug 27 19:02:06 2013
@@ -19,45 +19,47 @@
 using namespace llvm;
 using namespace llvm::opt;
 
-namespace llvm {
-namespace opt {
-
-// 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) {
-  size_t I = strlen(A);
-  size_t J = strlen(B);
-  // If A and B are the same length, compare them ignoring case.
-  if (I == J)
-    return strcasecmp(A, B);
-  // A is shorter than B. In this case A is less than B only when it's
-  // lexicographically less than B. strncasecmp() == 0 means A is a prefix of B,
-  // which in turn means A should appear *after* B.
-  if (I < J)
-    return strncasecmp(A, B, I) < 0 ? -1 : 1;
-  // Otherwise, vice versa.
-  return strncasecmp(A, B, J) <= 0 ? -1 : 1;
-}
+// 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.
 
 static int StrCmpOptionName(const char *A, const char *B) {
-  if (int N = StrCmpOptionNameIgnoreCase(A, B))
-    return N;
-  return strcmp(A, B);
+  char a = *A, b = *B;
+  while (a == b) {
+    if (a == '\0')
+      return 0;
+
+    a = *++A;
+    b = *++B;
+  }
+
+  if (a == '\0') // A is a prefix of B.
+    return 1;
+  if (b == '\0') // B is a prefix of A.
+    return -1;
+
+  // Otherwise lexicographic.
+  return (a < b) ? -1 : 1;
 }
 
+namespace llvm {
+namespace opt {
+
 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 < 0;
+    return N == -1;
 
   for (const char * const *APre = A.Prefixes,
                   * const *BPre = B.Prefixes;
                           *APre != 0 && *BPre != 0; ++APre, ++BPre) {
     if (int N = StrCmpOptionName(*APre, *BPre))
-      return N < 0;
+      return N == -1;
   }
 
   // Names are the same, check that classes are in order; exactly one
@@ -69,21 +71,19 @@ static inline bool operator<(const OptTa
 
 // Support lower_bound between info and an option name.
 static inline bool operator<(const OptTable::Info &I, const char *Name) {
-  return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0;
+  return StrCmpOptionName(I.Name, Name) == -1;
 }
 static inline bool operator<(const char *Name, const OptTable::Info &I) {
-  return StrCmpOptionNameIgnoreCase(Name, I.Name) < 0;
+  return StrCmpOptionName(Name, I.Name) == -1;
 }
 }
 }
 
 OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
 
-OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos,
-                   bool _IgnoreCase)
+OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
   : OptionInfos(_OptionInfos),
     NumOptionInfos(_NumOptionInfos),
-    IgnoreCase(_IgnoreCase),
     TheInputOptionID(0),
     TheUnknownOptionID(0),
     FirstSearchableIndex(0)
@@ -170,26 +170,12 @@ static bool isInput(const llvm::StringSe
   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.
-static unsigned matchOption(const OptTable::Info *I, StringRef Str,
-                            bool IgnoreCase) {
+static unsigned matchOption(const OptTable::Info *I, StringRef Str) {
   for (const char * const *Pre = I->Prefixes; *Pre != 0; ++Pre) {
     StringRef Prefix(*Pre);
-    if (Str.startswith(Prefix)) {
-      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();
-    }
+    if (Str.startswith(Prefix) && Str.substr(Prefix.size()).startswith(I->Name))
+      return Prefix.size() + StringRef(I->Name).size();
   }
   return 0;
 }
@@ -224,7 +210,7 @@ Arg *OptTable::ParseOneArg(const ArgList
     unsigned ArgSize = 0;
     // Scan for first option which is a proper prefix.
     for (; Start != End; ++Start)
-      if ((ArgSize = matchOption(Start, Str, IgnoreCase)))
+      if ((ArgSize = matchOption(Start, Str)))
         break;
     if (Start == End)
       break;

Modified: llvm/trunk/unittests/Option/OptionParsingTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Option/OptionParsingTest.cpp?rev=189424&r1=189423&r2=189424&view=diff
==============================================================================
--- llvm/trunk/unittests/Option/OptionParsingTest.cpp (original)
+++ llvm/trunk/unittests/Option/OptionParsingTest.cpp Tue Aug 27 19:02:06 2013
@@ -20,7 +20,7 @@ using namespace llvm::opt;
 enum ID {
   OPT_INVALID = 0, // This is not an option ID.
 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
-               HELPTEXT, METAVAR) OPT_##ID,
+              HELPTEXT, METAVAR) OPT_##ID,
 #include "Opts.inc"
   LastOption
 #undef OPTION
@@ -48,8 +48,8 @@ static const OptTable::Info InfoTable[]
 namespace {
 class TestOptTable : public OptTable {
 public:
-  TestOptTable(bool IgnoreCase = false)
-    : OptTable(InfoTable, array_lengthof(InfoTable), IgnoreCase) {}
+  TestOptTable()
+    : OptTable(InfoTable, array_lengthof(InfoTable)) {}
 };
 }
 
@@ -157,26 +157,6 @@ TEST(Option, AliasArgs) {
   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) {
   TestOptTable T;
   unsigned MAI, MAC;

Modified: llvm/trunk/utils/TableGen/OptParserEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/OptParserEmitter.cpp?rev=189424&r1=189423&r2=189424&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/OptParserEmitter.cpp (original)
+++ llvm/trunk/utils/TableGen/OptParserEmitter.cpp Tue Aug 27 19:02:06 2013
@@ -13,25 +13,27 @@
 #include "llvm/ADT/Twine.h"
 #include "llvm/TableGen/Record.h"
 #include "llvm/TableGen/TableGenBackend.h"
-#include <cstring>
 #include <map>
-#include <strings.h>
 
 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) {
-  size_t I = strlen(A);
-  size_t J = strlen(B);
-  if (I == J) {
-    if (int N = strcasecmp(A, B))
-      return N;
-    return strcmp(A, B);
+  char a = *A, b = *B;
+  while (a == b) {
+    if (a == '\0')
+      return 0;
+
+    a = *++A;
+    b = *++B;
   }
-  if (I < J)
-    return strncasecmp(A, B, I) < 0 ? -1 : 1;
-  return strncasecmp(A, B, J) <= 0 ? -1 : 1;
+
+  if (a == '\0') // A is a prefix of B.
+    return 1;
+  if (b == '\0') // B is a prefix of A.
+    return -1;
+
+  // Otherwise lexicographic.
+  return (a < b) ? -1 : 1;
 }
 
 static int CompareOptionRecords(const void *Av, const void *Bv) {
@@ -48,7 +50,7 @@ static int CompareOptionRecords(const vo
   if (!ASent)
     if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
                                    B->getValueAsString("Name").c_str()))
-      return Cmp;
+    return Cmp;
 
   if (!ASent) {
     std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes");





More information about the llvm-commits mailing list