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