1 //===--- ContinuationIndenter.cpp - Format C++ code -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements the continuation indenter.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "ContinuationIndenter.h"
15 #include "BreakableToken.h"
16 #include "FormatInternal.h"
17 #include "FormatToken.h"
18 #include "WhitespaceManager.h"
19 #include "clang/Basic/OperatorPrecedence.h"
20 #include "clang/Basic/SourceManager.h"
21 #include "clang/Basic/TokenKinds.h"
22 #include "clang/Format/Format.h"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/Support/Debug.h"
25 #include <optional>
26 
27 #define DEBUG_TYPE "format-indenter"
28 
29 namespace clang {
30 namespace format {
31 
32 // Returns true if a TT_SelectorName should be indented when wrapped,
33 // false otherwise.
34 static bool shouldIndentWrappedSelectorName(const FormatStyle &Style,
35                                             LineType LineType) {
36   return Style.IndentWrappedFunctionNames || LineType == LT_ObjCMethodDecl;
37 }
38 
39 // Returns true if a binary operator following \p Tok should be unindented when
40 // the style permits it.
41 static bool shouldUnindentNextOperator(const FormatToken &Tok) {
42   const FormatToken *Previous = Tok.getPreviousNonComment();
43   return Previous && (Previous->getPrecedence() == prec::Assignment ||
44                       Previous->isOneOf(tok::kw_return, TT_RequiresClause));
45 }
46 
47 // Returns the length of everything up to the first possible line break after
48 // the ), ], } or > matching \c Tok.
49 static unsigned getLengthToMatchingParen(const FormatToken &Tok,
50                                          ArrayRef<ParenState> Stack) {
51   // Normally whether or not a break before T is possible is calculated and
52   // stored in T.CanBreakBefore. Braces, array initializers and text proto
53   // messages like `key: < ... >` are an exception: a break is possible
54   // before a closing brace R if a break was inserted after the corresponding
55   // opening brace. The information about whether or not a break is needed
56   // before a closing brace R is stored in the ParenState field
57   // S.BreakBeforeClosingBrace where S is the state that R closes.
58   //
59   // In order to decide whether there can be a break before encountered right
60   // braces, this implementation iterates over the sequence of tokens and over
61   // the paren stack in lockstep, keeping track of the stack level which visited
62   // right braces correspond to in MatchingStackIndex.
63   //
64   // For example, consider:
65   // L. <- line number
66   // 1. {
67   // 2. {1},
68   // 3. {2},
69   // 4. {{3}}}
70   //     ^ where we call this method with this token.
71   // The paren stack at this point contains 3 brace levels:
72   //  0. { at line 1, BreakBeforeClosingBrace: true
73   //  1. first { at line 4, BreakBeforeClosingBrace: false
74   //  2. second { at line 4, BreakBeforeClosingBrace: false,
75   //  where there might be fake parens levels in-between these levels.
76   // The algorithm will start at the first } on line 4, which is the matching
77   // brace of the initial left brace and at level 2 of the stack. Then,
78   // examining BreakBeforeClosingBrace: false at level 2, it will continue to
79   // the second } on line 4, and will traverse the stack downwards until it
80   // finds the matching { on level 1. Then, examining BreakBeforeClosingBrace:
81   // false at level 1, it will continue to the third } on line 4 and will
82   // traverse the stack downwards until it finds the matching { on level 0.
83   // Then, examining BreakBeforeClosingBrace: true at level 0, the algorithm
84   // will stop and will use the second } on line 4 to determine the length to
85   // return, as in this example the range will include the tokens: {3}}
86   //
87   // The algorithm will only traverse the stack if it encounters braces, array
88   // initializer squares or text proto angle brackets.
89   if (!Tok.MatchingParen)
90     return 0;
91   FormatToken *End = Tok.MatchingParen;
92   // Maintains a stack level corresponding to the current End token.
93   int MatchingStackIndex = Stack.size() - 1;
94   // Traverses the stack downwards, looking for the level to which LBrace
95   // corresponds. Returns either a pointer to the matching level or nullptr if
96   // LParen is not found in the initial portion of the stack up to
97   // MatchingStackIndex.
98   auto FindParenState = [&](const FormatToken *LBrace) -> const ParenState * {
99     while (MatchingStackIndex >= 0 && Stack[MatchingStackIndex].Tok != LBrace)
100       --MatchingStackIndex;
101     return MatchingStackIndex >= 0 ? &Stack[MatchingStackIndex] : nullptr;
102   };
103   for (; End->Next; End = End->Next) {
104     if (End->Next->CanBreakBefore)
105       break;
106     if (!End->Next->closesScope())
107       continue;
108     if (End->Next->MatchingParen &&
109         End->Next->MatchingParen->isOneOf(
110             tok::l_brace, TT_ArrayInitializerLSquare, tok::less)) {
111       const ParenState *State = FindParenState(End->Next->MatchingParen);
112       if (State && State->BreakBeforeClosingBrace)
113         break;
114     }
115   }
116   return End->TotalLength - Tok.TotalLength + 1;
117 }
118 
119 static unsigned getLengthToNextOperator(const FormatToken &Tok) {
120   if (!Tok.NextOperator)
121     return 0;
122   return Tok.NextOperator->TotalLength - Tok.TotalLength;
123 }
124 
125 // Returns \c true if \c Tok is the "." or "->" of a call and starts the next
126 // segment of a builder type call.
127 static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
128   return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
129 }
130 
131 // Returns \c true if \c Current starts a new parameter.
132 static bool startsNextParameter(const FormatToken &Current,
133                                 const FormatStyle &Style) {
134   const FormatToken &Previous = *Current.Previous;
135   if (Current.is(TT_CtorInitializerComma) &&
136       Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
137     return true;
138   }
139   if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName))
140     return true;
141   return Previous.is(tok::comma) && !Current.isTrailingComment() &&
142          ((Previous.isNot(TT_CtorInitializerComma) ||
143            Style.BreakConstructorInitializers !=
144                FormatStyle::BCIS_BeforeComma) &&
145           (Previous.isNot(TT_InheritanceComma) ||
146            Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma));
147 }
148 
149 static bool opensProtoMessageField(const FormatToken &LessTok,
150                                    const FormatStyle &Style) {
151   if (LessTok.isNot(tok::less))
152     return false;
153   return Style.Language == FormatStyle::LK_TextProto ||
154          (Style.Language == FormatStyle::LK_Proto &&
155           (LessTok.NestingLevel > 0 ||
156            (LessTok.Previous && LessTok.Previous->is(tok::equal))));
157 }
158 
159 // Returns the delimiter of a raw string literal, or std::nullopt if TokenText
160 // is not the text of a raw string literal. The delimiter could be the empty
161 // string.  For example, the delimiter of R"deli(cont)deli" is deli.
162 static std::optional<StringRef> getRawStringDelimiter(StringRef TokenText) {
163   if (TokenText.size() < 5 // The smallest raw string possible is 'R"()"'.
164       || !TokenText.starts_with("R\"") || !TokenText.ends_with("\"")) {
165     return std::nullopt;
166   }
167 
168   // A raw string starts with 'R"<delimiter>(' and delimiter is ascii and has
169   // size at most 16 by the standard, so the first '(' must be among the first
170   // 19 bytes.
171   size_t LParenPos = TokenText.substr(0, 19).find_first_of('(');
172   if (LParenPos == StringRef::npos)
173     return std::nullopt;
174   StringRef Delimiter = TokenText.substr(2, LParenPos - 2);
175 
176   // Check that the string ends in ')Delimiter"'.
177   size_t RParenPos = TokenText.size() - Delimiter.size() - 2;
178   if (TokenText[RParenPos] != ')')
179     return std::nullopt;
180   if (!TokenText.substr(RParenPos + 1).starts_with(Delimiter))
181     return std::nullopt;
182   return Delimiter;
183 }
184 
185 // Returns the canonical delimiter for \p Language, or the empty string if no
186 // canonical delimiter is specified.
187 static StringRef
188 getCanonicalRawStringDelimiter(const FormatStyle &Style,
189                                FormatStyle::LanguageKind Language) {
190   for (const auto &Format : Style.RawStringFormats)
191     if (Format.Language == Language)
192       return StringRef(Format.CanonicalDelimiter);
193   return "";
194 }
195 
196 RawStringFormatStyleManager::RawStringFormatStyleManager(
197     const FormatStyle &CodeStyle) {
198   for (const auto &RawStringFormat : CodeStyle.RawStringFormats) {
199     std::optional<FormatStyle> LanguageStyle =
200         CodeStyle.GetLanguageStyle(RawStringFormat.Language);
201     if (!LanguageStyle) {
202       FormatStyle PredefinedStyle;
203       if (!getPredefinedStyle(RawStringFormat.BasedOnStyle,
204                               RawStringFormat.Language, &PredefinedStyle)) {
205         PredefinedStyle = getLLVMStyle();
206         PredefinedStyle.Language = RawStringFormat.Language;
207       }
208       LanguageStyle = PredefinedStyle;
209     }
210     LanguageStyle->ColumnLimit = CodeStyle.ColumnLimit;
211     for (StringRef Delimiter : RawStringFormat.Delimiters)
212       DelimiterStyle.insert({Delimiter, *LanguageStyle});
213     for (StringRef EnclosingFunction : RawStringFormat.EnclosingFunctions)
214       EnclosingFunctionStyle.insert({EnclosingFunction, *LanguageStyle});
215   }
216 }
217 
218 std::optional<FormatStyle>
219 RawStringFormatStyleManager::getDelimiterStyle(StringRef Delimiter) const {
220   auto It = DelimiterStyle.find(Delimiter);
221   if (It == DelimiterStyle.end())
222     return std::nullopt;
223   return It->second;
224 }
225 
226 std::optional<FormatStyle>
227 RawStringFormatStyleManager::getEnclosingFunctionStyle(
228     StringRef EnclosingFunction) const {
229   auto It = EnclosingFunctionStyle.find(EnclosingFunction);
230   if (It == EnclosingFunctionStyle.end())
231     return std::nullopt;
232   return It->second;
233 }
234 
235 ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
236                                            const AdditionalKeywords &Keywords,
237                                            const SourceManager &SourceMgr,
238                                            WhitespaceManager &Whitespaces,
239                                            encoding::Encoding Encoding,
240                                            bool BinPackInconclusiveFunctions)
241     : Style(Style), Keywords(Keywords), SourceMgr(SourceMgr),
242       Whitespaces(Whitespaces), Encoding(Encoding),
243       BinPackInconclusiveFunctions(BinPackInconclusiveFunctions),
244       CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {}
245 
246 LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
247                                                 unsigned FirstStartColumn,
248                                                 const AnnotatedLine *Line,
249                                                 bool DryRun) {
250   LineState State;
251   State.FirstIndent = FirstIndent;
252   if (FirstStartColumn && Line->First->NewlinesBefore == 0)
253     State.Column = FirstStartColumn;
254   else
255     State.Column = FirstIndent;
256   // With preprocessor directive indentation, the line starts on column 0
257   // since it's indented after the hash, but FirstIndent is set to the
258   // preprocessor indent.
259   if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash &&
260       (Line->Type == LT_PreprocessorDirective ||
261        Line->Type == LT_ImportStatement)) {
262     State.Column = 0;
263   }
264   State.Line = Line;
265   State.NextToken = Line->First;
266   State.Stack.push_back(ParenState(/*Tok=*/nullptr, FirstIndent, FirstIndent,
267                                    /*AvoidBinPacking=*/false,
268                                    /*NoLineBreak=*/false));
269   State.NoContinuation = false;
270   State.StartOfStringLiteral = 0;
271   State.NoLineBreak = false;
272   State.StartOfLineLevel = 0;
273   State.LowestLevelOnLine = 0;
274   State.IgnoreStackForComparison = false;
275 
276   if (Style.Language == FormatStyle::LK_TextProto) {
277     // We need this in order to deal with the bin packing of text fields at
278     // global scope.
279     auto &CurrentState = State.Stack.back();
280     CurrentState.AvoidBinPacking = true;
281     CurrentState.BreakBeforeParameter = true;
282     CurrentState.AlignColons = false;
283   }
284 
285   // The first token has already been indented and thus consumed.
286   moveStateToNextToken(State, DryRun, /*Newline=*/false);
287   return State;
288 }
289 
290 bool ContinuationIndenter::canBreak(const LineState &State) {
291   const FormatToken &Current = *State.NextToken;
292   const FormatToken &Previous = *Current.Previous;
293   const auto &CurrentState = State.Stack.back();
294   assert(&Previous == Current.Previous);
295   if (!Current.CanBreakBefore && !(CurrentState.BreakBeforeClosingBrace &&
296                                    Current.closesBlockOrBlockTypeList(Style))) {
297     return false;
298   }
299   // The opening "{" of a braced list has to be on the same line as the first
300   // element if it is nested in another braced init list or function call.
301   if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
302       Previous.isNot(TT_DictLiteral) && Previous.is(BK_BracedInit) &&
303       Previous.Previous &&
304       Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma)) {
305     return false;
306   }
307   // This prevents breaks like:
308   //   ...
309   //   SomeParameter, OtherParameter).DoSomething(
310   //   ...
311   // As they hide "DoSomething" and are generally bad for readability.
312   if (Previous.opensScope() && Previous.isNot(tok::l_brace) &&
313       State.LowestLevelOnLine < State.StartOfLineLevel &&
314       State.LowestLevelOnLine < Current.NestingLevel) {
315     return false;
316   }
317   if (Current.isMemberAccess() && CurrentState.ContainsUnwrappedBuilder)
318     return false;
319 
320   // Don't create a 'hanging' indent if there are multiple blocks in a single
321   // statement and we are aligning lambda blocks to their signatures.
322   if (Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
323       State.Stack[State.Stack.size() - 2].NestedBlockInlined &&
324       State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks &&
325       Style.LambdaBodyIndentation == FormatStyle::LBI_Signature) {
326     return false;
327   }
328 
329   // Don't break after very short return types (e.g. "void") as that is often
330   // unexpected.
331   if (Current.is(TT_FunctionDeclarationName) && State.Column < 6) {
332     if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None)
333       return false;
334   }
335 
336   // If binary operators are moved to the next line (including commas for some
337   // styles of constructor initializers), that's always ok.
338   if (!Current.isOneOf(TT_BinaryOperator, tok::comma) &&
339       // Allow breaking opening brace of lambdas (when passed as function
340       // arguments) to a new line when BeforeLambdaBody brace wrapping is
341       // enabled.
342       (!Style.BraceWrapping.BeforeLambdaBody ||
343        Current.isNot(TT_LambdaLBrace)) &&
344       CurrentState.NoLineBreakInOperand) {
345     return false;
346   }
347 
348   if (Previous.is(tok::l_square) && Previous.is(TT_ObjCMethodExpr))
349     return false;
350 
351   if (Current.is(TT_ConditionalExpr) && Previous.is(tok::r_paren) &&
352       Previous.MatchingParen && Previous.MatchingParen->Previous &&
353       Previous.MatchingParen->Previous->MatchingParen &&
354       Previous.MatchingParen->Previous->MatchingParen->is(TT_LambdaLBrace)) {
355     // We have a lambda within a conditional expression, allow breaking here.
356     assert(Previous.MatchingParen->Previous->is(tok::r_brace));
357     return true;
358   }
359 
360   return !State.NoLineBreak && !CurrentState.NoLineBreak;
361 }
362 
363 bool ContinuationIndenter::mustBreak(const LineState &State) {
364   const FormatToken &Current = *State.NextToken;
365   const FormatToken &Previous = *Current.Previous;
366   const auto &CurrentState = State.Stack.back();
367   if (Style.BraceWrapping.BeforeLambdaBody && Current.CanBreakBefore &&
368       Current.is(TT_LambdaLBrace) && Previous.isNot(TT_LineComment)) {
369     auto LambdaBodyLength = getLengthToMatchingParen(Current, State.Stack);
370     return LambdaBodyLength > getColumnLimit(State);
371   }
372   if (Current.MustBreakBefore ||
373       (Current.is(TT_InlineASMColon) &&
374        (Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always ||
375         (Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_OnlyMultiline &&
376          Style.ColumnLimit > 0)))) {
377     return true;
378   }
379   if (CurrentState.BreakBeforeClosingBrace &&
380       (Current.closesBlockOrBlockTypeList(Style) ||
381        (Current.is(tok::r_brace) &&
382         Current.isBlockIndentedInitRBrace(Style)))) {
383     return true;
384   }
385   if (CurrentState.BreakBeforeClosingParen && Current.is(tok::r_paren))
386     return true;
387   if (Style.Language == FormatStyle::LK_ObjC &&
388       Style.ObjCBreakBeforeNestedBlockParam &&
389       Current.ObjCSelectorNameParts > 1 &&
390       Current.startsSequence(TT_SelectorName, tok::colon, tok::caret)) {
391     return true;
392   }
393   // Avoid producing inconsistent states by requiring breaks where they are not
394   // permitted for C# generic type constraints.
395   if (CurrentState.IsCSharpGenericTypeConstraint &&
396       Previous.isNot(TT_CSharpGenericTypeConstraintComma)) {
397     return false;
398   }
399   if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
400        (Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) &&
401         Style.isCpp() &&
402         // FIXME: This is a temporary workaround for the case where clang-format
403         // sets BreakBeforeParameter to avoid bin packing and this creates a
404         // completely unnecessary line break after a template type that isn't
405         // line-wrapped.
406         (Previous.NestingLevel == 1 || Style.BinPackParameters)) ||
407        (Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) &&
408         Previous.isNot(tok::question)) ||
409        (!Style.BreakBeforeTernaryOperators &&
410         Previous.is(TT_ConditionalExpr))) &&
411       CurrentState.BreakBeforeParameter && !Current.isTrailingComment() &&
412       !Current.isOneOf(tok::r_paren, tok::r_brace)) {
413     return true;
414   }
415   if (CurrentState.IsChainedConditional &&
416       ((Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) &&
417         Current.is(tok::colon)) ||
418        (!Style.BreakBeforeTernaryOperators && Previous.is(TT_ConditionalExpr) &&
419         Previous.is(tok::colon)))) {
420     return true;
421   }
422   if (((Previous.is(TT_DictLiteral) && Previous.is(tok::l_brace)) ||
423        (Previous.is(TT_ArrayInitializerLSquare) &&
424         Previous.ParameterCount > 1) ||
425        opensProtoMessageField(Previous, Style)) &&
426       Style.ColumnLimit > 0 &&
427       getLengthToMatchingParen(Previous, State.Stack) + State.Column - 1 >
428           getColumnLimit(State)) {
429     return true;
430   }
431 
432   const FormatToken &BreakConstructorInitializersToken =
433       Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon
434           ? Previous
435           : Current;
436   if (BreakConstructorInitializersToken.is(TT_CtorInitializerColon) &&
437       (State.Column + State.Line->Last->TotalLength - Previous.TotalLength >
438            getColumnLimit(State) ||
439        CurrentState.BreakBeforeParameter) &&
440       (!Current.isTrailingComment() || Current.NewlinesBefore > 0) &&
441       (Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All ||
442        Style.BreakConstructorInitializers != FormatStyle::BCIS_BeforeColon ||
443        Style.ColumnLimit != 0)) {
444     return true;
445   }
446 
447   if (Current.is(TT_ObjCMethodExpr) && Previous.isNot(TT_SelectorName) &&
448       State.Line->startsWith(TT_ObjCMethodSpecifier)) {
449     return true;
450   }
451   if (Current.is(TT_SelectorName) && Previous.isNot(tok::at) &&
452       CurrentState.ObjCSelectorNameFound && CurrentState.BreakBeforeParameter &&
453       (Style.ObjCBreakBeforeNestedBlockParam ||
454        !Current.startsSequence(TT_SelectorName, tok::colon, tok::caret))) {
455     return true;
456   }
457 
458   unsigned NewLineColumn = getNewLineColumn(State);
459   if (Current.isMemberAccess() && Style.ColumnLimit != 0 &&
460       State.Column + getLengthToNextOperator(Current) > Style.ColumnLimit &&
461       (State.Column > NewLineColumn ||
462        Current.NestingLevel < State.StartOfLineLevel)) {
463     return true;
464   }
465 
466   if (startsSegmentOfBuilderTypeCall(Current) &&
467       (CurrentState.CallContinuation != 0 ||
468        CurrentState.BreakBeforeParameter) &&
469       // JavaScript is treated different here as there is a frequent pattern:
470       //   SomeFunction(function() {
471       //     ...
472       //   }.bind(...));
473       // FIXME: We should find a more generic solution to this problem.
474       !(State.Column <= NewLineColumn && Style.isJavaScript()) &&
475       !(Previous.closesScopeAfterBlock() && State.Column <= NewLineColumn)) {
476     return true;
477   }
478 
479   // If the template declaration spans multiple lines, force wrap before the
480   // function/class declaration.
481   if (Previous.ClosesTemplateDeclaration && CurrentState.BreakBeforeParameter &&
482       Current.CanBreakBefore) {
483     return true;
484   }
485 
486   if (State.Line->First->isNot(tok::kw_enum) && State.Column <= NewLineColumn)
487     return false;
488 
489   if (Style.AlwaysBreakBeforeMultilineStrings &&
490       (NewLineColumn == State.FirstIndent + Style.ContinuationIndentWidth ||
491        Previous.is(tok::comma) || Current.NestingLevel < 2) &&
492       !Previous.isOneOf(tok::kw_return, tok::lessless, tok::at,
493                         Keywords.kw_dollar) &&
494       !Previous.isOneOf(TT_InlineASMColon, TT_ConditionalExpr) &&
495       nextIsMultilineString(State)) {
496     return true;
497   }
498 
499   // Using CanBreakBefore here and below takes care of the decision whether the
500   // current style uses wrapping before or after operators for the given
501   // operator.
502   if (Previous.is(TT_BinaryOperator) && Current.CanBreakBefore) {
503     const auto PreviousPrecedence = Previous.getPrecedence();
504     if (PreviousPrecedence != prec::Assignment &&
505         CurrentState.BreakBeforeParameter && !Current.isTrailingComment()) {
506       const bool LHSIsBinaryExpr =
507           Previous.Previous && Previous.Previous->EndsBinaryExpression;
508       if (LHSIsBinaryExpr)
509         return true;
510       // If we need to break somewhere inside the LHS of a binary expression, we
511       // should also break after the operator. Otherwise, the formatting would
512       // hide the operator precedence, e.g. in:
513       //   if (aaaaaaaaaaaaaa ==
514       //           bbbbbbbbbbbbbb && c) {..
515       // For comparisons, we only apply this rule, if the LHS is a binary
516       // expression itself as otherwise, the line breaks seem superfluous.
517       // We need special cases for ">>" which we have split into two ">" while
518       // lexing in order to make template parsing easier.
519       const bool IsComparison =
520           (PreviousPrecedence == prec::Relational ||
521            PreviousPrecedence == prec::Equality ||
522            PreviousPrecedence == prec::Spaceship) &&
523           Previous.Previous &&
524           Previous.Previous->isNot(TT_BinaryOperator); // For >>.
525       if (!IsComparison)
526         return true;
527     }
528   } else if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore &&
529              CurrentState.BreakBeforeParameter) {
530     return true;
531   }
532 
533   // Same as above, but for the first "<<" operator.
534   if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator) &&
535       CurrentState.BreakBeforeParameter && CurrentState.FirstLessLess == 0) {
536     return true;
537   }
538 
539   if (Current.NestingLevel == 0 && !Current.isTrailingComment()) {
540     // Always break after "template <...>"(*) and leading annotations. This is
541     // only for cases where the entire line does not fit on a single line as a
542     // different LineFormatter would be used otherwise.
543     // *: Except when another option interferes with that, like concepts.
544     if (Previous.ClosesTemplateDeclaration) {
545       if (Current.is(tok::kw_concept)) {
546         switch (Style.BreakBeforeConceptDeclarations) {
547         case FormatStyle::BBCDS_Allowed:
548           break;
549         case FormatStyle::BBCDS_Always:
550           return true;
551         case FormatStyle::BBCDS_Never:
552           return false;
553         }
554       }
555       if (Current.is(TT_RequiresClause)) {
556         switch (Style.RequiresClausePosition) {
557         case FormatStyle::RCPS_SingleLine:
558         case FormatStyle::RCPS_WithPreceding:
559           return false;
560         default:
561           return true;
562         }
563       }
564       return Style.AlwaysBreakTemplateDeclarations != FormatStyle::BTDS_No;
565     }
566     if (Previous.is(TT_FunctionAnnotationRParen) &&
567         State.Line->Type != LT_PreprocessorDirective) {
568       return true;
569     }
570     if (Previous.is(TT_LeadingJavaAnnotation) && Current.isNot(tok::l_paren) &&
571         Current.isNot(TT_LeadingJavaAnnotation)) {
572       return true;
573     }
574   }
575 
576   if (Style.isJavaScript() && Previous.is(tok::r_paren) &&
577       Previous.is(TT_JavaAnnotation)) {
578     // Break after the closing parenthesis of TypeScript decorators before
579     // functions, getters and setters.
580     static const llvm::StringSet<> BreakBeforeDecoratedTokens = {"get", "set",
581                                                                  "function"};
582     if (BreakBeforeDecoratedTokens.contains(Current.TokenText))
583       return true;
584   }
585 
586   // If the return type spans multiple lines, wrap before the function name.
587   if (((Current.is(TT_FunctionDeclarationName) &&
588         !State.Line->ReturnTypeWrapped &&
589         // Don't break before a C# function when no break after return type.
590         (!Style.isCSharp() ||
591          Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None) &&
592         // Don't always break between a JavaScript `function` and the function
593         // name.
594         !Style.isJavaScript()) ||
595        (Current.is(tok::kw_operator) && Previous.isNot(tok::coloncolon))) &&
596       Previous.isNot(tok::kw_template) && CurrentState.BreakBeforeParameter) {
597     return true;
598   }
599 
600   // The following could be precomputed as they do not depend on the state.
601   // However, as they should take effect only if the UnwrappedLine does not fit
602   // into the ColumnLimit, they are checked here in the ContinuationIndenter.
603   if (Style.ColumnLimit != 0 && Previous.is(BK_Block) &&
604       Previous.is(tok::l_brace) &&
605       !Current.isOneOf(tok::r_brace, tok::comment)) {
606     return true;
607   }
608 
609   if (Current.is(tok::lessless) &&
610       ((Previous.is(tok::identifier) && Previous.TokenText == "endl") ||
611        (Previous.Tok.isLiteral() && (Previous.TokenText.ends_with("\\n\"") ||
612                                      Previous.TokenText == "\'\\n\'")))) {
613     return true;
614   }
615 
616   if (Previous.is(TT_BlockComment) && Previous.IsMultiline)
617     return true;
618 
619   if (State.NoContinuation)
620     return true;
621 
622   return false;
623 }
624 
625 unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
626                                                bool DryRun,
627                                                unsigned ExtraSpaces) {
628   const FormatToken &Current = *State.NextToken;
629   assert(State.NextToken->Previous);
630   const FormatToken &Previous = *State.NextToken->Previous;
631 
632   assert(!State.Stack.empty());
633   State.NoContinuation = false;
634 
635   if (Current.is(TT_ImplicitStringLiteral) &&
636       (!Previous.Tok.getIdentifierInfo() ||
637        Previous.Tok.getIdentifierInfo()->getPPKeywordID() ==
638            tok::pp_not_keyword)) {
639     unsigned EndColumn =
640         SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getEnd());
641     if (Current.LastNewlineOffset != 0) {
642       // If there is a newline within this token, the final column will solely
643       // determined by the current end column.
644       State.Column = EndColumn;
645     } else {
646       unsigned StartColumn =
647           SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getBegin());
648       assert(EndColumn >= StartColumn);
649       State.Column += EndColumn - StartColumn;
650     }
651     moveStateToNextToken(State, DryRun, /*Newline=*/false);
652     return 0;
653   }
654 
655   unsigned Penalty = 0;
656   if (Newline)
657     Penalty = addTokenOnNewLine(State, DryRun);
658   else
659     addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
660 
661   return moveStateToNextToken(State, DryRun, Newline) + Penalty;
662 }
663 
664 void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
665                                                  unsigned ExtraSpaces) {
666   FormatToken &Current = *State.NextToken;
667   assert(State.NextToken->Previous);
668   const FormatToken &Previous = *State.NextToken->Previous;
669   auto &CurrentState = State.Stack.back();
670 
671   bool DisallowLineBreaksOnThisLine =
672       Style.LambdaBodyIndentation == FormatStyle::LBI_Signature &&
673       Style.isCpp() && [&Current] {
674         // Deal with lambda arguments in C++. The aim here is to ensure that we
675         // don't over-indent lambda function bodies when lambdas are passed as
676         // arguments to function calls. We do this by ensuring that either all
677         // arguments (including any lambdas) go on the same line as the function
678         // call, or we break before the first argument.
679         auto PrevNonComment = Current.getPreviousNonComment();
680         if (!PrevNonComment || PrevNonComment->isNot(tok::l_paren))
681           return false;
682         if (Current.isOneOf(tok::comment, tok::l_paren, TT_LambdaLSquare))
683           return false;
684         auto BlockParameterCount = PrevNonComment->BlockParameterCount;
685         if (BlockParameterCount == 0)
686           return false;
687 
688         // Multiple lambdas in the same function call.
689         if (BlockParameterCount > 1)
690           return true;
691 
692         // A lambda followed by another arg.
693         if (!PrevNonComment->Role)
694           return false;
695         auto Comma = PrevNonComment->Role->lastComma();
696         if (!Comma)
697           return false;
698         auto Next = Comma->getNextNonComment();
699         return Next &&
700                !Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret);
701       }();
702 
703   if (DisallowLineBreaksOnThisLine)
704     State.NoLineBreak = true;
705 
706   if (Current.is(tok::equal) &&
707       (State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) &&
708       CurrentState.VariablePos == 0) {
709     CurrentState.VariablePos = State.Column;
710     // Move over * and & if they are bound to the variable name.
711     const FormatToken *Tok = &Previous;
712     while (Tok && CurrentState.VariablePos >= Tok->ColumnWidth) {
713       CurrentState.VariablePos -= Tok->ColumnWidth;
714       if (Tok->SpacesRequiredBefore != 0)
715         break;
716       Tok = Tok->Previous;
717     }
718     if (Previous.PartOfMultiVariableDeclStmt)
719       CurrentState.LastSpace = CurrentState.VariablePos;
720   }
721 
722   unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
723 
724   // Indent preprocessor directives after the hash if required.
725   int PPColumnCorrection = 0;
726   if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash &&
727       Previous.is(tok::hash) && State.FirstIndent > 0 &&
728       &Previous == State.Line->First &&
729       (State.Line->Type == LT_PreprocessorDirective ||
730        State.Line->Type == LT_ImportStatement)) {
731     Spaces += State.FirstIndent;
732 
733     // For preprocessor indent with tabs, State.Column will be 1 because of the
734     // hash. This causes second-level indents onward to have an extra space
735     // after the tabs. We avoid this misalignment by subtracting 1 from the
736     // column value passed to replaceWhitespace().
737     if (Style.UseTab != FormatStyle::UT_Never)
738       PPColumnCorrection = -1;
739   }
740 
741   if (!DryRun) {
742     Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces,
743                                   State.Column + Spaces + PPColumnCorrection);
744   }
745 
746   // If "BreakBeforeInheritanceComma" mode, don't break within the inheritance
747   // declaration unless there is multiple inheritance.
748   if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
749       Current.is(TT_InheritanceColon)) {
750     CurrentState.NoLineBreak = true;
751   }
752   if (Style.BreakInheritanceList == FormatStyle::BILS_AfterColon &&
753       Previous.is(TT_InheritanceColon)) {
754     CurrentState.NoLineBreak = true;
755   }
756 
757   if (Current.is(TT_SelectorName) && !CurrentState.ObjCSelectorNameFound) {
758     unsigned MinIndent = std::max(
759         State.FirstIndent + Style.ContinuationIndentWidth, CurrentState.Indent);
760     unsigned FirstColonPos = State.Column + Spaces + Current.ColumnWidth;
761     if (Current.LongestObjCSelectorName == 0)
762       CurrentState.AlignColons = false;
763     else if (MinIndent + Current.LongestObjCSelectorName > FirstColonPos)
764       CurrentState.ColonPos = MinIndent + Current.LongestObjCSelectorName;
765     else
766       CurrentState.ColonPos = FirstColonPos;
767   }
768 
769   // In "AlwaysBreak" or "BlockIndent" mode, enforce wrapping directly after the
770   // parenthesis by disallowing any further line breaks if there is no line
771   // break after the opening parenthesis. Don't break if it doesn't conserve
772   // columns.
773   if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
774        Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) &&
775       (Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) ||
776        (Previous.is(tok::l_brace) && Previous.isNot(BK_Block) &&
777         Style.Cpp11BracedListStyle)) &&
778       State.Column > getNewLineColumn(State) &&
779       (!Previous.Previous ||
780        !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
781                                    tok::kw_switch)) &&
782       // Don't do this for simple (no expressions) one-argument function calls
783       // as that feels like needlessly wasting whitespace, e.g.:
784       //
785       //   caaaaaaaaaaaall(
786       //       caaaaaaaaaaaall(
787       //           caaaaaaaaaaaall(
788       //               caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa))));
789       Current.FakeLParens.size() > 0 &&
790       Current.FakeLParens.back() > prec::Unknown) {
791     CurrentState.NoLineBreak = true;
792   }
793   if (Previous.is(TT_TemplateString) && Previous.opensScope())
794     CurrentState.NoLineBreak = true;
795 
796   // Align following lines within parentheses / brackets if configured.
797   // Note: This doesn't apply to macro expansion lines, which are MACRO( , , )
798   // with args as children of the '(' and ',' tokens. It does not make sense to
799   // align the commas with the opening paren.
800   if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
801       !CurrentState.IsCSharpGenericTypeConstraint && Previous.opensScope() &&
802       Previous.isNot(TT_ObjCMethodExpr) && Previous.isNot(TT_RequiresClause) &&
803       !(Current.MacroParent && Previous.MacroParent) &&
804       (Current.isNot(TT_LineComment) ||
805        Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen))) {
806     CurrentState.Indent = State.Column + Spaces;
807     CurrentState.IsAligned = true;
808   }
809   if (CurrentState.AvoidBinPacking && startsNextParameter(Current, Style))
810     CurrentState.NoLineBreak = true;
811   if (startsSegmentOfBuilderTypeCall(Current) &&
812       State.Column > getNewLineColumn(State)) {
813     CurrentState.ContainsUnwrappedBuilder = true;
814   }
815 
816   if (Current.is(TT_TrailingReturnArrow) &&
817       Style.Language == FormatStyle::LK_Java) {
818     CurrentState.NoLineBreak = true;
819   }
820   if (Current.isMemberAccess() && Previous.is(tok::r_paren) &&
821       (Previous.MatchingParen &&
822        (Previous.TotalLength - Previous.MatchingParen->TotalLength > 10))) {
823     // If there is a function call with long parameters, break before trailing
824     // calls. This prevents things like:
825     //   EXPECT_CALL(SomeLongParameter).Times(
826     //       2);
827     // We don't want to do this for short parameters as they can just be
828     // indexes.
829     CurrentState.NoLineBreak = true;
830   }
831 
832   // Don't allow the RHS of an operator to be split over multiple lines unless
833   // there is a line-break right after the operator.
834   // Exclude relational operators, as there, it is always more desirable to
835   // have the LHS 'left' of the RHS.
836   const FormatToken *P = Current.getPreviousNonComment();
837   if (Current.isNot(tok::comment) && P &&
838       (P->isOneOf(TT_BinaryOperator, tok::comma) ||
839        (P->is(TT_ConditionalExpr) && P->is(tok::colon))) &&
840       !P->isOneOf(TT_OverloadedOperator, TT_CtorInitializerComma) &&
841       P->getPrecedence() != prec::Assignment &&
842       P->getPrecedence() != prec::Relational &&
843       P->getPrecedence() != prec::Spaceship) {
844     bool BreakBeforeOperator =
845         P->MustBreakBefore || P->is(tok::lessless) ||
846         (P->is(TT_BinaryOperator) &&
847          Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None) ||
848         (P->is(TT_ConditionalExpr) && Style.BreakBeforeTernaryOperators);
849     // Don't do this if there are only two operands. In these cases, there is
850     // always a nice vertical separation between them and the extra line break
851     // does not help.
852     bool HasTwoOperands = P->OperatorIndex == 0 && !P->NextOperator &&
853                           P->isNot(TT_ConditionalExpr);
854     if ((!BreakBeforeOperator &&
855          !(HasTwoOperands &&
856            Style.AlignOperands != FormatStyle::OAS_DontAlign)) ||
857         (!CurrentState.LastOperatorWrapped && BreakBeforeOperator)) {
858       CurrentState.NoLineBreakInOperand = true;
859     }
860   }
861 
862   State.Column += Spaces;
863   if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) &&
864       Previous.Previous &&
865       (Previous.Previous->is(tok::kw_for) || Previous.Previous->isIf())) {
866     // Treat the condition inside an if as if it was a second function
867     // parameter, i.e. let nested calls have a continuation indent.
868     CurrentState.LastSpace = State.Column;
869     CurrentState.NestedBlockIndent = State.Column;
870   } else if (!Current.isOneOf(tok::comment, tok::caret) &&
871              ((Previous.is(tok::comma) &&
872                Previous.isNot(TT_OverloadedOperator)) ||
873               (Previous.is(tok::colon) && Previous.is(TT_ObjCMethodExpr)))) {
874     CurrentState.LastSpace = State.Column;
875   } else if (Previous.is(TT_CtorInitializerColon) &&
876              (!Current.isTrailingComment() || Current.NewlinesBefore > 0) &&
877              Style.BreakConstructorInitializers ==
878                  FormatStyle::BCIS_AfterColon) {
879     CurrentState.Indent = State.Column;
880     CurrentState.LastSpace = State.Column;
881   } else if (Previous.isOneOf(TT_ConditionalExpr, TT_CtorInitializerColon)) {
882     CurrentState.LastSpace = State.Column;
883   } else if (Previous.is(TT_BinaryOperator) &&
884              ((Previous.getPrecedence() != prec::Assignment &&
885                (Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 ||
886                 Previous.NextOperator)) ||
887               Current.StartsBinaryExpression)) {
888     // Indent relative to the RHS of the expression unless this is a simple
889     // assignment without binary expression on the RHS.
890     if (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None)
891       CurrentState.LastSpace = State.Column;
892   } else if (Previous.is(TT_InheritanceColon)) {
893     CurrentState.Indent = State.Column;
894     CurrentState.LastSpace = State.Column;
895   } else if (Current.is(TT_CSharpGenericTypeConstraintColon)) {
896     CurrentState.ColonPos = State.Column;
897   } else if (Previous.opensScope()) {
898     // If a function has a trailing call, indent all parameters from the
899     // opening parenthesis. This avoids confusing indents like:
900     //   OuterFunction(InnerFunctionCall( // break
901     //       ParameterToInnerFunction))   // break
902     //       .SecondInnerFunctionCall();
903     if (Previous.MatchingParen) {
904       const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
905       if (Next && Next->isMemberAccess() && State.Stack.size() > 1 &&
906           State.Stack[State.Stack.size() - 2].CallContinuation == 0) {
907         CurrentState.LastSpace = State.Column;
908       }
909     }
910   }
911 }
912 
913 unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
914                                                  bool DryRun) {
915   FormatToken &Current = *State.NextToken;
916   assert(State.NextToken->Previous);
917   const FormatToken &Previous = *State.NextToken->Previous;
918   auto &CurrentState = State.Stack.back();
919 
920   // Extra penalty that needs to be added because of the way certain line
921   // breaks are chosen.
922   unsigned Penalty = 0;
923 
924   const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
925   const FormatToken *NextNonComment = Previous.getNextNonComment();
926   if (!NextNonComment)
927     NextNonComment = &Current;
928   // The first line break on any NestingLevel causes an extra penalty in order
929   // prefer similar line breaks.
930   if (!CurrentState.ContainsLineBreak)
931     Penalty += 15;
932   CurrentState.ContainsLineBreak = true;
933 
934   Penalty += State.NextToken->SplitPenalty;
935 
936   // Breaking before the first "<<" is generally not desirable if the LHS is
937   // short. Also always add the penalty if the LHS is split over multiple lines
938   // to avoid unnecessary line breaks that just work around this penalty.
939   if (NextNonComment->is(tok::lessless) && CurrentState.FirstLessLess == 0 &&
940       (State.Column <= Style.ColumnLimit / 3 ||
941        CurrentState.BreakBeforeParameter)) {
942     Penalty += Style.PenaltyBreakFirstLessLess;
943   }
944 
945   State.Column = getNewLineColumn(State);
946 
947   // Add Penalty proportional to amount of whitespace away from FirstColumn
948   // This tends to penalize several lines that are far-right indented,
949   // and prefers a line-break prior to such a block, e.g:
950   //
951   // Constructor() :
952   //   member(value), looooooooooooooooong_member(
953   //                      looooooooooong_call(param_1, param_2, param_3))
954   // would then become
955   // Constructor() :
956   //   member(value),
957   //   looooooooooooooooong_member(
958   //       looooooooooong_call(param_1, param_2, param_3))
959   if (State.Column > State.FirstIndent) {
960     Penalty +=
961         Style.PenaltyIndentedWhitespace * (State.Column - State.FirstIndent);
962   }
963 
964   // Indent nested blocks relative to this column, unless in a very specific
965   // JavaScript special case where:
966   //
967   //   var loooooong_name =
968   //       function() {
969   //     // code
970   //   }
971   //
972   // is common and should be formatted like a free-standing function. The same
973   // goes for wrapping before the lambda return type arrow.
974   if (Current.isNot(TT_TrailingReturnArrow) &&
975       (!Style.isJavaScript() || Current.NestingLevel != 0 ||
976        !PreviousNonComment || PreviousNonComment->isNot(tok::equal) ||
977        !Current.isOneOf(Keywords.kw_async, Keywords.kw_function))) {
978     CurrentState.NestedBlockIndent = State.Column;
979   }
980 
981   if (NextNonComment->isMemberAccess()) {
982     if (CurrentState.CallContinuation == 0)
983       CurrentState.CallContinuation = State.Column;
984   } else if (NextNonComment->is(TT_SelectorName)) {
985     if (!CurrentState.ObjCSelectorNameFound) {
986       if (NextNonComment->LongestObjCSelectorName == 0) {
987         CurrentState.AlignColons = false;
988       } else {
989         CurrentState.ColonPos =
990             (shouldIndentWrappedSelectorName(Style, State.Line->Type)
991                  ? std::max(CurrentState.Indent,
992                             State.FirstIndent + Style.ContinuationIndentWidth)
993                  : CurrentState.Indent) +
994             std::max(NextNonComment->LongestObjCSelectorName,
995                      NextNonComment->ColumnWidth);
996       }
997     } else if (CurrentState.AlignColons &&
998                CurrentState.ColonPos <= NextNonComment->ColumnWidth) {
999       CurrentState.ColonPos = State.Column + NextNonComment->ColumnWidth;
1000     }
1001   } else if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
1002              PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) {
1003     // FIXME: This is hacky, find a better way. The problem is that in an ObjC
1004     // method expression, the block should be aligned to the line starting it,
1005     // e.g.:
1006     //   [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason
1007     //                        ^(int *i) {
1008     //                            // ...
1009     //                        }];
1010     // Thus, we set LastSpace of the next higher NestingLevel, to which we move
1011     // when we consume all of the "}"'s FakeRParens at the "{".
1012     if (State.Stack.size() > 1) {
1013       State.Stack[State.Stack.size() - 2].LastSpace =
1014           std::max(CurrentState.LastSpace, CurrentState.Indent) +
1015           Style.ContinuationIndentWidth;
1016     }
1017   }
1018 
1019   if ((PreviousNonComment &&
1020        PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
1021        !CurrentState.AvoidBinPacking) ||
1022       Previous.is(TT_BinaryOperator)) {
1023     CurrentState.BreakBeforeParameter = false;
1024   }
1025   if (PreviousNonComment &&
1026       (PreviousNonComment->isOneOf(TT_TemplateCloser, TT_JavaAnnotation) ||
1027        PreviousNonComment->ClosesRequiresClause) &&
1028       Current.NestingLevel == 0) {
1029     CurrentState.BreakBeforeParameter = false;
1030   }
1031   if (NextNonComment->is(tok::question) ||
1032       (PreviousNonComment && PreviousNonComment->is(tok::question))) {
1033     CurrentState.BreakBeforeParameter = true;
1034   }
1035   if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore)
1036     CurrentState.BreakBeforeParameter = false;
1037 
1038   if (!DryRun) {
1039     unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1;
1040     if (Current.is(tok::r_brace) && Current.MatchingParen &&
1041         // Only strip trailing empty lines for l_braces that have children, i.e.
1042         // for function expressions (lambdas, arrows, etc).
1043         !Current.MatchingParen->Children.empty()) {
1044       // lambdas and arrow functions are expressions, thus their r_brace is not
1045       // on its own line, and thus not covered by UnwrappedLineFormatter's logic
1046       // about removing empty lines on closing blocks. Special case them here.
1047       MaxEmptyLinesToKeep = 1;
1048     }
1049     unsigned Newlines =
1050         std::max(1u, std::min(Current.NewlinesBefore, MaxEmptyLinesToKeep));
1051     bool ContinuePPDirective =
1052         State.Line->InPPDirective && State.Line->Type != LT_ImportStatement;
1053     Whitespaces.replaceWhitespace(Current, Newlines, State.Column, State.Column,
1054                                   CurrentState.IsAligned, ContinuePPDirective);
1055   }
1056 
1057   if (!Current.isTrailingComment())
1058     CurrentState.LastSpace = State.Column;
1059   if (Current.is(tok::lessless)) {
1060     // If we are breaking before a "<<", we always want to indent relative to
1061     // RHS. This is necessary only for "<<", as we special-case it and don't
1062     // always indent relative to the RHS.
1063     CurrentState.LastSpace += 3; // 3 -> width of "<< ".
1064   }
1065 
1066   State.StartOfLineLevel = Current.NestingLevel;
1067   State.LowestLevelOnLine = Current.NestingLevel;
1068 
1069   // Any break on this level means that the parent level has been broken
1070   // and we need to avoid bin packing there.
1071   bool NestedBlockSpecialCase =
1072       (!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
1073        State.Stack[State.Stack.size() - 2].NestedBlockInlined) ||
1074       (Style.Language == FormatStyle::LK_ObjC && Current.is(tok::r_brace) &&
1075        State.Stack.size() > 1 && !Style.ObjCBreakBeforeNestedBlockParam);
1076   // Do not force parameter break for statements with requires expressions.
1077   NestedBlockSpecialCase =
1078       NestedBlockSpecialCase ||
1079       (Current.MatchingParen &&
1080        Current.MatchingParen->is(TT_RequiresExpressionLBrace));
1081   if (!NestedBlockSpecialCase) {
1082     auto ParentLevelIt = std::next(State.Stack.rbegin());
1083     if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
1084         Current.MatchingParen && Current.MatchingParen->is(TT_LambdaLBrace)) {
1085       // If the first character on the new line is a lambda's closing brace, the
1086       // stack still contains that lambda's parenthesis. As such, we need to
1087       // recurse further down the stack than usual to find the parenthesis level
1088       // containing the lambda, which is where we want to set
1089       // BreakBeforeParameter.
1090       //
1091       // We specifically special case "OuterScope"-formatted lambdas here
1092       // because, when using that setting, breaking before the parameter
1093       // directly following the lambda is particularly unsightly. However, when
1094       // "OuterScope" is not set, the logic to find the parent parenthesis level
1095       // still appears to be sometimes incorrect. It has not been fixed yet
1096       // because it would lead to significant changes in existing behaviour.
1097       //
1098       // TODO: fix the non-"OuterScope" case too.
1099       auto FindCurrentLevel = [&](const auto &It) {
1100         return std::find_if(It, State.Stack.rend(), [](const auto &PState) {
1101           return PState.Tok != nullptr; // Ignore fake parens.
1102         });
1103       };
1104       auto MaybeIncrement = [&](const auto &It) {
1105         return It != State.Stack.rend() ? std::next(It) : It;
1106       };
1107       auto LambdaLevelIt = FindCurrentLevel(State.Stack.rbegin());
1108       auto LevelContainingLambdaIt =
1109           FindCurrentLevel(MaybeIncrement(LambdaLevelIt));
1110       ParentLevelIt = MaybeIncrement(LevelContainingLambdaIt);
1111     }
1112     for (auto I = ParentLevelIt, E = State.Stack.rend(); I != E; ++I)
1113       I->BreakBeforeParameter = true;
1114   }
1115 
1116   if (PreviousNonComment &&
1117       !PreviousNonComment->isOneOf(tok::comma, tok::colon, tok::semi) &&
1118       ((PreviousNonComment->isNot(TT_TemplateCloser) &&
1119         !PreviousNonComment->ClosesRequiresClause) ||
1120        Current.NestingLevel != 0) &&
1121       !PreviousNonComment->isOneOf(
1122           TT_BinaryOperator, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
1123           TT_LeadingJavaAnnotation) &&
1124       Current.isNot(TT_BinaryOperator) && !PreviousNonComment->opensScope() &&
1125       // We don't want to enforce line breaks for subsequent arguments just
1126       // because we have been forced to break before a lambda body.
1127       (!Style.BraceWrapping.BeforeLambdaBody ||
1128        Current.isNot(TT_LambdaLBrace))) {
1129     CurrentState.BreakBeforeParameter = true;
1130   }
1131 
1132   // If we break after { or the [ of an array initializer, we should also break
1133   // before the corresponding } or ].
1134   if (PreviousNonComment &&
1135       (PreviousNonComment->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
1136        opensProtoMessageField(*PreviousNonComment, Style))) {
1137     CurrentState.BreakBeforeClosingBrace = true;
1138   }
1139 
1140   if (PreviousNonComment && PreviousNonComment->is(tok::l_paren)) {
1141     CurrentState.BreakBeforeClosingParen =
1142         Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
1143   }
1144 
1145   if (CurrentState.AvoidBinPacking) {
1146     // If we are breaking after '(', '{', '<', or this is the break after a ':'
1147     // to start a member initializer list in a constructor, this should not
1148     // be considered bin packing unless the relevant AllowAll option is false or
1149     // this is a dict/object literal.
1150     bool PreviousIsBreakingCtorInitializerColon =
1151         PreviousNonComment && PreviousNonComment->is(TT_CtorInitializerColon) &&
1152         Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon;
1153     bool AllowAllConstructorInitializersOnNextLine =
1154         Style.PackConstructorInitializers == FormatStyle::PCIS_NextLine ||
1155         Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly;
1156     if (!(Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) ||
1157           PreviousIsBreakingCtorInitializerColon) ||
1158         (!Style.AllowAllParametersOfDeclarationOnNextLine &&
1159          State.Line->MustBeDeclaration) ||
1160         (!Style.AllowAllArgumentsOnNextLine &&
1161          !State.Line->MustBeDeclaration) ||
1162         (!AllowAllConstructorInitializersOnNextLine &&
1163          PreviousIsBreakingCtorInitializerColon) ||
1164         Previous.is(TT_DictLiteral)) {
1165       CurrentState.BreakBeforeParameter = true;
1166     }
1167 
1168     // If we are breaking after a ':' to start a member initializer list,
1169     // and we allow all arguments on the next line, we should not break
1170     // before the next parameter.
1171     if (PreviousIsBreakingCtorInitializerColon &&
1172         AllowAllConstructorInitializersOnNextLine) {
1173       CurrentState.BreakBeforeParameter = false;
1174     }
1175   }
1176 
1177   return Penalty;
1178 }
1179 
1180 unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
1181   if (!State.NextToken || !State.NextToken->Previous)
1182     return 0;
1183 
1184   FormatToken &Current = *State.NextToken;
1185   const auto &CurrentState = State.Stack.back();
1186 
1187   if (CurrentState.IsCSharpGenericTypeConstraint &&
1188       Current.isNot(TT_CSharpGenericTypeConstraint)) {
1189     return CurrentState.ColonPos + 2;
1190   }
1191 
1192   const FormatToken &Previous = *Current.Previous;
1193   // If we are continuing an expression, we want to use the continuation indent.
1194   unsigned ContinuationIndent =
1195       std::max(CurrentState.LastSpace, CurrentState.Indent) +
1196       Style.ContinuationIndentWidth;
1197   const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
1198   const FormatToken *NextNonComment = Previous.getNextNonComment();
1199   if (!NextNonComment)
1200     NextNonComment = &Current;
1201 
1202   // Java specific bits.
1203   if (Style.Language == FormatStyle::LK_Java &&
1204       Current.isOneOf(Keywords.kw_implements, Keywords.kw_extends)) {
1205     return std::max(CurrentState.LastSpace,
1206                     CurrentState.Indent + Style.ContinuationIndentWidth);
1207   }
1208 
1209   // Indentation of the statement following a Verilog case label is taken care
1210   // of in moveStateToNextToken.
1211   if (Style.isVerilog() && PreviousNonComment &&
1212       Keywords.isVerilogEndOfLabel(*PreviousNonComment)) {
1213     return State.FirstIndent;
1214   }
1215 
1216   if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths &&
1217       State.Line->First->is(tok::kw_enum)) {
1218     return (Style.IndentWidth * State.Line->First->IndentLevel) +
1219            Style.IndentWidth;
1220   }
1221 
1222   if ((NextNonComment->is(tok::l_brace) && NextNonComment->is(BK_Block)) ||
1223       (Style.isVerilog() && Keywords.isVerilogBegin(*NextNonComment))) {
1224     if (Current.NestingLevel == 0 ||
1225         (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
1226          State.NextToken->is(TT_LambdaLBrace))) {
1227       return State.FirstIndent;
1228     }
1229     return CurrentState.Indent;
1230   }
1231   if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
1232        (Current.is(tok::greater) && Style.isProto())) &&
1233       State.Stack.size() > 1) {
1234     if (Current.closesBlockOrBlockTypeList(Style))
1235       return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
1236     if (Current.MatchingParen && Current.MatchingParen->is(BK_BracedInit))
1237       return State.Stack[State.Stack.size() - 2].LastSpace;
1238     return State.FirstIndent;
1239   }
1240   // Indent a closing parenthesis at the previous level if followed by a semi,
1241   // const, or opening brace. This allows indentations such as:
1242   //     foo(
1243   //       a,
1244   //     );
1245   //     int Foo::getter(
1246   //         //
1247   //     ) const {
1248   //       return foo;
1249   //     }
1250   //     function foo(
1251   //       a,
1252   //     ) {
1253   //       code(); //
1254   //     }
1255   if (Current.is(tok::r_paren) && State.Stack.size() > 1 &&
1256       (!Current.Next ||
1257        Current.Next->isOneOf(tok::semi, tok::kw_const, tok::l_brace))) {
1258     return State.Stack[State.Stack.size() - 2].LastSpace;
1259   }
1260   if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
1261       (Current.is(tok::r_paren) ||
1262        (Current.is(tok::r_brace) && Current.MatchingParen &&
1263         Current.MatchingParen->is(BK_BracedInit))) &&
1264       State.Stack.size() > 1) {
1265     return State.Stack[State.Stack.size() - 2].LastSpace;
1266   }
1267   if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope())
1268     return State.Stack[State.Stack.size() - 2].LastSpace;
1269   // Field labels in a nested type should be aligned to the brace. For example
1270   // in ProtoBuf:
1271   //   optional int32 b = 2 [(foo_options) = {aaaaaaaaaaaaaaaaaaa: 123,
1272   //                                          bbbbbbbbbbbbbbbbbbbbbbbb:"baz"}];
1273   // For Verilog, a quote following a brace is treated as an identifier.  And
1274   // Both braces and colons get annotated as TT_DictLiteral.  So we have to
1275   // check.
1276   if (Current.is(tok::identifier) && Current.Next &&
1277       (!Style.isVerilog() || Current.Next->is(tok::colon)) &&
1278       (Current.Next->is(TT_DictLiteral) ||
1279        (Style.isProto() && Current.Next->isOneOf(tok::less, tok::l_brace)))) {
1280     return CurrentState.Indent;
1281   }
1282   if (NextNonComment->is(TT_ObjCStringLiteral) &&
1283       State.StartOfStringLiteral != 0) {
1284     return State.StartOfStringLiteral - 1;
1285   }
1286   if (NextNonComment->isStringLiteral() && State.StartOfStringLiteral != 0)
1287     return State.StartOfStringLiteral;
1288   if (NextNonComment->is(tok::lessless) && CurrentState.FirstLessLess != 0)
1289     return CurrentState.FirstLessLess;
1290   if (NextNonComment->isMemberAccess()) {
1291     if (CurrentState.CallContinuation == 0)
1292       return ContinuationIndent;
1293     return CurrentState.CallContinuation;
1294   }
1295   if (CurrentState.QuestionColumn != 0 &&
1296       ((NextNonComment->is(tok::colon) &&
1297         NextNonComment->is(TT_ConditionalExpr)) ||
1298        Previous.is(TT_ConditionalExpr))) {
1299     if (((NextNonComment->is(tok::colon) && NextNonComment->Next &&
1300           !NextNonComment->Next->FakeLParens.empty() &&
1301           NextNonComment->Next->FakeLParens.back() == prec::Conditional) ||
1302          (Previous.is(tok::colon) && !Current.FakeLParens.empty() &&
1303           Current.FakeLParens.back() == prec::Conditional)) &&
1304         !CurrentState.IsWrappedConditional) {
1305       // NOTE: we may tweak this slightly:
1306       //    * not remove the 'lead' ContinuationIndentWidth
1307       //    * always un-indent by the operator when
1308       //    BreakBeforeTernaryOperators=true
1309       unsigned Indent = CurrentState.Indent;
1310       if (Style.AlignOperands != FormatStyle::OAS_DontAlign)
1311         Indent -= Style.ContinuationIndentWidth;
1312       if (Style.BreakBeforeTernaryOperators && CurrentState.UnindentOperator)
1313         Indent -= 2;
1314       return Indent;
1315     }
1316     return CurrentState.QuestionColumn;
1317   }
1318   if (Previous.is(tok::comma) && CurrentState.VariablePos != 0)
1319     return CurrentState.VariablePos;
1320   if (Current.is(TT_RequiresClause)) {
1321     if (Style.IndentRequiresClause)
1322       return CurrentState.Indent + Style.IndentWidth;
1323     switch (Style.RequiresClausePosition) {
1324     case FormatStyle::RCPS_OwnLine:
1325     case FormatStyle::RCPS_WithFollowing:
1326       return CurrentState.Indent;
1327     default:
1328       break;
1329     }
1330   }
1331   if (NextNonComment->isOneOf(TT_CtorInitializerColon, TT_InheritanceColon,
1332                               TT_InheritanceComma)) {
1333     return State.FirstIndent + Style.ConstructorInitializerIndentWidth;
1334   }
1335   if ((PreviousNonComment &&
1336        (PreviousNonComment->ClosesTemplateDeclaration ||
1337         PreviousNonComment->ClosesRequiresClause ||
1338         (PreviousNonComment->is(TT_AttributeMacro) &&
1339          Current.isNot(tok::l_paren)) ||
1340         PreviousNonComment->isOneOf(
1341             TT_AttributeRParen, TT_AttributeSquare, TT_FunctionAnnotationRParen,
1342             TT_JavaAnnotation, TT_LeadingJavaAnnotation))) ||
1343       (!Style.IndentWrappedFunctionNames &&
1344        NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName))) {
1345     return std::max(CurrentState.LastSpace, CurrentState.Indent);
1346   }
1347   if (NextNonComment->is(TT_SelectorName)) {
1348     if (!CurrentState.ObjCSelectorNameFound) {
1349       unsigned MinIndent = CurrentState.Indent;
1350       if (shouldIndentWrappedSelectorName(Style, State.Line->Type)) {
1351         MinIndent = std::max(MinIndent,
1352                              State.FirstIndent + Style.ContinuationIndentWidth);
1353       }
1354       // If LongestObjCSelectorName is 0, we are indenting the first
1355       // part of an ObjC selector (or a selector component which is
1356       // not colon-aligned due to block formatting).
1357       //
1358       // Otherwise, we are indenting a subsequent part of an ObjC
1359       // selector which should be colon-aligned to the longest
1360       // component of the ObjC selector.
1361       //
1362       // In either case, we want to respect Style.IndentWrappedFunctionNames.
1363       return MinIndent +
1364              std::max(NextNonComment->LongestObjCSelectorName,
1365                       NextNonComment->ColumnWidth) -
1366              NextNonComment->ColumnWidth;
1367     }
1368     if (!CurrentState.AlignColons)
1369       return CurrentState.Indent;
1370     if (CurrentState.ColonPos > NextNonComment->ColumnWidth)
1371       return CurrentState.ColonPos - NextNonComment->ColumnWidth;
1372     return CurrentState.Indent;
1373   }
1374   if (NextNonComment->is(tok::colon) && NextNonComment->is(TT_ObjCMethodExpr))
1375     return CurrentState.ColonPos;
1376   if (NextNonComment->is(TT_ArraySubscriptLSquare)) {
1377     if (CurrentState.StartOfArraySubscripts != 0) {
1378       return CurrentState.StartOfArraySubscripts;
1379     } else if (Style.isCSharp()) { // C# allows `["key"] = value` inside object
1380                                    // initializers.
1381       return CurrentState.Indent;
1382     }
1383     return ContinuationIndent;
1384   }
1385 
1386   // OpenMP clauses want to get additional indentation when they are pushed onto
1387   // the next line.
1388   if (State.Line->InPragmaDirective) {
1389     FormatToken *PragmaType = State.Line->First->Next->Next;
1390     if (PragmaType && PragmaType->TokenText.equals("omp"))
1391       return CurrentState.Indent + Style.ContinuationIndentWidth;
1392   }
1393 
1394   // This ensure that we correctly format ObjC methods calls without inputs,
1395   // i.e. where the last element isn't selector like: [callee method];
1396   if (NextNonComment->is(tok::identifier) && NextNonComment->FakeRParens == 0 &&
1397       NextNonComment->Next && NextNonComment->Next->is(TT_ObjCMethodExpr)) {
1398     return CurrentState.Indent;
1399   }
1400 
1401   if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) ||
1402       Previous.isOneOf(tok::coloncolon, tok::equal, TT_JsTypeColon)) {
1403     return ContinuationIndent;
1404   }
1405   if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
1406       PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) {
1407     return ContinuationIndent;
1408   }
1409   if (NextNonComment->is(TT_CtorInitializerComma))
1410     return CurrentState.Indent;
1411   if (PreviousNonComment && PreviousNonComment->is(TT_CtorInitializerColon) &&
1412       Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon) {
1413     return CurrentState.Indent;
1414   }
1415   if (PreviousNonComment && PreviousNonComment->is(TT_InheritanceColon) &&
1416       Style.BreakInheritanceList == FormatStyle::BILS_AfterColon) {
1417     return CurrentState.Indent;
1418   }
1419   if (Previous.is(tok::r_paren) && !Current.isBinaryOperator() &&
1420       !Current.isOneOf(tok::colon, tok::comment)) {
1421     return ContinuationIndent;
1422   }
1423   if (Current.is(TT_ProtoExtensionLSquare))
1424     return CurrentState.Indent;
1425   if (Current.isBinaryOperator() && CurrentState.UnindentOperator) {
1426     return CurrentState.Indent - Current.Tok.getLength() -
1427            Current.SpacesRequiredBefore;
1428   }
1429   if (Current.is(tok::comment) && NextNonComment->isBinaryOperator() &&
1430       CurrentState.UnindentOperator) {
1431     return CurrentState.Indent - NextNonComment->Tok.getLength() -
1432            NextNonComment->SpacesRequiredBefore;
1433   }
1434   if (CurrentState.Indent == State.FirstIndent && PreviousNonComment &&
1435       !PreviousNonComment->isOneOf(tok::r_brace, TT_CtorInitializerComma)) {
1436     // Ensure that we fall back to the continuation indent width instead of
1437     // just flushing continuations left.
1438     return CurrentState.Indent + Style.ContinuationIndentWidth;
1439   }
1440   return CurrentState.Indent;
1441 }
1442 
1443 static bool hasNestedBlockInlined(const FormatToken *Previous,
1444                                   const FormatToken &Current,
1445                                   const FormatStyle &Style) {
1446   if (Previous->isNot(tok::l_paren))
1447     return true;
1448   if (Previous->ParameterCount > 1)
1449     return true;
1450 
1451   // Also a nested block if contains a lambda inside function with 1 parameter.
1452   return Style.BraceWrapping.BeforeLambdaBody && Current.is(TT_LambdaLSquare);
1453 }
1454 
1455 unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
1456                                                     bool DryRun, bool Newline) {
1457   assert(State.Stack.size());
1458   const FormatToken &Current = *State.NextToken;
1459   auto &CurrentState = State.Stack.back();
1460 
1461   if (Current.is(TT_CSharpGenericTypeConstraint))
1462     CurrentState.IsCSharpGenericTypeConstraint = true;
1463   if (Current.isOneOf(tok::comma, TT_BinaryOperator))
1464     CurrentState.NoLineBreakInOperand = false;
1465   if (Current.isOneOf(TT_InheritanceColon, TT_CSharpGenericTypeConstraintColon))
1466     CurrentState.AvoidBinPacking = true;
1467   if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator)) {
1468     if (CurrentState.FirstLessLess == 0)
1469       CurrentState.FirstLessLess = State.Column;
1470     else
1471       CurrentState.LastOperatorWrapped = Newline;
1472   }
1473   if (Current.is(TT_BinaryOperator) && Current.isNot(tok::lessless))
1474     CurrentState.LastOperatorWrapped = Newline;
1475   if (Current.is(TT_ConditionalExpr) && Current.Previous &&
1476       Current.Previous->isNot(TT_ConditionalExpr)) {
1477     CurrentState.LastOperatorWrapped = Newline;
1478   }
1479   if (Current.is(TT_ArraySubscriptLSquare) &&
1480       CurrentState.StartOfArraySubscripts == 0) {
1481     CurrentState.StartOfArraySubscripts = State.Column;
1482   }
1483 
1484   auto IsWrappedConditional = [](const FormatToken &Tok) {
1485     if (!(Tok.is(TT_ConditionalExpr) && Tok.is(tok::question)))
1486       return false;
1487     if (Tok.MustBreakBefore)
1488       return true;
1489 
1490     const FormatToken *Next = Tok.getNextNonComment();
1491     return Next && Next->MustBreakBefore;
1492   };
1493   if (IsWrappedConditional(Current))
1494     CurrentState.IsWrappedConditional = true;
1495   if (Style.BreakBeforeTernaryOperators && Current.is(tok::question))
1496     CurrentState.QuestionColumn = State.Column;
1497   if (!Style.BreakBeforeTernaryOperators && Current.isNot(tok::colon)) {
1498     const FormatToken *Previous = Current.Previous;
1499     while (Previous && Previous->isTrailingComment())
1500       Previous = Previous->Previous;
1501     if (Previous && Previous->is(tok::question))
1502       CurrentState.QuestionColumn = State.Column;
1503   }
1504   if (!Current.opensScope() && !Current.closesScope() &&
1505       Current.isNot(TT_PointerOrReference)) {
1506     State.LowestLevelOnLine =
1507         std::min(State.LowestLevelOnLine, Current.NestingLevel);
1508   }
1509   if (Current.isMemberAccess())
1510     CurrentState.StartOfFunctionCall = !Current.NextOperator ? 0 : State.Column;
1511   if (Current.is(TT_SelectorName))
1512     CurrentState.ObjCSelectorNameFound = true;
1513   if (Current.is(TT_CtorInitializerColon) &&
1514       Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon) {
1515     // Indent 2 from the column, so:
1516     // SomeClass::SomeClass()
1517     //     : First(...), ...
1518     //       Next(...)
1519     //       ^ line up here.
1520     CurrentState.Indent = State.Column + (Style.BreakConstructorInitializers ==
1521                                                   FormatStyle::BCIS_BeforeComma
1522                                               ? 0
1523                                               : 2);
1524     CurrentState.NestedBlockIndent = CurrentState.Indent;
1525     if (Style.PackConstructorInitializers > FormatStyle::PCIS_BinPack) {
1526       CurrentState.AvoidBinPacking = true;
1527       CurrentState.BreakBeforeParameter =
1528           Style.ColumnLimit > 0 &&
1529           Style.PackConstructorInitializers != FormatStyle::PCIS_NextLine &&
1530           Style.PackConstructorInitializers != FormatStyle::PCIS_NextLineOnly;
1531     } else {
1532       CurrentState.BreakBeforeParameter = false;
1533     }
1534   }
1535   if (Current.is(TT_CtorInitializerColon) &&
1536       Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon) {
1537     CurrentState.Indent =
1538         State.FirstIndent + Style.ConstructorInitializerIndentWidth;
1539     CurrentState.NestedBlockIndent = CurrentState.Indent;
1540     if (Style.PackConstructorInitializers > FormatStyle::PCIS_BinPack)
1541       CurrentState.AvoidBinPacking = true;
1542     else
1543       CurrentState.BreakBeforeParameter = false;
1544   }
1545   if (Current.is(TT_InheritanceColon)) {
1546     CurrentState.Indent =
1547         State.FirstIndent + Style.ConstructorInitializerIndentWidth;
1548   }
1549   if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && Newline)
1550     CurrentState.NestedBlockIndent = State.Column + Current.ColumnWidth + 1;
1551   if (Current.isOneOf(TT_LambdaLSquare, TT_TrailingReturnArrow))
1552     CurrentState.LastSpace = State.Column;
1553   if (Current.is(TT_RequiresExpression) &&
1554       Style.RequiresExpressionIndentation == FormatStyle::REI_Keyword) {
1555     CurrentState.NestedBlockIndent = State.Column;
1556   }
1557 
1558   // Insert scopes created by fake parenthesis.
1559   const FormatToken *Previous = Current.getPreviousNonComment();
1560 
1561   // Add special behavior to support a format commonly used for JavaScript
1562   // closures:
1563   //   SomeFunction(function() {
1564   //     foo();
1565   //     bar();
1566   //   }, a, b, c);
1567   if (Current.isNot(tok::comment) && !Current.ClosesRequiresClause &&
1568       Previous && Previous->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
1569       Previous->isNot(TT_DictLiteral) && State.Stack.size() > 1 &&
1570       !CurrentState.HasMultipleNestedBlocks) {
1571     if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline)
1572       for (ParenState &PState : llvm::drop_end(State.Stack))
1573         PState.NoLineBreak = true;
1574     State.Stack[State.Stack.size() - 2].NestedBlockInlined = false;
1575   }
1576   if (Previous && (Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr) ||
1577                    (Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) &&
1578                     !Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)))) {
1579     CurrentState.NestedBlockInlined =
1580         !Newline && hasNestedBlockInlined(Previous, Current, Style);
1581   }
1582 
1583   moveStatePastFakeLParens(State, Newline);
1584   moveStatePastScopeCloser(State);
1585   // Do not use CurrentState here, since the two functions before may change the
1586   // Stack.
1587   bool AllowBreak = !State.Stack.back().NoLineBreak &&
1588                     !State.Stack.back().NoLineBreakInOperand;
1589   moveStatePastScopeOpener(State, Newline);
1590   moveStatePastFakeRParens(State);
1591 
1592   if (Current.is(TT_ObjCStringLiteral) && State.StartOfStringLiteral == 0)
1593     State.StartOfStringLiteral = State.Column + 1;
1594   if (Current.is(TT_CSharpStringLiteral) && State.StartOfStringLiteral == 0) {
1595     State.StartOfStringLiteral = State.Column + 1;
1596   } else if (Current.isStringLiteral() && State.StartOfStringLiteral == 0) {
1597     State.StartOfStringLiteral = State.Column;
1598   } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
1599              !Current.isStringLiteral()) {
1600     State.StartOfStringLiteral = 0;
1601   }
1602 
1603   State.Column += Current.ColumnWidth;
1604   State.NextToken = State.NextToken->Next;
1605   // Verilog case labels are on the same unwrapped lines as the statements that
1606   // follow. TokenAnnotator identifies them and sets MustBreakBefore.
1607   // Indentation is taken care of here. A case label can only have 1 statement
1608   // in Verilog, so we don't have to worry about lines that follow.
1609   if (Style.isVerilog() && State.NextToken &&
1610       State.NextToken->MustBreakBefore &&
1611       Keywords.isVerilogEndOfLabel(Current)) {
1612     State.FirstIndent += Style.IndentWidth;
1613     CurrentState.Indent = State.FirstIndent;
1614   }
1615 
1616   unsigned Penalty =
1617       handleEndOfLine(Current, State, DryRun, AllowBreak, Newline);
1618 
1619   if (Current.Role)
1620     Current.Role->formatFromToken(State, this, DryRun);
1621   // If the previous has a special role, let it consume tokens as appropriate.
1622   // It is necessary to start at the previous token for the only implemented
1623   // role (comma separated list). That way, the decision whether or not to break
1624   // after the "{" is already done and both options are tried and evaluated.
1625   // FIXME: This is ugly, find a better way.
1626   if (Previous && Previous->Role)
1627     Penalty += Previous->Role->formatAfterToken(State, this, DryRun);
1628 
1629   return Penalty;
1630 }
1631 
1632 void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
1633                                                     bool Newline) {
1634   const FormatToken &Current = *State.NextToken;
1635   if (Current.FakeLParens.empty())
1636     return;
1637 
1638   const FormatToken *Previous = Current.getPreviousNonComment();
1639 
1640   // Don't add extra indentation for the first fake parenthesis after
1641   // 'return', assignments, opening <({[, or requires clauses. The indentation
1642   // for these cases is special cased.
1643   bool SkipFirstExtraIndent =
1644       Previous &&
1645       (Previous->opensScope() ||
1646        Previous->isOneOf(tok::semi, tok::kw_return, TT_RequiresClause) ||
1647        (Previous->getPrecedence() == prec::Assignment &&
1648         Style.AlignOperands != FormatStyle::OAS_DontAlign) ||
1649        Previous->is(TT_ObjCMethodExpr));
1650   for (const auto &PrecedenceLevel : llvm::reverse(Current.FakeLParens)) {
1651     const auto &CurrentState = State.Stack.back();
1652     ParenState NewParenState = CurrentState;
1653     NewParenState.Tok = nullptr;
1654     NewParenState.ContainsLineBreak = false;
1655     NewParenState.LastOperatorWrapped = true;
1656     NewParenState.IsChainedConditional = false;
1657     NewParenState.IsWrappedConditional = false;
1658     NewParenState.UnindentOperator = false;
1659     NewParenState.NoLineBreak =
1660         NewParenState.NoLineBreak || CurrentState.NoLineBreakInOperand;
1661 
1662     // Don't propagate AvoidBinPacking into subexpressions of arg/param lists.
1663     if (PrecedenceLevel > prec::Comma)
1664       NewParenState.AvoidBinPacking = false;
1665 
1666     // Indent from 'LastSpace' unless these are fake parentheses encapsulating
1667     // a builder type call after 'return' or, if the alignment after opening
1668     // brackets is disabled.
1669     if (!Current.isTrailingComment() &&
1670         (Style.AlignOperands != FormatStyle::OAS_DontAlign ||
1671          PrecedenceLevel < prec::Assignment) &&
1672         (!Previous || Previous->isNot(tok::kw_return) ||
1673          (Style.Language != FormatStyle::LK_Java && PrecedenceLevel > 0)) &&
1674         (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
1675          PrecedenceLevel != prec::Comma || Current.NestingLevel == 0)) {
1676       NewParenState.Indent = std::max(
1677           std::max(State.Column, NewParenState.Indent), CurrentState.LastSpace);
1678     }
1679 
1680     // Special case for generic selection expressions, its comma-separated
1681     // expressions are not aligned to the opening paren like regular calls, but
1682     // rather continuation-indented relative to the _Generic keyword.
1683     if (Previous && Previous->endsSequence(tok::l_paren, tok::kw__Generic))
1684       NewParenState.Indent = CurrentState.LastSpace;
1685 
1686     if ((shouldUnindentNextOperator(Current) ||
1687          (Previous &&
1688           (PrecedenceLevel == prec::Conditional &&
1689            Previous->is(tok::question) && Previous->is(TT_ConditionalExpr)))) &&
1690         !Newline) {
1691       // If BreakBeforeBinaryOperators is set, un-indent a bit to account for
1692       // the operator and keep the operands aligned.
1693       if (Style.AlignOperands == FormatStyle::OAS_AlignAfterOperator)
1694         NewParenState.UnindentOperator = true;
1695       // Mark indentation as alignment if the expression is aligned.
1696       if (Style.AlignOperands != FormatStyle::OAS_DontAlign)
1697         NewParenState.IsAligned = true;
1698     }
1699 
1700     // Do not indent relative to the fake parentheses inserted for "." or "->".
1701     // This is a special case to make the following to statements consistent:
1702     //   OuterFunction(InnerFunctionCall( // break
1703     //       ParameterToInnerFunction));
1704     //   OuterFunction(SomeObject.InnerFunctionCall( // break
1705     //       ParameterToInnerFunction));
1706     if (PrecedenceLevel > prec::Unknown)
1707       NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
1708     if (PrecedenceLevel != prec::Conditional &&
1709         Current.isNot(TT_UnaryOperator) &&
1710         Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
1711       NewParenState.StartOfFunctionCall = State.Column;
1712     }
1713 
1714     // Indent conditional expressions, unless they are chained "else-if"
1715     // conditionals. Never indent expression where the 'operator' is ',', ';' or
1716     // an assignment (i.e. *I <= prec::Assignment) as those have different
1717     // indentation rules. Indent other expression, unless the indentation needs
1718     // to be skipped.
1719     if (PrecedenceLevel == prec::Conditional && Previous &&
1720         Previous->is(tok::colon) && Previous->is(TT_ConditionalExpr) &&
1721         &PrecedenceLevel == &Current.FakeLParens.back() &&
1722         !CurrentState.IsWrappedConditional) {
1723       NewParenState.IsChainedConditional = true;
1724       NewParenState.UnindentOperator = State.Stack.back().UnindentOperator;
1725     } else if (PrecedenceLevel == prec::Conditional ||
1726                (!SkipFirstExtraIndent && PrecedenceLevel > prec::Assignment &&
1727                 !Current.isTrailingComment())) {
1728       NewParenState.Indent += Style.ContinuationIndentWidth;
1729     }
1730     if ((Previous && !Previous->opensScope()) || PrecedenceLevel != prec::Comma)
1731       NewParenState.BreakBeforeParameter = false;
1732     State.Stack.push_back(NewParenState);
1733     SkipFirstExtraIndent = false;
1734   }
1735 }
1736 
1737 void ContinuationIndenter::moveStatePastFakeRParens(LineState &State) {
1738   for (unsigned i = 0, e = State.NextToken->FakeRParens; i != e; ++i) {
1739     unsigned VariablePos = State.Stack.back().VariablePos;
1740     if (State.Stack.size() == 1) {
1741       // Do not pop the last element.
1742       break;
1743     }
1744     State.Stack.pop_back();
1745     State.Stack.back().VariablePos = VariablePos;
1746   }
1747 
1748   if (State.NextToken->ClosesRequiresClause && Style.IndentRequiresClause) {
1749     // Remove the indentation of the requires clauses (which is not in Indent,
1750     // but in LastSpace).
1751     State.Stack.back().LastSpace -= Style.IndentWidth;
1752   }
1753 }
1754 
1755 void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
1756                                                     bool Newline) {
1757   const FormatToken &Current = *State.NextToken;
1758   if (!Current.opensScope())
1759     return;
1760 
1761   const auto &CurrentState = State.Stack.back();
1762 
1763   // Don't allow '<' or '(' in C# generic type constraints to start new scopes.
1764   if (Current.isOneOf(tok::less, tok::l_paren) &&
1765       CurrentState.IsCSharpGenericTypeConstraint) {
1766     return;
1767   }
1768 
1769   if (Current.MatchingParen && Current.is(BK_Block)) {
1770     moveStateToNewBlock(State);
1771     return;
1772   }
1773 
1774   unsigned NewIndent;
1775   unsigned LastSpace = CurrentState.LastSpace;
1776   bool AvoidBinPacking;
1777   bool BreakBeforeParameter = false;
1778   unsigned NestedBlockIndent = std::max(CurrentState.StartOfFunctionCall,
1779                                         CurrentState.NestedBlockIndent);
1780   if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
1781       opensProtoMessageField(Current, Style)) {
1782     if (Current.opensBlockOrBlockTypeList(Style)) {
1783       NewIndent = Style.IndentWidth +
1784                   std::min(State.Column, CurrentState.NestedBlockIndent);
1785     } else if (Current.is(tok::l_brace)) {
1786       NewIndent =
1787           CurrentState.LastSpace + Style.BracedInitializerIndentWidth.value_or(
1788                                        Style.ContinuationIndentWidth);
1789     } else {
1790       NewIndent = CurrentState.LastSpace + Style.ContinuationIndentWidth;
1791     }
1792     const FormatToken *NextNonComment = Current.getNextNonComment();
1793     bool EndsInComma = Current.MatchingParen &&
1794                        Current.MatchingParen->Previous &&
1795                        Current.MatchingParen->Previous->is(tok::comma);
1796     AvoidBinPacking = EndsInComma || Current.is(TT_DictLiteral) ||
1797                       Style.isProto() || !Style.BinPackArguments ||
1798                       (NextNonComment && NextNonComment->isOneOf(
1799                                              TT_DesignatedInitializerPeriod,
1800                                              TT_DesignatedInitializerLSquare));
1801     BreakBeforeParameter = EndsInComma;
1802     if (Current.ParameterCount > 1)
1803       NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1);
1804   } else {
1805     NewIndent =
1806         Style.ContinuationIndentWidth +
1807         std::max(CurrentState.LastSpace, CurrentState.StartOfFunctionCall);
1808 
1809     // Ensure that different different brackets force relative alignment, e.g.:
1810     // void SomeFunction(vector<  // break
1811     //                       int> v);
1812     // FIXME: We likely want to do this for more combinations of brackets.
1813     if (Current.is(tok::less) && Current.ParentBracket == tok::l_paren) {
1814       NewIndent = std::max(NewIndent, CurrentState.Indent);
1815       LastSpace = std::max(LastSpace, CurrentState.Indent);
1816     }
1817 
1818     bool EndsInComma =
1819         Current.MatchingParen &&
1820         Current.MatchingParen->getPreviousNonComment() &&
1821         Current.MatchingParen->getPreviousNonComment()->is(tok::comma);
1822 
1823     // If ObjCBinPackProtocolList is unspecified, fall back to BinPackParameters
1824     // for backwards compatibility.
1825     bool ObjCBinPackProtocolList =
1826         (Style.ObjCBinPackProtocolList == FormatStyle::BPS_Auto &&
1827          Style.BinPackParameters) ||
1828         Style.ObjCBinPackProtocolList == FormatStyle::BPS_Always;
1829 
1830     bool BinPackDeclaration =
1831         (State.Line->Type != LT_ObjCDecl && Style.BinPackParameters) ||
1832         (State.Line->Type == LT_ObjCDecl && ObjCBinPackProtocolList);
1833 
1834     bool GenericSelection =
1835         Current.getPreviousNonComment() &&
1836         Current.getPreviousNonComment()->is(tok::kw__Generic);
1837 
1838     AvoidBinPacking =
1839         (CurrentState.IsCSharpGenericTypeConstraint) || GenericSelection ||
1840         (Style.isJavaScript() && EndsInComma) ||
1841         (State.Line->MustBeDeclaration && !BinPackDeclaration) ||
1842         (!State.Line->MustBeDeclaration && !Style.BinPackArguments) ||
1843         (Style.ExperimentalAutoDetectBinPacking &&
1844          (Current.is(PPK_OnePerLine) ||
1845           (!BinPackInconclusiveFunctions && Current.is(PPK_Inconclusive))));
1846 
1847     if (Current.is(TT_ObjCMethodExpr) && Current.MatchingParen &&
1848         Style.ObjCBreakBeforeNestedBlockParam) {
1849       if (Style.ColumnLimit) {
1850         // If this '[' opens an ObjC call, determine whether all parameters fit
1851         // into one line and put one per line if they don't.
1852         if (getLengthToMatchingParen(Current, State.Stack) + State.Column >
1853             getColumnLimit(State)) {
1854           BreakBeforeParameter = true;
1855         }
1856       } else {
1857         // For ColumnLimit = 0, we have to figure out whether there is or has to
1858         // be a line break within this call.
1859         for (const FormatToken *Tok = &Current;
1860              Tok && Tok != Current.MatchingParen; Tok = Tok->Next) {
1861           if (Tok->MustBreakBefore ||
1862               (Tok->CanBreakBefore && Tok->NewlinesBefore > 0)) {
1863             BreakBeforeParameter = true;
1864             break;
1865           }
1866         }
1867       }
1868     }
1869 
1870     if (Style.isJavaScript() && EndsInComma)
1871       BreakBeforeParameter = true;
1872   }
1873   // Generally inherit NoLineBreak from the current scope to nested scope.
1874   // However, don't do this for non-empty nested blocks, dict literals and
1875   // array literals as these follow different indentation rules.
1876   bool NoLineBreak =
1877       Current.Children.empty() &&
1878       !Current.isOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) &&
1879       (CurrentState.NoLineBreak || CurrentState.NoLineBreakInOperand ||
1880        (Current.is(TT_TemplateOpener) &&
1881         CurrentState.ContainsUnwrappedBuilder));
1882   State.Stack.push_back(
1883       ParenState(&Current, NewIndent, LastSpace, AvoidBinPacking, NoLineBreak));
1884   auto &NewState = State.Stack.back();
1885   NewState.NestedBlockIndent = NestedBlockIndent;
1886   NewState.BreakBeforeParameter = BreakBeforeParameter;
1887   NewState.HasMultipleNestedBlocks = (Current.BlockParameterCount > 1);
1888 
1889   if (Style.BraceWrapping.BeforeLambdaBody && Current.Next &&
1890       Current.is(tok::l_paren)) {
1891     // Search for any parameter that is a lambda.
1892     FormatToken const *next = Current.Next;
1893     while (next) {
1894       if (next->is(TT_LambdaLSquare)) {
1895         NewState.HasMultipleNestedBlocks = true;
1896         break;
1897       }
1898       next = next->Next;
1899     }
1900   }
1901 
1902   NewState.IsInsideObjCArrayLiteral = Current.is(TT_ArrayInitializerLSquare) &&
1903                                       Current.Previous &&
1904                                       Current.Previous->is(tok::at);
1905 }
1906 
1907 void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
1908   const FormatToken &Current = *State.NextToken;
1909   if (!Current.closesScope())
1910     return;
1911 
1912   // If we encounter a closing ), ], } or >, we can remove a level from our
1913   // stacks.
1914   if (State.Stack.size() > 1 &&
1915       (Current.isOneOf(tok::r_paren, tok::r_square, TT_TemplateString) ||
1916        (Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
1917        State.NextToken->is(TT_TemplateCloser) ||
1918        (Current.is(tok::greater) && Current.is(TT_DictLiteral)))) {
1919     State.Stack.pop_back();
1920   }
1921 
1922   auto &CurrentState = State.Stack.back();
1923 
1924   // Reevaluate whether ObjC message arguments fit into one line.
1925   // If a receiver spans multiple lines, e.g.:
1926   //   [[object block:^{
1927   //     return 42;
1928   //   }] a:42 b:42];
1929   // BreakBeforeParameter is calculated based on an incorrect assumption
1930   // (it is checked whether the whole expression fits into one line without
1931   // considering a line break inside a message receiver).
1932   // We check whether arguments fit after receiver scope closer (into the same
1933   // line).
1934   if (CurrentState.BreakBeforeParameter && Current.MatchingParen &&
1935       Current.MatchingParen->Previous) {
1936     const FormatToken &CurrentScopeOpener = *Current.MatchingParen->Previous;
1937     if (CurrentScopeOpener.is(TT_ObjCMethodExpr) &&
1938         CurrentScopeOpener.MatchingParen) {
1939       int NecessarySpaceInLine =
1940           getLengthToMatchingParen(CurrentScopeOpener, State.Stack) +
1941           CurrentScopeOpener.TotalLength - Current.TotalLength - 1;
1942       if (State.Column + Current.ColumnWidth + NecessarySpaceInLine <=
1943           Style.ColumnLimit) {
1944         CurrentState.BreakBeforeParameter = false;
1945       }
1946     }
1947   }
1948 
1949   if (Current.is(tok::r_square)) {
1950     // If this ends the array subscript expr, reset the corresponding value.
1951     const FormatToken *NextNonComment = Current.getNextNonComment();
1952     if (NextNonComment && NextNonComment->isNot(tok::l_square))
1953       CurrentState.StartOfArraySubscripts = 0;
1954   }
1955 }
1956 
1957 void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
1958   if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
1959       State.NextToken->is(TT_LambdaLBrace) &&
1960       !State.Line->MightBeFunctionDecl) {
1961     State.Stack.back().NestedBlockIndent = State.FirstIndent;
1962   }
1963   unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
1964   // ObjC block sometimes follow special indentation rules.
1965   unsigned NewIndent =
1966       NestedBlockIndent + (State.NextToken->is(TT_ObjCBlockLBrace)
1967                                ? Style.ObjCBlockIndentWidth
1968                                : Style.IndentWidth);
1969   State.Stack.push_back(ParenState(State.NextToken, NewIndent,
1970                                    State.Stack.back().LastSpace,
1971                                    /*AvoidBinPacking=*/true,
1972                                    /*NoLineBreak=*/false));
1973   State.Stack.back().NestedBlockIndent = NestedBlockIndent;
1974   State.Stack.back().BreakBeforeParameter = true;
1975 }
1976 
1977 static unsigned getLastLineEndColumn(StringRef Text, unsigned StartColumn,
1978                                      unsigned TabWidth,
1979                                      encoding::Encoding Encoding) {
1980   size_t LastNewlinePos = Text.find_last_of("\n");
1981   if (LastNewlinePos == StringRef::npos) {
1982     return StartColumn +
1983            encoding::columnWidthWithTabs(Text, StartColumn, TabWidth, Encoding);
1984   } else {
1985     return encoding::columnWidthWithTabs(Text.substr(LastNewlinePos),
1986                                          /*StartColumn=*/0, TabWidth, Encoding);
1987   }
1988 }
1989 
1990 unsigned ContinuationIndenter::reformatRawStringLiteral(
1991     const FormatToken &Current, LineState &State,
1992     const FormatStyle &RawStringStyle, bool DryRun, bool Newline) {
1993   unsigned StartColumn = State.Column - Current.ColumnWidth;
1994   StringRef OldDelimiter = *getRawStringDelimiter(Current.TokenText);
1995   StringRef NewDelimiter =
1996       getCanonicalRawStringDelimiter(Style, RawStringStyle.Language);
1997   if (NewDelimiter.empty())
1998     NewDelimiter = OldDelimiter;
1999   // The text of a raw string is between the leading 'R"delimiter(' and the
2000   // trailing 'delimiter)"'.
2001   unsigned OldPrefixSize = 3 + OldDelimiter.size();
2002   unsigned OldSuffixSize = 2 + OldDelimiter.size();
2003   // We create a virtual text environment which expects a null-terminated
2004   // string, so we cannot use StringRef.
2005   std::string RawText = std::string(
2006       Current.TokenText.substr(OldPrefixSize).drop_back(OldSuffixSize));
2007   if (NewDelimiter != OldDelimiter) {
2008     // Don't update to the canonical delimiter 'deli' if ')deli"' occurs in the
2009     // raw string.
2010     std::string CanonicalDelimiterSuffix = (")" + NewDelimiter + "\"").str();
2011     if (StringRef(RawText).contains(CanonicalDelimiterSuffix))
2012       NewDelimiter = OldDelimiter;
2013   }
2014 
2015   unsigned NewPrefixSize = 3 + NewDelimiter.size();
2016   unsigned NewSuffixSize = 2 + NewDelimiter.size();
2017 
2018   // The first start column is the column the raw text starts after formatting.
2019   unsigned FirstStartColumn = StartColumn + NewPrefixSize;
2020 
2021   // The next start column is the intended indentation a line break inside
2022   // the raw string at level 0. It is determined by the following rules:
2023   //   - if the content starts on newline, it is one level more than the current
2024   //     indent, and
2025   //   - if the content does not start on a newline, it is the first start
2026   //     column.
2027   // These rules have the advantage that the formatted content both does not
2028   // violate the rectangle rule and visually flows within the surrounding
2029   // source.
2030   bool ContentStartsOnNewline = Current.TokenText[OldPrefixSize] == '\n';
2031   // If this token is the last parameter (checked by looking if it's followed by
2032   // `)` and is not on a newline, the base the indent off the line's nested
2033   // block indent. Otherwise, base the indent off the arguments indent, so we
2034   // can achieve:
2035   //
2036   // fffffffffff(1, 2, 3, R"pb(
2037   //     key1: 1  #
2038   //     key2: 2)pb");
2039   //
2040   // fffffffffff(1, 2, 3,
2041   //             R"pb(
2042   //               key1: 1  #
2043   //               key2: 2
2044   //             )pb");
2045   //
2046   // fffffffffff(1, 2, 3,
2047   //             R"pb(
2048   //               key1: 1  #
2049   //               key2: 2
2050   //             )pb",
2051   //             5);
2052   unsigned CurrentIndent =
2053       (!Newline && Current.Next && Current.Next->is(tok::r_paren))
2054           ? State.Stack.back().NestedBlockIndent
2055           : State.Stack.back().Indent;
2056   unsigned NextStartColumn = ContentStartsOnNewline
2057                                  ? CurrentIndent + Style.IndentWidth
2058                                  : FirstStartColumn;
2059 
2060   // The last start column is the column the raw string suffix starts if it is
2061   // put on a newline.
2062   // The last start column is the intended indentation of the raw string postfix
2063   // if it is put on a newline. It is determined by the following rules:
2064   //   - if the raw string prefix starts on a newline, it is the column where
2065   //     that raw string prefix starts, and
2066   //   - if the raw string prefix does not start on a newline, it is the current
2067   //     indent.
2068   unsigned LastStartColumn =
2069       Current.NewlinesBefore ? FirstStartColumn - NewPrefixSize : CurrentIndent;
2070 
2071   std::pair<tooling::Replacements, unsigned> Fixes = internal::reformat(
2072       RawStringStyle, RawText, {tooling::Range(0, RawText.size())},
2073       FirstStartColumn, NextStartColumn, LastStartColumn, "<stdin>",
2074       /*Status=*/nullptr);
2075 
2076   auto NewCode = applyAllReplacements(RawText, Fixes.first);
2077   tooling::Replacements NoFixes;
2078   if (!NewCode)
2079     return addMultilineToken(Current, State);
2080   if (!DryRun) {
2081     if (NewDelimiter != OldDelimiter) {
2082       // In 'R"delimiter(...', the delimiter starts 2 characters after the start
2083       // of the token.
2084       SourceLocation PrefixDelimiterStart =
2085           Current.Tok.getLocation().getLocWithOffset(2);
2086       auto PrefixErr = Whitespaces.addReplacement(tooling::Replacement(
2087           SourceMgr, PrefixDelimiterStart, OldDelimiter.size(), NewDelimiter));
2088       if (PrefixErr) {
2089         llvm::errs()
2090             << "Failed to update the prefix delimiter of a raw string: "
2091             << llvm::toString(std::move(PrefixErr)) << "\n";
2092       }
2093       // In 'R"delimiter(...)delimiter"', the suffix delimiter starts at
2094       // position length - 1 - |delimiter|.
2095       SourceLocation SuffixDelimiterStart =
2096           Current.Tok.getLocation().getLocWithOffset(Current.TokenText.size() -
2097                                                      1 - OldDelimiter.size());
2098       auto SuffixErr = Whitespaces.addReplacement(tooling::Replacement(
2099           SourceMgr, SuffixDelimiterStart, OldDelimiter.size(), NewDelimiter));
2100       if (SuffixErr) {
2101         llvm::errs()
2102             << "Failed to update the suffix delimiter of a raw string: "
2103             << llvm::toString(std::move(SuffixErr)) << "\n";
2104       }
2105     }
2106     SourceLocation OriginLoc =
2107         Current.Tok.getLocation().getLocWithOffset(OldPrefixSize);
2108     for (const tooling::Replacement &Fix : Fixes.first) {
2109       auto Err = Whitespaces.addReplacement(tooling::Replacement(
2110           SourceMgr, OriginLoc.getLocWithOffset(Fix.getOffset()),
2111           Fix.getLength(), Fix.getReplacementText()));
2112       if (Err) {
2113         llvm::errs() << "Failed to reformat raw string: "
2114                      << llvm::toString(std::move(Err)) << "\n";
2115       }
2116     }
2117   }
2118   unsigned RawLastLineEndColumn = getLastLineEndColumn(
2119       *NewCode, FirstStartColumn, Style.TabWidth, Encoding);
2120   State.Column = RawLastLineEndColumn + NewSuffixSize;
2121   // Since we're updating the column to after the raw string literal here, we
2122   // have to manually add the penalty for the prefix R"delim( over the column
2123   // limit.
2124   unsigned PrefixExcessCharacters =
2125       StartColumn + NewPrefixSize > Style.ColumnLimit
2126           ? StartColumn + NewPrefixSize - Style.ColumnLimit
2127           : 0;
2128   bool IsMultiline =
2129       ContentStartsOnNewline || (NewCode->find('\n') != std::string::npos);
2130   if (IsMultiline) {
2131     // Break before further function parameters on all levels.
2132     for (ParenState &Paren : State.Stack)
2133       Paren.BreakBeforeParameter = true;
2134   }
2135   return Fixes.second + PrefixExcessCharacters * Style.PenaltyExcessCharacter;
2136 }
2137 
2138 unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
2139                                                  LineState &State) {
2140   // Break before further function parameters on all levels.
2141   for (ParenState &Paren : State.Stack)
2142     Paren.BreakBeforeParameter = true;
2143 
2144   unsigned ColumnsUsed = State.Column;
2145   // We can only affect layout of the first and the last line, so the penalty
2146   // for all other lines is constant, and we ignore it.
2147   State.Column = Current.LastLineColumnWidth;
2148 
2149   if (ColumnsUsed > getColumnLimit(State))
2150     return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
2151   return 0;
2152 }
2153 
2154 unsigned ContinuationIndenter::handleEndOfLine(const FormatToken &Current,
2155                                                LineState &State, bool DryRun,
2156                                                bool AllowBreak, bool Newline) {
2157   unsigned Penalty = 0;
2158   // Compute the raw string style to use in case this is a raw string literal
2159   // that can be reformatted.
2160   auto RawStringStyle = getRawStringStyle(Current, State);
2161   if (RawStringStyle && !Current.Finalized) {
2162     Penalty = reformatRawStringLiteral(Current, State, *RawStringStyle, DryRun,
2163                                        Newline);
2164   } else if (Current.IsMultiline && Current.isNot(TT_BlockComment)) {
2165     // Don't break multi-line tokens other than block comments and raw string
2166     // literals. Instead, just update the state.
2167     Penalty = addMultilineToken(Current, State);
2168   } else if (State.Line->Type != LT_ImportStatement) {
2169     // We generally don't break import statements.
2170     LineState OriginalState = State;
2171 
2172     // Whether we force the reflowing algorithm to stay strictly within the
2173     // column limit.
2174     bool Strict = false;
2175     // Whether the first non-strict attempt at reflowing did intentionally
2176     // exceed the column limit.
2177     bool Exceeded = false;
2178     std::tie(Penalty, Exceeded) = breakProtrudingToken(
2179         Current, State, AllowBreak, /*DryRun=*/true, Strict);
2180     if (Exceeded) {
2181       // If non-strict reflowing exceeds the column limit, try whether strict
2182       // reflowing leads to an overall lower penalty.
2183       LineState StrictState = OriginalState;
2184       unsigned StrictPenalty =
2185           breakProtrudingToken(Current, StrictState, AllowBreak,
2186                                /*DryRun=*/true, /*Strict=*/true)
2187               .first;
2188       Strict = StrictPenalty <= Penalty;
2189       if (Strict) {
2190         Penalty = StrictPenalty;
2191         State = StrictState;
2192       }
2193     }
2194     if (!DryRun) {
2195       // If we're not in dry-run mode, apply the changes with the decision on
2196       // strictness made above.
2197       breakProtrudingToken(Current, OriginalState, AllowBreak, /*DryRun=*/false,
2198                            Strict);
2199     }
2200   }
2201   if (State.Column > getColumnLimit(State)) {
2202     unsigned ExcessCharacters = State.Column - getColumnLimit(State);
2203     Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
2204   }
2205   return Penalty;
2206 }
2207 
2208 // Returns the enclosing function name of a token, or the empty string if not
2209 // found.
2210 static StringRef getEnclosingFunctionName(const FormatToken &Current) {
2211   // Look for: 'function(' or 'function<templates>(' before Current.
2212   auto Tok = Current.getPreviousNonComment();
2213   if (!Tok || Tok->isNot(tok::l_paren))
2214     return "";
2215   Tok = Tok->getPreviousNonComment();
2216   if (!Tok)
2217     return "";
2218   if (Tok->is(TT_TemplateCloser)) {
2219     Tok = Tok->MatchingParen;
2220     if (Tok)
2221       Tok = Tok->getPreviousNonComment();
2222   }
2223   if (!Tok || Tok->isNot(tok::identifier))
2224     return "";
2225   return Tok->TokenText;
2226 }
2227 
2228 std::optional<FormatStyle>
2229 ContinuationIndenter::getRawStringStyle(const FormatToken &Current,
2230                                         const LineState &State) {
2231   if (!Current.isStringLiteral())
2232     return std::nullopt;
2233   auto Delimiter = getRawStringDelimiter(Current.TokenText);
2234   if (!Delimiter)
2235     return std::nullopt;
2236   auto RawStringStyle = RawStringFormats.getDelimiterStyle(*Delimiter);
2237   if (!RawStringStyle && Delimiter->empty()) {
2238     RawStringStyle = RawStringFormats.getEnclosingFunctionStyle(
2239         getEnclosingFunctionName(Current));
2240   }
2241   if (!RawStringStyle)
2242     return std::nullopt;
2243   RawStringStyle->ColumnLimit = getColumnLimit(State);
2244   return RawStringStyle;
2245 }
2246 
2247 std::unique_ptr<BreakableToken>
2248 ContinuationIndenter::createBreakableToken(const FormatToken &Current,
2249                                            LineState &State, bool AllowBreak) {
2250   unsigned StartColumn = State.Column - Current.ColumnWidth;
2251   if (Current.isStringLiteral()) {
2252     // Strings in JSON cannot be broken. Breaking strings in JavaScript is
2253     // disabled for now.
2254     if (Style.isJson() || Style.isJavaScript() || !Style.BreakStringLiterals ||
2255         !AllowBreak) {
2256       return nullptr;
2257     }
2258 
2259     // Don't break string literals inside preprocessor directives (except for
2260     // #define directives, as their contents are stored in separate lines and
2261     // are not affected by this check).
2262     // This way we avoid breaking code with line directives and unknown
2263     // preprocessor directives that contain long string literals.
2264     if (State.Line->Type == LT_PreprocessorDirective)
2265       return nullptr;
2266     // Exempts unterminated string literals from line breaking. The user will
2267     // likely want to terminate the string before any line breaking is done.
2268     if (Current.IsUnterminatedLiteral)
2269       return nullptr;
2270     // Don't break string literals inside Objective-C array literals (doing so
2271     // raises the warning -Wobjc-string-concatenation).
2272     if (State.Stack.back().IsInsideObjCArrayLiteral)
2273       return nullptr;
2274 
2275     // The "DPI"/"DPI-C" in SystemVerilog direct programming interface
2276     // imports/exports cannot be split, e.g.
2277     // `import "DPI" function foo();`
2278     // FIXME: make this use same infra as C++ import checks
2279     if (Style.isVerilog() && Current.Previous &&
2280         Current.Previous->isOneOf(tok::kw_export, Keywords.kw_import)) {
2281       return nullptr;
2282     }
2283     StringRef Text = Current.TokenText;
2284 
2285     // We need this to address the case where there is an unbreakable tail only
2286     // if certain other formatting decisions have been taken. The
2287     // UnbreakableTailLength of Current is an overapproximation in that case and
2288     // we need to be correct here.
2289     unsigned UnbreakableTailLength = (State.NextToken && canBreak(State))
2290                                          ? 0
2291                                          : Current.UnbreakableTailLength;
2292 
2293     if (Style.isVerilog() || Style.Language == FormatStyle::LK_Java ||
2294         Style.isJavaScript() || Style.isCSharp()) {
2295       BreakableStringLiteralUsingOperators::QuoteStyleType QuoteStyle;
2296       if (Style.isJavaScript() && Text.starts_with("'") &&
2297           Text.ends_with("'")) {
2298         QuoteStyle = BreakableStringLiteralUsingOperators::SingleQuotes;
2299       } else if (Style.isCSharp() && Text.starts_with("@\"") &&
2300                  Text.ends_with("\"")) {
2301         QuoteStyle = BreakableStringLiteralUsingOperators::AtDoubleQuotes;
2302       } else if (Text.starts_with("\"") && Text.ends_with("\"")) {
2303         QuoteStyle = BreakableStringLiteralUsingOperators::DoubleQuotes;
2304       } else {
2305         return nullptr;
2306       }
2307       return std::make_unique<BreakableStringLiteralUsingOperators>(
2308           Current, QuoteStyle,
2309           /*UnindentPlus=*/shouldUnindentNextOperator(Current), StartColumn,
2310           UnbreakableTailLength, State.Line->InPPDirective, Encoding, Style);
2311     }
2312 
2313     StringRef Prefix;
2314     StringRef Postfix;
2315     // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
2316     // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
2317     // reduce the overhead) for each FormatToken, which is a string, so that we
2318     // don't run multiple checks here on the hot path.
2319     if ((Text.ends_with(Postfix = "\"") &&
2320          (Text.starts_with(Prefix = "@\"") || Text.starts_with(Prefix = "\"") ||
2321           Text.starts_with(Prefix = "u\"") ||
2322           Text.starts_with(Prefix = "U\"") ||
2323           Text.starts_with(Prefix = "u8\"") ||
2324           Text.starts_with(Prefix = "L\""))) ||
2325         (Text.starts_with(Prefix = "_T(\"") &&
2326          Text.ends_with(Postfix = "\")"))) {
2327       return std::make_unique<BreakableStringLiteral>(
2328           Current, StartColumn, Prefix, Postfix, UnbreakableTailLength,
2329           State.Line->InPPDirective, Encoding, Style);
2330     }
2331   } else if (Current.is(TT_BlockComment)) {
2332     if (!Style.ReflowComments ||
2333         // If a comment token switches formatting, like
2334         // /* clang-format on */, we don't want to break it further,
2335         // but we may still want to adjust its indentation.
2336         switchesFormatting(Current)) {
2337       return nullptr;
2338     }
2339     return std::make_unique<BreakableBlockComment>(
2340         Current, StartColumn, Current.OriginalColumn, !Current.Previous,
2341         State.Line->InPPDirective, Encoding, Style, Whitespaces.useCRLF());
2342   } else if (Current.is(TT_LineComment) &&
2343              (!Current.Previous ||
2344               Current.Previous->isNot(TT_ImplicitStringLiteral))) {
2345     bool RegularComments = [&]() {
2346       for (const FormatToken *T = &Current; T && T->is(TT_LineComment);
2347            T = T->Next) {
2348         if (!(T->TokenText.starts_with("//") || T->TokenText.starts_with("#")))
2349           return false;
2350       }
2351       return true;
2352     }();
2353     if (!Style.ReflowComments ||
2354         CommentPragmasRegex.match(Current.TokenText.substr(2)) ||
2355         switchesFormatting(Current) || !RegularComments) {
2356       return nullptr;
2357     }
2358     return std::make_unique<BreakableLineCommentSection>(
2359         Current, StartColumn, /*InPPDirective=*/false, Encoding, Style);
2360   }
2361   return nullptr;
2362 }
2363 
2364 std::pair<unsigned, bool>
2365 ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
2366                                            LineState &State, bool AllowBreak,
2367                                            bool DryRun, bool Strict) {
2368   std::unique_ptr<const BreakableToken> Token =
2369       createBreakableToken(Current, State, AllowBreak);
2370   if (!Token)
2371     return {0, false};
2372   assert(Token->getLineCount() > 0);
2373   unsigned ColumnLimit = getColumnLimit(State);
2374   if (Current.is(TT_LineComment)) {
2375     // We don't insert backslashes when breaking line comments.
2376     ColumnLimit = Style.ColumnLimit;
2377   }
2378   if (ColumnLimit == 0) {
2379     // To make the rest of the function easier set the column limit to the
2380     // maximum, if there should be no limit.
2381     ColumnLimit = std::numeric_limits<decltype(ColumnLimit)>::max();
2382   }
2383   if (Current.UnbreakableTailLength >= ColumnLimit)
2384     return {0, false};
2385   // ColumnWidth was already accounted into State.Column before calling
2386   // breakProtrudingToken.
2387   unsigned StartColumn = State.Column - Current.ColumnWidth;
2388   unsigned NewBreakPenalty = Current.isStringLiteral()
2389                                  ? Style.PenaltyBreakString
2390                                  : Style.PenaltyBreakComment;
2391   // Stores whether we intentionally decide to let a line exceed the column
2392   // limit.
2393   bool Exceeded = false;
2394   // Stores whether we introduce a break anywhere in the token.
2395   bool BreakInserted = Token->introducesBreakBeforeToken();
2396   // Store whether we inserted a new line break at the end of the previous
2397   // logical line.
2398   bool NewBreakBefore = false;
2399   // We use a conservative reflowing strategy. Reflow starts after a line is
2400   // broken or the corresponding whitespace compressed. Reflow ends as soon as a
2401   // line that doesn't get reflown with the previous line is reached.
2402   bool Reflow = false;
2403   // Keep track of where we are in the token:
2404   // Where we are in the content of the current logical line.
2405   unsigned TailOffset = 0;
2406   // The column number we're currently at.
2407   unsigned ContentStartColumn =
2408       Token->getContentStartColumn(0, /*Break=*/false);
2409   // The number of columns left in the current logical line after TailOffset.
2410   unsigned RemainingTokenColumns =
2411       Token->getRemainingLength(0, TailOffset, ContentStartColumn);
2412   // Adapt the start of the token, for example indent.
2413   if (!DryRun)
2414     Token->adaptStartOfLine(0, Whitespaces);
2415 
2416   unsigned ContentIndent = 0;
2417   unsigned Penalty = 0;
2418   LLVM_DEBUG(llvm::dbgs() << "Breaking protruding token at column "
2419                           << StartColumn << ".\n");
2420   for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
2421        LineIndex != EndIndex; ++LineIndex) {
2422     LLVM_DEBUG(llvm::dbgs()
2423                << "  Line: " << LineIndex << " (Reflow: " << Reflow << ")\n");
2424     NewBreakBefore = false;
2425     // If we did reflow the previous line, we'll try reflowing again. Otherwise
2426     // we'll start reflowing if the current line is broken or whitespace is
2427     // compressed.
2428     bool TryReflow = Reflow;
2429     // Break the current token until we can fit the rest of the line.
2430     while (ContentStartColumn + RemainingTokenColumns > ColumnLimit) {
2431       LLVM_DEBUG(llvm::dbgs() << "    Over limit, need: "
2432                               << (ContentStartColumn + RemainingTokenColumns)
2433                               << ", space: " << ColumnLimit
2434                               << ", reflown prefix: " << ContentStartColumn
2435                               << ", offset in line: " << TailOffset << "\n");
2436       // If the current token doesn't fit, find the latest possible split in the
2437       // current line so that breaking at it will be under the column limit.
2438       // FIXME: Use the earliest possible split while reflowing to correctly
2439       // compress whitespace within a line.
2440       BreakableToken::Split Split =
2441           Token->getSplit(LineIndex, TailOffset, ColumnLimit,
2442                           ContentStartColumn, CommentPragmasRegex);
2443       if (Split.first == StringRef::npos) {
2444         // No break opportunity - update the penalty and continue with the next
2445         // logical line.
2446         if (LineIndex < EndIndex - 1) {
2447           // The last line's penalty is handled in addNextStateToQueue() or when
2448           // calling replaceWhitespaceAfterLastLine below.
2449           Penalty += Style.PenaltyExcessCharacter *
2450                      (ContentStartColumn + RemainingTokenColumns - ColumnLimit);
2451         }
2452         LLVM_DEBUG(llvm::dbgs() << "    No break opportunity.\n");
2453         break;
2454       }
2455       assert(Split.first != 0);
2456 
2457       if (Token->supportsReflow()) {
2458         // Check whether the next natural split point after the current one can
2459         // still fit the line, either because we can compress away whitespace,
2460         // or because the penalty the excess characters introduce is lower than
2461         // the break penalty.
2462         // We only do this for tokens that support reflowing, and thus allow us
2463         // to change the whitespace arbitrarily (e.g. comments).
2464         // Other tokens, like string literals, can be broken on arbitrary
2465         // positions.
2466 
2467         // First, compute the columns from TailOffset to the next possible split
2468         // position.
2469         // For example:
2470         // ColumnLimit:     |
2471         // // Some text   that    breaks
2472         //    ^ tail offset
2473         //             ^-- split
2474         //    ^-------- to split columns
2475         //                    ^--- next split
2476         //    ^--------------- to next split columns
2477         unsigned ToSplitColumns = Token->getRangeLength(
2478             LineIndex, TailOffset, Split.first, ContentStartColumn);
2479         LLVM_DEBUG(llvm::dbgs() << "    ToSplit: " << ToSplitColumns << "\n");
2480 
2481         BreakableToken::Split NextSplit = Token->getSplit(
2482             LineIndex, TailOffset + Split.first + Split.second, ColumnLimit,
2483             ContentStartColumn + ToSplitColumns + 1, CommentPragmasRegex);
2484         // Compute the columns necessary to fit the next non-breakable sequence
2485         // into the current line.
2486         unsigned ToNextSplitColumns = 0;
2487         if (NextSplit.first == StringRef::npos) {
2488           ToNextSplitColumns = Token->getRemainingLength(LineIndex, TailOffset,
2489                                                          ContentStartColumn);
2490         } else {
2491           ToNextSplitColumns = Token->getRangeLength(
2492               LineIndex, TailOffset,
2493               Split.first + Split.second + NextSplit.first, ContentStartColumn);
2494         }
2495         // Compress the whitespace between the break and the start of the next
2496         // unbreakable sequence.
2497         ToNextSplitColumns =
2498             Token->getLengthAfterCompression(ToNextSplitColumns, Split);
2499         LLVM_DEBUG(llvm::dbgs()
2500                    << "    ContentStartColumn: " << ContentStartColumn << "\n");
2501         LLVM_DEBUG(llvm::dbgs()
2502                    << "    ToNextSplit: " << ToNextSplitColumns << "\n");
2503         // If the whitespace compression makes us fit, continue on the current
2504         // line.
2505         bool ContinueOnLine =
2506             ContentStartColumn + ToNextSplitColumns <= ColumnLimit;
2507         unsigned ExcessCharactersPenalty = 0;
2508         if (!ContinueOnLine && !Strict) {
2509           // Similarly, if the excess characters' penalty is lower than the
2510           // penalty of introducing a new break, continue on the current line.
2511           ExcessCharactersPenalty =
2512               (ContentStartColumn + ToNextSplitColumns - ColumnLimit) *
2513               Style.PenaltyExcessCharacter;
2514           LLVM_DEBUG(llvm::dbgs()
2515                      << "    Penalty excess: " << ExcessCharactersPenalty
2516                      << "\n            break : " << NewBreakPenalty << "\n");
2517           if (ExcessCharactersPenalty < NewBreakPenalty) {
2518             Exceeded = true;
2519             ContinueOnLine = true;
2520           }
2521         }
2522         if (ContinueOnLine) {
2523           LLVM_DEBUG(llvm::dbgs() << "    Continuing on line...\n");
2524           // The current line fits after compressing the whitespace - reflow
2525           // the next line into it if possible.
2526           TryReflow = true;
2527           if (!DryRun) {
2528             Token->compressWhitespace(LineIndex, TailOffset, Split,
2529                                       Whitespaces);
2530           }
2531           // When we continue on the same line, leave one space between content.
2532           ContentStartColumn += ToSplitColumns + 1;
2533           Penalty += ExcessCharactersPenalty;
2534           TailOffset += Split.first + Split.second;
2535           RemainingTokenColumns = Token->getRemainingLength(
2536               LineIndex, TailOffset, ContentStartColumn);
2537           continue;
2538         }
2539       }
2540       LLVM_DEBUG(llvm::dbgs() << "    Breaking...\n");
2541       // Update the ContentIndent only if the current line was not reflown with
2542       // the previous line, since in that case the previous line should still
2543       // determine the ContentIndent. Also never intent the last line.
2544       if (!Reflow)
2545         ContentIndent = Token->getContentIndent(LineIndex);
2546       LLVM_DEBUG(llvm::dbgs()
2547                  << "    ContentIndent: " << ContentIndent << "\n");
2548       ContentStartColumn = ContentIndent + Token->getContentStartColumn(
2549                                                LineIndex, /*Break=*/true);
2550 
2551       unsigned NewRemainingTokenColumns = Token->getRemainingLength(
2552           LineIndex, TailOffset + Split.first + Split.second,
2553           ContentStartColumn);
2554       if (NewRemainingTokenColumns == 0) {
2555         // No content to indent.
2556         ContentIndent = 0;
2557         ContentStartColumn =
2558             Token->getContentStartColumn(LineIndex, /*Break=*/true);
2559         NewRemainingTokenColumns = Token->getRemainingLength(
2560             LineIndex, TailOffset + Split.first + Split.second,
2561             ContentStartColumn);
2562       }
2563 
2564       // When breaking before a tab character, it may be moved by a few columns,
2565       // but will still be expanded to the next tab stop, so we don't save any
2566       // columns.
2567       if (NewRemainingTokenColumns >= RemainingTokenColumns) {
2568         // FIXME: Do we need to adjust the penalty?
2569         break;
2570       }
2571 
2572       LLVM_DEBUG(llvm::dbgs() << "    Breaking at: " << TailOffset + Split.first
2573                               << ", " << Split.second << "\n");
2574       if (!DryRun) {
2575         Token->insertBreak(LineIndex, TailOffset, Split, ContentIndent,
2576                            Whitespaces);
2577       }
2578 
2579       Penalty += NewBreakPenalty;
2580       TailOffset += Split.first + Split.second;
2581       RemainingTokenColumns = NewRemainingTokenColumns;
2582       BreakInserted = true;
2583       NewBreakBefore = true;
2584     }
2585     // In case there's another line, prepare the state for the start of the next
2586     // line.
2587     if (LineIndex + 1 != EndIndex) {
2588       unsigned NextLineIndex = LineIndex + 1;
2589       if (NewBreakBefore) {
2590         // After breaking a line, try to reflow the next line into the current
2591         // one once RemainingTokenColumns fits.
2592         TryReflow = true;
2593       }
2594       if (TryReflow) {
2595         // We decided that we want to try reflowing the next line into the
2596         // current one.
2597         // We will now adjust the state as if the reflow is successful (in
2598         // preparation for the next line), and see whether that works. If we
2599         // decide that we cannot reflow, we will later reset the state to the
2600         // start of the next line.
2601         Reflow = false;
2602         // As we did not continue breaking the line, RemainingTokenColumns is
2603         // known to fit after ContentStartColumn. Adapt ContentStartColumn to
2604         // the position at which we want to format the next line if we do
2605         // actually reflow.
2606         // When we reflow, we need to add a space between the end of the current
2607         // line and the next line's start column.
2608         ContentStartColumn += RemainingTokenColumns + 1;
2609         // Get the split that we need to reflow next logical line into the end
2610         // of the current one; the split will include any leading whitespace of
2611         // the next logical line.
2612         BreakableToken::Split SplitBeforeNext =
2613             Token->getReflowSplit(NextLineIndex, CommentPragmasRegex);
2614         LLVM_DEBUG(llvm::dbgs()
2615                    << "    Size of reflown text: " << ContentStartColumn
2616                    << "\n    Potential reflow split: ");
2617         if (SplitBeforeNext.first != StringRef::npos) {
2618           LLVM_DEBUG(llvm::dbgs() << SplitBeforeNext.first << ", "
2619                                   << SplitBeforeNext.second << "\n");
2620           TailOffset = SplitBeforeNext.first + SplitBeforeNext.second;
2621           // If the rest of the next line fits into the current line below the
2622           // column limit, we can safely reflow.
2623           RemainingTokenColumns = Token->getRemainingLength(
2624               NextLineIndex, TailOffset, ContentStartColumn);
2625           Reflow = true;
2626           if (ContentStartColumn + RemainingTokenColumns > ColumnLimit) {
2627             LLVM_DEBUG(llvm::dbgs()
2628                        << "    Over limit after reflow, need: "
2629                        << (ContentStartColumn + RemainingTokenColumns)
2630                        << ", space: " << ColumnLimit
2631                        << ", reflown prefix: " << ContentStartColumn
2632                        << ", offset in line: " << TailOffset << "\n");
2633             // If the whole next line does not fit, try to find a point in
2634             // the next line at which we can break so that attaching the part
2635             // of the next line to that break point onto the current line is
2636             // below the column limit.
2637             BreakableToken::Split Split =
2638                 Token->getSplit(NextLineIndex, TailOffset, ColumnLimit,
2639                                 ContentStartColumn, CommentPragmasRegex);
2640             if (Split.first == StringRef::npos) {
2641               LLVM_DEBUG(llvm::dbgs() << "    Did not find later break\n");
2642               Reflow = false;
2643             } else {
2644               // Check whether the first split point gets us below the column
2645               // limit. Note that we will execute this split below as part of
2646               // the normal token breaking and reflow logic within the line.
2647               unsigned ToSplitColumns = Token->getRangeLength(
2648                   NextLineIndex, TailOffset, Split.first, ContentStartColumn);
2649               if (ContentStartColumn + ToSplitColumns > ColumnLimit) {
2650                 LLVM_DEBUG(llvm::dbgs() << "    Next split protrudes, need: "
2651                                         << (ContentStartColumn + ToSplitColumns)
2652                                         << ", space: " << ColumnLimit);
2653                 unsigned ExcessCharactersPenalty =
2654                     (ContentStartColumn + ToSplitColumns - ColumnLimit) *
2655                     Style.PenaltyExcessCharacter;
2656                 if (NewBreakPenalty < ExcessCharactersPenalty)
2657                   Reflow = false;
2658               }
2659             }
2660           }
2661         } else {
2662           LLVM_DEBUG(llvm::dbgs() << "not found.\n");
2663         }
2664       }
2665       if (!Reflow) {
2666         // If we didn't reflow into the next line, the only space to consider is
2667         // the next logical line. Reset our state to match the start of the next
2668         // line.
2669         TailOffset = 0;
2670         ContentStartColumn =
2671             Token->getContentStartColumn(NextLineIndex, /*Break=*/false);
2672         RemainingTokenColumns = Token->getRemainingLength(
2673             NextLineIndex, TailOffset, ContentStartColumn);
2674         // Adapt the start of the token, for example indent.
2675         if (!DryRun)
2676           Token->adaptStartOfLine(NextLineIndex, Whitespaces);
2677       } else {
2678         // If we found a reflow split and have added a new break before the next
2679         // line, we are going to remove the line break at the start of the next
2680         // logical line. For example, here we'll add a new line break after
2681         // 'text', and subsequently delete the line break between 'that' and
2682         // 'reflows'.
2683         //   // some text that
2684         //   // reflows
2685         // ->
2686         //   // some text
2687         //   // that reflows
2688         // When adding the line break, we also added the penalty for it, so we
2689         // need to subtract that penalty again when we remove the line break due
2690         // to reflowing.
2691         if (NewBreakBefore) {
2692           assert(Penalty >= NewBreakPenalty);
2693           Penalty -= NewBreakPenalty;
2694         }
2695         if (!DryRun)
2696           Token->reflow(NextLineIndex, Whitespaces);
2697       }
2698     }
2699   }
2700 
2701   BreakableToken::Split SplitAfterLastLine =
2702       Token->getSplitAfterLastLine(TailOffset);
2703   if (SplitAfterLastLine.first != StringRef::npos) {
2704     LLVM_DEBUG(llvm::dbgs() << "Replacing whitespace after last line.\n");
2705 
2706     // We add the last line's penalty here, since that line is going to be split
2707     // now.
2708     Penalty += Style.PenaltyExcessCharacter *
2709                (ContentStartColumn + RemainingTokenColumns - ColumnLimit);
2710 
2711     if (!DryRun) {
2712       Token->replaceWhitespaceAfterLastLine(TailOffset, SplitAfterLastLine,
2713                                             Whitespaces);
2714     }
2715     ContentStartColumn =
2716         Token->getContentStartColumn(Token->getLineCount() - 1, /*Break=*/true);
2717     RemainingTokenColumns = Token->getRemainingLength(
2718         Token->getLineCount() - 1,
2719         TailOffset + SplitAfterLastLine.first + SplitAfterLastLine.second,
2720         ContentStartColumn);
2721   }
2722 
2723   State.Column = ContentStartColumn + RemainingTokenColumns -
2724                  Current.UnbreakableTailLength;
2725 
2726   if (BreakInserted) {
2727     if (!DryRun)
2728       Token->updateAfterBroken(Whitespaces);
2729 
2730     // If we break the token inside a parameter list, we need to break before
2731     // the next parameter on all levels, so that the next parameter is clearly
2732     // visible. Line comments already introduce a break.
2733     if (Current.isNot(TT_LineComment))
2734       for (ParenState &Paren : State.Stack)
2735         Paren.BreakBeforeParameter = true;
2736 
2737     if (Current.is(TT_BlockComment))
2738       State.NoContinuation = true;
2739 
2740     State.Stack.back().LastSpace = StartColumn;
2741   }
2742 
2743   Token->updateNextToken(State);
2744 
2745   return {Penalty, Exceeded};
2746 }
2747 
2748 unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
2749   // In preprocessor directives reserve two chars for trailing " \".
2750   return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
2751 }
2752 
2753 bool ContinuationIndenter::nextIsMultilineString(const LineState &State) {
2754   const FormatToken &Current = *State.NextToken;
2755   if (!Current.isStringLiteral() || Current.is(TT_ImplicitStringLiteral))
2756     return false;
2757   // We never consider raw string literals "multiline" for the purpose of
2758   // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased
2759   // (see TokenAnnotator::mustBreakBefore().
2760   if (Current.TokenText.starts_with("R\""))
2761     return false;
2762   if (Current.IsMultiline)
2763     return true;
2764   if (Current.getNextNonComment() &&
2765       Current.getNextNonComment()->isStringLiteral()) {
2766     return true; // Implicit concatenation.
2767   }
2768   if (Style.ColumnLimit != 0 && Style.BreakStringLiterals &&
2769       State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
2770           Style.ColumnLimit) {
2771     return true; // String will be split.
2772   }
2773   return false;
2774 }
2775 
2776 } // namespace format
2777 } // namespace clang
2778