r293633 - [clang-format] Fix reflow in block comment lines with leading whitespace.

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 31 06:31:45 PST 2017


Author: krasimir
Date: Tue Jan 31 08:31:44 2017
New Revision: 293633

URL: http://llvm.org/viewvc/llvm-project?rev=293633&view=rev
Log:
[clang-format] Fix reflow in block comment lines with leading whitespace.

Summary:
The reflower was not taking into account the additional  leading whitespace in block comment lines.

source:
```
{
/*
 * long long long long
 *   long
 * long long long long
 */
}
```

format (with column limit 20) before:
```
{
  /*
   * long long long
   * long long long long
   * long long
   */
}
```
format after:
```
{
  /*
   * long long long
   * long long long
   * long long long
   */
}
```

Reviewers: djasper, klimek

Reviewed By: djasper

Subscribers: cfe-commits, sammccall, klimek

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

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

Modified: cfe/trunk/lib/Format/BreakableToken.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/BreakableToken.cpp?rev=293633&r1=293632&r2=293633&view=diff
==============================================================================
--- cfe/trunk/lib/Format/BreakableToken.cpp (original)
+++ cfe/trunk/lib/Format/BreakableToken.cpp Tue Jan 31 08:31:44 2017
@@ -521,10 +521,15 @@ unsigned BreakableBlockComment::getLineL
     unsigned PreviousEndColumn,
     unsigned ColumnLimit,
     Split SplitBefore) const {
-    if (SplitBefore.first == StringRef::npos ||
-        SplitBefore.first + SplitBefore.second < Content[LineIndex].size()) {
-      // A piece of line, not the whole, gets reflown.
-      return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
+  if (SplitBefore.first == StringRef::npos ||
+      // Block comment line contents contain the trailing whitespace after the
+      // decoration, so the need of left trim. Note that this behavior is
+      // consistent with the breaking of block comments where the indentation of
+      // a broken line is uniform across all the lines of the block comment.
+      SplitBefore.first + SplitBefore.second <
+          Content[LineIndex].ltrim().size()) {
+    // A piece of line, not the whole, gets reflown.
+    return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
   } else {
     // The whole line gets reflown, need to check if we need to insert a break
     // for the postfix or not.

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=293633&r1=293632&r2=293633&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Jan 31 08:31:44 2017
@@ -1960,6 +1960,23 @@ TEST_F(FormatTest, ReflowsComments) {
                    " * longg */",
                    getLLVMStyleWithColumns(20)));
 
+  // Reflow lines with leading whitespace.
+  EXPECT_EQ("{\n"
+            "  /*\n"
+            "   * long long long\n"
+            "   * long long long\n"
+            "   * long long long\n"
+            "   */\n"
+            "}",
+            format("{\n"
+                   "/*\n"
+                   " * long long long long\n"
+                   " *   long\n"
+                   " * long long long long\n"
+                   " */\n"
+                   "}",
+                   getLLVMStyleWithColumns(20)));
+
   // Break single line block comments that are first in the line with ' *'
   // decoration.
   EXPECT_EQ("/* long long long\n"




More information about the cfe-commits mailing list