]> granicus.if.org Git - clang/commitdiff
[clang-format] Fix regression that breaks comments without a comment prefix
authorKrasimir Georgiev <krasimir@google.com>
Mon, 30 Jan 2017 21:00:01 +0000 (21:00 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Mon, 30 Jan 2017 21:00:01 +0000 (21:00 +0000)
Summary:
Consider formatting the following code fragment with column limit 20:
```
{
  // line 1
  // line 2\
  // long long long line
}
```
Before this fix the output is:
```
{
  // line 1
  // line 2\
  // long long
  long line
}
```
This patch fixes a regression that breaks the last comment line without
adding the '//' prefix.

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

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

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

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

index 569ff1c648345796dd0d0ab1986bac22aed96a31..4a29faded144656feec67c44df341a4d73215e31 100644 (file)
@@ -642,7 +642,11 @@ BreakableLineCommentSection::BreakableLineCommentSection(
     Prefix.resize(Lines.size());
     OriginalPrefix.resize(Lines.size());
     for (size_t i = FirstLineIndex, e = Lines.size(); i < e; ++i) {
-      StringRef IndentPrefix = getLineCommentIndentPrefix(Lines[i]);
+      // We need to trim the blanks in case this is not the first line in a
+      // multiline comment. Then the indent is included in Lines[i].
+      StringRef IndentPrefix =
+          getLineCommentIndentPrefix(Lines[i].ltrim(Blanks));
+      assert(IndentPrefix.startswith("//"));
       OriginalPrefix[i] = Prefix[i] = IndentPrefix;
       if (Lines[i].size() > Prefix[i].size() &&
           isAlphanumeric(Lines[i][Prefix[i].size()])) {
index d28200104056ed320f1ec25ae956df759c0aff91..4d4b64b20b31927120668b18764cd634895c2c6f 100644 (file)
@@ -1386,6 +1386,18 @@ TEST_F(FormatTest, SplitsLongCxxComments) {
       "#define XXX // q w e r\n"
       "            // t y u i",
       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
+  EXPECT_EQ("{\n"
+            "  //\n"
+            "  //\\\n"
+            "  // long 1 2 3 4\n"
+            "  // 5\n"
+            "}",
+            format("{\n"
+                   "  //\n"
+                   "  //\\\n"
+                   "  // long 1 2 3 4 5\n"
+                   "}",
+                   getLLVMStyleWithColumns(20)));
 }
 
 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {