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