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