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