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