]> granicus.if.org Git - clang/commitdiff
[clang-format] Re-align broken comment lines where appropriate.
authorKrasimir Georgiev <krasimir@google.com>
Fri, 3 Feb 2017 10:18:25 +0000 (10:18 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Fri, 3 Feb 2017 10:18:25 +0000 (10:18 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@293997 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/WhitespaceManager.cpp
unittests/Format/FormatTest.cpp

index 3fd92eaf7856e3514bd89d1082b3cf9b12145c77..93a2e490c660404cd1eb5a772cb785138089586f 100644 (file)
@@ -164,8 +164,9 @@ void WhitespaceManager::calculateLineBreakInformation() {
   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;
index d9f56e38f841c9c1c6df80619904e3bfbf898a73..6cc9d1979fae6ee6ee1967633b94e446e1dd5170 100644 (file)
@@ -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