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