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