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