Summary:
The check performed in the comparator is invalid, as some STL
implementations enforce strict weak ordering by calling the comparator with the
same value. This check was also in a wrong place: the assertion would only fire
when -help was used. The new check is performed each time the category is
registered (we are not going to have thousands of them, so it's fine to do it in
O(N^2)).
Reviewers: jordan_rose
Reviewed By: jordan_rose
CC: cfe-commits, alexmc
Differential Revision: http://llvm-reviews.chandlerc.com/D2699
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@200853
91177308-0d34-0410-b5e6-
96231b3b80d8
public:
OptionCategory(const char *const Name, const char *const Description = 0)
: Name(Name), Description(Description) { registerCategory(); }
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).
};
// The general Option Category (used as default category).
// Initialise the general option category.
OptionCategory llvm::cl::GeneralCategory("General options");
// 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()
{
void OptionCategory::registerCategory()
{
+ assert(std::count_if(RegisteredOptionCategories->begin(),
+ RegisteredOptionCategories->end(),
+ HasName(getName())) == 0 &&
+ "Duplicate option categories");
+
RegisteredOptionCategories->insert(this);
}
RegisteredOptionCategories->insert(this);
}
// 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) {
// 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=()
}
// Make sure we inherit our base class's operator=()