sugak created this revision.

This fixes clang-format internal assertion for the following code:

  /* override */ using std::string;

Added add a unit test.


https://reviews.llvm.org/D39478

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


Index: unittests/Format/UsingDeclarationsSorterTest.cpp
===================================================================
--- unittests/Format/UsingDeclarationsSorterTest.cpp
+++ unittests/Format/UsingDeclarationsSorterTest.cpp
@@ -328,6 +328,13 @@
                                   {tooling::Range(19, 1)}));
 }
 
+TEST_F(UsingDeclarationsSorterTest, 
SortsUsingDeclarationsWithLeadingkComments) {
+  EXPECT_EQ("/* comment */ using a;\n"
+            "/* comment */ using b;",
+            sortUsingDeclarations("/* comment */ using b;\n"
+                                  "/* comment */ using a;"));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang
Index: lib/Format/UsingDeclarationsSorter.cpp
===================================================================
--- lib/Format/UsingDeclarationsSorter.cpp
+++ lib/Format/UsingDeclarationsSorter.cpp
@@ -133,15 +133,18 @@
   tooling::Replacements Fixes;
   SmallVector<UsingDeclaration, 4> UsingDeclarations;
   for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
+    auto FirstTok = AnnotatedLines[I]->First;
     if (AnnotatedLines[I]->InPPDirective ||
-        !AnnotatedLines[I]->startsWith(tok::kw_using) ||
-        AnnotatedLines[I]->First->Finalized) {
+        !AnnotatedLines[I]->startsWith(tok::kw_using) || FirstTok->Finalized) {
       endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes);
       continue;
     }
-    if (AnnotatedLines[I]->First->NewlinesBefore > 1)
+    if (FirstTok->NewlinesBefore > 1) {
       endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes);
-    std::string Label = computeUsingDeclarationLabel(AnnotatedLines[I]->First);
+    }
+    auto UsingTok =
+        FirstTok->is(tok::comment) ? FirstTok->getNextNonComment() : FirstTok;
+    std::string Label = computeUsingDeclarationLabel(UsingTok);
     if (Label.empty()) {
       endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes);
       continue;


Index: unittests/Format/UsingDeclarationsSorterTest.cpp
===================================================================
--- unittests/Format/UsingDeclarationsSorterTest.cpp
+++ unittests/Format/UsingDeclarationsSorterTest.cpp
@@ -328,6 +328,13 @@
                                   {tooling::Range(19, 1)}));
 }
 
+TEST_F(UsingDeclarationsSorterTest, SortsUsingDeclarationsWithLeadingkComments) {
+  EXPECT_EQ("/* comment */ using a;\n"
+            "/* comment */ using b;",
+            sortUsingDeclarations("/* comment */ using b;\n"
+                                  "/* comment */ using a;"));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang
Index: lib/Format/UsingDeclarationsSorter.cpp
===================================================================
--- lib/Format/UsingDeclarationsSorter.cpp
+++ lib/Format/UsingDeclarationsSorter.cpp
@@ -133,15 +133,18 @@
   tooling::Replacements Fixes;
   SmallVector<UsingDeclaration, 4> UsingDeclarations;
   for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
+    auto FirstTok = AnnotatedLines[I]->First;
     if (AnnotatedLines[I]->InPPDirective ||
-        !AnnotatedLines[I]->startsWith(tok::kw_using) ||
-        AnnotatedLines[I]->First->Finalized) {
+        !AnnotatedLines[I]->startsWith(tok::kw_using) || FirstTok->Finalized) {
       endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes);
       continue;
     }
-    if (AnnotatedLines[I]->First->NewlinesBefore > 1)
+    if (FirstTok->NewlinesBefore > 1) {
       endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes);
-    std::string Label = computeUsingDeclarationLabel(AnnotatedLines[I]->First);
+    }
+    auto UsingTok =
+        FirstTok->is(tok::comment) ? FirstTok->getNextNonComment() : FirstTok;
+    std::string Label = computeUsingDeclarationLabel(UsingTok);
     if (Label.empty()) {
       endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes);
       continue;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to