From: Daniel Jasper Date: Tue, 29 Jan 2013 15:03:01 +0000 (+0000) Subject: Calculate the split penalty upfront. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=adc6aba07293928122629e9d8a91a37144254831;p=clang Calculate the split penalty upfront. This is a) More efficient. b) Important as we move forward with further metrics for penalty. No functional changes intended. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173801 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 80298df844..f6d7ec42f3 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -108,6 +108,9 @@ public: /// \brief The total length of the line up to and including this token. unsigned TotalLength; + /// \brief Penalty for inserting a line break before this token. + unsigned SplitPenalty; + std::vector Children; AnnotatedToken *Parent; @@ -709,60 +712,6 @@ private: State.Column += Current.FormatTok.TokenLength; } - /// \brief Calculate the penalty for splitting after the token at \p Index. - unsigned splitPenalty(const AnnotatedToken &Tok) { - const AnnotatedToken &Left = Tok; - const AnnotatedToken &Right = Tok.Children[0]; - - if (Left.is(tok::l_brace) && Right.isNot(tok::l_brace)) - return 50; - if (Left.is(tok::equal) && Right.is(tok::l_brace)) - return 150; - if (Left.is(tok::coloncolon)) - return 500; - - if (Left.Type == TT_RangeBasedForLoopColon) - return 5; - - if (Right.is(tok::arrow) || Right.is(tok::period)) { - if (Left.is(tok::r_paren) && Line.Type == LT_BuilderTypeCall) - return 5; // Should be smaller than breaking at a nested comma. - return 150; - } - - // In for-loops, prefer breaking at ',' and ';'. - if (RootToken.is(tok::kw_for) && - (Left.isNot(tok::comma) && Left.isNot(tok::semi))) - return 20; - - if (Left.is(tok::semi) || Left.is(tok::comma)) - return 0; - - // In Objective-C method expressions, prefer breaking before "param:" over - // breaking after it. - if (isObjCSelectorName(Right)) - return 0; - if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr) - return 20; - - if (Left.is(tok::l_paren)) - return 20; - // FIXME: The penalty for a trailing "<" or "[" being higher than the - // penalty for a trainling "(" is a temporary workaround until we can - // properly avoid breaking in array subscripts or template parameters. - if (Left.is(tok::l_square) || Left.Type == TT_TemplateOpener) - return 50; - - if (Left.Type == TT_ConditionalExpr) - return prec::Assignment; - prec::Level Level = getPrecedence(Left); - - if (Level != prec::Unknown) - return Level; - - return 3; - } - unsigned getColumnLimit() { return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0); } @@ -811,7 +760,7 @@ private: unsigned CurrentPenalty = 0; if (NewLine) CurrentPenalty += Parameters.PenaltyIndentLevel * State.Stack.size() + - splitPenalty(*State.NextToken->Parent); + State.NextToken->SplitPenalty; addTokenToState(NewLine, true, State); @@ -1285,6 +1234,8 @@ public: Current.TotalLength = Current.Parent->TotalLength + Current.FormatTok.TokenLength + (Current.SpaceRequiredBefore ? 1 : 0); + if (Current.CanBreakBefore) + Current.SplitPenalty = splitPenalty(Current); if (!Current.Children.empty()) calculateExtraInformation(Current.Children[0]); } @@ -1316,6 +1267,60 @@ public: } private: + /// \brief Calculate the penalty for splitting before \c Tok. + unsigned splitPenalty(const AnnotatedToken &Tok) { + const AnnotatedToken &Left = *Tok.Parent; + const AnnotatedToken &Right = Tok; + + if (Left.is(tok::l_brace) && Right.isNot(tok::l_brace)) + return 50; + if (Left.is(tok::equal) && Right.is(tok::l_brace)) + return 150; + if (Left.is(tok::coloncolon)) + return 500; + + if (Left.Type == TT_RangeBasedForLoopColon) + return 5; + + if (Right.is(tok::arrow) || Right.is(tok::period)) { + if (Left.is(tok::r_paren) && Line.Type == LT_BuilderTypeCall) + return 5; // Should be smaller than breaking at a nested comma. + return 150; + } + + // In for-loops, prefer breaking at ',' and ';'. + if (Line.First.is(tok::kw_for) && + (Left.isNot(tok::comma) && Left.isNot(tok::semi))) + return 20; + + if (Left.is(tok::semi) || Left.is(tok::comma)) + return 0; + + // In Objective-C method expressions, prefer breaking before "param:" over + // breaking after it. + if (isObjCSelectorName(Right)) + return 0; + if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr) + return 20; + + if (Left.is(tok::l_paren)) + return 20; + // FIXME: The penalty for a trailing "<" or "[" being higher than the + // penalty for a trainling "(" is a temporary workaround until we can + // properly avoid breaking in array subscripts or template parameters. + if (Left.is(tok::l_square) || Left.Type == TT_TemplateOpener) + return 50; + + if (Left.Type == TT_ConditionalExpr) + return prec::Assignment; + prec::Level Level = getPrecedence(Left); + + if (Level != prec::Unknown) + return Level; + + return 3; + } + void determineTokenTypes(AnnotatedToken &Current, bool IsExpression, bool LookForFunctionName) { if (getPrecedence(Current) == prec::Assignment) {