]> granicus.if.org Git - clang/commitdiff
[clang-format] Fix regression merging comments across newlines.
authorKrasimir Georgiev <krasimir@google.com>
Tue, 31 Jan 2017 13:32:38 +0000 (13:32 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Tue, 31 Jan 2017 13:32:38 +0000 (13:32 +0000)
Summary:
This fixes a regression that causes example:
```
enum A {
  a, // line a

  // line b
  b
};
```
to be formatted as follows:
```
enum A {
  a, // line a
     // line b
  b
};
```

Reviewers: djasper, klimek

Reviewed By: klimek

Subscribers: cfe-commits, sammccall, djasper, klimek

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

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

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

index dd28ba7d4a28ee06f8b5cca57c420b990292572e..82e62ac80f891b3c85fa61e7d772b6a031cac90f 100644 (file)
@@ -681,6 +681,23 @@ BreakableLineCommentSection::BreakableLineCommentSection(
       Content[i] = Content[i].substr(0, EndOfLine);
     }
     LineTok = CurrentTok->Next;
+    if (CurrentTok->Next && CurrentTok->Next->NewlinesBefore > 1) {
+      // A line comment section needs to broken by a line comment that is
+      // preceded by at least two newlines. Note that we put this break here
+      // instead of breaking at a previous stage during parsing, since that
+      // would split the contents of the enum into two unwrapped lines in this
+      // example, which is undesirable:
+      // enum A {
+      //   a, // comment about a
+      //
+      //   // comment about b
+      //   b
+      // };
+      //
+      // FIXME: Consider putting separate line comment sections as children to
+      // the unwrapped line instead.
+      break;
+    }
   }
 }
 
index c10a13f39e016660e6e48ec508775285678f6083..a96d4d9be3e7bf3387487b95bfb5446df101eab5 100644 (file)
@@ -2124,7 +2124,12 @@ void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
        I != E; ++I) {
     // Line comments that belong to the same line comment section are put on the
     // same line since later we might want to reflow content between them.
-    // See BreakableToken.
+    // Additional fine-grained breaking of line comment sections is controlled
+    // by the class BreakableLineCommentSection in case it is desirable to keep
+    // several line comment sections in the same unwrapped line.
+    //
+    // FIXME: Consider putting separate line comment sections as children to the
+    // unwrapped line instead.
     if (isOnNewLine(**I) && JustComments && !continuesLineComment(**I, *Line))
       addUnwrappedLine();
     pushToken(*I);
index 7348fa030df1d53552e6e3a2ded93ab389efb50b..fb770064753a2319f27836c9cc56e7829477a32c 100644 (file)
@@ -934,6 +934,24 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
                "  VAL_B\n"
                "};");
 
+  EXPECT_EQ("enum A {\n"
+            "  // line a\n"
+            "  a,\n"
+            "  b, // line b\n"
+            "\n"
+            "  // line c\n"
+            "  c\n"
+            "};",
+            format("enum A {\n"
+                   "  // line a\n"
+                   "  a,\n"
+                   "  b, // line b\n"
+                   "\n"
+                   "  // line c\n"
+                   "  c\n"
+                   "};",
+                   getLLVMStyleWithColumns(20)));
+
   verifyFormat(
       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");