r317901 - [clang-format] Handle leading comments in using declaration

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 10 09:11:18 PST 2017


Author: djasper
Date: Fri Nov 10 09:11:18 2017
New Revision: 317901

URL: http://llvm.org/viewvc/llvm-project?rev=317901&view=rev
Log:
[clang-format] Handle leading comments in using declaration

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

  /* override */ using std::string;

Patch by Igor Sugak. Thank you.

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

Modified: cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp?rev=317901&r1=317900&r2=317901&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp (original)
+++ cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp Fri Nov 10 09:11:18 2017
@@ -172,15 +172,17 @@ std::pair<tooling::Replacements, unsigne
   tooling::Replacements Fixes;
   SmallVector<UsingDeclaration, 4> UsingDeclarations;
   for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
+    const 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);
+    const auto *UsingTok =
+        FirstTok->is(tok::comment) ? FirstTok->getNextNonComment() : FirstTok;
+    std::string Label = computeUsingDeclarationLabel(UsingTok);
     if (Label.empty()) {
       endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes);
       continue;

Modified: cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp?rev=317901&r1=317900&r2=317901&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp (original)
+++ cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp Fri Nov 10 09:11:18 2017
@@ -348,6 +348,13 @@ TEST_F(UsingDeclarationsSorterTest, Sort
                                   {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




More information about the cfe-commits mailing list