diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -966,6 +966,10 @@ unsigned NextColumn = SourceMgr.getSpellingColumnNumber( Changes[j].OriginalWhitespaceRange.getEnd()); + // Handle files without newline before the end of file correct + if (Changes[j].Tok->is(tok::eof)) + NextColumn = 1; + // The start of the next token was previously aligned with the // start of this comment. WasAlignedWithStartOfNextLine = diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -17458,6 +17458,40 @@ Alignment); } +TEST_F(FormatTest, AlignTrailingComments) { + FormatStyle Style = getLLVMStyle(); + EXPECT_EQ(Style.AlignTrailingComments, true); + + verifyFormat("namespace test {\n" + "class C;\n" + "} // namespace test\n" + "// Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "Nullam cursus nunc\n" + "// non", + Style); + verifyFormat("namespace test {\n" + "class C;\n" + "} // namespace test\n" + "// Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "Nullam cursus nunc\n" + "// non\n", + Style); + verifyFormat("namespace test {\n" + "class C;\n" + "} // namespace test\n" + "// Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "Nullam cursus nunc\n" + "// non other text", + Style); + verifyFormat("namespace test {\n" + "class C;\n" + "} // namespace test\n" + "// Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "Nullam cursus nunc\n" + "// non other text\n", + Style); +} + TEST_F(FormatTest, AlignConsecutiveBitFields) { FormatStyle Alignment = getLLVMStyle(); Alignment.AlignConsecutiveBitFields.Enabled = true;