[clang-tools-extra] r369068 - [clang-doc] Sort index elements case insensitive

Diego Astiazaran via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 15 16:32:13 PDT 2019


Author: diegoastiazaran
Date: Thu Aug 15 16:32:12 2019
New Revision: 369068

URL: http://llvm.org/viewvc/llvm-project?rev=369068&view=rev
Log:
[clang-doc] Sort index elements case insensitive

Implement logic to compare the references of the index case insensitive.

Differential revision: https://reviews.llvm.org/D66299

Modified:
    clang-tools-extra/trunk/clang-doc/Representation.cpp
    clang-tools-extra/trunk/clang-doc/Representation.h
    clang-tools-extra/trunk/unittests/clang-doc/GeneratorTest.cpp

Modified: clang-tools-extra/trunk/clang-doc/Representation.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-doc/Representation.cpp?rev=369068&r1=369067&r2=369068&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-doc/Representation.cpp (original)
+++ clang-tools-extra/trunk/clang-doc/Representation.cpp Thu Aug 15 16:32:12 2019
@@ -245,6 +245,26 @@ llvm::SmallString<16> Info::extractName(
   return llvm::SmallString<16>("");
 }
 
+// Order is based on the Name attribute: case insensitive order
+bool Index::operator<(const Index &Other) const {
+  // Loop through each character of both strings
+  for (unsigned I = 0; I < Name.size() && I < Other.Name.size(); ++I) {
+    // Compare them after converting both to lower case
+    int D = tolower(Name[I]) - tolower(Other.Name[I]);
+    if (D == 0)
+      continue;
+    return D < 0;
+  }
+  // If both strings have the size it means they would be equal if changed to
+  // lower case. In here, lower case will be smaller than upper case
+  // Example: string < stRing = true
+  // This is the opposite of how operator < handles strings
+  if (Name.size() == Other.Name.size())
+    return Name > Other.Name;
+  // If they are not the same size; the shorter string is smaller
+  return Name.size() < Other.Name.size();
+}
+
 void Index::sort() {
   std::sort(Children.begin(), Children.end());
   for (auto &C : Children)

Modified: clang-tools-extra/trunk/clang-doc/Representation.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-doc/Representation.h?rev=369068&r1=369067&r2=369068&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-doc/Representation.h (original)
+++ clang-tools-extra/trunk/clang-doc/Representation.h Thu Aug 15 16:32:12 2019
@@ -292,7 +292,8 @@ struct SymbolInfo : public Info {
   SymbolInfo(InfoType IT) : Info(IT) {}
   SymbolInfo(InfoType IT, SymbolID USR) : Info(IT, USR) {}
   SymbolInfo(InfoType IT, SymbolID USR, StringRef Name) : Info(IT, USR, Name) {}
-  SymbolInfo(InfoType IT, SymbolID USR, StringRef Name, StringRef Path) : Info(IT, USR, Name, Path) {}
+  SymbolInfo(InfoType IT, SymbolID USR, StringRef Name, StringRef Path)
+      : Info(IT, USR, Name, Path) {}
 
   void merge(SymbolInfo &&I);
 
@@ -368,13 +369,14 @@ struct EnumInfo : public SymbolInfo {
 
 struct Index : public Reference {
   Index() = default;
+  Index(StringRef Name) : Reference(Name) {}
   Index(StringRef Name, StringRef JumpToSection)
       : Reference(Name), JumpToSection(JumpToSection) {}
   Index(SymbolID USR, StringRef Name, InfoType IT, StringRef Path)
       : Reference(USR, Name, IT, Path) {}
   // This is used to look for a USR in a vector of Indexes using std::find
   bool operator==(const SymbolID &Other) const { return USR == Other; }
-  bool operator<(const Index &Other) const { return Name < Other.Name; }
+  bool operator<(const Index &Other) const;
 
   llvm::Optional<SmallString<16>> JumpToSection;
   std::vector<Index> Children;

Modified: clang-tools-extra/trunk/unittests/clang-doc/GeneratorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-doc/GeneratorTest.cpp?rev=369068&r1=369067&r2=369068&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-doc/GeneratorTest.cpp (original)
+++ clang-tools-extra/trunk/unittests/clang-doc/GeneratorTest.cpp Thu Aug 15 16:32:12 2019
@@ -70,5 +70,24 @@ TEST(GeneratorTest, emitIndex) {
   CheckIndex(ExpectedIdx, Idx);
 }
 
+TEST(GeneratorTest, sortIndex) {
+  Index Idx;
+  Idx.Children.emplace_back("b");
+  Idx.Children.emplace_back("aA");
+  Idx.Children.emplace_back("aa");
+  Idx.Children.emplace_back("A");
+  Idx.Children.emplace_back("a");
+  Idx.sort();
+
+  Index ExpectedIdx;
+  ExpectedIdx.Children.emplace_back("a");
+  ExpectedIdx.Children.emplace_back("A");
+  ExpectedIdx.Children.emplace_back("aa");
+  ExpectedIdx.Children.emplace_back("aA");
+  ExpectedIdx.Children.emplace_back("b");
+
+  CheckIndex(ExpectedIdx, Idx);
+}
+
 } // namespace doc
 } // namespace clang




More information about the cfe-commits mailing list