[PATCH] D40435: [clang-format] Deduplicate using declarations

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 24 10:00:21 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL318960: [clang-format] Deduplicate using declarations (authored by krasimir).

Repository:
  rL LLVM

https://reviews.llvm.org/D40435

Files:
  cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp
  cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp


Index: cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp
===================================================================
--- cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp
+++ cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp
@@ -130,7 +130,27 @@
       UsingDeclarations->begin(), UsingDeclarations->end());
   std::stable_sort(SortedUsingDeclarations.begin(),
                    SortedUsingDeclarations.end());
+  SortedUsingDeclarations.erase(
+      std::unique(SortedUsingDeclarations.begin(),
+                  SortedUsingDeclarations.end(),
+                  [](const UsingDeclaration &a, const UsingDeclaration &b) {
+                    return a.Label == b.Label;
+                  }),
+      SortedUsingDeclarations.end());
   for (size_t I = 0, E = UsingDeclarations->size(); I < E; ++I) {
+    if (I >= SortedUsingDeclarations.size()) {
+      // This using declaration has been deduplicated, delete it.
+      auto Begin =
+          (*UsingDeclarations)[I].Line->First->WhitespaceRange.getBegin();
+      auto End = (*UsingDeclarations)[I].Line->Last->Tok.getEndLoc();
+      auto Range = CharSourceRange::getCharRange(Begin, End);
+      auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, ""));
+      if (Err) {
+        llvm::errs() << "Error while sorting using declarations: "
+                     << llvm::toString(std::move(Err)) << "\n";
+      }
+      continue;
+    }
     if ((*UsingDeclarations)[I].Line == SortedUsingDeclarations[I].Line)
       continue;
     auto Begin = (*UsingDeclarations)[I].Line->First->Tok.getLocation();
Index: cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp
===================================================================
--- cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp
+++ cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp
@@ -142,20 +142,16 @@
 
 TEST_F(UsingDeclarationsSorterTest, SortsStably) {
   EXPECT_EQ("using a;\n"
-            "using a;\n"
             "using A;\n"
             "using a;\n"
             "using A;\n"
             "using a;\n"
             "using A;\n"
             "using a;\n"
             "using B;\n"
             "using b;\n"
-            "using b;\n"
             "using B;\n"
             "using b;\n"
-            "using b;\n"
-            "using b;\n"
             "using B;\n"
             "using b;",
             sortUsingDeclarations("using a;\n"
@@ -355,6 +351,25 @@
                                   "/* comment */ using a;"));
 }
 
+TEST_F(UsingDeclarationsSorterTest, DeduplicatesUsingDeclarations) {
+  EXPECT_EQ("using a;\n"
+            "using b;\n"
+            "using c;\n"
+            "\n"
+            "using a;\n"
+            "using e;",
+            sortUsingDeclarations("using c;\n"
+                                  "using a;\n"
+                                  "using b;\n"
+                                  "using a;\n"
+                                  "using b;\n"
+                                  "\n"
+                                  "using e;\n"
+                                  "using a;\n"
+                                  "using e;"));
+
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40435.124227.patch
Type: text/x-patch
Size: 3213 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171124/542f6004/attachment.bin>


More information about the cfe-commits mailing list