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