r335449 - [clang-format] Fix end-of-file comments text proto formatting

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 25 04:08:25 PDT 2018


Author: krasimir
Date: Mon Jun 25 04:08:24 2018
New Revision: 335449

URL: http://llvm.org/viewvc/llvm-project?rev=335449&view=rev
Log:
[clang-format] Fix end-of-file comments text proto formatting

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

Modified:
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/unittests/Format/FormatTestTextProto.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=335449&r1=335448&r2=335449&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon Jun 25 04:08:24 2018
@@ -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();
 }

Modified: cfe/trunk/unittests/Format/FormatTestTextProto.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestTextProto.cpp?rev=335449&r1=335448&r2=335449&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestTextProto.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestTextProto.cpp Mon Jun 25 04:08:24 2018
@@ -700,5 +700,22 @@ TEST_F(FormatTestTextProto, PreventBreak
       "}");
 }
 
+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




More information about the cfe-commits mailing list