From: Krasimir Georgiev Date: Fri, 3 Feb 2017 10:18:25 +0000 (+0000) Subject: [clang-format] Re-align broken comment lines where appropriate. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0b6e3eba6801a3995dd16fc50952fbff3537537e;p=clang [clang-format] Re-align broken comment lines where appropriate. Summary: The comment aligner was skipping over newly broken comment lines. This patch fixes that. source: ``` int ab; // line int a; // long long ``` format with column limit 15 before: ``` int ab; // line int a; // long // long ``` format with column limit 15 after: ``` int ab; // line int a; // long // long ``` Reviewers: djasper Reviewed By: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D29486 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@293997 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/WhitespaceManager.cpp b/lib/Format/WhitespaceManager.cpp index 3fd92eaf78..93a2e490c6 100644 --- a/lib/Format/WhitespaceManager.cpp +++ b/lib/Format/WhitespaceManager.cpp @@ -164,8 +164,9 @@ void WhitespaceManager::calculateLineBreakInformation() { const WhitespaceManager::Change *LastBlockComment = nullptr; for (auto &Change : Changes) { // Reset the IsTrailingComment flag for changes inside of trailing comments - // so they don't get realigned later. - if (Change.IsInsideToken) + // so they don't get realigned later. Comment line breaks however still need + // to be aligned. + if (Change.IsInsideToken && Change.NewlinesBefore == 0) Change.IsTrailingComment = false; Change.StartOfBlockComment = nullptr; Change.IndentationOffset = 0; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index d9f56e38f8..6cc9d1979f 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -11852,6 +11852,40 @@ TEST_F(FormatTest, AlignTrailingComments) { " // line 3\n" " b);", getLLVMStyleWithColumns(40))); + + // Align newly broken trailing comments. + EXPECT_EQ("int ab; // line\n" + "int a; // long\n" + " // long\n", + format("int ab; // line\n" + "int a; // long long\n", + getLLVMStyleWithColumns(15))); + EXPECT_EQ("int ab; // line\n" + "int a; // long\n" + " // long\n" + " // long", + format("int ab; // line\n" + "int a; // long long\n" + " // long", + getLLVMStyleWithColumns(15))); + EXPECT_EQ("int ab; // line\n" + "int a; // long\n" + " // long\n" + "pt c; // long", + format("int ab; // line\n" + "int a; // long long\n" + "pt c; // long", + getLLVMStyleWithColumns(15))); + EXPECT_EQ("int ab; // line\n" + "int a; // long\n" + " // long\n" + "\n" + "// long", + format("int ab; // line\n" + "int a; // long long\n" + "\n" + "// long", + getLLVMStyleWithColumns(15))); } } // end namespace } // end namespace format