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