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