From: Alexander Kornienko Date: Mon, 1 Jul 2013 13:42:42 +0000 (+0000) Subject: Avoid column limit violation in block comments in certain cases. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d446f737f0c4840f25a371bfc9ebedab69e4027d;p=clang Avoid column limit violation in block comments in certain cases. Summary: Add penalty when an excessively long line in a block comment can not be broken on a leading whitespace. Lack of this addition can lead to severe column width violations when they can be easily avoided. Reviewers: djasper Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D1071 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185337 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 7b9950a43b..9cfb04162e 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -806,9 +806,10 @@ private: /// /// \returns An extra penalty if a token was broken, otherwise 0. /// - /// Note that the penalty of the token protruding the allowed line length is - /// already handled in \c addNextStateToQueue; the returned penalty will only - /// cover the cost of the additional line breaks. + /// The returned penalty will cover the cost of the additional line breaks and + /// column limit violation in all lines except for the last one. The penalty + /// for the column limit violation in the last line (and in single line + /// tokens) is handled in \c addNextStateToQueue. unsigned breakProtrudingToken(const FormatToken &Current, LineState &State, bool DryRun) { llvm::OwningPtr Token; @@ -854,8 +855,13 @@ private: while (RemainingTokenColumns > RemainingSpace) { BreakableToken::Split Split = Token->getSplit(LineIndex, TailOffset, getColumnLimit()); - if (Split.first == StringRef::npos) + if (Split.first == StringRef::npos) { + // The last line's penalty is handled in addNextStateToQueue(). + if (LineIndex < EndIndex - 1) + Penalty += Style.PenaltyExcessCharacter * + (RemainingTokenColumns - RemainingSpace); break; + } assert(Split.first != 0); unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit( LineIndex, TailOffset + Split.first + Split.second, diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 5227554d65..affec47121 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -844,6 +844,23 @@ TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); + EXPECT_EQ( + "void ffffffffffff(\n" + " int aaaaaaaa, int bbbbbbbb,\n" + " int cccccccccccc) { /*\n" + " aaaaaaaaaa\n" + " aaaaaaaaaaaaa\n" + " bbbbbbbbbbbbbb\n" + " bbbbbbbbbb\n" + " */\n" + "}", + format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" + "{ /*\n" + " aaaaaaaaaa aaaaaaaaaaaaa\n" + " bbbbbbbbbbbbbb bbbbbbbbbb\n" + " */\n" + "}", + getLLVMStyleWithColumns(40))); } TEST_F(FormatTest, SplitsLongCxxComments) {