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() { 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"