Daniel Jasper | 6b2afe4 | 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 | |
| 15 | #define DEBUG_TYPE "format-formatter" |
| 16 | |
| 17 | #include "BreakableToken.h" |
| 18 | #include "ContinuationIndenter.h" |
| 19 | #include "WhitespaceManager.h" |
| 20 | #include "clang/Basic/OperatorPrecedence.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "clang/Format/Format.h" |
| 23 | #include "llvm/Support/Debug.h" |
| 24 | #include <string> |
| 25 | |
| 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) { |
| 32 | if (Tok.MatchingParen == NULL) |
| 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 | d3fef0f | 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 | 19ccb12 | 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 | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 59 | ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style, |
| 60 | SourceManager &SourceMgr, |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 61 | WhitespaceManager &Whitespaces, |
| 62 | encoding::Encoding Encoding, |
| 63 | bool BinPackInconclusiveFunctions) |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 64 | : Style(Style), SourceMgr(SourceMgr), Whitespaces(Whitespaces), |
| 65 | Encoding(Encoding), |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 66 | BinPackInconclusiveFunctions(BinPackInconclusiveFunctions) {} |
| 67 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 68 | LineState ContinuationIndenter::getInitialState(unsigned FirstIndent, |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 69 | const AnnotatedLine *Line, |
| 70 | bool DryRun) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 71 | LineState State; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 72 | State.FirstIndent = FirstIndent; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 73 | State.Column = FirstIndent; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 74 | State.Line = Line; |
| 75 | State.NextToken = Line->First; |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 76 | State.Stack.push_back(ParenState(FirstIndent, Line->Level, FirstIndent, |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 77 | /*AvoidBinPacking=*/false, |
| 78 | /*NoLineBreak=*/false)); |
| 79 | State.LineContainsContinuedForLoopSection = false; |
| 80 | State.ParenLevel = 0; |
| 81 | State.StartOfStringLiteral = 0; |
| 82 | State.StartOfLineLevel = State.ParenLevel; |
| 83 | State.LowestLevelOnLine = State.ParenLevel; |
| 84 | State.IgnoreStackForComparison = false; |
| 85 | |
| 86 | // The first token has already been indented and thus consumed. |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 87 | moveStateToNextToken(State, DryRun, /*Newline=*/false); |
Daniel Jasper | 6b2afe4 | 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 | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 95 | if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace && |
| 96 | Current.closesBlockTypeList(Style))) |
Daniel Jasper | 6b2afe4 | 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 | 3c6aea7 | 2013-10-24 10:31:50 +0000 | [diff] [blame] | 101 | Previous.Type != TT_DictLiteral && |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 102 | Previous.BlockKind == BK_BracedInit && Previous.Previous && |
Daniel Jasper | 6b2afe4 | 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. |
| 110 | if (Previous.opensScope() && State.LowestLevelOnLine < State.StartOfLineLevel) |
| 111 | return false; |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 112 | if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder) |
| 113 | return false; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 114 | return !State.Stack.back().NoLineBreak; |
| 115 | } |
| 116 | |
| 117 | bool ContinuationIndenter::mustBreak(const LineState &State) { |
| 118 | const FormatToken &Current = *State.NextToken; |
| 119 | const FormatToken &Previous = *Current.Previous; |
| 120 | if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon) |
| 121 | return true; |
Daniel Jasper | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 122 | if (State.Stack.back().BreakBeforeClosingBrace && |
| 123 | Current.closesBlockTypeList(Style)) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 124 | return true; |
| 125 | if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection) |
| 126 | return true; |
Daniel Jasper | 19ccb12 | 2013-10-08 05:11:18 +0000 | [diff] [blame] | 127 | if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) || |
| 128 | Current.is(tok::question) || |
| 129 | (Current.Type == TT_ConditionalExpr && Previous.isNot(tok::question))) && |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 130 | State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() && |
| 131 | !Current.isOneOf(tok::r_paren, tok::r_brace)) |
| 132 | return true; |
| 133 | if (Style.AlwaysBreakBeforeMultilineStrings && |
Daniel Jasper | 4df1ff9 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 134 | State.Column > State.Stack.back().Indent && // Breaking saves columns. |
Daniel Jasper | 49c77b2 | 2013-10-18 16:47:55 +0000 | [diff] [blame] | 135 | !Previous.isOneOf(tok::kw_return, tok::lessless) && |
| 136 | Previous.Type != TT_InlineASMColon && NextIsMultilineString(State)) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 137 | return true; |
Daniel Jasper | 3c6aea7 | 2013-10-24 10:31:50 +0000 | [diff] [blame] | 138 | if (((Previous.Type == TT_DictLiteral && Previous.is(tok::l_brace)) || |
Daniel Jasper | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 139 | Previous.Type == TT_ArrayInitializerLSquare) && |
Daniel Jasper | a53bbae | 2013-10-20 16:45:46 +0000 | [diff] [blame] | 140 | getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State)) |
| 141 | return true; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 142 | |
| 143 | if (!Style.BreakBeforeBinaryOperators) { |
| 144 | // If we need to break somewhere inside the LHS of a binary expression, we |
| 145 | // should also break after the operator. Otherwise, the formatting would |
| 146 | // hide the operator precedence, e.g. in: |
| 147 | // if (aaaaaaaaaaaaaa == |
| 148 | // bbbbbbbbbbbbbb && c) {.. |
| 149 | // For comparisons, we only apply this rule, if the LHS is a binary |
| 150 | // expression itself as otherwise, the line breaks seem superfluous. |
| 151 | // We need special cases for ">>" which we have split into two ">" while |
| 152 | // lexing in order to make template parsing easier. |
| 153 | // |
| 154 | // FIXME: We'll need something similar for styles that break before binary |
| 155 | // operators. |
| 156 | bool IsComparison = (Previous.getPrecedence() == prec::Relational || |
| 157 | Previous.getPrecedence() == prec::Equality) && |
| 158 | Previous.Previous && |
| 159 | Previous.Previous->Type != TT_BinaryOperator; // For >>. |
| 160 | bool LHSIsBinaryExpr = |
Daniel Jasper | db4813a | 2013-09-06 08:08:14 +0000 | [diff] [blame] | 161 | Previous.Previous && Previous.Previous->EndsBinaryExpression; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 162 | if (Previous.Type == TT_BinaryOperator && |
| 163 | (!IsComparison || LHSIsBinaryExpr) && |
| 164 | Current.Type != TT_BinaryOperator && // For >>. |
| 165 | !Current.isTrailingComment() && |
| 166 | !Previous.isOneOf(tok::lessless, tok::question) && |
| 167 | Previous.getPrecedence() != prec::Assignment && |
| 168 | State.Stack.back().BreakBeforeParameter) |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | // Same as above, but for the first "<<" operator. |
| 173 | if (Current.is(tok::lessless) && State.Stack.back().BreakBeforeParameter && |
| 174 | State.Stack.back().FirstLessLess == 0) |
| 175 | return true; |
| 176 | |
| 177 | // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding |
| 178 | // out whether it is the first parameter. Clean this up. |
| 179 | if (Current.Type == TT_ObjCSelectorName && |
| 180 | Current.LongestObjCSelectorName == 0 && |
| 181 | State.Stack.back().BreakBeforeParameter) |
| 182 | return true; |
| 183 | if ((Current.Type == TT_CtorInitializerColon || |
Daniel Jasper | 6e7f193 | 2013-10-09 15:06:17 +0000 | [diff] [blame] | 184 | (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0 && |
| 185 | !Current.isTrailingComment()))) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 186 | return true; |
| 187 | |
| 188 | if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) && |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 189 | State.Line->MightBeFunctionDecl && |
| 190 | State.Stack.back().BreakBeforeParameter && State.ParenLevel == 0) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 191 | return true; |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 192 | if (startsSegmentOfBuilderTypeCall(Current) && |
Daniel Jasper | eb33183 | 2013-08-30 07:12:40 +0000 | [diff] [blame] | 193 | (State.Stack.back().CallContinuation != 0 || |
| 194 | (State.Stack.back().BreakBeforeParameter && |
| 195 | State.Stack.back().ContainsUnwrappedBuilder))) |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 196 | return true; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | |
| 200 | unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline, |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 201 | bool DryRun, |
| 202 | unsigned ExtraSpaces) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 203 | const FormatToken &Current = *State.NextToken; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 204 | |
Daniel Jasper | 8437957 | 2013-10-30 13:54:53 +0000 | [diff] [blame] | 205 | if (State.Stack.size() == 0 || |
| 206 | (Current.Type == TT_ImplicitStringLiteral && |
| 207 | (Current.Previous->Tok.getIdentifierInfo() == NULL || |
| 208 | Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() == |
| 209 | tok::pp_not_keyword))) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 210 | // FIXME: Is this correct? |
| 211 | int WhitespaceLength = SourceMgr.getSpellingColumnNumber( |
| 212 | State.NextToken->WhitespaceRange.getEnd()) - |
| 213 | SourceMgr.getSpellingColumnNumber( |
| 214 | State.NextToken->WhitespaceRange.getBegin()); |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 215 | State.Column += WhitespaceLength + State.NextToken->ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 216 | State.NextToken = State.NextToken->Next; |
| 217 | return 0; |
| 218 | } |
| 219 | |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 220 | unsigned Penalty = 0; |
| 221 | if (Newline) |
| 222 | Penalty = addTokenOnNewLine(State, DryRun); |
| 223 | else |
| 224 | addTokenOnCurrentLine(State, DryRun, ExtraSpaces); |
| 225 | |
| 226 | return moveStateToNextToken(State, DryRun, Newline) + Penalty; |
| 227 | } |
| 228 | |
| 229 | void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, |
| 230 | unsigned ExtraSpaces) { |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 231 | FormatToken &Current = *State.NextToken; |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 232 | const FormatToken &Previous = *State.NextToken->Previous; |
| 233 | if (Current.is(tok::equal) && |
| 234 | (State.Line->First->is(tok::kw_for) || State.ParenLevel == 0) && |
| 235 | State.Stack.back().VariablePos == 0) { |
| 236 | State.Stack.back().VariablePos = State.Column; |
| 237 | // Move over * and & if they are bound to the variable name. |
| 238 | const FormatToken *Tok = &Previous; |
| 239 | while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) { |
| 240 | State.Stack.back().VariablePos -= Tok->ColumnWidth; |
| 241 | if (Tok->SpacesRequiredBefore != 0) |
| 242 | break; |
| 243 | Tok = Tok->Previous; |
| 244 | } |
| 245 | if (Previous.PartOfMultiVariableDeclStmt) |
| 246 | State.Stack.back().LastSpace = State.Stack.back().VariablePos; |
| 247 | } |
| 248 | |
| 249 | unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces; |
| 250 | |
| 251 | if (!DryRun) |
| 252 | Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, /*IndentLevel=*/0, |
| 253 | Spaces, State.Column + Spaces); |
| 254 | |
| 255 | if (Current.Type == TT_ObjCSelectorName && State.Stack.back().ColonPos == 0) { |
| 256 | if (State.Stack.back().Indent + Current.LongestObjCSelectorName > |
| 257 | State.Column + Spaces + Current.ColumnWidth) |
| 258 | State.Stack.back().ColonPos = |
| 259 | State.Stack.back().Indent + Current.LongestObjCSelectorName; |
| 260 | else |
| 261 | State.Stack.back().ColonPos = State.Column + Spaces + Current.ColumnWidth; |
| 262 | } |
| 263 | |
| 264 | if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr && |
| 265 | Current.Type != TT_LineComment) |
| 266 | State.Stack.back().Indent = State.Column + Spaces; |
Daniel Jasper | 19ccb12 | 2013-10-08 05:11:18 +0000 | [diff] [blame] | 267 | if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style)) |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 268 | State.Stack.back().NoLineBreak = true; |
| 269 | if (startsSegmentOfBuilderTypeCall(Current)) |
| 270 | State.Stack.back().ContainsUnwrappedBuilder = true; |
| 271 | |
| 272 | State.Column += Spaces; |
| 273 | if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for)) |
| 274 | // Treat the condition inside an if as if it was a second function |
Daniel Jasper | c2827ec | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 275 | // parameter, i.e. let nested calls have a continuation indent. |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 276 | State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(". |
| 277 | else if (Previous.is(tok::comma)) |
| 278 | State.Stack.back().LastSpace = State.Column; |
| 279 | else if ((Previous.Type == TT_BinaryOperator || |
| 280 | Previous.Type == TT_ConditionalExpr || |
| 281 | Previous.Type == TT_UnaryOperator || |
| 282 | Previous.Type == TT_CtorInitializerColon) && |
| 283 | (Previous.getPrecedence() != prec::Assignment || |
| 284 | Current.StartsBinaryExpression)) |
| 285 | // Always indent relative to the RHS of the expression unless this is a |
| 286 | // simple assignment without binary expression on the RHS. Also indent |
| 287 | // relative to unary operators and the colons of constructor initializers. |
| 288 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | cea014b | 2013-10-08 16:24:07 +0000 | [diff] [blame] | 289 | else if (Previous.Type == TT_InheritanceColon) { |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 290 | State.Stack.back().Indent = State.Column; |
Daniel Jasper | cea014b | 2013-10-08 16:24:07 +0000 | [diff] [blame] | 291 | State.Stack.back().LastSpace = State.Column; |
| 292 | } else if (Previous.opensScope()) { |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 293 | // If a function has a trailing call, indent all parameters from the |
| 294 | // opening parenthesis. This avoids confusing indents like: |
| 295 | // OuterFunction(InnerFunctionCall( // break |
| 296 | // ParameterToInnerFunction)) // break |
| 297 | // .SecondInnerFunctionCall(); |
| 298 | bool HasTrailingCall = false; |
| 299 | if (Previous.MatchingParen) { |
| 300 | const FormatToken *Next = Previous.MatchingParen->getNextNonComment(); |
| 301 | HasTrailingCall = Next && Next->isMemberAccess(); |
| 302 | } |
| 303 | if (HasTrailingCall && |
| 304 | State.Stack[State.Stack.size() - 2].CallContinuation == 0) |
| 305 | State.Stack.back().LastSpace = State.Column; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, |
| 310 | bool DryRun) { |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 311 | FormatToken &Current = *State.NextToken; |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 312 | const FormatToken &Previous = *State.NextToken->Previous; |
Daniel Jasper | c2827ec | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 313 | // If we are continuing an expression, we want to use the continuation indent. |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 314 | unsigned ContinuationIndent = |
Daniel Jasper | c2827ec | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 315 | std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + |
| 316 | Style.ContinuationIndentWidth; |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 317 | // Extra penalty that needs to be added because of the way certain line |
| 318 | // breaks are chosen. |
| 319 | unsigned Penalty = 0; |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 320 | |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 321 | const FormatToken *PreviousNonComment = |
| 322 | State.NextToken->getPreviousNonComment(); |
| 323 | // The first line break on any ParenLevel causes an extra penalty in order |
| 324 | // prefer similar line breaks. |
| 325 | if (!State.Stack.back().ContainsLineBreak) |
| 326 | Penalty += 15; |
| 327 | State.Stack.back().ContainsLineBreak = true; |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 328 | |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 329 | Penalty += State.NextToken->SplitPenalty; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 330 | |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 331 | // Breaking before the first "<<" is generally not desirable if the LHS is |
| 332 | // short. |
| 333 | if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 && |
| 334 | State.Column <= Style.ColumnLimit / 2) |
| 335 | Penalty += Style.PenaltyBreakFirstLessLess; |
| 336 | |
| 337 | if (Current.is(tok::l_brace) && Current.BlockKind == BK_Block) { |
| 338 | State.Column = State.FirstIndent; |
Daniel Jasper | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 339 | } else if (Current.isOneOf(tok::r_brace, tok::r_square)) { |
Daniel Jasper | 16a8b0e | 2013-11-07 14:02:28 +0000 | [diff] [blame] | 340 | if (Current.closesBlockTypeList(Style) || |
| 341 | (Current.MatchingParen && |
| 342 | Current.MatchingParen->BlockKind == BK_BracedInit)) |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 343 | State.Column = State.Stack[State.Stack.size() - 2].LastSpace; |
| 344 | else |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 345 | State.Column = State.FirstIndent; |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 346 | } else if (Current.is(tok::string_literal) && |
| 347 | State.StartOfStringLiteral != 0) { |
| 348 | State.Column = State.StartOfStringLiteral; |
| 349 | State.Stack.back().BreakBeforeParameter = true; |
| 350 | } else if (Current.is(tok::lessless) && |
| 351 | State.Stack.back().FirstLessLess != 0) { |
| 352 | State.Column = State.Stack.back().FirstLessLess; |
| 353 | } else if (Current.isMemberAccess()) { |
| 354 | if (State.Stack.back().CallContinuation == 0) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 355 | State.Column = ContinuationIndent; |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 356 | State.Stack.back().CallContinuation = State.Column; |
| 357 | } else { |
| 358 | State.Column = State.Stack.back().CallContinuation; |
| 359 | } |
| 360 | } else if (Current.Type == TT_ConditionalExpr) { |
| 361 | State.Column = State.Stack.back().QuestionColumn; |
| 362 | } else if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) { |
| 363 | State.Column = State.Stack.back().VariablePos; |
| 364 | } else if ((PreviousNonComment && |
| 365 | PreviousNonComment->ClosesTemplateDeclaration) || |
| 366 | ((Current.Type == TT_StartOfName || |
| 367 | Current.is(tok::kw_operator)) && |
| 368 | State.ParenLevel == 0 && |
| 369 | (!Style.IndentFunctionDeclarationAfterType || |
| 370 | State.Line->StartsDefinition))) { |
| 371 | State.Column = State.Stack.back().Indent; |
| 372 | } else if (Current.Type == TT_ObjCSelectorName) { |
| 373 | if (State.Stack.back().ColonPos > Current.ColumnWidth) { |
| 374 | State.Column = State.Stack.back().ColonPos - Current.ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 375 | } else { |
| 376 | State.Column = State.Stack.back().Indent; |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 377 | State.Stack.back().ColonPos = State.Column + Current.ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 378 | } |
Daniel Jasper | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 379 | } else if (Current.Type == TT_ArraySubscriptLSquare) { |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 380 | if (State.Stack.back().StartOfArraySubscripts != 0) |
| 381 | State.Column = State.Stack.back().StartOfArraySubscripts; |
| 382 | else |
| 383 | State.Column = ContinuationIndent; |
| 384 | } else if (Current.Type == TT_StartOfName || |
| 385 | Previous.isOneOf(tok::coloncolon, tok::equal) || |
| 386 | Previous.Type == TT_ObjCMethodExpr) { |
| 387 | State.Column = ContinuationIndent; |
| 388 | } else if (Current.Type == TT_CtorInitializerColon) { |
| 389 | State.Column = State.FirstIndent + Style.ConstructorInitializerIndentWidth; |
| 390 | } else if (Current.Type == TT_CtorInitializerComma) { |
| 391 | State.Column = State.Stack.back().Indent; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 392 | } else { |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 393 | State.Column = State.Stack.back().Indent; |
Daniel Jasper | c2827ec | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 394 | // Ensure that we fall back to the continuation indent width instead of just |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 395 | // flushing continuations left. |
Daniel Jasper | dc837b1 | 2013-10-30 14:04:10 +0000 | [diff] [blame] | 396 | if (State.Column == State.FirstIndent && |
| 397 | PreviousNonComment->isNot(tok::r_brace)) |
Daniel Jasper | c2827ec | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 398 | State.Column += Style.ContinuationIndentWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 401 | if (Current.is(tok::question)) |
| 402 | State.Stack.back().BreakBeforeParameter = true; |
| 403 | if ((Previous.isOneOf(tok::comma, tok::semi) && |
| 404 | !State.Stack.back().AvoidBinPacking) || |
| 405 | Previous.Type == TT_BinaryOperator) |
| 406 | State.Stack.back().BreakBeforeParameter = false; |
| 407 | if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0) |
| 408 | State.Stack.back().BreakBeforeParameter = false; |
| 409 | |
| 410 | if (!DryRun) { |
| 411 | unsigned Newlines = 1; |
| 412 | if (Current.is(tok::comment)) |
| 413 | Newlines = std::max(Newlines, std::min(Current.NewlinesBefore, |
| 414 | Style.MaxEmptyLinesToKeep + 1)); |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 415 | Whitespaces.replaceWhitespace(Current, Newlines, |
| 416 | State.Stack.back().IndentLevel, State.Column, |
| 417 | State.Column, State.Line->InPPDirective); |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | if (!Current.isTrailingComment()) |
| 421 | State.Stack.back().LastSpace = State.Column; |
| 422 | if (Current.isMemberAccess()) |
| 423 | State.Stack.back().LastSpace += Current.ColumnWidth; |
| 424 | State.StartOfLineLevel = State.ParenLevel; |
| 425 | State.LowestLevelOnLine = State.ParenLevel; |
| 426 | |
| 427 | // Any break on this level means that the parent level has been broken |
| 428 | // and we need to avoid bin packing there. |
| 429 | for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { |
| 430 | State.Stack[i].BreakBeforeParameter = true; |
| 431 | } |
| 432 | const FormatToken *TokenBefore = Current.getPreviousNonComment(); |
| 433 | if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) && |
| 434 | TokenBefore->Type != TT_TemplateCloser && |
| 435 | TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope()) |
| 436 | State.Stack.back().BreakBeforeParameter = true; |
| 437 | |
Daniel Jasper | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 438 | // If we break after { or the [ of an array initializer, we should also break |
| 439 | // before the corresponding } or ]. |
| 440 | if (Previous.is(tok::l_brace) || Previous.Type == TT_ArrayInitializerLSquare) |
Alexander Kornienko | e5321c4 | 2013-10-01 14:41:18 +0000 | [diff] [blame] | 441 | State.Stack.back().BreakBeforeClosingBrace = true; |
| 442 | |
| 443 | if (State.Stack.back().AvoidBinPacking) { |
| 444 | // If we are breaking after '(', '{', '<', this is not bin packing |
| 445 | // unless AllowAllParametersOfDeclarationOnNextLine is false. |
| 446 | if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) || |
| 447 | Previous.Type == TT_BinaryOperator) || |
| 448 | (!Style.AllowAllParametersOfDeclarationOnNextLine && |
| 449 | State.Line->MustBeDeclaration)) |
| 450 | State.Stack.back().BreakBeforeParameter = true; |
| 451 | } |
| 452 | |
| 453 | return Penalty; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, |
| 457 | bool DryRun, bool Newline) { |
| 458 | const FormatToken &Current = *State.NextToken; |
| 459 | assert(State.Stack.size()); |
| 460 | |
| 461 | if (Current.Type == TT_InheritanceColon) |
| 462 | State.Stack.back().AvoidBinPacking = true; |
| 463 | if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0) |
| 464 | State.Stack.back().FirstLessLess = State.Column; |
Daniel Jasper | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 465 | if (Current.Type == TT_ArraySubscriptLSquare && |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 466 | State.Stack.back().StartOfArraySubscripts == 0) |
| 467 | State.Stack.back().StartOfArraySubscripts = State.Column; |
| 468 | if (Current.is(tok::question)) |
| 469 | State.Stack.back().QuestionColumn = State.Column; |
| 470 | if (!Current.opensScope() && !Current.closesScope()) |
| 471 | State.LowestLevelOnLine = |
| 472 | std::min(State.LowestLevelOnLine, State.ParenLevel); |
Daniel Jasper | d3fef0f | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 473 | if (Current.isMemberAccess()) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 474 | State.Stack.back().StartOfFunctionCall = |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 475 | Current.LastInChainOfCalls ? 0 : State.Column + Current.ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 476 | if (Current.Type == TT_CtorInitializerColon) { |
| 477 | // Indent 2 from the column, so: |
| 478 | // SomeClass::SomeClass() |
| 479 | // : First(...), ... |
| 480 | // Next(...) |
| 481 | // ^ line up here. |
| 482 | State.Stack.back().Indent = |
| 483 | State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2); |
| 484 | if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) |
| 485 | State.Stack.back().AvoidBinPacking = true; |
| 486 | State.Stack.back().BreakBeforeParameter = false; |
| 487 | } |
| 488 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 489 | // In ObjC method declaration we align on the ":" of parameters, but we need |
Daniel Jasper | c2827ec | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 490 | // to ensure that we indent parameters on subsequent lines by at least our |
| 491 | // continuation indent width. |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 492 | if (Current.Type == TT_ObjCMethodSpecifier) |
Daniel Jasper | c2827ec | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 493 | State.Stack.back().Indent += Style.ContinuationIndentWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 494 | |
| 495 | // Insert scopes created by fake parenthesis. |
| 496 | const FormatToken *Previous = Current.getPreviousNonComment(); |
| 497 | // Don't add extra indentation for the first fake parenthesis after |
| 498 | // 'return', assignements or opening <({[. The indentation for these cases |
| 499 | // is special cased. |
| 500 | bool SkipFirstExtraIndent = |
Daniel Jasper | f78bf4a | 2013-09-30 08:29:03 +0000 | [diff] [blame] | 501 | (Previous && (Previous->opensScope() || Previous->is(tok::kw_return) || |
Daniel Jasper | 966e6d3 | 2013-11-07 19:23:49 +0000 | [diff] [blame^] | 502 | Previous->getPrecedence() == prec::Assignment || |
| 503 | Previous->Type == TT_ObjCMethodExpr)); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 504 | for (SmallVectorImpl<prec::Level>::const_reverse_iterator |
| 505 | I = Current.FakeLParens.rbegin(), |
| 506 | E = Current.FakeLParens.rend(); |
| 507 | I != E; ++I) { |
| 508 | ParenState NewParenState = State.Stack.back(); |
| 509 | NewParenState.ContainsLineBreak = false; |
Daniel Jasper | f78bf4a | 2013-09-30 08:29:03 +0000 | [diff] [blame] | 510 | |
| 511 | // Indent from 'LastSpace' unless this the fake parentheses encapsulating a |
| 512 | // builder type call after 'return'. If such a call is line-wrapped, we |
| 513 | // commonly just want to indent from the start of the line. |
| 514 | if (!Previous || Previous->isNot(tok::kw_return) || *I > 0) |
| 515 | NewParenState.Indent = |
| 516 | std::max(std::max(State.Column, NewParenState.Indent), |
| 517 | State.Stack.back().LastSpace); |
| 518 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 519 | // Do not indent relative to the fake parentheses inserted for "." or "->". |
| 520 | // This is a special case to make the following to statements consistent: |
| 521 | // OuterFunction(InnerFunctionCall( // break |
| 522 | // ParameterToInnerFunction)); |
| 523 | // OuterFunction(SomeObject.InnerFunctionCall( // break |
| 524 | // ParameterToInnerFunction)); |
| 525 | if (*I > prec::Unknown) |
| 526 | NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 527 | |
| 528 | // Always indent conditional expressions. Never indent expression where |
| 529 | // the 'operator' is ',', ';' or an assignment (i.e. *I <= |
| 530 | // prec::Assignment) as those have different indentation rules. Indent |
| 531 | // other expression, unless the indentation needs to be skipped. |
| 532 | if (*I == prec::Conditional || |
| 533 | (!SkipFirstExtraIndent && *I > prec::Assignment && |
| 534 | !Style.BreakBeforeBinaryOperators)) |
Daniel Jasper | c2827ec | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 535 | NewParenState.Indent += Style.ContinuationIndentWidth; |
Daniel Jasper | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 536 | if ((Previous && !Previous->opensScope()) || *I > prec::Comma) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 537 | NewParenState.BreakBeforeParameter = false; |
| 538 | State.Stack.push_back(NewParenState); |
| 539 | SkipFirstExtraIndent = false; |
| 540 | } |
| 541 | |
| 542 | // If we encounter an opening (, [, { or <, we add a level to our stacks to |
| 543 | // prepare for the following tokens. |
| 544 | if (Current.opensScope()) { |
| 545 | unsigned NewIndent; |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 546 | unsigned NewIndentLevel = State.Stack.back().IndentLevel; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 547 | bool AvoidBinPacking; |
Daniel Jasper | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 548 | bool BreakBeforeParameter = false; |
| 549 | if (Current.is(tok::l_brace) || |
| 550 | Current.Type == TT_ArrayInitializerLSquare) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 551 | if (Current.MatchingParen && Current.BlockKind == BK_Block) { |
Daniel Jasper | 1a925bc | 2013-09-05 10:48:50 +0000 | [diff] [blame] | 552 | // If this is an l_brace starting a nested block, we pretend (wrt. to |
| 553 | // indentation) that we already consumed the corresponding r_brace. |
| 554 | // Thus, we remove all ParenStates caused bake fake parentheses that end |
| 555 | // at the r_brace. The net effect of this is that we don't indent |
| 556 | // relative to the l_brace, if the nested block is the last parameter of |
| 557 | // a function. For example, this formats: |
| 558 | // |
| 559 | // SomeFunction(a, [] { |
| 560 | // f(); // break |
| 561 | // }); |
| 562 | // |
| 563 | // instead of: |
| 564 | // SomeFunction(a, [] { |
| 565 | // f(); // break |
| 566 | // }); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 567 | for (unsigned i = 0; i != Current.MatchingParen->FakeRParens; ++i) |
| 568 | State.Stack.pop_back(); |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 569 | NewIndent = State.Stack.back().LastSpace + Style.IndentWidth; |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 570 | ++NewIndentLevel; |
Daniel Jasper | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 571 | BreakBeforeParameter = true; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 572 | } else { |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 573 | NewIndent = State.Stack.back().LastSpace; |
Daniel Jasper | c968943 | 2013-10-22 15:45:58 +0000 | [diff] [blame] | 574 | if (Current.opensBlockTypeList(Style)) { |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 575 | NewIndent += Style.IndentWidth; |
| 576 | ++NewIndentLevel; |
Daniel Jasper | c968943 | 2013-10-22 15:45:58 +0000 | [diff] [blame] | 577 | } else { |
| 578 | NewIndent += Style.ContinuationIndentWidth; |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 579 | } |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 580 | } |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 581 | const FormatToken *NextNoComment = Current.getNextNonComment(); |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 582 | AvoidBinPacking = Current.BlockKind == BK_Block || |
Daniel Jasper | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 583 | Current.Type == TT_ArrayInitializerLSquare || |
Daniel Jasper | 3c6aea7 | 2013-10-24 10:31:50 +0000 | [diff] [blame] | 584 | Current.Type == TT_DictLiteral || |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 585 | (NextNoComment && |
| 586 | NextNoComment->Type == TT_DesignatedInitializerPeriod); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 587 | } else { |
Daniel Jasper | c2827ec | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 588 | NewIndent = Style.ContinuationIndentWidth + |
| 589 | std::max(State.Stack.back().LastSpace, |
| 590 | State.Stack.back().StartOfFunctionCall); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 591 | AvoidBinPacking = !Style.BinPackParameters || |
| 592 | (Style.ExperimentalAutoDetectBinPacking && |
| 593 | (Current.PackingKind == PPK_OnePerLine || |
| 594 | (!BinPackInconclusiveFunctions && |
| 595 | Current.PackingKind == PPK_Inconclusive))); |
Daniel Jasper | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 596 | // If this '[' opens an ObjC call, determine whether all parameters fit |
| 597 | // into one line and put one per line if they don't. |
| 598 | if (Current.Type == TT_ObjCMethodExpr && |
| 599 | getLengthToMatchingParen(Current) + State.Column > |
| 600 | getColumnLimit(State)) |
| 601 | BreakBeforeParameter = true; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Daniel Jasper | 9b3cb44 | 2013-10-18 15:23:06 +0000 | [diff] [blame] | 604 | bool NoLineBreak = State.Stack.back().NoLineBreak || |
| 605 | (Current.Type == TT_TemplateOpener && |
| 606 | State.Stack.back().ContainsUnwrappedBuilder); |
| 607 | State.Stack.push_back(ParenState(NewIndent, NewIndentLevel, |
| 608 | State.Stack.back().LastSpace, |
| 609 | AvoidBinPacking, NoLineBreak)); |
Daniel Jasper | a07aa66 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 610 | State.Stack.back().BreakBeforeParameter = BreakBeforeParameter; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 611 | ++State.ParenLevel; |
| 612 | } |
| 613 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 614 | // If we encounter a closing ), ], } or >, we can remove a level from our |
| 615 | // stacks. |
Daniel Jasper | 7143a21 | 2013-08-28 09:17:37 +0000 | [diff] [blame] | 616 | if (State.Stack.size() > 1 && |
| 617 | (Current.isOneOf(tok::r_paren, tok::r_square) || |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 618 | (Current.is(tok::r_brace) && State.NextToken != State.Line->First) || |
Daniel Jasper | 7143a21 | 2013-08-28 09:17:37 +0000 | [diff] [blame] | 619 | State.NextToken->Type == TT_TemplateCloser)) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 620 | State.Stack.pop_back(); |
| 621 | --State.ParenLevel; |
| 622 | } |
| 623 | if (Current.is(tok::r_square)) { |
| 624 | // If this ends the array subscript expr, reset the corresponding value. |
| 625 | const FormatToken *NextNonComment = Current.getNextNonComment(); |
| 626 | if (NextNonComment && NextNonComment->isNot(tok::l_square)) |
| 627 | State.Stack.back().StartOfArraySubscripts = 0; |
| 628 | } |
| 629 | |
| 630 | // Remove scopes created by fake parenthesis. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 631 | if (Current.isNot(tok::r_brace) || |
| 632 | (Current.MatchingParen && Current.MatchingParen->BlockKind != BK_Block)) { |
Daniel Jasper | 1a925bc | 2013-09-05 10:48:50 +0000 | [diff] [blame] | 633 | // Don't remove FakeRParens attached to r_braces that surround nested blocks |
| 634 | // as they will have been removed early (see above). |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 635 | for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) { |
| 636 | unsigned VariablePos = State.Stack.back().VariablePos; |
| 637 | State.Stack.pop_back(); |
| 638 | State.Stack.back().VariablePos = VariablePos; |
| 639 | } |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) { |
| 643 | State.StartOfStringLiteral = State.Column; |
| 644 | } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash, |
| 645 | tok::string_literal)) { |
| 646 | State.StartOfStringLiteral = 0; |
| 647 | } |
| 648 | |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 649 | State.Column += Current.ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 650 | State.NextToken = State.NextToken->Next; |
Daniel Jasper | d489f8c | 2013-08-27 11:09:05 +0000 | [diff] [blame] | 651 | unsigned Penalty = breakProtrudingToken(Current, State, DryRun); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 652 | if (State.Column > getColumnLimit(State)) { |
| 653 | unsigned ExcessCharacters = State.Column - getColumnLimit(State); |
| 654 | Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; |
| 655 | } |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 656 | |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 657 | // If the previous has a special role, let it consume tokens as appropriate. |
| 658 | // It is necessary to start at the previous token for the only implemented |
| 659 | // role (comma separated list). That way, the decision whether or not to break |
| 660 | // after the "{" is already done and both options are tried and evaluated. |
| 661 | // FIXME: This is ugly, find a better way. |
| 662 | if (Previous && Previous->Role) |
| 663 | Penalty += Previous->Role->format(State, this, DryRun); |
| 664 | |
| 665 | return Penalty; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Alexander Kornienko | 6f6154c | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 668 | unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current, |
| 669 | LineState &State) { |
Alexander Kornienko | dcc0c5b | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 670 | // Break before further function parameters on all levels. |
| 671 | for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) |
| 672 | State.Stack[i].BreakBeforeParameter = true; |
| 673 | |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 674 | unsigned ColumnsUsed = State.Column; |
Alexander Kornienko | 4b762a9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 675 | // We can only affect layout of the first and the last line, so the penalty |
| 676 | // for all other lines is constant, and we ignore it. |
Alexander Kornienko | 0b62cc3 | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 677 | State.Column = Current.LastLineColumnWidth; |
Alexander Kornienko | 4b762a9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 678 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 679 | if (ColumnsUsed > getColumnLimit(State)) |
| 680 | return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State)); |
Alexander Kornienko | dcc0c5b | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
Alexander Kornienko | 2c2f729 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 684 | static bool getRawStringLiteralPrefixPostfix(StringRef Text, |
| 685 | StringRef &Prefix, |
| 686 | StringRef &Postfix) { |
| 687 | if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") || |
| 688 | Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") || |
| 689 | Text.startswith(Prefix = "LR\"")) { |
| 690 | size_t ParenPos = Text.find('('); |
| 691 | if (ParenPos != StringRef::npos) { |
| 692 | StringRef Delimiter = |
| 693 | Text.substr(Prefix.size(), ParenPos - Prefix.size()); |
| 694 | Prefix = Text.substr(0, ParenPos + 1); |
| 695 | Postfix = Text.substr(Text.size() - 2 - Delimiter.size()); |
| 696 | return Postfix.front() == ')' && Postfix.back() == '"' && |
| 697 | Postfix.substr(1).startswith(Delimiter); |
| 698 | } |
| 699 | } |
| 700 | return false; |
| 701 | } |
| 702 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 703 | unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, |
| 704 | LineState &State, |
| 705 | bool DryRun) { |
Alexander Kornienko | 6f6154c | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 706 | // Don't break multi-line tokens other than block comments. Instead, just |
| 707 | // update the state. |
| 708 | if (Current.Type != TT_BlockComment && Current.IsMultiline) |
| 709 | return addMultilineToken(Current, State); |
| 710 | |
Daniel Jasper | 8437957 | 2013-10-30 13:54:53 +0000 | [diff] [blame] | 711 | // Don't break implicit string literals. |
| 712 | if (Current.Type == TT_ImplicitStringLiteral) |
| 713 | return 0; |
| 714 | |
Alexander Kornienko | 2c2f729 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 715 | if (!Current.isOneOf(tok::string_literal, tok::wide_string_literal, |
| 716 | tok::utf8_string_literal, tok::utf16_string_literal, |
| 717 | tok::utf32_string_literal, tok::comment)) |
Daniel Jasper | ed51c02 | 2013-08-23 10:05:49 +0000 | [diff] [blame] | 718 | return 0; |
| 719 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 720 | llvm::OwningPtr<BreakableToken> Token; |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 721 | unsigned StartColumn = State.Column - Current.ColumnWidth; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 722 | |
Alexander Kornienko | 2c2f729 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 723 | if (Current.isOneOf(tok::string_literal, tok::wide_string_literal, |
| 724 | tok::utf8_string_literal, tok::utf16_string_literal, |
| 725 | tok::utf32_string_literal) && |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 726 | Current.Type != TT_ImplicitStringLiteral) { |
Alexander Kornienko | b18c258 | 2013-10-11 21:43:05 +0000 | [diff] [blame] | 727 | // Don't break string literals inside preprocessor directives (except for |
| 728 | // #define directives, as their contents are stored in separate lines and |
| 729 | // are not affected by this check). |
| 730 | // This way we avoid breaking code with line directives and unknown |
| 731 | // preprocessor directives that contain long string literals. |
| 732 | if (State.Line->Type == LT_PreprocessorDirective) |
| 733 | return 0; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 734 | // Exempts unterminated string literals from line breaking. The user will |
| 735 | // likely want to terminate the string before any line breaking is done. |
| 736 | if (Current.IsUnterminatedLiteral) |
| 737 | return 0; |
| 738 | |
Alexander Kornienko | 2c2f729 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 739 | StringRef Text = Current.TokenText; |
| 740 | StringRef Prefix; |
| 741 | StringRef Postfix; |
| 742 | // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'. |
| 743 | // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to |
| 744 | // reduce the overhead) for each FormatToken, which is a string, so that we |
| 745 | // don't run multiple checks here on the hot path. |
| 746 | if ((Text.endswith(Postfix = "\"") && |
| 747 | (Text.startswith(Prefix = "\"") || Text.startswith(Prefix = "u\"") || |
| 748 | Text.startswith(Prefix = "U\"") || Text.startswith(Prefix = "u8\"") || |
| 749 | Text.startswith(Prefix = "L\""))) || |
| 750 | (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")")) || |
| 751 | getRawStringLiteralPrefixPostfix(Text, Prefix, Postfix)) { |
Alexander Kornienko | 3d9ffcf | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 752 | Token.reset(new BreakableStringLiteral( |
| 753 | Current, State.Line->Level, StartColumn, Prefix, Postfix, |
| 754 | State.Line->InPPDirective, Encoding, Style)); |
Alexander Kornienko | 2c2f729 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 755 | } else { |
| 756 | return 0; |
| 757 | } |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 758 | } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) { |
| 759 | Token.reset(new BreakableBlockComment( |
Alexander Kornienko | 3d9ffcf | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 760 | Current, State.Line->Level, StartColumn, Current.OriginalColumn, |
| 761 | !Current.Previous, State.Line->InPPDirective, Encoding, Style)); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 762 | } else if (Current.Type == TT_LineComment && |
| 763 | (Current.Previous == NULL || |
| 764 | Current.Previous->Type != TT_ImplicitStringLiteral)) { |
Alexander Kornienko | 3d9ffcf | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 765 | Token.reset(new BreakableLineComment(Current, State.Line->Level, |
| 766 | StartColumn, State.Line->InPPDirective, |
| 767 | Encoding, Style)); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 768 | } else { |
| 769 | return 0; |
| 770 | } |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 771 | if (Current.UnbreakableTailLength >= getColumnLimit(State)) |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 772 | return 0; |
| 773 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 774 | unsigned RemainingSpace = |
| 775 | getColumnLimit(State) - Current.UnbreakableTailLength; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 776 | bool BreakInserted = false; |
| 777 | unsigned Penalty = 0; |
| 778 | unsigned RemainingTokenColumns = 0; |
| 779 | for (unsigned LineIndex = 0, EndIndex = Token->getLineCount(); |
| 780 | LineIndex != EndIndex; ++LineIndex) { |
| 781 | if (!DryRun) |
| 782 | Token->replaceWhitespaceBefore(LineIndex, Whitespaces); |
| 783 | unsigned TailOffset = 0; |
| 784 | RemainingTokenColumns = |
| 785 | Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos); |
| 786 | while (RemainingTokenColumns > RemainingSpace) { |
| 787 | BreakableToken::Split Split = |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 788 | Token->getSplit(LineIndex, TailOffset, getColumnLimit(State)); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 789 | if (Split.first == StringRef::npos) { |
| 790 | // The last line's penalty is handled in addNextStateToQueue(). |
| 791 | if (LineIndex < EndIndex - 1) |
| 792 | Penalty += Style.PenaltyExcessCharacter * |
| 793 | (RemainingTokenColumns - RemainingSpace); |
| 794 | break; |
| 795 | } |
| 796 | assert(Split.first != 0); |
| 797 | unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit( |
| 798 | LineIndex, TailOffset + Split.first + Split.second, StringRef::npos); |
| 799 | assert(NewRemainingTokenColumns < RemainingTokenColumns); |
| 800 | if (!DryRun) |
| 801 | Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces); |
Daniel Jasper | f546178 | 2013-08-28 10:03:58 +0000 | [diff] [blame] | 802 | Penalty += Current.SplitPenalty; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 803 | unsigned ColumnsUsed = |
| 804 | Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 805 | if (ColumnsUsed > getColumnLimit(State)) { |
| 806 | Penalty += Style.PenaltyExcessCharacter * |
| 807 | (ColumnsUsed - getColumnLimit(State)); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 808 | } |
| 809 | TailOffset += Split.first + Split.second; |
| 810 | RemainingTokenColumns = NewRemainingTokenColumns; |
| 811 | BreakInserted = true; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | State.Column = RemainingTokenColumns; |
| 816 | |
| 817 | if (BreakInserted) { |
| 818 | // If we break the token inside a parameter list, we need to break before |
| 819 | // the next parameter on all levels, so that the next parameter is clearly |
| 820 | // visible. Line comments already introduce a break. |
| 821 | if (Current.Type != TT_LineComment) { |
| 822 | for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) |
| 823 | State.Stack[i].BreakBeforeParameter = true; |
| 824 | } |
| 825 | |
Daniel Jasper | f546178 | 2013-08-28 10:03:58 +0000 | [diff] [blame] | 826 | Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString |
| 827 | : Style.PenaltyBreakComment; |
| 828 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 829 | State.Stack.back().LastSpace = StartColumn; |
| 830 | } |
| 831 | return Penalty; |
| 832 | } |
| 833 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 834 | unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 835 | // In preprocessor directives reserve two chars for trailing " \" |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 836 | return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Daniel Jasper | 4df1ff9 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 839 | bool ContinuationIndenter::NextIsMultilineString(const LineState &State) { |
| 840 | const FormatToken &Current = *State.NextToken; |
| 841 | if (!Current.is(tok::string_literal)) |
| 842 | return false; |
Alexander Kornienko | dcc0c5b | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 843 | // We never consider raw string literals "multiline" for the purpose of |
| 844 | // AlwaysBreakBeforeMultilineStrings implementation. |
| 845 | if (Current.TokenText.startswith("R\"")) |
| 846 | return false; |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 847 | if (Current.IsMultiline) |
Alexander Kornienko | dcc0c5b | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 848 | return true; |
Daniel Jasper | 4df1ff9 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 849 | if (Current.getNextNonComment() && |
| 850 | Current.getNextNonComment()->is(tok::string_literal)) |
| 851 | return true; // Implicit concatenation. |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 852 | if (State.Column + Current.ColumnWidth + Current.UnbreakableTailLength > |
Daniel Jasper | 4df1ff9 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 853 | Style.ColumnLimit) |
| 854 | return true; // String will be split. |
Alexander Kornienko | dcc0c5b | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 855 | return false; |
Daniel Jasper | 4df1ff9 | 2013-08-23 11:57:34 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 858 | } // namespace format |
| 859 | } // namespace clang |