From: Alexander Kornienko Date: Tue, 12 Nov 2013 17:30:49 +0000 (+0000) Subject: Do not insert backslashes, when breaking line comments after preprocessor directives. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5486a421a47a8922337aa83ac3b0fbb098dc1fe4;p=clang Do not insert backslashes, when breaking line comments after preprocessor directives. Summary: This solves http://llvm.org/PR17536 Reviewers: klimek, djasper Reviewed By: klimek CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D2142 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194491 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 4e1417cf4d..1e10ce3492 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -734,6 +734,7 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, llvm::OwningPtr Token; unsigned StartColumn = State.Column - Current.ColumnWidth; + unsigned ColumnLimit = getColumnLimit(State); if (Current.isOneOf(tok::string_literal, tok::wide_string_literal, tok::utf8_string_literal, tok::utf16_string_literal, @@ -778,16 +779,17 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, (Current.Previous == NULL || Current.Previous->Type != TT_ImplicitStringLiteral)) { Token.reset(new BreakableLineComment(Current, State.Line->Level, - StartColumn, State.Line->InPPDirective, + StartColumn, /*InPPDirective=*/false, Encoding, Style)); + // We don't insert backslashes when breaking line comments. + ColumnLimit = Style.ColumnLimit; } else { return 0; } - if (Current.UnbreakableTailLength >= getColumnLimit(State)) + if (Current.UnbreakableTailLength >= ColumnLimit) return 0; - unsigned RemainingSpace = - getColumnLimit(State) - Current.UnbreakableTailLength; + unsigned RemainingSpace = ColumnLimit - Current.UnbreakableTailLength; bool BreakInserted = false; unsigned Penalty = 0; unsigned RemainingTokenColumns = 0; @@ -800,7 +802,7 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos); while (RemainingTokenColumns > RemainingSpace) { BreakableToken::Split Split = - Token->getSplit(LineIndex, TailOffset, getColumnLimit(State)); + Token->getSplit(LineIndex, TailOffset, ColumnLimit); if (Split.first == StringRef::npos) { // The last line's penalty is handled in addNextStateToQueue(). if (LineIndex < EndIndex - 1) @@ -817,9 +819,8 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, Penalty += Current.SplitPenalty; unsigned ColumnsUsed = Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first); - if (ColumnsUsed > getColumnLimit(State)) { - Penalty += Style.PenaltyExcessCharacter * - (ColumnsUsed - getColumnLimit(State)); + if (ColumnsUsed > ColumnLimit) { + Penalty += Style.PenaltyExcessCharacter * (ColumnsUsed - ColumnLimit); } TailOffset += Split.first + Split.second; RemainingTokenColumns = NewRemainingTokenColumns; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index d02fb3405c..9f05619bb1 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1005,6 +1005,14 @@ TEST_F(FormatTest, SplitsLongCxxComments) { EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", getLLVMStyleWithColumns(20))); + EXPECT_EQ( + "#define XXX // a b c d\n" + " // e f g h", + format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); + EXPECT_EQ( + "#define XXX // q w e r\n" + " // t y u i", + format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); } TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {