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