[PATCH] D33394: [clang-format] Keep trailing preprocessor line comments separate from the following section comments

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 22 01:44:48 PDT 2017


krasimir created this revision.
Herald added a subscriber: klimek.

r303415 changed the way a sequence of line comments following a preprocessor
macro is handled, which has the unfortunate effect of aligning a trailing
preprocessor line comment and following unrelated section comments, so:

  #ifdef A // comment about A
  // section comment
  #endif

gets turned into:

  #ifdef A // comment about A
           // section comment
  #endif

This patch fixes this by additionally checking the original start columns of
the line comments.


https://reviews.llvm.org/D33394

Files:
  lib/Format/UnwrappedLineParser.cpp
  unittests/Format/FormatTestComments.cpp


Index: unittests/Format/FormatTestComments.cpp
===================================================================
--- unittests/Format/FormatTestComments.cpp
+++ unittests/Format/FormatTestComments.cpp
@@ -1020,6 +1020,24 @@
                    getLLVMStyleWithColumns(20)));
 }
 
+TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) {
+  verifyFormat("#ifdef A // line about A\n"
+               "// section comment\n"
+               "#endif",
+               getLLVMStyleWithColumns(80));
+  verifyFormat("int f() {\n"
+               "  int i;\n"
+               "#ifdef A // comment about A\n"
+               "  // section comment 1\n"
+               "  // section comment 2\n"
+               "  i = 2;\n"
+               "#else // comment about #else\n"
+               "  // section comment 3\n"
+               "  i = 4;\n"
+               "#endif\n"
+               "}", getLLVMStyleWithColumns(80));
+}
+
 TEST_F(FormatTestComments, CommentsInStaticInitializers) {
   EXPECT_EQ(
       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -102,7 +102,8 @@
   bool eof() {
     return Token && Token->HasUnescapedNewline &&
            !(PreviousToken && isLineComment(*PreviousToken) &&
-             isLineComment(*Token) && Token->NewlinesBefore == 1);
+             isLineComment(*Token) && Token->NewlinesBefore == 1 &&
+             Token->OriginalColumn == PreviousToken->OriginalColumn);
   }
 
   FormatToken *getFakeEOF() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33394.99722.patch
Type: text/x-patch
Size: 1675 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170522/4734ee52/attachment.bin>


More information about the cfe-commits mailing list