krasimir updated this revision to Diff 119518.
krasimir added a comment.

- Address review comments


https://reviews.llvm.org/D39024

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


Index: unittests/Format/UsingDeclarationsSorterTest.cpp
===================================================================
--- unittests/Format/UsingDeclarationsSorterTest.cpp
+++ unittests/Format/UsingDeclarationsSorterTest.cpp
@@ -291,13 +291,41 @@
 }
 
 TEST_F(UsingDeclarationsSorterTest, SortsPartialRangeOfUsingDeclarations) {
-  EXPECT_EQ("using b;\n"
-            "using a;\n"
+  // Sorts the whole block of using declarations surrounding the range.
+  EXPECT_EQ("using a;\n"
+            "using b;\n"
             "using c;",
             sortUsingDeclarations("using b;\n"
                                   "using c;\n" // starts at offset 10
                                   "using a;",
                                   {tooling::Range(10, 15)}));
+  EXPECT_EQ("using a;\n"
+            "using b;\n"
+            "using c;\n"
+            "using A = b;",
+            sortUsingDeclarations("using b;\n"
+                                  "using c;\n" // starts at offset 10
+                                  "using a;\n"
+                                  "using A = b;",
+                                  {tooling::Range(10, 15)}));
+
+  EXPECT_EQ("using d;\n"
+            "using c;\n"
+            "\n"
+            "using a;\n"
+            "using b;\n"
+            "\n"
+            "using f;\n"
+            "using e;",
+            sortUsingDeclarations("using d;\n"
+                                  "using c;\n"
+                                  "\n"
+                                  "using b;\n" // starts at offset 19
+                                  "using a;\n"
+                                  "\n"
+                                  "using f;\n"
+                                  "using e;",
+                                  {tooling::Range(19, 1)}));
 }
 
 } // end namespace
Index: lib/Format/UsingDeclarationsSorter.cpp
===================================================================
--- lib/Format/UsingDeclarationsSorter.cpp
+++ lib/Format/UsingDeclarationsSorter.cpp
@@ -76,6 +76,17 @@
 void endUsingDeclarationBlock(
     SmallVectorImpl<UsingDeclaration> *UsingDeclarations,
     const SourceManager &SourceMgr, tooling::Replacements *Fixes) {
+  bool BlockAffected = false;
+  for (const UsingDeclaration& Declaration : *UsingDeclarations) {
+    if (Declaration.Line->Affected) {
+      BlockAffected = true;
+      break;
+    }
+  }
+  if (!BlockAffected) {
+    UsingDeclarations->clear();
+    return;
+  }
   SmallVector<UsingDeclaration, 4> SortedUsingDeclarations(
       UsingDeclarations->begin(), UsingDeclarations->end());
   std::stable_sort(SortedUsingDeclarations.begin(),
@@ -122,7 +133,7 @@
   tooling::Replacements Fixes;
   SmallVector<UsingDeclaration, 4> UsingDeclarations;
   for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
-    if (!AnnotatedLines[I]->Affected || AnnotatedLines[I]->InPPDirective ||
+    if (AnnotatedLines[I]->InPPDirective ||
         !AnnotatedLines[I]->startsWith(tok::kw_using) ||
         AnnotatedLines[I]->First->Finalized) {
       endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes);


Index: unittests/Format/UsingDeclarationsSorterTest.cpp
===================================================================
--- unittests/Format/UsingDeclarationsSorterTest.cpp
+++ unittests/Format/UsingDeclarationsSorterTest.cpp
@@ -291,13 +291,41 @@
 }
 
 TEST_F(UsingDeclarationsSorterTest, SortsPartialRangeOfUsingDeclarations) {
-  EXPECT_EQ("using b;\n"
-            "using a;\n"
+  // Sorts the whole block of using declarations surrounding the range.
+  EXPECT_EQ("using a;\n"
+            "using b;\n"
             "using c;",
             sortUsingDeclarations("using b;\n"
                                   "using c;\n" // starts at offset 10
                                   "using a;",
                                   {tooling::Range(10, 15)}));
+  EXPECT_EQ("using a;\n"
+            "using b;\n"
+            "using c;\n"
+            "using A = b;",
+            sortUsingDeclarations("using b;\n"
+                                  "using c;\n" // starts at offset 10
+                                  "using a;\n"
+                                  "using A = b;",
+                                  {tooling::Range(10, 15)}));
+
+  EXPECT_EQ("using d;\n"
+            "using c;\n"
+            "\n"
+            "using a;\n"
+            "using b;\n"
+            "\n"
+            "using f;\n"
+            "using e;",
+            sortUsingDeclarations("using d;\n"
+                                  "using c;\n"
+                                  "\n"
+                                  "using b;\n" // starts at offset 19
+                                  "using a;\n"
+                                  "\n"
+                                  "using f;\n"
+                                  "using e;",
+                                  {tooling::Range(19, 1)}));
 }
 
 } // end namespace
Index: lib/Format/UsingDeclarationsSorter.cpp
===================================================================
--- lib/Format/UsingDeclarationsSorter.cpp
+++ lib/Format/UsingDeclarationsSorter.cpp
@@ -76,6 +76,17 @@
 void endUsingDeclarationBlock(
     SmallVectorImpl<UsingDeclaration> *UsingDeclarations,
     const SourceManager &SourceMgr, tooling::Replacements *Fixes) {
+  bool BlockAffected = false;
+  for (const UsingDeclaration& Declaration : *UsingDeclarations) {
+    if (Declaration.Line->Affected) {
+      BlockAffected = true;
+      break;
+    }
+  }
+  if (!BlockAffected) {
+    UsingDeclarations->clear();
+    return;
+  }
   SmallVector<UsingDeclaration, 4> SortedUsingDeclarations(
       UsingDeclarations->begin(), UsingDeclarations->end());
   std::stable_sort(SortedUsingDeclarations.begin(),
@@ -122,7 +133,7 @@
   tooling::Replacements Fixes;
   SmallVector<UsingDeclaration, 4> UsingDeclarations;
   for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
-    if (!AnnotatedLines[I]->Affected || AnnotatedLines[I]->InPPDirective ||
+    if (AnnotatedLines[I]->InPPDirective ||
         !AnnotatedLines[I]->startsWith(tok::kw_using) ||
         AnnotatedLines[I]->First->Finalized) {
       endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to