]> granicus.if.org Git - clang/commitdiff
[clang-format] Fix regression about adding leading whitespace to the content of line...
authorKrasimir Georgiev <krasimir@google.com>
Tue, 31 Jan 2017 15:40:15 +0000 (15:40 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Tue, 31 Jan 2017 15:40:15 +0000 (15:40 +0000)
Summary:
The reflower didn't measure precisely the line column of a line in the middle of
a line comment section that has a prefix that needs to be adapted.

source:
```
/// a
//b
```

format before:
```
/// a
 //b
```

format after:
```
/// a
// b
```

Reviewers: djasper

Reviewed By: djasper

Subscribers: sammccall, cfe-commits, klimek

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

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

lib/Format/BreakableToken.cpp
unittests/Format/FormatTest.cpp

index 683d6e7249b6c8e0f1db898cae24c64be9b33bdf..148656080d32604c477ccdd8319852e2b544d318 100644 (file)
@@ -796,10 +796,13 @@ void BreakableLineCommentSection::replaceWhitespaceBefore(
     } else {
       // This is the first line for the current token, but no reflow with the
       // previous token is necessary. However, we still may need to adjust the
-      // start column.
+      // start column. Note that ContentColumn[LineIndex] is the expected
+      // content column after a possible update to the prefix, hence the prefix
+      // length change is included.
       unsigned LineColumn =
           ContentColumn[LineIndex] -
-          (Content[LineIndex].data() - Lines[LineIndex].data());
+          (Content[LineIndex].data() - Lines[LineIndex].data()) +
+          (OriginalPrefix[LineIndex].size() - Prefix[LineIndex].size());
       if (tokenAt(LineIndex).OriginalColumn != LineColumn) {
         Whitespaces.replaceWhitespace(*Tokens[LineIndex],
                                       /*Newlines=*/1,
@@ -813,13 +816,14 @@ void BreakableLineCommentSection::replaceWhitespaceBefore(
                                         /*InPPDirective=*/false);
       }
     }
-  } else if (OriginalPrefix[LineIndex] != Prefix[LineIndex]) {
-    // This is not the first line of the token. Adjust the prefix if necessary.
+  }
+  if (OriginalPrefix[LineIndex] != Prefix[LineIndex]) {
+    // Adjust the prefix if necessary.
 
     // Take care of the space possibly introduced after a decoration.
     assert(Prefix[LineIndex] == (OriginalPrefix[LineIndex] + " ").str() &&
-           "Expecting a block comment decoration to differ from original by "
-           "at most a space");
+           "Expecting a line comment prefix to differ from original by at most "
+           "a space");
     Whitespaces.replaceWhitespaceInToken(
         tokenAt(LineIndex), OriginalPrefix[LineIndex].size(), 0, "", "",
         /*InPPDirective=*/false, /*Newlines=*/0, /*Spaces=*/1);
index 1097514a985cd991a363634bdad646af04958ca3..b316350f470fe7b31f51f1e2ad9e38d4d5e6ae1e 100644 (file)
@@ -1371,6 +1371,25 @@ TEST_F(FormatTest, SplitsLongCxxComments) {
             format("//Even if it makes the line exceed the column limit",
                    getLLVMStyleWithColumns(51)));
   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
+  EXPECT_EQ("/// line 1\n"
+            "// add leading whitespace",
+            format("/// line 1\n"
+                   "//add leading whitespace",
+                   getLLVMStyleWithColumns(30)));
+  EXPECT_EQ("/// line 1\n"
+            "/// line 2\n"
+            "//! line 3\n"
+            "//! line 4\n"
+            "//! line 5\n"
+            "// line 6\n"
+            "// line 7",
+            format("///line 1\n"
+                   "///line 2\n"
+                   "//! line 3\n"
+                   "//!line 4\n"
+                   "//!line 5\n"
+                   "// line 6\n"
+                   "//line 7", getLLVMStyleWithColumns(20)));
 
   EXPECT_EQ("// aa bb cc dd",
             format("// aa bb             cc dd                   ",