r236573 - clang-format: Fix bug in multiline comment wrapping.

Daniel Jasper djasper at google.com
Wed May 6 00:17:22 PDT 2015


Author: djasper
Date: Wed May  6 02:17:22 2015
New Revision: 236573

URL: http://llvm.org/viewvc/llvm-project?rev=236573&view=rev
Log:
clang-format: Fix bug in multiline comment wrapping.

Splitting:
  /**
   * multiline block comment
   *
   */

Before:
  /**
   * multiline block
   *comment
   *
   */

After:
  /**
   * multiline block
   * comment
   *
   */

The reason was that the empty line inside the comment (with just the "*") was
confusing the comment breaking logic.

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=236573&r1=236572&r2=236573&view=diff
==============================================================================
--- cfe/trunk/lib/Format/BreakableToken.cpp (original)
+++ cfe/trunk/lib/Format/BreakableToken.cpp Wed May  6 02:17:22 2015
@@ -277,6 +277,8 @@ BreakableBlockComment::BreakableBlockCom
     // If the last line is empty, the closing "*/" will have a star.
     if (i + 1 == e && Lines[i].empty())
       break;
+    if (!Lines[i].empty() && i + 1 != e && Decoration.startswith(Lines[i]))
+      continue;
     while (!Lines[i].startswith(Decoration))
       Decoration = Decoration.substr(0, Decoration.size() - 1);
   }
@@ -297,14 +299,18 @@ BreakableBlockComment::BreakableBlockCom
       }
       continue;
     }
+
     // The first line already excludes the star.
     // For all other lines, adjust the line to exclude the star and
     // (optionally) the first whitespace.
-    StartOfLineColumn[i] += Decoration.size();
-    Lines[i] = Lines[i].substr(Decoration.size());
-    LeadingWhitespace[i] += Decoration.size();
-    IndentAtLineBreak =
-        std::min<int>(IndentAtLineBreak, std::max(0, StartOfLineColumn[i]));
+    unsigned DecorationSize =
+        Decoration.startswith(Lines[i]) ? Lines[i].size() : Decoration.size();
+    StartOfLineColumn[i] += DecorationSize;
+    Lines[i] = Lines[i].substr(DecorationSize);
+    LeadingWhitespace[i] += DecorationSize;
+    if (!Decoration.startswith(Lines[i]))
+      IndentAtLineBreak =
+          std::min<int>(IndentAtLineBreak, std::max(0, StartOfLineColumn[i]));
   }
   IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size());
   DEBUG({

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=236573&r1=236572&r2=236573&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed May  6 02:17:22 2015
@@ -1621,6 +1621,17 @@ TEST_F(FormatTest, SplitsLongLinesInComm
                    "   */",
                    getLLVMStyleWithColumns(20)));
 
+  EXPECT_EQ("/**\n"
+            " * multiline block\n"
+            " * comment\n"
+            " *\n"
+            " */",
+            format("/**\n"
+                   " * multiline block comment\n"
+                   " *\n"
+                   " */",
+                   getLLVMStyleWithColumns(20)));
+
   EXPECT_EQ("/*\n"
             "\n"
             "\n"
@@ -6525,7 +6536,7 @@ TEST_F(FormatTest, BlockComments) {
   EXPECT_EQ("/*\n"
             "*\n"
             " * aaaaaa\n"
-            "*aaaaaa\n"
+            " * aaaaaa\n"
             "*/",
             format("/*\n"
                    "*\n"





More information about the cfe-commits mailing list