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