clang  7.0.0svn
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 /// This file implements the continuation indenter.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "ContinuationIndenter.h"
16 #include "BreakableToken.h"
17 #include "FormatInternal.h"
18 #include "WhitespaceManager.h"
21 #include "clang/Format/Format.h"
22 #include "llvm/Support/Debug.h"
23 
24 #define DEBUG_TYPE "format-indenter"
25 
26 namespace clang {
27 namespace format {
28 
29 // Returns true if a TT_SelectorName should be indented when wrapped,
30 // false otherwise.
33  return Style.IndentWrappedFunctionNames || LineType == LT_ObjCMethodDecl;
34 }
35 
36 // Returns the length of everything up to the first possible line break after
37 // the ), ], } or > matching \c Tok.
38 static unsigned getLengthToMatchingParen(const FormatToken &Tok,
39  const std::vector<ParenState> &Stack) {
40  // Normally whether or not a break before T is possible is calculated and
41  // stored in T.CanBreakBefore. Braces, array initializers and text proto
42  // messages like `key: < ... >` are an exception: a break is possible
43  // before a closing brace R if a break was inserted after the corresponding
44  // opening brace. The information about whether or not a break is needed
45  // before a closing brace R is stored in the ParenState field
46  // S.BreakBeforeClosingBrace where S is the state that R closes.
47  //
48  // In order to decide whether there can be a break before encountered right
49  // braces, this implementation iterates over the sequence of tokens and over
50  // the paren stack in lockstep, keeping track of the stack level which visited
51  // right braces correspond to in MatchingStackIndex.
52  //
53  // For example, consider:
54  // L. <- line number
55  // 1. {
56  // 2. {1},
57  // 3. {2},
58  // 4. {{3}}}
59  // ^ where we call this method with this token.
60  // The paren stack at this point contains 3 brace levels:
61  // 0. { at line 1, BreakBeforeClosingBrace: true
62  // 1. first { at line 4, BreakBeforeClosingBrace: false
63  // 2. second { at line 4, BreakBeforeClosingBrace: false,
64  // where there might be fake parens levels in-between these levels.
65  // The algorithm will start at the first } on line 4, which is the matching
66  // brace of the initial left brace and at level 2 of the stack. Then,
67  // examining BreakBeforeClosingBrace: false at level 2, it will continue to
68  // the second } on line 4, and will traverse the stack downwards until it
69  // finds the matching { on level 1. Then, examining BreakBeforeClosingBrace:
70  // false at level 1, it will continue to the third } on line 4 and will
71  // traverse the stack downwards until it finds the matching { on level 0.
72  // Then, examining BreakBeforeClosingBrace: true at level 0, the algorithm
73  // will stop and will use the second } on line 4 to determine the length to
74  // return, as in this example the range will include the tokens: {3}}
75  //
76  // The algorithm will only traverse the stack if it encounters braces, array
77  // initializer squares or text proto angle brackets.
78  if (!Tok.MatchingParen)
79  return 0;
81  // Maintains a stack level corresponding to the current End token.
82  int MatchingStackIndex = Stack.size() - 1;
83  // Traverses the stack downwards, looking for the level to which LBrace
84  // corresponds. Returns either a pointer to the matching level or nullptr if
85  // LParen is not found in the initial portion of the stack up to
86  // MatchingStackIndex.
87  auto FindParenState = [&](const FormatToken *LBrace) -> const ParenState * {
88  while (MatchingStackIndex >= 0 && Stack[MatchingStackIndex].Tok != LBrace)
89  --MatchingStackIndex;
90  return MatchingStackIndex >= 0 ? &Stack[MatchingStackIndex] : nullptr;
91  };
92  for (; End->Next; End = End->Next) {
93  if (End->Next->CanBreakBefore)
94  break;
95  if (!End->Next->closesScope())
96  continue;
97  if (End->Next->MatchingParen->isOneOf(tok::l_brace,
98  TT_ArrayInitializerLSquare,
99  tok::less)) {
100  const ParenState *State = FindParenState(End->Next->MatchingParen);
101  if (State && State->BreakBeforeClosingBrace)
102  break;
103  }
104  }
105  return End->TotalLength - Tok.TotalLength + 1;
106 }
107 
108 static unsigned getLengthToNextOperator(const FormatToken &Tok) {
109  if (!Tok.NextOperator)
110  return 0;
111  return Tok.NextOperator->TotalLength - Tok.TotalLength;
112 }
113 
114 // Returns \c true if \c Tok is the "." or "->" of a call and starts the next
115 // segment of a builder type call.
117  return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
118 }
119 
120 // Returns \c true if \c Current starts a new parameter.
121 static bool startsNextParameter(const FormatToken &Current,
122  const FormatStyle &Style) {
123  const FormatToken &Previous = *Current.Previous;
124  if (Current.is(TT_CtorInitializerComma) &&
126  return true;
127  if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName))
128  return true;
129  return Previous.is(tok::comma) && !Current.isTrailingComment() &&
130  ((Previous.isNot(TT_CtorInitializerComma) ||
133  (Previous.isNot(TT_InheritanceComma) ||
135 }
136 
137 static bool opensProtoMessageField(const FormatToken &LessTok,
138  const FormatStyle &Style) {
139  if (LessTok.isNot(tok::less))
140  return false;
141  return Style.Language == FormatStyle::LK_TextProto ||
142  (Style.Language == FormatStyle::LK_Proto &&
143  (LessTok.NestingLevel > 0 ||
144  (LessTok.Previous && LessTok.Previous->is(tok::equal))));
145 }
146 
147 // Returns the delimiter of a raw string literal, or None if TokenText is not
148 // the text of a raw string literal. The delimiter could be the empty string.
149 // For example, the delimiter of R"deli(cont)deli" is deli.
151  if (TokenText.size() < 5 // The smallest raw string possible is 'R"()"'.
152  || !TokenText.startswith("R\"") || !TokenText.endswith("\""))
153  return None;
154 
155  // A raw string starts with 'R"<delimiter>(' and delimiter is ascii and has
156  // size at most 16 by the standard, so the first '(' must be among the first
157  // 19 bytes.
158  size_t LParenPos = TokenText.substr(0, 19).find_first_of('(');
159  if (LParenPos == StringRef::npos)
160  return None;
161  StringRef Delimiter = TokenText.substr(2, LParenPos - 2);
162 
163  // Check that the string ends in ')Delimiter"'.
164  size_t RParenPos = TokenText.size() - Delimiter.size() - 2;
165  if (TokenText[RParenPos] != ')')
166  return None;
167  if (!TokenText.substr(RParenPos + 1).startswith(Delimiter))
168  return None;
169  return Delimiter;
170 }
171 
172 // Returns the canonical delimiter for \p Language, or the empty string if no
173 // canonical delimiter is specified.
174 static StringRef
176  FormatStyle::LanguageKind Language) {
177  for (const auto &Format : Style.RawStringFormats) {
178  if (Format.Language == Language)
179  return StringRef(Format.CanonicalDelimiter);
180  }
181  return "";
182 }
183 
185  const FormatStyle &CodeStyle) {
186  for (const auto &RawStringFormat : CodeStyle.RawStringFormats) {
187  llvm::Optional<FormatStyle> LanguageStyle =
188  CodeStyle.GetLanguageStyle(RawStringFormat.Language);
189  if (!LanguageStyle) {
190  FormatStyle PredefinedStyle;
191  if (!getPredefinedStyle(RawStringFormat.BasedOnStyle,
192  RawStringFormat.Language, &PredefinedStyle)) {
193  PredefinedStyle = getLLVMStyle();
194  PredefinedStyle.Language = RawStringFormat.Language;
195  }
196  LanguageStyle = PredefinedStyle;
197  }
198  LanguageStyle->ColumnLimit = CodeStyle.ColumnLimit;
199  for (StringRef Delimiter : RawStringFormat.Delimiters) {
200  DelimiterStyle.insert({Delimiter, *LanguageStyle});
201  }
202  for (StringRef EnclosingFunction : RawStringFormat.EnclosingFunctions) {
203  EnclosingFunctionStyle.insert({EnclosingFunction, *LanguageStyle});
204  }
205  }
206 }
207 
210  auto It = DelimiterStyle.find(Delimiter);
211  if (It == DelimiterStyle.end())
212  return None;
213  return It->second;
214 }
215 
218  StringRef EnclosingFunction) const {
219  auto It = EnclosingFunctionStyle.find(EnclosingFunction);
220  if (It == EnclosingFunctionStyle.end())
221  return None;
222  return It->second;
223 }
224 
226  const AdditionalKeywords &Keywords,
227  const SourceManager &SourceMgr,
228  WhitespaceManager &Whitespaces,
230  bool BinPackInconclusiveFunctions)
231  : Style(Style), Keywords(Keywords), SourceMgr(SourceMgr),
232  Whitespaces(Whitespaces), Encoding(Encoding),
233  BinPackInconclusiveFunctions(BinPackInconclusiveFunctions),
234  CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {}
235 
237  unsigned FirstStartColumn,
238  const AnnotatedLine *Line,
239  bool DryRun) {
241  State.FirstIndent = FirstIndent;
242  if (FirstStartColumn && Line->First->NewlinesBefore == 0)
243  State.Column = FirstStartColumn;
244  else
245  State.Column = FirstIndent;
246  // With preprocessor directive indentation, the line starts on column 0
247  // since it's indented after the hash, but FirstIndent is set to the
248  // preprocessor indent.
250  (Line->Type == LT_PreprocessorDirective ||
251  Line->Type == LT_ImportStatement))
252  State.Column = 0;
253  State.Line = Line;
254  State.NextToken = Line->First;
255  State.Stack.push_back(ParenState(/*Tok=*/nullptr, FirstIndent, FirstIndent,
256  /*AvoidBinPacking=*/false,
257  /*NoLineBreak=*/false));
259  State.NoContinuation = false;
260  State.StartOfStringLiteral = 0;
261  State.StartOfLineLevel = 0;
262  State.LowestLevelOnLine = 0;
263  State.IgnoreStackForComparison = false;
264 
265  if (Style.Language == FormatStyle::LK_TextProto) {
266  // We need this in order to deal with the bin packing of text fields at
267  // global scope.
268  State.Stack.back().AvoidBinPacking = true;
269  State.Stack.back().BreakBeforeParameter = true;
270  State.Stack.back().AlignColons = false;
271  }
272 
273  // The first token has already been indented and thus consumed.
274  moveStateToNextToken(State, DryRun, /*Newline=*/false);
275  return State;
276 }
277 
279  const FormatToken &Current = *State.NextToken;
280  const FormatToken &Previous = *Current.Previous;
281  assert(&Previous == Current.Previous);
282  if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace &&
283  Current.closesBlockOrBlockTypeList(Style)))
284  return false;
285  // The opening "{" of a braced list has to be on the same line as the first
286  // element if it is nested in another braced init list or function call.
287  if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
288  Previous.isNot(TT_DictLiteral) && Previous.BlockKind == BK_BracedInit &&
289  Previous.Previous &&
290  Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
291  return false;
292  // This prevents breaks like:
293  // ...
294  // SomeParameter, OtherParameter).DoSomething(
295  // ...
296  // As they hide "DoSomething" and are generally bad for readability.
297  if (Previous.opensScope() && Previous.isNot(tok::l_brace) &&
298  State.LowestLevelOnLine < State.StartOfLineLevel &&
299  State.LowestLevelOnLine < Current.NestingLevel)
300  return false;
301  if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder)
302  return false;
303 
304  // Don't create a 'hanging' indent if there are multiple blocks in a single
305  // statement.
306  if (Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
307  State.Stack[State.Stack.size() - 2].NestedBlockInlined &&
308  State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks)
309  return false;
310 
311  // Don't break after very short return types (e.g. "void") as that is often
312  // unexpected.
313  if (Current.is(TT_FunctionDeclarationName) && State.Column < 6) {
315  return false;
316  }
317 
318  // If binary operators are moved to the next line (including commas for some
319  // styles of constructor initializers), that's always ok.
320  if (!Current.isOneOf(TT_BinaryOperator, tok::comma) &&
321  State.Stack.back().NoLineBreakInOperand)
322  return false;
323 
324  return !State.Stack.back().NoLineBreak;
325 }
326 
328  const FormatToken &Current = *State.NextToken;
329  const FormatToken &Previous = *Current.Previous;
330  if (Current.MustBreakBefore || Current.is(TT_InlineASMColon))
331  return true;
332  if (State.Stack.back().BreakBeforeClosingBrace &&
333  Current.closesBlockOrBlockTypeList(Style))
334  return true;
335  if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
336  return true;
337  if (Style.Language == FormatStyle::LK_ObjC &&
338  Current.ObjCSelectorNameParts > 1 &&
339  Current.startsSequence(TT_SelectorName, tok::colon, tok::caret)) {
340  return true;
341  }
342  if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
343  (Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) &&
344  Style.isCpp() &&
345  // FIXME: This is a temporary workaround for the case where clang-format
346  // sets BreakBeforeParameter to avoid bin packing and this creates a
347  // completely unnecessary line break after a template type that isn't
348  // line-wrapped.
349  (Previous.NestingLevel == 1 || Style.BinPackParameters)) ||
350  (Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) &&
351  Previous.isNot(tok::question)) ||
352  (!Style.BreakBeforeTernaryOperators &&
353  Previous.is(TT_ConditionalExpr))) &&
354  State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() &&
355  !Current.isOneOf(tok::r_paren, tok::r_brace))
356  return true;
357  if (((Previous.is(TT_DictLiteral) && Previous.is(tok::l_brace)) ||
358  (Previous.is(TT_ArrayInitializerLSquare) &&
359  Previous.ParameterCount > 1) ||
360  opensProtoMessageField(Previous, Style)) &&
361  Style.ColumnLimit > 0 &&
362  getLengthToMatchingParen(Previous, State.Stack) + State.Column - 1 >
363  getColumnLimit(State))
364  return true;
365 
366  const FormatToken &BreakConstructorInitializersToken =
368  ? Previous
369  : Current;
370  if (BreakConstructorInitializersToken.is(TT_CtorInitializerColon) &&
371  (State.Column + State.Line->Last->TotalLength - Previous.TotalLength >
372  getColumnLimit(State) ||
373  State.Stack.back().BreakBeforeParameter) &&
376  Style.ColumnLimit != 0))
377  return true;
378 
379  if (Current.is(TT_ObjCMethodExpr) && !Previous.is(TT_SelectorName) &&
380  State.Line->startsWith(TT_ObjCMethodSpecifier))
381  return true;
382  if (Current.is(TT_SelectorName) && State.Stack.back().ObjCSelectorNameFound &&
383  State.Stack.back().BreakBeforeParameter)
384  return true;
385 
386  unsigned NewLineColumn = getNewLineColumn(State);
387  if (Current.isMemberAccess() && Style.ColumnLimit != 0 &&
388  State.Column + getLengthToNextOperator(Current) > Style.ColumnLimit &&
389  (State.Column > NewLineColumn ||
390  Current.NestingLevel < State.StartOfLineLevel))
391  return true;
392 
393  if (startsSegmentOfBuilderTypeCall(Current) &&
394  (State.Stack.back().CallContinuation != 0 ||
395  State.Stack.back().BreakBeforeParameter) &&
396  // JavaScript is treated different here as there is a frequent pattern:
397  // SomeFunction(function() {
398  // ...
399  // }.bind(...));
400  // FIXME: We should find a more generic solution to this problem.
401  !(State.Column <= NewLineColumn &&
403  return true;
404 
405  // If the template declaration spans multiple lines, force wrap before the
406  // function/class declaration
407  if (Previous.ClosesTemplateDeclaration &&
408  State.Stack.back().BreakBeforeParameter)
409  return true;
410 
411  if (State.Column <= NewLineColumn)
412  return false;
413 
415  (NewLineColumn == State.FirstIndent + Style.ContinuationIndentWidth ||
416  Previous.is(tok::comma) || Current.NestingLevel < 2) &&
417  !Previous.isOneOf(tok::kw_return, tok::lessless, tok::at) &&
418  !Previous.isOneOf(TT_InlineASMColon, TT_ConditionalExpr) &&
419  nextIsMultilineString(State))
420  return true;
421 
422  // Using CanBreakBefore here and below takes care of the decision whether the
423  // current style uses wrapping before or after operators for the given
424  // operator.
425  if (Previous.is(TT_BinaryOperator) && Current.CanBreakBefore) {
426  // If we need to break somewhere inside the LHS of a binary expression, we
427  // should also break after the operator. Otherwise, the formatting would
428  // hide the operator precedence, e.g. in:
429  // if (aaaaaaaaaaaaaa ==
430  // bbbbbbbbbbbbbb && c) {..
431  // For comparisons, we only apply this rule, if the LHS is a binary
432  // expression itself as otherwise, the line breaks seem superfluous.
433  // We need special cases for ">>" which we have split into two ">" while
434  // lexing in order to make template parsing easier.
435  bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
436  Previous.getPrecedence() == prec::Equality ||
437  Previous.getPrecedence() == prec::Spaceship) &&
438  Previous.Previous &&
439  Previous.Previous->isNot(TT_BinaryOperator); // For >>.
440  bool LHSIsBinaryExpr =
441  Previous.Previous && Previous.Previous->EndsBinaryExpression;
442  if ((!IsComparison || LHSIsBinaryExpr) && !Current.isTrailingComment() &&
443  Previous.getPrecedence() != prec::Assignment &&
444  State.Stack.back().BreakBeforeParameter)
445  return true;
446  } else if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore &&
447  State.Stack.back().BreakBeforeParameter) {
448  return true;
449  }
450 
451  // Same as above, but for the first "<<" operator.
452  if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator) &&
453  State.Stack.back().BreakBeforeParameter &&
454  State.Stack.back().FirstLessLess == 0)
455  return true;
456 
457  if (Current.NestingLevel == 0 && !Current.isTrailingComment()) {
458  // Always break after "template <...>" and leading annotations. This is only
459  // for cases where the entire line does not fit on a single line as a
460  // different LineFormatter would be used otherwise.
461  if (Previous.ClosesTemplateDeclaration)
463  if (Previous.is(TT_FunctionAnnotationRParen))
464  return true;
465  if (Previous.is(TT_LeadingJavaAnnotation) && Current.isNot(tok::l_paren) &&
466  Current.isNot(TT_LeadingJavaAnnotation))
467  return true;
468  }
469 
470  // If the return type spans multiple lines, wrap before the function name.
471  if ((Current.is(TT_FunctionDeclarationName) ||
472  (Current.is(tok::kw_operator) && !Previous.is(tok::coloncolon))) &&
473  !Previous.is(tok::kw_template) && State.Stack.back().BreakBeforeParameter)
474  return true;
475 
476  // The following could be precomputed as they do not depend on the state.
477  // However, as they should take effect only if the UnwrappedLine does not fit
478  // into the ColumnLimit, they are checked here in the ContinuationIndenter.
479  if (Style.ColumnLimit != 0 && Previous.BlockKind == BK_Block &&
480  Previous.is(tok::l_brace) && !Current.isOneOf(tok::r_brace, tok::comment))
481  return true;
482 
483  if (Current.is(tok::lessless) &&
484  ((Previous.is(tok::identifier) && Previous.TokenText == "endl") ||
485  (Previous.Tok.isLiteral() && (Previous.TokenText.endswith("\\n\"") ||
486  Previous.TokenText == "\'\\n\'"))))
487  return true;
488 
489  if (Previous.is(TT_BlockComment) && Previous.IsMultiline)
490  return true;
491 
492  if (State.NoContinuation)
493  return true;
494 
495  return false;
496 }
497 
499  bool DryRun,
500  unsigned ExtraSpaces) {
501  const FormatToken &Current = *State.NextToken;
502 
503  assert(!State.Stack.empty());
504  State.NoContinuation = false;
505 
506  if ((Current.is(TT_ImplicitStringLiteral) &&
507  (Current.Previous->Tok.getIdentifierInfo() == nullptr ||
509  tok::pp_not_keyword))) {
510  unsigned EndColumn =
511  SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getEnd());
512  if (Current.LastNewlineOffset != 0) {
513  // If there is a newline within this token, the final column will solely
514  // determined by the current end column.
515  State.Column = EndColumn;
516  } else {
517  unsigned StartColumn =
518  SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getBegin());
519  assert(EndColumn >= StartColumn);
520  State.Column += EndColumn - StartColumn;
521  }
522  moveStateToNextToken(State, DryRun, /*Newline=*/false);
523  return 0;
524  }
525 
526  unsigned Penalty = 0;
527  if (Newline)
528  Penalty = addTokenOnNewLine(State, DryRun);
529  else
530  addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
531 
532  return moveStateToNextToken(State, DryRun, Newline) + Penalty;
533 }
534 
535 void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
536  unsigned ExtraSpaces) {
537  FormatToken &Current = *State.NextToken;
538  const FormatToken &Previous = *State.NextToken->Previous;
539  if (Current.is(tok::equal) &&
540  (State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) &&
541  State.Stack.back().VariablePos == 0) {
542  State.Stack.back().VariablePos = State.Column;
543  // Move over * and & if they are bound to the variable name.
544  const FormatToken *Tok = &Previous;
545  while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) {
546  State.Stack.back().VariablePos -= Tok->ColumnWidth;
547  if (Tok->SpacesRequiredBefore != 0)
548  break;
549  Tok = Tok->Previous;
550  }
551  if (Previous.PartOfMultiVariableDeclStmt)
552  State.Stack.back().LastSpace = State.Stack.back().VariablePos;
553  }
554 
555  unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
556 
557  // Indent preprocessor directives after the hash if required.
558  int PPColumnCorrection = 0;
560  Previous.is(tok::hash) && State.FirstIndent > 0 &&
561  (State.Line->Type == LT_PreprocessorDirective ||
562  State.Line->Type == LT_ImportStatement)) {
563  Spaces += State.FirstIndent;
564 
565  // For preprocessor indent with tabs, State.Column will be 1 because of the
566  // hash. This causes second-level indents onward to have an extra space
567  // after the tabs. We avoid this misalignment by subtracting 1 from the
568  // column value passed to replaceWhitespace().
569  if (Style.UseTab != FormatStyle::UT_Never)
570  PPColumnCorrection = -1;
571  }
572 
573  if (!DryRun)
574  Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces,
575  State.Column + Spaces + PPColumnCorrection);
576 
577  // If "BreakBeforeInheritanceComma" mode, don't break within the inheritance
578  // declaration unless there is multiple inheritance.
579  if (Style.BreakBeforeInheritanceComma && Current.is(TT_InheritanceColon))
580  State.Stack.back().NoLineBreak = true;
581 
582  if (Current.is(TT_SelectorName) &&
583  !State.Stack.back().ObjCSelectorNameFound) {
584  unsigned MinIndent =
586  State.Stack.back().Indent);
587  unsigned FirstColonPos = State.Column + Spaces + Current.ColumnWidth;
588  if (Current.LongestObjCSelectorName == 0)
589  State.Stack.back().AlignColons = false;
590  else if (MinIndent + Current.LongestObjCSelectorName > FirstColonPos)
591  State.Stack.back().ColonPos = MinIndent + Current.LongestObjCSelectorName;
592  else
593  State.Stack.back().ColonPos = FirstColonPos;
594  }
595 
596  // In "AlwaysBreak" mode, enforce wrapping directly after the parenthesis by
597  // disallowing any further line breaks if there is no line break after the
598  // opening parenthesis. Don't break if it doesn't conserve columns.
600  Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
601  State.Column > getNewLineColumn(State) &&
602  (!Previous.Previous || !Previous.Previous->isOneOf(
603  tok::kw_for, tok::kw_while, tok::kw_switch)) &&
604  // Don't do this for simple (no expressions) one-argument function calls
605  // as that feels like needlessly wasting whitespace, e.g.:
606  //
607  // caaaaaaaaaaaall(
608  // caaaaaaaaaaaall(
609  // caaaaaaaaaaaall(
610  // caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa))));
611  Current.FakeLParens.size() > 0 &&
612  Current.FakeLParens.back() > prec::Unknown)
613  State.Stack.back().NoLineBreak = true;
614  if (Previous.is(TT_TemplateString) && Previous.opensScope())
615  State.Stack.back().NoLineBreak = true;
616 
618  Previous.opensScope() && Previous.isNot(TT_ObjCMethodExpr) &&
619  (Current.isNot(TT_LineComment) || Previous.BlockKind == BK_BracedInit))
620  State.Stack.back().Indent = State.Column + Spaces;
621  if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style))
622  State.Stack.back().NoLineBreak = true;
623  if (startsSegmentOfBuilderTypeCall(Current) &&
624  State.Column > getNewLineColumn(State))
625  State.Stack.back().ContainsUnwrappedBuilder = true;
626 
627  if (Current.is(TT_LambdaArrow) && Style.Language == FormatStyle::LK_Java)
628  State.Stack.back().NoLineBreak = true;
629  if (Current.isMemberAccess() && Previous.is(tok::r_paren) &&
630  (Previous.MatchingParen &&
631  (Previous.TotalLength - Previous.MatchingParen->TotalLength > 10)))
632  // If there is a function call with long parameters, break before trailing
633  // calls. This prevents things like:
634  // EXPECT_CALL(SomeLongParameter).Times(
635  // 2);
636  // We don't want to do this for short parameters as they can just be
637  // indexes.
638  State.Stack.back().NoLineBreak = true;
639 
640  // Don't allow the RHS of an operator to be split over multiple lines unless
641  // there is a line-break right after the operator.
642  // Exclude relational operators, as there, it is always more desirable to
643  // have the LHS 'left' of the RHS.
644  const FormatToken *P = Current.getPreviousNonComment();
645  if (!Current.is(tok::comment) && P &&
646  (P->isOneOf(TT_BinaryOperator, tok::comma) ||
647  (P->is(TT_ConditionalExpr) && P->is(tok::colon))) &&
648  !P->isOneOf(TT_OverloadedOperator, TT_CtorInitializerComma) &&
651  P->getPrecedence() != prec::Spaceship) {
652  bool BreakBeforeOperator =
653  P->MustBreakBefore || P->is(tok::lessless) ||
654  (P->is(TT_BinaryOperator) &&
656  (P->is(TT_ConditionalExpr) && Style.BreakBeforeTernaryOperators);
657  // Don't do this if there are only two operands. In these cases, there is
658  // always a nice vertical separation between them and the extra line break
659  // does not help.
660  bool HasTwoOperands =
661  P->OperatorIndex == 0 && !P->NextOperator && !P->is(TT_ConditionalExpr);
662  if ((!BreakBeforeOperator && !(HasTwoOperands && Style.AlignOperands)) ||
663  (!State.Stack.back().LastOperatorWrapped && BreakBeforeOperator))
664  State.Stack.back().NoLineBreakInOperand = true;
665  }
666 
667  State.Column += Spaces;
668  if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) &&
669  Previous.Previous &&
670  (Previous.Previous->isOneOf(tok::kw_if, tok::kw_for) ||
671  Previous.Previous->endsSequence(tok::kw_constexpr, tok::kw_if))) {
672  // Treat the condition inside an if as if it was a second function
673  // parameter, i.e. let nested calls have a continuation indent.
674  State.Stack.back().LastSpace = State.Column;
675  State.Stack.back().NestedBlockIndent = State.Column;
676  } else if (!Current.isOneOf(tok::comment, tok::caret) &&
677  ((Previous.is(tok::comma) &&
678  !Previous.is(TT_OverloadedOperator)) ||
679  (Previous.is(tok::colon) && Previous.is(TT_ObjCMethodExpr)))) {
680  State.Stack.back().LastSpace = State.Column;
681  } else if (Previous.is(TT_CtorInitializerColon) &&
684  State.Stack.back().Indent = State.Column;
685  State.Stack.back().LastSpace = State.Column;
686  } else if ((Previous.isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
687  TT_CtorInitializerColon)) &&
688  ((Previous.getPrecedence() != prec::Assignment &&
689  (Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 ||
690  Previous.NextOperator)) ||
691  Current.StartsBinaryExpression)) {
692  // Indent relative to the RHS of the expression unless this is a simple
693  // assignment without binary expression on the RHS. Also indent relative to
694  // unary operators and the colons of constructor initializers.
695  State.Stack.back().LastSpace = State.Column;
696  } else if (Previous.is(TT_InheritanceColon)) {
697  State.Stack.back().Indent = State.Column;
698  State.Stack.back().LastSpace = State.Column;
699  } else if (Previous.opensScope()) {
700  // If a function has a trailing call, indent all parameters from the
701  // opening parenthesis. This avoids confusing indents like:
702  // OuterFunction(InnerFunctionCall( // break
703  // ParameterToInnerFunction)) // break
704  // .SecondInnerFunctionCall();
705  bool HasTrailingCall = false;
706  if (Previous.MatchingParen) {
707  const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
708  HasTrailingCall = Next && Next->isMemberAccess();
709  }
710  if (HasTrailingCall && State.Stack.size() > 1 &&
711  State.Stack[State.Stack.size() - 2].CallContinuation == 0)
712  State.Stack.back().LastSpace = State.Column;
713  }
714 }
715 
716 unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
717  bool DryRun) {
718  FormatToken &Current = *State.NextToken;
719  const FormatToken &Previous = *State.NextToken->Previous;
720 
721  // Extra penalty that needs to be added because of the way certain line
722  // breaks are chosen.
723  unsigned Penalty = 0;
724 
725  const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
726  const FormatToken *NextNonComment = Previous.getNextNonComment();
727  if (!NextNonComment)
728  NextNonComment = &Current;
729  // The first line break on any NestingLevel causes an extra penalty in order
730  // prefer similar line breaks.
731  if (!State.Stack.back().ContainsLineBreak)
732  Penalty += 15;
733  State.Stack.back().ContainsLineBreak = true;
734 
735  Penalty += State.NextToken->SplitPenalty;
736 
737  // Breaking before the first "<<" is generally not desirable if the LHS is
738  // short. Also always add the penalty if the LHS is split over multiple lines
739  // to avoid unnecessary line breaks that just work around this penalty.
740  if (NextNonComment->is(tok::lessless) &&
741  State.Stack.back().FirstLessLess == 0 &&
742  (State.Column <= Style.ColumnLimit / 3 ||
743  State.Stack.back().BreakBeforeParameter))
744  Penalty += Style.PenaltyBreakFirstLessLess;
745 
746  State.Column = getNewLineColumn(State);
747 
748  // Indent nested blocks relative to this column, unless in a very specific
749  // JavaScript special case where:
750  //
751  // var loooooong_name =
752  // function() {
753  // // code
754  // }
755  //
756  // is common and should be formatted like a free-standing function. The same
757  // goes for wrapping before the lambda return type arrow.
758  if (!Current.is(TT_LambdaArrow) &&
760  Current.NestingLevel != 0 || !PreviousNonComment ||
761  !PreviousNonComment->is(tok::equal) ||
762  !Current.isOneOf(Keywords.kw_async, Keywords.kw_function)))
763  State.Stack.back().NestedBlockIndent = State.Column;
764 
765  if (NextNonComment->isMemberAccess()) {
766  if (State.Stack.back().CallContinuation == 0)
767  State.Stack.back().CallContinuation = State.Column;
768  } else if (NextNonComment->is(TT_SelectorName)) {
769  if (!State.Stack.back().ObjCSelectorNameFound) {
770  if (NextNonComment->LongestObjCSelectorName == 0) {
771  State.Stack.back().AlignColons = false;
772  } else {
773  State.Stack.back().ColonPos =
775  ? std::max(State.Stack.back().Indent,
776  State.FirstIndent + Style.ContinuationIndentWidth)
777  : State.Stack.back().Indent) +
778  std::max(NextNonComment->LongestObjCSelectorName,
779  NextNonComment->ColumnWidth);
780  }
781  } else if (State.Stack.back().AlignColons &&
782  State.Stack.back().ColonPos <= NextNonComment->ColumnWidth) {
783  State.Stack.back().ColonPos = State.Column + NextNonComment->ColumnWidth;
784  }
785  } else if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
786  PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) {
787  // FIXME: This is hacky, find a better way. The problem is that in an ObjC
788  // method expression, the block should be aligned to the line starting it,
789  // e.g.:
790  // [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason
791  // ^(int *i) {
792  // // ...
793  // }];
794  // Thus, we set LastSpace of the next higher NestingLevel, to which we move
795  // when we consume all of the "}"'s FakeRParens at the "{".
796  if (State.Stack.size() > 1)
797  State.Stack[State.Stack.size() - 2].LastSpace =
798  std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
800  }
801 
802  if ((PreviousNonComment &&
803  PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
804  !State.Stack.back().AvoidBinPacking) ||
805  Previous.is(TT_BinaryOperator))
806  State.Stack.back().BreakBeforeParameter = false;
807  if (Previous.isOneOf(TT_TemplateCloser, TT_JavaAnnotation) &&
808  Current.NestingLevel == 0)
809  State.Stack.back().BreakBeforeParameter = false;
810  if (NextNonComment->is(tok::question) ||
811  (PreviousNonComment && PreviousNonComment->is(tok::question)))
812  State.Stack.back().BreakBeforeParameter = true;
813  if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore)
814  State.Stack.back().BreakBeforeParameter = false;
815 
816  if (!DryRun) {
817  unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1;
818  if (Current.is(tok::r_brace) && Current.MatchingParen &&
819  // Only strip trailing empty lines for l_braces that have children, i.e.
820  // for function expressions (lambdas, arrows, etc).
821  !Current.MatchingParen->Children.empty()) {
822  // lambdas and arrow functions are expressions, thus their r_brace is not
823  // on its own line, and thus not covered by UnwrappedLineFormatter's logic
824  // about removing empty lines on closing blocks. Special case them here.
825  MaxEmptyLinesToKeep = 1;
826  }
827  unsigned Newlines = std::max(
828  1u, std::min(Current.NewlinesBefore, MaxEmptyLinesToKeep));
829  bool ContinuePPDirective =
830  State.Line->InPPDirective && State.Line->Type != LT_ImportStatement;
831  Whitespaces.replaceWhitespace(Current, Newlines, State.Column, State.Column,
832  ContinuePPDirective);
833  }
834 
835  if (!Current.isTrailingComment())
836  State.Stack.back().LastSpace = State.Column;
837  if (Current.is(tok::lessless))
838  // If we are breaking before a "<<", we always want to indent relative to
839  // RHS. This is necessary only for "<<", as we special-case it and don't
840  // always indent relative to the RHS.
841  State.Stack.back().LastSpace += 3; // 3 -> width of "<< ".
842 
843  State.StartOfLineLevel = Current.NestingLevel;
844  State.LowestLevelOnLine = Current.NestingLevel;
845 
846  // Any break on this level means that the parent level has been broken
847  // and we need to avoid bin packing there.
848  bool NestedBlockSpecialCase =
849  !Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
850  State.Stack[State.Stack.size() - 2].NestedBlockInlined;
851  if (!NestedBlockSpecialCase)
852  for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
853  State.Stack[i].BreakBeforeParameter = true;
854 
855  if (PreviousNonComment &&
856  !PreviousNonComment->isOneOf(tok::comma, tok::colon, tok::semi) &&
857  (PreviousNonComment->isNot(TT_TemplateCloser) ||
858  Current.NestingLevel != 0) &&
859  !PreviousNonComment->isOneOf(
860  TT_BinaryOperator, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
861  TT_LeadingJavaAnnotation) &&
862  Current.isNot(TT_BinaryOperator) && !PreviousNonComment->opensScope())
863  State.Stack.back().BreakBeforeParameter = true;
864 
865  // If we break after { or the [ of an array initializer, we should also break
866  // before the corresponding } or ].
867  if (PreviousNonComment &&
868  (PreviousNonComment->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
869  opensProtoMessageField(*PreviousNonComment, Style)))
870  State.Stack.back().BreakBeforeClosingBrace = true;
871 
872  if (State.Stack.back().AvoidBinPacking) {
873  // If we are breaking after '(', '{', '<', this is not bin packing
874  // unless AllowAllParametersOfDeclarationOnNextLine is false or this is a
875  // dict/object literal.
876  if (!Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) ||
878  State.Line->MustBeDeclaration) ||
879  Previous.is(TT_DictLiteral))
880  State.Stack.back().BreakBeforeParameter = true;
881  }
882 
883  return Penalty;
884 }
885 
886 unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
887  if (!State.NextToken || !State.NextToken->Previous)
888  return 0;
889  FormatToken &Current = *State.NextToken;
890  const FormatToken &Previous = *Current.Previous;
891  // If we are continuing an expression, we want to use the continuation indent.
892  unsigned ContinuationIndent =
893  std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
895  const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
896  const FormatToken *NextNonComment = Previous.getNextNonComment();
897  if (!NextNonComment)
898  NextNonComment = &Current;
899 
900  // Java specific bits.
901  if (Style.Language == FormatStyle::LK_Java &&
902  Current.isOneOf(Keywords.kw_implements, Keywords.kw_extends))
903  return std::max(State.Stack.back().LastSpace,
904  State.Stack.back().Indent + Style.ContinuationIndentWidth);
905 
906  if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block)
907  return Current.NestingLevel == 0 ? State.FirstIndent
908  : State.Stack.back().Indent;
909  if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
910  (Current.is(tok::greater) &&
911  (Style.Language == FormatStyle::LK_Proto ||
912  Style.Language == FormatStyle::LK_TextProto))) &&
913  State.Stack.size() > 1) {
914  if (Current.closesBlockOrBlockTypeList(Style))
915  return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
916  if (Current.MatchingParen &&
918  return State.Stack[State.Stack.size() - 2].LastSpace;
919  return State.FirstIndent;
920  }
921  // Indent a closing parenthesis at the previous level if followed by a semi or
922  // opening brace. This allows indentations such as:
923  // foo(
924  // a,
925  // );
926  // function foo(
927  // a,
928  // ) {
929  // code(); //
930  // }
931  if (Current.is(tok::r_paren) && State.Stack.size() > 1 &&
932  (!Current.Next || Current.Next->isOneOf(tok::semi, tok::l_brace)))
933  return State.Stack[State.Stack.size() - 2].LastSpace;
934  if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope())
935  return State.Stack[State.Stack.size() - 2].LastSpace;
936  if (Current.is(tok::identifier) && Current.Next &&
937  (Current.Next->is(TT_DictLiteral) ||
938  ((Style.Language == FormatStyle::LK_Proto ||
940  Current.Next->isOneOf(tok::less, tok::l_brace))))
941  return State.Stack.back().Indent;
942  if (NextNonComment->is(TT_ObjCStringLiteral) &&
943  State.StartOfStringLiteral != 0)
944  return State.StartOfStringLiteral - 1;
945  if (NextNonComment->isStringLiteral() && State.StartOfStringLiteral != 0)
946  return State.StartOfStringLiteral;
947  if (NextNonComment->is(tok::lessless) &&
948  State.Stack.back().FirstLessLess != 0)
949  return State.Stack.back().FirstLessLess;
950  if (NextNonComment->isMemberAccess()) {
951  if (State.Stack.back().CallContinuation == 0)
952  return ContinuationIndent;
953  return State.Stack.back().CallContinuation;
954  }
955  if (State.Stack.back().QuestionColumn != 0 &&
956  ((NextNonComment->is(tok::colon) &&
957  NextNonComment->is(TT_ConditionalExpr)) ||
958  Previous.is(TT_ConditionalExpr)))
959  return State.Stack.back().QuestionColumn;
960  if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0)
961  return State.Stack.back().VariablePos;
962  if ((PreviousNonComment &&
963  (PreviousNonComment->ClosesTemplateDeclaration ||
964  PreviousNonComment->isOneOf(
965  TT_AttributeParen, TT_AttributeSquare, TT_FunctionAnnotationRParen,
966  TT_JavaAnnotation, TT_LeadingJavaAnnotation))) ||
967  (!Style.IndentWrappedFunctionNames &&
968  NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName)))
969  return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent);
970  if (NextNonComment->is(TT_SelectorName)) {
971  if (!State.Stack.back().ObjCSelectorNameFound) {
972  unsigned MinIndent = State.Stack.back().Indent;
973  if (shouldIndentWrappedSelectorName(Style, State.Line->Type))
974  MinIndent = std::max(MinIndent,
975  State.FirstIndent + Style.ContinuationIndentWidth);
976  // If LongestObjCSelectorName is 0, we are indenting the first
977  // part of an ObjC selector (or a selector component which is
978  // not colon-aligned due to block formatting).
979  //
980  // Otherwise, we are indenting a subsequent part of an ObjC
981  // selector which should be colon-aligned to the longest
982  // component of the ObjC selector.
983  //
984  // In either case, we want to respect Style.IndentWrappedFunctionNames.
985  return MinIndent +
986  std::max(NextNonComment->LongestObjCSelectorName,
987  NextNonComment->ColumnWidth) -
988  NextNonComment->ColumnWidth;
989  }
990  if (!State.Stack.back().AlignColons)
991  return State.Stack.back().Indent;
992  if (State.Stack.back().ColonPos > NextNonComment->ColumnWidth)
993  return State.Stack.back().ColonPos - NextNonComment->ColumnWidth;
994  return State.Stack.back().Indent;
995  }
996  if (NextNonComment->is(tok::colon) && NextNonComment->is(TT_ObjCMethodExpr))
997  return State.Stack.back().ColonPos;
998  if (NextNonComment->is(TT_ArraySubscriptLSquare)) {
999  if (State.Stack.back().StartOfArraySubscripts != 0)
1000  return State.Stack.back().StartOfArraySubscripts;
1001  return ContinuationIndent;
1002  }
1003 
1004  // This ensure that we correctly format ObjC methods calls without inputs,
1005  // i.e. where the last element isn't selector like: [callee method];
1006  if (NextNonComment->is(tok::identifier) && NextNonComment->FakeRParens == 0 &&
1007  NextNonComment->Next && NextNonComment->Next->is(TT_ObjCMethodExpr))
1008  return State.Stack.back().Indent;
1009 
1010  if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) ||
1011  Previous.isOneOf(tok::coloncolon, tok::equal, TT_JsTypeColon))
1012  return ContinuationIndent;
1013  if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
1014  PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))
1015  return ContinuationIndent;
1016  if (NextNonComment->is(TT_CtorInitializerComma))
1017  return State.Stack.back().Indent;
1018  if (PreviousNonComment && PreviousNonComment->is(TT_CtorInitializerColon) &&
1020  return State.Stack.back().Indent;
1021  if (NextNonComment->isOneOf(TT_CtorInitializerColon, TT_InheritanceColon,
1022  TT_InheritanceComma))
1023  return State.FirstIndent + Style.ConstructorInitializerIndentWidth;
1024  if (Previous.is(tok::r_paren) && !Current.isBinaryOperator() &&
1025  !Current.isOneOf(tok::colon, tok::comment))
1026  return ContinuationIndent;
1027  if (Current.is(TT_ProtoExtensionLSquare))
1028  return State.Stack.back().Indent;
1029  if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment &&
1030  PreviousNonComment->isNot(tok::r_brace))
1031  // Ensure that we fall back to the continuation indent width instead of
1032  // just flushing continuations left.
1033  return State.Stack.back().Indent + Style.ContinuationIndentWidth;
1034  return State.Stack.back().Indent;
1035 }
1036 
1037 unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
1038  bool DryRun, bool Newline) {
1039  assert(State.Stack.size());
1040  const FormatToken &Current = *State.NextToken;
1041 
1042  if (Current.isOneOf(tok::comma, TT_BinaryOperator))
1043  State.Stack.back().NoLineBreakInOperand = false;
1044  if (Current.is(TT_InheritanceColon))
1045  State.Stack.back().AvoidBinPacking = true;
1046  if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator)) {
1047  if (State.Stack.back().FirstLessLess == 0)
1048  State.Stack.back().FirstLessLess = State.Column;
1049  else
1050  State.Stack.back().LastOperatorWrapped = Newline;
1051  }
1052  if (Current.is(TT_BinaryOperator) && Current.isNot(tok::lessless))
1053  State.Stack.back().LastOperatorWrapped = Newline;
1054  if (Current.is(TT_ConditionalExpr) && Current.Previous &&
1055  !Current.Previous->is(TT_ConditionalExpr))
1056  State.Stack.back().LastOperatorWrapped = Newline;
1057  if (Current.is(TT_ArraySubscriptLSquare) &&
1058  State.Stack.back().StartOfArraySubscripts == 0)
1059  State.Stack.back().StartOfArraySubscripts = State.Column;
1060  if (Style.BreakBeforeTernaryOperators && Current.is(tok::question))
1061  State.Stack.back().QuestionColumn = State.Column;
1062  if (!Style.BreakBeforeTernaryOperators && Current.isNot(tok::colon)) {
1063  const FormatToken *Previous = Current.Previous;
1064  while (Previous && Previous->isTrailingComment())
1065  Previous = Previous->Previous;
1066  if (Previous && Previous->is(tok::question))
1067  State.Stack.back().QuestionColumn = State.Column;
1068  }
1069  if (!Current.opensScope() && !Current.closesScope() &&
1070  !Current.is(TT_PointerOrReference))
1071  State.LowestLevelOnLine =
1072  std::min(State.LowestLevelOnLine, Current.NestingLevel);
1073  if (Current.isMemberAccess())
1074  State.Stack.back().StartOfFunctionCall =
1075  !Current.NextOperator ? 0 : State.Column;
1076  if (Current.is(TT_SelectorName) &&
1077  !State.Stack.back().ObjCSelectorNameFound) {
1078  State.Stack.back().ObjCSelectorNameFound = true;
1079 
1080  // Reevaluate whether ObjC message arguments fit into one line.
1081  // If a receiver spans multiple lines, e.g.:
1082  // [[object block:^{
1083  // return 42;
1084  // }] a:42 b:42];
1085  // BreakBeforeParameter is calculated based on an incorrect assumption
1086  // (it is checked whether the whole expression fits into one line without
1087  // considering a line break inside a message receiver).
1088  if (Current.Previous && Current.Previous->closesScope() &&
1089  Current.Previous->MatchingParen &&
1090  Current.Previous->MatchingParen->Previous) {
1091  const FormatToken &CurrentScopeOpener =
1092  *Current.Previous->MatchingParen->Previous;
1093  if (CurrentScopeOpener.is(TT_ObjCMethodExpr) &&
1094  CurrentScopeOpener.MatchingParen) {
1095  int NecessarySpaceInLine =
1096  getLengthToMatchingParen(CurrentScopeOpener, State.Stack) +
1097  CurrentScopeOpener.TotalLength - Current.TotalLength - 1;
1098  if (State.Column + Current.ColumnWidth + NecessarySpaceInLine <=
1099  Style.ColumnLimit)
1100  State.Stack.back().BreakBeforeParameter = false;
1101  }
1102  }
1103  }
1104  if (Current.is(TT_CtorInitializerColon) &&
1106  // Indent 2 from the column, so:
1107  // SomeClass::SomeClass()
1108  // : First(...), ...
1109  // Next(...)
1110  // ^ line up here.
1111  State.Stack.back().Indent =
1112  State.Column +
1114  ? 0
1115  : 2);
1116  State.Stack.back().NestedBlockIndent = State.Stack.back().Indent;
1118  State.Stack.back().AvoidBinPacking = true;
1119  State.Stack.back().BreakBeforeParameter = false;
1120  }
1121  if (Current.is(TT_CtorInitializerColon) &&
1123  State.Stack.back().Indent =
1125  State.Stack.back().NestedBlockIndent = State.Stack.back().Indent;
1127  State.Stack.back().AvoidBinPacking = true;
1128  }
1129  if (Current.is(TT_InheritanceColon))
1130  State.Stack.back().Indent =
1131  State.FirstIndent + Style.ContinuationIndentWidth;
1132  if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && Newline)
1133  State.Stack.back().NestedBlockIndent =
1134  State.Column + Current.ColumnWidth + 1;
1135  if (Current.isOneOf(TT_LambdaLSquare, TT_LambdaArrow))
1136  State.Stack.back().LastSpace = State.Column;
1137 
1138  // Insert scopes created by fake parenthesis.
1139  const FormatToken *Previous = Current.getPreviousNonComment();
1140 
1141  // Add special behavior to support a format commonly used for JavaScript
1142  // closures:
1143  // SomeFunction(function() {
1144  // foo();
1145  // bar();
1146  // }, a, b, c);
1147  if (Current.isNot(tok::comment) && Previous &&
1148  Previous->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
1149  !Previous->is(TT_DictLiteral) && State.Stack.size() > 1) {
1150  if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline)
1151  for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
1152  State.Stack[i].NoLineBreak = true;
1153  State.Stack[State.Stack.size() - 2].NestedBlockInlined = false;
1154  }
1155  if (Previous &&
1156  (Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) ||
1157  Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr)) &&
1158  !Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
1159  State.Stack.back().NestedBlockInlined =
1160  !Newline &&
1161  (Previous->isNot(tok::l_paren) || Previous->ParameterCount > 1);
1162  }
1163 
1164  moveStatePastFakeLParens(State, Newline);
1165  moveStatePastScopeCloser(State);
1166  bool AllowBreak = !State.Stack.back().NoLineBreak &&
1167  !State.Stack.back().NoLineBreakInOperand;
1168  moveStatePastScopeOpener(State, Newline);
1169  moveStatePastFakeRParens(State);
1170 
1171  if (Current.is(TT_ObjCStringLiteral) && State.StartOfStringLiteral == 0)
1172  State.StartOfStringLiteral = State.Column + 1;
1173  else if (Current.isStringLiteral() && State.StartOfStringLiteral == 0)
1174  State.StartOfStringLiteral = State.Column;
1175  else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
1176  !Current.isStringLiteral())
1177  State.StartOfStringLiteral = 0;
1178 
1179  State.Column += Current.ColumnWidth;
1180  State.NextToken = State.NextToken->Next;
1181 
1182  unsigned Penalty =
1183  handleEndOfLine(Current, State, DryRun, AllowBreak);
1184 
1185  if (Current.Role)
1186  Current.Role->formatFromToken(State, this, DryRun);
1187  // If the previous has a special role, let it consume tokens as appropriate.
1188  // It is necessary to start at the previous token for the only implemented
1189  // role (comma separated list). That way, the decision whether or not to break
1190  // after the "{" is already done and both options are tried and evaluated.
1191  // FIXME: This is ugly, find a better way.
1192  if (Previous && Previous->Role)
1193  Penalty += Previous->Role->formatAfterToken(State, this, DryRun);
1194 
1195  return Penalty;
1196 }
1197 
1198 void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
1199  bool Newline) {
1200  const FormatToken &Current = *State.NextToken;
1201  const FormatToken *Previous = Current.getPreviousNonComment();
1202 
1203  // Don't add extra indentation for the first fake parenthesis after
1204  // 'return', assignments or opening <({[. The indentation for these cases
1205  // is special cased.
1206  bool SkipFirstExtraIndent =
1207  (Previous && (Previous->opensScope() ||
1208  Previous->isOneOf(tok::semi, tok::kw_return) ||
1209  (Previous->getPrecedence() == prec::Assignment &&
1210  Style.AlignOperands) ||
1211  Previous->is(TT_ObjCMethodExpr)));
1213  I = Current.FakeLParens.rbegin(),
1214  E = Current.FakeLParens.rend();
1215  I != E; ++I) {
1216  ParenState NewParenState = State.Stack.back();
1217  NewParenState.Tok = nullptr;
1218  NewParenState.ContainsLineBreak = false;
1219  NewParenState.LastOperatorWrapped = true;
1220  NewParenState.NoLineBreak =
1221  NewParenState.NoLineBreak || State.Stack.back().NoLineBreakInOperand;
1222 
1223  // Don't propagate AvoidBinPacking into subexpressions of arg/param lists.
1224  if (*I > prec::Comma)
1225  NewParenState.AvoidBinPacking = false;
1226 
1227  // Indent from 'LastSpace' unless these are fake parentheses encapsulating
1228  // a builder type call after 'return' or, if the alignment after opening
1229  // brackets is disabled.
1230  if (!Current.isTrailingComment() &&
1231  (Style.AlignOperands || *I < prec::Assignment) &&
1232  (!Previous || Previous->isNot(tok::kw_return) ||
1233  (Style.Language != FormatStyle::LK_Java && *I > 0)) &&
1235  *I != prec::Comma || Current.NestingLevel == 0))
1236  NewParenState.Indent =
1237  std::max(std::max(State.Column, NewParenState.Indent),
1238  State.Stack.back().LastSpace);
1239 
1240  // Do not indent relative to the fake parentheses inserted for "." or "->".
1241  // This is a special case to make the following to statements consistent:
1242  // OuterFunction(InnerFunctionCall( // break
1243  // ParameterToInnerFunction));
1244  // OuterFunction(SomeObject.InnerFunctionCall( // break
1245  // ParameterToInnerFunction));
1246  if (*I > prec::Unknown)
1247  NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
1248  if (*I != prec::Conditional && !Current.is(TT_UnaryOperator) &&
1250  NewParenState.StartOfFunctionCall = State.Column;
1251 
1252  // Always indent conditional expressions. Never indent expression where
1253  // the 'operator' is ',', ';' or an assignment (i.e. *I <=
1254  // prec::Assignment) as those have different indentation rules. Indent
1255  // other expression, unless the indentation needs to be skipped.
1256  if (*I == prec::Conditional ||
1257  (!SkipFirstExtraIndent && *I > prec::Assignment &&
1258  !Current.isTrailingComment()))
1259  NewParenState.Indent += Style.ContinuationIndentWidth;
1260  if ((Previous && !Previous->opensScope()) || *I != prec::Comma)
1261  NewParenState.BreakBeforeParameter = false;
1262  State.Stack.push_back(NewParenState);
1263  SkipFirstExtraIndent = false;
1264  }
1265 }
1266 
1267 void ContinuationIndenter::moveStatePastFakeRParens(LineState &State) {
1268  for (unsigned i = 0, e = State.NextToken->FakeRParens; i != e; ++i) {
1269  unsigned VariablePos = State.Stack.back().VariablePos;
1270  if (State.Stack.size() == 1) {
1271  // Do not pop the last element.
1272  break;
1273  }
1274  State.Stack.pop_back();
1275  State.Stack.back().VariablePos = VariablePos;
1276  }
1277 }
1278 
1279 void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
1280  bool Newline) {
1281  const FormatToken &Current = *State.NextToken;
1282  if (!Current.opensScope())
1283  return;
1284 
1285  if (Current.MatchingParen && Current.BlockKind == BK_Block) {
1286  moveStateToNewBlock(State);
1287  return;
1288  }
1289 
1290  unsigned NewIndent;
1291  unsigned LastSpace = State.Stack.back().LastSpace;
1292  bool AvoidBinPacking;
1293  bool BreakBeforeParameter = false;
1294  unsigned NestedBlockIndent = std::max(State.Stack.back().StartOfFunctionCall,
1295  State.Stack.back().NestedBlockIndent);
1296  if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
1297  opensProtoMessageField(Current, Style)) {
1298  if (Current.opensBlockOrBlockTypeList(Style)) {
1299  NewIndent = Style.IndentWidth +
1300  std::min(State.Column, State.Stack.back().NestedBlockIndent);
1301  } else {
1302  NewIndent = State.Stack.back().LastSpace + Style.ContinuationIndentWidth;
1303  }
1304  const FormatToken *NextNoComment = Current.getNextNonComment();
1305  bool EndsInComma = Current.MatchingParen &&
1306  Current.MatchingParen->Previous &&
1307  Current.MatchingParen->Previous->is(tok::comma);
1308  AvoidBinPacking = EndsInComma || Current.is(TT_DictLiteral) ||
1309  Style.Language == FormatStyle::LK_Proto ||
1311  !Style.BinPackArguments ||
1312  (NextNoComment &&
1313  NextNoComment->isOneOf(TT_DesignatedInitializerPeriod,
1314  TT_DesignatedInitializerLSquare));
1315  BreakBeforeParameter = EndsInComma;
1316  if (Current.ParameterCount > 1)
1317  NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1);
1318  } else {
1319  NewIndent = Style.ContinuationIndentWidth +
1320  std::max(State.Stack.back().LastSpace,
1321  State.Stack.back().StartOfFunctionCall);
1322 
1323  // Ensure that different different brackets force relative alignment, e.g.:
1324  // void SomeFunction(vector< // break
1325  // int> v);
1326  // FIXME: We likely want to do this for more combinations of brackets.
1327  if (Current.is(tok::less) && Current.ParentBracket == tok::l_paren) {
1328  NewIndent = std::max(NewIndent, State.Stack.back().Indent);
1329  LastSpace = std::max(LastSpace, State.Stack.back().Indent);
1330  }
1331 
1332  bool EndsInComma =
1333  Current.MatchingParen &&
1334  Current.MatchingParen->getPreviousNonComment() &&
1335  Current.MatchingParen->getPreviousNonComment()->is(tok::comma);
1336 
1337  // If ObjCBinPackProtocolList is unspecified, fall back to BinPackParameters
1338  // for backwards compatibility.
1339  bool ObjCBinPackProtocolList =
1341  Style.BinPackParameters) ||
1343 
1344  bool BinPackDeclaration =
1345  (State.Line->Type != LT_ObjCDecl && Style.BinPackParameters) ||
1346  (State.Line->Type == LT_ObjCDecl && ObjCBinPackProtocolList);
1347 
1348  AvoidBinPacking =
1349  (Style.Language == FormatStyle::LK_JavaScript && EndsInComma) ||
1350  (State.Line->MustBeDeclaration && !BinPackDeclaration) ||
1351  (!State.Line->MustBeDeclaration && !Style.BinPackArguments) ||
1353  (Current.PackingKind == PPK_OnePerLine ||
1354  (!BinPackInconclusiveFunctions &&
1355  Current.PackingKind == PPK_Inconclusive)));
1356 
1357  if (Current.is(TT_ObjCMethodExpr) && Current.MatchingParen) {
1358  if (Style.ColumnLimit) {
1359  // If this '[' opens an ObjC call, determine whether all parameters fit
1360  // into one line and put one per line if they don't.
1361  if (getLengthToMatchingParen(Current, State.Stack) + State.Column >
1362  getColumnLimit(State))
1363  BreakBeforeParameter = true;
1364  } else {
1365  // For ColumnLimit = 0, we have to figure out whether there is or has to
1366  // be a line break within this call.
1367  for (const FormatToken *Tok = &Current;
1368  Tok && Tok != Current.MatchingParen; Tok = Tok->Next) {
1369  if (Tok->MustBreakBefore ||
1370  (Tok->CanBreakBefore && Tok->NewlinesBefore > 0)) {
1371  BreakBeforeParameter = true;
1372  break;
1373  }
1374  }
1375  }
1376  }
1377 
1378  if (Style.Language == FormatStyle::LK_JavaScript && EndsInComma)
1379  BreakBeforeParameter = true;
1380  }
1381  // Generally inherit NoLineBreak from the current scope to nested scope.
1382  // However, don't do this for non-empty nested blocks, dict literals and
1383  // array literals as these follow different indentation rules.
1384  bool NoLineBreak =
1385  Current.Children.empty() &&
1386  !Current.isOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) &&
1387  (State.Stack.back().NoLineBreak ||
1388  State.Stack.back().NoLineBreakInOperand ||
1389  (Current.is(TT_TemplateOpener) &&
1390  State.Stack.back().ContainsUnwrappedBuilder));
1391  State.Stack.push_back(
1392  ParenState(&Current, NewIndent, LastSpace, AvoidBinPacking, NoLineBreak));
1393  State.Stack.back().NestedBlockIndent = NestedBlockIndent;
1394  State.Stack.back().BreakBeforeParameter = BreakBeforeParameter;
1395  State.Stack.back().HasMultipleNestedBlocks = Current.BlockParameterCount > 1;
1396  State.Stack.back().IsInsideObjCArrayLiteral =
1397  Current.is(TT_ArrayInitializerLSquare) && Current.Previous &&
1398  Current.Previous->is(tok::at);
1399 }
1400 
1401 void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
1402  const FormatToken &Current = *State.NextToken;
1403  if (!Current.closesScope())
1404  return;
1405 
1406  // If we encounter a closing ), ], } or >, we can remove a level from our
1407  // stacks.
1408  if (State.Stack.size() > 1 &&
1409  (Current.isOneOf(tok::r_paren, tok::r_square, TT_TemplateString) ||
1410  (Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
1411  State.NextToken->is(TT_TemplateCloser) ||
1412  (Current.is(tok::greater) && Current.is(TT_DictLiteral))))
1413  State.Stack.pop_back();
1414 
1415  if (Current.is(tok::r_square)) {
1416  // If this ends the array subscript expr, reset the corresponding value.
1417  const FormatToken *NextNonComment = Current.getNextNonComment();
1418  if (NextNonComment && NextNonComment->isNot(tok::l_square))
1419  State.Stack.back().StartOfArraySubscripts = 0;
1420  }
1421 }
1422 
1423 void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
1424  unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
1425  // ObjC block sometimes follow special indentation rules.
1426  unsigned NewIndent =
1427  NestedBlockIndent + (State.NextToken->is(TT_ObjCBlockLBrace)
1428  ? Style.ObjCBlockIndentWidth
1429  : Style.IndentWidth);
1430  State.Stack.push_back(ParenState(State.NextToken, NewIndent,
1431  State.Stack.back().LastSpace,
1432  /*AvoidBinPacking=*/true,
1433  /*NoLineBreak=*/false));
1434  State.Stack.back().NestedBlockIndent = NestedBlockIndent;
1435  State.Stack.back().BreakBeforeParameter = true;
1436 }
1437 
1438 static unsigned getLastLineEndColumn(StringRef Text, unsigned StartColumn,
1439  unsigned TabWidth,
1440  encoding::Encoding Encoding) {
1441  size_t LastNewlinePos = Text.find_last_of("\n");
1442  if (LastNewlinePos == StringRef::npos) {
1443  return StartColumn +
1444  encoding::columnWidthWithTabs(Text, StartColumn, TabWidth, Encoding);
1445  } else {
1446  return encoding::columnWidthWithTabs(Text.substr(LastNewlinePos),
1447  /*StartColumn=*/0, TabWidth, Encoding);
1448  }
1449 }
1450 
1451 unsigned ContinuationIndenter::reformatRawStringLiteral(
1452  const FormatToken &Current, LineState &State,
1453  const FormatStyle &RawStringStyle, bool DryRun) {
1454  unsigned StartColumn = State.Column - Current.ColumnWidth;
1455  StringRef OldDelimiter = *getRawStringDelimiter(Current.TokenText);
1456  StringRef NewDelimiter =
1457  getCanonicalRawStringDelimiter(Style, RawStringStyle.Language);
1458  if (NewDelimiter.empty() || OldDelimiter.empty())
1459  NewDelimiter = OldDelimiter;
1460  // The text of a raw string is between the leading 'R"delimiter(' and the
1461  // trailing 'delimiter)"'.
1462  unsigned OldPrefixSize = 3 + OldDelimiter.size();
1463  unsigned OldSuffixSize = 2 + OldDelimiter.size();
1464  // We create a virtual text environment which expects a null-terminated
1465  // string, so we cannot use StringRef.
1466  std::string RawText =
1467  Current.TokenText.substr(OldPrefixSize).drop_back(OldSuffixSize);
1468  if (NewDelimiter != OldDelimiter) {
1469  // Don't update to the canonical delimiter 'deli' if ')deli"' occurs in the
1470  // raw string.
1471  std::string CanonicalDelimiterSuffix = (")" + NewDelimiter + "\"").str();
1472  if (StringRef(RawText).contains(CanonicalDelimiterSuffix))
1473  NewDelimiter = OldDelimiter;
1474  }
1475 
1476  unsigned NewPrefixSize = 3 + NewDelimiter.size();
1477  unsigned NewSuffixSize = 2 + NewDelimiter.size();
1478 
1479  // The first start column is the column the raw text starts after formatting.
1480  unsigned FirstStartColumn = StartColumn + NewPrefixSize;
1481 
1482  // The next start column is the intended indentation a line break inside
1483  // the raw string at level 0. It is determined by the following rules:
1484  // - if the content starts on newline, it is one level more than the current
1485  // indent, and
1486  // - if the content does not start on a newline, it is the first start
1487  // column.
1488  // These rules have the advantage that the formatted content both does not
1489  // violate the rectangle rule and visually flows within the surrounding
1490  // source.
1491  bool ContentStartsOnNewline = Current.TokenText[OldPrefixSize] == '\n';
1492  unsigned NextStartColumn =
1493  ContentStartsOnNewline
1494  ? State.Stack.back().NestedBlockIndent + Style.IndentWidth
1495  : FirstStartColumn;
1496 
1497  // The last start column is the column the raw string suffix starts if it is
1498  // put on a newline.
1499  // The last start column is the intended indentation of the raw string postfix
1500  // if it is put on a newline. It is determined by the following rules:
1501  // - if the raw string prefix starts on a newline, it is the column where
1502  // that raw string prefix starts, and
1503  // - if the raw string prefix does not start on a newline, it is the current
1504  // indent.
1505  unsigned LastStartColumn = Current.NewlinesBefore
1506  ? FirstStartColumn - NewPrefixSize
1507  : State.Stack.back().NestedBlockIndent;
1508 
1509  std::pair<tooling::Replacements, unsigned> Fixes = internal::reformat(
1510  RawStringStyle, RawText, {tooling::Range(0, RawText.size())},
1511  FirstStartColumn, NextStartColumn, LastStartColumn, "<stdin>",
1512  /*Status=*/nullptr);
1513 
1514  auto NewCode = applyAllReplacements(RawText, Fixes.first);
1515  tooling::Replacements NoFixes;
1516  if (!NewCode) {
1517  State.Column += Current.ColumnWidth;
1518  return 0;
1519  }
1520  if (!DryRun) {
1521  if (NewDelimiter != OldDelimiter) {
1522  // In 'R"delimiter(...', the delimiter starts 2 characters after the start
1523  // of the token.
1524  SourceLocation PrefixDelimiterStart =
1525  Current.Tok.getLocation().getLocWithOffset(2);
1526  auto PrefixErr = Whitespaces.addReplacement(tooling::Replacement(
1527  SourceMgr, PrefixDelimiterStart, OldDelimiter.size(), NewDelimiter));
1528  if (PrefixErr) {
1529  llvm::errs()
1530  << "Failed to update the prefix delimiter of a raw string: "
1531  << llvm::toString(std::move(PrefixErr)) << "\n";
1532  }
1533  // In 'R"delimiter(...)delimiter"', the suffix delimiter starts at
1534  // position length - 1 - |delimiter|.
1535  SourceLocation SuffixDelimiterStart =
1536  Current.Tok.getLocation().getLocWithOffset(Current.TokenText.size() -
1537  1 - OldDelimiter.size());
1538  auto SuffixErr = Whitespaces.addReplacement(tooling::Replacement(
1539  SourceMgr, SuffixDelimiterStart, OldDelimiter.size(), NewDelimiter));
1540  if (SuffixErr) {
1541  llvm::errs()
1542  << "Failed to update the suffix delimiter of a raw string: "
1543  << llvm::toString(std::move(SuffixErr)) << "\n";
1544  }
1545  }
1546  SourceLocation OriginLoc =
1547  Current.Tok.getLocation().getLocWithOffset(OldPrefixSize);
1548  for (const tooling::Replacement &Fix : Fixes.first) {
1549  auto Err = Whitespaces.addReplacement(tooling::Replacement(
1550  SourceMgr, OriginLoc.getLocWithOffset(Fix.getOffset()),
1551  Fix.getLength(), Fix.getReplacementText()));
1552  if (Err) {
1553  llvm::errs() << "Failed to reformat raw string: "
1554  << llvm::toString(std::move(Err)) << "\n";
1555  }
1556  }
1557  }
1558  unsigned RawLastLineEndColumn = getLastLineEndColumn(
1559  *NewCode, FirstStartColumn, Style.TabWidth, Encoding);
1560  State.Column = RawLastLineEndColumn + NewSuffixSize;
1561  // Since we're updating the column to after the raw string literal here, we
1562  // have to manually add the penalty for the prefix R"delim( over the column
1563  // limit.
1564  unsigned PrefixExcessCharacters =
1565  StartColumn + NewPrefixSize > Style.ColumnLimit ?
1566  StartColumn + NewPrefixSize - Style.ColumnLimit : 0;
1567  return Fixes.second + PrefixExcessCharacters * Style.PenaltyExcessCharacter;
1568 }
1569 
1570 unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
1571  LineState &State) {
1572  // Break before further function parameters on all levels.
1573  for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
1574  State.Stack[i].BreakBeforeParameter = true;
1575 
1576  unsigned ColumnsUsed = State.Column;
1577  // We can only affect layout of the first and the last line, so the penalty
1578  // for all other lines is constant, and we ignore it.
1579  State.Column = Current.LastLineColumnWidth;
1580 
1581  if (ColumnsUsed > getColumnLimit(State))
1582  return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
1583  return 0;
1584 }
1585 
1586 unsigned ContinuationIndenter::handleEndOfLine(const FormatToken &Current,
1587  LineState &State, bool DryRun,
1588  bool AllowBreak) {
1589  unsigned Penalty = 0;
1590  // Compute the raw string style to use in case this is a raw string literal
1591  // that can be reformatted.
1592  auto RawStringStyle = getRawStringStyle(Current, State);
1593  if (RawStringStyle && !Current.Finalized) {
1594  Penalty = reformatRawStringLiteral(Current, State, *RawStringStyle, DryRun);
1595  } else if (Current.IsMultiline && Current.isNot(TT_BlockComment)) {
1596  // Don't break multi-line tokens other than block comments and raw string
1597  // literals. Instead, just update the state.
1598  Penalty = addMultilineToken(Current, State);
1599  } else if (State.Line->Type != LT_ImportStatement) {
1600  // We generally don't break import statements.
1601  LineState OriginalState = State;
1602 
1603  // Whether we force the reflowing algorithm to stay strictly within the
1604  // column limit.
1605  bool Strict = false;
1606  // Whether the first non-strict attempt at reflowing did intentionally
1607  // exceed the column limit.
1608  bool Exceeded = false;
1609  std::tie(Penalty, Exceeded) = breakProtrudingToken(
1610  Current, State, AllowBreak, /*DryRun=*/true, Strict);
1611  if (Exceeded) {
1612  // If non-strict reflowing exceeds the column limit, try whether strict
1613  // reflowing leads to an overall lower penalty.
1614  LineState StrictState = OriginalState;
1615  unsigned StrictPenalty =
1616  breakProtrudingToken(Current, StrictState, AllowBreak,
1617  /*DryRun=*/true, /*Strict=*/true)
1618  .first;
1619  Strict = StrictPenalty <= Penalty;
1620  if (Strict) {
1621  Penalty = StrictPenalty;
1622  State = StrictState;
1623  }
1624  }
1625  if (!DryRun) {
1626  // If we're not in dry-run mode, apply the changes with the decision on
1627  // strictness made above.
1628  breakProtrudingToken(Current, OriginalState, AllowBreak, /*DryRun=*/false,
1629  Strict);
1630  }
1631  }
1632  if (State.Column > getColumnLimit(State)) {
1633  unsigned ExcessCharacters = State.Column - getColumnLimit(State);
1634  Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
1635  }
1636  return Penalty;
1637 }
1638 
1639 // Returns the enclosing function name of a token, or the empty string if not
1640 // found.
1641 static StringRef getEnclosingFunctionName(const FormatToken &Current) {
1642  // Look for: 'function(' or 'function<templates>(' before Current.
1643  auto Tok = Current.getPreviousNonComment();
1644  if (!Tok || !Tok->is(tok::l_paren))
1645  return "";
1647  if (!Tok)
1648  return "";
1649  if (Tok->is(TT_TemplateCloser)) {
1650  Tok = Tok->MatchingParen;
1651  if (Tok)
1653  }
1654  if (!Tok || !Tok->is(tok::identifier))
1655  return "";
1656  return Tok->TokenText;
1657 }
1658 
1660 ContinuationIndenter::getRawStringStyle(const FormatToken &Current,
1661  const LineState &State) {
1662  if (!Current.isStringLiteral())
1663  return None;
1664  auto Delimiter = getRawStringDelimiter(Current.TokenText);
1665  if (!Delimiter)
1666  return None;
1667  auto RawStringStyle = RawStringFormats.getDelimiterStyle(*Delimiter);
1668  if (!RawStringStyle)
1669  RawStringStyle = RawStringFormats.getEnclosingFunctionStyle(
1670  getEnclosingFunctionName(Current));
1671  if (!RawStringStyle)
1672  return None;
1673  RawStringStyle->ColumnLimit = getColumnLimit(State);
1674  return RawStringStyle;
1675 }
1676 
1677 std::unique_ptr<BreakableToken> ContinuationIndenter::createBreakableToken(
1678  const FormatToken &Current, LineState &State, bool AllowBreak) {
1679  unsigned StartColumn = State.Column - Current.ColumnWidth;
1680  if (Current.isStringLiteral()) {
1681  // FIXME: String literal breaking is currently disabled for Java and JS, as
1682  // it requires strings to be merged using "+" which we don't support.
1683  if (Style.Language == FormatStyle::LK_Java ||
1685  !Style.BreakStringLiterals ||
1686  !AllowBreak)
1687  return nullptr;
1688 
1689  // Don't break string literals inside preprocessor directives (except for
1690  // #define directives, as their contents are stored in separate lines and
1691  // are not affected by this check).
1692  // This way we avoid breaking code with line directives and unknown
1693  // preprocessor directives that contain long string literals.
1694  if (State.Line->Type == LT_PreprocessorDirective)
1695  return nullptr;
1696  // Exempts unterminated string literals from line breaking. The user will
1697  // likely want to terminate the string before any line breaking is done.
1698  if (Current.IsUnterminatedLiteral)
1699  return nullptr;
1700  // Don't break string literals inside Objective-C array literals (doing so
1701  // raises the warning -Wobjc-string-concatenation).
1702  if (State.Stack.back().IsInsideObjCArrayLiteral) {
1703  return nullptr;
1704  }
1705 
1706  StringRef Text = Current.TokenText;
1707  StringRef Prefix;
1708  StringRef Postfix;
1709  // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
1710  // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
1711  // reduce the overhead) for each FormatToken, which is a string, so that we
1712  // don't run multiple checks here on the hot path.
1713  if ((Text.endswith(Postfix = "\"") &&
1714  (Text.startswith(Prefix = "@\"") || Text.startswith(Prefix = "\"") ||
1715  Text.startswith(Prefix = "u\"") || Text.startswith(Prefix = "U\"") ||
1716  Text.startswith(Prefix = "u8\"") ||
1717  Text.startswith(Prefix = "L\""))) ||
1718  (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")"))) {
1719  // We need this to address the case where there is an unbreakable tail
1720  // only if certain other formatting decisions have been taken. The
1721  // UnbreakableTailLength of Current is an overapproximation is that case
1722  // and we need to be correct here.
1723  unsigned UnbreakableTailLength = (State.NextToken && canBreak(State))
1724  ? 0
1725  : Current.UnbreakableTailLength;
1726  return llvm::make_unique<BreakableStringLiteral>(
1727  Current, StartColumn, Prefix, Postfix, UnbreakableTailLength,
1728  State.Line->InPPDirective, Encoding, Style);
1729  }
1730  } else if (Current.is(TT_BlockComment)) {
1731  if (!Style.ReflowComments ||
1732  // If a comment token switches formatting, like
1733  // /* clang-format on */, we don't want to break it further,
1734  // but we may still want to adjust its indentation.
1735  switchesFormatting(Current)) {
1736  return nullptr;
1737  }
1738  return llvm::make_unique<BreakableBlockComment>(
1739  Current, StartColumn, Current.OriginalColumn, !Current.Previous,
1740  State.Line->InPPDirective, Encoding, Style);
1741  } else if (Current.is(TT_LineComment) &&
1742  (Current.Previous == nullptr ||
1743  Current.Previous->isNot(TT_ImplicitStringLiteral))) {
1744  if (!Style.ReflowComments ||
1745  CommentPragmasRegex.match(Current.TokenText.substr(2)) ||
1746  switchesFormatting(Current))
1747  return nullptr;
1748  return llvm::make_unique<BreakableLineCommentSection>(
1749  Current, StartColumn, Current.OriginalColumn, !Current.Previous,
1750  /*InPPDirective=*/false, Encoding, Style);
1751  }
1752  return nullptr;
1753 }
1754 
1755 std::pair<unsigned, bool>
1756 ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
1757  LineState &State, bool AllowBreak,
1758  bool DryRun, bool Strict) {
1759  std::unique_ptr<const BreakableToken> Token =
1760  createBreakableToken(Current, State, AllowBreak);
1761  if (!Token)
1762  return {0, false};
1763  assert(Token->getLineCount() > 0);
1764  unsigned ColumnLimit = getColumnLimit(State);
1765  if (Current.is(TT_LineComment)) {
1766  // We don't insert backslashes when breaking line comments.
1767  ColumnLimit = Style.ColumnLimit;
1768  }
1769  if (Current.UnbreakableTailLength >= ColumnLimit)
1770  return {0, false};
1771  // ColumnWidth was already accounted into State.Column before calling
1772  // breakProtrudingToken.
1773  unsigned StartColumn = State.Column - Current.ColumnWidth;
1774  unsigned NewBreakPenalty = Current.isStringLiteral()
1775  ? Style.PenaltyBreakString
1776  : Style.PenaltyBreakComment;
1777  // Stores whether we intentionally decide to let a line exceed the column
1778  // limit.
1779  bool Exceeded = false;
1780  // Stores whether we introduce a break anywhere in the token.
1781  bool BreakInserted = Token->introducesBreakBeforeToken();
1782  // Store whether we inserted a new line break at the end of the previous
1783  // logical line.
1784  bool NewBreakBefore = false;
1785  // We use a conservative reflowing strategy. Reflow starts after a line is
1786  // broken or the corresponding whitespace compressed. Reflow ends as soon as a
1787  // line that doesn't get reflown with the previous line is reached.
1788  bool Reflow = false;
1789  // Keep track of where we are in the token:
1790  // Where we are in the content of the current logical line.
1791  unsigned TailOffset = 0;
1792  // The column number we're currently at.
1793  unsigned ContentStartColumn =
1794  Token->getContentStartColumn(0, /*Break=*/false);
1795  // The number of columns left in the current logical line after TailOffset.
1796  unsigned RemainingTokenColumns =
1797  Token->getRemainingLength(0, TailOffset, ContentStartColumn);
1798  // Adapt the start of the token, for example indent.
1799  if (!DryRun)
1800  Token->adaptStartOfLine(0, Whitespaces);
1801 
1802  unsigned Penalty = 0;
1803  LLVM_DEBUG(llvm::dbgs() << "Breaking protruding token at column "
1804  << StartColumn << ".\n");
1805  for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
1806  LineIndex != EndIndex; ++LineIndex) {
1807  LLVM_DEBUG(llvm::dbgs()
1808  << " Line: " << LineIndex << " (Reflow: " << Reflow << ")\n");
1809  NewBreakBefore = false;
1810  // If we did reflow the previous line, we'll try reflowing again. Otherwise
1811  // we'll start reflowing if the current line is broken or whitespace is
1812  // compressed.
1813  bool TryReflow = Reflow;
1814  // Break the current token until we can fit the rest of the line.
1815  while (ContentStartColumn + RemainingTokenColumns > ColumnLimit) {
1816  LLVM_DEBUG(llvm::dbgs() << " Over limit, need: "
1817  << (ContentStartColumn + RemainingTokenColumns)
1818  << ", space: " << ColumnLimit
1819  << ", reflown prefix: " << ContentStartColumn
1820  << ", offset in line: " << TailOffset << "\n");
1821  // If the current token doesn't fit, find the latest possible split in the
1822  // current line so that breaking at it will be under the column limit.
1823  // FIXME: Use the earliest possible split while reflowing to correctly
1824  // compress whitespace within a line.
1825  BreakableToken::Split Split =
1826  Token->getSplit(LineIndex, TailOffset, ColumnLimit,
1827  ContentStartColumn, CommentPragmasRegex);
1828  if (Split.first == StringRef::npos) {
1829  // No break opportunity - update the penalty and continue with the next
1830  // logical line.
1831  if (LineIndex < EndIndex - 1)
1832  // The last line's penalty is handled in addNextStateToQueue().
1833  Penalty += Style.PenaltyExcessCharacter *
1834  (ContentStartColumn + RemainingTokenColumns - ColumnLimit);
1835  LLVM_DEBUG(llvm::dbgs() << " No break opportunity.\n");
1836  break;
1837  }
1838  assert(Split.first != 0);
1839 
1840  if (Token->supportsReflow()) {
1841  // Check whether the next natural split point after the current one can
1842  // still fit the line, either because we can compress away whitespace,
1843  // or because the penalty the excess characters introduce is lower than
1844  // the break penalty.
1845  // We only do this for tokens that support reflowing, and thus allow us
1846  // to change the whitespace arbitrarily (e.g. comments).
1847  // Other tokens, like string literals, can be broken on arbitrary
1848  // positions.
1849 
1850  // First, compute the columns from TailOffset to the next possible split
1851  // position.
1852  // For example:
1853  // ColumnLimit: |
1854  // // Some text that breaks
1855  // ^ tail offset
1856  // ^-- split
1857  // ^-------- to split columns
1858  // ^--- next split
1859  // ^--------------- to next split columns
1860  unsigned ToSplitColumns = Token->getRangeLength(
1861  LineIndex, TailOffset, Split.first, ContentStartColumn);
1862  LLVM_DEBUG(llvm::dbgs() << " ToSplit: " << ToSplitColumns << "\n");
1863 
1864  BreakableToken::Split NextSplit = Token->getSplit(
1865  LineIndex, TailOffset + Split.first + Split.second, ColumnLimit,
1866  ContentStartColumn + ToSplitColumns + 1, CommentPragmasRegex);
1867  // Compute the columns necessary to fit the next non-breakable sequence
1868  // into the current line.
1869  unsigned ToNextSplitColumns = 0;
1870  if (NextSplit.first == StringRef::npos) {
1871  ToNextSplitColumns = Token->getRemainingLength(LineIndex, TailOffset,
1872  ContentStartColumn);
1873  } else {
1874  ToNextSplitColumns = Token->getRangeLength(
1875  LineIndex, TailOffset,
1876  Split.first + Split.second + NextSplit.first, ContentStartColumn);
1877  }
1878  // Compress the whitespace between the break and the start of the next
1879  // unbreakable sequence.
1880  ToNextSplitColumns =
1881  Token->getLengthAfterCompression(ToNextSplitColumns, Split);
1882  LLVM_DEBUG(llvm::dbgs()
1883  << " ContentStartColumn: " << ContentStartColumn << "\n");
1884  LLVM_DEBUG(llvm::dbgs()
1885  << " ToNextSplit: " << ToNextSplitColumns << "\n");
1886  // If the whitespace compression makes us fit, continue on the current
1887  // line.
1888  bool ContinueOnLine =
1889  ContentStartColumn + ToNextSplitColumns <= ColumnLimit;
1890  unsigned ExcessCharactersPenalty = 0;
1891  if (!ContinueOnLine && !Strict) {
1892  // Similarly, if the excess characters' penalty is lower than the
1893  // penalty of introducing a new break, continue on the current line.
1894  ExcessCharactersPenalty =
1895  (ContentStartColumn + ToNextSplitColumns - ColumnLimit) *
1896  Style.PenaltyExcessCharacter;
1897  LLVM_DEBUG(llvm::dbgs()
1898  << " Penalty excess: " << ExcessCharactersPenalty
1899  << "\n break : " << NewBreakPenalty << "\n");
1900  if (ExcessCharactersPenalty < NewBreakPenalty) {
1901  Exceeded = true;
1902  ContinueOnLine = true;
1903  }
1904  }
1905  if (ContinueOnLine) {
1906  LLVM_DEBUG(llvm::dbgs() << " Continuing on line...\n");
1907  // The current line fits after compressing the whitespace - reflow
1908  // the next line into it if possible.
1909  TryReflow = true;
1910  if (!DryRun)
1911  Token->compressWhitespace(LineIndex, TailOffset, Split,
1912  Whitespaces);
1913  // When we continue on the same line, leave one space between content.
1914  ContentStartColumn += ToSplitColumns + 1;
1915  Penalty += ExcessCharactersPenalty;
1916  TailOffset += Split.first + Split.second;
1917  RemainingTokenColumns = Token->getRemainingLength(
1918  LineIndex, TailOffset, ContentStartColumn);
1919  continue;
1920  }
1921  }
1922  LLVM_DEBUG(llvm::dbgs() << " Breaking...\n");
1923  ContentStartColumn =
1924  Token->getContentStartColumn(LineIndex, /*Break=*/true);
1925  unsigned NewRemainingTokenColumns = Token->getRemainingLength(
1926  LineIndex, TailOffset + Split.first + Split.second,
1927  ContentStartColumn);
1928 
1929  // When breaking before a tab character, it may be moved by a few columns,
1930  // but will still be expanded to the next tab stop, so we don't save any
1931  // columns.
1932  if (NewRemainingTokenColumns == RemainingTokenColumns) {
1933  // FIXME: Do we need to adjust the penalty?
1934  break;
1935  }
1936  assert(NewRemainingTokenColumns < RemainingTokenColumns);
1937 
1938  LLVM_DEBUG(llvm::dbgs() << " Breaking at: " << TailOffset + Split.first
1939  << ", " << Split.second << "\n");
1940  if (!DryRun)
1941  Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
1942 
1943  Penalty += NewBreakPenalty;
1944  TailOffset += Split.first + Split.second;
1945  RemainingTokenColumns = NewRemainingTokenColumns;
1946  BreakInserted = true;
1947  NewBreakBefore = true;
1948  }
1949  // In case there's another line, prepare the state for the start of the next
1950  // line.
1951  if (LineIndex + 1 != EndIndex) {
1952  unsigned NextLineIndex = LineIndex + 1;
1953  if (NewBreakBefore)
1954  // After breaking a line, try to reflow the next line into the current
1955  // one once RemainingTokenColumns fits.
1956  TryReflow = true;
1957  if (TryReflow) {
1958  // We decided that we want to try reflowing the next line into the
1959  // current one.
1960  // We will now adjust the state as if the reflow is successful (in
1961  // preparation for the next line), and see whether that works. If we
1962  // decide that we cannot reflow, we will later reset the state to the
1963  // start of the next line.
1964  Reflow = false;
1965  // As we did not continue breaking the line, RemainingTokenColumns is
1966  // known to fit after ContentStartColumn. Adapt ContentStartColumn to
1967  // the position at which we want to format the next line if we do
1968  // actually reflow.
1969  // When we reflow, we need to add a space between the end of the current
1970  // line and the next line's start column.
1971  ContentStartColumn += RemainingTokenColumns + 1;
1972  // Get the split that we need to reflow next logical line into the end
1973  // of the current one; the split will include any leading whitespace of
1974  // the next logical line.
1975  BreakableToken::Split SplitBeforeNext =
1976  Token->getReflowSplit(NextLineIndex, CommentPragmasRegex);
1977  LLVM_DEBUG(llvm::dbgs()
1978  << " Size of reflown text: " << ContentStartColumn
1979  << "\n Potential reflow split: ");
1980  if (SplitBeforeNext.first != StringRef::npos) {
1981  LLVM_DEBUG(llvm::dbgs() << SplitBeforeNext.first << ", "
1982  << SplitBeforeNext.second << "\n");
1983  TailOffset = SplitBeforeNext.first + SplitBeforeNext.second;
1984  // If the rest of the next line fits into the current line below the
1985  // column limit, we can safely reflow.
1986  RemainingTokenColumns = Token->getRemainingLength(
1987  NextLineIndex, TailOffset, ContentStartColumn);
1988  Reflow = true;
1989  if (ContentStartColumn + RemainingTokenColumns > ColumnLimit) {
1990  LLVM_DEBUG(llvm::dbgs()
1991  << " Over limit after reflow, need: "
1992  << (ContentStartColumn + RemainingTokenColumns)
1993  << ", space: " << ColumnLimit
1994  << ", reflown prefix: " << ContentStartColumn
1995  << ", offset in line: " << TailOffset << "\n");
1996  // If the whole next line does not fit, try to find a point in
1997  // the next line at which we can break so that attaching the part
1998  // of the next line to that break point onto the current line is
1999  // below the column limit.
2000  BreakableToken::Split Split =
2001  Token->getSplit(NextLineIndex, TailOffset, ColumnLimit,
2002  ContentStartColumn, CommentPragmasRegex);
2003  if (Split.first == StringRef::npos) {
2004  LLVM_DEBUG(llvm::dbgs() << " Did not find later break\n");
2005  Reflow = false;
2006  } else {
2007  // Check whether the first split point gets us below the column
2008  // limit. Note that we will execute this split below as part of
2009  // the normal token breaking and reflow logic within the line.
2010  unsigned ToSplitColumns = Token->getRangeLength(
2011  NextLineIndex, TailOffset, Split.first, ContentStartColumn);
2012  if (ContentStartColumn + ToSplitColumns > ColumnLimit) {
2013  LLVM_DEBUG(llvm::dbgs() << " Next split protrudes, need: "
2014  << (ContentStartColumn + ToSplitColumns)
2015  << ", space: " << ColumnLimit);
2016  unsigned ExcessCharactersPenalty =
2017  (ContentStartColumn + ToSplitColumns - ColumnLimit) *
2018  Style.PenaltyExcessCharacter;
2019  if (NewBreakPenalty < ExcessCharactersPenalty) {
2020  Reflow = false;
2021  }
2022  }
2023  }
2024  }
2025  } else {
2026  LLVM_DEBUG(llvm::dbgs() << "not found.\n");
2027  }
2028  }
2029  if (!Reflow) {
2030  // If we didn't reflow into the next line, the only space to consider is
2031  // the next logical line. Reset our state to match the start of the next
2032  // line.
2033  TailOffset = 0;
2034  ContentStartColumn =
2035  Token->getContentStartColumn(NextLineIndex, /*Break=*/false);
2036  RemainingTokenColumns = Token->getRemainingLength(
2037  NextLineIndex, TailOffset, ContentStartColumn);
2038  // Adapt the start of the token, for example indent.
2039  if (!DryRun)
2040  Token->adaptStartOfLine(NextLineIndex, Whitespaces);
2041  } else {
2042  // If we found a reflow split and have added a new break before the next
2043  // line, we are going to remove the line break at the start of the next
2044  // logical line. For example, here we'll add a new line break after
2045  // 'text', and subsequently delete the line break between 'that' and
2046  // 'reflows'.
2047  // // some text that
2048  // // reflows
2049  // ->
2050  // // some text
2051  // // that reflows
2052  // When adding the line break, we also added the penalty for it, so we
2053  // need to subtract that penalty again when we remove the line break due
2054  // to reflowing.
2055  if (NewBreakBefore) {
2056  assert(Penalty >= NewBreakPenalty);
2057  Penalty -= NewBreakPenalty;
2058  }
2059  if (!DryRun)
2060  Token->reflow(NextLineIndex, Whitespaces);
2061  }
2062  }
2063  }
2064 
2065  BreakableToken::Split SplitAfterLastLine =
2066  Token->getSplitAfterLastLine(TailOffset);
2067  if (SplitAfterLastLine.first != StringRef::npos) {
2068  LLVM_DEBUG(llvm::dbgs() << "Replacing whitespace after last line.\n");
2069  if (!DryRun)
2070  Token->replaceWhitespaceAfterLastLine(TailOffset, SplitAfterLastLine,
2071  Whitespaces);
2072  ContentStartColumn =
2073  Token->getContentStartColumn(Token->getLineCount() - 1, /*Break=*/true);
2074  RemainingTokenColumns = Token->getRemainingLength(
2075  Token->getLineCount() - 1,
2076  TailOffset + SplitAfterLastLine.first + SplitAfterLastLine.second,
2077  ContentStartColumn);
2078  }
2079 
2080  State.Column = ContentStartColumn + RemainingTokenColumns -
2081  Current.UnbreakableTailLength;
2082 
2083  if (BreakInserted) {
2084  // If we break the token inside a parameter list, we need to break before
2085  // the next parameter on all levels, so that the next parameter is clearly
2086  // visible. Line comments already introduce a break.
2087  if (Current.isNot(TT_LineComment)) {
2088  for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
2089  State.Stack[i].BreakBeforeParameter = true;
2090  }
2091 
2092  if (Current.is(TT_BlockComment))
2093  State.NoContinuation = true;
2094 
2095  State.Stack.back().LastSpace = StartColumn;
2096  }
2097 
2098  Token->updateNextToken(State);
2099 
2100  return {Penalty, Exceeded};
2101 }
2102 
2103 unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
2104  // In preprocessor directives reserve two chars for trailing " \"
2105  return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
2106 }
2107 
2108 bool ContinuationIndenter::nextIsMultilineString(const LineState &State) {
2109  const FormatToken &Current = *State.NextToken;
2110  if (!Current.isStringLiteral() || Current.is(TT_ImplicitStringLiteral))
2111  return false;
2112  // We never consider raw string literals "multiline" for the purpose of
2113  // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased
2114  // (see TokenAnnotator::mustBreakBefore().
2115  if (Current.TokenText.startswith("R\""))
2116  return false;
2117  if (Current.IsMultiline)
2118  return true;
2119  if (Current.getNextNonComment() &&
2120  Current.getNextNonComment()->isStringLiteral())
2121  return true; // Implicit concatenation.
2122  if (Style.ColumnLimit != 0 && Style.BreakStringLiterals &&
2123  State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
2124  Style.ColumnLimit)
2125  return true; // String will be split.
2126  return false;
2127 }
2128 
2129 } // namespace format
2130 } // namespace clang
static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok)
static bool shouldIndentWrappedSelectorName(const FormatStyle &Style, LineType LineType)
static StringRef getEnclosingFunctionName(const FormatToken &Current)
bool endsSequence(A K1, Ts... Tokens) const
true if this token ends a sequence with the given tokens in order, following the Previous pointers...
Definition: FormatToken.h:332
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
unsigned LowestLevelOnLine
The lowest NestingLevel on the current line.
unsigned NestingLevel
The nesting level of this token, i.e.
Definition: FormatToken.h:231
bool switchesFormatting(const FormatToken &Token)
Checks if Token switches formatting, like /* clang-format off.
bool AlwaysBreakBeforeMultilineStrings
If true, always break before multiline string literals.
Definition: Format.h:352
Token Tok
The Token.
Definition: FormatToken.h:127
bool ContainsLineBreak
true if this ParenState already contains a line-break.
Should be used for Protocol Buffers (https://developers.google.com/protocol-buffers/).
Definition: Format.h:1170
Defines the SourceManager interface.
unsigned IndentWidth
The number of columns to use for indentation.
Definition: Format.h:1087
std::unique_ptr< TokenRole > Role
A token can have a special role that can carry extra information about the token&#39;s formatting...
Definition: FormatToken.h:207
Break constructor initializers before the colon and commas, and align the commas with the colon...
Definition: Format.h:855
unsigned OriginalColumn
The original 0-based column of this token, including expanded tabs.
Definition: FormatToken.h:218
bool isMemberAccess() const
Returns true if this is a "." or "->" accessing a member.
Definition: FormatToken.h:380
Declares BreakableToken, BreakableStringLiteral, BreakableComment, BreakableBlockComment and Breakabl...
StringRef P
static unsigned getLastLineEndColumn(StringRef Text, unsigned StartColumn, unsigned TabWidth, encoding::Encoding Encoding)
bool BreakBeforeClosingBrace
Whether a newline needs to be inserted before the block&#39;s closing brace.
const FormatToken * getNextNonComment() const
Returns the next token ignoring comments.
Definition: FormatToken.h:470
Maintains a set of replacements that are conflict-free.
Definition: Replacement.h:210
bool IsMultiline
Whether the token text contains newlines (escaped or not).
Definition: FormatToken.h:156
unsigned ObjCSelectorNameParts
How many parts ObjC selector have (i.e.
Definition: FormatToken.h:248
unsigned PenaltyBreakFirstLessLess
The penalty for breaking before the first <<.
Definition: Format.h:1323
LanguageKind
Supported languages.
Definition: Format.h:1157
LineState getInitialState(unsigned FirstIndent, unsigned FirstStartColumn, const AnnotatedLine *Line, bool DryRun)
Get the initial state, i.e.
bool EndsBinaryExpression
true if this token ends a binary expression.
Definition: FormatToken.h:263
unsigned TotalLength
The total length of the unwrapped line up to and including this token.
Definition: FormatToken.h:214
bool isBinaryOperator() const
Definition: FormatToken.h:402
PPDirectiveIndentStyle IndentPPDirectives
The preprocessor directive indenting style to use.
Definition: Format.h:1074
unsigned NewlinesBefore
The number of newlines immediately before the Token.
Definition: FormatToken.h:133
FormatToken * Next
The next token in the unwrapped line.
Definition: FormatToken.h:290
Break after operators.
Definition: Format.h:449
bool isLiteral() const
Return true if this is a "literal", like a numeric constant, string, etc.
Definition: Token.h:113
Break constructor initializers before the colon and after the commas.
Definition: Format.h:847
unsigned UnbreakableTailLength
The length of following tokens until the next natural split point, or the next token that can be brok...
Definition: FormatToken.h:222
bool closesScope() const
Returns whether Tok is )]} or a closing > of a template or in protos.
Definition: FormatToken.h:370
bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite)
Apply all replacements in Replaces to the Rewriter Rewrite.
unsigned SplitPenalty
Penalty for inserting a line break before this token.
Definition: FormatToken.h:237
prec::Level getPrecedence() const
Definition: FormatToken.h:456
unsigned ParameterCount
Number of parameters, if this is "(", "[" or "<".
Definition: FormatToken.h:195
unsigned FakeRParens
Insert this many fake ) after this token for correct indentation.
Definition: FormatToken.h:257
bool ExperimentalAutoDetectBinPacking
If true, clang-format detects whether function calls and definitions are formatted with one parameter...
Definition: Format.h:1004
Always bin-pack parameters.
Definition: Format.h:431
LineState State
bool CanBreakBefore
true if it is allowed to break before this token.
Definition: FormatToken.h:185
This file declares Format APIs to be used internally by the formatting library implementation.
unsigned Column
The number of used columns in the current line.
bool NoContinuation
true if NextToken should not continue this line.
Should be used for Java.
Definition: Format.h:1163
FormatToken * Previous
The previous token in the unwrapped line.
Definition: FormatToken.h:287
bool StartsBinaryExpression
true if this token starts a binary expression, i.e.
Definition: FormatToken.h:261
std::pair< tooling::Replacements, unsigned > reformat(const FormatStyle &Style, StringRef Code, ArrayRef< tooling::Range > Ranges, unsigned FirstStartColumn, unsigned NextStartColumn, unsigned LastStartColumn, StringRef FileName, FormattingAttemptStatus *Status)
Reformats the given Ranges in the code fragment Code.
Definition: Format.cpp:1940
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
unsigned columnWidthWithTabs(StringRef Text, unsigned StartColumn, unsigned TabWidth, Encoding Encoding)
Returns the number of columns required to display the Text, starting from the StartColumn on a termin...
Definition: Encoding.h:62
unsigned LongestObjCSelectorName
If this is the first ObjC selector name in an ObjC method definition or call, this contains the lengt...
Definition: FormatToken.h:244
Manages the whitespaces around tokens and their replacements.
unsigned getColumnLimit(const LineState &State) const
Get the column limit for this line.
bool ConstructorInitializerAllOnOneLineOrOnePerLine
If the constructor initializers don&#39;t fit on a line, put each initializer on its own line...
Definition: Format.h:946
unsigned PenaltyBreakComment
The penalty for each line break introduced inside a comment.
Definition: Format.h:1320
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:267
unsigned SpacesRequiredBefore
The number of spaces that should be inserted before this token.
Definition: FormatToken.h:182
bool IndentWrappedFunctionNames
Indent if a function definition or declaration is wrapped after the type.
Definition: Format.h:1100
const FormatToken * Tok
The token opening this parenthesis level, or nullptr if this level is opened by fake parenthesis...
unsigned Indent
The position to which a specific parenthesis level needs to be indented.
bool isNot(T Kind) const
Definition: FormatToken.h:320
Always break after an open bracket, if the parameters don&#39;t fit on a single line, e...
Definition: Format.h:72
static unsigned getLengthToNextOperator(const FormatToken &Tok)
bool closesBlockOrBlockTypeList(const FormatStyle &Style) const
Same as opensBlockOrBlockTypeList, but for the closing token.
Definition: FormatToken.h:505
const FormatToken & Tok
ReturnTypeBreakingStyle AlwaysBreakAfterReturnType
The function declaration return type breaking style to use.
Definition: Format.h:338
unsigned BlockParameterCount
Number of parameters that are nested blocks, if this is "(", "[" or "<".
Definition: FormatToken.h:199
const AnnotatedLine * Line
The line that is being formatted.
bool BinPackArguments
If false, a function call&#39;s arguments will either be all on the same line or will have one line each...
Definition: Format.h:409
unsigned ObjCBlockIndentWidth
The number of characters to use for indentation of ObjC blocks.
Definition: Format.h:1303
bool LineContainsContinuedForLoopSection
true if this line contains a continued for-loop section.
unsigned ColumnLimit
The column limit.
Definition: Format.h:885
bool LastOperatorWrapped
True if the last binary operator on this level was wrapped to the next line.
llvm::Optional< FormatStyle > getEnclosingFunctionStyle(StringRef EnclosingFunction) const
bool BreakBeforeParameter
Break after the next comma (or all the commas in this context if AvoidBinPacking is true)...
FormatToken * getPreviousNonComment() const
Returns the previous token ignoring comments.
Definition: FormatToken.h:462
BracketAlignmentStyle AlignAfterOpenBracket
If true, horizontally aligns arguments after an open bracket.
Definition: Format.h:79
bool isOneOf(A K1, B K2) const
Definition: FormatToken.h:313
A text replacement.
Definition: Replacement.h:84
UseTabStyle UseTab
The way to use tab characters in the resulting file.
Definition: Format.h:1641
A source range independent of the SourceManager.
Definition: Replacement.h:45
FormatStyle getLLVMStyle()
Returns a format style complying with the LLVM coding standards: http://llvm.org/docs/CodingStandards...
Definition: Format.cpp:597
The current state when indenting a unwrapped line.
Break after return type automatically.
Definition: Format.h:274
WhitespaceManager class manages whitespace around tokens and their replacements.
SourceLocation End
llvm::Error addReplacement(const tooling::Replacement &Replacement)
Should be used for JavaScript.
Definition: Format.h:1165
const AnnotatedLine * Line
StateNode * Previous
ParameterPackingKind PackingKind
If this is an opening parenthesis, how are the parameters packed?
Definition: FormatToken.h:210
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Definition: Token.h:124
bool AlignOperands
If true, horizontally align operands of binary and ternary expressions.
Definition: Format.h:146
A wrapper around a Token storing information about the whitespace characters preceding it...
Definition: FormatToken.h:123
Defines and computes precedence levels for binary/ternary operators.
SourceLocation getEnd() const
bool isTrailingComment() const
Definition: FormatToken.h:407
llvm::StringMap< FormatStyle > DelimiterStyle
void replaceWhitespace(FormatToken &Tok, unsigned Newlines, unsigned Spaces, unsigned StartOfTokenColumn, bool InPPDirective=false)
Replaces the whitespace in front of Tok.
static bool startsNextParameter(const FormatToken &Current, const FormatStyle &Style)
unsigned LastNewlineOffset
The offset just past the last &#39; &#39; in this token&#39;s leading whitespace (relative to WhiteSpaceStart)...
Definition: FormatToken.h:144
Don&#39;t align, instead use ContinuationIndentWidth, e.g.
Definition: Format.h:65
static bool opensProtoMessageField(const FormatToken &LessTok, const FormatStyle &Style)
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:1326
bool NoLineBreak
Line breaking in this context would break a formatting rule.
unsigned PenaltyExcessCharacter
The penalty for each character outside of the column limit.
Definition: Format.h:1332
Encodes a location in the source.
bool is(tok::TokenKind Kind) const
Definition: FormatToken.h:304
Various functions to configurably format source code.
bool ReflowComments
If true, clang-format will attempt to re-flow comments.
Definition: Format.h:1431
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:177
unsigned LastSpace
The position of the last space on each level.
Break constructor initializers after the colon and commas.
Definition: Format.h:862
bool BreakBeforeTernaryOperators
If true, ternary operators will be placed after line breaks.
Definition: Format.h:837
Encapsulates keywords that are context sensitive or for languages not properly supported by Clang&#39;s l...
Definition: FormatToken.h:649
SourceRange WhitespaceRange
The range of the whitespace immediately preceding the Token.
Definition: FormatToken.h:140
unsigned ContinuationIndentWidth
Indent width for line continuations.
Definition: Format.h:960
tok::TokenKind ParentBracket
If this is a bracket ("<", "(", "[" or "{"), contains the kind of the surrounding bracket...
Definition: FormatToken.h:203
bool IgnoreStackForComparison
Ignore the stack of ParenStates for state comparison.
bool IsUnterminatedLiteral
Set to true if this token is an unterminated literal.
Definition: FormatToken.h:174
bool startsWith(Ts... Tokens) const
true if this line starts with the given tokens in order, ignoring comments.
StringRef TokenText
The raw text of the token.
Definition: FormatToken.h:171
Automatically determine parameter bin-packing behavior.
Definition: Format.h:429
SmallVector< prec::Level, 4 > FakeLParens
Stores the number of required fake parentheses and the corresponding operator precedence.
Definition: FormatToken.h:255
unsigned addTokenToState(LineState &State, bool Newline, bool DryRun, unsigned ExtraSpaces=0)
Appends the next token to State and updates information necessary for indentation.
BreakTemplateDeclarationsStyle AlwaysBreakTemplateDeclarations
The template declaration breaking style to use.
Definition: Format.h:391
unsigned getLength() const
Definition: Replacement.h:123
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:48
RawStringFormatStyleManager(const FormatStyle &CodeStyle)
static unsigned getLengthToMatchingParen(const FormatToken &Tok, const std::vector< ParenState > &Stack)
LanguageKind Language
Language, this format style is targeted at.
Definition: Format.h:1180
unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
Dataflow Directional Tag Classes.
ShortFunctionStyle AllowShortFunctionsOnASingleLine
Dependent on the value, int f() { return 0; } can be put on a single line.
Definition: Format.h:241
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:149
unsigned getOffset() const
Definition: Replacement.h:122
bool Finalized
If true, this token has been fully formatted (indented and potentially re-formatted inside)...
Definition: FormatToken.h:302
llvm::Optional< FormatStyle > getDelimiterStyle(StringRef Delimiter) const
unsigned FirstIndent
The indent of the first token.
tok::PPKeywordKind getPPKeywordID() const
Return the preprocessor keyword ID for this identifier.
static StringRef getCanonicalRawStringDelimiter(const FormatStyle &Style, FormatStyle::LanguageKind Language)
bool BreakStringLiterals
Allow breaking string literals when formatting.
Definition: Format.h:878
Merge all functions fitting on a single line.
Definition: Format.h:236
std::string toString(const til::SExpr *E)
bool canBreak(const LineState &State)
Returns true, if a line break after State is allowed.
bool AllowAllParametersOfDeclarationOnNextLine
If the function declaration doesn&#39;t fit on a line, allow putting all parameters of a function declara...
Definition: Format.h:171
bool AvoidBinPacking
Avoid bin packing, i.e.
bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language, FormatStyle *Style)
Gets a predefined style for the specified language by name.
Definition: Format.cpp:900
unsigned TabWidth
The number of columns used for tab stops.
Definition: Format.h:1625
bool BreakBeforeInheritanceComma
If true, in the class inheritance expression clang-format will break before : and ...
Definition: Format.h:905
bool mustBreak(const LineState &State)
Returns true, if a line break after State is mandatory.
ContinuationIndenter(const FormatStyle &Style, const AdditionalKeywords &Keywords, const SourceManager &SourceMgr, WhitespaceManager &Whitespaces, encoding::Encoding Encoding, bool BinPackInconclusiveFunctions)
Constructs a ContinuationIndenter to format Line starting in column FirstIndent.
Should be used for Objective-C, Objective-C++.
Definition: Format.h:1167
BinPackStyle ObjCBinPackProtocolList
Controls bin-packing Objective-C protocol conformance list items into as few lines as possible when t...
Definition: Format.h:1293
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:271
BreakConstructorInitializersStyle BreakConstructorInitializers
The constructor initializers style to use.
Definition: Format.h:866
bool ClosesTemplateDeclaration
true if this is the ">" of "template<..>".
Definition: FormatToken.h:188
FormatToken * MatchingParen
If this is a bracket, this points to the matching one.
Definition: FormatToken.h:284
std::vector< RawStringFormat > RawStringFormats
Defines hints for detecting supported languages code blocks in raw strings.
Definition: Format.h:1417
SmallVector< AnnotatedLine *, 1 > Children
If this token starts a block, this contains all the unwrapped lines in it.
Definition: FormatToken.h:294
bool startsSequence(A K1, Ts... Tokens) const
true if this token starts a sequence with the given tokens in order, following the Next pointers...
Definition: FormatToken.h:325
unsigned ConstructorInitializerIndentWidth
The number of characters to use for indentation of constructor initializer lists. ...
Definition: Format.h:950
BinaryOperatorStyle BreakBeforeBinaryOperators
The way to wrap binary operators.
Definition: Format.h:477
bool opensBlockOrBlockTypeList(const FormatStyle &Style) const
Returns true if this tokens starts a block-type list, i.e.
Definition: FormatToken.h:479
Indents directives after the hash.
Definition: Format.h:1070
llvm::Optional< FormatStyle > GetLanguageStyle(LanguageKind Language) const
Definition: Format.cpp:1017
bool opensScope() const
Returns whether Tok is ([{ or an opening < of a template or in protos.
Definition: FormatToken.h:360
bool MustBreakBefore
Whether there must be a line break before this token.
Definition: FormatToken.h:165
unsigned StartOfLineLevel
The NestingLevel at the start of this line.
__DEVICE__ int max(int __a, int __b)
Should be used for Protocol Buffer messages in text format (https://developers.google.com/protocol-buffers/).
Definition: Format.h:1175
__DEVICE__ int min(int __a, int __b)
unsigned StartOfStringLiteral
The start column of the string literal, if we&#39;re in a string literal sequence, 0 otherwise.
StringRef Text
Definition: Format.cpp:1573
llvm::StringMap< FormatStyle > EnclosingFunctionStyle
FormatToken * NextToken
The token that needs to be next formatted.
unsigned StartOfFunctionCall
The start of the most recent function in a builder-type call.
This file implements an indenter that manages the indentation of continuations.
StringRef getReplacementText() const
Definition: Replacement.h:124
bool isStringLiteral() const
Definition: FormatToken.h:336
SourceLocation getBegin() const
unsigned MaxEmptyLinesToKeep
The maximum number of consecutive empty lines to keep.
Definition: Format.h:1224
BraceBlockKind BlockKind
Contains the kind of block if this token is a brace.
Definition: FormatToken.h:177
bool PartOfMultiVariableDeclStmt
Is this token part of a DeclStmt defining multiple variables?
Definition: FormatToken.h:276
This class handles loading and caching of source files into memory.
static llvm::Optional< StringRef > getRawStringDelimiter(StringRef TokenText)
Do not force break before declaration.
Definition: Format.h:365
unsigned LastLineColumnWidth
Contains the width in columns of the last line of a multi-line token.
Definition: FormatToken.h:153
const encoding::Encoding Encoding
const FormatStyle & Style
bool BinPackParameters
If false, a function declaration&#39;s or function definition&#39;s parameters will either all be on the same...
Definition: Format.h:423