]> granicus.if.org Git - clang/commitdiff
[clang-format] Fix end-of-file comments text proto formatting
authorKrasimir Georgiev <krasimir@google.com>
Mon, 25 Jun 2018 11:08:24 +0000 (11:08 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Mon, 25 Jun 2018 11:08:24 +0000 (11:08 +0000)
Summary:
The case of end-of-file comments was formatted badly:
```
key: value
    # end-of-file comment
```
This patch fixes that formatting:
```
key: value
# end-of-file comment
```

Subscribers: cfe-commits

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

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

lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestTextProto.cpp

index e1fa72e84bda5504318853b9bc5ce77e87415800..e5afa1264abb5f5870ed7da4a77e224e224ce0b2 100644 (file)
@@ -303,6 +303,18 @@ void UnwrappedLineParser::parseFile() {
   else
     parseLevel(/*HasOpeningBrace=*/false);
   // Make sure to format the remaining tokens.
+  //
+  // LK_TextProto is special since its top-level is parsed as the body of a
+  // braced list, which does not necessarily have natural line separators such
+  // as a semicolon. Comments after the last entry that have been determined to
+  // not belong to that line, as in:
+  //   key: value
+  //   // endfile comment
+  // do not have a chance to be put on a line of their own until this point.
+  // Here we add this newline before end-of-file comments.
+  if (Style.Language == FormatStyle::LK_TextProto &&
+      !CommentsBeforeNextToken.empty())
+    addUnwrappedLine();
   flushComments(true);
   addUnwrappedLine();
 }
index 8ef4bf9a40a38fcb0f66353d1d9d7e95ae3a0f24..9c4e1a8520523874da9516af6dc0720a544549a8 100644 (file)
@@ -700,5 +700,22 @@ TEST_F(FormatTestTextProto, PreventBreaksBetweenKeyAndSubmessages) {
       "}");
 }
 
+TEST_F(FormatTestTextProto, FormatsCommentsAtEndOfFile) {
+  verifyFormat("key: value\n"
+               "# endfile comment");
+  verifyFormat("key: value\n"
+               "// endfile comment");
+  verifyFormat("key: value\n"
+               "// endfile comment 1\n"
+               "// endfile comment 2");
+  verifyFormat("submessage { key: value }\n"
+               "# endfile comment");
+  verifyFormat("submessage <\n"
+               "  key: value\n"
+               "  item {}\n"
+               ">\n"
+               "# endfile comment");
+}
+
 } // end namespace tooling
 } // end namespace clang