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