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