ContinuationIndenter.cpp revision 263508
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
26namespace clang {
27namespace format {
28
29// Returns the length of everything up to the first possible line break after
30// the ), ], } or > matching \c Tok.
31static 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.
43static 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.
48static 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
59ContinuationIndenter::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
68LineState 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
91bool 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
117bool 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
203unsigned 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
232void 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
312unsigned 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.
336  if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 &&
337      State.Column <= Style.ColumnLimit / 2)
338    Penalty += Style.PenaltyBreakFirstLessLess;
339
340  if (Current.is(tok::l_brace) && Current.BlockKind == BK_Block) {
341    State.Column = State.FirstIndent;
342  } else if (Current.isOneOf(tok::r_brace, tok::r_square)) {
343    if (Current.closesBlockTypeList(Style) ||
344        (Current.MatchingParen &&
345         Current.MatchingParen->BlockKind == BK_BracedInit))
346      State.Column = State.Stack[State.Stack.size() - 2].LastSpace;
347    else
348      State.Column = State.FirstIndent;
349  } else if (Current.is(tok::string_literal) &&
350             State.StartOfStringLiteral != 0) {
351    State.Column = State.StartOfStringLiteral;
352    State.Stack.back().BreakBeforeParameter = true;
353  } else if (Current.is(tok::lessless) &&
354             State.Stack.back().FirstLessLess != 0) {
355    State.Column = State.Stack.back().FirstLessLess;
356  } else if (Current.isMemberAccess()) {
357    if (State.Stack.back().CallContinuation == 0) {
358      State.Column = ContinuationIndent;
359      State.Stack.back().CallContinuation = State.Column;
360    } else {
361      State.Column = State.Stack.back().CallContinuation;
362    }
363  } else if (State.Stack.back().QuestionColumn != 0 &&
364             (Current.Type == TT_ConditionalExpr ||
365              Previous.Type == TT_ConditionalExpr)) {
366    State.Column = State.Stack.back().QuestionColumn;
367  } else if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) {
368    State.Column = State.Stack.back().VariablePos;
369  } else if ((PreviousNonComment &&
370              PreviousNonComment->ClosesTemplateDeclaration) ||
371             ((Current.Type == TT_StartOfName ||
372               Current.is(tok::kw_operator)) &&
373              State.ParenLevel == 0 &&
374              (!Style.IndentFunctionDeclarationAfterType ||
375               State.Line->StartsDefinition))) {
376    State.Column = State.Stack.back().Indent;
377  } else if (Current.Type == TT_ObjCSelectorName) {
378    if (State.Stack.back().ColonPos == 0) {
379      State.Stack.back().ColonPos =
380          State.Stack.back().Indent + Current.LongestObjCSelectorName;
381      State.Column = State.Stack.back().ColonPos - Current.ColumnWidth;
382    } else if (State.Stack.back().ColonPos > Current.ColumnWidth) {
383      State.Column = State.Stack.back().ColonPos - Current.ColumnWidth;
384    } else {
385      State.Column = State.Stack.back().Indent;
386      State.Stack.back().ColonPos = State.Column + Current.ColumnWidth;
387    }
388  } else if (Current.Type == TT_ArraySubscriptLSquare) {
389    if (State.Stack.back().StartOfArraySubscripts != 0)
390      State.Column = State.Stack.back().StartOfArraySubscripts;
391    else
392      State.Column = ContinuationIndent;
393  } else if (Current.Type == TT_StartOfName ||
394             Previous.isOneOf(tok::coloncolon, tok::equal) ||
395             Previous.Type == TT_ObjCMethodExpr) {
396    State.Column = ContinuationIndent;
397  } else if (Current.Type == TT_CtorInitializerColon) {
398    State.Column = State.FirstIndent + Style.ConstructorInitializerIndentWidth;
399  } else if (Current.Type == TT_CtorInitializerComma) {
400    State.Column = State.Stack.back().Indent;
401  } else {
402    State.Column = State.Stack.back().Indent;
403    // Ensure that we fall back to the continuation indent width instead of just
404    // flushing continuations left.
405    if (State.Column == State.FirstIndent &&
406        PreviousNonComment->isNot(tok::r_brace))
407      State.Column += Style.ContinuationIndentWidth;
408  }
409
410  if ((Previous.isOneOf(tok::comma, tok::semi) &&
411       !State.Stack.back().AvoidBinPacking) ||
412      Previous.Type == TT_BinaryOperator)
413    State.Stack.back().BreakBeforeParameter = false;
414  if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0)
415    State.Stack.back().BreakBeforeParameter = false;
416  if (Current.is(tok::question) ||
417      (PreviousNonComment && PreviousNonComment->is(tok::question)))
418    State.Stack.back().BreakBeforeParameter = true;
419
420  if (!DryRun) {
421    unsigned Newlines = 1;
422    if (Current.is(tok::comment))
423      Newlines = std::max(Newlines, std::min(Current.NewlinesBefore,
424                                             Style.MaxEmptyLinesToKeep + 1));
425    Whitespaces.replaceWhitespace(Current, Newlines,
426                                  State.Stack.back().IndentLevel, State.Column,
427                                  State.Column, State.Line->InPPDirective);
428  }
429
430  if (!Current.isTrailingComment())
431    State.Stack.back().LastSpace = State.Column;
432  if (Current.isMemberAccess())
433    State.Stack.back().LastSpace += Current.ColumnWidth;
434  State.StartOfLineLevel = State.ParenLevel;
435  State.LowestLevelOnLine = State.ParenLevel;
436
437  // Any break on this level means that the parent level has been broken
438  // and we need to avoid bin packing there.
439  for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
440    State.Stack[i].BreakBeforeParameter = true;
441  }
442  if (PreviousNonComment &&
443      !PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
444      PreviousNonComment->Type != TT_TemplateCloser &&
445      PreviousNonComment->Type != TT_BinaryOperator &&
446      Current.Type != TT_BinaryOperator &&
447      !PreviousNonComment->opensScope())
448    State.Stack.back().BreakBeforeParameter = true;
449
450  // If we break after { or the [ of an array initializer, we should also break
451  // before the corresponding } or ].
452  if (Previous.is(tok::l_brace) || Previous.Type == TT_ArrayInitializerLSquare)
453    State.Stack.back().BreakBeforeClosingBrace = true;
454
455  if (State.Stack.back().AvoidBinPacking) {
456    // If we are breaking after '(', '{', '<', this is not bin packing
457    // unless AllowAllParametersOfDeclarationOnNextLine is false.
458    if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
459          Previous.Type == TT_BinaryOperator) ||
460        (!Style.AllowAllParametersOfDeclarationOnNextLine &&
461         State.Line->MustBeDeclaration))
462      State.Stack.back().BreakBeforeParameter = true;
463  }
464
465  return Penalty;
466}
467
468unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
469                                                    bool DryRun, bool Newline) {
470  const FormatToken &Current = *State.NextToken;
471  assert(State.Stack.size());
472
473  if (Current.Type == TT_InheritanceColon)
474    State.Stack.back().AvoidBinPacking = true;
475  if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
476    State.Stack.back().FirstLessLess = State.Column;
477  if (Current.Type == TT_ArraySubscriptLSquare &&
478      State.Stack.back().StartOfArraySubscripts == 0)
479    State.Stack.back().StartOfArraySubscripts = State.Column;
480  if ((Current.is(tok::question) && Style.BreakBeforeTernaryOperators) ||
481      (Current.getPreviousNonComment() && Current.isNot(tok::colon) &&
482       Current.getPreviousNonComment()->is(tok::question) &&
483       !Style.BreakBeforeTernaryOperators))
484    State.Stack.back().QuestionColumn = State.Column;
485  if (!Current.opensScope() && !Current.closesScope())
486    State.LowestLevelOnLine =
487        std::min(State.LowestLevelOnLine, State.ParenLevel);
488  if (Current.isMemberAccess())
489    State.Stack.back().StartOfFunctionCall =
490        Current.LastInChainOfCalls ? 0 : State.Column + Current.ColumnWidth;
491  if (Current.Type == TT_CtorInitializerColon) {
492    // Indent 2 from the column, so:
493    // SomeClass::SomeClass()
494    //     : First(...), ...
495    //       Next(...)
496    //       ^ line up here.
497    State.Stack.back().Indent =
498        State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2);
499    if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
500      State.Stack.back().AvoidBinPacking = true;
501    State.Stack.back().BreakBeforeParameter = false;
502  }
503
504  // In ObjC method declaration we align on the ":" of parameters, but we need
505  // to ensure that we indent parameters on subsequent lines by at least our
506  // continuation indent width.
507  if (Current.Type == TT_ObjCMethodSpecifier)
508    State.Stack.back().Indent += Style.ContinuationIndentWidth;
509
510  // Insert scopes created by fake parenthesis.
511  const FormatToken *Previous = Current.getPreviousNonComment();
512  // Don't add extra indentation for the first fake parenthesis after
513  // 'return', assignements or opening <({[. The indentation for these cases
514  // is special cased.
515  bool SkipFirstExtraIndent =
516      (Previous && (Previous->opensScope() || Previous->is(tok::kw_return) ||
517                    Previous->getPrecedence() == prec::Assignment ||
518                    Previous->Type == TT_ObjCMethodExpr));
519  for (SmallVectorImpl<prec::Level>::const_reverse_iterator
520           I = Current.FakeLParens.rbegin(),
521           E = Current.FakeLParens.rend();
522       I != E; ++I) {
523    ParenState NewParenState = State.Stack.back();
524    NewParenState.ContainsLineBreak = false;
525
526    // Indent from 'LastSpace' unless this the fake parentheses encapsulating a
527    // builder type call after 'return'. If such a call is line-wrapped, we
528    // commonly just want to indent from the start of the line.
529    if (!Previous || Previous->isNot(tok::kw_return) || *I > 0)
530      NewParenState.Indent =
531          std::max(std::max(State.Column, NewParenState.Indent),
532                   State.Stack.back().LastSpace);
533
534    // Do not indent relative to the fake parentheses inserted for "." or "->".
535    // This is a special case to make the following to statements consistent:
536    //   OuterFunction(InnerFunctionCall( // break
537    //       ParameterToInnerFunction));
538    //   OuterFunction(SomeObject.InnerFunctionCall( // break
539    //       ParameterToInnerFunction));
540    if (*I > prec::Unknown)
541      NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
542
543    // Always indent conditional expressions. Never indent expression where
544    // the 'operator' is ',', ';' or an assignment (i.e. *I <=
545    // prec::Assignment) as those have different indentation rules. Indent
546    // other expression, unless the indentation needs to be skipped.
547    if (*I == prec::Conditional ||
548        (!SkipFirstExtraIndent && *I > prec::Assignment &&
549         !Style.BreakBeforeBinaryOperators))
550      NewParenState.Indent += Style.ContinuationIndentWidth;
551    if ((Previous && !Previous->opensScope()) || *I > prec::Comma)
552      NewParenState.BreakBeforeParameter = false;
553    State.Stack.push_back(NewParenState);
554    SkipFirstExtraIndent = false;
555  }
556
557  // If we encounter an opening (, [, { or <, we add a level to our stacks to
558  // prepare for the following tokens.
559  if (Current.opensScope()) {
560    unsigned NewIndent;
561    unsigned NewIndentLevel = State.Stack.back().IndentLevel;
562    bool AvoidBinPacking;
563    bool BreakBeforeParameter = false;
564    if (Current.is(tok::l_brace) ||
565        Current.Type == TT_ArrayInitializerLSquare) {
566      if (Current.MatchingParen && Current.BlockKind == BK_Block) {
567        // If this is an l_brace starting a nested block, we pretend (wrt. to
568        // indentation) that we already consumed the corresponding r_brace.
569        // Thus, we remove all ParenStates caused bake fake parentheses that end
570        // at the r_brace. The net effect of this is that we don't indent
571        // relative to the l_brace, if the nested block is the last parameter of
572        // a function. For example, this formats:
573        //
574        //   SomeFunction(a, [] {
575        //     f();  // break
576        //   });
577        //
578        // instead of:
579        //   SomeFunction(a, [] {
580        //                        f();  // break
581        //                      });
582        for (unsigned i = 0; i != Current.MatchingParen->FakeRParens; ++i)
583          State.Stack.pop_back();
584        NewIndent = State.Stack.back().LastSpace + Style.IndentWidth;
585        ++NewIndentLevel;
586        BreakBeforeParameter = true;
587      } else {
588        NewIndent = State.Stack.back().LastSpace;
589        if (Current.opensBlockTypeList(Style)) {
590          NewIndent += Style.IndentWidth;
591          ++NewIndentLevel;
592        } else {
593          NewIndent += Style.ContinuationIndentWidth;
594        }
595      }
596      const FormatToken *NextNoComment = Current.getNextNonComment();
597      AvoidBinPacking = Current.BlockKind == BK_Block ||
598                        Current.Type == TT_ArrayInitializerLSquare ||
599                        Current.Type == TT_DictLiteral ||
600                        (NextNoComment &&
601                         NextNoComment->Type == TT_DesignatedInitializerPeriod);
602    } else {
603      NewIndent = Style.ContinuationIndentWidth +
604                  std::max(State.Stack.back().LastSpace,
605                           State.Stack.back().StartOfFunctionCall);
606      AvoidBinPacking = !Style.BinPackParameters ||
607                        (Style.ExperimentalAutoDetectBinPacking &&
608                         (Current.PackingKind == PPK_OnePerLine ||
609                          (!BinPackInconclusiveFunctions &&
610                           Current.PackingKind == PPK_Inconclusive)));
611      // If this '[' opens an ObjC call, determine whether all parameters fit
612      // into one line and put one per line if they don't.
613      if (Current.Type == TT_ObjCMethodExpr &&
614          getLengthToMatchingParen(Current) + State.Column >
615              getColumnLimit(State))
616        BreakBeforeParameter = true;
617    }
618
619    bool NoLineBreak = State.Stack.back().NoLineBreak ||
620                       (Current.Type == TT_TemplateOpener &&
621                        State.Stack.back().ContainsUnwrappedBuilder);
622    State.Stack.push_back(ParenState(NewIndent, NewIndentLevel,
623                                     State.Stack.back().LastSpace,
624                                     AvoidBinPacking, NoLineBreak));
625    State.Stack.back().BreakBeforeParameter = BreakBeforeParameter;
626    ++State.ParenLevel;
627  }
628
629  // If we encounter a closing ), ], } or >, we can remove a level from our
630  // stacks.
631  if (State.Stack.size() > 1 &&
632      (Current.isOneOf(tok::r_paren, tok::r_square) ||
633       (Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
634       State.NextToken->Type == TT_TemplateCloser)) {
635    State.Stack.pop_back();
636    --State.ParenLevel;
637  }
638  if (Current.is(tok::r_square)) {
639    // If this ends the array subscript expr, reset the corresponding value.
640    const FormatToken *NextNonComment = Current.getNextNonComment();
641    if (NextNonComment && NextNonComment->isNot(tok::l_square))
642      State.Stack.back().StartOfArraySubscripts = 0;
643  }
644
645  // Remove scopes created by fake parenthesis.
646  if (Current.isNot(tok::r_brace) ||
647      (Current.MatchingParen && Current.MatchingParen->BlockKind != BK_Block)) {
648    // Don't remove FakeRParens attached to r_braces that surround nested blocks
649    // as they will have been removed early (see above).
650    for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
651      unsigned VariablePos = State.Stack.back().VariablePos;
652      State.Stack.pop_back();
653      State.Stack.back().VariablePos = VariablePos;
654    }
655  }
656
657  if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
658    State.StartOfStringLiteral = State.Column;
659  } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash,
660                              tok::string_literal)) {
661    State.StartOfStringLiteral = 0;
662  }
663
664  State.Column += Current.ColumnWidth;
665  State.NextToken = State.NextToken->Next;
666  unsigned Penalty = breakProtrudingToken(Current, State, DryRun);
667  if (State.Column > getColumnLimit(State)) {
668    unsigned ExcessCharacters = State.Column - getColumnLimit(State);
669    Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
670  }
671
672  // If the previous has a special role, let it consume tokens as appropriate.
673  // It is necessary to start at the previous token for the only implemented
674  // role (comma separated list). That way, the decision whether or not to break
675  // after the "{" is already done and both options are tried and evaluated.
676  // FIXME: This is ugly, find a better way.
677  if (Previous && Previous->Role)
678    Penalty += Previous->Role->format(State, this, DryRun);
679
680  return Penalty;
681}
682
683unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
684                                                 LineState &State) {
685  // Break before further function parameters on all levels.
686  for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
687    State.Stack[i].BreakBeforeParameter = true;
688
689  unsigned ColumnsUsed = State.Column;
690  // We can only affect layout of the first and the last line, so the penalty
691  // for all other lines is constant, and we ignore it.
692  State.Column = Current.LastLineColumnWidth;
693
694  if (ColumnsUsed > getColumnLimit(State))
695    return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
696  return 0;
697}
698
699static bool getRawStringLiteralPrefixPostfix(StringRef Text,
700                                             StringRef &Prefix,
701                                             StringRef &Postfix) {
702  if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") ||
703      Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") ||
704      Text.startswith(Prefix = "LR\"")) {
705    size_t ParenPos = Text.find('(');
706    if (ParenPos != StringRef::npos) {
707      StringRef Delimiter =
708          Text.substr(Prefix.size(), ParenPos - Prefix.size());
709      Prefix = Text.substr(0, ParenPos + 1);
710      Postfix = Text.substr(Text.size() - 2 - Delimiter.size());
711      return Postfix.front() == ')' && Postfix.back() == '"' &&
712             Postfix.substr(1).startswith(Delimiter);
713    }
714  }
715  return false;
716}
717
718unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
719                                                    LineState &State,
720                                                    bool DryRun) {
721  // Don't break multi-line tokens other than block comments. Instead, just
722  // update the state.
723  if (Current.Type != TT_BlockComment && Current.IsMultiline)
724    return addMultilineToken(Current, State);
725
726  // Don't break implicit string literals.
727  if (Current.Type == TT_ImplicitStringLiteral)
728    return 0;
729
730  if (!Current.isOneOf(tok::string_literal, tok::wide_string_literal,
731                       tok::utf8_string_literal, tok::utf16_string_literal,
732                       tok::utf32_string_literal, tok::comment))
733    return 0;
734
735  llvm::OwningPtr<BreakableToken> Token;
736  unsigned StartColumn = State.Column - Current.ColumnWidth;
737  unsigned ColumnLimit = getColumnLimit(State);
738
739  if (Current.isOneOf(tok::string_literal, tok::wide_string_literal,
740                      tok::utf8_string_literal, tok::utf16_string_literal,
741                      tok::utf32_string_literal) &&
742      Current.Type != TT_ImplicitStringLiteral) {
743    // Don't break string literals inside preprocessor directives (except for
744    // #define directives, as their contents are stored in separate lines and
745    // are not affected by this check).
746    // This way we avoid breaking code with line directives and unknown
747    // preprocessor directives that contain long string literals.
748    if (State.Line->Type == LT_PreprocessorDirective)
749      return 0;
750    // Exempts unterminated string literals from line breaking. The user will
751    // likely want to terminate the string before any line breaking is done.
752    if (Current.IsUnterminatedLiteral)
753      return 0;
754
755    StringRef Text = Current.TokenText;
756    StringRef Prefix;
757    StringRef Postfix;
758    // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
759    // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
760    // reduce the overhead) for each FormatToken, which is a string, so that we
761    // don't run multiple checks here on the hot path.
762    if ((Text.endswith(Postfix = "\"") &&
763         (Text.startswith(Prefix = "\"") || Text.startswith(Prefix = "u\"") ||
764          Text.startswith(Prefix = "U\"") || Text.startswith(Prefix = "u8\"") ||
765          Text.startswith(Prefix = "L\""))) ||
766        (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")")) ||
767        getRawStringLiteralPrefixPostfix(Text, Prefix, Postfix)) {
768      Token.reset(new BreakableStringLiteral(
769          Current, State.Line->Level, StartColumn, Prefix, Postfix,
770          State.Line->InPPDirective, Encoding, Style));
771    } else {
772      return 0;
773    }
774  } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) {
775    Token.reset(new BreakableBlockComment(
776        Current, State.Line->Level, StartColumn, Current.OriginalColumn,
777        !Current.Previous, State.Line->InPPDirective, Encoding, Style));
778  } else if (Current.Type == TT_LineComment &&
779             (Current.Previous == NULL ||
780              Current.Previous->Type != TT_ImplicitStringLiteral)) {
781    Token.reset(new BreakableLineComment(Current, State.Line->Level,
782                                         StartColumn, /*InPPDirective=*/false,
783                                         Encoding, Style));
784    // We don't insert backslashes when breaking line comments.
785    ColumnLimit = Style.ColumnLimit;
786  } else {
787    return 0;
788  }
789  if (Current.UnbreakableTailLength >= ColumnLimit)
790    return 0;
791
792  unsigned RemainingSpace = ColumnLimit - Current.UnbreakableTailLength;
793  bool BreakInserted = false;
794  unsigned Penalty = 0;
795  unsigned RemainingTokenColumns = 0;
796  for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
797       LineIndex != EndIndex; ++LineIndex) {
798    if (!DryRun)
799      Token->replaceWhitespaceBefore(LineIndex, Whitespaces);
800    unsigned TailOffset = 0;
801    RemainingTokenColumns =
802        Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
803    while (RemainingTokenColumns > RemainingSpace) {
804      BreakableToken::Split Split =
805          Token->getSplit(LineIndex, TailOffset, ColumnLimit);
806      if (Split.first == StringRef::npos) {
807        // The last line's penalty is handled in addNextStateToQueue().
808        if (LineIndex < EndIndex - 1)
809          Penalty += Style.PenaltyExcessCharacter *
810                     (RemainingTokenColumns - RemainingSpace);
811        break;
812      }
813      assert(Split.first != 0);
814      unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
815          LineIndex, TailOffset + Split.first + Split.second, StringRef::npos);
816
817      // We can remove extra whitespace instead of breaking the line.
818      if (RemainingTokenColumns + 1 - Split.second <= RemainingSpace) {
819        RemainingTokenColumns = 0;
820        if (!DryRun)
821          Token->replaceWhitespace(LineIndex, TailOffset, Split, Whitespaces);
822        break;
823      }
824
825      assert(NewRemainingTokenColumns < RemainingTokenColumns);
826      if (!DryRun)
827        Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
828      Penalty += Current.SplitPenalty;
829      unsigned ColumnsUsed =
830          Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
831      if (ColumnsUsed > ColumnLimit) {
832        Penalty += Style.PenaltyExcessCharacter * (ColumnsUsed - ColumnLimit);
833      }
834      TailOffset += Split.first + Split.second;
835      RemainingTokenColumns = NewRemainingTokenColumns;
836      BreakInserted = true;
837    }
838  }
839
840  State.Column = RemainingTokenColumns;
841
842  if (BreakInserted) {
843    // If we break the token inside a parameter list, we need to break before
844    // the next parameter on all levels, so that the next parameter is clearly
845    // visible. Line comments already introduce a break.
846    if (Current.Type != TT_LineComment) {
847      for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
848        State.Stack[i].BreakBeforeParameter = true;
849    }
850
851    Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString
852                                               : Style.PenaltyBreakComment;
853
854    State.Stack.back().LastSpace = StartColumn;
855  }
856  return Penalty;
857}
858
859unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
860  // In preprocessor directives reserve two chars for trailing " \"
861  return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
862}
863
864bool ContinuationIndenter::NextIsMultilineString(const LineState &State) {
865  const FormatToken &Current = *State.NextToken;
866  if (!Current.is(tok::string_literal))
867    return false;
868  // We never consider raw string literals "multiline" for the purpose of
869  // AlwaysBreakBeforeMultilineStrings implementation.
870  if (Current.TokenText.startswith("R\""))
871    return false;
872  if (Current.IsMultiline)
873    return true;
874  if (Current.getNextNonComment() &&
875      Current.getNextNonComment()->is(tok::string_literal))
876    return true; // Implicit concatenation.
877  if (State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
878      Style.ColumnLimit)
879    return true; // String will be split.
880  return false;
881}
882
883} // namespace format
884} // namespace clang
885