r185822 - Prefer similar line breaks.
Daniel Jasper
djasper at google.com
Mon Jul 8 07:25:24 PDT 2013
Author: djasper
Date: Mon Jul 8 09:25:23 2013
New Revision: 185822
URL: http://llvm.org/viewvc/llvm-project?rev=185822&view=rev
Log:
Prefer similar line breaks.
This adds a penalty for clang-format for each break that occurs in
a set of parentheses (including fake parenthesis that determine
the range of certain operator precendences) that have not yet been
broken. Thereby, clang-format prefers similar line breaks.
This fixes llvm.org/PR15506.
Before:
const int kTrackingOptions =
NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited |
NSTrackingActiveAlways;
After:
const int kTrackingOptions = NSTrackingMouseMoved |
NSTrackingMouseEnteredAndExited |
NSTrackingActiveAlways;
Also removed ParenState::ForFakeParenthesis which has become unused.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=185822&r1=185821&r2=185822&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Jul 8 09:25:23 2013
@@ -140,7 +140,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.PenaltyBreakComment = 45;
LLVMStyle.PenaltyBreakString = 1000;
LLVMStyle.PenaltyExcessCharacter = 1000000;
- LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 75;
+ LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
LLVMStyle.PointerBindsToType = false;
LLVMStyle.SpacesBeforeTrailingComments = 1;
LLVMStyle.SpacesInBracedLists = true;
@@ -320,7 +320,7 @@ private:
AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
NoLineBreak(NoLineBreak), ColonPos(0), StartOfFunctionCall(0),
NestedNameSpecifierContinuation(0), CallContinuation(0),
- VariablePos(0), ForFakeParenthesis(false) {}
+ VariablePos(0), ContainsLineBreak(false) {}
/// \brief The position to which a specific parenthesis level needs to be
/// indented.
@@ -379,12 +379,12 @@ private:
/// Used to align further variables if necessary.
unsigned VariablePos;
- /// \brief \c true if this \c ParenState was created for a fake parenthesis.
+ /// \brief \c true if this \c ParenState already contains a line-break.
///
- /// Does not need to be considered for memoization / the comparison function
- /// as otherwise identical states will have the same fake/non-fake
- /// \c ParenStates.
- bool ForFakeParenthesis;
+ /// The first line break in a certain \c ParenState causes extra penalty so
+ /// that clang-format prefers similar breaks, i.e. breaks in the same
+ /// parenthesis.
+ bool ContainsLineBreak;
bool operator<(const ParenState &Other) const {
if (Indent != Other.Indent)
@@ -411,6 +411,8 @@ private:
return CallContinuation < Other.CallContinuation;
if (VariablePos != Other.VariablePos)
return VariablePos < Other.VariablePos;
+ if (ContainsLineBreak != Other.ContainsLineBreak)
+ return ContainsLineBreak < Other.ContainsLineBreak;
return false;
}
};
@@ -510,6 +512,7 @@ private:
unsigned ContinuationIndent =
std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4;
if (Newline) {
+ State.Stack.back().ContainsLineBreak = true;
if (Current.is(tok::r_brace)) {
State.Column = Line.Level * Style.IndentWidth;
} else if (Current.is(tok::string_literal) &&
@@ -728,7 +731,7 @@ private:
E = Current.FakeLParens.rend();
I != E; ++I) {
ParenState NewParenState = State.Stack.back();
- NewParenState.ForFakeParenthesis = true;
+ NewParenState.ContainsLineBreak = false;
NewParenState.Indent =
std::max(std::max(State.Column, NewParenState.Indent),
State.Stack.back().LastSpace);
@@ -1013,8 +1016,11 @@ private:
return;
if (!NewLine && mustBreak(PreviousNode->State))
return;
- if (NewLine)
+ if (NewLine) {
+ if (!PreviousNode->State.Stack.back().ContainsLineBreak)
+ Penalty += 15;
Penalty += PreviousNode->State.NextToken->SplitPenalty;
+ }
StateNode *Node = new (Allocator.Allocate())
StateNode(PreviousNode->State, NewLine, PreviousNode);
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=185822&r1=185821&r2=185822&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jul 8 09:25:23 2013
@@ -773,11 +773,11 @@ TEST_F(FormatTest, UnderstandsBlockComme
EXPECT_EQ(
"void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaa) { /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaa */\n"
+ " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
"}",
format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaa ,\n"
- " aaaaaaaaaaaaaaaaaa) { /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaa */\n"
+ " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
"}"));
FormatStyle NoBinPacking = getLLVMStyle();
@@ -2558,6 +2558,12 @@ TEST_F(FormatTest, BreaksDesireably) {
" x, y);",
format("f(g(h(a, // comment\n"
" b, c), d, e), x, y);"));
+
+ // Prefer breaking similar line breaks.
+ verifyFormat(
+ "const int kTrackingOptions = NSTrackingMouseMoved |\n"
+ " NSTrackingMouseEnteredAndExited |\n"
+ " NSTrackingActiveAlways;");
}
TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
@@ -2696,9 +2702,10 @@ TEST_F(FormatTest, AlignsAfterAssignment
verifyFormat(
"int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
" aaaaaaaaaaaaaaaaaaaaaaaaa);");
- verifyFormat("double LooooooooooooooooooooooooongResult =\n"
- " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n"
- " aaaaaaaaaaaaaaaaaaaaaaaa;");
+ verifyFormat(
+ "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaa;");
}
TEST_F(FormatTest, AlignsAfterReturn) {
@@ -2723,9 +2730,9 @@ TEST_F(FormatTest, AlignsAfterReturn) {
TEST_F(FormatTest, BreaksConditionalExpressions) {
verifyFormat(
- "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
+ "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat(
"aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
@@ -3000,9 +3007,9 @@ TEST_F(FormatTest, WrapsAtFunctionCallsI
verifyFormat(
"aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
- " .aaaaaaaaaaaaaaa(\n"
- " aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
+ " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
More information about the cfe-commits
mailing list