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