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