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