From 88cc562ad2d3e75280d453f824e89380622f4c9c Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 8 Jul 2013 14:25:23 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185822 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 24 +++++++++++++++--------- unittests/Format/FormatTest.cpp | 29 ++++++++++++++++++----------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 791035c307..0b0092d31f 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -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); diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index cff178eeb9..dd137aabd6 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -773,11 +773,11 @@ TEST_F(FormatTest, UnderstandsBlockComments) { 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, AlignsAfterAssignments) { 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, WrapsAtFunctionCallsIfNecessary) { 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" -- 2.40.0