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