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