[PATCH] Fix an invalid check for duplicate option categories.

Alexander Kornienko alexfh at google.com
Wed Feb 5 08:36:47 PST 2014


  Fixed a dangerous typo ;)

Hi jordan_rose,

http://llvm-reviews.chandlerc.com/D2699

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D2699?vs=6885&id=6891#toc

Files:
  include/llvm/Support/CommandLine.h
  lib/Support/CommandLine.cpp

Index: include/llvm/Support/CommandLine.h
===================================================================
--- include/llvm/Support/CommandLine.h
+++ include/llvm/Support/CommandLine.h
@@ -149,8 +149,8 @@
 public:
   OptionCategory(const char *const Name, const char *const Description = 0)
       : Name(Name), Description(Description) { registerCategory(); }
-  const char *getName() { return Name; }
-  const char *getDescription() { return Description; }
+  const char *getName() const { return Name; }
+  const char *getDescription() const { return Description; }
 };
 
 // The general Option Category (used as default category).
Index: lib/Support/CommandLine.cpp
===================================================================
--- lib/Support/CommandLine.cpp
+++ lib/Support/CommandLine.cpp
@@ -125,8 +125,21 @@
 // Initialise the general option category.
 OptionCategory llvm::cl::GeneralCategory("General options");
 
+struct HasName {
+  HasName(StringRef Name) : Name(Name) {}
+  bool operator()(const OptionCategory *Category) const {
+    return Name == Category->getName();
+  }
+  StringRef Name;
+};
+
 void OptionCategory::registerCategory()
 {
+  assert(std::count_if(RegisteredOptionCategories->begin(),
+                       RegisteredOptionCategories->end(),
+                       HasName(getName())) == 0 &&
+         "Duplicate option categories");
+
   RegisteredOptionCategories->insert(this);
 }
 
@@ -1495,9 +1508,7 @@
   // It shall return true if A's name should be lexographically
   // ordered before B's name. It returns false otherwise.
   static bool OptionCategoryCompare(OptionCategory *A, OptionCategory *B) {
-    int Length = strcmp(A->getName(), B->getName());
-    assert(Length != 0 && "Duplicate option categories");
-    return Length < 0;
+    return strcmp(A->getName(), B->getName()) < 0;
   }
 
   // Make sure we inherit our base class's operator=()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2699.2.patch
Type: text/x-patch
Size: 1910 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140205/3297e419/attachment.bin>


More information about the cfe-commits mailing list