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