r293997 - [clang-format] Re-align broken comment lines where appropriate.

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 3 02:18:25 PST 2017


Author: krasimir
Date: Fri Feb  3 04:18:25 2017
New Revision: 293997

URL: http://llvm.org/viewvc/llvm-project?rev=293997&view=rev
Log:
[clang-format] Re-align broken comment lines where appropriate.

Summary:
The comment aligner was skipping over newly broken comment lines. This patch fixes that.

source:
```
int ab; // line
int a; // long long
```

format with column limit 15 before:
```
int ab; // line
int a;  // long
       // long
```

format with column limit 15 after:
```
int ab; // line
int a;  // long
        // long
```

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

Differential Revision: https://reviews.llvm.org/D29486

Modified:
    cfe/trunk/lib/Format/WhitespaceManager.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/WhitespaceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/WhitespaceManager.cpp?rev=293997&r1=293996&r2=293997&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.cpp (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.cpp Fri Feb  3 04:18:25 2017
@@ -164,8 +164,9 @@ void WhitespaceManager::calculateLineBre
   const WhitespaceManager::Change *LastBlockComment = nullptr;
   for (auto &Change : Changes) {
     // Reset the IsTrailingComment flag for changes inside of trailing comments
-    // so they don't get realigned later.
-    if (Change.IsInsideToken)
+    // so they don't get realigned later. Comment line breaks however still need
+    // to be aligned.
+    if (Change.IsInsideToken && Change.NewlinesBefore == 0)
       Change.IsTrailingComment = false;
     Change.StartOfBlockComment = nullptr;
     Change.IndentationOffset = 0;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=293997&r1=293996&r2=293997&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Feb  3 04:18:25 2017
@@ -11852,6 +11852,40 @@ TEST_F(FormatTest, AlignTrailingComments
                    "             // line 3\n"
                    "          b);",
                    getLLVMStyleWithColumns(40)));
+
+  // Align newly broken trailing comments.
+  EXPECT_EQ("int ab; // line\n"
+            "int a;  // long\n"
+            "        // long\n",
+            format("int ab; // line\n"
+                   "int a; // long long\n",
+                   getLLVMStyleWithColumns(15)));
+  EXPECT_EQ("int ab; // line\n"
+            "int a;  // long\n"
+            "        // long\n"
+            "        // long",
+            format("int ab; // line\n"
+                   "int a; // long long\n"
+                   "       // long",
+                   getLLVMStyleWithColumns(15)));
+  EXPECT_EQ("int ab; // line\n"
+            "int a;  // long\n"
+            "        // long\n"
+            "pt c;   // long",
+            format("int ab; // line\n"
+                   "int a; // long long\n"
+                   "pt c; // long",
+                   getLLVMStyleWithColumns(15)));
+  EXPECT_EQ("int ab; // line\n"
+            "int a;  // long\n"
+            "        // long\n"
+            "\n"
+            "// long",
+            format("int ab; // line\n"
+                   "int a; // long long\n"
+                   "\n"
+                   "// long",
+                   getLLVMStyleWithColumns(15)));
 }
 } // end namespace
 } // end namespace format




More information about the cfe-commits mailing list