Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1 | //===--- ContinuationIndenter.cpp - Format C++ code -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file implements the continuation indenter. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 15 | #include "BreakableToken.h" |
| 16 | #include "ContinuationIndenter.h" |
| 17 | #include "WhitespaceManager.h" |
| 18 | #include "clang/Basic/OperatorPrecedence.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | #include "clang/Format/Format.h" |
| 21 | #include "llvm/Support/Debug.h" |
| 22 | #include <string> |
| 23 | |
Chandler Carruth | 1034666 | 2014-04-22 03:17:02 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "format-formatter" |
| 25 | |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 26 | namespace clang { |
| 27 | namespace format { |
| 28 | |
| 29 | // Returns the length of everything up to the first possible line break after |
| 30 | // the ), ], } or > matching \c Tok. |
| 31 | static unsigned getLengthToMatchingParen(const FormatToken &Tok) { |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 32 | if (!Tok.MatchingParen) |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 33 | return 0; |
| 34 | FormatToken *End = Tok.MatchingParen; |
| 35 | while (End->Next && !End->Next->CanBreakBefore) { |
| 36 | End = End->Next; |
| 37 | } |
| 38 | return End->TotalLength - Tok.TotalLength + 1; |
| 39 | } |
| 40 | |
Daniel Jasper | 4c6e005 | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 41 | // Returns \c true if \c Tok is the "." or "->" of a call and starts the next |
| 42 | // segment of a builder type call. |
| 43 | static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) { |
| 44 | return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope(); |
| 45 | } |
| 46 | |
Daniel Jasper | ec01cd6 | 2013-10-08 05:11:18 +0000 | [diff] [blame] | 47 | // Returns \c true if \c Current starts a new parameter. |
| 48 | static bool startsNextParameter(const FormatToken &Current, |
| 49 | const FormatStyle &Style) { |
| 50 | const FormatToken &Previous = *Current.Previous; |
| 51 | if (Current.Type == TT_CtorInitializerComma && |
| 52 | Style.BreakConstructorInitializersBeforeComma) |
| 53 | return true; |
| 54 | return Previous.is(tok::comma) && !Current.isTrailingComment() && |
| 55 | (Previous.Type != TT_CtorInitializerComma || |
| 56 | !Style.BreakConstructorInitializersBeforeComma); |
| 57 | } |
| 58 | |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 59 | ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style, |
| 60 | SourceManager &SourceMgr, |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 61 | WhitespaceManager &Whitespaces, |
| 62 | encoding::Encoding Encoding, |
| 63 | bool BinPackInconclusiveFunctions) |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 64 | : Style(Style), SourceMgr(SourceMgr), Whitespaces(Whitespaces), |
| 65 | Encoding(Encoding), |
Alexander Kornienko | ce9161a | 2014-01-02 15:13:14 +0000 | [diff] [blame] | 66 | BinPackInconclusiveFunctions(BinPackInconclusiveFunctions), |
| 67 | CommentPragmasRegex(Style.CommentPragmas) {} |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 68 | |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 69 | LineState ContinuationIndenter::getInitialState(unsigned FirstIndent, |
Daniel Jasper | 1c5d9df | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 70 | const AnnotatedLine *Line, |
| 71 | bool DryRun) { |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 72 | LineState State; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 73 | State.FirstIndent = FirstIndent; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 74 | State.Column = FirstIndent; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 75 | State.Line = Line; |
| 76 | State.NextToken = Line->First; |
Alexander Kornienko | e2e0387 | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 77 | State.Stack.push_back(ParenState(FirstIndent, Line->Level, FirstIndent, |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 78 | /*AvoidBinPacking=*/false, |
| 79 | /*NoLineBreak=*/false)); |
| 80 | State.LineContainsContinuedForLoopSection = false; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 81 | State.StartOfStringLiteral = 0; |
Daniel Jasper | 05cd586 | 2014-05-08 12:21:30 +0000 | [diff] [blame] | 82 | State.StartOfLineLevel = 0; |
| 83 | State.LowestLevelOnLine = 0; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 84 | State.IgnoreStackForComparison = false; |
| 85 | |
| 86 | // The first token has already been indented and thus consumed. |
Daniel Jasper | 1c5d9df | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 87 | moveStateToNextToken(State, DryRun, /*Newline=*/false); |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 88 | return State; |
| 89 | } |
| 90 | |
| 91 | bool ContinuationIndenter::canBreak(const LineState &State) { |
| 92 | const FormatToken &Current = *State.NextToken; |
| 93 | const FormatToken &Previous = *Current.Previous; |
| 94 | assert(&Previous == Current.Previous); |
Daniel Jasper | 1db6c38 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 95 | if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace && |
| 96 | Current.closesBlockTypeList(Style))) |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 97 | return false; |
| 98 | // The opening "{" of a braced list has to be on the same line as the first |
| 99 | // element if it is nested in another braced init list or function call. |
| 100 | if (!Current.MustBreakBefore && Previous.is(tok::l_brace) && |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 101 | Previous.Type != TT_DictLiteral && Previous.BlockKind == BK_BracedInit && |
| 102 | Previous.Previous && |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 103 | Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma)) |
| 104 | return false; |
| 105 | // This prevents breaks like: |
| 106 | // ... |
| 107 | // SomeParameter, OtherParameter).DoSomething( |
| 108 | // ... |
| 109 | // As they hide "DoSomething" and are generally bad for readability. |
Daniel Jasper | 8f59ae5 | 2014-03-11 11:03:26 +0000 | [diff] [blame] | 110 | if (Previous.opensScope() && Previous.isNot(tok::l_brace) && |
| 111 | State.LowestLevelOnLine < State.StartOfLineLevel) |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 112 | return false; |
Daniel Jasper | 4c6e005 | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 113 | if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder) |
| 114 | return false; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 115 | return !State.Stack.back().NoLineBreak; |
| 116 | } |
| 117 | |
| 118 | bool ContinuationIndenter::mustBreak(const LineState &State) { |
| 119 | const FormatToken &Current = *State.NextToken; |
| 120 | const FormatToken &Previous = *Current.Previous; |
| 121 | if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon) |
| 122 | return true; |
Daniel Jasper | 1db6c38 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 123 | if (State.Stack.back().BreakBeforeClosingBrace && |
| 124 | Current.closesBlockTypeList(Style)) |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 125 | return true; |
| 126 | if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection) |
| 127 | return true; |
Daniel Jasper | ec01cd6 | 2013-10-08 05:11:18 +0000 | [diff] [blame] | 128 | if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) || |
Daniel Jasper | 165b29e | 2013-11-08 00:57:11 +0000 | [diff] [blame] | 129 | (Style.BreakBeforeTernaryOperators && |
| 130 | (Current.is(tok::question) || (Current.Type == TT_ConditionalExpr && |
| 131 | Previous.isNot(tok::question)))) || |
| 132 | (!Style.BreakBeforeTernaryOperators && |
| 133 | (Previous.is(tok::question) || Previous.Type == TT_ConditionalExpr))) && |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 134 | State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() && |
| 135 | !Current.isOneOf(tok::r_paren, tok::r_brace)) |
| 136 | return true; |
| 137 | if (Style.AlwaysBreakBeforeMultilineStrings && |
Daniel Jasper | f438cb7 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 138 | State.Column > State.Stack.back().Indent && // Breaking saves columns. |
Daniel Jasper | 2794305 | 2013-11-09 03:08:25 +0000 | [diff] [blame] | 139 | !Previous.isOneOf(tok::kw_return, tok::lessless, tok::at) && |
Daniel Jasper | c39b56f | 2013-12-16 07:23:08 +0000 | [diff] [blame] | 140 | Previous.Type != TT_InlineASMColon && nextIsMultilineString(State)) |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 141 | return true; |
Daniel Jasper | b596fb2 | 2013-10-24 10:31:50 +0000 | [diff] [blame] | 142 | if (((Previous.Type == TT_DictLiteral && Previous.is(tok::l_brace)) || |
Daniel Jasper | 1db6c38 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 143 | Previous.Type == TT_ArrayInitializerLSquare) && |
Daniel Jasper | db8804b | 2014-04-14 12:11:07 +0000 | [diff] [blame] | 144 | Style.ColumnLimit > 0 && |
Daniel Jasper | d489dd3 | 2013-10-20 16:45:46 +0000 | [diff] [blame] | 145 | getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State)) |
| 146 | return true; |
Daniel Jasper | 5d2587d | 2014-03-27 16:14:13 +0000 | [diff] [blame] | 147 | if (Current.Type == TT_CtorInitializerColon && |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 148 | ((Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All) || |
Daniel Jasper | 5d2587d | 2014-03-27 16:14:13 +0000 | [diff] [blame] | 149 | Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0)) |
| 150 | return true; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 151 | |
Daniel Jasper | 5d2587d | 2014-03-27 16:14:13 +0000 | [diff] [blame] | 152 | if (State.Column < getNewLineColumn(State)) |
| 153 | return false; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 154 | if (!Style.BreakBeforeBinaryOperators) { |
| 155 | // If we need to break somewhere inside the LHS of a binary expression, we |
| 156 | // should also break after the operator. Otherwise, the formatting would |
| 157 | // hide the operator precedence, e.g. in: |
| 158 | // if (aaaaaaaaaaaaaa == |
| 159 | // bbbbbbbbbbbbbb && c) {.. |
| 160 | // For comparisons, we only apply this rule, if the LHS is a binary |
| 161 | // expression itself as otherwise, the line breaks seem superfluous. |
| 162 | // We need special cases for ">>" which we have split into two ">" while |
| 163 | // lexing in order to make template parsing easier. |
| 164 | // |
| 165 | // FIXME: We'll need something similar for styles that break before binary |
| 166 | // operators. |
| 167 | bool IsComparison = (Previous.getPrecedence() == prec::Relational || |
| 168 | Previous.getPrecedence() == prec::Equality) && |
| 169 | Previous.Previous && |
| 170 | Previous.Previous->Type != TT_BinaryOperator; // For >>. |
| 171 | bool LHSIsBinaryExpr = |
Daniel Jasper | 562ecd4 | 2013-09-06 08:08:14 +0000 | [diff] [blame] | 172 | Previous.Previous && Previous.Previous->EndsBinaryExpression; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 173 | if (Previous.Type == TT_BinaryOperator && |
| 174 | (!IsComparison || LHSIsBinaryExpr) && |
| 175 | Current.Type != TT_BinaryOperator && // For >>. |
Daniel Jasper | c0d606a | 2014-04-14 11:08:45 +0000 | [diff] [blame] | 176 | !Current.isTrailingComment() && !Previous.is(tok::lessless) && |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 177 | Previous.getPrecedence() != prec::Assignment && |
| 178 | State.Stack.back().BreakBeforeParameter) |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | // Same as above, but for the first "<<" operator. |
Alexander Kornienko | 86b2dfd | 2014-03-06 15:13:08 +0000 | [diff] [blame] | 183 | if (Current.is(tok::lessless) && Current.Type != TT_OverloadedOperator && |
| 184 | State.Stack.back().BreakBeforeParameter && |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 185 | State.Stack.back().FirstLessLess == 0) |
| 186 | return true; |
| 187 | |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 188 | if (Current.Type == TT_ObjCSelectorName && |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 189 | State.Stack.back().ObjCSelectorNameFound && |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 190 | State.Stack.back().BreakBeforeParameter) |
| 191 | return true; |
Daniel Jasper | 05cd586 | 2014-05-08 12:21:30 +0000 | [diff] [blame] | 192 | if (Previous.ClosesTemplateDeclaration && Current.NestingLevel == 0 && |
Alexander Kornienko | a594ba8 | 2013-12-16 14:35:51 +0000 | [diff] [blame] | 193 | !Current.isTrailingComment()) |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 194 | return true; |
| 195 | |
| 196 | if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) && |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 197 | State.Line->MightBeFunctionDecl && |
Daniel Jasper | 05cd586 | 2014-05-08 12:21:30 +0000 | [diff] [blame] | 198 | State.Stack.back().BreakBeforeParameter && Current.NestingLevel == 0) |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 199 | return true; |
Daniel Jasper | 4c6e005 | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 200 | if (startsSegmentOfBuilderTypeCall(Current) && |
Daniel Jasper | f8151e9 | 2013-08-30 07:12:40 +0000 | [diff] [blame] | 201 | (State.Stack.back().CallContinuation != 0 || |
| 202 | (State.Stack.back().BreakBeforeParameter && |
| 203 | State.Stack.back().ContainsUnwrappedBuilder))) |
Daniel Jasper | 4c6e005 | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 204 | return true; |
Daniel Jasper | 9697281 | 2014-01-05 12:38:10 +0000 | [diff] [blame] | 205 | |
| 206 | // The following could be precomputed as they do not depend on the state. |
| 207 | // However, as they should take effect only if the UnwrappedLine does not fit |
| 208 | // into the ColumnLimit, they are checked here in the ContinuationIndenter. |
Daniel Jasper | 3599567 | 2014-04-29 14:05:20 +0000 | [diff] [blame] | 209 | if (Style.ColumnLimit != 0 && Previous.BlockKind == BK_Block && |
| 210 | Previous.is(tok::l_brace) && !Current.isOneOf(tok::r_brace, tok::comment)) |
Daniel Jasper | 9697281 | 2014-01-05 12:38:10 +0000 | [diff] [blame] | 211 | return true; |
Daniel Jasper | 9697281 | 2014-01-05 12:38:10 +0000 | [diff] [blame] | 212 | |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
| 216 | unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline, |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 217 | bool DryRun, |
| 218 | unsigned ExtraSpaces) { |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 219 | const FormatToken &Current = *State.NextToken; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 220 | |
Manuel Klimek | 819788d | 2014-03-18 11:22:45 +0000 | [diff] [blame] | 221 | assert(!State.Stack.empty()); |
| 222 | if ((Current.Type == TT_ImplicitStringLiteral && |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 223 | (Current.Previous->Tok.getIdentifierInfo() == nullptr || |
Daniel Jasper | 9885784 | 2013-10-30 13:54:53 +0000 | [diff] [blame] | 224 | Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() == |
| 225 | tok::pp_not_keyword))) { |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 226 | // FIXME: Is this correct? |
| 227 | int WhitespaceLength = SourceMgr.getSpellingColumnNumber( |
| 228 | State.NextToken->WhitespaceRange.getEnd()) - |
| 229 | SourceMgr.getSpellingColumnNumber( |
| 230 | State.NextToken->WhitespaceRange.getBegin()); |
Daniel Jasper | da353cd | 2014-03-12 08:24:47 +0000 | [diff] [blame] | 231 | State.Column += WhitespaceLength; |
Daniel Jasper | 240dfda | 2014-03-31 14:23:49 +0000 | [diff] [blame] | 232 | moveStateToNextToken(State, DryRun, /*Newline=*/false); |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 236 | unsigned Penalty = 0; |
| 237 | if (Newline) |
| 238 | Penalty = addTokenOnNewLine(State, DryRun); |
| 239 | else |
Daniel Jasper | 48437ce | 2013-11-20 14:54:39 +0000 | [diff] [blame] | 240 | addTokenOnCurrentLine(State, DryRun, ExtraSpaces); |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 241 | |
| 242 | return moveStateToNextToken(State, DryRun, Newline) + Penalty; |
| 243 | } |
| 244 | |
Daniel Jasper | 48437ce | 2013-11-20 14:54:39 +0000 | [diff] [blame] | 245 | void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, |
| 246 | unsigned ExtraSpaces) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 247 | FormatToken &Current = *State.NextToken; |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 248 | const FormatToken &Previous = *State.NextToken->Previous; |
| 249 | if (Current.is(tok::equal) && |
Daniel Jasper | 05cd586 | 2014-05-08 12:21:30 +0000 | [diff] [blame] | 250 | (State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) && |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 251 | State.Stack.back().VariablePos == 0) { |
| 252 | State.Stack.back().VariablePos = State.Column; |
| 253 | // Move over * and & if they are bound to the variable name. |
| 254 | const FormatToken *Tok = &Previous; |
| 255 | while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) { |
| 256 | State.Stack.back().VariablePos -= Tok->ColumnWidth; |
| 257 | if (Tok->SpacesRequiredBefore != 0) |
| 258 | break; |
| 259 | Tok = Tok->Previous; |
| 260 | } |
| 261 | if (Previous.PartOfMultiVariableDeclStmt) |
| 262 | State.Stack.back().LastSpace = State.Stack.back().VariablePos; |
| 263 | } |
| 264 | |
| 265 | unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces; |
| 266 | |
| 267 | if (!DryRun) |
| 268 | Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, /*IndentLevel=*/0, |
| 269 | Spaces, State.Column + Spaces); |
| 270 | |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 271 | if (Current.Type == TT_ObjCSelectorName && |
| 272 | !State.Stack.back().ObjCSelectorNameFound) { |
| 273 | if (Current.LongestObjCSelectorName == 0) |
| 274 | State.Stack.back().AlignColons = false; |
| 275 | else if (State.Stack.back().Indent + Current.LongestObjCSelectorName > |
| 276 | State.Column + Spaces + Current.ColumnWidth) |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 277 | State.Stack.back().ColonPos = |
| 278 | State.Stack.back().Indent + Current.LongestObjCSelectorName; |
| 279 | else |
| 280 | State.Stack.back().ColonPos = State.Column + Spaces + Current.ColumnWidth; |
| 281 | } |
| 282 | |
| 283 | if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr && |
Daniel Jasper | 5a61139 | 2013-12-19 21:41:37 +0000 | [diff] [blame] | 284 | (Current.Type != TT_LineComment || Previous.BlockKind == BK_BracedInit)) |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 285 | State.Stack.back().Indent = State.Column + Spaces; |
Daniel Jasper | ec01cd6 | 2013-10-08 05:11:18 +0000 | [diff] [blame] | 286 | if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style)) |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 287 | State.Stack.back().NoLineBreak = true; |
| 288 | if (startsSegmentOfBuilderTypeCall(Current)) |
| 289 | State.Stack.back().ContainsUnwrappedBuilder = true; |
| 290 | |
| 291 | State.Column += Spaces; |
Daniel Jasper | 8acf822 | 2014-05-07 09:23:05 +0000 | [diff] [blame] | 292 | if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) && |
| 293 | Previous.Previous && Previous.Previous->isOneOf(tok::kw_if, tok::kw_for)) |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 294 | // Treat the condition inside an if as if it was a second function |
Daniel Jasper | 6633ab8 | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 295 | // parameter, i.e. let nested calls have a continuation indent. |
Daniel Jasper | 8acf822 | 2014-05-07 09:23:05 +0000 | [diff] [blame] | 296 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 297 | else if (Current.isNot(tok::comment) && |
| 298 | (Previous.is(tok::comma) || |
| 299 | (Previous.is(tok::colon) && Previous.Type == TT_ObjCMethodExpr))) |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 300 | State.Stack.back().LastSpace = State.Column; |
| 301 | else if ((Previous.Type == TT_BinaryOperator || |
| 302 | Previous.Type == TT_ConditionalExpr || |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 303 | Previous.Type == TT_CtorInitializerColon) && |
Daniel Jasper | 0e61784 | 2014-04-16 12:26:54 +0000 | [diff] [blame] | 304 | ((Previous.getPrecedence() != prec::Assignment && |
| 305 | (Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 || |
| 306 | !Previous.LastOperator)) || |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 307 | Current.StartsBinaryExpression)) |
| 308 | // Always indent relative to the RHS of the expression unless this is a |
| 309 | // simple assignment without binary expression on the RHS. Also indent |
| 310 | // relative to unary operators and the colons of constructor initializers. |
| 311 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | f9a5e40 | 2013-10-08 16:24:07 +0000 | [diff] [blame] | 312 | else if (Previous.Type == TT_InheritanceColon) { |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 313 | State.Stack.back().Indent = State.Column; |
Daniel Jasper | f9a5e40 | 2013-10-08 16:24:07 +0000 | [diff] [blame] | 314 | State.Stack.back().LastSpace = State.Column; |
| 315 | } else if (Previous.opensScope()) { |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 316 | // If a function has a trailing call, indent all parameters from the |
| 317 | // opening parenthesis. This avoids confusing indents like: |
| 318 | // OuterFunction(InnerFunctionCall( // break |
| 319 | // ParameterToInnerFunction)) // break |
| 320 | // .SecondInnerFunctionCall(); |
| 321 | bool HasTrailingCall = false; |
| 322 | if (Previous.MatchingParen) { |
| 323 | const FormatToken *Next = Previous.MatchingParen->getNextNonComment(); |
| 324 | HasTrailingCall = Next && Next->isMemberAccess(); |
| 325 | } |
| 326 | if (HasTrailingCall && |
| 327 | State.Stack[State.Stack.size() - 2].CallContinuation == 0) |
| 328 | State.Stack.back().LastSpace = State.Column; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, |
| 333 | bool DryRun) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 334 | FormatToken &Current = *State.NextToken; |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 335 | const FormatToken &Previous = *State.NextToken->Previous; |
Daniel Jasper | 9f388d0 | 2014-03-27 14:33:30 +0000 | [diff] [blame] | 336 | |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 337 | // Extra penalty that needs to be added because of the way certain line |
| 338 | // breaks are chosen. |
| 339 | unsigned Penalty = 0; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 340 | |
Daniel Jasper | a040774 | 2014-02-11 10:08:11 +0000 | [diff] [blame] | 341 | const FormatToken *PreviousNonComment = Current.getPreviousNonComment(); |
| 342 | const FormatToken *NextNonComment = Previous.getNextNonComment(); |
| 343 | if (!NextNonComment) |
| 344 | NextNonComment = &Current; |
Daniel Jasper | 05cd586 | 2014-05-08 12:21:30 +0000 | [diff] [blame] | 345 | // The first line break on any NestingLevel causes an extra penalty in order |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 346 | // prefer similar line breaks. |
| 347 | if (!State.Stack.back().ContainsLineBreak) |
| 348 | Penalty += 15; |
| 349 | State.Stack.back().ContainsLineBreak = true; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 350 | |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 351 | Penalty += State.NextToken->SplitPenalty; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 352 | |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 353 | // Breaking before the first "<<" is generally not desirable if the LHS is |
Daniel Jasper | 48437ce | 2013-11-20 14:54:39 +0000 | [diff] [blame] | 354 | // short. Also always add the penalty if the LHS is split over mutliple lines |
Daniel Jasper | 2b7556e | 2014-04-03 12:00:27 +0000 | [diff] [blame] | 355 | // to avoid unnecessary line breaks that just work around this penalty. |
Daniel Jasper | a040774 | 2014-02-11 10:08:11 +0000 | [diff] [blame] | 356 | if (NextNonComment->is(tok::lessless) && |
| 357 | State.Stack.back().FirstLessLess == 0 && |
Daniel Jasper | 004177e | 2013-12-19 16:06:40 +0000 | [diff] [blame] | 358 | (State.Column <= Style.ColumnLimit / 3 || |
Daniel Jasper | 48437ce | 2013-11-20 14:54:39 +0000 | [diff] [blame] | 359 | State.Stack.back().BreakBeforeParameter)) |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 360 | Penalty += Style.PenaltyBreakFirstLessLess; |
| 361 | |
Daniel Jasper | 9f388d0 | 2014-03-27 14:33:30 +0000 | [diff] [blame] | 362 | State.Column = getNewLineColumn(State); |
| 363 | if (NextNonComment->isMemberAccess()) { |
| 364 | if (State.Stack.back().CallContinuation == 0) |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 365 | State.Stack.back().CallContinuation = State.Column; |
Daniel Jasper | a040774 | 2014-02-11 10:08:11 +0000 | [diff] [blame] | 366 | } else if (NextNonComment->Type == TT_ObjCSelectorName) { |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 367 | if (!State.Stack.back().ObjCSelectorNameFound) { |
Daniel Jasper | a040774 | 2014-02-11 10:08:11 +0000 | [diff] [blame] | 368 | if (NextNonComment->LongestObjCSelectorName == 0) { |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 369 | State.Stack.back().AlignColons = false; |
| 370 | } else { |
| 371 | State.Stack.back().ColonPos = |
Daniel Jasper | a040774 | 2014-02-11 10:08:11 +0000 | [diff] [blame] | 372 | State.Stack.back().Indent + NextNonComment->LongestObjCSelectorName; |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 373 | } |
Daniel Jasper | 9f388d0 | 2014-03-27 14:33:30 +0000 | [diff] [blame] | 374 | } else if (State.Stack.back().AlignColons && |
| 375 | State.Stack.back().ColonPos <= NextNonComment->ColumnWidth) { |
Daniel Jasper | a040774 | 2014-02-11 10:08:11 +0000 | [diff] [blame] | 376 | State.Stack.back().ColonPos = State.Column + NextNonComment->ColumnWidth; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 377 | } |
Daniel Jasper | 1fd6f1f | 2014-03-17 14:32:47 +0000 | [diff] [blame] | 378 | } else if (PreviousNonComment && PreviousNonComment->is(tok::colon) && |
| 379 | (PreviousNonComment->Type == TT_ObjCMethodExpr || |
| 380 | PreviousNonComment->Type == TT_DictLiteral)) { |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 381 | // FIXME: This is hacky, find a better way. The problem is that in an ObjC |
| 382 | // method expression, the block should be aligned to the line starting it, |
| 383 | // e.g.: |
| 384 | // [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason |
| 385 | // ^(int *i) { |
| 386 | // // ... |
| 387 | // }]; |
Daniel Jasper | 05cd586 | 2014-05-08 12:21:30 +0000 | [diff] [blame] | 388 | // Thus, we set LastSpace of the next higher NestingLevel, to which we move |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 389 | // when we consume all of the "}"'s FakeRParens at the "{". |
Daniel Jasper | 9a26e77 | 2013-12-23 11:25:40 +0000 | [diff] [blame] | 390 | if (State.Stack.size() > 1) |
Daniel Jasper | 9f388d0 | 2014-03-27 14:33:30 +0000 | [diff] [blame] | 391 | State.Stack[State.Stack.size() - 2].LastSpace = |
| 392 | std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + |
| 393 | Style.ContinuationIndentWidth; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 396 | if ((Previous.isOneOf(tok::comma, tok::semi) && |
| 397 | !State.Stack.back().AvoidBinPacking) || |
| 398 | Previous.Type == TT_BinaryOperator) |
| 399 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | 05cd586 | 2014-05-08 12:21:30 +0000 | [diff] [blame] | 400 | if (Previous.Type == TT_TemplateCloser && Current.NestingLevel == 0) |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 401 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | a040774 | 2014-02-11 10:08:11 +0000 | [diff] [blame] | 402 | if (NextNonComment->is(tok::question) || |
Daniel Jasper | 165b29e | 2013-11-08 00:57:11 +0000 | [diff] [blame] | 403 | (PreviousNonComment && PreviousNonComment->is(tok::question))) |
| 404 | State.Stack.back().BreakBeforeParameter = true; |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 405 | |
| 406 | if (!DryRun) { |
| 407 | unsigned Newlines = 1; |
| 408 | if (Current.is(tok::comment)) |
| 409 | Newlines = std::max(Newlines, std::min(Current.NewlinesBefore, |
| 410 | Style.MaxEmptyLinesToKeep + 1)); |
Alexander Kornienko | e2e0387 | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 411 | Whitespaces.replaceWhitespace(Current, Newlines, |
| 412 | State.Stack.back().IndentLevel, State.Column, |
| 413 | State.Column, State.Line->InPPDirective); |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | if (!Current.isTrailingComment()) |
| 417 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | 05cd586 | 2014-05-08 12:21:30 +0000 | [diff] [blame] | 418 | State.StartOfLineLevel = Current.NestingLevel; |
| 419 | State.LowestLevelOnLine = Current.NestingLevel; |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 420 | |
| 421 | // Any break on this level means that the parent level has been broken |
| 422 | // and we need to avoid bin packing there. |
Daniel Jasper | b16b969 | 2014-05-21 12:51:23 +0000 | [diff] [blame] | 423 | bool JavaScriptFormat = Style.Language == FormatStyle::LK_JavaScript && |
| 424 | Current.is(tok::r_brace) && |
| 425 | State.Stack.size() > 1 && |
| 426 | State.Stack[State.Stack.size() - 2].JSFunctionInlined; |
| 427 | if (!JavaScriptFormat) { |
| 428 | for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { |
| 429 | State.Stack[i].BreakBeforeParameter = true; |
| 430 | } |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 431 | } |
Daniel Jasper | b16b969 | 2014-05-21 12:51:23 +0000 | [diff] [blame] | 432 | |
Daniel Jasper | 9e5ede0 | 2013-11-08 19:56:28 +0000 | [diff] [blame] | 433 | if (PreviousNonComment && |
| 434 | !PreviousNonComment->isOneOf(tok::comma, tok::semi) && |
| 435 | PreviousNonComment->Type != TT_TemplateCloser && |
| 436 | PreviousNonComment->Type != TT_BinaryOperator && |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 437 | Current.Type != TT_BinaryOperator && !PreviousNonComment->opensScope()) |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 438 | State.Stack.back().BreakBeforeParameter = true; |
| 439 | |
Daniel Jasper | 1db6c38 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 440 | // If we break after { or the [ of an array initializer, we should also break |
| 441 | // before the corresponding } or ]. |
| 442 | if (Previous.is(tok::l_brace) || Previous.Type == TT_ArrayInitializerLSquare) |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 443 | State.Stack.back().BreakBeforeClosingBrace = true; |
| 444 | |
| 445 | if (State.Stack.back().AvoidBinPacking) { |
| 446 | // If we are breaking after '(', '{', '<', this is not bin packing |
Daniel Jasper | 2a95832 | 2014-05-21 13:26:58 +0000 | [diff] [blame] | 447 | // unless AllowAllParametersOfDeclarationOnNextLine is false or this is a |
| 448 | // dict/object literal. |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 449 | if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) || |
| 450 | Previous.Type == TT_BinaryOperator) || |
| 451 | (!Style.AllowAllParametersOfDeclarationOnNextLine && |
Daniel Jasper | 2a95832 | 2014-05-21 13:26:58 +0000 | [diff] [blame] | 452 | State.Line->MustBeDeclaration) || |
| 453 | Previous.Type == TT_DictLiteral) |
Alexander Kornienko | 1f80396 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 454 | State.Stack.back().BreakBeforeParameter = true; |
| 455 | } |
| 456 | |
| 457 | return Penalty; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Daniel Jasper | 9f388d0 | 2014-03-27 14:33:30 +0000 | [diff] [blame] | 460 | unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { |
Daniel Jasper | 5d2587d | 2014-03-27 16:14:13 +0000 | [diff] [blame] | 461 | if (!State.NextToken || !State.NextToken->Previous) |
| 462 | return 0; |
Daniel Jasper | 9f388d0 | 2014-03-27 14:33:30 +0000 | [diff] [blame] | 463 | FormatToken &Current = *State.NextToken; |
| 464 | const FormatToken &Previous = *State.NextToken->Previous; |
| 465 | // If we are continuing an expression, we want to use the continuation indent. |
| 466 | unsigned ContinuationIndent = |
| 467 | std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + |
| 468 | Style.ContinuationIndentWidth; |
| 469 | const FormatToken *PreviousNonComment = Current.getPreviousNonComment(); |
| 470 | const FormatToken *NextNonComment = Previous.getNextNonComment(); |
| 471 | if (!NextNonComment) |
| 472 | NextNonComment = &Current; |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 473 | if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block) |
Daniel Jasper | 05cd586 | 2014-05-08 12:21:30 +0000 | [diff] [blame] | 474 | return Current.NestingLevel == 0 ? State.FirstIndent |
| 475 | : State.Stack.back().Indent; |
Daniel Jasper | 9f388d0 | 2014-03-27 14:33:30 +0000 | [diff] [blame] | 476 | if (Current.isOneOf(tok::r_brace, tok::r_square)) { |
Daniel Jasper | b16b969 | 2014-05-21 12:51:23 +0000 | [diff] [blame] | 477 | if (State.Stack.size() > 1 && |
| 478 | State.Stack[State.Stack.size() - 2].JSFunctionInlined) |
| 479 | return State.FirstIndent; |
Daniel Jasper | 9f388d0 | 2014-03-27 14:33:30 +0000 | [diff] [blame] | 480 | if (Current.closesBlockTypeList(Style) || |
| 481 | (Current.MatchingParen && |
| 482 | Current.MatchingParen->BlockKind == BK_BracedInit)) |
| 483 | return State.Stack[State.Stack.size() - 2].LastSpace; |
| 484 | else |
| 485 | return State.FirstIndent; |
| 486 | } |
Daniel Jasper | 783bac6 | 2014-04-15 09:54:30 +0000 | [diff] [blame] | 487 | if (Current.is(tok::identifier) && Current.Next && |
| 488 | Current.Next->Type == TT_DictLiteral) |
| 489 | return State.Stack.back().Indent; |
Daniel Jasper | 9f388d0 | 2014-03-27 14:33:30 +0000 | [diff] [blame] | 490 | if (NextNonComment->isStringLiteral() && State.StartOfStringLiteral != 0) |
| 491 | return State.StartOfStringLiteral; |
| 492 | if (NextNonComment->is(tok::lessless) && |
| 493 | State.Stack.back().FirstLessLess != 0) |
| 494 | return State.Stack.back().FirstLessLess; |
| 495 | if (NextNonComment->isMemberAccess()) { |
| 496 | if (State.Stack.back().CallContinuation == 0) { |
| 497 | return ContinuationIndent; |
| 498 | } else { |
| 499 | return State.Stack.back().CallContinuation; |
| 500 | } |
| 501 | } |
| 502 | if (State.Stack.back().QuestionColumn != 0 && |
Daniel Jasper | c0d606a | 2014-04-14 11:08:45 +0000 | [diff] [blame] | 503 | ((NextNonComment->is(tok::colon) && |
| 504 | NextNonComment->Type == TT_ConditionalExpr) || |
Daniel Jasper | 9f388d0 | 2014-03-27 14:33:30 +0000 | [diff] [blame] | 505 | Previous.Type == TT_ConditionalExpr)) |
| 506 | return State.Stack.back().QuestionColumn; |
| 507 | if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) |
| 508 | return State.Stack.back().VariablePos; |
| 509 | if ((PreviousNonComment && (PreviousNonComment->ClosesTemplateDeclaration || |
| 510 | PreviousNonComment->Type == TT_AttributeParen)) || |
| 511 | ((NextNonComment->Type == TT_StartOfName || |
| 512 | NextNonComment->is(tok::kw_operator)) && |
Daniel Jasper | 05cd586 | 2014-05-08 12:21:30 +0000 | [diff] [blame] | 513 | Current.NestingLevel == 0 && |
| 514 | (!Style.IndentFunctionDeclarationAfterType || |
| 515 | State.Line->StartsDefinition))) |
Daniel Jasper | 9f388d0 | 2014-03-27 14:33:30 +0000 | [diff] [blame] | 516 | return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent); |
| 517 | if (NextNonComment->Type == TT_ObjCSelectorName) { |
| 518 | if (!State.Stack.back().ObjCSelectorNameFound) { |
| 519 | if (NextNonComment->LongestObjCSelectorName == 0) { |
| 520 | return State.Stack.back().Indent; |
| 521 | } else { |
| 522 | return State.Stack.back().Indent + |
| 523 | NextNonComment->LongestObjCSelectorName - |
| 524 | NextNonComment->ColumnWidth; |
| 525 | } |
| 526 | } else if (!State.Stack.back().AlignColons) { |
| 527 | return State.Stack.back().Indent; |
| 528 | } else if (State.Stack.back().ColonPos > NextNonComment->ColumnWidth) { |
| 529 | return State.Stack.back().ColonPos - NextNonComment->ColumnWidth; |
| 530 | } else { |
| 531 | return State.Stack.back().Indent; |
| 532 | } |
| 533 | } |
| 534 | if (NextNonComment->Type == TT_ArraySubscriptLSquare) { |
| 535 | if (State.Stack.back().StartOfArraySubscripts != 0) |
| 536 | return State.Stack.back().StartOfArraySubscripts; |
| 537 | else |
| 538 | return ContinuationIndent; |
| 539 | } |
| 540 | if (NextNonComment->Type == TT_StartOfName || |
| 541 | Previous.isOneOf(tok::coloncolon, tok::equal)) { |
| 542 | return ContinuationIndent; |
| 543 | } |
| 544 | if (PreviousNonComment && PreviousNonComment->is(tok::colon) && |
| 545 | (PreviousNonComment->Type == TT_ObjCMethodExpr || |
| 546 | PreviousNonComment->Type == TT_DictLiteral)) |
| 547 | return ContinuationIndent; |
| 548 | if (NextNonComment->Type == TT_CtorInitializerColon) |
| 549 | return State.FirstIndent + Style.ConstructorInitializerIndentWidth; |
| 550 | if (NextNonComment->Type == TT_CtorInitializerComma) |
| 551 | return State.Stack.back().Indent; |
Daniel Jasper | 5d2587d | 2014-03-27 16:14:13 +0000 | [diff] [blame] | 552 | if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment && |
Daniel Jasper | 9f388d0 | 2014-03-27 14:33:30 +0000 | [diff] [blame] | 553 | PreviousNonComment->isNot(tok::r_brace)) |
| 554 | // Ensure that we fall back to the continuation indent width instead of |
| 555 | // just flushing continuations left. |
| 556 | return State.Stack.back().Indent + Style.ContinuationIndentWidth; |
| 557 | return State.Stack.back().Indent; |
| 558 | } |
| 559 | |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 560 | unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, |
| 561 | bool DryRun, bool Newline) { |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 562 | assert(State.Stack.size()); |
Daniel Jasper | 60553be | 2014-05-26 13:10:39 +0000 | [diff] [blame] | 563 | const FormatToken &Current = *State.NextToken; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 564 | |
| 565 | if (Current.Type == TT_InheritanceColon) |
| 566 | State.Stack.back().AvoidBinPacking = true; |
Daniel Jasper | c0d606a | 2014-04-14 11:08:45 +0000 | [diff] [blame] | 567 | if (Current.is(tok::lessless) && Current.Type != TT_OverloadedOperator) { |
| 568 | if (State.Stack.back().FirstLessLess == 0) |
| 569 | State.Stack.back().FirstLessLess = State.Column; |
| 570 | else |
| 571 | State.Stack.back().LastOperatorWrapped = Newline; |
| 572 | } |
| 573 | if ((Current.Type == TT_BinaryOperator && Current.isNot(tok::lessless)) || |
| 574 | Current.Type == TT_ConditionalExpr) |
| 575 | State.Stack.back().LastOperatorWrapped = Newline; |
Daniel Jasper | 1db6c38 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 576 | if (Current.Type == TT_ArraySubscriptLSquare && |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 577 | State.Stack.back().StartOfArraySubscripts == 0) |
| 578 | State.Stack.back().StartOfArraySubscripts = State.Column; |
Daniel Jasper | 165b29e | 2013-11-08 00:57:11 +0000 | [diff] [blame] | 579 | if ((Current.is(tok::question) && Style.BreakBeforeTernaryOperators) || |
| 580 | (Current.getPreviousNonComment() && Current.isNot(tok::colon) && |
| 581 | Current.getPreviousNonComment()->is(tok::question) && |
| 582 | !Style.BreakBeforeTernaryOperators)) |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 583 | State.Stack.back().QuestionColumn = State.Column; |
| 584 | if (!Current.opensScope() && !Current.closesScope()) |
| 585 | State.LowestLevelOnLine = |
Daniel Jasper | 05cd586 | 2014-05-08 12:21:30 +0000 | [diff] [blame] | 586 | std::min(State.LowestLevelOnLine, Current.NestingLevel); |
Daniel Jasper | 4c6e005 | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 587 | if (Current.isMemberAccess()) |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 588 | State.Stack.back().StartOfFunctionCall = |
Daniel Jasper | 0e61784 | 2014-04-16 12:26:54 +0000 | [diff] [blame] | 589 | Current.LastOperator ? 0 : State.Column + Current.ColumnWidth; |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 590 | if (Current.Type == TT_ObjCSelectorName) |
| 591 | State.Stack.back().ObjCSelectorNameFound = true; |
Daniel Jasper | 3ae6f5a | 2014-04-09 12:08:39 +0000 | [diff] [blame] | 592 | if (Current.Type == TT_LambdaLSquare) |
| 593 | ++State.Stack.back().LambdasFound; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 594 | if (Current.Type == TT_CtorInitializerColon) { |
| 595 | // Indent 2 from the column, so: |
| 596 | // SomeClass::SomeClass() |
| 597 | // : First(...), ... |
| 598 | // Next(...) |
| 599 | // ^ line up here. |
| 600 | State.Stack.back().Indent = |
| 601 | State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2); |
| 602 | if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) |
| 603 | State.Stack.back().AvoidBinPacking = true; |
| 604 | State.Stack.back().BreakBeforeParameter = false; |
| 605 | } |
| 606 | |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 607 | // In ObjC method declaration we align on the ":" of parameters, but we need |
Daniel Jasper | 6633ab8 | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 608 | // to ensure that we indent parameters on subsequent lines by at least our |
| 609 | // continuation indent width. |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 610 | if (Current.Type == TT_ObjCMethodSpecifier) |
Daniel Jasper | 6633ab8 | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 611 | State.Stack.back().Indent += Style.ContinuationIndentWidth; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 612 | |
| 613 | // Insert scopes created by fake parenthesis. |
| 614 | const FormatToken *Previous = Current.getPreviousNonComment(); |
Daniel Jasper | b16b969 | 2014-05-21 12:51:23 +0000 | [diff] [blame] | 615 | |
| 616 | // Add special behavior to support a format commonly used for JavaScript |
| 617 | // closures: |
| 618 | // SomeFunction(function() { |
| 619 | // foo(); |
| 620 | // bar(); |
| 621 | // }, a, b, c); |
| 622 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 623 | if (Current.isNot(tok::comment) && Previous && Previous->is(tok::l_brace) && |
| 624 | State.Stack.size() > 1) { |
| 625 | if (State.Stack[State.Stack.size() - 2].JSFunctionInlined && Newline) { |
| 626 | for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { |
| 627 | State.Stack[i].NoLineBreak = true; |
| 628 | } |
| 629 | } |
| 630 | State.Stack[State.Stack.size() - 2].JSFunctionInlined = false; |
| 631 | } |
| 632 | if (Current.TokenText == "function") |
| 633 | State.Stack.back().JSFunctionInlined = !Newline; |
| 634 | } |
| 635 | |
Daniel Jasper | 60553be | 2014-05-26 13:10:39 +0000 | [diff] [blame] | 636 | moveStatePastFakeLParens(State, Newline); |
| 637 | moveStatePastScopeOpener(State, Newline); |
| 638 | moveStatePastScopeCloser(State); |
| 639 | moveStatePastFakeRParens(State); |
| 640 | |
| 641 | if (Current.isStringLiteral() && State.StartOfStringLiteral == 0) { |
| 642 | State.StartOfStringLiteral = State.Column; |
| 643 | } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) && |
| 644 | !Current.isStringLiteral()) { |
| 645 | State.StartOfStringLiteral = 0; |
| 646 | } |
| 647 | |
| 648 | State.Column += Current.ColumnWidth; |
| 649 | State.NextToken = State.NextToken->Next; |
| 650 | unsigned Penalty = breakProtrudingToken(Current, State, DryRun); |
| 651 | if (State.Column > getColumnLimit(State)) { |
| 652 | unsigned ExcessCharacters = State.Column - getColumnLimit(State); |
| 653 | Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; |
| 654 | } |
| 655 | |
| 656 | if (Current.Role) |
| 657 | Current.Role->formatFromToken(State, this, DryRun); |
| 658 | // If the previous has a special role, let it consume tokens as appropriate. |
| 659 | // It is necessary to start at the previous token for the only implemented |
| 660 | // role (comma separated list). That way, the decision whether or not to break |
| 661 | // after the "{" is already done and both options are tried and evaluated. |
| 662 | // FIXME: This is ugly, find a better way. |
| 663 | if (Previous && Previous->Role) |
| 664 | Penalty += Previous->Role->formatAfterToken(State, this, DryRun); |
| 665 | |
| 666 | return Penalty; |
| 667 | } |
| 668 | |
| 669 | void ContinuationIndenter::moveStatePastFakeLParens(LineState &State, |
| 670 | bool Newline) { |
| 671 | const FormatToken &Current = *State.NextToken; |
| 672 | const FormatToken *Previous = Current.getPreviousNonComment(); |
| 673 | |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 674 | // Don't add extra indentation for the first fake parenthesis after |
Dinesh Dwivedi | 0db806b | 2014-05-01 17:19:34 +0000 | [diff] [blame] | 675 | // 'return', assignments or opening <({[. The indentation for these cases |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 676 | // is special cased. |
| 677 | bool SkipFirstExtraIndent = |
Daniel Jasper | eabede6 | 2013-09-30 08:29:03 +0000 | [diff] [blame] | 678 | (Previous && (Previous->opensScope() || Previous->is(tok::kw_return) || |
Daniel Jasper | f48b5ab | 2013-11-07 19:23:49 +0000 | [diff] [blame] | 679 | Previous->getPrecedence() == prec::Assignment || |
| 680 | Previous->Type == TT_ObjCMethodExpr)); |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 681 | for (SmallVectorImpl<prec::Level>::const_reverse_iterator |
| 682 | I = Current.FakeLParens.rbegin(), |
| 683 | E = Current.FakeLParens.rend(); |
| 684 | I != E; ++I) { |
| 685 | ParenState NewParenState = State.Stack.back(); |
| 686 | NewParenState.ContainsLineBreak = false; |
Daniel Jasper | eabede6 | 2013-09-30 08:29:03 +0000 | [diff] [blame] | 687 | |
| 688 | // Indent from 'LastSpace' unless this the fake parentheses encapsulating a |
| 689 | // builder type call after 'return'. If such a call is line-wrapped, we |
| 690 | // commonly just want to indent from the start of the line. |
| 691 | if (!Previous || Previous->isNot(tok::kw_return) || *I > 0) |
| 692 | NewParenState.Indent = |
| 693 | std::max(std::max(State.Column, NewParenState.Indent), |
| 694 | State.Stack.back().LastSpace); |
| 695 | |
Daniel Jasper | 5c33265 | 2014-04-03 12:00:33 +0000 | [diff] [blame] | 696 | // Don't allow the RHS of an operator to be split over multiple lines unless |
| 697 | // there is a line-break right after the operator. |
| 698 | // Exclude relational operators, as there, it is always more desirable to |
| 699 | // have the LHS 'left' of the RHS. |
Daniel Jasper | c0d606a | 2014-04-14 11:08:45 +0000 | [diff] [blame] | 700 | if (Previous && Previous->getPrecedence() > prec::Assignment && |
| 701 | (Previous->Type == TT_BinaryOperator || |
| 702 | Previous->Type == TT_ConditionalExpr) && |
| 703 | Previous->getPrecedence() != prec::Relational) { |
| 704 | bool BreakBeforeOperator = Previous->is(tok::lessless) || |
| 705 | (Previous->Type == TT_BinaryOperator && |
| 706 | Style.BreakBeforeBinaryOperators) || |
| 707 | (Previous->Type == TT_ConditionalExpr && |
| 708 | Style.BreakBeforeTernaryOperators); |
| 709 | if ((!Newline && !BreakBeforeOperator) || |
| 710 | (!State.Stack.back().LastOperatorWrapped && BreakBeforeOperator)) |
| 711 | NewParenState.NoLineBreak = true; |
| 712 | } |
Daniel Jasper | 5c33265 | 2014-04-03 12:00:33 +0000 | [diff] [blame] | 713 | |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 714 | // Do not indent relative to the fake parentheses inserted for "." or "->". |
| 715 | // This is a special case to make the following to statements consistent: |
| 716 | // OuterFunction(InnerFunctionCall( // break |
| 717 | // ParameterToInnerFunction)); |
| 718 | // OuterFunction(SomeObject.InnerFunctionCall( // break |
| 719 | // ParameterToInnerFunction)); |
| 720 | if (*I > prec::Unknown) |
| 721 | NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column); |
Daniel Jasper | 9696435 | 2013-12-18 10:44:36 +0000 | [diff] [blame] | 722 | NewParenState.StartOfFunctionCall = State.Column; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 723 | |
| 724 | // Always indent conditional expressions. Never indent expression where |
| 725 | // the 'operator' is ',', ';' or an assignment (i.e. *I <= |
| 726 | // prec::Assignment) as those have different indentation rules. Indent |
| 727 | // other expression, unless the indentation needs to be skipped. |
| 728 | if (*I == prec::Conditional || |
| 729 | (!SkipFirstExtraIndent && *I > prec::Assignment && |
| 730 | !Style.BreakBeforeBinaryOperators)) |
Daniel Jasper | 6633ab8 | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 731 | NewParenState.Indent += Style.ContinuationIndentWidth; |
Daniel Jasper | 1db6c38 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 732 | if ((Previous && !Previous->opensScope()) || *I > prec::Comma) |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 733 | NewParenState.BreakBeforeParameter = false; |
| 734 | State.Stack.push_back(NewParenState); |
| 735 | SkipFirstExtraIndent = false; |
| 736 | } |
Daniel Jasper | 60553be | 2014-05-26 13:10:39 +0000 | [diff] [blame] | 737 | } |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 738 | |
Daniel Jasper | 335ff26 | 2014-05-28 09:11:53 +0000 | [diff] [blame^] | 739 | // Remove the fake r_parens after 'Tok'. |
| 740 | static void consumeRParens(LineState& State, const FormatToken &Tok) { |
| 741 | for (unsigned i = 0, e = Tok.FakeRParens; i != e; ++i) { |
| 742 | unsigned VariablePos = State.Stack.back().VariablePos; |
| 743 | assert(State.Stack.size() > 1); |
| 744 | if (State.Stack.size() == 1) { |
| 745 | // Do not pop the last element. |
| 746 | break; |
| 747 | } |
| 748 | State.Stack.pop_back(); |
| 749 | State.Stack.back().VariablePos = VariablePos; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | // Returns whether 'Tok' opens or closes a scope requiring special handling |
| 754 | // of the subsequent fake r_parens. |
| 755 | // |
| 756 | // For example, if this is an l_brace starting a nested block, we pretend (wrt. |
| 757 | // to indentation) that we already consumed the corresponding r_brace. Thus, we |
| 758 | // remove all ParenStates caused by fake parentheses that end at the r_brace. |
| 759 | // The net effect of this is that we don't indent relative to the l_brace, if |
| 760 | // the nested block is the last parameter of a function. This formats: |
| 761 | // |
| 762 | // SomeFunction(a, [] { |
| 763 | // f(); // break |
| 764 | // }); |
| 765 | // |
| 766 | // instead of: |
| 767 | // SomeFunction(a, [] { |
| 768 | // f(); // break |
| 769 | // }); |
| 770 | static bool fakeRParenSpecialCase(const FormatToken& Tok) { |
| 771 | if (!Tok.MatchingParen) |
| 772 | return false; |
| 773 | const FormatToken *Left = &Tok; |
| 774 | if (Tok.isOneOf(tok::r_brace, tok::r_square)) |
| 775 | Left = Tok.MatchingParen; |
| 776 | return Left->isOneOf(tok::l_brace, tok::l_square) && |
| 777 | (Left->BlockKind == BK_Block || |
| 778 | Left->Type == TT_ArrayInitializerLSquare || |
| 779 | Left->Type == TT_DictLiteral); |
| 780 | } |
| 781 | |
Daniel Jasper | 60553be | 2014-05-26 13:10:39 +0000 | [diff] [blame] | 782 | void ContinuationIndenter::moveStatePastFakeRParens(LineState &State) { |
| 783 | const FormatToken &Current = *State.NextToken; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 784 | |
Daniel Jasper | 335ff26 | 2014-05-28 09:11:53 +0000 | [diff] [blame^] | 785 | // Don't remove FakeRParens attached to r_braces that surround nested blocks |
| 786 | // as they will have been removed early (see above). |
| 787 | if (fakeRParenSpecialCase(Current)) |
| 788 | return; |
| 789 | |
| 790 | consumeRParens(State, *State.NextToken); |
Daniel Jasper | 60553be | 2014-05-26 13:10:39 +0000 | [diff] [blame] | 791 | } |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 792 | |
Daniel Jasper | 60553be | 2014-05-26 13:10:39 +0000 | [diff] [blame] | 793 | void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, |
| 794 | bool Newline) { |
| 795 | const FormatToken &Current = *State.NextToken; |
| 796 | if (!Current.opensScope()) |
| 797 | return; |
| 798 | |
| 799 | if (Current.MatchingParen && Current.BlockKind == BK_Block) { |
| 800 | moveStateToNewBlock(State); |
| 801 | return; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Daniel Jasper | 60553be | 2014-05-26 13:10:39 +0000 | [diff] [blame] | 804 | unsigned NewIndent; |
| 805 | unsigned NewIndentLevel = State.Stack.back().IndentLevel; |
| 806 | bool AvoidBinPacking; |
| 807 | bool BreakBeforeParameter = false; |
| 808 | if (Current.is(tok::l_brace) || Current.Type == TT_ArrayInitializerLSquare) { |
Daniel Jasper | 335ff26 | 2014-05-28 09:11:53 +0000 | [diff] [blame^] | 809 | if (fakeRParenSpecialCase(Current)) |
| 810 | consumeRParens(State, *Current.MatchingParen); |
| 811 | |
Daniel Jasper | 60553be | 2014-05-26 13:10:39 +0000 | [diff] [blame] | 812 | NewIndent = State.Stack.back().LastSpace; |
| 813 | if (Current.opensBlockTypeList(Style)) { |
| 814 | NewIndent += Style.IndentWidth; |
| 815 | NewIndent = std::min(State.Column + 2, NewIndent); |
| 816 | ++NewIndentLevel; |
| 817 | } else { |
| 818 | NewIndent += Style.ContinuationIndentWidth; |
| 819 | NewIndent = std::min(State.Column + 1, NewIndent); |
| 820 | } |
| 821 | const FormatToken *NextNoComment = Current.getNextNonComment(); |
| 822 | AvoidBinPacking = Current.Type == TT_ArrayInitializerLSquare || |
| 823 | Current.Type == TT_DictLiteral || |
| 824 | Style.Language == FormatStyle::LK_Proto || |
| 825 | !Style.BinPackParameters || |
| 826 | (NextNoComment && |
| 827 | NextNoComment->Type == TT_DesignatedInitializerPeriod); |
| 828 | } else { |
| 829 | NewIndent = Style.ContinuationIndentWidth + |
| 830 | std::max(State.Stack.back().LastSpace, |
| 831 | State.Stack.back().StartOfFunctionCall); |
| 832 | AvoidBinPacking = !Style.BinPackParameters || |
| 833 | (Style.ExperimentalAutoDetectBinPacking && |
| 834 | (Current.PackingKind == PPK_OnePerLine || |
| 835 | (!BinPackInconclusiveFunctions && |
| 836 | Current.PackingKind == PPK_Inconclusive))); |
| 837 | // If this '[' opens an ObjC call, determine whether all parameters fit |
| 838 | // into one line and put one per line if they don't. |
| 839 | if (Current.Type == TT_ObjCMethodExpr && Style.ColumnLimit != 0 && |
| 840 | getLengthToMatchingParen(Current) + State.Column > |
| 841 | getColumnLimit(State)) |
| 842 | BreakBeforeParameter = true; |
| 843 | } |
| 844 | bool NoLineBreak = State.Stack.back().NoLineBreak || |
| 845 | (Current.Type == TT_TemplateOpener && |
| 846 | State.Stack.back().ContainsUnwrappedBuilder); |
| 847 | State.Stack.push_back(ParenState(NewIndent, NewIndentLevel, |
| 848 | State.Stack.back().LastSpace, |
| 849 | AvoidBinPacking, NoLineBreak)); |
| 850 | State.Stack.back().BreakBeforeParameter = BreakBeforeParameter; |
| 851 | } |
| 852 | |
| 853 | void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) { |
| 854 | const FormatToken &Current = *State.NextToken; |
| 855 | if (!Current.closesScope()) |
| 856 | return; |
| 857 | |
| 858 | // If we encounter a closing ), ], } or >, we can remove a level from our |
| 859 | // stacks. |
| 860 | if (State.Stack.size() > 1 && |
| 861 | (Current.isOneOf(tok::r_paren, tok::r_square) || |
| 862 | (Current.is(tok::r_brace) && State.NextToken != State.Line->First) || |
Daniel Jasper | 335ff26 | 2014-05-28 09:11:53 +0000 | [diff] [blame^] | 863 | State.NextToken->Type == TT_TemplateCloser)) |
Daniel Jasper | 60553be | 2014-05-26 13:10:39 +0000 | [diff] [blame] | 864 | State.Stack.pop_back(); |
Daniel Jasper | 335ff26 | 2014-05-28 09:11:53 +0000 | [diff] [blame^] | 865 | |
Daniel Jasper | 60553be | 2014-05-26 13:10:39 +0000 | [diff] [blame] | 866 | if (Current.is(tok::r_square)) { |
| 867 | // If this ends the array subscript expr, reset the corresponding value. |
| 868 | const FormatToken *NextNonComment = Current.getNextNonComment(); |
| 869 | if (NextNonComment && NextNonComment->isNot(tok::l_square)) |
| 870 | State.Stack.back().StartOfArraySubscripts = 0; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | void ContinuationIndenter::moveStateToNewBlock(LineState &State) { |
Daniel Jasper | 60553be | 2014-05-26 13:10:39 +0000 | [diff] [blame] | 875 | // If we have already found more than one lambda introducers on this level, we |
| 876 | // opt out of this because similarity between the lambdas is more important. |
Daniel Jasper | 335ff26 | 2014-05-28 09:11:53 +0000 | [diff] [blame^] | 877 | // FIXME: This should use fakeRParenSpecialCase() and fakeRParenSpecialCase() |
| 878 | // Needs to include the LambdasFound check. Otherwise the corresponding |
| 879 | // fake r_parens will never be consumed. |
| 880 | if (State.Stack.back().LambdasFound <= 1) |
| 881 | consumeRParens(State, *State.NextToken->MatchingParen); |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 882 | |
Daniel Jasper | 60553be | 2014-05-26 13:10:39 +0000 | [diff] [blame] | 883 | // For some reason, ObjC blocks are indented like continuations. |
| 884 | unsigned NewIndent = State.Stack.back().LastSpace + |
| 885 | (State.NextToken->Type == TT_ObjCBlockLBrace |
| 886 | ? Style.ContinuationIndentWidth |
| 887 | : Style.IndentWidth); |
| 888 | State.Stack.push_back(ParenState( |
| 889 | NewIndent, /*NewIndentLevel=*/State.Stack.back().IndentLevel + 1, |
| 890 | State.Stack.back().LastSpace, /*AvoidBinPacking=*/true, |
| 891 | State.Stack.back().NoLineBreak)); |
| 892 | State.Stack.back().BreakBeforeParameter = true; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Alexander Kornienko | 917f9e0 | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 895 | unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current, |
| 896 | LineState &State) { |
Alexander Kornienko | d7b837e | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 897 | // Break before further function parameters on all levels. |
| 898 | for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) |
| 899 | State.Stack[i].BreakBeforeParameter = true; |
| 900 | |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 901 | unsigned ColumnsUsed = State.Column; |
Alexander Kornienko | 632abb9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 902 | // We can only affect layout of the first and the last line, so the penalty |
| 903 | // for all other lines is constant, and we ignore it. |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 904 | State.Column = Current.LastLineColumnWidth; |
Alexander Kornienko | 632abb9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 905 | |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 906 | if (ColumnsUsed > getColumnLimit(State)) |
| 907 | return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State)); |
Alexander Kornienko | d7b837e | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 908 | return 0; |
| 909 | } |
| 910 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 911 | static bool getRawStringLiteralPrefixPostfix(StringRef Text, StringRef &Prefix, |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 912 | StringRef &Postfix) { |
| 913 | if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") || |
| 914 | Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") || |
| 915 | Text.startswith(Prefix = "LR\"")) { |
| 916 | size_t ParenPos = Text.find('('); |
| 917 | if (ParenPos != StringRef::npos) { |
| 918 | StringRef Delimiter = |
| 919 | Text.substr(Prefix.size(), ParenPos - Prefix.size()); |
| 920 | Prefix = Text.substr(0, ParenPos + 1); |
| 921 | Postfix = Text.substr(Text.size() - 2 - Delimiter.size()); |
| 922 | return Postfix.front() == ')' && Postfix.back() == '"' && |
| 923 | Postfix.substr(1).startswith(Delimiter); |
| 924 | } |
| 925 | } |
| 926 | return false; |
| 927 | } |
| 928 | |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 929 | unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, |
| 930 | LineState &State, |
| 931 | bool DryRun) { |
Alexander Kornienko | 917f9e0 | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 932 | // Don't break multi-line tokens other than block comments. Instead, just |
| 933 | // update the state. |
| 934 | if (Current.Type != TT_BlockComment && Current.IsMultiline) |
| 935 | return addMultilineToken(Current, State); |
| 936 | |
Daniel Jasper | 9885784 | 2013-10-30 13:54:53 +0000 | [diff] [blame] | 937 | // Don't break implicit string literals. |
| 938 | if (Current.Type == TT_ImplicitStringLiteral) |
| 939 | return 0; |
| 940 | |
Daniel Jasper | 04b6a08 | 2013-12-20 06:22:01 +0000 | [diff] [blame] | 941 | if (!Current.isStringLiteral() && !Current.is(tok::comment)) |
Daniel Jasper | f93551c | 2013-08-23 10:05:49 +0000 | [diff] [blame] | 942 | return 0; |
| 943 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 944 | std::unique_ptr<BreakableToken> Token; |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 945 | unsigned StartColumn = State.Column - Current.ColumnWidth; |
Alexander Kornienko | 3abbb8a | 2013-11-12 17:30:49 +0000 | [diff] [blame] | 946 | unsigned ColumnLimit = getColumnLimit(State); |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 947 | |
Daniel Jasper | 04b6a08 | 2013-12-20 06:22:01 +0000 | [diff] [blame] | 948 | if (Current.isStringLiteral()) { |
Alexander Kornienko | 384b40b | 2013-10-11 21:43:05 +0000 | [diff] [blame] | 949 | // Don't break string literals inside preprocessor directives (except for |
| 950 | // #define directives, as their contents are stored in separate lines and |
| 951 | // are not affected by this check). |
| 952 | // This way we avoid breaking code with line directives and unknown |
| 953 | // preprocessor directives that contain long string literals. |
| 954 | if (State.Line->Type == LT_PreprocessorDirective) |
| 955 | return 0; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 956 | // Exempts unterminated string literals from line breaking. The user will |
| 957 | // likely want to terminate the string before any line breaking is done. |
| 958 | if (Current.IsUnterminatedLiteral) |
| 959 | return 0; |
| 960 | |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 961 | StringRef Text = Current.TokenText; |
| 962 | StringRef Prefix; |
| 963 | StringRef Postfix; |
Daniel Jasper | 174b012 | 2014-01-09 14:18:12 +0000 | [diff] [blame] | 964 | bool IsNSStringLiteral = false; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 965 | // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'. |
| 966 | // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to |
| 967 | // reduce the overhead) for each FormatToken, which is a string, so that we |
| 968 | // don't run multiple checks here on the hot path. |
Daniel Jasper | 174b012 | 2014-01-09 14:18:12 +0000 | [diff] [blame] | 969 | if (Text.startswith("\"") && Current.Previous && |
| 970 | Current.Previous->is(tok::at)) { |
| 971 | IsNSStringLiteral = true; |
| 972 | Prefix = "@\""; |
Daniel Jasper | 174b012 | 2014-01-09 14:18:12 +0000 | [diff] [blame] | 973 | } |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 974 | if ((Text.endswith(Postfix = "\"") && |
Daniel Jasper | 174b012 | 2014-01-09 14:18:12 +0000 | [diff] [blame] | 975 | (IsNSStringLiteral || Text.startswith(Prefix = "\"") || |
| 976 | Text.startswith(Prefix = "u\"") || Text.startswith(Prefix = "U\"") || |
| 977 | Text.startswith(Prefix = "u8\"") || |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 978 | Text.startswith(Prefix = "L\""))) || |
| 979 | (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")")) || |
| 980 | getRawStringLiteralPrefixPostfix(Text, Prefix, Postfix)) { |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 981 | Token.reset(new BreakableStringLiteral( |
| 982 | Current, State.Line->Level, StartColumn, Prefix, Postfix, |
| 983 | State.Line->InPPDirective, Encoding, Style)); |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 984 | } else { |
| 985 | return 0; |
| 986 | } |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 987 | } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) { |
Alexander Kornienko | ce9161a | 2014-01-02 15:13:14 +0000 | [diff] [blame] | 988 | if (CommentPragmasRegex.match(Current.TokenText.substr(2))) |
| 989 | return 0; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 990 | Token.reset(new BreakableBlockComment( |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 991 | Current, State.Line->Level, StartColumn, Current.OriginalColumn, |
| 992 | !Current.Previous, State.Line->InPPDirective, Encoding, Style)); |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 993 | } else if (Current.Type == TT_LineComment && |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 994 | (Current.Previous == nullptr || |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 995 | Current.Previous->Type != TT_ImplicitStringLiteral)) { |
Alexander Kornienko | ce9161a | 2014-01-02 15:13:14 +0000 | [diff] [blame] | 996 | if (CommentPragmasRegex.match(Current.TokenText.substr(2))) |
| 997 | return 0; |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 998 | Token.reset(new BreakableLineComment(Current, State.Line->Level, |
Alexander Kornienko | 3abbb8a | 2013-11-12 17:30:49 +0000 | [diff] [blame] | 999 | StartColumn, /*InPPDirective=*/false, |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 1000 | Encoding, Style)); |
Alexander Kornienko | 3abbb8a | 2013-11-12 17:30:49 +0000 | [diff] [blame] | 1001 | // We don't insert backslashes when breaking line comments. |
| 1002 | ColumnLimit = Style.ColumnLimit; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1003 | } else { |
| 1004 | return 0; |
| 1005 | } |
Alexander Kornienko | 3abbb8a | 2013-11-12 17:30:49 +0000 | [diff] [blame] | 1006 | if (Current.UnbreakableTailLength >= ColumnLimit) |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1007 | return 0; |
| 1008 | |
Alexander Kornienko | 3abbb8a | 2013-11-12 17:30:49 +0000 | [diff] [blame] | 1009 | unsigned RemainingSpace = ColumnLimit - Current.UnbreakableTailLength; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1010 | bool BreakInserted = false; |
| 1011 | unsigned Penalty = 0; |
| 1012 | unsigned RemainingTokenColumns = 0; |
| 1013 | for (unsigned LineIndex = 0, EndIndex = Token->getLineCount(); |
| 1014 | LineIndex != EndIndex; ++LineIndex) { |
| 1015 | if (!DryRun) |
| 1016 | Token->replaceWhitespaceBefore(LineIndex, Whitespaces); |
| 1017 | unsigned TailOffset = 0; |
| 1018 | RemainingTokenColumns = |
| 1019 | Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos); |
| 1020 | while (RemainingTokenColumns > RemainingSpace) { |
| 1021 | BreakableToken::Split Split = |
Alexander Kornienko | 3abbb8a | 2013-11-12 17:30:49 +0000 | [diff] [blame] | 1022 | Token->getSplit(LineIndex, TailOffset, ColumnLimit); |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1023 | if (Split.first == StringRef::npos) { |
| 1024 | // The last line's penalty is handled in addNextStateToQueue(). |
| 1025 | if (LineIndex < EndIndex - 1) |
| 1026 | Penalty += Style.PenaltyExcessCharacter * |
| 1027 | (RemainingTokenColumns - RemainingSpace); |
| 1028 | break; |
| 1029 | } |
| 1030 | assert(Split.first != 0); |
| 1031 | unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit( |
| 1032 | LineIndex, TailOffset + Split.first + Split.second, StringRef::npos); |
Alexander Kornienko | 875395f | 2013-11-12 17:50:13 +0000 | [diff] [blame] | 1033 | |
| 1034 | // We can remove extra whitespace instead of breaking the line. |
| 1035 | if (RemainingTokenColumns + 1 - Split.second <= RemainingSpace) { |
| 1036 | RemainingTokenColumns = 0; |
| 1037 | if (!DryRun) |
| 1038 | Token->replaceWhitespace(LineIndex, TailOffset, Split, Whitespaces); |
| 1039 | break; |
| 1040 | } |
| 1041 | |
Alexander Kornienko | 64a42b8 | 2014-04-15 14:52:43 +0000 | [diff] [blame] | 1042 | // When breaking before a tab character, it may be moved by a few columns, |
| 1043 | // but will still be expanded to the next tab stop, so we don't save any |
| 1044 | // columns. |
| 1045 | if (NewRemainingTokenColumns == RemainingTokenColumns) |
| 1046 | break; |
| 1047 | |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1048 | assert(NewRemainingTokenColumns < RemainingTokenColumns); |
| 1049 | if (!DryRun) |
| 1050 | Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces); |
Daniel Jasper | 2739af3 | 2013-08-28 10:03:58 +0000 | [diff] [blame] | 1051 | Penalty += Current.SplitPenalty; |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1052 | unsigned ColumnsUsed = |
| 1053 | Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first); |
Alexander Kornienko | 3abbb8a | 2013-11-12 17:30:49 +0000 | [diff] [blame] | 1054 | if (ColumnsUsed > ColumnLimit) { |
| 1055 | Penalty += Style.PenaltyExcessCharacter * (ColumnsUsed - ColumnLimit); |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1056 | } |
| 1057 | TailOffset += Split.first + Split.second; |
| 1058 | RemainingTokenColumns = NewRemainingTokenColumns; |
| 1059 | BreakInserted = true; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | State.Column = RemainingTokenColumns; |
| 1064 | |
| 1065 | if (BreakInserted) { |
| 1066 | // If we break the token inside a parameter list, we need to break before |
| 1067 | // the next parameter on all levels, so that the next parameter is clearly |
| 1068 | // visible. Line comments already introduce a break. |
| 1069 | if (Current.Type != TT_LineComment) { |
| 1070 | for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) |
| 1071 | State.Stack[i].BreakBeforeParameter = true; |
| 1072 | } |
| 1073 | |
Daniel Jasper | 04b6a08 | 2013-12-20 06:22:01 +0000 | [diff] [blame] | 1074 | Penalty += Current.isStringLiteral() ? Style.PenaltyBreakString |
| 1075 | : Style.PenaltyBreakComment; |
Daniel Jasper | 2739af3 | 2013-08-28 10:03:58 +0000 | [diff] [blame] | 1076 | |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1077 | State.Stack.back().LastSpace = StartColumn; |
| 1078 | } |
| 1079 | return Penalty; |
| 1080 | } |
| 1081 | |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1082 | unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const { |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1083 | // In preprocessor directives reserve two chars for trailing " \" |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1084 | return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0); |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Daniel Jasper | c39b56f | 2013-12-16 07:23:08 +0000 | [diff] [blame] | 1087 | bool ContinuationIndenter::nextIsMultilineString(const LineState &State) { |
Daniel Jasper | f438cb7 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 1088 | const FormatToken &Current = *State.NextToken; |
Daniel Jasper | 9509f18 | 2014-05-05 08:08:07 +0000 | [diff] [blame] | 1089 | if (!Current.isStringLiteral() || Current.Type == TT_ImplicitStringLiteral) |
Daniel Jasper | f438cb7 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 1090 | return false; |
Alexander Kornienko | d7b837e | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 1091 | // We never consider raw string literals "multiline" for the purpose of |
Daniel Jasper | c39b56f | 2013-12-16 07:23:08 +0000 | [diff] [blame] | 1092 | // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased |
| 1093 | // (see TokenAnnotator::mustBreakBefore(). |
Alexander Kornienko | d7b837e | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 1094 | if (Current.TokenText.startswith("R\"")) |
| 1095 | return false; |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 1096 | if (Current.IsMultiline) |
Alexander Kornienko | d7b837e | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 1097 | return true; |
Daniel Jasper | f438cb7 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 1098 | if (Current.getNextNonComment() && |
Daniel Jasper | 04b6a08 | 2013-12-20 06:22:01 +0000 | [diff] [blame] | 1099 | Current.getNextNonComment()->isStringLiteral()) |
Daniel Jasper | f438cb7 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 1100 | return true; // Implicit concatenation. |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 1101 | if (State.Column + Current.ColumnWidth + Current.UnbreakableTailLength > |
Daniel Jasper | f438cb7 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 1102 | Style.ColumnLimit) |
| 1103 | return true; // String will be split. |
Alexander Kornienko | d7b837e | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 1104 | return false; |
Daniel Jasper | f438cb7 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 1107 | } // namespace format |
| 1108 | } // namespace clang |