r195240 - Fix bug where optimization would lead to strange line breaks.
Manuel Klimek
klimek at google.com
Wed Nov 20 03:20:32 PST 2013
Author: klimek
Date: Wed Nov 20 05:20:32 2013
New Revision: 195240
URL: http://llvm.org/viewvc/llvm-project?rev=195240&view=rev
Log:
Fix bug where optimization would lead to strange line breaks.
Before:
void f() {
CHECK_EQ(aaaa, (
*bbbbbbbbb)->cccccc)
<< "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq";
}
After:
void f() {
CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)
<< "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq";
}
Modified:
cfe/trunk/lib/Format/ContinuationIndenter.cpp
cfe/trunk/lib/Format/ContinuationIndenter.h
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=195240&r1=195239&r2=195240&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Wed Nov 20 05:20:32 2013
@@ -224,13 +224,14 @@ unsigned ContinuationIndenter::addTokenT
if (Newline)
Penalty = addTokenOnNewLine(State, DryRun);
else
- addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
+ Penalty = addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
return moveStateToNextToken(State, DryRun, Newline) + Penalty;
}
-void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
- unsigned ExtraSpaces) {
+unsigned ContinuationIndenter::addTokenOnCurrentLine(LineState &State,
+ bool DryRun,
+ unsigned ExtraSpaces) {
FormatToken &Current = *State.NextToken;
const FormatToken &Previous = *State.NextToken->Previous;
if (Current.is(tok::equal) &&
@@ -249,6 +250,15 @@ void ContinuationIndenter::addTokenOnCur
State.Stack.back().LastSpace = State.Stack.back().VariablePos;
}
+ unsigned Penalty = 0;
+ // A break before a "<<" will get Style.PenaltyBreakFirstLessLess, so a
+ // continuation with "<<" has a smaller penalty in general.
+ // If the LHS is long, we don't want to penalize the break though, so we
+ // also add Style.PenaltyBreakFirstLessLess.
+ if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 &&
+ State.Column > Style.ColumnLimit / 2)
+ Penalty += Style.PenaltyBreakFirstLessLess;
+
unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
if (!DryRun)
@@ -307,6 +317,7 @@ void ContinuationIndenter::addTokenOnCur
State.Stack[State.Stack.size() - 2].CallContinuation == 0)
State.Stack.back().LastSpace = State.Column;
}
+ return Penalty;
}
unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
@@ -332,9 +343,8 @@ unsigned ContinuationIndenter::addTokenO
Penalty += State.NextToken->SplitPenalty;
// Breaking before the first "<<" is generally not desirable if the LHS is
- // short.
- if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 &&
- State.Column <= Style.ColumnLimit / 2)
+ // short (not breaking with a long LHS is penalized in addTokenOnCurrentLine).
+ if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
Penalty += Style.PenaltyBreakFirstLessLess;
if (Current.is(tok::l_brace) && Current.BlockKind == BK_Block) {
Modified: cfe/trunk/lib/Format/ContinuationIndenter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.h?rev=195240&r1=195239&r2=195240&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.h (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.h Wed Nov 20 05:20:32 2013
@@ -91,8 +91,8 @@ private:
///
/// If \p DryRun is \c false, also creates and stores the required
/// \c Replacement.
- void addTokenOnCurrentLine(LineState &State, bool DryRun,
- unsigned ExtraSpaces);
+ unsigned addTokenOnCurrentLine(LineState &State, bool DryRun,
+ unsigned ExtraSpaces);
/// \brief Appends the next token to \p State and updates information
/// necessary for indentation.
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=195240&r1=195239&r2=195240&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Nov 20 05:20:32 2013
@@ -3744,6 +3744,11 @@ TEST_F(FormatTest, AlignsPipes) {
EXPECT_EQ("llvm::errs() << \"\n"
" << a;",
format("llvm::errs() << \"\n<<a;"));
+
+ verifyFormat("void f() {\n"
+ " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
+ " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
+ "}");
}
TEST_F(FormatTest, UnderstandsEquals) {
More information about the cfe-commits
mailing list