1 //===--- ContinuationIndenter.cpp - Format C++ code -----------------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements the continuation indenter.
12 //===----------------------------------------------------------------------===//
14 #include "ContinuationIndenter.h"
15 #include "BreakableToken.h"
16 #include "FormatInternal.h"
17 #include "FormatToken.h"
18 #include "WhitespaceManager.h"
19 #include "clang/Basic/OperatorPrecedence.h"
20 #include "clang/Basic/SourceManager.h"
21 #include "clang/Basic/TokenKinds.h"
22 #include "clang/Format/Format.h"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/Support/Debug.h"
27 #define DEBUG_TYPE "format-indenter"
32 // Returns true if a TT_SelectorName should be indented when wrapped,
34 static bool shouldIndentWrappedSelectorName(const FormatStyle
&Style
,
36 return Style
.IndentWrappedFunctionNames
|| LineType
== LT_ObjCMethodDecl
;
39 // Returns true if a binary operator following \p Tok should be unindented when
40 // the style permits it.
41 static bool shouldUnindentNextOperator(const FormatToken
&Tok
) {
42 const FormatToken
*Previous
= Tok
.getPreviousNonComment();
43 return Previous
&& (Previous
->getPrecedence() == prec::Assignment
||
44 Previous
->isOneOf(tok::kw_return
, TT_RequiresClause
));
47 // Returns the length of everything up to the first possible line break after
48 // the ), ], } or > matching \c Tok.
49 static unsigned getLengthToMatchingParen(const FormatToken
&Tok
,
50 ArrayRef
<ParenState
> Stack
) {
51 // Normally whether or not a break before T is possible is calculated and
52 // stored in T.CanBreakBefore. Braces, array initializers and text proto
53 // messages like `key: < ... >` are an exception: a break is possible
54 // before a closing brace R if a break was inserted after the corresponding
55 // opening brace. The information about whether or not a break is needed
56 // before a closing brace R is stored in the ParenState field
57 // S.BreakBeforeClosingBrace where S is the state that R closes.
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.
64 // For example, consider:
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}}
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
)
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;
103 for (; End
->Next
; End
= End
->Next
) {
104 if (End
->Next
->CanBreakBefore
)
106 if (!End
->Next
->closesScope())
108 if (End
->Next
->MatchingParen
&&
109 End
->Next
->MatchingParen
->isOneOf(
110 tok::l_brace
, TT_ArrayInitializerLSquare
, tok::less
)) {
111 const ParenState
*State
= FindParenState(End
->Next
->MatchingParen
);
112 if (State
&& State
->BreakBeforeClosingBrace
)
116 return End
->TotalLength
- Tok
.TotalLength
+ 1;
119 static unsigned getLengthToNextOperator(const FormatToken
&Tok
) {
120 if (!Tok
.NextOperator
)
122 return Tok
.NextOperator
->TotalLength
- Tok
.TotalLength
;
125 // Returns \c true if \c Tok is the "." or "->" of a call and starts the next
126 // segment of a builder type call.
127 static bool startsSegmentOfBuilderTypeCall(const FormatToken
&Tok
) {
128 return Tok
.isMemberAccess() && Tok
.Previous
&& Tok
.Previous
->closesScope();
131 // Returns \c true if \c Token in an alignable binary operator
132 static bool isAlignableBinaryOperator(const FormatToken
&Token
) {
133 // No need to align binary operators that only have two operands.
134 bool HasTwoOperands
= Token
.OperatorIndex
== 0 && !Token
.NextOperator
;
135 return Token
.is(TT_BinaryOperator
) && !HasTwoOperands
&&
136 Token
.getPrecedence() > prec::Conditional
&&
137 Token
.getPrecedence() < prec::PointerToMember
;
140 // Returns \c true if \c Current starts the next operand in a binary operation.
141 static bool startsNextOperand(const FormatToken
&Current
) {
142 assert(Current
.Previous
);
143 const auto &Previous
= *Current
.Previous
;
144 return isAlignableBinaryOperator(Previous
) && !Current
.isTrailingComment();
147 // Returns \c true if \c Current is a binary operation that must break.
148 static bool mustBreakBinaryOperation(const FormatToken
&Current
,
149 const FormatStyle
&Style
) {
150 return Style
.BreakBinaryOperations
!= FormatStyle::BBO_Never
&&
151 (Style
.BreakBeforeBinaryOperators
== FormatStyle::BOS_None
153 : isAlignableBinaryOperator
)(Current
);
156 static bool opensProtoMessageField(const FormatToken
&LessTok
,
157 const FormatStyle
&Style
) {
158 if (LessTok
.isNot(tok::less
))
160 return Style
.Language
== FormatStyle::LK_TextProto
||
161 (Style
.Language
== FormatStyle::LK_Proto
&&
162 (LessTok
.NestingLevel
> 0 ||
163 (LessTok
.Previous
&& LessTok
.Previous
->is(tok::equal
))));
166 // Returns the delimiter of a raw string literal, or std::nullopt if TokenText
167 // is not the text of a raw string literal. The delimiter could be the empty
168 // string. For example, the delimiter of R"deli(cont)deli" is deli.
169 static std::optional
<StringRef
> getRawStringDelimiter(StringRef TokenText
) {
170 if (TokenText
.size() < 5 // The smallest raw string possible is 'R"()"'.
171 || !TokenText
.starts_with("R\"") || !TokenText
.ends_with("\"")) {
175 // A raw string starts with 'R"<delimiter>(' and delimiter is ascii and has
176 // size at most 16 by the standard, so the first '(' must be among the first
178 size_t LParenPos
= TokenText
.substr(0, 19).find_first_of('(');
179 if (LParenPos
== StringRef::npos
)
181 StringRef Delimiter
= TokenText
.substr(2, LParenPos
- 2);
183 // Check that the string ends in ')Delimiter"'.
184 size_t RParenPos
= TokenText
.size() - Delimiter
.size() - 2;
185 if (TokenText
[RParenPos
] != ')')
187 if (!TokenText
.substr(RParenPos
+ 1).starts_with(Delimiter
))
192 // Returns the canonical delimiter for \p Language, or the empty string if no
193 // canonical delimiter is specified.
195 getCanonicalRawStringDelimiter(const FormatStyle
&Style
,
196 FormatStyle::LanguageKind Language
) {
197 for (const auto &Format
: Style
.RawStringFormats
)
198 if (Format
.Language
== Language
)
199 return StringRef(Format
.CanonicalDelimiter
);
203 RawStringFormatStyleManager::RawStringFormatStyleManager(
204 const FormatStyle
&CodeStyle
) {
205 for (const auto &RawStringFormat
: CodeStyle
.RawStringFormats
) {
206 std::optional
<FormatStyle
> LanguageStyle
=
207 CodeStyle
.GetLanguageStyle(RawStringFormat
.Language
);
208 if (!LanguageStyle
) {
209 FormatStyle PredefinedStyle
;
210 if (!getPredefinedStyle(RawStringFormat
.BasedOnStyle
,
211 RawStringFormat
.Language
, &PredefinedStyle
)) {
212 PredefinedStyle
= getLLVMStyle();
213 PredefinedStyle
.Language
= RawStringFormat
.Language
;
215 LanguageStyle
= PredefinedStyle
;
217 LanguageStyle
->ColumnLimit
= CodeStyle
.ColumnLimit
;
218 for (StringRef Delimiter
: RawStringFormat
.Delimiters
)
219 DelimiterStyle
.insert({Delimiter
, *LanguageStyle
});
220 for (StringRef EnclosingFunction
: RawStringFormat
.EnclosingFunctions
)
221 EnclosingFunctionStyle
.insert({EnclosingFunction
, *LanguageStyle
});
225 std::optional
<FormatStyle
>
226 RawStringFormatStyleManager::getDelimiterStyle(StringRef Delimiter
) const {
227 auto It
= DelimiterStyle
.find(Delimiter
);
228 if (It
== DelimiterStyle
.end())
233 std::optional
<FormatStyle
>
234 RawStringFormatStyleManager::getEnclosingFunctionStyle(
235 StringRef EnclosingFunction
) const {
236 auto It
= EnclosingFunctionStyle
.find(EnclosingFunction
);
237 if (It
== EnclosingFunctionStyle
.end())
242 ContinuationIndenter::ContinuationIndenter(const FormatStyle
&Style
,
243 const AdditionalKeywords
&Keywords
,
244 const SourceManager
&SourceMgr
,
245 WhitespaceManager
&Whitespaces
,
246 encoding::Encoding Encoding
,
247 bool BinPackInconclusiveFunctions
)
248 : Style(Style
), Keywords(Keywords
), SourceMgr(SourceMgr
),
249 Whitespaces(Whitespaces
), Encoding(Encoding
),
250 BinPackInconclusiveFunctions(BinPackInconclusiveFunctions
),
251 CommentPragmasRegex(Style
.CommentPragmas
), RawStringFormats(Style
) {}
253 LineState
ContinuationIndenter::getInitialState(unsigned FirstIndent
,
254 unsigned FirstStartColumn
,
255 const AnnotatedLine
*Line
,
258 State
.FirstIndent
= FirstIndent
;
259 if (FirstStartColumn
&& Line
->First
->NewlinesBefore
== 0)
260 State
.Column
= FirstStartColumn
;
262 State
.Column
= FirstIndent
;
263 // With preprocessor directive indentation, the line starts on column 0
264 // since it's indented after the hash, but FirstIndent is set to the
265 // preprocessor indent.
266 if (Style
.IndentPPDirectives
== FormatStyle::PPDIS_AfterHash
&&
267 (Line
->Type
== LT_PreprocessorDirective
||
268 Line
->Type
== LT_ImportStatement
)) {
272 State
.NextToken
= Line
->First
;
273 State
.Stack
.push_back(ParenState(/*Tok=*/nullptr, FirstIndent
, FirstIndent
,
274 /*AvoidBinPacking=*/false,
275 /*NoLineBreak=*/false));
276 State
.NoContinuation
= false;
277 State
.StartOfStringLiteral
= 0;
278 State
.NoLineBreak
= false;
279 State
.StartOfLineLevel
= 0;
280 State
.LowestLevelOnLine
= 0;
281 State
.IgnoreStackForComparison
= false;
283 if (Style
.Language
== FormatStyle::LK_TextProto
) {
284 // We need this in order to deal with the bin packing of text fields at
286 auto &CurrentState
= State
.Stack
.back();
287 CurrentState
.AvoidBinPacking
= true;
288 CurrentState
.BreakBeforeParameter
= true;
289 CurrentState
.AlignColons
= false;
292 // The first token has already been indented and thus consumed.
293 moveStateToNextToken(State
, DryRun
, /*Newline=*/false);
297 bool ContinuationIndenter::canBreak(const LineState
&State
) {
298 const FormatToken
&Current
= *State
.NextToken
;
299 const FormatToken
&Previous
= *Current
.Previous
;
300 const auto &CurrentState
= State
.Stack
.back();
301 assert(&Previous
== Current
.Previous
);
302 if (!Current
.CanBreakBefore
&& !(CurrentState
.BreakBeforeClosingBrace
&&
303 Current
.closesBlockOrBlockTypeList(Style
))) {
306 // The opening "{" of a braced list has to be on the same line as the first
307 // element if it is nested in another braced init list or function call.
308 if (!Current
.MustBreakBefore
&& Previous
.is(tok::l_brace
) &&
309 Previous
.isNot(TT_DictLiteral
) && Previous
.is(BK_BracedInit
) &&
311 Previous
.Previous
->isOneOf(tok::l_brace
, tok::l_paren
, tok::comma
)) {
314 // This prevents breaks like:
316 // SomeParameter, OtherParameter).DoSomething(
318 // As they hide "DoSomething" and are generally bad for readability.
319 if (Previous
.opensScope() && Previous
.isNot(tok::l_brace
) &&
320 State
.LowestLevelOnLine
< State
.StartOfLineLevel
&&
321 State
.LowestLevelOnLine
< Current
.NestingLevel
) {
324 if (Current
.isMemberAccess() && CurrentState
.ContainsUnwrappedBuilder
)
327 // Don't create a 'hanging' indent if there are multiple blocks in a single
328 // statement and we are aligning lambda blocks to their signatures.
329 if (Previous
.is(tok::l_brace
) && State
.Stack
.size() > 1 &&
330 State
.Stack
[State
.Stack
.size() - 2].NestedBlockInlined
&&
331 State
.Stack
[State
.Stack
.size() - 2].HasMultipleNestedBlocks
&&
332 Style
.LambdaBodyIndentation
== FormatStyle::LBI_Signature
) {
336 // Don't break after very short return types (e.g. "void") as that is often
338 if (Current
.is(TT_FunctionDeclarationName
)) {
339 if (Style
.BreakAfterReturnType
== FormatStyle::RTBS_None
&&
344 if (Style
.BreakAfterReturnType
== FormatStyle::RTBS_ExceptShortType
) {
345 assert(State
.Column
>= State
.FirstIndent
);
346 if (State
.Column
- State
.FirstIndent
< 6)
351 // Don't allow breaking before a closing brace of a block-indented braced list
352 // initializer if there isn't already a break.
353 if (Current
.is(tok::r_brace
) && Current
.MatchingParen
&&
354 Current
.isBlockIndentedInitRBrace(Style
)) {
355 return CurrentState
.BreakBeforeClosingBrace
;
358 // If binary operators are moved to the next line (including commas for some
359 // styles of constructor initializers), that's always ok.
360 if (!Current
.isOneOf(TT_BinaryOperator
, tok::comma
) &&
361 // Allow breaking opening brace of lambdas (when passed as function
362 // arguments) to a new line when BeforeLambdaBody brace wrapping is
364 (!Style
.BraceWrapping
.BeforeLambdaBody
||
365 Current
.isNot(TT_LambdaLBrace
)) &&
366 CurrentState
.NoLineBreakInOperand
) {
370 if (Previous
.is(tok::l_square
) && Previous
.is(TT_ObjCMethodExpr
))
373 if (Current
.is(TT_ConditionalExpr
) && Previous
.is(tok::r_paren
) &&
374 Previous
.MatchingParen
&& Previous
.MatchingParen
->Previous
&&
375 Previous
.MatchingParen
->Previous
->MatchingParen
&&
376 Previous
.MatchingParen
->Previous
->MatchingParen
->is(TT_LambdaLBrace
)) {
377 // We have a lambda within a conditional expression, allow breaking here.
378 assert(Previous
.MatchingParen
->Previous
->is(tok::r_brace
));
382 return !State
.NoLineBreak
&& !CurrentState
.NoLineBreak
;
385 bool ContinuationIndenter::mustBreak(const LineState
&State
) {
386 const FormatToken
&Current
= *State
.NextToken
;
387 const FormatToken
&Previous
= *Current
.Previous
;
388 const auto &CurrentState
= State
.Stack
.back();
389 if (Style
.BraceWrapping
.BeforeLambdaBody
&& Current
.CanBreakBefore
&&
390 Current
.is(TT_LambdaLBrace
) && Previous
.isNot(TT_LineComment
)) {
391 auto LambdaBodyLength
= getLengthToMatchingParen(Current
, State
.Stack
);
392 return LambdaBodyLength
> getColumnLimit(State
);
394 if (Current
.MustBreakBefore
||
395 (Current
.is(TT_InlineASMColon
) &&
396 (Style
.BreakBeforeInlineASMColon
== FormatStyle::BBIAS_Always
||
397 (Style
.BreakBeforeInlineASMColon
== FormatStyle::BBIAS_OnlyMultiline
&&
398 Style
.ColumnLimit
> 0)))) {
401 if (CurrentState
.BreakBeforeClosingBrace
&&
402 (Current
.closesBlockOrBlockTypeList(Style
) ||
403 (Current
.is(tok::r_brace
) &&
404 Current
.isBlockIndentedInitRBrace(Style
)))) {
407 if (CurrentState
.BreakBeforeClosingParen
&& Current
.is(tok::r_paren
))
409 if (Style
.Language
== FormatStyle::LK_ObjC
&&
410 Style
.ObjCBreakBeforeNestedBlockParam
&&
411 Current
.ObjCSelectorNameParts
> 1 &&
412 Current
.startsSequence(TT_SelectorName
, tok::colon
, tok::caret
)) {
415 // Avoid producing inconsistent states by requiring breaks where they are not
416 // permitted for C# generic type constraints.
417 if (CurrentState
.IsCSharpGenericTypeConstraint
&&
418 Previous
.isNot(TT_CSharpGenericTypeConstraintComma
)) {
421 if ((startsNextParameter(Current
, Style
) || Previous
.is(tok::semi
) ||
422 (Previous
.is(TT_TemplateCloser
) && Current
.is(TT_StartOfName
) &&
423 State
.Line
->First
->isNot(TT_AttributeSquare
) && Style
.isCpp() &&
424 // FIXME: This is a temporary workaround for the case where clang-format
425 // sets BreakBeforeParameter to avoid bin packing and this creates a
426 // completely unnecessary line break after a template type that isn't
428 (Previous
.NestingLevel
== 1 ||
429 Style
.BinPackParameters
== FormatStyle::BPPS_BinPack
)) ||
430 (Style
.BreakBeforeTernaryOperators
&& Current
.is(TT_ConditionalExpr
) &&
431 Previous
.isNot(tok::question
)) ||
432 (!Style
.BreakBeforeTernaryOperators
&&
433 Previous
.is(TT_ConditionalExpr
))) &&
434 CurrentState
.BreakBeforeParameter
&& !Current
.isTrailingComment() &&
435 !Current
.isOneOf(tok::r_paren
, tok::r_brace
)) {
438 if (CurrentState
.IsChainedConditional
&&
439 ((Style
.BreakBeforeTernaryOperators
&& Current
.is(TT_ConditionalExpr
) &&
440 Current
.is(tok::colon
)) ||
441 (!Style
.BreakBeforeTernaryOperators
&& Previous
.is(TT_ConditionalExpr
) &&
442 Previous
.is(tok::colon
)))) {
445 if (((Previous
.is(TT_DictLiteral
) && Previous
.is(tok::l_brace
)) ||
446 (Previous
.is(TT_ArrayInitializerLSquare
) &&
447 Previous
.ParameterCount
> 1) ||
448 opensProtoMessageField(Previous
, Style
)) &&
449 Style
.ColumnLimit
> 0 &&
450 getLengthToMatchingParen(Previous
, State
.Stack
) + State
.Column
- 1 >
451 getColumnLimit(State
)) {
455 const FormatToken
&BreakConstructorInitializersToken
=
456 Style
.BreakConstructorInitializers
== FormatStyle::BCIS_AfterColon
459 if (BreakConstructorInitializersToken
.is(TT_CtorInitializerColon
) &&
460 (State
.Column
+ State
.Line
->Last
->TotalLength
- Previous
.TotalLength
>
461 getColumnLimit(State
) ||
462 CurrentState
.BreakBeforeParameter
) &&
463 (!Current
.isTrailingComment() || Current
.NewlinesBefore
> 0) &&
464 (Style
.AllowShortFunctionsOnASingleLine
!= FormatStyle::SFS_All
||
465 Style
.BreakConstructorInitializers
!= FormatStyle::BCIS_BeforeColon
||
466 Style
.ColumnLimit
!= 0)) {
470 if (Current
.is(TT_ObjCMethodExpr
) && Previous
.isNot(TT_SelectorName
) &&
471 State
.Line
->startsWith(TT_ObjCMethodSpecifier
)) {
474 if (Current
.is(TT_SelectorName
) && Previous
.isNot(tok::at
) &&
475 CurrentState
.ObjCSelectorNameFound
&& CurrentState
.BreakBeforeParameter
&&
476 (Style
.ObjCBreakBeforeNestedBlockParam
||
477 !Current
.startsSequence(TT_SelectorName
, tok::colon
, tok::caret
))) {
481 unsigned NewLineColumn
= getNewLineColumn(State
);
482 if (Current
.isMemberAccess() && Style
.ColumnLimit
!= 0 &&
483 State
.Column
+ getLengthToNextOperator(Current
) > Style
.ColumnLimit
&&
484 (State
.Column
> NewLineColumn
||
485 Current
.NestingLevel
< State
.StartOfLineLevel
)) {
489 if (startsSegmentOfBuilderTypeCall(Current
) &&
490 (CurrentState
.CallContinuation
!= 0 ||
491 CurrentState
.BreakBeforeParameter
) &&
492 // JavaScript is treated different here as there is a frequent pattern:
493 // SomeFunction(function() {
496 // FIXME: We should find a more generic solution to this problem.
497 !(State
.Column
<= NewLineColumn
&& Style
.isJavaScript()) &&
498 !(Previous
.closesScopeAfterBlock() && State
.Column
<= NewLineColumn
)) {
502 // If the template declaration spans multiple lines, force wrap before the
503 // function/class declaration.
504 if (Previous
.ClosesTemplateDeclaration
&& CurrentState
.BreakBeforeParameter
&&
505 Current
.CanBreakBefore
) {
509 if (State
.Line
->First
->isNot(tok::kw_enum
) && State
.Column
<= NewLineColumn
)
512 if (Style
.AlwaysBreakBeforeMultilineStrings
&&
513 (NewLineColumn
== State
.FirstIndent
+ Style
.ContinuationIndentWidth
||
514 Previous
.is(tok::comma
) || Current
.NestingLevel
< 2) &&
515 !Previous
.isOneOf(tok::kw_return
, tok::lessless
, tok::at
,
516 Keywords
.kw_dollar
) &&
517 !Previous
.isOneOf(TT_InlineASMColon
, TT_ConditionalExpr
) &&
518 nextIsMultilineString(State
)) {
522 // Using CanBreakBefore here and below takes care of the decision whether the
523 // current style uses wrapping before or after operators for the given
525 if (Previous
.is(TT_BinaryOperator
) && Current
.CanBreakBefore
) {
526 const auto PreviousPrecedence
= Previous
.getPrecedence();
527 if (PreviousPrecedence
!= prec::Assignment
&&
528 CurrentState
.BreakBeforeParameter
&& !Current
.isTrailingComment()) {
529 const bool LHSIsBinaryExpr
=
530 Previous
.Previous
&& Previous
.Previous
->EndsBinaryExpression
;
533 // If we need to break somewhere inside the LHS of a binary expression, we
534 // should also break after the operator. Otherwise, the formatting would
535 // hide the operator precedence, e.g. in:
536 // if (aaaaaaaaaaaaaa ==
537 // bbbbbbbbbbbbbb && c) {..
538 // For comparisons, we only apply this rule, if the LHS is a binary
539 // expression itself as otherwise, the line breaks seem superfluous.
540 // We need special cases for ">>" which we have split into two ">" while
541 // lexing in order to make template parsing easier.
542 const bool IsComparison
=
543 (PreviousPrecedence
== prec::Relational
||
544 PreviousPrecedence
== prec::Equality
||
545 PreviousPrecedence
== prec::Spaceship
) &&
547 Previous
.Previous
->isNot(TT_BinaryOperator
); // For >>.
551 } else if (Current
.is(TT_BinaryOperator
) && Current
.CanBreakBefore
&&
552 CurrentState
.BreakBeforeParameter
) {
556 // Same as above, but for the first "<<" operator.
557 if (Current
.is(tok::lessless
) && Current
.isNot(TT_OverloadedOperator
) &&
558 CurrentState
.BreakBeforeParameter
&& CurrentState
.FirstLessLess
== 0) {
562 if (Current
.NestingLevel
== 0 && !Current
.isTrailingComment()) {
563 // Always break after "template <...>"(*) and leading annotations. This is
564 // only for cases where the entire line does not fit on a single line as a
565 // different LineFormatter would be used otherwise.
566 // *: Except when another option interferes with that, like concepts.
567 if (Previous
.ClosesTemplateDeclaration
) {
568 if (Current
.is(tok::kw_concept
)) {
569 switch (Style
.BreakBeforeConceptDeclarations
) {
570 case FormatStyle::BBCDS_Allowed
:
572 case FormatStyle::BBCDS_Always
:
574 case FormatStyle::BBCDS_Never
:
578 if (Current
.is(TT_RequiresClause
)) {
579 switch (Style
.RequiresClausePosition
) {
580 case FormatStyle::RCPS_SingleLine
:
581 case FormatStyle::RCPS_WithPreceding
:
587 return Style
.BreakTemplateDeclarations
!= FormatStyle::BTDS_No
&&
588 (Style
.BreakTemplateDeclarations
!= FormatStyle::BTDS_Leave
||
589 Current
.NewlinesBefore
> 0);
591 if (Previous
.is(TT_FunctionAnnotationRParen
) &&
592 State
.Line
->Type
!= LT_PreprocessorDirective
) {
595 if (Previous
.is(TT_LeadingJavaAnnotation
) && Current
.isNot(tok::l_paren
) &&
596 Current
.isNot(TT_LeadingJavaAnnotation
)) {
601 if (Style
.isJavaScript() && Previous
.is(tok::r_paren
) &&
602 Previous
.is(TT_JavaAnnotation
)) {
603 // Break after the closing parenthesis of TypeScript decorators before
604 // functions, getters and setters.
605 static const llvm::StringSet
<> BreakBeforeDecoratedTokens
= {"get", "set",
607 if (BreakBeforeDecoratedTokens
.contains(Current
.TokenText
))
611 if (Current
.is(TT_FunctionDeclarationName
) &&
612 !State
.Line
->ReturnTypeWrapped
&&
613 // Don't break before a C# function when no break after return type.
614 (!Style
.isCSharp() ||
615 Style
.BreakAfterReturnType
> FormatStyle::RTBS_ExceptShortType
) &&
616 // Don't always break between a JavaScript `function` and the function
618 !Style
.isJavaScript() && Previous
.isNot(tok::kw_template
) &&
619 CurrentState
.BreakBeforeParameter
) {
623 // The following could be precomputed as they do not depend on the state.
624 // However, as they should take effect only if the UnwrappedLine does not fit
625 // into the ColumnLimit, they are checked here in the ContinuationIndenter.
626 if (Style
.ColumnLimit
!= 0 && Previous
.is(BK_Block
) &&
627 Previous
.is(tok::l_brace
) &&
628 !Current
.isOneOf(tok::r_brace
, tok::comment
)) {
632 if (Current
.is(tok::lessless
) &&
633 ((Previous
.is(tok::identifier
) && Previous
.TokenText
== "endl") ||
634 (Previous
.Tok
.isLiteral() && (Previous
.TokenText
.ends_with("\\n\"") ||
635 Previous
.TokenText
== "\'\\n\'")))) {
639 if (Previous
.is(TT_BlockComment
) && Previous
.IsMultiline
)
642 if (State
.NoContinuation
)
648 unsigned ContinuationIndenter::addTokenToState(LineState
&State
, bool Newline
,
650 unsigned ExtraSpaces
) {
651 const FormatToken
&Current
= *State
.NextToken
;
652 assert(State
.NextToken
->Previous
);
653 const FormatToken
&Previous
= *State
.NextToken
->Previous
;
655 assert(!State
.Stack
.empty());
656 State
.NoContinuation
= false;
658 if (Current
.is(TT_ImplicitStringLiteral
) &&
659 (!Previous
.Tok
.getIdentifierInfo() ||
660 Previous
.Tok
.getIdentifierInfo()->getPPKeywordID() ==
661 tok::pp_not_keyword
)) {
663 SourceMgr
.getSpellingColumnNumber(Current
.WhitespaceRange
.getEnd());
664 if (Current
.LastNewlineOffset
!= 0) {
665 // If there is a newline within this token, the final column will solely
666 // determined by the current end column.
667 State
.Column
= EndColumn
;
669 unsigned StartColumn
=
670 SourceMgr
.getSpellingColumnNumber(Current
.WhitespaceRange
.getBegin());
671 assert(EndColumn
>= StartColumn
);
672 State
.Column
+= EndColumn
- StartColumn
;
674 moveStateToNextToken(State
, DryRun
, /*Newline=*/false);
678 unsigned Penalty
= 0;
680 Penalty
= addTokenOnNewLine(State
, DryRun
);
682 addTokenOnCurrentLine(State
, DryRun
, ExtraSpaces
);
684 return moveStateToNextToken(State
, DryRun
, Newline
) + Penalty
;
687 void ContinuationIndenter::addTokenOnCurrentLine(LineState
&State
, bool DryRun
,
688 unsigned ExtraSpaces
) {
689 FormatToken
&Current
= *State
.NextToken
;
690 assert(State
.NextToken
->Previous
);
691 const FormatToken
&Previous
= *State
.NextToken
->Previous
;
692 auto &CurrentState
= State
.Stack
.back();
694 bool DisallowLineBreaksOnThisLine
=
695 Style
.LambdaBodyIndentation
== FormatStyle::LBI_Signature
&&
696 // Deal with lambda arguments in C++. The aim here is to ensure that we
697 // don't over-indent lambda function bodies when lambdas are passed as
698 // arguments to function calls. We do this by ensuring that either all
699 // arguments (including any lambdas) go on the same line as the function
700 // call, or we break before the first argument.
701 Style
.isCpp() && [&] {
702 // For example, `/*Newline=*/false`.
703 if (Previous
.is(TT_BlockComment
) && Current
.SpacesRequiredBefore
== 0)
705 const auto *PrevNonComment
= Current
.getPreviousNonComment();
706 if (!PrevNonComment
|| PrevNonComment
->isNot(tok::l_paren
))
708 if (Current
.isOneOf(tok::comment
, tok::l_paren
, TT_LambdaLSquare
))
710 auto BlockParameterCount
= PrevNonComment
->BlockParameterCount
;
711 if (BlockParameterCount
== 0)
714 // Multiple lambdas in the same function call.
715 if (BlockParameterCount
> 1)
718 // A lambda followed by another arg.
719 if (!PrevNonComment
->Role
)
721 auto Comma
= PrevNonComment
->Role
->lastComma();
724 auto Next
= Comma
->getNextNonComment();
726 !Next
->isOneOf(TT_LambdaLSquare
, tok::l_brace
, tok::caret
);
729 if (DisallowLineBreaksOnThisLine
)
730 State
.NoLineBreak
= true;
732 if (Current
.is(tok::equal
) &&
733 (State
.Line
->First
->is(tok::kw_for
) || Current
.NestingLevel
== 0) &&
734 CurrentState
.VariablePos
== 0 &&
735 (!Previous
.Previous
||
736 Previous
.Previous
->isNot(TT_DesignatedInitializerPeriod
))) {
737 CurrentState
.VariablePos
= State
.Column
;
738 // Move over * and & if they are bound to the variable name.
739 const FormatToken
*Tok
= &Previous
;
740 while (Tok
&& CurrentState
.VariablePos
>= Tok
->ColumnWidth
) {
741 CurrentState
.VariablePos
-= Tok
->ColumnWidth
;
742 if (Tok
->SpacesRequiredBefore
!= 0)
746 if (Previous
.PartOfMultiVariableDeclStmt
)
747 CurrentState
.LastSpace
= CurrentState
.VariablePos
;
750 unsigned Spaces
= Current
.SpacesRequiredBefore
+ ExtraSpaces
;
752 // Indent preprocessor directives after the hash if required.
753 int PPColumnCorrection
= 0;
754 if (Style
.IndentPPDirectives
== FormatStyle::PPDIS_AfterHash
&&
755 Previous
.is(tok::hash
) && State
.FirstIndent
> 0 &&
756 &Previous
== State
.Line
->First
&&
757 (State
.Line
->Type
== LT_PreprocessorDirective
||
758 State
.Line
->Type
== LT_ImportStatement
)) {
759 Spaces
+= State
.FirstIndent
;
761 // For preprocessor indent with tabs, State.Column will be 1 because of the
762 // hash. This causes second-level indents onward to have an extra space
763 // after the tabs. We avoid this misalignment by subtracting 1 from the
764 // column value passed to replaceWhitespace().
765 if (Style
.UseTab
!= FormatStyle::UT_Never
)
766 PPColumnCorrection
= -1;
770 Whitespaces
.replaceWhitespace(Current
, /*Newlines=*/0, Spaces
,
771 State
.Column
+ Spaces
+ PPColumnCorrection
,
772 /*IsAligned=*/false, State
.Line
->InMacroBody
);
775 // If "BreakBeforeInheritanceComma" mode, don't break within the inheritance
776 // declaration unless there is multiple inheritance.
777 if (Style
.BreakInheritanceList
== FormatStyle::BILS_BeforeComma
&&
778 Current
.is(TT_InheritanceColon
)) {
779 CurrentState
.NoLineBreak
= true;
781 if (Style
.BreakInheritanceList
== FormatStyle::BILS_AfterColon
&&
782 Previous
.is(TT_InheritanceColon
)) {
783 CurrentState
.NoLineBreak
= true;
786 if (Current
.is(TT_SelectorName
) && !CurrentState
.ObjCSelectorNameFound
) {
787 unsigned MinIndent
= std::max(
788 State
.FirstIndent
+ Style
.ContinuationIndentWidth
, CurrentState
.Indent
);
789 unsigned FirstColonPos
= State
.Column
+ Spaces
+ Current
.ColumnWidth
;
790 if (Current
.LongestObjCSelectorName
== 0)
791 CurrentState
.AlignColons
= false;
792 else if (MinIndent
+ Current
.LongestObjCSelectorName
> FirstColonPos
)
793 CurrentState
.ColonPos
= MinIndent
+ Current
.LongestObjCSelectorName
;
795 CurrentState
.ColonPos
= FirstColonPos
;
798 // In "AlwaysBreak" or "BlockIndent" mode, enforce wrapping directly after the
799 // parenthesis by disallowing any further line breaks if there is no line
800 // break after the opening parenthesis. Don't break if it doesn't conserve
802 auto IsOpeningBracket
= [&](const FormatToken
&Tok
) {
803 auto IsStartOfBracedList
= [&]() {
804 return Tok
.is(tok::l_brace
) && Tok
.isNot(BK_Block
) &&
805 Style
.Cpp11BracedListStyle
;
807 if (!Tok
.isOneOf(tok::l_paren
, TT_TemplateOpener
, tok::l_square
) &&
808 !IsStartOfBracedList()) {
813 if (Tok
.Previous
->isIf())
814 return Style
.AlignAfterOpenBracket
== FormatStyle::BAS_AlwaysBreak
;
815 return !Tok
.Previous
->isOneOf(TT_CastRParen
, tok::kw_for
, tok::kw_while
,
817 !(Style
.isJavaScript() && Tok
.Previous
->is(Keywords
.kw_await
));
819 auto IsFunctionCallParen
= [](const FormatToken
&Tok
) {
820 return Tok
.is(tok::l_paren
) && Tok
.ParameterCount
> 0 && Tok
.Previous
&&
821 Tok
.Previous
->is(tok::identifier
);
823 auto IsInTemplateString
= [this](const FormatToken
&Tok
) {
824 if (!Style
.isJavaScript())
826 for (const auto *Prev
= &Tok
; Prev
; Prev
= Prev
->Previous
) {
827 if (Prev
->is(TT_TemplateString
) && Prev
->opensScope())
829 if (Prev
->is(TT_TemplateString
) && Prev
->closesScope())
834 // Identifies simple (no expression) one-argument function calls.
835 auto StartsSimpleOneArgList
= [&](const FormatToken
&TokAfterLParen
) {
836 assert(TokAfterLParen
.isNot(tok::comment
) || TokAfterLParen
.Next
);
838 TokAfterLParen
.is(tok::comment
) ? *TokAfterLParen
.Next
: TokAfterLParen
;
839 if (!Tok
.FakeLParens
.empty() && Tok
.FakeLParens
.back() > prec::Unknown
)
841 // Nested calls that involve `new` expressions also look like simple
842 // function calls, eg:
844 // - foo(::new Bar())
845 if (Tok
.is(tok::kw_new
) || Tok
.startsSequence(tok::coloncolon
, tok::kw_new
))
847 if (Tok
.is(TT_UnaryOperator
) ||
848 (Style
.isJavaScript() &&
849 Tok
.isOneOf(tok::ellipsis
, Keywords
.kw_await
))) {
852 const auto *Previous
= Tok
.Previous
;
853 if (!Previous
|| (!Previous
->isOneOf(TT_FunctionDeclarationLParen
,
854 TT_LambdaDefinitionLParen
) &&
855 !IsFunctionCallParen(*Previous
))) {
858 if (IsOpeningBracket(Tok
) || IsInTemplateString(Tok
))
860 const auto *Next
= Tok
.Next
;
861 return !Next
|| Next
->isMemberAccess() ||
862 Next
->is(TT_FunctionDeclarationLParen
) || IsFunctionCallParen(*Next
);
864 if ((Style
.AlignAfterOpenBracket
== FormatStyle::BAS_AlwaysBreak
||
865 Style
.AlignAfterOpenBracket
== FormatStyle::BAS_BlockIndent
) &&
866 IsOpeningBracket(Previous
) && State
.Column
> getNewLineColumn(State
) &&
867 // Don't do this for simple (no expressions) one-argument function calls
868 // as that feels like needlessly wasting whitespace, e.g.:
873 // caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa))));
875 // caaaaaaaaaaaaaaaaaaaaal(
876 // new SomethingElseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee());
877 !StartsSimpleOneArgList(Current
)) {
878 CurrentState
.NoLineBreak
= true;
881 if (Previous
.is(TT_TemplateString
) && Previous
.opensScope())
882 CurrentState
.NoLineBreak
= true;
884 // Align following lines within parentheses / brackets if configured.
885 // Note: This doesn't apply to macro expansion lines, which are MACRO( , , )
886 // with args as children of the '(' and ',' tokens. It does not make sense to
887 // align the commas with the opening paren.
888 if (Style
.AlignAfterOpenBracket
!= FormatStyle::BAS_DontAlign
&&
889 !CurrentState
.IsCSharpGenericTypeConstraint
&& Previous
.opensScope() &&
890 Previous
.isNot(TT_ObjCMethodExpr
) && Previous
.isNot(TT_RequiresClause
) &&
891 Previous
.isNot(TT_TableGenDAGArgOpener
) &&
892 Previous
.isNot(TT_TableGenDAGArgOpenerToBreak
) &&
893 !(Current
.MacroParent
&& Previous
.MacroParent
) &&
894 (Current
.isNot(TT_LineComment
) ||
895 Previous
.isOneOf(BK_BracedInit
, TT_VerilogMultiLineListLParen
)) &&
896 !IsInTemplateString(Current
)) {
897 CurrentState
.Indent
= State
.Column
+ Spaces
;
898 CurrentState
.IsAligned
= true;
900 if (CurrentState
.AvoidBinPacking
&& startsNextParameter(Current
, Style
))
901 CurrentState
.NoLineBreak
= true;
902 if (mustBreakBinaryOperation(Current
, Style
))
903 CurrentState
.NoLineBreak
= true;
905 if (startsSegmentOfBuilderTypeCall(Current
) &&
906 State
.Column
> getNewLineColumn(State
)) {
907 CurrentState
.ContainsUnwrappedBuilder
= true;
910 if (Current
.is(TT_LambdaArrow
) && Style
.Language
== FormatStyle::LK_Java
)
911 CurrentState
.NoLineBreak
= true;
912 if (Current
.isMemberAccess() && Previous
.is(tok::r_paren
) &&
913 (Previous
.MatchingParen
&&
914 (Previous
.TotalLength
- Previous
.MatchingParen
->TotalLength
> 10))) {
915 // If there is a function call with long parameters, break before trailing
916 // calls. This prevents things like:
917 // EXPECT_CALL(SomeLongParameter).Times(
919 // We don't want to do this for short parameters as they can just be
921 CurrentState
.NoLineBreak
= true;
924 // Don't allow the RHS of an operator to be split over multiple lines unless
925 // there is a line-break right after the operator.
926 // Exclude relational operators, as there, it is always more desirable to
927 // have the LHS 'left' of the RHS.
928 const FormatToken
*P
= Current
.getPreviousNonComment();
929 if (Current
.isNot(tok::comment
) && P
&&
930 (P
->isOneOf(TT_BinaryOperator
, tok::comma
) ||
931 (P
->is(TT_ConditionalExpr
) && P
->is(tok::colon
))) &&
932 !P
->isOneOf(TT_OverloadedOperator
, TT_CtorInitializerComma
) &&
933 P
->getPrecedence() != prec::Assignment
&&
934 P
->getPrecedence() != prec::Relational
&&
935 P
->getPrecedence() != prec::Spaceship
) {
936 bool BreakBeforeOperator
=
937 P
->MustBreakBefore
|| P
->is(tok::lessless
) ||
938 (P
->is(TT_BinaryOperator
) &&
939 Style
.BreakBeforeBinaryOperators
!= FormatStyle::BOS_None
) ||
940 (P
->is(TT_ConditionalExpr
) && Style
.BreakBeforeTernaryOperators
);
941 // Don't do this if there are only two operands. In these cases, there is
942 // always a nice vertical separation between them and the extra line break
944 bool HasTwoOperands
= P
->OperatorIndex
== 0 && !P
->NextOperator
&&
945 P
->isNot(TT_ConditionalExpr
);
946 if ((!BreakBeforeOperator
&&
948 Style
.AlignOperands
!= FormatStyle::OAS_DontAlign
)) ||
949 (!CurrentState
.LastOperatorWrapped
&& BreakBeforeOperator
)) {
950 CurrentState
.NoLineBreakInOperand
= true;
954 State
.Column
+= Spaces
;
955 if (Current
.isNot(tok::comment
) && Previous
.is(tok::l_paren
) &&
957 (Previous
.Previous
->is(tok::kw_for
) || Previous
.Previous
->isIf())) {
958 // Treat the condition inside an if as if it was a second function
959 // parameter, i.e. let nested calls have a continuation indent.
960 CurrentState
.LastSpace
= State
.Column
;
961 CurrentState
.NestedBlockIndent
= State
.Column
;
962 } else if (!Current
.isOneOf(tok::comment
, tok::caret
) &&
963 ((Previous
.is(tok::comma
) &&
964 Previous
.isNot(TT_OverloadedOperator
)) ||
965 (Previous
.is(tok::colon
) && Previous
.is(TT_ObjCMethodExpr
)))) {
966 CurrentState
.LastSpace
= State
.Column
;
967 } else if (Previous
.is(TT_CtorInitializerColon
) &&
968 (!Current
.isTrailingComment() || Current
.NewlinesBefore
> 0) &&
969 Style
.BreakConstructorInitializers
==
970 FormatStyle::BCIS_AfterColon
) {
971 CurrentState
.Indent
= State
.Column
;
972 CurrentState
.LastSpace
= State
.Column
;
973 } else if (Previous
.isOneOf(TT_ConditionalExpr
, TT_CtorInitializerColon
)) {
974 CurrentState
.LastSpace
= State
.Column
;
975 } else if (Previous
.is(TT_BinaryOperator
) &&
976 ((Previous
.getPrecedence() != prec::Assignment
&&
977 (Previous
.isNot(tok::lessless
) || Previous
.OperatorIndex
!= 0 ||
978 Previous
.NextOperator
)) ||
979 Current
.StartsBinaryExpression
)) {
980 // Indent relative to the RHS of the expression unless this is a simple
981 // assignment without binary expression on the RHS.
982 if (Style
.BreakBeforeBinaryOperators
== FormatStyle::BOS_None
)
983 CurrentState
.LastSpace
= State
.Column
;
984 } else if (Previous
.is(TT_InheritanceColon
)) {
985 CurrentState
.Indent
= State
.Column
;
986 CurrentState
.LastSpace
= State
.Column
;
987 } else if (Current
.is(TT_CSharpGenericTypeConstraintColon
)) {
988 CurrentState
.ColonPos
= State
.Column
;
989 } else if (Previous
.opensScope()) {
990 // If a function has a trailing call, indent all parameters from the
991 // opening parenthesis. This avoids confusing indents like:
992 // OuterFunction(InnerFunctionCall( // break
993 // ParameterToInnerFunction)) // break
994 // .SecondInnerFunctionCall();
995 if (Previous
.MatchingParen
) {
996 const FormatToken
*Next
= Previous
.MatchingParen
->getNextNonComment();
997 if (Next
&& Next
->isMemberAccess() && State
.Stack
.size() > 1 &&
998 State
.Stack
[State
.Stack
.size() - 2].CallContinuation
== 0) {
999 CurrentState
.LastSpace
= State
.Column
;
1005 unsigned ContinuationIndenter::addTokenOnNewLine(LineState
&State
,
1007 FormatToken
&Current
= *State
.NextToken
;
1008 assert(State
.NextToken
->Previous
);
1009 const FormatToken
&Previous
= *State
.NextToken
->Previous
;
1010 auto &CurrentState
= State
.Stack
.back();
1012 // Extra penalty that needs to be added because of the way certain line
1013 // breaks are chosen.
1014 unsigned Penalty
= 0;
1016 const FormatToken
*PreviousNonComment
= Current
.getPreviousNonComment();
1017 const FormatToken
*NextNonComment
= Previous
.getNextNonComment();
1018 if (!NextNonComment
)
1019 NextNonComment
= &Current
;
1020 // The first line break on any NestingLevel causes an extra penalty in order
1021 // prefer similar line breaks.
1022 if (!CurrentState
.ContainsLineBreak
)
1024 CurrentState
.ContainsLineBreak
= true;
1026 Penalty
+= State
.NextToken
->SplitPenalty
;
1028 // Breaking before the first "<<" is generally not desirable if the LHS is
1029 // short. Also always add the penalty if the LHS is split over multiple lines
1030 // to avoid unnecessary line breaks that just work around this penalty.
1031 if (NextNonComment
->is(tok::lessless
) && CurrentState
.FirstLessLess
== 0 &&
1032 (State
.Column
<= Style
.ColumnLimit
/ 3 ||
1033 CurrentState
.BreakBeforeParameter
)) {
1034 Penalty
+= Style
.PenaltyBreakFirstLessLess
;
1037 State
.Column
= getNewLineColumn(State
);
1039 // Add Penalty proportional to amount of whitespace away from FirstColumn
1040 // This tends to penalize several lines that are far-right indented,
1041 // and prefers a line-break prior to such a block, e.g:
1044 // member(value), looooooooooooooooong_member(
1045 // looooooooooong_call(param_1, param_2, param_3))
1046 // would then become
1049 // looooooooooooooooong_member(
1050 // looooooooooong_call(param_1, param_2, param_3))
1051 if (State
.Column
> State
.FirstIndent
) {
1053 Style
.PenaltyIndentedWhitespace
* (State
.Column
- State
.FirstIndent
);
1056 // Indent nested blocks relative to this column, unless in a very specific
1057 // JavaScript special case where:
1059 // var loooooong_name =
1064 // is common and should be formatted like a free-standing function. The same
1065 // goes for wrapping before the lambda return type arrow.
1066 if (Current
.isNot(TT_LambdaArrow
) &&
1067 (!Style
.isJavaScript() || Current
.NestingLevel
!= 0 ||
1068 !PreviousNonComment
|| PreviousNonComment
->isNot(tok::equal
) ||
1069 !Current
.isOneOf(Keywords
.kw_async
, Keywords
.kw_function
))) {
1070 CurrentState
.NestedBlockIndent
= State
.Column
;
1073 if (NextNonComment
->isMemberAccess()) {
1074 if (CurrentState
.CallContinuation
== 0)
1075 CurrentState
.CallContinuation
= State
.Column
;
1076 } else if (NextNonComment
->is(TT_SelectorName
)) {
1077 if (!CurrentState
.ObjCSelectorNameFound
) {
1078 if (NextNonComment
->LongestObjCSelectorName
== 0) {
1079 CurrentState
.AlignColons
= false;
1081 CurrentState
.ColonPos
=
1082 (shouldIndentWrappedSelectorName(Style
, State
.Line
->Type
)
1083 ? std::max(CurrentState
.Indent
,
1084 State
.FirstIndent
+ Style
.ContinuationIndentWidth
)
1085 : CurrentState
.Indent
) +
1086 std::max(NextNonComment
->LongestObjCSelectorName
,
1087 NextNonComment
->ColumnWidth
);
1089 } else if (CurrentState
.AlignColons
&&
1090 CurrentState
.ColonPos
<= NextNonComment
->ColumnWidth
) {
1091 CurrentState
.ColonPos
= State
.Column
+ NextNonComment
->ColumnWidth
;
1093 } else if (PreviousNonComment
&& PreviousNonComment
->is(tok::colon
) &&
1094 PreviousNonComment
->isOneOf(TT_ObjCMethodExpr
, TT_DictLiteral
)) {
1095 // FIXME: This is hacky, find a better way. The problem is that in an ObjC
1096 // method expression, the block should be aligned to the line starting it,
1098 // [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason
1102 // Thus, we set LastSpace of the next higher NestingLevel, to which we move
1103 // when we consume all of the "}"'s FakeRParens at the "{".
1104 if (State
.Stack
.size() > 1) {
1105 State
.Stack
[State
.Stack
.size() - 2].LastSpace
=
1106 std::max(CurrentState
.LastSpace
, CurrentState
.Indent
) +
1107 Style
.ContinuationIndentWidth
;
1111 if ((PreviousNonComment
&&
1112 PreviousNonComment
->isOneOf(tok::comma
, tok::semi
) &&
1113 !CurrentState
.AvoidBinPacking
) ||
1114 Previous
.is(TT_BinaryOperator
)) {
1115 CurrentState
.BreakBeforeParameter
= false;
1117 if (PreviousNonComment
&&
1118 (PreviousNonComment
->isOneOf(TT_TemplateCloser
, TT_JavaAnnotation
) ||
1119 PreviousNonComment
->ClosesRequiresClause
) &&
1120 Current
.NestingLevel
== 0) {
1121 CurrentState
.BreakBeforeParameter
= false;
1123 if (NextNonComment
->is(tok::question
) ||
1124 (PreviousNonComment
&& PreviousNonComment
->is(tok::question
))) {
1125 CurrentState
.BreakBeforeParameter
= true;
1127 if (Current
.is(TT_BinaryOperator
) && Current
.CanBreakBefore
)
1128 CurrentState
.BreakBeforeParameter
= false;
1131 unsigned MaxEmptyLinesToKeep
= Style
.MaxEmptyLinesToKeep
+ 1;
1132 if (Current
.is(tok::r_brace
) && Current
.MatchingParen
&&
1133 // Only strip trailing empty lines for l_braces that have children, i.e.
1134 // for function expressions (lambdas, arrows, etc).
1135 !Current
.MatchingParen
->Children
.empty()) {
1136 // lambdas and arrow functions are expressions, thus their r_brace is not
1137 // on its own line, and thus not covered by UnwrappedLineFormatter's logic
1138 // about removing empty lines on closing blocks. Special case them here.
1139 MaxEmptyLinesToKeep
= 1;
1142 std::max(1u, std::min(Current
.NewlinesBefore
, MaxEmptyLinesToKeep
));
1143 bool ContinuePPDirective
=
1144 State
.Line
->InPPDirective
&& State
.Line
->Type
!= LT_ImportStatement
;
1145 Whitespaces
.replaceWhitespace(Current
, Newlines
, State
.Column
, State
.Column
,
1146 CurrentState
.IsAligned
, ContinuePPDirective
);
1149 if (!Current
.isTrailingComment())
1150 CurrentState
.LastSpace
= State
.Column
;
1151 if (Current
.is(tok::lessless
)) {
1152 // If we are breaking before a "<<", we always want to indent relative to
1153 // RHS. This is necessary only for "<<", as we special-case it and don't
1154 // always indent relative to the RHS.
1155 CurrentState
.LastSpace
+= 3; // 3 -> width of "<< ".
1158 State
.StartOfLineLevel
= Current
.NestingLevel
;
1159 State
.LowestLevelOnLine
= Current
.NestingLevel
;
1161 // Any break on this level means that the parent level has been broken
1162 // and we need to avoid bin packing there.
1163 bool NestedBlockSpecialCase
=
1164 (!Style
.isCpp() && Current
.is(tok::r_brace
) && State
.Stack
.size() > 1 &&
1165 State
.Stack
[State
.Stack
.size() - 2].NestedBlockInlined
) ||
1166 (Style
.Language
== FormatStyle::LK_ObjC
&& Current
.is(tok::r_brace
) &&
1167 State
.Stack
.size() > 1 && !Style
.ObjCBreakBeforeNestedBlockParam
);
1168 // Do not force parameter break for statements with requires expressions.
1169 NestedBlockSpecialCase
=
1170 NestedBlockSpecialCase
||
1171 (Current
.MatchingParen
&&
1172 Current
.MatchingParen
->is(TT_RequiresExpressionLBrace
));
1173 if (!NestedBlockSpecialCase
) {
1174 auto ParentLevelIt
= std::next(State
.Stack
.rbegin());
1175 if (Style
.LambdaBodyIndentation
== FormatStyle::LBI_OuterScope
&&
1176 Current
.MatchingParen
&& Current
.MatchingParen
->is(TT_LambdaLBrace
)) {
1177 // If the first character on the new line is a lambda's closing brace, the
1178 // stack still contains that lambda's parenthesis. As such, we need to
1179 // recurse further down the stack than usual to find the parenthesis level
1180 // containing the lambda, which is where we want to set
1181 // BreakBeforeParameter.
1183 // We specifically special case "OuterScope"-formatted lambdas here
1184 // because, when using that setting, breaking before the parameter
1185 // directly following the lambda is particularly unsightly. However, when
1186 // "OuterScope" is not set, the logic to find the parent parenthesis level
1187 // still appears to be sometimes incorrect. It has not been fixed yet
1188 // because it would lead to significant changes in existing behaviour.
1190 // TODO: fix the non-"OuterScope" case too.
1191 auto FindCurrentLevel
= [&](const auto &It
) {
1192 return std::find_if(It
, State
.Stack
.rend(), [](const auto &PState
) {
1193 return PState
.Tok
!= nullptr; // Ignore fake parens.
1196 auto MaybeIncrement
= [&](const auto &It
) {
1197 return It
!= State
.Stack
.rend() ? std::next(It
) : It
;
1199 auto LambdaLevelIt
= FindCurrentLevel(State
.Stack
.rbegin());
1200 auto LevelContainingLambdaIt
=
1201 FindCurrentLevel(MaybeIncrement(LambdaLevelIt
));
1202 ParentLevelIt
= MaybeIncrement(LevelContainingLambdaIt
);
1204 for (auto I
= ParentLevelIt
, E
= State
.Stack
.rend(); I
!= E
; ++I
)
1205 I
->BreakBeforeParameter
= true;
1208 if (PreviousNonComment
&&
1209 !PreviousNonComment
->isOneOf(tok::comma
, tok::colon
, tok::semi
) &&
1210 ((PreviousNonComment
->isNot(TT_TemplateCloser
) &&
1211 !PreviousNonComment
->ClosesRequiresClause
) ||
1212 Current
.NestingLevel
!= 0) &&
1213 !PreviousNonComment
->isOneOf(
1214 TT_BinaryOperator
, TT_FunctionAnnotationRParen
, TT_JavaAnnotation
,
1215 TT_LeadingJavaAnnotation
) &&
1216 Current
.isNot(TT_BinaryOperator
) && !PreviousNonComment
->opensScope() &&
1217 // We don't want to enforce line breaks for subsequent arguments just
1218 // because we have been forced to break before a lambda body.
1219 (!Style
.BraceWrapping
.BeforeLambdaBody
||
1220 Current
.isNot(TT_LambdaLBrace
))) {
1221 CurrentState
.BreakBeforeParameter
= true;
1224 // If we break after { or the [ of an array initializer, we should also break
1225 // before the corresponding } or ].
1226 if (PreviousNonComment
&&
1227 (PreviousNonComment
->isOneOf(tok::l_brace
, TT_ArrayInitializerLSquare
) ||
1228 opensProtoMessageField(*PreviousNonComment
, Style
))) {
1229 CurrentState
.BreakBeforeClosingBrace
= true;
1232 if (PreviousNonComment
&& PreviousNonComment
->is(tok::l_paren
)) {
1233 CurrentState
.BreakBeforeClosingParen
=
1234 Style
.AlignAfterOpenBracket
== FormatStyle::BAS_BlockIndent
;
1237 if (CurrentState
.AvoidBinPacking
) {
1238 // If we are breaking after '(', '{', '<', or this is the break after a ':'
1239 // to start a member initializer list in a constructor, this should not
1240 // be considered bin packing unless the relevant AllowAll option is false or
1241 // this is a dict/object literal.
1242 bool PreviousIsBreakingCtorInitializerColon
=
1243 PreviousNonComment
&& PreviousNonComment
->is(TT_CtorInitializerColon
) &&
1244 Style
.BreakConstructorInitializers
== FormatStyle::BCIS_AfterColon
;
1245 bool AllowAllConstructorInitializersOnNextLine
=
1246 Style
.PackConstructorInitializers
== FormatStyle::PCIS_NextLine
||
1247 Style
.PackConstructorInitializers
== FormatStyle::PCIS_NextLineOnly
;
1248 if (!(Previous
.isOneOf(tok::l_paren
, tok::l_brace
, TT_BinaryOperator
) ||
1249 PreviousIsBreakingCtorInitializerColon
) ||
1250 (!Style
.AllowAllParametersOfDeclarationOnNextLine
&&
1251 State
.Line
->MustBeDeclaration
) ||
1252 (!Style
.AllowAllArgumentsOnNextLine
&&
1253 !State
.Line
->MustBeDeclaration
) ||
1254 (!AllowAllConstructorInitializersOnNextLine
&&
1255 PreviousIsBreakingCtorInitializerColon
) ||
1256 Previous
.is(TT_DictLiteral
)) {
1257 CurrentState
.BreakBeforeParameter
= true;
1260 // If we are breaking after a ':' to start a member initializer list,
1261 // and we allow all arguments on the next line, we should not break
1262 // before the next parameter.
1263 if (PreviousIsBreakingCtorInitializerColon
&&
1264 AllowAllConstructorInitializersOnNextLine
) {
1265 CurrentState
.BreakBeforeParameter
= false;
1269 if (mustBreakBinaryOperation(Current
, Style
))
1270 CurrentState
.BreakBeforeParameter
= true;
1275 unsigned ContinuationIndenter::getNewLineColumn(const LineState
&State
) {
1276 if (!State
.NextToken
|| !State
.NextToken
->Previous
)
1279 FormatToken
&Current
= *State
.NextToken
;
1280 const auto &CurrentState
= State
.Stack
.back();
1282 if (CurrentState
.IsCSharpGenericTypeConstraint
&&
1283 Current
.isNot(TT_CSharpGenericTypeConstraint
)) {
1284 return CurrentState
.ColonPos
+ 2;
1287 const FormatToken
&Previous
= *Current
.Previous
;
1288 // If we are continuing an expression, we want to use the continuation indent.
1289 unsigned ContinuationIndent
=
1290 std::max(CurrentState
.LastSpace
, CurrentState
.Indent
) +
1291 Style
.ContinuationIndentWidth
;
1292 const FormatToken
*PreviousNonComment
= Current
.getPreviousNonComment();
1293 const FormatToken
*NextNonComment
= Previous
.getNextNonComment();
1294 if (!NextNonComment
)
1295 NextNonComment
= &Current
;
1297 // Java specific bits.
1298 if (Style
.Language
== FormatStyle::LK_Java
&&
1299 Current
.isOneOf(Keywords
.kw_implements
, Keywords
.kw_extends
)) {
1300 return std::max(CurrentState
.LastSpace
,
1301 CurrentState
.Indent
+ Style
.ContinuationIndentWidth
);
1304 // Indentation of the statement following a Verilog case label is taken care
1305 // of in moveStateToNextToken.
1306 if (Style
.isVerilog() && PreviousNonComment
&&
1307 Keywords
.isVerilogEndOfLabel(*PreviousNonComment
)) {
1308 return State
.FirstIndent
;
1311 if (Style
.BreakBeforeBraces
== FormatStyle::BS_Whitesmiths
&&
1312 State
.Line
->First
->is(tok::kw_enum
)) {
1313 return (Style
.IndentWidth
* State
.Line
->First
->IndentLevel
) +
1317 if ((NextNonComment
->is(tok::l_brace
) && NextNonComment
->is(BK_Block
)) ||
1318 (Style
.isVerilog() && Keywords
.isVerilogBegin(*NextNonComment
))) {
1319 if (Current
.NestingLevel
== 0 ||
1320 (Style
.LambdaBodyIndentation
== FormatStyle::LBI_OuterScope
&&
1321 State
.NextToken
->is(TT_LambdaLBrace
))) {
1322 return State
.FirstIndent
;
1324 return CurrentState
.Indent
;
1326 if (Current
.is(TT_LambdaArrow
) &&
1327 Previous
.isOneOf(tok::kw_noexcept
, tok::kw_mutable
, tok::kw_constexpr
,
1328 tok::kw_consteval
, tok::kw_static
, TT_AttributeSquare
)) {
1329 return ContinuationIndent
;
1331 if ((Current
.isOneOf(tok::r_brace
, tok::r_square
) ||
1332 (Current
.is(tok::greater
) && (Style
.isProto() || Style
.isTableGen()))) &&
1333 State
.Stack
.size() > 1) {
1334 if (Current
.closesBlockOrBlockTypeList(Style
))
1335 return State
.Stack
[State
.Stack
.size() - 2].NestedBlockIndent
;
1336 if (Current
.MatchingParen
&& Current
.MatchingParen
->is(BK_BracedInit
))
1337 return State
.Stack
[State
.Stack
.size() - 2].LastSpace
;
1338 return State
.FirstIndent
;
1340 // Indent a closing parenthesis at the previous level if followed by a semi,
1341 // const, or opening brace. This allows indentations such as:
1355 if (Current
.is(tok::r_paren
) && State
.Stack
.size() > 1 &&
1357 Current
.Next
->isOneOf(tok::semi
, tok::kw_const
, tok::l_brace
))) {
1358 return State
.Stack
[State
.Stack
.size() - 2].LastSpace
;
1360 // When DAGArg closer exists top of line, it should be aligned in the similar
1361 // way as function call above.
1362 if (Style
.isTableGen() && Current
.is(TT_TableGenDAGArgCloser
) &&
1363 State
.Stack
.size() > 1) {
1364 return State
.Stack
[State
.Stack
.size() - 2].LastSpace
;
1366 if (Style
.AlignAfterOpenBracket
== FormatStyle::BAS_BlockIndent
&&
1367 (Current
.is(tok::r_paren
) ||
1368 (Current
.is(tok::r_brace
) && Current
.MatchingParen
&&
1369 Current
.MatchingParen
->is(BK_BracedInit
))) &&
1370 State
.Stack
.size() > 1) {
1371 return State
.Stack
[State
.Stack
.size() - 2].LastSpace
;
1373 if (NextNonComment
->is(TT_TemplateString
) && NextNonComment
->closesScope())
1374 return State
.Stack
[State
.Stack
.size() - 2].LastSpace
;
1375 // Field labels in a nested type should be aligned to the brace. For example
1377 // optional int32 b = 2 [(foo_options) = {aaaaaaaaaaaaaaaaaaa: 123,
1378 // bbbbbbbbbbbbbbbbbbbbbbbb:"baz"}];
1379 // For Verilog, a quote following a brace is treated as an identifier. And
1380 // Both braces and colons get annotated as TT_DictLiteral. So we have to
1382 if (Current
.is(tok::identifier
) && Current
.Next
&&
1383 (!Style
.isVerilog() || Current
.Next
->is(tok::colon
)) &&
1384 (Current
.Next
->is(TT_DictLiteral
) ||
1385 (Style
.isProto() && Current
.Next
->isOneOf(tok::less
, tok::l_brace
)))) {
1386 return CurrentState
.Indent
;
1388 if (NextNonComment
->is(TT_ObjCStringLiteral
) &&
1389 State
.StartOfStringLiteral
!= 0) {
1390 return State
.StartOfStringLiteral
- 1;
1392 if (NextNonComment
->isStringLiteral() && State
.StartOfStringLiteral
!= 0)
1393 return State
.StartOfStringLiteral
;
1394 if (NextNonComment
->is(tok::lessless
) && CurrentState
.FirstLessLess
!= 0)
1395 return CurrentState
.FirstLessLess
;
1396 if (NextNonComment
->isMemberAccess()) {
1397 if (CurrentState
.CallContinuation
== 0)
1398 return ContinuationIndent
;
1399 return CurrentState
.CallContinuation
;
1401 if (CurrentState
.QuestionColumn
!= 0 &&
1402 ((NextNonComment
->is(tok::colon
) &&
1403 NextNonComment
->is(TT_ConditionalExpr
)) ||
1404 Previous
.is(TT_ConditionalExpr
))) {
1405 if (((NextNonComment
->is(tok::colon
) && NextNonComment
->Next
&&
1406 !NextNonComment
->Next
->FakeLParens
.empty() &&
1407 NextNonComment
->Next
->FakeLParens
.back() == prec::Conditional
) ||
1408 (Previous
.is(tok::colon
) && !Current
.FakeLParens
.empty() &&
1409 Current
.FakeLParens
.back() == prec::Conditional
)) &&
1410 !CurrentState
.IsWrappedConditional
) {
1411 // NOTE: we may tweak this slightly:
1412 // * not remove the 'lead' ContinuationIndentWidth
1413 // * always un-indent by the operator when
1414 // BreakBeforeTernaryOperators=true
1415 unsigned Indent
= CurrentState
.Indent
;
1416 if (Style
.AlignOperands
!= FormatStyle::OAS_DontAlign
)
1417 Indent
-= Style
.ContinuationIndentWidth
;
1418 if (Style
.BreakBeforeTernaryOperators
&& CurrentState
.UnindentOperator
)
1422 return CurrentState
.QuestionColumn
;
1424 if (Previous
.is(tok::comma
) && CurrentState
.VariablePos
!= 0)
1425 return CurrentState
.VariablePos
;
1426 if (Current
.is(TT_RequiresClause
)) {
1427 if (Style
.IndentRequiresClause
)
1428 return CurrentState
.Indent
+ Style
.IndentWidth
;
1429 switch (Style
.RequiresClausePosition
) {
1430 case FormatStyle::RCPS_OwnLine
:
1431 case FormatStyle::RCPS_WithFollowing
:
1432 case FormatStyle::RCPS_OwnLineWithBrace
:
1433 return CurrentState
.Indent
;
1438 if (NextNonComment
->isOneOf(TT_CtorInitializerColon
, TT_InheritanceColon
,
1439 TT_InheritanceComma
)) {
1440 return State
.FirstIndent
+ Style
.ConstructorInitializerIndentWidth
;
1442 if ((PreviousNonComment
&&
1443 (PreviousNonComment
->ClosesTemplateDeclaration
||
1444 PreviousNonComment
->ClosesRequiresClause
||
1445 (PreviousNonComment
->is(TT_AttributeMacro
) &&
1446 Current
.isNot(tok::l_paren
)) ||
1447 PreviousNonComment
->isOneOf(
1448 TT_AttributeRParen
, TT_AttributeSquare
, TT_FunctionAnnotationRParen
,
1449 TT_JavaAnnotation
, TT_LeadingJavaAnnotation
))) ||
1450 (!Style
.IndentWrappedFunctionNames
&&
1451 NextNonComment
->isOneOf(tok::kw_operator
, TT_FunctionDeclarationName
))) {
1452 return std::max(CurrentState
.LastSpace
, CurrentState
.Indent
);
1454 if (NextNonComment
->is(TT_SelectorName
)) {
1455 if (!CurrentState
.ObjCSelectorNameFound
) {
1456 unsigned MinIndent
= CurrentState
.Indent
;
1457 if (shouldIndentWrappedSelectorName(Style
, State
.Line
->Type
)) {
1458 MinIndent
= std::max(MinIndent
,
1459 State
.FirstIndent
+ Style
.ContinuationIndentWidth
);
1461 // If LongestObjCSelectorName is 0, we are indenting the first
1462 // part of an ObjC selector (or a selector component which is
1463 // not colon-aligned due to block formatting).
1465 // Otherwise, we are indenting a subsequent part of an ObjC
1466 // selector which should be colon-aligned to the longest
1467 // component of the ObjC selector.
1469 // In either case, we want to respect Style.IndentWrappedFunctionNames.
1471 std::max(NextNonComment
->LongestObjCSelectorName
,
1472 NextNonComment
->ColumnWidth
) -
1473 NextNonComment
->ColumnWidth
;
1475 if (!CurrentState
.AlignColons
)
1476 return CurrentState
.Indent
;
1477 if (CurrentState
.ColonPos
> NextNonComment
->ColumnWidth
)
1478 return CurrentState
.ColonPos
- NextNonComment
->ColumnWidth
;
1479 return CurrentState
.Indent
;
1481 if (NextNonComment
->is(tok::colon
) && NextNonComment
->is(TT_ObjCMethodExpr
))
1482 return CurrentState
.ColonPos
;
1483 if (NextNonComment
->is(TT_ArraySubscriptLSquare
)) {
1484 if (CurrentState
.StartOfArraySubscripts
!= 0) {
1485 return CurrentState
.StartOfArraySubscripts
;
1486 } else if (Style
.isCSharp()) { // C# allows `["key"] = value` inside object
1488 return CurrentState
.Indent
;
1490 return ContinuationIndent
;
1493 // OpenMP clauses want to get additional indentation when they are pushed onto
1495 if (State
.Line
->InPragmaDirective
) {
1496 FormatToken
*PragmaType
= State
.Line
->First
->Next
->Next
;
1497 if (PragmaType
&& PragmaType
->TokenText
== "omp")
1498 return CurrentState
.Indent
+ Style
.ContinuationIndentWidth
;
1501 // This ensure that we correctly format ObjC methods calls without inputs,
1502 // i.e. where the last element isn't selector like: [callee method];
1503 if (NextNonComment
->is(tok::identifier
) && NextNonComment
->FakeRParens
== 0 &&
1504 NextNonComment
->Next
&& NextNonComment
->Next
->is(TT_ObjCMethodExpr
)) {
1505 return CurrentState
.Indent
;
1508 if (NextNonComment
->isOneOf(TT_StartOfName
, TT_PointerOrReference
) ||
1509 Previous
.isOneOf(tok::coloncolon
, tok::equal
, TT_JsTypeColon
)) {
1510 return ContinuationIndent
;
1512 if (PreviousNonComment
&& PreviousNonComment
->is(tok::colon
) &&
1513 PreviousNonComment
->isOneOf(TT_ObjCMethodExpr
, TT_DictLiteral
)) {
1514 return ContinuationIndent
;
1516 if (NextNonComment
->is(TT_CtorInitializerComma
))
1517 return CurrentState
.Indent
;
1518 if (PreviousNonComment
&& PreviousNonComment
->is(TT_CtorInitializerColon
) &&
1519 Style
.BreakConstructorInitializers
== FormatStyle::BCIS_AfterColon
) {
1520 return CurrentState
.Indent
;
1522 if (PreviousNonComment
&& PreviousNonComment
->is(TT_InheritanceColon
) &&
1523 Style
.BreakInheritanceList
== FormatStyle::BILS_AfterColon
) {
1524 return CurrentState
.Indent
;
1526 if (Previous
.is(tok::r_paren
) &&
1527 Previous
.isNot(TT_TableGenDAGArgOperatorToBreak
) &&
1528 !Current
.isBinaryOperator() &&
1529 !Current
.isOneOf(tok::colon
, tok::comment
)) {
1530 return ContinuationIndent
;
1532 if (Current
.is(TT_ProtoExtensionLSquare
))
1533 return CurrentState
.Indent
;
1534 if (Current
.isBinaryOperator() && CurrentState
.UnindentOperator
) {
1535 return CurrentState
.Indent
- Current
.Tok
.getLength() -
1536 Current
.SpacesRequiredBefore
;
1538 if (Current
.is(tok::comment
) && NextNonComment
->isBinaryOperator() &&
1539 CurrentState
.UnindentOperator
) {
1540 return CurrentState
.Indent
- NextNonComment
->Tok
.getLength() -
1541 NextNonComment
->SpacesRequiredBefore
;
1543 if (CurrentState
.Indent
== State
.FirstIndent
&& PreviousNonComment
&&
1544 !PreviousNonComment
->isOneOf(tok::r_brace
, TT_CtorInitializerComma
)) {
1545 // Ensure that we fall back to the continuation indent width instead of
1546 // just flushing continuations left.
1547 return CurrentState
.Indent
+ Style
.ContinuationIndentWidth
;
1549 return CurrentState
.Indent
;
1552 static bool hasNestedBlockInlined(const FormatToken
*Previous
,
1553 const FormatToken
&Current
,
1554 const FormatStyle
&Style
) {
1555 if (Previous
->isNot(tok::l_paren
))
1557 if (Previous
->ParameterCount
> 1)
1560 // Also a nested block if contains a lambda inside function with 1 parameter.
1561 return Style
.BraceWrapping
.BeforeLambdaBody
&& Current
.is(TT_LambdaLSquare
);
1564 unsigned ContinuationIndenter::moveStateToNextToken(LineState
&State
,
1565 bool DryRun
, bool Newline
) {
1566 assert(State
.Stack
.size());
1567 const FormatToken
&Current
= *State
.NextToken
;
1568 auto &CurrentState
= State
.Stack
.back();
1570 if (Current
.is(TT_CSharpGenericTypeConstraint
))
1571 CurrentState
.IsCSharpGenericTypeConstraint
= true;
1572 if (Current
.isOneOf(tok::comma
, TT_BinaryOperator
))
1573 CurrentState
.NoLineBreakInOperand
= false;
1574 if (Current
.isOneOf(TT_InheritanceColon
, TT_CSharpGenericTypeConstraintColon
))
1575 CurrentState
.AvoidBinPacking
= true;
1576 if (Current
.is(tok::lessless
) && Current
.isNot(TT_OverloadedOperator
)) {
1577 if (CurrentState
.FirstLessLess
== 0)
1578 CurrentState
.FirstLessLess
= State
.Column
;
1580 CurrentState
.LastOperatorWrapped
= Newline
;
1582 if (Current
.is(TT_BinaryOperator
) && Current
.isNot(tok::lessless
))
1583 CurrentState
.LastOperatorWrapped
= Newline
;
1584 if (Current
.is(TT_ConditionalExpr
) && Current
.Previous
&&
1585 Current
.Previous
->isNot(TT_ConditionalExpr
)) {
1586 CurrentState
.LastOperatorWrapped
= Newline
;
1588 if (Current
.is(TT_ArraySubscriptLSquare
) &&
1589 CurrentState
.StartOfArraySubscripts
== 0) {
1590 CurrentState
.StartOfArraySubscripts
= State
.Column
;
1593 auto IsWrappedConditional
= [](const FormatToken
&Tok
) {
1594 if (!(Tok
.is(TT_ConditionalExpr
) && Tok
.is(tok::question
)))
1596 if (Tok
.MustBreakBefore
)
1599 const FormatToken
*Next
= Tok
.getNextNonComment();
1600 return Next
&& Next
->MustBreakBefore
;
1602 if (IsWrappedConditional(Current
))
1603 CurrentState
.IsWrappedConditional
= true;
1604 if (Style
.BreakBeforeTernaryOperators
&& Current
.is(tok::question
))
1605 CurrentState
.QuestionColumn
= State
.Column
;
1606 if (!Style
.BreakBeforeTernaryOperators
&& Current
.isNot(tok::colon
)) {
1607 const FormatToken
*Previous
= Current
.Previous
;
1608 while (Previous
&& Previous
->isTrailingComment())
1609 Previous
= Previous
->Previous
;
1610 if (Previous
&& Previous
->is(tok::question
))
1611 CurrentState
.QuestionColumn
= State
.Column
;
1613 if (!Current
.opensScope() && !Current
.closesScope() &&
1614 Current
.isNot(TT_PointerOrReference
)) {
1615 State
.LowestLevelOnLine
=
1616 std::min(State
.LowestLevelOnLine
, Current
.NestingLevel
);
1618 if (Current
.isMemberAccess())
1619 CurrentState
.StartOfFunctionCall
= !Current
.NextOperator
? 0 : State
.Column
;
1620 if (Current
.is(TT_SelectorName
))
1621 CurrentState
.ObjCSelectorNameFound
= true;
1622 if (Current
.is(TT_CtorInitializerColon
) &&
1623 Style
.BreakConstructorInitializers
!= FormatStyle::BCIS_AfterColon
) {
1624 // Indent 2 from the column, so:
1625 // SomeClass::SomeClass()
1626 // : First(...), ...
1629 CurrentState
.Indent
= State
.Column
+ (Style
.BreakConstructorInitializers
==
1630 FormatStyle::BCIS_BeforeComma
1633 CurrentState
.NestedBlockIndent
= CurrentState
.Indent
;
1634 if (Style
.PackConstructorInitializers
> FormatStyle::PCIS_BinPack
) {
1635 CurrentState
.AvoidBinPacking
= true;
1636 CurrentState
.BreakBeforeParameter
=
1637 Style
.ColumnLimit
> 0 &&
1638 Style
.PackConstructorInitializers
!= FormatStyle::PCIS_NextLine
&&
1639 Style
.PackConstructorInitializers
!= FormatStyle::PCIS_NextLineOnly
;
1641 CurrentState
.BreakBeforeParameter
= false;
1644 if (Current
.is(TT_CtorInitializerColon
) &&
1645 Style
.BreakConstructorInitializers
== FormatStyle::BCIS_AfterColon
) {
1646 CurrentState
.Indent
=
1647 State
.FirstIndent
+ Style
.ConstructorInitializerIndentWidth
;
1648 CurrentState
.NestedBlockIndent
= CurrentState
.Indent
;
1649 if (Style
.PackConstructorInitializers
> FormatStyle::PCIS_BinPack
)
1650 CurrentState
.AvoidBinPacking
= true;
1652 CurrentState
.BreakBeforeParameter
= false;
1654 if (Current
.is(TT_InheritanceColon
)) {
1655 CurrentState
.Indent
=
1656 State
.FirstIndent
+ Style
.ConstructorInitializerIndentWidth
;
1658 if (Current
.isOneOf(TT_BinaryOperator
, TT_ConditionalExpr
) && Newline
)
1659 CurrentState
.NestedBlockIndent
= State
.Column
+ Current
.ColumnWidth
+ 1;
1660 if (Current
.isOneOf(TT_LambdaLSquare
, TT_LambdaArrow
))
1661 CurrentState
.LastSpace
= State
.Column
;
1662 if (Current
.is(TT_RequiresExpression
) &&
1663 Style
.RequiresExpressionIndentation
== FormatStyle::REI_Keyword
) {
1664 CurrentState
.NestedBlockIndent
= State
.Column
;
1667 // Insert scopes created by fake parenthesis.
1668 const FormatToken
*Previous
= Current
.getPreviousNonComment();
1670 // Add special behavior to support a format commonly used for JavaScript
1672 // SomeFunction(function() {
1676 if (Current
.isNot(tok::comment
) && !Current
.ClosesRequiresClause
&&
1677 Previous
&& Previous
->isOneOf(tok::l_brace
, TT_ArrayInitializerLSquare
) &&
1678 Previous
->isNot(TT_DictLiteral
) && State
.Stack
.size() > 1 &&
1679 !CurrentState
.HasMultipleNestedBlocks
) {
1680 if (State
.Stack
[State
.Stack
.size() - 2].NestedBlockInlined
&& Newline
)
1681 for (ParenState
&PState
: llvm::drop_end(State
.Stack
))
1682 PState
.NoLineBreak
= true;
1683 State
.Stack
[State
.Stack
.size() - 2].NestedBlockInlined
= false;
1685 if (Previous
&& (Previous
->isOneOf(TT_BinaryOperator
, TT_ConditionalExpr
) ||
1686 (Previous
->isOneOf(tok::l_paren
, tok::comma
, tok::colon
) &&
1687 !Previous
->isOneOf(TT_DictLiteral
, TT_ObjCMethodExpr
)))) {
1688 CurrentState
.NestedBlockInlined
=
1689 !Newline
&& hasNestedBlockInlined(Previous
, Current
, Style
);
1692 moveStatePastFakeLParens(State
, Newline
);
1693 moveStatePastScopeCloser(State
);
1694 // Do not use CurrentState here, since the two functions before may change the
1696 bool AllowBreak
= !State
.Stack
.back().NoLineBreak
&&
1697 !State
.Stack
.back().NoLineBreakInOperand
;
1698 moveStatePastScopeOpener(State
, Newline
);
1699 moveStatePastFakeRParens(State
);
1701 if (Current
.is(TT_ObjCStringLiteral
) && State
.StartOfStringLiteral
== 0)
1702 State
.StartOfStringLiteral
= State
.Column
+ 1;
1703 if (Current
.is(TT_CSharpStringLiteral
) && State
.StartOfStringLiteral
== 0) {
1704 State
.StartOfStringLiteral
= State
.Column
+ 1;
1705 } else if (Current
.is(TT_TableGenMultiLineString
) &&
1706 State
.StartOfStringLiteral
== 0) {
1707 State
.StartOfStringLiteral
= State
.Column
+ 1;
1708 } else if (Current
.isStringLiteral() && State
.StartOfStringLiteral
== 0) {
1709 State
.StartOfStringLiteral
= State
.Column
;
1710 } else if (!Current
.isOneOf(tok::comment
, tok::identifier
, tok::hash
) &&
1711 !Current
.isStringLiteral()) {
1712 State
.StartOfStringLiteral
= 0;
1715 State
.Column
+= Current
.ColumnWidth
;
1716 State
.NextToken
= State
.NextToken
->Next
;
1717 // Verilog case labels are on the same unwrapped lines as the statements that
1718 // follow. TokenAnnotator identifies them and sets MustBreakBefore.
1719 // Indentation is taken care of here. A case label can only have 1 statement
1720 // in Verilog, so we don't have to worry about lines that follow.
1721 if (Style
.isVerilog() && State
.NextToken
&&
1722 State
.NextToken
->MustBreakBefore
&&
1723 Keywords
.isVerilogEndOfLabel(Current
)) {
1724 State
.FirstIndent
+= Style
.IndentWidth
;
1725 CurrentState
.Indent
= State
.FirstIndent
;
1729 handleEndOfLine(Current
, State
, DryRun
, AllowBreak
, Newline
);
1732 Current
.Role
->formatFromToken(State
, this, DryRun
);
1733 // If the previous has a special role, let it consume tokens as appropriate.
1734 // It is necessary to start at the previous token for the only implemented
1735 // role (comma separated list). That way, the decision whether or not to break
1736 // after the "{" is already done and both options are tried and evaluated.
1737 // FIXME: This is ugly, find a better way.
1738 if (Previous
&& Previous
->Role
)
1739 Penalty
+= Previous
->Role
->formatAfterToken(State
, this, DryRun
);
1744 void ContinuationIndenter::moveStatePastFakeLParens(LineState
&State
,
1746 const FormatToken
&Current
= *State
.NextToken
;
1747 if (Current
.FakeLParens
.empty())
1750 const FormatToken
*Previous
= Current
.getPreviousNonComment();
1752 // Don't add extra indentation for the first fake parenthesis after
1753 // 'return', assignments, opening <({[, or requires clauses. The indentation
1754 // for these cases is special cased.
1755 bool SkipFirstExtraIndent
=
1757 (Previous
->opensScope() ||
1758 Previous
->isOneOf(tok::semi
, tok::kw_return
, TT_RequiresClause
) ||
1759 (Previous
->getPrecedence() == prec::Assignment
&&
1760 Style
.AlignOperands
!= FormatStyle::OAS_DontAlign
) ||
1761 Previous
->is(TT_ObjCMethodExpr
));
1762 for (const auto &PrecedenceLevel
: llvm::reverse(Current
.FakeLParens
)) {
1763 const auto &CurrentState
= State
.Stack
.back();
1764 ParenState NewParenState
= CurrentState
;
1765 NewParenState
.Tok
= nullptr;
1766 NewParenState
.ContainsLineBreak
= false;
1767 NewParenState
.LastOperatorWrapped
= true;
1768 NewParenState
.IsChainedConditional
= false;
1769 NewParenState
.IsWrappedConditional
= false;
1770 NewParenState
.UnindentOperator
= false;
1771 NewParenState
.NoLineBreak
=
1772 NewParenState
.NoLineBreak
|| CurrentState
.NoLineBreakInOperand
;
1774 // Don't propagate AvoidBinPacking into subexpressions of arg/param lists.
1775 if (PrecedenceLevel
> prec::Comma
)
1776 NewParenState
.AvoidBinPacking
= false;
1778 // Indent from 'LastSpace' unless these are fake parentheses encapsulating
1779 // a builder type call after 'return' or, if the alignment after opening
1780 // brackets is disabled.
1781 if (!Current
.isTrailingComment() &&
1782 (Style
.AlignOperands
!= FormatStyle::OAS_DontAlign
||
1783 PrecedenceLevel
< prec::Assignment
) &&
1784 (!Previous
|| Previous
->isNot(tok::kw_return
) ||
1785 (Style
.Language
!= FormatStyle::LK_Java
&& PrecedenceLevel
> 0)) &&
1786 (Style
.AlignAfterOpenBracket
!= FormatStyle::BAS_DontAlign
||
1787 PrecedenceLevel
> prec::Comma
|| Current
.NestingLevel
== 0) &&
1788 (!Style
.isTableGen() ||
1789 (Previous
&& Previous
->isOneOf(TT_TableGenDAGArgListComma
,
1790 TT_TableGenDAGArgListCommaToBreak
)))) {
1791 NewParenState
.Indent
= std::max(
1792 std::max(State
.Column
, NewParenState
.Indent
), CurrentState
.LastSpace
);
1795 // Special case for generic selection expressions, its comma-separated
1796 // expressions are not aligned to the opening paren like regular calls, but
1797 // rather continuation-indented relative to the _Generic keyword.
1798 if (Previous
&& Previous
->endsSequence(tok::l_paren
, tok::kw__Generic
) &&
1799 State
.Stack
.size() > 1) {
1800 NewParenState
.Indent
= State
.Stack
[State
.Stack
.size() - 2].Indent
+
1801 Style
.ContinuationIndentWidth
;
1804 if ((shouldUnindentNextOperator(Current
) ||
1806 (PrecedenceLevel
== prec::Conditional
&&
1807 Previous
->is(tok::question
) && Previous
->is(TT_ConditionalExpr
)))) &&
1809 // If BreakBeforeBinaryOperators is set, un-indent a bit to account for
1810 // the operator and keep the operands aligned.
1811 if (Style
.AlignOperands
== FormatStyle::OAS_AlignAfterOperator
)
1812 NewParenState
.UnindentOperator
= true;
1813 // Mark indentation as alignment if the expression is aligned.
1814 if (Style
.AlignOperands
!= FormatStyle::OAS_DontAlign
)
1815 NewParenState
.IsAligned
= true;
1818 // Do not indent relative to the fake parentheses inserted for "." or "->".
1819 // This is a special case to make the following to statements consistent:
1820 // OuterFunction(InnerFunctionCall( // break
1821 // ParameterToInnerFunction));
1822 // OuterFunction(SomeObject.InnerFunctionCall( // break
1823 // ParameterToInnerFunction));
1824 if (PrecedenceLevel
> prec::Unknown
)
1825 NewParenState
.LastSpace
= std::max(NewParenState
.LastSpace
, State
.Column
);
1826 if (PrecedenceLevel
!= prec::Conditional
&&
1827 Current
.isNot(TT_UnaryOperator
) &&
1828 Style
.AlignAfterOpenBracket
!= FormatStyle::BAS_DontAlign
) {
1829 NewParenState
.StartOfFunctionCall
= State
.Column
;
1832 // Indent conditional expressions, unless they are chained "else-if"
1833 // conditionals. Never indent expression where the 'operator' is ',', ';' or
1834 // an assignment (i.e. *I <= prec::Assignment) as those have different
1835 // indentation rules. Indent other expression, unless the indentation needs
1837 if (PrecedenceLevel
== prec::Conditional
&& Previous
&&
1838 Previous
->is(tok::colon
) && Previous
->is(TT_ConditionalExpr
) &&
1839 &PrecedenceLevel
== &Current
.FakeLParens
.back() &&
1840 !CurrentState
.IsWrappedConditional
) {
1841 NewParenState
.IsChainedConditional
= true;
1842 NewParenState
.UnindentOperator
= State
.Stack
.back().UnindentOperator
;
1843 } else if (PrecedenceLevel
== prec::Conditional
||
1844 (!SkipFirstExtraIndent
&& PrecedenceLevel
> prec::Assignment
&&
1845 !Current
.isTrailingComment())) {
1846 NewParenState
.Indent
+= Style
.ContinuationIndentWidth
;
1848 if ((Previous
&& !Previous
->opensScope()) || PrecedenceLevel
!= prec::Comma
)
1849 NewParenState
.BreakBeforeParameter
= false;
1850 State
.Stack
.push_back(NewParenState
);
1851 SkipFirstExtraIndent
= false;
1855 void ContinuationIndenter::moveStatePastFakeRParens(LineState
&State
) {
1856 for (unsigned i
= 0, e
= State
.NextToken
->FakeRParens
; i
!= e
; ++i
) {
1857 unsigned VariablePos
= State
.Stack
.back().VariablePos
;
1858 if (State
.Stack
.size() == 1) {
1859 // Do not pop the last element.
1862 State
.Stack
.pop_back();
1863 State
.Stack
.back().VariablePos
= VariablePos
;
1866 if (State
.NextToken
->ClosesRequiresClause
&& Style
.IndentRequiresClause
) {
1867 // Remove the indentation of the requires clauses (which is not in Indent,
1868 // but in LastSpace).
1869 State
.Stack
.back().LastSpace
-= Style
.IndentWidth
;
1873 void ContinuationIndenter::moveStatePastScopeOpener(LineState
&State
,
1875 const FormatToken
&Current
= *State
.NextToken
;
1876 if (!Current
.opensScope())
1879 const auto &CurrentState
= State
.Stack
.back();
1881 // Don't allow '<' or '(' in C# generic type constraints to start new scopes.
1882 if (Current
.isOneOf(tok::less
, tok::l_paren
) &&
1883 CurrentState
.IsCSharpGenericTypeConstraint
) {
1887 if (Current
.MatchingParen
&& Current
.is(BK_Block
)) {
1888 moveStateToNewBlock(State
, Newline
);
1893 unsigned LastSpace
= CurrentState
.LastSpace
;
1894 bool AvoidBinPacking
;
1895 bool BreakBeforeParameter
= false;
1896 unsigned NestedBlockIndent
= std::max(CurrentState
.StartOfFunctionCall
,
1897 CurrentState
.NestedBlockIndent
);
1898 if (Current
.isOneOf(tok::l_brace
, TT_ArrayInitializerLSquare
) ||
1899 opensProtoMessageField(Current
, Style
)) {
1900 if (Current
.opensBlockOrBlockTypeList(Style
)) {
1901 NewIndent
= Style
.IndentWidth
+
1902 std::min(State
.Column
, CurrentState
.NestedBlockIndent
);
1903 } else if (Current
.is(tok::l_brace
)) {
1905 CurrentState
.LastSpace
+ Style
.BracedInitializerIndentWidth
.value_or(
1906 Style
.ContinuationIndentWidth
);
1908 NewIndent
= CurrentState
.LastSpace
+ Style
.ContinuationIndentWidth
;
1910 const FormatToken
*NextNonComment
= Current
.getNextNonComment();
1911 bool EndsInComma
= Current
.MatchingParen
&&
1912 Current
.MatchingParen
->Previous
&&
1913 Current
.MatchingParen
->Previous
->is(tok::comma
);
1914 AvoidBinPacking
= EndsInComma
|| Current
.is(TT_DictLiteral
) ||
1915 Style
.isProto() || !Style
.BinPackArguments
||
1916 (NextNonComment
&& NextNonComment
->isOneOf(
1917 TT_DesignatedInitializerPeriod
,
1918 TT_DesignatedInitializerLSquare
));
1919 BreakBeforeParameter
= EndsInComma
;
1920 if (Current
.ParameterCount
> 1)
1921 NestedBlockIndent
= std::max(NestedBlockIndent
, State
.Column
+ 1);
1924 Style
.ContinuationIndentWidth
+
1925 std::max(CurrentState
.LastSpace
, CurrentState
.StartOfFunctionCall
);
1927 if (Style
.isTableGen() && Current
.is(TT_TableGenDAGArgOpenerToBreak
) &&
1928 Style
.TableGenBreakInsideDAGArg
== FormatStyle::DAS_BreakElements
) {
1929 // For the case the next token is a TableGen DAGArg operator identifier
1930 // that is not marked to have a line break after it.
1931 // In this case the option DAS_BreakElements requires to align the
1932 // DAGArg elements to the operator.
1933 const FormatToken
*Next
= Current
.Next
;
1934 if (Next
&& Next
->is(TT_TableGenDAGArgOperatorID
))
1935 NewIndent
= State
.Column
+ Next
->TokenText
.size() + 2;
1938 // Ensure that different different brackets force relative alignment, e.g.:
1939 // void SomeFunction(vector< // break
1941 // FIXME: We likely want to do this for more combinations of brackets.
1942 if (Current
.is(tok::less
) && Current
.ParentBracket
== tok::l_paren
) {
1943 NewIndent
= std::max(NewIndent
, CurrentState
.Indent
);
1944 LastSpace
= std::max(LastSpace
, CurrentState
.Indent
);
1948 Current
.MatchingParen
&&
1949 Current
.MatchingParen
->getPreviousNonComment() &&
1950 Current
.MatchingParen
->getPreviousNonComment()->is(tok::comma
);
1952 // If ObjCBinPackProtocolList is unspecified, fall back to BinPackParameters
1953 // for backwards compatibility.
1954 bool ObjCBinPackProtocolList
=
1955 (Style
.ObjCBinPackProtocolList
== FormatStyle::BPS_Auto
&&
1956 Style
.BinPackParameters
== FormatStyle::BPPS_BinPack
) ||
1957 Style
.ObjCBinPackProtocolList
== FormatStyle::BPS_Always
;
1959 bool BinPackDeclaration
=
1960 (State
.Line
->Type
!= LT_ObjCDecl
&&
1961 Style
.BinPackParameters
== FormatStyle::BPPS_BinPack
) ||
1962 (State
.Line
->Type
== LT_ObjCDecl
&& ObjCBinPackProtocolList
);
1964 bool GenericSelection
=
1965 Current
.getPreviousNonComment() &&
1966 Current
.getPreviousNonComment()->is(tok::kw__Generic
);
1969 (CurrentState
.IsCSharpGenericTypeConstraint
) || GenericSelection
||
1970 (Style
.isJavaScript() && EndsInComma
) ||
1971 (State
.Line
->MustBeDeclaration
&& !BinPackDeclaration
) ||
1972 (!State
.Line
->MustBeDeclaration
&& !Style
.BinPackArguments
) ||
1973 (Style
.ExperimentalAutoDetectBinPacking
&&
1974 (Current
.is(PPK_OnePerLine
) ||
1975 (!BinPackInconclusiveFunctions
&& Current
.is(PPK_Inconclusive
))));
1977 if (Current
.is(TT_ObjCMethodExpr
) && Current
.MatchingParen
&&
1978 Style
.ObjCBreakBeforeNestedBlockParam
) {
1979 if (Style
.ColumnLimit
) {
1980 // If this '[' opens an ObjC call, determine whether all parameters fit
1981 // into one line and put one per line if they don't.
1982 if (getLengthToMatchingParen(Current
, State
.Stack
) + State
.Column
>
1983 getColumnLimit(State
)) {
1984 BreakBeforeParameter
= true;
1987 // For ColumnLimit = 0, we have to figure out whether there is or has to
1988 // be a line break within this call.
1989 for (const FormatToken
*Tok
= &Current
;
1990 Tok
&& Tok
!= Current
.MatchingParen
; Tok
= Tok
->Next
) {
1991 if (Tok
->MustBreakBefore
||
1992 (Tok
->CanBreakBefore
&& Tok
->NewlinesBefore
> 0)) {
1993 BreakBeforeParameter
= true;
2000 if (Style
.isJavaScript() && EndsInComma
)
2001 BreakBeforeParameter
= true;
2003 // Generally inherit NoLineBreak from the current scope to nested scope.
2004 // However, don't do this for non-empty nested blocks, dict literals and
2005 // array literals as these follow different indentation rules.
2007 Current
.Children
.empty() &&
2008 !Current
.isOneOf(TT_DictLiteral
, TT_ArrayInitializerLSquare
) &&
2009 (CurrentState
.NoLineBreak
|| CurrentState
.NoLineBreakInOperand
||
2010 (Current
.is(TT_TemplateOpener
) &&
2011 CurrentState
.ContainsUnwrappedBuilder
));
2012 State
.Stack
.push_back(
2013 ParenState(&Current
, NewIndent
, LastSpace
, AvoidBinPacking
, NoLineBreak
));
2014 auto &NewState
= State
.Stack
.back();
2015 NewState
.NestedBlockIndent
= NestedBlockIndent
;
2016 NewState
.BreakBeforeParameter
= BreakBeforeParameter
;
2017 NewState
.HasMultipleNestedBlocks
= (Current
.BlockParameterCount
> 1);
2019 if (Style
.BraceWrapping
.BeforeLambdaBody
&& Current
.Next
&&
2020 Current
.is(tok::l_paren
)) {
2021 // Search for any parameter that is a lambda.
2022 FormatToken
const *next
= Current
.Next
;
2024 if (next
->is(TT_LambdaLSquare
)) {
2025 NewState
.HasMultipleNestedBlocks
= true;
2032 NewState
.IsInsideObjCArrayLiteral
= Current
.is(TT_ArrayInitializerLSquare
) &&
2034 Current
.Previous
->is(tok::at
);
2037 void ContinuationIndenter::moveStatePastScopeCloser(LineState
&State
) {
2038 const FormatToken
&Current
= *State
.NextToken
;
2039 if (!Current
.closesScope())
2042 // If we encounter a closing ), ], } or >, we can remove a level from our
2044 if (State
.Stack
.size() > 1 &&
2045 (Current
.isOneOf(tok::r_paren
, tok::r_square
, TT_TemplateString
) ||
2046 (Current
.is(tok::r_brace
) && State
.NextToken
!= State
.Line
->First
) ||
2047 State
.NextToken
->is(TT_TemplateCloser
) ||
2048 State
.NextToken
->is(TT_TableGenListCloser
) ||
2049 (Current
.is(tok::greater
) && Current
.is(TT_DictLiteral
)))) {
2050 State
.Stack
.pop_back();
2053 auto &CurrentState
= State
.Stack
.back();
2055 // Reevaluate whether ObjC message arguments fit into one line.
2056 // If a receiver spans multiple lines, e.g.:
2057 // [[object block:^{
2060 // BreakBeforeParameter is calculated based on an incorrect assumption
2061 // (it is checked whether the whole expression fits into one line without
2062 // considering a line break inside a message receiver).
2063 // We check whether arguments fit after receiver scope closer (into the same
2065 if (CurrentState
.BreakBeforeParameter
&& Current
.MatchingParen
&&
2066 Current
.MatchingParen
->Previous
) {
2067 const FormatToken
&CurrentScopeOpener
= *Current
.MatchingParen
->Previous
;
2068 if (CurrentScopeOpener
.is(TT_ObjCMethodExpr
) &&
2069 CurrentScopeOpener
.MatchingParen
) {
2070 int NecessarySpaceInLine
=
2071 getLengthToMatchingParen(CurrentScopeOpener
, State
.Stack
) +
2072 CurrentScopeOpener
.TotalLength
- Current
.TotalLength
- 1;
2073 if (State
.Column
+ Current
.ColumnWidth
+ NecessarySpaceInLine
<=
2074 Style
.ColumnLimit
) {
2075 CurrentState
.BreakBeforeParameter
= false;
2080 if (Current
.is(tok::r_square
)) {
2081 // If this ends the array subscript expr, reset the corresponding value.
2082 const FormatToken
*NextNonComment
= Current
.getNextNonComment();
2083 if (NextNonComment
&& NextNonComment
->isNot(tok::l_square
))
2084 CurrentState
.StartOfArraySubscripts
= 0;
2088 void ContinuationIndenter::moveStateToNewBlock(LineState
&State
, bool NewLine
) {
2089 if (Style
.LambdaBodyIndentation
== FormatStyle::LBI_OuterScope
&&
2090 State
.NextToken
->is(TT_LambdaLBrace
) &&
2091 !State
.Line
->MightBeFunctionDecl
) {
2092 State
.Stack
.back().NestedBlockIndent
= State
.FirstIndent
;
2094 unsigned NestedBlockIndent
= State
.Stack
.back().NestedBlockIndent
;
2095 // ObjC block sometimes follow special indentation rules.
2096 unsigned NewIndent
=
2097 NestedBlockIndent
+ (State
.NextToken
->is(TT_ObjCBlockLBrace
)
2098 ? Style
.ObjCBlockIndentWidth
2099 : Style
.IndentWidth
);
2101 // Even when wrapping before lambda body, the left brace can still be added to
2102 // the same line. This occurs when checking whether the whole lambda body can
2103 // go on a single line. In this case we have to make sure there are no line
2104 // breaks in the body, otherwise we could just end up with a regular lambda
2105 // body without the brace wrapped.
2106 bool NoLineBreak
= Style
.BraceWrapping
.BeforeLambdaBody
&& !NewLine
&&
2107 State
.NextToken
->is(TT_LambdaLBrace
);
2109 State
.Stack
.push_back(ParenState(State
.NextToken
, NewIndent
,
2110 State
.Stack
.back().LastSpace
,
2111 /*AvoidBinPacking=*/true, NoLineBreak
));
2112 State
.Stack
.back().NestedBlockIndent
= NestedBlockIndent
;
2113 State
.Stack
.back().BreakBeforeParameter
= true;
2116 static unsigned getLastLineEndColumn(StringRef Text
, unsigned StartColumn
,
2118 encoding::Encoding Encoding
) {
2119 size_t LastNewlinePos
= Text
.find_last_of("\n");
2120 if (LastNewlinePos
== StringRef::npos
) {
2121 return StartColumn
+
2122 encoding::columnWidthWithTabs(Text
, StartColumn
, TabWidth
, Encoding
);
2124 return encoding::columnWidthWithTabs(Text
.substr(LastNewlinePos
),
2125 /*StartColumn=*/0, TabWidth
, Encoding
);
2129 unsigned ContinuationIndenter::reformatRawStringLiteral(
2130 const FormatToken
&Current
, LineState
&State
,
2131 const FormatStyle
&RawStringStyle
, bool DryRun
, bool Newline
) {
2132 unsigned StartColumn
= State
.Column
- Current
.ColumnWidth
;
2133 StringRef OldDelimiter
= *getRawStringDelimiter(Current
.TokenText
);
2134 StringRef NewDelimiter
=
2135 getCanonicalRawStringDelimiter(Style
, RawStringStyle
.Language
);
2136 if (NewDelimiter
.empty())
2137 NewDelimiter
= OldDelimiter
;
2138 // The text of a raw string is between the leading 'R"delimiter(' and the
2139 // trailing 'delimiter)"'.
2140 unsigned OldPrefixSize
= 3 + OldDelimiter
.size();
2141 unsigned OldSuffixSize
= 2 + OldDelimiter
.size();
2142 // We create a virtual text environment which expects a null-terminated
2143 // string, so we cannot use StringRef.
2144 std::string RawText
= std::string(
2145 Current
.TokenText
.substr(OldPrefixSize
).drop_back(OldSuffixSize
));
2146 if (NewDelimiter
!= OldDelimiter
) {
2147 // Don't update to the canonical delimiter 'deli' if ')deli"' occurs in the
2149 std::string CanonicalDelimiterSuffix
= (")" + NewDelimiter
+ "\"").str();
2150 if (StringRef(RawText
).contains(CanonicalDelimiterSuffix
))
2151 NewDelimiter
= OldDelimiter
;
2154 unsigned NewPrefixSize
= 3 + NewDelimiter
.size();
2155 unsigned NewSuffixSize
= 2 + NewDelimiter
.size();
2157 // The first start column is the column the raw text starts after formatting.
2158 unsigned FirstStartColumn
= StartColumn
+ NewPrefixSize
;
2160 // The next start column is the intended indentation a line break inside
2161 // the raw string at level 0. It is determined by the following rules:
2162 // - if the content starts on newline, it is one level more than the current
2164 // - if the content does not start on a newline, it is the first start
2166 // These rules have the advantage that the formatted content both does not
2167 // violate the rectangle rule and visually flows within the surrounding
2169 bool ContentStartsOnNewline
= Current
.TokenText
[OldPrefixSize
] == '\n';
2170 // If this token is the last parameter (checked by looking if it's followed by
2171 // `)` and is not on a newline, the base the indent off the line's nested
2172 // block indent. Otherwise, base the indent off the arguments indent, so we
2175 // fffffffffff(1, 2, 3, R"pb(
2179 // fffffffffff(1, 2, 3,
2185 // fffffffffff(1, 2, 3,
2191 unsigned CurrentIndent
=
2192 (!Newline
&& Current
.Next
&& Current
.Next
->is(tok::r_paren
))
2193 ? State
.Stack
.back().NestedBlockIndent
2194 : State
.Stack
.back().Indent
;
2195 unsigned NextStartColumn
= ContentStartsOnNewline
2196 ? CurrentIndent
+ Style
.IndentWidth
2199 // The last start column is the column the raw string suffix starts if it is
2200 // put on a newline.
2201 // The last start column is the intended indentation of the raw string postfix
2202 // if it is put on a newline. It is determined by the following rules:
2203 // - if the raw string prefix starts on a newline, it is the column where
2204 // that raw string prefix starts, and
2205 // - if the raw string prefix does not start on a newline, it is the current
2207 unsigned LastStartColumn
=
2208 Current
.NewlinesBefore
? FirstStartColumn
- NewPrefixSize
: CurrentIndent
;
2210 std::pair
<tooling::Replacements
, unsigned> Fixes
= internal::reformat(
2211 RawStringStyle
, RawText
, {tooling::Range(0, RawText
.size())},
2212 FirstStartColumn
, NextStartColumn
, LastStartColumn
, "<stdin>",
2213 /*Status=*/nullptr);
2215 auto NewCode
= applyAllReplacements(RawText
, Fixes
.first
);
2216 tooling::Replacements NoFixes
;
2218 return addMultilineToken(Current
, State
);
2220 if (NewDelimiter
!= OldDelimiter
) {
2221 // In 'R"delimiter(...', the delimiter starts 2 characters after the start
2223 SourceLocation PrefixDelimiterStart
=
2224 Current
.Tok
.getLocation().getLocWithOffset(2);
2225 auto PrefixErr
= Whitespaces
.addReplacement(tooling::Replacement(
2226 SourceMgr
, PrefixDelimiterStart
, OldDelimiter
.size(), NewDelimiter
));
2229 << "Failed to update the prefix delimiter of a raw string: "
2230 << llvm::toString(std::move(PrefixErr
)) << "\n";
2232 // In 'R"delimiter(...)delimiter"', the suffix delimiter starts at
2233 // position length - 1 - |delimiter|.
2234 SourceLocation SuffixDelimiterStart
=
2235 Current
.Tok
.getLocation().getLocWithOffset(Current
.TokenText
.size() -
2236 1 - OldDelimiter
.size());
2237 auto SuffixErr
= Whitespaces
.addReplacement(tooling::Replacement(
2238 SourceMgr
, SuffixDelimiterStart
, OldDelimiter
.size(), NewDelimiter
));
2241 << "Failed to update the suffix delimiter of a raw string: "
2242 << llvm::toString(std::move(SuffixErr
)) << "\n";
2245 SourceLocation OriginLoc
=
2246 Current
.Tok
.getLocation().getLocWithOffset(OldPrefixSize
);
2247 for (const tooling::Replacement
&Fix
: Fixes
.first
) {
2248 auto Err
= Whitespaces
.addReplacement(tooling::Replacement(
2249 SourceMgr
, OriginLoc
.getLocWithOffset(Fix
.getOffset()),
2250 Fix
.getLength(), Fix
.getReplacementText()));
2252 llvm::errs() << "Failed to reformat raw string: "
2253 << llvm::toString(std::move(Err
)) << "\n";
2257 unsigned RawLastLineEndColumn
= getLastLineEndColumn(
2258 *NewCode
, FirstStartColumn
, Style
.TabWidth
, Encoding
);
2259 State
.Column
= RawLastLineEndColumn
+ NewSuffixSize
;
2260 // Since we're updating the column to after the raw string literal here, we
2261 // have to manually add the penalty for the prefix R"delim( over the column
2263 unsigned PrefixExcessCharacters
=
2264 StartColumn
+ NewPrefixSize
> Style
.ColumnLimit
2265 ? StartColumn
+ NewPrefixSize
- Style
.ColumnLimit
2268 ContentStartsOnNewline
|| (NewCode
->find('\n') != std::string::npos
);
2270 // Break before further function parameters on all levels.
2271 for (ParenState
&Paren
: State
.Stack
)
2272 Paren
.BreakBeforeParameter
= true;
2274 return Fixes
.second
+ PrefixExcessCharacters
* Style
.PenaltyExcessCharacter
;
2277 unsigned ContinuationIndenter::addMultilineToken(const FormatToken
&Current
,
2279 // Break before further function parameters on all levels.
2280 for (ParenState
&Paren
: State
.Stack
)
2281 Paren
.BreakBeforeParameter
= true;
2283 unsigned ColumnsUsed
= State
.Column
;
2284 // We can only affect layout of the first and the last line, so the penalty
2285 // for all other lines is constant, and we ignore it.
2286 State
.Column
= Current
.LastLineColumnWidth
;
2288 if (ColumnsUsed
> getColumnLimit(State
))
2289 return Style
.PenaltyExcessCharacter
* (ColumnsUsed
- getColumnLimit(State
));
2293 unsigned ContinuationIndenter::handleEndOfLine(const FormatToken
&Current
,
2294 LineState
&State
, bool DryRun
,
2295 bool AllowBreak
, bool Newline
) {
2296 unsigned Penalty
= 0;
2297 // Compute the raw string style to use in case this is a raw string literal
2298 // that can be reformatted.
2299 auto RawStringStyle
= getRawStringStyle(Current
, State
);
2300 if (RawStringStyle
&& !Current
.Finalized
) {
2301 Penalty
= reformatRawStringLiteral(Current
, State
, *RawStringStyle
, DryRun
,
2303 } else if (Current
.IsMultiline
&& Current
.isNot(TT_BlockComment
)) {
2304 // Don't break multi-line tokens other than block comments and raw string
2305 // literals. Instead, just update the state.
2306 Penalty
= addMultilineToken(Current
, State
);
2307 } else if (State
.Line
->Type
!= LT_ImportStatement
) {
2308 // We generally don't break import statements.
2309 LineState OriginalState
= State
;
2311 // Whether we force the reflowing algorithm to stay strictly within the
2313 bool Strict
= false;
2314 // Whether the first non-strict attempt at reflowing did intentionally
2315 // exceed the column limit.
2316 bool Exceeded
= false;
2317 std::tie(Penalty
, Exceeded
) = breakProtrudingToken(
2318 Current
, State
, AllowBreak
, /*DryRun=*/true, Strict
);
2320 // If non-strict reflowing exceeds the column limit, try whether strict
2321 // reflowing leads to an overall lower penalty.
2322 LineState StrictState
= OriginalState
;
2323 unsigned StrictPenalty
=
2324 breakProtrudingToken(Current
, StrictState
, AllowBreak
,
2325 /*DryRun=*/true, /*Strict=*/true)
2327 Strict
= StrictPenalty
<= Penalty
;
2329 Penalty
= StrictPenalty
;
2330 State
= StrictState
;
2334 // If we're not in dry-run mode, apply the changes with the decision on
2335 // strictness made above.
2336 breakProtrudingToken(Current
, OriginalState
, AllowBreak
, /*DryRun=*/false,
2340 if (State
.Column
> getColumnLimit(State
)) {
2341 unsigned ExcessCharacters
= State
.Column
- getColumnLimit(State
);
2342 Penalty
+= Style
.PenaltyExcessCharacter
* ExcessCharacters
;
2347 // Returns the enclosing function name of a token, or the empty string if not
2349 static StringRef
getEnclosingFunctionName(const FormatToken
&Current
) {
2350 // Look for: 'function(' or 'function<templates>(' before Current.
2351 auto Tok
= Current
.getPreviousNonComment();
2352 if (!Tok
|| Tok
->isNot(tok::l_paren
))
2354 Tok
= Tok
->getPreviousNonComment();
2357 if (Tok
->is(TT_TemplateCloser
)) {
2358 Tok
= Tok
->MatchingParen
;
2360 Tok
= Tok
->getPreviousNonComment();
2362 if (!Tok
|| Tok
->isNot(tok::identifier
))
2364 return Tok
->TokenText
;
2367 std::optional
<FormatStyle
>
2368 ContinuationIndenter::getRawStringStyle(const FormatToken
&Current
,
2369 const LineState
&State
) {
2370 if (!Current
.isStringLiteral())
2371 return std::nullopt
;
2372 auto Delimiter
= getRawStringDelimiter(Current
.TokenText
);
2374 return std::nullopt
;
2375 auto RawStringStyle
= RawStringFormats
.getDelimiterStyle(*Delimiter
);
2376 if (!RawStringStyle
&& Delimiter
->empty()) {
2377 RawStringStyle
= RawStringFormats
.getEnclosingFunctionStyle(
2378 getEnclosingFunctionName(Current
));
2380 if (!RawStringStyle
)
2381 return std::nullopt
;
2382 RawStringStyle
->ColumnLimit
= getColumnLimit(State
);
2383 return RawStringStyle
;
2386 std::unique_ptr
<BreakableToken
>
2387 ContinuationIndenter::createBreakableToken(const FormatToken
&Current
,
2388 LineState
&State
, bool AllowBreak
) {
2389 unsigned StartColumn
= State
.Column
- Current
.ColumnWidth
;
2390 if (Current
.isStringLiteral()) {
2391 // Strings in JSON cannot be broken. Breaking strings in JavaScript is
2392 // disabled for now.
2393 if (Style
.isJson() || Style
.isJavaScript() || !Style
.BreakStringLiterals
||
2398 // Don't break string literals inside preprocessor directives (except for
2399 // #define directives, as their contents are stored in separate lines and
2400 // are not affected by this check).
2401 // This way we avoid breaking code with line directives and unknown
2402 // preprocessor directives that contain long string literals.
2403 if (State
.Line
->Type
== LT_PreprocessorDirective
)
2405 // Exempts unterminated string literals from line breaking. The user will
2406 // likely want to terminate the string before any line breaking is done.
2407 if (Current
.IsUnterminatedLiteral
)
2409 // Don't break string literals inside Objective-C array literals (doing so
2410 // raises the warning -Wobjc-string-concatenation).
2411 if (State
.Stack
.back().IsInsideObjCArrayLiteral
)
2414 // The "DPI"/"DPI-C" in SystemVerilog direct programming interface
2415 // imports/exports cannot be split, e.g.
2416 // `import "DPI" function foo();`
2417 // FIXME: make this use same infra as C++ import checks
2418 if (Style
.isVerilog() && Current
.Previous
&&
2419 Current
.Previous
->isOneOf(tok::kw_export
, Keywords
.kw_import
)) {
2422 StringRef Text
= Current
.TokenText
;
2424 // We need this to address the case where there is an unbreakable tail only
2425 // if certain other formatting decisions have been taken. The
2426 // UnbreakableTailLength of Current is an overapproximation in that case and
2427 // we need to be correct here.
2428 unsigned UnbreakableTailLength
= (State
.NextToken
&& canBreak(State
))
2430 : Current
.UnbreakableTailLength
;
2432 if (Style
.isVerilog() || Style
.Language
== FormatStyle::LK_Java
||
2433 Style
.isJavaScript() || Style
.isCSharp()) {
2434 BreakableStringLiteralUsingOperators::QuoteStyleType QuoteStyle
;
2435 if (Style
.isJavaScript() && Text
.starts_with("'") &&
2436 Text
.ends_with("'")) {
2437 QuoteStyle
= BreakableStringLiteralUsingOperators::SingleQuotes
;
2438 } else if (Style
.isCSharp() && Text
.starts_with("@\"") &&
2439 Text
.ends_with("\"")) {
2440 QuoteStyle
= BreakableStringLiteralUsingOperators::AtDoubleQuotes
;
2441 } else if (Text
.starts_with("\"") && Text
.ends_with("\"")) {
2442 QuoteStyle
= BreakableStringLiteralUsingOperators::DoubleQuotes
;
2446 return std::make_unique
<BreakableStringLiteralUsingOperators
>(
2447 Current
, QuoteStyle
,
2448 /*UnindentPlus=*/shouldUnindentNextOperator(Current
), StartColumn
,
2449 UnbreakableTailLength
, State
.Line
->InPPDirective
, Encoding
, Style
);
2454 // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
2455 // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
2456 // reduce the overhead) for each FormatToken, which is a string, so that we
2457 // don't run multiple checks here on the hot path.
2458 if ((Text
.ends_with(Postfix
= "\"") &&
2459 (Text
.starts_with(Prefix
= "@\"") || Text
.starts_with(Prefix
= "\"") ||
2460 Text
.starts_with(Prefix
= "u\"") ||
2461 Text
.starts_with(Prefix
= "U\"") ||
2462 Text
.starts_with(Prefix
= "u8\"") ||
2463 Text
.starts_with(Prefix
= "L\""))) ||
2464 (Text
.starts_with(Prefix
= "_T(\"") &&
2465 Text
.ends_with(Postfix
= "\")"))) {
2466 return std::make_unique
<BreakableStringLiteral
>(
2467 Current
, StartColumn
, Prefix
, Postfix
, UnbreakableTailLength
,
2468 State
.Line
->InPPDirective
, Encoding
, Style
);
2470 } else if (Current
.is(TT_BlockComment
)) {
2471 if (Style
.ReflowComments
== FormatStyle::RCS_Never
||
2472 // If a comment token switches formatting, like
2473 // /* clang-format on */, we don't want to break it further,
2474 // but we may still want to adjust its indentation.
2475 switchesFormatting(Current
)) {
2478 return std::make_unique
<BreakableBlockComment
>(
2479 Current
, StartColumn
, Current
.OriginalColumn
, !Current
.Previous
,
2480 State
.Line
->InPPDirective
, Encoding
, Style
, Whitespaces
.useCRLF());
2481 } else if (Current
.is(TT_LineComment
) &&
2482 (!Current
.Previous
||
2483 Current
.Previous
->isNot(TT_ImplicitStringLiteral
))) {
2484 bool RegularComments
= [&]() {
2485 for (const FormatToken
*T
= &Current
; T
&& T
->is(TT_LineComment
);
2487 if (!(T
->TokenText
.starts_with("//") || T
->TokenText
.starts_with("#")))
2492 if (Style
.ReflowComments
== FormatStyle::RCS_Never
||
2493 CommentPragmasRegex
.match(Current
.TokenText
.substr(2)) ||
2494 switchesFormatting(Current
) || !RegularComments
) {
2497 return std::make_unique
<BreakableLineCommentSection
>(
2498 Current
, StartColumn
, /*InPPDirective=*/false, Encoding
, Style
);
2503 std::pair
<unsigned, bool>
2504 ContinuationIndenter::breakProtrudingToken(const FormatToken
&Current
,
2505 LineState
&State
, bool AllowBreak
,
2506 bool DryRun
, bool Strict
) {
2507 std::unique_ptr
<const BreakableToken
> Token
=
2508 createBreakableToken(Current
, State
, AllowBreak
);
2511 assert(Token
->getLineCount() > 0);
2512 unsigned ColumnLimit
= getColumnLimit(State
);
2513 if (Current
.is(TT_LineComment
)) {
2514 // We don't insert backslashes when breaking line comments.
2515 ColumnLimit
= Style
.ColumnLimit
;
2517 if (ColumnLimit
== 0) {
2518 // To make the rest of the function easier set the column limit to the
2519 // maximum, if there should be no limit.
2520 ColumnLimit
= std::numeric_limits
<decltype(ColumnLimit
)>::max();
2522 if (Current
.UnbreakableTailLength
>= ColumnLimit
)
2524 // ColumnWidth was already accounted into State.Column before calling
2525 // breakProtrudingToken.
2526 unsigned StartColumn
= State
.Column
- Current
.ColumnWidth
;
2527 unsigned NewBreakPenalty
= Current
.isStringLiteral()
2528 ? Style
.PenaltyBreakString
2529 : Style
.PenaltyBreakComment
;
2530 // Stores whether we intentionally decide to let a line exceed the column
2532 bool Exceeded
= false;
2533 // Stores whether we introduce a break anywhere in the token.
2534 bool BreakInserted
= Token
->introducesBreakBeforeToken();
2535 // Store whether we inserted a new line break at the end of the previous
2537 bool NewBreakBefore
= false;
2538 // We use a conservative reflowing strategy. Reflow starts after a line is
2539 // broken or the corresponding whitespace compressed. Reflow ends as soon as a
2540 // line that doesn't get reflown with the previous line is reached.
2541 bool Reflow
= false;
2542 // Keep track of where we are in the token:
2543 // Where we are in the content of the current logical line.
2544 unsigned TailOffset
= 0;
2545 // The column number we're currently at.
2546 unsigned ContentStartColumn
=
2547 Token
->getContentStartColumn(0, /*Break=*/false);
2548 // The number of columns left in the current logical line after TailOffset.
2549 unsigned RemainingTokenColumns
=
2550 Token
->getRemainingLength(0, TailOffset
, ContentStartColumn
);
2551 // Adapt the start of the token, for example indent.
2553 Token
->adaptStartOfLine(0, Whitespaces
);
2555 unsigned ContentIndent
= 0;
2556 unsigned Penalty
= 0;
2557 LLVM_DEBUG(llvm::dbgs() << "Breaking protruding token at column "
2558 << StartColumn
<< ".\n");
2559 for (unsigned LineIndex
= 0, EndIndex
= Token
->getLineCount();
2560 LineIndex
!= EndIndex
; ++LineIndex
) {
2561 LLVM_DEBUG(llvm::dbgs()
2562 << " Line: " << LineIndex
<< " (Reflow: " << Reflow
<< ")\n");
2563 NewBreakBefore
= false;
2564 // If we did reflow the previous line, we'll try reflowing again. Otherwise
2565 // we'll start reflowing if the current line is broken or whitespace is
2567 bool TryReflow
= Reflow
;
2568 // Break the current token until we can fit the rest of the line.
2569 while (ContentStartColumn
+ RemainingTokenColumns
> ColumnLimit
) {
2570 LLVM_DEBUG(llvm::dbgs() << " Over limit, need: "
2571 << (ContentStartColumn
+ RemainingTokenColumns
)
2572 << ", space: " << ColumnLimit
2573 << ", reflown prefix: " << ContentStartColumn
2574 << ", offset in line: " << TailOffset
<< "\n");
2575 // If the current token doesn't fit, find the latest possible split in the
2576 // current line so that breaking at it will be under the column limit.
2577 // FIXME: Use the earliest possible split while reflowing to correctly
2578 // compress whitespace within a line.
2579 BreakableToken::Split Split
=
2580 Token
->getSplit(LineIndex
, TailOffset
, ColumnLimit
,
2581 ContentStartColumn
, CommentPragmasRegex
);
2582 if (Split
.first
== StringRef::npos
) {
2583 // No break opportunity - update the penalty and continue with the next
2585 if (LineIndex
< EndIndex
- 1) {
2586 // The last line's penalty is handled in addNextStateToQueue() or when
2587 // calling replaceWhitespaceAfterLastLine below.
2588 Penalty
+= Style
.PenaltyExcessCharacter
*
2589 (ContentStartColumn
+ RemainingTokenColumns
- ColumnLimit
);
2591 LLVM_DEBUG(llvm::dbgs() << " No break opportunity.\n");
2594 assert(Split
.first
!= 0);
2596 if (Token
->supportsReflow()) {
2597 // Check whether the next natural split point after the current one can
2598 // still fit the line, either because we can compress away whitespace,
2599 // or because the penalty the excess characters introduce is lower than
2600 // the break penalty.
2601 // We only do this for tokens that support reflowing, and thus allow us
2602 // to change the whitespace arbitrarily (e.g. comments).
2603 // Other tokens, like string literals, can be broken on arbitrary
2606 // First, compute the columns from TailOffset to the next possible split
2610 // // Some text that breaks
2613 // ^-------- to split columns
2615 // ^--------------- to next split columns
2616 unsigned ToSplitColumns
= Token
->getRangeLength(
2617 LineIndex
, TailOffset
, Split
.first
, ContentStartColumn
);
2618 LLVM_DEBUG(llvm::dbgs() << " ToSplit: " << ToSplitColumns
<< "\n");
2620 BreakableToken::Split NextSplit
= Token
->getSplit(
2621 LineIndex
, TailOffset
+ Split
.first
+ Split
.second
, ColumnLimit
,
2622 ContentStartColumn
+ ToSplitColumns
+ 1, CommentPragmasRegex
);
2623 // Compute the columns necessary to fit the next non-breakable sequence
2624 // into the current line.
2625 unsigned ToNextSplitColumns
= 0;
2626 if (NextSplit
.first
== StringRef::npos
) {
2627 ToNextSplitColumns
= Token
->getRemainingLength(LineIndex
, TailOffset
,
2628 ContentStartColumn
);
2630 ToNextSplitColumns
= Token
->getRangeLength(
2631 LineIndex
, TailOffset
,
2632 Split
.first
+ Split
.second
+ NextSplit
.first
, ContentStartColumn
);
2634 // Compress the whitespace between the break and the start of the next
2635 // unbreakable sequence.
2636 ToNextSplitColumns
=
2637 Token
->getLengthAfterCompression(ToNextSplitColumns
, Split
);
2638 LLVM_DEBUG(llvm::dbgs()
2639 << " ContentStartColumn: " << ContentStartColumn
<< "\n");
2640 LLVM_DEBUG(llvm::dbgs()
2641 << " ToNextSplit: " << ToNextSplitColumns
<< "\n");
2642 // If the whitespace compression makes us fit, continue on the current
2644 bool ContinueOnLine
=
2645 ContentStartColumn
+ ToNextSplitColumns
<= ColumnLimit
;
2646 unsigned ExcessCharactersPenalty
= 0;
2647 if (!ContinueOnLine
&& !Strict
) {
2648 // Similarly, if the excess characters' penalty is lower than the
2649 // penalty of introducing a new break, continue on the current line.
2650 ExcessCharactersPenalty
=
2651 (ContentStartColumn
+ ToNextSplitColumns
- ColumnLimit
) *
2652 Style
.PenaltyExcessCharacter
;
2653 LLVM_DEBUG(llvm::dbgs()
2654 << " Penalty excess: " << ExcessCharactersPenalty
2655 << "\n break : " << NewBreakPenalty
<< "\n");
2656 if (ExcessCharactersPenalty
< NewBreakPenalty
) {
2658 ContinueOnLine
= true;
2661 if (ContinueOnLine
) {
2662 LLVM_DEBUG(llvm::dbgs() << " Continuing on line...\n");
2663 // The current line fits after compressing the whitespace - reflow
2664 // the next line into it if possible.
2667 Token
->compressWhitespace(LineIndex
, TailOffset
, Split
,
2670 // When we continue on the same line, leave one space between content.
2671 ContentStartColumn
+= ToSplitColumns
+ 1;
2672 Penalty
+= ExcessCharactersPenalty
;
2673 TailOffset
+= Split
.first
+ Split
.second
;
2674 RemainingTokenColumns
= Token
->getRemainingLength(
2675 LineIndex
, TailOffset
, ContentStartColumn
);
2679 LLVM_DEBUG(llvm::dbgs() << " Breaking...\n");
2680 // Update the ContentIndent only if the current line was not reflown with
2681 // the previous line, since in that case the previous line should still
2682 // determine the ContentIndent. Also never intent the last line.
2684 ContentIndent
= Token
->getContentIndent(LineIndex
);
2685 LLVM_DEBUG(llvm::dbgs()
2686 << " ContentIndent: " << ContentIndent
<< "\n");
2687 ContentStartColumn
= ContentIndent
+ Token
->getContentStartColumn(
2688 LineIndex
, /*Break=*/true);
2690 unsigned NewRemainingTokenColumns
= Token
->getRemainingLength(
2691 LineIndex
, TailOffset
+ Split
.first
+ Split
.second
,
2692 ContentStartColumn
);
2693 if (NewRemainingTokenColumns
== 0) {
2694 // No content to indent.
2696 ContentStartColumn
=
2697 Token
->getContentStartColumn(LineIndex
, /*Break=*/true);
2698 NewRemainingTokenColumns
= Token
->getRemainingLength(
2699 LineIndex
, TailOffset
+ Split
.first
+ Split
.second
,
2700 ContentStartColumn
);
2703 // When breaking before a tab character, it may be moved by a few columns,
2704 // but will still be expanded to the next tab stop, so we don't save any
2706 if (NewRemainingTokenColumns
>= RemainingTokenColumns
) {
2707 // FIXME: Do we need to adjust the penalty?
2711 LLVM_DEBUG(llvm::dbgs() << " Breaking at: " << TailOffset
+ Split
.first
2712 << ", " << Split
.second
<< "\n");
2714 Token
->insertBreak(LineIndex
, TailOffset
, Split
, ContentIndent
,
2718 Penalty
+= NewBreakPenalty
;
2719 TailOffset
+= Split
.first
+ Split
.second
;
2720 RemainingTokenColumns
= NewRemainingTokenColumns
;
2721 BreakInserted
= true;
2722 NewBreakBefore
= true;
2724 // In case there's another line, prepare the state for the start of the next
2726 if (LineIndex
+ 1 != EndIndex
) {
2727 unsigned NextLineIndex
= LineIndex
+ 1;
2728 if (NewBreakBefore
) {
2729 // After breaking a line, try to reflow the next line into the current
2730 // one once RemainingTokenColumns fits.
2734 // We decided that we want to try reflowing the next line into the
2736 // We will now adjust the state as if the reflow is successful (in
2737 // preparation for the next line), and see whether that works. If we
2738 // decide that we cannot reflow, we will later reset the state to the
2739 // start of the next line.
2741 // As we did not continue breaking the line, RemainingTokenColumns is
2742 // known to fit after ContentStartColumn. Adapt ContentStartColumn to
2743 // the position at which we want to format the next line if we do
2745 // When we reflow, we need to add a space between the end of the current
2746 // line and the next line's start column.
2747 ContentStartColumn
+= RemainingTokenColumns
+ 1;
2748 // Get the split that we need to reflow next logical line into the end
2749 // of the current one; the split will include any leading whitespace of
2750 // the next logical line.
2751 BreakableToken::Split SplitBeforeNext
=
2752 Token
->getReflowSplit(NextLineIndex
, CommentPragmasRegex
);
2753 LLVM_DEBUG(llvm::dbgs()
2754 << " Size of reflown text: " << ContentStartColumn
2755 << "\n Potential reflow split: ");
2756 if (SplitBeforeNext
.first
!= StringRef::npos
) {
2757 LLVM_DEBUG(llvm::dbgs() << SplitBeforeNext
.first
<< ", "
2758 << SplitBeforeNext
.second
<< "\n");
2759 TailOffset
= SplitBeforeNext
.first
+ SplitBeforeNext
.second
;
2760 // If the rest of the next line fits into the current line below the
2761 // column limit, we can safely reflow.
2762 RemainingTokenColumns
= Token
->getRemainingLength(
2763 NextLineIndex
, TailOffset
, ContentStartColumn
);
2765 if (ContentStartColumn
+ RemainingTokenColumns
> ColumnLimit
) {
2766 LLVM_DEBUG(llvm::dbgs()
2767 << " Over limit after reflow, need: "
2768 << (ContentStartColumn
+ RemainingTokenColumns
)
2769 << ", space: " << ColumnLimit
2770 << ", reflown prefix: " << ContentStartColumn
2771 << ", offset in line: " << TailOffset
<< "\n");
2772 // If the whole next line does not fit, try to find a point in
2773 // the next line at which we can break so that attaching the part
2774 // of the next line to that break point onto the current line is
2775 // below the column limit.
2776 BreakableToken::Split Split
=
2777 Token
->getSplit(NextLineIndex
, TailOffset
, ColumnLimit
,
2778 ContentStartColumn
, CommentPragmasRegex
);
2779 if (Split
.first
== StringRef::npos
) {
2780 LLVM_DEBUG(llvm::dbgs() << " Did not find later break\n");
2783 // Check whether the first split point gets us below the column
2784 // limit. Note that we will execute this split below as part of
2785 // the normal token breaking and reflow logic within the line.
2786 unsigned ToSplitColumns
= Token
->getRangeLength(
2787 NextLineIndex
, TailOffset
, Split
.first
, ContentStartColumn
);
2788 if (ContentStartColumn
+ ToSplitColumns
> ColumnLimit
) {
2789 LLVM_DEBUG(llvm::dbgs() << " Next split protrudes, need: "
2790 << (ContentStartColumn
+ ToSplitColumns
)
2791 << ", space: " << ColumnLimit
);
2792 unsigned ExcessCharactersPenalty
=
2793 (ContentStartColumn
+ ToSplitColumns
- ColumnLimit
) *
2794 Style
.PenaltyExcessCharacter
;
2795 if (NewBreakPenalty
< ExcessCharactersPenalty
)
2801 LLVM_DEBUG(llvm::dbgs() << "not found.\n");
2805 // If we didn't reflow into the next line, the only space to consider is
2806 // the next logical line. Reset our state to match the start of the next
2809 ContentStartColumn
=
2810 Token
->getContentStartColumn(NextLineIndex
, /*Break=*/false);
2811 RemainingTokenColumns
= Token
->getRemainingLength(
2812 NextLineIndex
, TailOffset
, ContentStartColumn
);
2813 // Adapt the start of the token, for example indent.
2815 Token
->adaptStartOfLine(NextLineIndex
, Whitespaces
);
2817 // If we found a reflow split and have added a new break before the next
2818 // line, we are going to remove the line break at the start of the next
2819 // logical line. For example, here we'll add a new line break after
2820 // 'text', and subsequently delete the line break between 'that' and
2822 // // some text that
2827 // When adding the line break, we also added the penalty for it, so we
2828 // need to subtract that penalty again when we remove the line break due
2830 if (NewBreakBefore
) {
2831 assert(Penalty
>= NewBreakPenalty
);
2832 Penalty
-= NewBreakPenalty
;
2835 Token
->reflow(NextLineIndex
, Whitespaces
);
2840 BreakableToken::Split SplitAfterLastLine
=
2841 Token
->getSplitAfterLastLine(TailOffset
);
2842 if (SplitAfterLastLine
.first
!= StringRef::npos
) {
2843 LLVM_DEBUG(llvm::dbgs() << "Replacing whitespace after last line.\n");
2845 // We add the last line's penalty here, since that line is going to be split
2847 Penalty
+= Style
.PenaltyExcessCharacter
*
2848 (ContentStartColumn
+ RemainingTokenColumns
- ColumnLimit
);
2851 Token
->replaceWhitespaceAfterLastLine(TailOffset
, SplitAfterLastLine
,
2854 ContentStartColumn
=
2855 Token
->getContentStartColumn(Token
->getLineCount() - 1, /*Break=*/true);
2856 RemainingTokenColumns
= Token
->getRemainingLength(
2857 Token
->getLineCount() - 1,
2858 TailOffset
+ SplitAfterLastLine
.first
+ SplitAfterLastLine
.second
,
2859 ContentStartColumn
);
2862 State
.Column
= ContentStartColumn
+ RemainingTokenColumns
-
2863 Current
.UnbreakableTailLength
;
2865 if (BreakInserted
) {
2867 Token
->updateAfterBroken(Whitespaces
);
2869 // If we break the token inside a parameter list, we need to break before
2870 // the next parameter on all levels, so that the next parameter is clearly
2871 // visible. Line comments already introduce a break.
2872 if (Current
.isNot(TT_LineComment
))
2873 for (ParenState
&Paren
: State
.Stack
)
2874 Paren
.BreakBeforeParameter
= true;
2876 if (Current
.is(TT_BlockComment
))
2877 State
.NoContinuation
= true;
2879 State
.Stack
.back().LastSpace
= StartColumn
;
2882 Token
->updateNextToken(State
);
2884 return {Penalty
, Exceeded
};
2887 unsigned ContinuationIndenter::getColumnLimit(const LineState
&State
) const {
2888 // In preprocessor directives reserve two chars for trailing " \".
2889 return Style
.ColumnLimit
- (State
.Line
->InPPDirective
? 2 : 0);
2892 bool ContinuationIndenter::nextIsMultilineString(const LineState
&State
) {
2893 const FormatToken
&Current
= *State
.NextToken
;
2894 if (!Current
.isStringLiteral() || Current
.is(TT_ImplicitStringLiteral
))
2896 // We never consider raw string literals "multiline" for the purpose of
2897 // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased
2898 // (see TokenAnnotator::mustBreakBefore().
2899 if (Current
.TokenText
.starts_with("R\""))
2901 if (Current
.IsMultiline
)
2903 if (Current
.getNextNonComment() &&
2904 Current
.getNextNonComment()->isStringLiteral()) {
2905 return true; // Implicit concatenation.
2907 if (Style
.ColumnLimit
!= 0 && Style
.BreakStringLiterals
&&
2908 State
.Column
+ Current
.ColumnWidth
+ Current
.UnbreakableTailLength
>
2909 Style
.ColumnLimit
) {
2910 return true; // String will be split.
2915 } // namespace format
2916 } // namespace clang