[PATCH] D39478: [clang-format] Handle leading comments in using declaration

Igor Sugak via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 1 12:34:52 PDT 2017


sugak updated this revision to Diff 121165.
sugak marked 3 inline comments as done.
sugak added a comment.

Updated per comments.

@djasper : Thank you for the review! Would you commit this for me?


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,17 @@
   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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39478.121165.patch
Type: text/x-patch
Size: 1973 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171101/2ccb8a2b/attachment.bin>


More information about the cfe-commits mailing list