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 Current starts a new parameter.
132 static bool startsNextParameter(const FormatToken
&Current
,
133 const FormatStyle
&Style
) {
134 const FormatToken
&Previous
= *Current
.Previous
;
135 if (Current
.is(TT_CtorInitializerComma
) &&
136 Style
.BreakConstructorInitializers
== FormatStyle::BCIS_BeforeComma
) {
139 if (Style
.Language
== FormatStyle::LK_Proto
&& Current
.is(TT_SelectorName
))
141 return Previous
.is(tok::comma
) && !Current
.isTrailingComment() &&
142 ((Previous
.isNot(TT_CtorInitializerComma
) ||
143 Style
.BreakConstructorInitializers
!=
144 FormatStyle::BCIS_BeforeComma
) &&
145 (Previous
.isNot(TT_InheritanceComma
) ||
146 Style
.BreakInheritanceList
!= FormatStyle::BILS_BeforeComma
));
149 static bool opensProtoMessageField(const FormatToken
&LessTok
,
150 const FormatStyle
&Style
) {
151 if (LessTok
.isNot(tok::less
))
153 return Style
.Language
== FormatStyle::LK_TextProto
||
154 (Style
.Language
== FormatStyle::LK_Proto
&&
155 (LessTok
.NestingLevel
> 0 ||
156 (LessTok
.Previous
&& LessTok
.Previous
->is(tok::equal
))));
159 // Returns the delimiter of a raw string literal, or std::nullopt if TokenText
160 // is not the text of a raw string literal. The delimiter could be the empty
161 // string. For example, the delimiter of R"deli(cont)deli" is deli.
162 static std::optional
<StringRef
> getRawStringDelimiter(StringRef TokenText
) {
163 if (TokenText
.size() < 5 // The smallest raw string possible is 'R"()"'.
164 || !TokenText
.startswith("R\"") || !TokenText
.endswith("\"")) {
168 // A raw string starts with 'R"<delimiter>(' and delimiter is ascii and has
169 // size at most 16 by the standard, so the first '(' must be among the first
171 size_t LParenPos
= TokenText
.substr(0, 19).find_first_of('(');
172 if (LParenPos
== StringRef::npos
)
174 StringRef Delimiter
= TokenText
.substr(2, LParenPos
- 2);
176 // Check that the string ends in ')Delimiter"'.
177 size_t RParenPos
= TokenText
.size() - Delimiter
.size() - 2;
178 if (TokenText
[RParenPos
] != ')')
180 if (!TokenText
.substr(RParenPos
+ 1).startswith(Delimiter
))
185 // Returns the canonical delimiter for \p Language, or the empty string if no
186 // canonical delimiter is specified.
188 getCanonicalRawStringDelimiter(const FormatStyle
&Style
,
189 FormatStyle::LanguageKind Language
) {
190 for (const auto &Format
: Style
.RawStringFormats
)
191 if (Format
.Language
== Language
)
192 return StringRef(Format
.CanonicalDelimiter
);
196 RawStringFormatStyleManager::RawStringFormatStyleManager(
197 const FormatStyle
&CodeStyle
) {
198 for (const auto &RawStringFormat
: CodeStyle
.RawStringFormats
) {
199 std::optional
<FormatStyle
> LanguageStyle
=
200 CodeStyle
.GetLanguageStyle(RawStringFormat
.Language
);
201 if (!LanguageStyle
) {
202 FormatStyle PredefinedStyle
;
203 if (!getPredefinedStyle(RawStringFormat
.BasedOnStyle
,
204 RawStringFormat
.Language
, &PredefinedStyle
)) {
205 PredefinedStyle
= getLLVMStyle();
206 PredefinedStyle
.Language
= RawStringFormat
.Language
;
208 LanguageStyle
= PredefinedStyle
;
210 LanguageStyle
->ColumnLimit
= CodeStyle
.ColumnLimit
;
211 for (StringRef Delimiter
: RawStringFormat
.Delimiters
)
212 DelimiterStyle
.insert({Delimiter
, *LanguageStyle
});
213 for (StringRef EnclosingFunction
: RawStringFormat
.EnclosingFunctions
)
214 EnclosingFunctionStyle
.insert({EnclosingFunction
, *LanguageStyle
});
218 std::optional
<FormatStyle
>
219 RawStringFormatStyleManager::getDelimiterStyle(StringRef Delimiter
) const {
220 auto It
= DelimiterStyle
.find(Delimiter
);
221 if (It
== DelimiterStyle
.end())
226 std::optional
<FormatStyle
>
227 RawStringFormatStyleManager::getEnclosingFunctionStyle(
228 StringRef EnclosingFunction
) const {
229 auto It
= EnclosingFunctionStyle
.find(EnclosingFunction
);
230 if (It
== EnclosingFunctionStyle
.end())
235 ContinuationIndenter::ContinuationIndenter(const FormatStyle
&Style
,
236 const AdditionalKeywords
&Keywords
,
237 const SourceManager
&SourceMgr
,
238 WhitespaceManager
&Whitespaces
,
239 encoding::Encoding Encoding
,
240 bool BinPackInconclusiveFunctions
)
241 : Style(Style
), Keywords(Keywords
), SourceMgr(SourceMgr
),
242 Whitespaces(Whitespaces
), Encoding(Encoding
),
243 BinPackInconclusiveFunctions(BinPackInconclusiveFunctions
),
244 CommentPragmasRegex(Style
.CommentPragmas
), RawStringFormats(Style
) {}
246 LineState
ContinuationIndenter::getInitialState(unsigned FirstIndent
,
247 unsigned FirstStartColumn
,
248 const AnnotatedLine
*Line
,
251 State
.FirstIndent
= FirstIndent
;
252 if (FirstStartColumn
&& Line
->First
->NewlinesBefore
== 0)
253 State
.Column
= FirstStartColumn
;
255 State
.Column
= FirstIndent
;
256 // With preprocessor directive indentation, the line starts on column 0
257 // since it's indented after the hash, but FirstIndent is set to the
258 // preprocessor indent.
259 if (Style
.IndentPPDirectives
== FormatStyle::PPDIS_AfterHash
&&
260 (Line
->Type
== LT_PreprocessorDirective
||
261 Line
->Type
== LT_ImportStatement
)) {
265 State
.NextToken
= Line
->First
;
266 State
.Stack
.push_back(ParenState(/*Tok=*/nullptr, FirstIndent
, FirstIndent
,
267 /*AvoidBinPacking=*/false,
268 /*NoLineBreak=*/false));
269 State
.NoContinuation
= false;
270 State
.StartOfStringLiteral
= 0;
271 State
.NoLineBreak
= false;
272 State
.StartOfLineLevel
= 0;
273 State
.LowestLevelOnLine
= 0;
274 State
.IgnoreStackForComparison
= false;
276 if (Style
.Language
== FormatStyle::LK_TextProto
) {
277 // We need this in order to deal with the bin packing of text fields at
279 auto &CurrentState
= State
.Stack
.back();
280 CurrentState
.AvoidBinPacking
= true;
281 CurrentState
.BreakBeforeParameter
= true;
282 CurrentState
.AlignColons
= false;
285 // The first token has already been indented and thus consumed.
286 moveStateToNextToken(State
, DryRun
, /*Newline=*/false);
290 bool ContinuationIndenter::canBreak(const LineState
&State
) {
291 const FormatToken
&Current
= *State
.NextToken
;
292 const FormatToken
&Previous
= *Current
.Previous
;
293 const auto &CurrentState
= State
.Stack
.back();
294 assert(&Previous
== Current
.Previous
);
295 if (!Current
.CanBreakBefore
&& !(CurrentState
.BreakBeforeClosingBrace
&&
296 Current
.closesBlockOrBlockTypeList(Style
))) {
299 // The opening "{" of a braced list has to be on the same line as the first
300 // element if it is nested in another braced init list or function call.
301 if (!Current
.MustBreakBefore
&& Previous
.is(tok::l_brace
) &&
302 Previous
.isNot(TT_DictLiteral
) && Previous
.is(BK_BracedInit
) &&
304 Previous
.Previous
->isOneOf(tok::l_brace
, tok::l_paren
, tok::comma
)) {
307 // This prevents breaks like:
309 // SomeParameter, OtherParameter).DoSomething(
311 // As they hide "DoSomething" and are generally bad for readability.
312 if (Previous
.opensScope() && Previous
.isNot(tok::l_brace
) &&
313 State
.LowestLevelOnLine
< State
.StartOfLineLevel
&&
314 State
.LowestLevelOnLine
< Current
.NestingLevel
) {
317 if (Current
.isMemberAccess() && CurrentState
.ContainsUnwrappedBuilder
)
320 // Don't create a 'hanging' indent if there are multiple blocks in a single
321 // statement and we are aligning lambda blocks to their signatures.
322 if (Previous
.is(tok::l_brace
) && State
.Stack
.size() > 1 &&
323 State
.Stack
[State
.Stack
.size() - 2].NestedBlockInlined
&&
324 State
.Stack
[State
.Stack
.size() - 2].HasMultipleNestedBlocks
&&
325 Style
.LambdaBodyIndentation
== FormatStyle::LBI_Signature
) {
329 // Don't break after very short return types (e.g. "void") as that is often
331 if (Current
.is(TT_FunctionDeclarationName
) && State
.Column
< 6) {
332 if (Style
.AlwaysBreakAfterReturnType
== FormatStyle::RTBS_None
)
336 // If binary operators are moved to the next line (including commas for some
337 // styles of constructor initializers), that's always ok.
338 if (!Current
.isOneOf(TT_BinaryOperator
, tok::comma
) &&
339 // Allow breaking opening brace of lambdas (when passed as function
340 // arguments) to a new line when BeforeLambdaBody brace wrapping is
342 (!Style
.BraceWrapping
.BeforeLambdaBody
||
343 Current
.isNot(TT_LambdaLBrace
)) &&
344 CurrentState
.NoLineBreakInOperand
) {
348 if (Previous
.is(tok::l_square
) && Previous
.is(TT_ObjCMethodExpr
))
351 if (Current
.is(TT_ConditionalExpr
) && Previous
.is(tok::r_paren
) &&
352 Previous
.MatchingParen
&& Previous
.MatchingParen
->Previous
&&
353 Previous
.MatchingParen
->Previous
->MatchingParen
&&
354 Previous
.MatchingParen
->Previous
->MatchingParen
->is(TT_LambdaLBrace
)) {
355 // We have a lambda within a conditional expression, allow breaking here.
356 assert(Previous
.MatchingParen
->Previous
->is(tok::r_brace
));
360 return !State
.NoLineBreak
&& !CurrentState
.NoLineBreak
;
363 bool ContinuationIndenter::mustBreak(const LineState
&State
) {
364 const FormatToken
&Current
= *State
.NextToken
;
365 const FormatToken
&Previous
= *Current
.Previous
;
366 const auto &CurrentState
= State
.Stack
.back();
367 if (Style
.BraceWrapping
.BeforeLambdaBody
&& Current
.CanBreakBefore
&&
368 Current
.is(TT_LambdaLBrace
) && Previous
.isNot(TT_LineComment
)) {
369 auto LambdaBodyLength
= getLengthToMatchingParen(Current
, State
.Stack
);
370 return LambdaBodyLength
> getColumnLimit(State
);
372 if (Current
.MustBreakBefore
||
373 (Current
.is(TT_InlineASMColon
) &&
374 (Style
.BreakBeforeInlineASMColon
== FormatStyle::BBIAS_Always
||
375 (Style
.BreakBeforeInlineASMColon
== FormatStyle::BBIAS_OnlyMultiline
&&
376 Style
.ColumnLimit
> 0)))) {
379 if (CurrentState
.BreakBeforeClosingBrace
&&
380 (Current
.closesBlockOrBlockTypeList(Style
) ||
381 (Current
.is(tok::r_brace
) &&
382 Current
.isBlockIndentedInitRBrace(Style
)))) {
385 if (CurrentState
.BreakBeforeClosingParen
&& Current
.is(tok::r_paren
))
387 if (Style
.Language
== FormatStyle::LK_ObjC
&&
388 Style
.ObjCBreakBeforeNestedBlockParam
&&
389 Current
.ObjCSelectorNameParts
> 1 &&
390 Current
.startsSequence(TT_SelectorName
, tok::colon
, tok::caret
)) {
393 // Avoid producing inconsistent states by requiring breaks where they are not
394 // permitted for C# generic type constraints.
395 if (CurrentState
.IsCSharpGenericTypeConstraint
&&
396 Previous
.isNot(TT_CSharpGenericTypeConstraintComma
)) {
399 if ((startsNextParameter(Current
, Style
) || Previous
.is(tok::semi
) ||
400 (Previous
.is(TT_TemplateCloser
) && Current
.is(TT_StartOfName
) &&
402 // FIXME: This is a temporary workaround for the case where clang-format
403 // sets BreakBeforeParameter to avoid bin packing and this creates a
404 // completely unnecessary line break after a template type that isn't
406 (Previous
.NestingLevel
== 1 || Style
.BinPackParameters
)) ||
407 (Style
.BreakBeforeTernaryOperators
&& Current
.is(TT_ConditionalExpr
) &&
408 Previous
.isNot(tok::question
)) ||
409 (!Style
.BreakBeforeTernaryOperators
&&
410 Previous
.is(TT_ConditionalExpr
))) &&
411 CurrentState
.BreakBeforeParameter
&& !Current
.isTrailingComment() &&
412 !Current
.isOneOf(tok::r_paren
, tok::r_brace
)) {
415 if (CurrentState
.IsChainedConditional
&&
416 ((Style
.BreakBeforeTernaryOperators
&& Current
.is(TT_ConditionalExpr
) &&
417 Current
.is(tok::colon
)) ||
418 (!Style
.BreakBeforeTernaryOperators
&& Previous
.is(TT_ConditionalExpr
) &&
419 Previous
.is(tok::colon
)))) {
422 if (((Previous
.is(TT_DictLiteral
) && Previous
.is(tok::l_brace
)) ||
423 (Previous
.is(TT_ArrayInitializerLSquare
) &&
424 Previous
.ParameterCount
> 1) ||
425 opensProtoMessageField(Previous
, Style
)) &&
426 Style
.ColumnLimit
> 0 &&
427 getLengthToMatchingParen(Previous
, State
.Stack
) + State
.Column
- 1 >
428 getColumnLimit(State
)) {
432 const FormatToken
&BreakConstructorInitializersToken
=
433 Style
.BreakConstructorInitializers
== FormatStyle::BCIS_AfterColon
436 if (BreakConstructorInitializersToken
.is(TT_CtorInitializerColon
) &&
437 (State
.Column
+ State
.Line
->Last
->TotalLength
- Previous
.TotalLength
>
438 getColumnLimit(State
) ||
439 CurrentState
.BreakBeforeParameter
) &&
440 (!Current
.isTrailingComment() || Current
.NewlinesBefore
> 0) &&
441 (Style
.AllowShortFunctionsOnASingleLine
!= FormatStyle::SFS_All
||
442 Style
.BreakConstructorInitializers
!= FormatStyle::BCIS_BeforeColon
||
443 Style
.ColumnLimit
!= 0)) {
447 if (Current
.is(TT_ObjCMethodExpr
) && Previous
.isNot(TT_SelectorName
) &&
448 State
.Line
->startsWith(TT_ObjCMethodSpecifier
)) {
451 if (Current
.is(TT_SelectorName
) && Previous
.isNot(tok::at
) &&
452 CurrentState
.ObjCSelectorNameFound
&& CurrentState
.BreakBeforeParameter
&&
453 (Style
.ObjCBreakBeforeNestedBlockParam
||
454 !Current
.startsSequence(TT_SelectorName
, tok::colon
, tok::caret
))) {
458 unsigned NewLineColumn
= getNewLineColumn(State
);
459 if (Current
.isMemberAccess() && Style
.ColumnLimit
!= 0 &&
460 State
.Column
+ getLengthToNextOperator(Current
) > Style
.ColumnLimit
&&
461 (State
.Column
> NewLineColumn
||
462 Current
.NestingLevel
< State
.StartOfLineLevel
)) {
466 if (startsSegmentOfBuilderTypeCall(Current
) &&
467 (CurrentState
.CallContinuation
!= 0 ||
468 CurrentState
.BreakBeforeParameter
) &&
469 // JavaScript is treated different here as there is a frequent pattern:
470 // SomeFunction(function() {
473 // FIXME: We should find a more generic solution to this problem.
474 !(State
.Column
<= NewLineColumn
&& Style
.isJavaScript()) &&
475 !(Previous
.closesScopeAfterBlock() && State
.Column
<= NewLineColumn
)) {
479 // If the template declaration spans multiple lines, force wrap before the
480 // function/class declaration.
481 if (Previous
.ClosesTemplateDeclaration
&& CurrentState
.BreakBeforeParameter
&&
482 Current
.CanBreakBefore
) {
486 if (State
.Line
->First
->isNot(tok::kw_enum
) && State
.Column
<= NewLineColumn
)
489 if (Style
.AlwaysBreakBeforeMultilineStrings
&&
490 (NewLineColumn
== State
.FirstIndent
+ Style
.ContinuationIndentWidth
||
491 Previous
.is(tok::comma
) || Current
.NestingLevel
< 2) &&
492 !Previous
.isOneOf(tok::kw_return
, tok::lessless
, tok::at
,
493 Keywords
.kw_dollar
) &&
494 !Previous
.isOneOf(TT_InlineASMColon
, TT_ConditionalExpr
) &&
495 nextIsMultilineString(State
)) {
499 // Using CanBreakBefore here and below takes care of the decision whether the
500 // current style uses wrapping before or after operators for the given
502 if (Previous
.is(TT_BinaryOperator
) && Current
.CanBreakBefore
) {
503 const auto PreviousPrecedence
= Previous
.getPrecedence();
504 if (PreviousPrecedence
!= prec::Assignment
&&
505 CurrentState
.BreakBeforeParameter
&& !Current
.isTrailingComment()) {
506 const bool LHSIsBinaryExpr
=
507 Previous
.Previous
&& Previous
.Previous
->EndsBinaryExpression
;
510 // If we need to break somewhere inside the LHS of a binary expression, we
511 // should also break after the operator. Otherwise, the formatting would
512 // hide the operator precedence, e.g. in:
513 // if (aaaaaaaaaaaaaa ==
514 // bbbbbbbbbbbbbb && c) {..
515 // For comparisons, we only apply this rule, if the LHS is a binary
516 // expression itself as otherwise, the line breaks seem superfluous.
517 // We need special cases for ">>" which we have split into two ">" while
518 // lexing in order to make template parsing easier.
519 const bool IsComparison
=
520 (PreviousPrecedence
== prec::Relational
||
521 PreviousPrecedence
== prec::Equality
||
522 PreviousPrecedence
== prec::Spaceship
) &&
524 Previous
.Previous
->isNot(TT_BinaryOperator
); // For >>.
528 } else if (Current
.is(TT_BinaryOperator
) && Current
.CanBreakBefore
&&
529 CurrentState
.BreakBeforeParameter
) {
533 // Same as above, but for the first "<<" operator.
534 if (Current
.is(tok::lessless
) && Current
.isNot(TT_OverloadedOperator
) &&
535 CurrentState
.BreakBeforeParameter
&& CurrentState
.FirstLessLess
== 0) {
539 if (Current
.NestingLevel
== 0 && !Current
.isTrailingComment()) {
540 // Always break after "template <...>"(*) and leading annotations. This is
541 // only for cases where the entire line does not fit on a single line as a
542 // different LineFormatter would be used otherwise.
543 // *: Except when another option interferes with that, like concepts.
544 if (Previous
.ClosesTemplateDeclaration
) {
545 if (Current
.is(tok::kw_concept
)) {
546 switch (Style
.BreakBeforeConceptDeclarations
) {
547 case FormatStyle::BBCDS_Allowed
:
549 case FormatStyle::BBCDS_Always
:
551 case FormatStyle::BBCDS_Never
:
555 if (Current
.is(TT_RequiresClause
)) {
556 switch (Style
.RequiresClausePosition
) {
557 case FormatStyle::RCPS_SingleLine
:
558 case FormatStyle::RCPS_WithPreceding
:
564 return Style
.AlwaysBreakTemplateDeclarations
!= FormatStyle::BTDS_No
;
566 if (Previous
.is(TT_FunctionAnnotationRParen
) &&
567 State
.Line
->Type
!= LT_PreprocessorDirective
) {
570 if (Previous
.is(TT_LeadingJavaAnnotation
) && Current
.isNot(tok::l_paren
) &&
571 Current
.isNot(TT_LeadingJavaAnnotation
)) {
576 if (Style
.isJavaScript() && Previous
.is(tok::r_paren
) &&
577 Previous
.is(TT_JavaAnnotation
)) {
578 // Break after the closing parenthesis of TypeScript decorators before
579 // functions, getters and setters.
580 static const llvm::StringSet
<> BreakBeforeDecoratedTokens
= {"get", "set",
582 if (BreakBeforeDecoratedTokens
.contains(Current
.TokenText
))
586 // If the return type spans multiple lines, wrap before the function name.
587 if (((Current
.is(TT_FunctionDeclarationName
) &&
588 !State
.Line
->ReturnTypeWrapped
&&
589 // Don't break before a C# function when no break after return type.
590 (!Style
.isCSharp() ||
591 Style
.AlwaysBreakAfterReturnType
!= FormatStyle::RTBS_None
) &&
592 // Don't always break between a JavaScript `function` and the function
594 !Style
.isJavaScript()) ||
595 (Current
.is(tok::kw_operator
) && Previous
.isNot(tok::coloncolon
))) &&
596 Previous
.isNot(tok::kw_template
) && CurrentState
.BreakBeforeParameter
) {
600 // The following could be precomputed as they do not depend on the state.
601 // However, as they should take effect only if the UnwrappedLine does not fit
602 // into the ColumnLimit, they are checked here in the ContinuationIndenter.
603 if (Style
.ColumnLimit
!= 0 && Previous
.is(BK_Block
) &&
604 Previous
.is(tok::l_brace
) &&
605 !Current
.isOneOf(tok::r_brace
, tok::comment
)) {
609 if (Current
.is(tok::lessless
) &&
610 ((Previous
.is(tok::identifier
) && Previous
.TokenText
== "endl") ||
611 (Previous
.Tok
.isLiteral() && (Previous
.TokenText
.endswith("\\n\"") ||
612 Previous
.TokenText
== "\'\\n\'")))) {
616 if (Previous
.is(TT_BlockComment
) && Previous
.IsMultiline
)
619 if (State
.NoContinuation
)
625 unsigned ContinuationIndenter::addTokenToState(LineState
&State
, bool Newline
,
627 unsigned ExtraSpaces
) {
628 const FormatToken
&Current
= *State
.NextToken
;
629 assert(State
.NextToken
->Previous
);
630 const FormatToken
&Previous
= *State
.NextToken
->Previous
;
632 assert(!State
.Stack
.empty());
633 State
.NoContinuation
= false;
635 if (Current
.is(TT_ImplicitStringLiteral
) &&
636 (!Previous
.Tok
.getIdentifierInfo() ||
637 Previous
.Tok
.getIdentifierInfo()->getPPKeywordID() ==
638 tok::pp_not_keyword
)) {
640 SourceMgr
.getSpellingColumnNumber(Current
.WhitespaceRange
.getEnd());
641 if (Current
.LastNewlineOffset
!= 0) {
642 // If there is a newline within this token, the final column will solely
643 // determined by the current end column.
644 State
.Column
= EndColumn
;
646 unsigned StartColumn
=
647 SourceMgr
.getSpellingColumnNumber(Current
.WhitespaceRange
.getBegin());
648 assert(EndColumn
>= StartColumn
);
649 State
.Column
+= EndColumn
- StartColumn
;
651 moveStateToNextToken(State
, DryRun
, /*Newline=*/false);
655 unsigned Penalty
= 0;
657 Penalty
= addTokenOnNewLine(State
, DryRun
);
659 addTokenOnCurrentLine(State
, DryRun
, ExtraSpaces
);
661 return moveStateToNextToken(State
, DryRun
, Newline
) + Penalty
;
664 void ContinuationIndenter::addTokenOnCurrentLine(LineState
&State
, bool DryRun
,
665 unsigned ExtraSpaces
) {
666 FormatToken
&Current
= *State
.NextToken
;
667 assert(State
.NextToken
->Previous
);
668 const FormatToken
&Previous
= *State
.NextToken
->Previous
;
669 auto &CurrentState
= State
.Stack
.back();
671 bool DisallowLineBreaksOnThisLine
=
672 Style
.LambdaBodyIndentation
== FormatStyle::LBI_Signature
&&
673 Style
.isCpp() && [&Current
] {
674 // Deal with lambda arguments in C++. The aim here is to ensure that we
675 // don't over-indent lambda function bodies when lambdas are passed as
676 // arguments to function calls. We do this by ensuring that either all
677 // arguments (including any lambdas) go on the same line as the function
678 // call, or we break before the first argument.
679 auto PrevNonComment
= Current
.getPreviousNonComment();
680 if (!PrevNonComment
|| PrevNonComment
->isNot(tok::l_paren
))
682 if (Current
.isOneOf(tok::comment
, tok::l_paren
, TT_LambdaLSquare
))
684 auto BlockParameterCount
= PrevNonComment
->BlockParameterCount
;
685 if (BlockParameterCount
== 0)
688 // Multiple lambdas in the same function call.
689 if (BlockParameterCount
> 1)
692 // A lambda followed by another arg.
693 if (!PrevNonComment
->Role
)
695 auto Comma
= PrevNonComment
->Role
->lastComma();
698 auto Next
= Comma
->getNextNonComment();
700 !Next
->isOneOf(TT_LambdaLSquare
, tok::l_brace
, tok::caret
);
703 if (DisallowLineBreaksOnThisLine
)
704 State
.NoLineBreak
= true;
706 if (Current
.is(tok::equal
) &&
707 (State
.Line
->First
->is(tok::kw_for
) || Current
.NestingLevel
== 0) &&
708 CurrentState
.VariablePos
== 0) {
709 CurrentState
.VariablePos
= State
.Column
;
710 // Move over * and & if they are bound to the variable name.
711 const FormatToken
*Tok
= &Previous
;
712 while (Tok
&& CurrentState
.VariablePos
>= Tok
->ColumnWidth
) {
713 CurrentState
.VariablePos
-= Tok
->ColumnWidth
;
714 if (Tok
->SpacesRequiredBefore
!= 0)
718 if (Previous
.PartOfMultiVariableDeclStmt
)
719 CurrentState
.LastSpace
= CurrentState
.VariablePos
;
722 unsigned Spaces
= Current
.SpacesRequiredBefore
+ ExtraSpaces
;
724 // Indent preprocessor directives after the hash if required.
725 int PPColumnCorrection
= 0;
726 if (Style
.IndentPPDirectives
== FormatStyle::PPDIS_AfterHash
&&
727 Previous
.is(tok::hash
) && State
.FirstIndent
> 0 &&
728 &Previous
== State
.Line
->First
&&
729 (State
.Line
->Type
== LT_PreprocessorDirective
||
730 State
.Line
->Type
== LT_ImportStatement
)) {
731 Spaces
+= State
.FirstIndent
;
733 // For preprocessor indent with tabs, State.Column will be 1 because of the
734 // hash. This causes second-level indents onward to have an extra space
735 // after the tabs. We avoid this misalignment by subtracting 1 from the
736 // column value passed to replaceWhitespace().
737 if (Style
.UseTab
!= FormatStyle::UT_Never
)
738 PPColumnCorrection
= -1;
742 Whitespaces
.replaceWhitespace(Current
, /*Newlines=*/0, Spaces
,
743 State
.Column
+ Spaces
+ PPColumnCorrection
);
746 // If "BreakBeforeInheritanceComma" mode, don't break within the inheritance
747 // declaration unless there is multiple inheritance.
748 if (Style
.BreakInheritanceList
== FormatStyle::BILS_BeforeComma
&&
749 Current
.is(TT_InheritanceColon
)) {
750 CurrentState
.NoLineBreak
= true;
752 if (Style
.BreakInheritanceList
== FormatStyle::BILS_AfterColon
&&
753 Previous
.is(TT_InheritanceColon
)) {
754 CurrentState
.NoLineBreak
= true;
757 if (Current
.is(TT_SelectorName
) && !CurrentState
.ObjCSelectorNameFound
) {
758 unsigned MinIndent
= std::max(
759 State
.FirstIndent
+ Style
.ContinuationIndentWidth
, CurrentState
.Indent
);
760 unsigned FirstColonPos
= State
.Column
+ Spaces
+ Current
.ColumnWidth
;
761 if (Current
.LongestObjCSelectorName
== 0)
762 CurrentState
.AlignColons
= false;
763 else if (MinIndent
+ Current
.LongestObjCSelectorName
> FirstColonPos
)
764 CurrentState
.ColonPos
= MinIndent
+ Current
.LongestObjCSelectorName
;
766 CurrentState
.ColonPos
= FirstColonPos
;
769 // In "AlwaysBreak" or "BlockIndent" mode, enforce wrapping directly after the
770 // parenthesis by disallowing any further line breaks if there is no line
771 // break after the opening parenthesis. Don't break if it doesn't conserve
773 if ((Style
.AlignAfterOpenBracket
== FormatStyle::BAS_AlwaysBreak
||
774 Style
.AlignAfterOpenBracket
== FormatStyle::BAS_BlockIndent
) &&
775 (Previous
.isOneOf(tok::l_paren
, TT_TemplateOpener
, tok::l_square
) ||
776 (Previous
.is(tok::l_brace
) && Previous
.isNot(BK_Block
) &&
777 Style
.Cpp11BracedListStyle
)) &&
778 State
.Column
> getNewLineColumn(State
) &&
779 (!Previous
.Previous
||
780 !Previous
.Previous
->isOneOf(TT_CastRParen
, tok::kw_for
, tok::kw_while
,
782 // Don't do this for simple (no expressions) one-argument function calls
783 // as that feels like needlessly wasting whitespace, e.g.:
788 // caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa))));
789 Current
.FakeLParens
.size() > 0 &&
790 Current
.FakeLParens
.back() > prec::Unknown
) {
791 CurrentState
.NoLineBreak
= true;
793 if (Previous
.is(TT_TemplateString
) && Previous
.opensScope())
794 CurrentState
.NoLineBreak
= true;
796 // Align following lines within parentheses / brackets if configured.
797 // Note: This doesn't apply to macro expansion lines, which are MACRO( , , )
798 // with args as children of the '(' and ',' tokens. It does not make sense to
799 // align the commas with the opening paren.
800 if (Style
.AlignAfterOpenBracket
!= FormatStyle::BAS_DontAlign
&&
801 !CurrentState
.IsCSharpGenericTypeConstraint
&& Previous
.opensScope() &&
802 Previous
.isNot(TT_ObjCMethodExpr
) && Previous
.isNot(TT_RequiresClause
) &&
803 !(Current
.MacroParent
&& Previous
.MacroParent
) &&
804 (Current
.isNot(TT_LineComment
) ||
805 Previous
.isOneOf(BK_BracedInit
, TT_VerilogMultiLineListLParen
))) {
806 CurrentState
.Indent
= State
.Column
+ Spaces
;
807 CurrentState
.IsAligned
= true;
809 if (CurrentState
.AvoidBinPacking
&& startsNextParameter(Current
, Style
))
810 CurrentState
.NoLineBreak
= true;
811 if (startsSegmentOfBuilderTypeCall(Current
) &&
812 State
.Column
> getNewLineColumn(State
)) {
813 CurrentState
.ContainsUnwrappedBuilder
= true;
816 if (Current
.is(TT_TrailingReturnArrow
) &&
817 Style
.Language
== FormatStyle::LK_Java
) {
818 CurrentState
.NoLineBreak
= true;
820 if (Current
.isMemberAccess() && Previous
.is(tok::r_paren
) &&
821 (Previous
.MatchingParen
&&
822 (Previous
.TotalLength
- Previous
.MatchingParen
->TotalLength
> 10))) {
823 // If there is a function call with long parameters, break before trailing
824 // calls. This prevents things like:
825 // EXPECT_CALL(SomeLongParameter).Times(
827 // We don't want to do this for short parameters as they can just be
829 CurrentState
.NoLineBreak
= true;
832 // Don't allow the RHS of an operator to be split over multiple lines unless
833 // there is a line-break right after the operator.
834 // Exclude relational operators, as there, it is always more desirable to
835 // have the LHS 'left' of the RHS.
836 const FormatToken
*P
= Current
.getPreviousNonComment();
837 if (Current
.isNot(tok::comment
) && P
&&
838 (P
->isOneOf(TT_BinaryOperator
, tok::comma
) ||
839 (P
->is(TT_ConditionalExpr
) && P
->is(tok::colon
))) &&
840 !P
->isOneOf(TT_OverloadedOperator
, TT_CtorInitializerComma
) &&
841 P
->getPrecedence() != prec::Assignment
&&
842 P
->getPrecedence() != prec::Relational
&&
843 P
->getPrecedence() != prec::Spaceship
) {
844 bool BreakBeforeOperator
=
845 P
->MustBreakBefore
|| P
->is(tok::lessless
) ||
846 (P
->is(TT_BinaryOperator
) &&
847 Style
.BreakBeforeBinaryOperators
!= FormatStyle::BOS_None
) ||
848 (P
->is(TT_ConditionalExpr
) && Style
.BreakBeforeTernaryOperators
);
849 // Don't do this if there are only two operands. In these cases, there is
850 // always a nice vertical separation between them and the extra line break
852 bool HasTwoOperands
= P
->OperatorIndex
== 0 && !P
->NextOperator
&&
853 P
->isNot(TT_ConditionalExpr
);
854 if ((!BreakBeforeOperator
&&
856 Style
.AlignOperands
!= FormatStyle::OAS_DontAlign
)) ||
857 (!CurrentState
.LastOperatorWrapped
&& BreakBeforeOperator
)) {
858 CurrentState
.NoLineBreakInOperand
= true;
862 State
.Column
+= Spaces
;
863 if (Current
.isNot(tok::comment
) && Previous
.is(tok::l_paren
) &&
865 (Previous
.Previous
->is(tok::kw_for
) || Previous
.Previous
->isIf())) {
866 // Treat the condition inside an if as if it was a second function
867 // parameter, i.e. let nested calls have a continuation indent.
868 CurrentState
.LastSpace
= State
.Column
;
869 CurrentState
.NestedBlockIndent
= State
.Column
;
870 } else if (!Current
.isOneOf(tok::comment
, tok::caret
) &&
871 ((Previous
.is(tok::comma
) &&
872 Previous
.isNot(TT_OverloadedOperator
)) ||
873 (Previous
.is(tok::colon
) && Previous
.is(TT_ObjCMethodExpr
)))) {
874 CurrentState
.LastSpace
= State
.Column
;
875 } else if (Previous
.is(TT_CtorInitializerColon
) &&
876 (!Current
.isTrailingComment() || Current
.NewlinesBefore
> 0) &&
877 Style
.BreakConstructorInitializers
==
878 FormatStyle::BCIS_AfterColon
) {
879 CurrentState
.Indent
= State
.Column
;
880 CurrentState
.LastSpace
= State
.Column
;
881 } else if (Previous
.isOneOf(TT_ConditionalExpr
, TT_CtorInitializerColon
)) {
882 CurrentState
.LastSpace
= State
.Column
;
883 } else if (Previous
.is(TT_BinaryOperator
) &&
884 ((Previous
.getPrecedence() != prec::Assignment
&&
885 (Previous
.isNot(tok::lessless
) || Previous
.OperatorIndex
!= 0 ||
886 Previous
.NextOperator
)) ||
887 Current
.StartsBinaryExpression
)) {
888 // Indent relative to the RHS of the expression unless this is a simple
889 // assignment without binary expression on the RHS.
890 if (Style
.BreakBeforeBinaryOperators
== FormatStyle::BOS_None
)
891 CurrentState
.LastSpace
= State
.Column
;
892 } else if (Previous
.is(TT_InheritanceColon
)) {
893 CurrentState
.Indent
= State
.Column
;
894 CurrentState
.LastSpace
= State
.Column
;
895 } else if (Current
.is(TT_CSharpGenericTypeConstraintColon
)) {
896 CurrentState
.ColonPos
= State
.Column
;
897 } else if (Previous
.opensScope()) {
898 // If a function has a trailing call, indent all parameters from the
899 // opening parenthesis. This avoids confusing indents like:
900 // OuterFunction(InnerFunctionCall( // break
901 // ParameterToInnerFunction)) // break
902 // .SecondInnerFunctionCall();
903 if (Previous
.MatchingParen
) {
904 const FormatToken
*Next
= Previous
.MatchingParen
->getNextNonComment();
905 if (Next
&& Next
->isMemberAccess() && State
.Stack
.size() > 1 &&
906 State
.Stack
[State
.Stack
.size() - 2].CallContinuation
== 0) {
907 CurrentState
.LastSpace
= State
.Column
;
913 unsigned ContinuationIndenter::addTokenOnNewLine(LineState
&State
,
915 FormatToken
&Current
= *State
.NextToken
;
916 assert(State
.NextToken
->Previous
);
917 const FormatToken
&Previous
= *State
.NextToken
->Previous
;
918 auto &CurrentState
= State
.Stack
.back();
920 // Extra penalty that needs to be added because of the way certain line
921 // breaks are chosen.
922 unsigned Penalty
= 0;
924 const FormatToken
*PreviousNonComment
= Current
.getPreviousNonComment();
925 const FormatToken
*NextNonComment
= Previous
.getNextNonComment();
927 NextNonComment
= &Current
;
928 // The first line break on any NestingLevel causes an extra penalty in order
929 // prefer similar line breaks.
930 if (!CurrentState
.ContainsLineBreak
)
932 CurrentState
.ContainsLineBreak
= true;
934 Penalty
+= State
.NextToken
->SplitPenalty
;
936 // Breaking before the first "<<" is generally not desirable if the LHS is
937 // short. Also always add the penalty if the LHS is split over multiple lines
938 // to avoid unnecessary line breaks that just work around this penalty.
939 if (NextNonComment
->is(tok::lessless
) && CurrentState
.FirstLessLess
== 0 &&
940 (State
.Column
<= Style
.ColumnLimit
/ 3 ||
941 CurrentState
.BreakBeforeParameter
)) {
942 Penalty
+= Style
.PenaltyBreakFirstLessLess
;
945 State
.Column
= getNewLineColumn(State
);
947 // Add Penalty proportional to amount of whitespace away from FirstColumn
948 // This tends to penalize several lines that are far-right indented,
949 // and prefers a line-break prior to such a block, e.g:
952 // member(value), looooooooooooooooong_member(
953 // looooooooooong_call(param_1, param_2, param_3))
957 // looooooooooooooooong_member(
958 // looooooooooong_call(param_1, param_2, param_3))
959 if (State
.Column
> State
.FirstIndent
) {
961 Style
.PenaltyIndentedWhitespace
* (State
.Column
- State
.FirstIndent
);
964 // Indent nested blocks relative to this column, unless in a very specific
965 // JavaScript special case where:
967 // var loooooong_name =
972 // is common and should be formatted like a free-standing function. The same
973 // goes for wrapping before the lambda return type arrow.
974 if (Current
.isNot(TT_TrailingReturnArrow
) &&
975 (!Style
.isJavaScript() || Current
.NestingLevel
!= 0 ||
976 !PreviousNonComment
|| PreviousNonComment
->isNot(tok::equal
) ||
977 !Current
.isOneOf(Keywords
.kw_async
, Keywords
.kw_function
))) {
978 CurrentState
.NestedBlockIndent
= State
.Column
;
981 if (NextNonComment
->isMemberAccess()) {
982 if (CurrentState
.CallContinuation
== 0)
983 CurrentState
.CallContinuation
= State
.Column
;
984 } else if (NextNonComment
->is(TT_SelectorName
)) {
985 if (!CurrentState
.ObjCSelectorNameFound
) {
986 if (NextNonComment
->LongestObjCSelectorName
== 0) {
987 CurrentState
.AlignColons
= false;
989 CurrentState
.ColonPos
=
990 (shouldIndentWrappedSelectorName(Style
, State
.Line
->Type
)
991 ? std::max(CurrentState
.Indent
,
992 State
.FirstIndent
+ Style
.ContinuationIndentWidth
)
993 : CurrentState
.Indent
) +
994 std::max(NextNonComment
->LongestObjCSelectorName
,
995 NextNonComment
->ColumnWidth
);
997 } else if (CurrentState
.AlignColons
&&
998 CurrentState
.ColonPos
<= NextNonComment
->ColumnWidth
) {
999 CurrentState
.ColonPos
= State
.Column
+ NextNonComment
->ColumnWidth
;
1001 } else if (PreviousNonComment
&& PreviousNonComment
->is(tok::colon
) &&
1002 PreviousNonComment
->isOneOf(TT_ObjCMethodExpr
, TT_DictLiteral
)) {
1003 // FIXME: This is hacky, find a better way. The problem is that in an ObjC
1004 // method expression, the block should be aligned to the line starting it,
1006 // [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason
1010 // Thus, we set LastSpace of the next higher NestingLevel, to which we move
1011 // when we consume all of the "}"'s FakeRParens at the "{".
1012 if (State
.Stack
.size() > 1) {
1013 State
.Stack
[State
.Stack
.size() - 2].LastSpace
=
1014 std::max(CurrentState
.LastSpace
, CurrentState
.Indent
) +
1015 Style
.ContinuationIndentWidth
;
1019 if ((PreviousNonComment
&&
1020 PreviousNonComment
->isOneOf(tok::comma
, tok::semi
) &&
1021 !CurrentState
.AvoidBinPacking
) ||
1022 Previous
.is(TT_BinaryOperator
)) {
1023 CurrentState
.BreakBeforeParameter
= false;
1025 if (PreviousNonComment
&&
1026 (PreviousNonComment
->isOneOf(TT_TemplateCloser
, TT_JavaAnnotation
) ||
1027 PreviousNonComment
->ClosesRequiresClause
) &&
1028 Current
.NestingLevel
== 0) {
1029 CurrentState
.BreakBeforeParameter
= false;
1031 if (NextNonComment
->is(tok::question
) ||
1032 (PreviousNonComment
&& PreviousNonComment
->is(tok::question
))) {
1033 CurrentState
.BreakBeforeParameter
= true;
1035 if (Current
.is(TT_BinaryOperator
) && Current
.CanBreakBefore
)
1036 CurrentState
.BreakBeforeParameter
= false;
1039 unsigned MaxEmptyLinesToKeep
= Style
.MaxEmptyLinesToKeep
+ 1;
1040 if (Current
.is(tok::r_brace
) && Current
.MatchingParen
&&
1041 // Only strip trailing empty lines for l_braces that have children, i.e.
1042 // for function expressions (lambdas, arrows, etc).
1043 !Current
.MatchingParen
->Children
.empty()) {
1044 // lambdas and arrow functions are expressions, thus their r_brace is not
1045 // on its own line, and thus not covered by UnwrappedLineFormatter's logic
1046 // about removing empty lines on closing blocks. Special case them here.
1047 MaxEmptyLinesToKeep
= 1;
1050 std::max(1u, std::min(Current
.NewlinesBefore
, MaxEmptyLinesToKeep
));
1051 bool ContinuePPDirective
=
1052 State
.Line
->InPPDirective
&& State
.Line
->Type
!= LT_ImportStatement
;
1053 Whitespaces
.replaceWhitespace(Current
, Newlines
, State
.Column
, State
.Column
,
1054 CurrentState
.IsAligned
, ContinuePPDirective
);
1057 if (!Current
.isTrailingComment())
1058 CurrentState
.LastSpace
= State
.Column
;
1059 if (Current
.is(tok::lessless
)) {
1060 // If we are breaking before a "<<", we always want to indent relative to
1061 // RHS. This is necessary only for "<<", as we special-case it and don't
1062 // always indent relative to the RHS.
1063 CurrentState
.LastSpace
+= 3; // 3 -> width of "<< ".
1066 State
.StartOfLineLevel
= Current
.NestingLevel
;
1067 State
.LowestLevelOnLine
= Current
.NestingLevel
;
1069 // Any break on this level means that the parent level has been broken
1070 // and we need to avoid bin packing there.
1071 bool NestedBlockSpecialCase
=
1072 (!Style
.isCpp() && Current
.is(tok::r_brace
) && State
.Stack
.size() > 1 &&
1073 State
.Stack
[State
.Stack
.size() - 2].NestedBlockInlined
) ||
1074 (Style
.Language
== FormatStyle::LK_ObjC
&& Current
.is(tok::r_brace
) &&
1075 State
.Stack
.size() > 1 && !Style
.ObjCBreakBeforeNestedBlockParam
);
1076 // Do not force parameter break for statements with requires expressions.
1077 NestedBlockSpecialCase
=
1078 NestedBlockSpecialCase
||
1079 (Current
.MatchingParen
&&
1080 Current
.MatchingParen
->is(TT_RequiresExpressionLBrace
));
1081 if (!NestedBlockSpecialCase
) {
1082 auto ParentLevelIt
= std::next(State
.Stack
.rbegin());
1083 if (Style
.LambdaBodyIndentation
== FormatStyle::LBI_OuterScope
&&
1084 Current
.MatchingParen
&& Current
.MatchingParen
->is(TT_LambdaLBrace
)) {
1085 // If the first character on the new line is a lambda's closing brace, the
1086 // stack still contains that lambda's parenthesis. As such, we need to
1087 // recurse further down the stack than usual to find the parenthesis level
1088 // containing the lambda, which is where we want to set
1089 // BreakBeforeParameter.
1091 // We specifically special case "OuterScope"-formatted lambdas here
1092 // because, when using that setting, breaking before the parameter
1093 // directly following the lambda is particularly unsightly. However, when
1094 // "OuterScope" is not set, the logic to find the parent parenthesis level
1095 // still appears to be sometimes incorrect. It has not been fixed yet
1096 // because it would lead to significant changes in existing behaviour.
1098 // TODO: fix the non-"OuterScope" case too.
1099 auto FindCurrentLevel
= [&](const auto &It
) {
1100 return std::find_if(It
, State
.Stack
.rend(), [](const auto &PState
) {
1101 return PState
.Tok
!= nullptr; // Ignore fake parens.
1104 auto MaybeIncrement
= [&](const auto &It
) {
1105 return It
!= State
.Stack
.rend() ? std::next(It
) : It
;
1107 auto LambdaLevelIt
= FindCurrentLevel(State
.Stack
.rbegin());
1108 auto LevelContainingLambdaIt
=
1109 FindCurrentLevel(MaybeIncrement(LambdaLevelIt
));
1110 ParentLevelIt
= MaybeIncrement(LevelContainingLambdaIt
);
1112 for (auto I
= ParentLevelIt
, E
= State
.Stack
.rend(); I
!= E
; ++I
)
1113 I
->BreakBeforeParameter
= true;
1116 if (PreviousNonComment
&&
1117 !PreviousNonComment
->isOneOf(tok::comma
, tok::colon
, tok::semi
) &&
1118 ((PreviousNonComment
->isNot(TT_TemplateCloser
) &&
1119 !PreviousNonComment
->ClosesRequiresClause
) ||
1120 Current
.NestingLevel
!= 0) &&
1121 !PreviousNonComment
->isOneOf(
1122 TT_BinaryOperator
, TT_FunctionAnnotationRParen
, TT_JavaAnnotation
,
1123 TT_LeadingJavaAnnotation
) &&
1124 Current
.isNot(TT_BinaryOperator
) && !PreviousNonComment
->opensScope() &&
1125 // We don't want to enforce line breaks for subsequent arguments just
1126 // because we have been forced to break before a lambda body.
1127 (!Style
.BraceWrapping
.BeforeLambdaBody
||
1128 Current
.isNot(TT_LambdaLBrace
))) {
1129 CurrentState
.BreakBeforeParameter
= true;
1132 // If we break after { or the [ of an array initializer, we should also break
1133 // before the corresponding } or ].
1134 if (PreviousNonComment
&&
1135 (PreviousNonComment
->isOneOf(tok::l_brace
, TT_ArrayInitializerLSquare
) ||
1136 opensProtoMessageField(*PreviousNonComment
, Style
))) {
1137 CurrentState
.BreakBeforeClosingBrace
= true;
1140 if (PreviousNonComment
&& PreviousNonComment
->is(tok::l_paren
)) {
1141 CurrentState
.BreakBeforeClosingParen
=
1142 Style
.AlignAfterOpenBracket
== FormatStyle::BAS_BlockIndent
;
1145 if (CurrentState
.AvoidBinPacking
) {
1146 // If we are breaking after '(', '{', '<', or this is the break after a ':'
1147 // to start a member initializer list in a constructor, this should not
1148 // be considered bin packing unless the relevant AllowAll option is false or
1149 // this is a dict/object literal.
1150 bool PreviousIsBreakingCtorInitializerColon
=
1151 PreviousNonComment
&& PreviousNonComment
->is(TT_CtorInitializerColon
) &&
1152 Style
.BreakConstructorInitializers
== FormatStyle::BCIS_AfterColon
;
1153 bool AllowAllConstructorInitializersOnNextLine
=
1154 Style
.PackConstructorInitializers
== FormatStyle::PCIS_NextLine
||
1155 Style
.PackConstructorInitializers
== FormatStyle::PCIS_NextLineOnly
;
1156 if (!(Previous
.isOneOf(tok::l_paren
, tok::l_brace
, TT_BinaryOperator
) ||
1157 PreviousIsBreakingCtorInitializerColon
) ||
1158 (!Style
.AllowAllParametersOfDeclarationOnNextLine
&&
1159 State
.Line
->MustBeDeclaration
) ||
1160 (!Style
.AllowAllArgumentsOnNextLine
&&
1161 !State
.Line
->MustBeDeclaration
) ||
1162 (!AllowAllConstructorInitializersOnNextLine
&&
1163 PreviousIsBreakingCtorInitializerColon
) ||
1164 Previous
.is(TT_DictLiteral
)) {
1165 CurrentState
.BreakBeforeParameter
= true;
1168 // If we are breaking after a ':' to start a member initializer list,
1169 // and we allow all arguments on the next line, we should not break
1170 // before the next parameter.
1171 if (PreviousIsBreakingCtorInitializerColon
&&
1172 AllowAllConstructorInitializersOnNextLine
) {
1173 CurrentState
.BreakBeforeParameter
= false;
1180 unsigned ContinuationIndenter::getNewLineColumn(const LineState
&State
) {
1181 if (!State
.NextToken
|| !State
.NextToken
->Previous
)
1184 FormatToken
&Current
= *State
.NextToken
;
1185 const auto &CurrentState
= State
.Stack
.back();
1187 if (CurrentState
.IsCSharpGenericTypeConstraint
&&
1188 Current
.isNot(TT_CSharpGenericTypeConstraint
)) {
1189 return CurrentState
.ColonPos
+ 2;
1192 const FormatToken
&Previous
= *Current
.Previous
;
1193 // If we are continuing an expression, we want to use the continuation indent.
1194 unsigned ContinuationIndent
=
1195 std::max(CurrentState
.LastSpace
, CurrentState
.Indent
) +
1196 Style
.ContinuationIndentWidth
;
1197 const FormatToken
*PreviousNonComment
= Current
.getPreviousNonComment();
1198 const FormatToken
*NextNonComment
= Previous
.getNextNonComment();
1199 if (!NextNonComment
)
1200 NextNonComment
= &Current
;
1202 // Java specific bits.
1203 if (Style
.Language
== FormatStyle::LK_Java
&&
1204 Current
.isOneOf(Keywords
.kw_implements
, Keywords
.kw_extends
)) {
1205 return std::max(CurrentState
.LastSpace
,
1206 CurrentState
.Indent
+ Style
.ContinuationIndentWidth
);
1209 // Indentation of the statement following a Verilog case label is taken care
1210 // of in moveStateToNextToken.
1211 if (Style
.isVerilog() && Keywords
.isVerilogEndOfLabel(Previous
))
1212 return State
.FirstIndent
;
1214 if (Style
.BreakBeforeBraces
== FormatStyle::BS_Whitesmiths
&&
1215 State
.Line
->First
->is(tok::kw_enum
)) {
1216 return (Style
.IndentWidth
* State
.Line
->First
->IndentLevel
) +
1220 if ((NextNonComment
->is(tok::l_brace
) && NextNonComment
->is(BK_Block
)) ||
1221 (Style
.isVerilog() && Keywords
.isVerilogBegin(*NextNonComment
))) {
1222 if (Current
.NestingLevel
== 0 ||
1223 (Style
.LambdaBodyIndentation
== FormatStyle::LBI_OuterScope
&&
1224 State
.NextToken
->is(TT_LambdaLBrace
))) {
1225 return State
.FirstIndent
;
1227 return CurrentState
.Indent
;
1229 if ((Current
.isOneOf(tok::r_brace
, tok::r_square
) ||
1230 (Current
.is(tok::greater
) &&
1231 (Style
.Language
== FormatStyle::LK_Proto
||
1232 Style
.Language
== FormatStyle::LK_TextProto
))) &&
1233 State
.Stack
.size() > 1) {
1234 if (Current
.closesBlockOrBlockTypeList(Style
))
1235 return State
.Stack
[State
.Stack
.size() - 2].NestedBlockIndent
;
1236 if (Current
.MatchingParen
&& Current
.MatchingParen
->is(BK_BracedInit
))
1237 return State
.Stack
[State
.Stack
.size() - 2].LastSpace
;
1238 return State
.FirstIndent
;
1240 // Indent a closing parenthesis at the previous level if followed by a semi,
1241 // const, or opening brace. This allows indentations such as:
1255 if (Current
.is(tok::r_paren
) && State
.Stack
.size() > 1 &&
1257 Current
.Next
->isOneOf(tok::semi
, tok::kw_const
, tok::l_brace
))) {
1258 return State
.Stack
[State
.Stack
.size() - 2].LastSpace
;
1260 if (Style
.AlignAfterOpenBracket
== FormatStyle::BAS_BlockIndent
&&
1261 (Current
.is(tok::r_paren
) ||
1262 (Current
.is(tok::r_brace
) &&
1263 Current
.MatchingParen
->is(BK_BracedInit
))) &&
1264 State
.Stack
.size() > 1) {
1265 return State
.Stack
[State
.Stack
.size() - 2].LastSpace
;
1267 if (NextNonComment
->is(TT_TemplateString
) && NextNonComment
->closesScope())
1268 return State
.Stack
[State
.Stack
.size() - 2].LastSpace
;
1269 // Field labels in a nested type should be aligned to the brace. For example
1271 // optional int32 b = 2 [(foo_options) = {aaaaaaaaaaaaaaaaaaa: 123,
1272 // bbbbbbbbbbbbbbbbbbbbbbbb:"baz"}];
1273 // For Verilog, a quote following a brace is treated as an identifier. And
1274 // Both braces and colons get annotated as TT_DictLiteral. So we have to
1276 if (Current
.is(tok::identifier
) && Current
.Next
&&
1277 (!Style
.isVerilog() || Current
.Next
->is(tok::colon
)) &&
1278 (Current
.Next
->is(TT_DictLiteral
) ||
1279 ((Style
.Language
== FormatStyle::LK_Proto
||
1280 Style
.Language
== FormatStyle::LK_TextProto
) &&
1281 Current
.Next
->isOneOf(tok::less
, tok::l_brace
)))) {
1282 return CurrentState
.Indent
;
1284 if (NextNonComment
->is(TT_ObjCStringLiteral
) &&
1285 State
.StartOfStringLiteral
!= 0) {
1286 return State
.StartOfStringLiteral
- 1;
1288 if (NextNonComment
->isStringLiteral() && State
.StartOfStringLiteral
!= 0)
1289 return State
.StartOfStringLiteral
;
1290 if (NextNonComment
->is(tok::lessless
) && CurrentState
.FirstLessLess
!= 0)
1291 return CurrentState
.FirstLessLess
;
1292 if (NextNonComment
->isMemberAccess()) {
1293 if (CurrentState
.CallContinuation
== 0)
1294 return ContinuationIndent
;
1295 return CurrentState
.CallContinuation
;
1297 if (CurrentState
.QuestionColumn
!= 0 &&
1298 ((NextNonComment
->is(tok::colon
) &&
1299 NextNonComment
->is(TT_ConditionalExpr
)) ||
1300 Previous
.is(TT_ConditionalExpr
))) {
1301 if (((NextNonComment
->is(tok::colon
) && NextNonComment
->Next
&&
1302 !NextNonComment
->Next
->FakeLParens
.empty() &&
1303 NextNonComment
->Next
->FakeLParens
.back() == prec::Conditional
) ||
1304 (Previous
.is(tok::colon
) && !Current
.FakeLParens
.empty() &&
1305 Current
.FakeLParens
.back() == prec::Conditional
)) &&
1306 !CurrentState
.IsWrappedConditional
) {
1307 // NOTE: we may tweak this slightly:
1308 // * not remove the 'lead' ContinuationIndentWidth
1309 // * always un-indent by the operator when
1310 // BreakBeforeTernaryOperators=true
1311 unsigned Indent
= CurrentState
.Indent
;
1312 if (Style
.AlignOperands
!= FormatStyle::OAS_DontAlign
)
1313 Indent
-= Style
.ContinuationIndentWidth
;
1314 if (Style
.BreakBeforeTernaryOperators
&& CurrentState
.UnindentOperator
)
1318 return CurrentState
.QuestionColumn
;
1320 if (Previous
.is(tok::comma
) && CurrentState
.VariablePos
!= 0)
1321 return CurrentState
.VariablePos
;
1322 if (Current
.is(TT_RequiresClause
)) {
1323 if (Style
.IndentRequiresClause
)
1324 return CurrentState
.Indent
+ Style
.IndentWidth
;
1325 switch (Style
.RequiresClausePosition
) {
1326 case FormatStyle::RCPS_OwnLine
:
1327 case FormatStyle::RCPS_WithFollowing
:
1328 return CurrentState
.Indent
;
1333 if (NextNonComment
->isOneOf(TT_CtorInitializerColon
, TT_InheritanceColon
,
1334 TT_InheritanceComma
)) {
1335 return State
.FirstIndent
+ Style
.ConstructorInitializerIndentWidth
;
1337 if ((PreviousNonComment
&&
1338 (PreviousNonComment
->ClosesTemplateDeclaration
||
1339 PreviousNonComment
->ClosesRequiresClause
||
1340 (PreviousNonComment
->is(TT_AttributeMacro
) &&
1341 Current
.isNot(tok::l_paren
)) ||
1342 PreviousNonComment
->isOneOf(
1343 TT_AttributeRParen
, TT_AttributeSquare
, TT_FunctionAnnotationRParen
,
1344 TT_JavaAnnotation
, TT_LeadingJavaAnnotation
))) ||
1345 (!Style
.IndentWrappedFunctionNames
&&
1346 NextNonComment
->isOneOf(tok::kw_operator
, TT_FunctionDeclarationName
))) {
1347 return std::max(CurrentState
.LastSpace
, CurrentState
.Indent
);
1349 if (NextNonComment
->is(TT_SelectorName
)) {
1350 if (!CurrentState
.ObjCSelectorNameFound
) {
1351 unsigned MinIndent
= CurrentState
.Indent
;
1352 if (shouldIndentWrappedSelectorName(Style
, State
.Line
->Type
)) {
1353 MinIndent
= std::max(MinIndent
,
1354 State
.FirstIndent
+ Style
.ContinuationIndentWidth
);
1356 // If LongestObjCSelectorName is 0, we are indenting the first
1357 // part of an ObjC selector (or a selector component which is
1358 // not colon-aligned due to block formatting).
1360 // Otherwise, we are indenting a subsequent part of an ObjC
1361 // selector which should be colon-aligned to the longest
1362 // component of the ObjC selector.
1364 // In either case, we want to respect Style.IndentWrappedFunctionNames.
1366 std::max(NextNonComment
->LongestObjCSelectorName
,
1367 NextNonComment
->ColumnWidth
) -
1368 NextNonComment
->ColumnWidth
;
1370 if (!CurrentState
.AlignColons
)
1371 return CurrentState
.Indent
;
1372 if (CurrentState
.ColonPos
> NextNonComment
->ColumnWidth
)
1373 return CurrentState
.ColonPos
- NextNonComment
->ColumnWidth
;
1374 return CurrentState
.Indent
;
1376 if (NextNonComment
->is(tok::colon
) && NextNonComment
->is(TT_ObjCMethodExpr
))
1377 return CurrentState
.ColonPos
;
1378 if (NextNonComment
->is(TT_ArraySubscriptLSquare
)) {
1379 if (CurrentState
.StartOfArraySubscripts
!= 0) {
1380 return CurrentState
.StartOfArraySubscripts
;
1381 } else if (Style
.isCSharp()) { // C# allows `["key"] = value` inside object
1383 return CurrentState
.Indent
;
1385 return ContinuationIndent
;
1388 // OpenMP clauses want to get additional indentation when they are pushed onto
1390 if (State
.Line
->InPragmaDirective
) {
1391 FormatToken
*PragmaType
= State
.Line
->First
->Next
->Next
;
1392 if (PragmaType
&& PragmaType
->TokenText
.equals("omp"))
1393 return CurrentState
.Indent
+ Style
.ContinuationIndentWidth
;
1396 // This ensure that we correctly format ObjC methods calls without inputs,
1397 // i.e. where the last element isn't selector like: [callee method];
1398 if (NextNonComment
->is(tok::identifier
) && NextNonComment
->FakeRParens
== 0 &&
1399 NextNonComment
->Next
&& NextNonComment
->Next
->is(TT_ObjCMethodExpr
)) {
1400 return CurrentState
.Indent
;
1403 if (NextNonComment
->isOneOf(TT_StartOfName
, TT_PointerOrReference
) ||
1404 Previous
.isOneOf(tok::coloncolon
, tok::equal
, TT_JsTypeColon
)) {
1405 return ContinuationIndent
;
1407 if (PreviousNonComment
&& PreviousNonComment
->is(tok::colon
) &&
1408 PreviousNonComment
->isOneOf(TT_ObjCMethodExpr
, TT_DictLiteral
)) {
1409 return ContinuationIndent
;
1411 if (NextNonComment
->is(TT_CtorInitializerComma
))
1412 return CurrentState
.Indent
;
1413 if (PreviousNonComment
&& PreviousNonComment
->is(TT_CtorInitializerColon
) &&
1414 Style
.BreakConstructorInitializers
== FormatStyle::BCIS_AfterColon
) {
1415 return CurrentState
.Indent
;
1417 if (PreviousNonComment
&& PreviousNonComment
->is(TT_InheritanceColon
) &&
1418 Style
.BreakInheritanceList
== FormatStyle::BILS_AfterColon
) {
1419 return CurrentState
.Indent
;
1421 if (Previous
.is(tok::r_paren
) && !Current
.isBinaryOperator() &&
1422 !Current
.isOneOf(tok::colon
, tok::comment
)) {
1423 return ContinuationIndent
;
1425 if (Current
.is(TT_ProtoExtensionLSquare
))
1426 return CurrentState
.Indent
;
1427 if (Current
.isBinaryOperator() && CurrentState
.UnindentOperator
) {
1428 return CurrentState
.Indent
- Current
.Tok
.getLength() -
1429 Current
.SpacesRequiredBefore
;
1431 if (Current
.isOneOf(tok::comment
, TT_BlockComment
, TT_LineComment
) &&
1432 NextNonComment
->isBinaryOperator() && CurrentState
.UnindentOperator
) {
1433 return CurrentState
.Indent
- NextNonComment
->Tok
.getLength() -
1434 NextNonComment
->SpacesRequiredBefore
;
1436 if (CurrentState
.Indent
== State
.FirstIndent
&& PreviousNonComment
&&
1437 !PreviousNonComment
->isOneOf(tok::r_brace
, TT_CtorInitializerComma
)) {
1438 // Ensure that we fall back to the continuation indent width instead of
1439 // just flushing continuations left.
1440 return CurrentState
.Indent
+ Style
.ContinuationIndentWidth
;
1442 return CurrentState
.Indent
;
1445 static bool hasNestedBlockInlined(const FormatToken
*Previous
,
1446 const FormatToken
&Current
,
1447 const FormatStyle
&Style
) {
1448 if (Previous
->isNot(tok::l_paren
))
1450 if (Previous
->ParameterCount
> 1)
1453 // Also a nested block if contains a lambda inside function with 1 parameter.
1454 return Style
.BraceWrapping
.BeforeLambdaBody
&& Current
.is(TT_LambdaLSquare
);
1457 unsigned ContinuationIndenter::moveStateToNextToken(LineState
&State
,
1458 bool DryRun
, bool Newline
) {
1459 assert(State
.Stack
.size());
1460 const FormatToken
&Current
= *State
.NextToken
;
1461 auto &CurrentState
= State
.Stack
.back();
1463 if (Current
.is(TT_CSharpGenericTypeConstraint
))
1464 CurrentState
.IsCSharpGenericTypeConstraint
= true;
1465 if (Current
.isOneOf(tok::comma
, TT_BinaryOperator
))
1466 CurrentState
.NoLineBreakInOperand
= false;
1467 if (Current
.isOneOf(TT_InheritanceColon
, TT_CSharpGenericTypeConstraintColon
))
1468 CurrentState
.AvoidBinPacking
= true;
1469 if (Current
.is(tok::lessless
) && Current
.isNot(TT_OverloadedOperator
)) {
1470 if (CurrentState
.FirstLessLess
== 0)
1471 CurrentState
.FirstLessLess
= State
.Column
;
1473 CurrentState
.LastOperatorWrapped
= Newline
;
1475 if (Current
.is(TT_BinaryOperator
) && Current
.isNot(tok::lessless
))
1476 CurrentState
.LastOperatorWrapped
= Newline
;
1477 if (Current
.is(TT_ConditionalExpr
) && Current
.Previous
&&
1478 Current
.Previous
->isNot(TT_ConditionalExpr
)) {
1479 CurrentState
.LastOperatorWrapped
= Newline
;
1481 if (Current
.is(TT_ArraySubscriptLSquare
) &&
1482 CurrentState
.StartOfArraySubscripts
== 0) {
1483 CurrentState
.StartOfArraySubscripts
= State
.Column
;
1486 auto IsWrappedConditional
= [](const FormatToken
&Tok
) {
1487 if (!(Tok
.is(TT_ConditionalExpr
) && Tok
.is(tok::question
)))
1489 if (Tok
.MustBreakBefore
)
1492 const FormatToken
*Next
= Tok
.getNextNonComment();
1493 return Next
&& Next
->MustBreakBefore
;
1495 if (IsWrappedConditional(Current
))
1496 CurrentState
.IsWrappedConditional
= true;
1497 if (Style
.BreakBeforeTernaryOperators
&& Current
.is(tok::question
))
1498 CurrentState
.QuestionColumn
= State
.Column
;
1499 if (!Style
.BreakBeforeTernaryOperators
&& Current
.isNot(tok::colon
)) {
1500 const FormatToken
*Previous
= Current
.Previous
;
1501 while (Previous
&& Previous
->isTrailingComment())
1502 Previous
= Previous
->Previous
;
1503 if (Previous
&& Previous
->is(tok::question
))
1504 CurrentState
.QuestionColumn
= State
.Column
;
1506 if (!Current
.opensScope() && !Current
.closesScope() &&
1507 Current
.isNot(TT_PointerOrReference
)) {
1508 State
.LowestLevelOnLine
=
1509 std::min(State
.LowestLevelOnLine
, Current
.NestingLevel
);
1511 if (Current
.isMemberAccess())
1512 CurrentState
.StartOfFunctionCall
= !Current
.NextOperator
? 0 : State
.Column
;
1513 if (Current
.is(TT_SelectorName
))
1514 CurrentState
.ObjCSelectorNameFound
= true;
1515 if (Current
.is(TT_CtorInitializerColon
) &&
1516 Style
.BreakConstructorInitializers
!= FormatStyle::BCIS_AfterColon
) {
1517 // Indent 2 from the column, so:
1518 // SomeClass::SomeClass()
1519 // : First(...), ...
1522 CurrentState
.Indent
= State
.Column
+ (Style
.BreakConstructorInitializers
==
1523 FormatStyle::BCIS_BeforeComma
1526 CurrentState
.NestedBlockIndent
= CurrentState
.Indent
;
1527 if (Style
.PackConstructorInitializers
> FormatStyle::PCIS_BinPack
) {
1528 CurrentState
.AvoidBinPacking
= true;
1529 CurrentState
.BreakBeforeParameter
=
1530 Style
.ColumnLimit
> 0 &&
1531 Style
.PackConstructorInitializers
!= FormatStyle::PCIS_NextLine
&&
1532 Style
.PackConstructorInitializers
!= FormatStyle::PCIS_NextLineOnly
;
1534 CurrentState
.BreakBeforeParameter
= false;
1537 if (Current
.is(TT_CtorInitializerColon
) &&
1538 Style
.BreakConstructorInitializers
== FormatStyle::BCIS_AfterColon
) {
1539 CurrentState
.Indent
=
1540 State
.FirstIndent
+ Style
.ConstructorInitializerIndentWidth
;
1541 CurrentState
.NestedBlockIndent
= CurrentState
.Indent
;
1542 if (Style
.PackConstructorInitializers
> FormatStyle::PCIS_BinPack
)
1543 CurrentState
.AvoidBinPacking
= true;
1545 CurrentState
.BreakBeforeParameter
= false;
1547 if (Current
.is(TT_InheritanceColon
)) {
1548 CurrentState
.Indent
=
1549 State
.FirstIndent
+ Style
.ConstructorInitializerIndentWidth
;
1551 if (Current
.isOneOf(TT_BinaryOperator
, TT_ConditionalExpr
) && Newline
)
1552 CurrentState
.NestedBlockIndent
= State
.Column
+ Current
.ColumnWidth
+ 1;
1553 if (Current
.isOneOf(TT_LambdaLSquare
, TT_TrailingReturnArrow
))
1554 CurrentState
.LastSpace
= State
.Column
;
1555 if (Current
.is(TT_RequiresExpression
) &&
1556 Style
.RequiresExpressionIndentation
== FormatStyle::REI_Keyword
) {
1557 CurrentState
.NestedBlockIndent
= State
.Column
;
1560 // Insert scopes created by fake parenthesis.
1561 const FormatToken
*Previous
= Current
.getPreviousNonComment();
1563 // Add special behavior to support a format commonly used for JavaScript
1565 // SomeFunction(function() {
1569 if (Current
.isNot(tok::comment
) && !Current
.ClosesRequiresClause
&&
1570 Previous
&& Previous
->isOneOf(tok::l_brace
, TT_ArrayInitializerLSquare
) &&
1571 Previous
->isNot(TT_DictLiteral
) && State
.Stack
.size() > 1 &&
1572 !CurrentState
.HasMultipleNestedBlocks
) {
1573 if (State
.Stack
[State
.Stack
.size() - 2].NestedBlockInlined
&& Newline
)
1574 for (ParenState
&PState
: llvm::drop_end(State
.Stack
))
1575 PState
.NoLineBreak
= true;
1576 State
.Stack
[State
.Stack
.size() - 2].NestedBlockInlined
= false;
1578 if (Previous
&& (Previous
->isOneOf(TT_BinaryOperator
, TT_ConditionalExpr
) ||
1579 (Previous
->isOneOf(tok::l_paren
, tok::comma
, tok::colon
) &&
1580 !Previous
->isOneOf(TT_DictLiteral
, TT_ObjCMethodExpr
)))) {
1581 CurrentState
.NestedBlockInlined
=
1582 !Newline
&& hasNestedBlockInlined(Previous
, Current
, Style
);
1585 moveStatePastFakeLParens(State
, Newline
);
1586 moveStatePastScopeCloser(State
);
1587 // Do not use CurrentState here, since the two functions before may change the
1589 bool AllowBreak
= !State
.Stack
.back().NoLineBreak
&&
1590 !State
.Stack
.back().NoLineBreakInOperand
;
1591 moveStatePastScopeOpener(State
, Newline
);
1592 moveStatePastFakeRParens(State
);
1594 if (Current
.is(TT_ObjCStringLiteral
) && State
.StartOfStringLiteral
== 0)
1595 State
.StartOfStringLiteral
= State
.Column
+ 1;
1596 if (Current
.is(TT_CSharpStringLiteral
) && State
.StartOfStringLiteral
== 0) {
1597 State
.StartOfStringLiteral
= State
.Column
+ 1;
1598 } else if (Current
.isStringLiteral() && State
.StartOfStringLiteral
== 0) {
1599 State
.StartOfStringLiteral
= State
.Column
;
1600 } else if (!Current
.isOneOf(tok::comment
, tok::identifier
, tok::hash
) &&
1601 !Current
.isStringLiteral()) {
1602 State
.StartOfStringLiteral
= 0;
1605 State
.Column
+= Current
.ColumnWidth
;
1606 State
.NextToken
= State
.NextToken
->Next
;
1607 // Verilog case labels are on the same unwrapped lines as the statements that
1608 // follow. TokenAnnotator identifies them and sets MustBreakBefore.
1609 // Indentation is taken care of here. A case label can only have 1 statement
1610 // in Verilog, so we don't have to worry about lines that follow.
1611 if (Style
.isVerilog() && State
.NextToken
&&
1612 State
.NextToken
->MustBreakBefore
&&
1613 Keywords
.isVerilogEndOfLabel(Current
)) {
1614 State
.FirstIndent
+= Style
.IndentWidth
;
1618 handleEndOfLine(Current
, State
, DryRun
, AllowBreak
, Newline
);
1621 Current
.Role
->formatFromToken(State
, this, DryRun
);
1622 // If the previous has a special role, let it consume tokens as appropriate.
1623 // It is necessary to start at the previous token for the only implemented
1624 // role (comma separated list). That way, the decision whether or not to break
1625 // after the "{" is already done and both options are tried and evaluated.
1626 // FIXME: This is ugly, find a better way.
1627 if (Previous
&& Previous
->Role
)
1628 Penalty
+= Previous
->Role
->formatAfterToken(State
, this, DryRun
);
1633 void ContinuationIndenter::moveStatePastFakeLParens(LineState
&State
,
1635 const FormatToken
&Current
= *State
.NextToken
;
1636 if (Current
.FakeLParens
.empty())
1639 const FormatToken
*Previous
= Current
.getPreviousNonComment();
1641 // Don't add extra indentation for the first fake parenthesis after
1642 // 'return', assignments, opening <({[, or requires clauses. The indentation
1643 // for these cases is special cased.
1644 bool SkipFirstExtraIndent
=
1646 (Previous
->opensScope() ||
1647 Previous
->isOneOf(tok::semi
, tok::kw_return
, TT_RequiresClause
) ||
1648 (Previous
->getPrecedence() == prec::Assignment
&&
1649 Style
.AlignOperands
!= FormatStyle::OAS_DontAlign
) ||
1650 Previous
->is(TT_ObjCMethodExpr
));
1651 for (const auto &PrecedenceLevel
: llvm::reverse(Current
.FakeLParens
)) {
1652 const auto &CurrentState
= State
.Stack
.back();
1653 ParenState NewParenState
= CurrentState
;
1654 NewParenState
.Tok
= nullptr;
1655 NewParenState
.ContainsLineBreak
= false;
1656 NewParenState
.LastOperatorWrapped
= true;
1657 NewParenState
.IsChainedConditional
= false;
1658 NewParenState
.IsWrappedConditional
= false;
1659 NewParenState
.UnindentOperator
= false;
1660 NewParenState
.NoLineBreak
=
1661 NewParenState
.NoLineBreak
|| CurrentState
.NoLineBreakInOperand
;
1663 // Don't propagate AvoidBinPacking into subexpressions of arg/param lists.
1664 if (PrecedenceLevel
> prec::Comma
)
1665 NewParenState
.AvoidBinPacking
= false;
1667 // Indent from 'LastSpace' unless these are fake parentheses encapsulating
1668 // a builder type call after 'return' or, if the alignment after opening
1669 // brackets is disabled.
1670 if (!Current
.isTrailingComment() &&
1671 (Style
.AlignOperands
!= FormatStyle::OAS_DontAlign
||
1672 PrecedenceLevel
< prec::Assignment
) &&
1673 (!Previous
|| Previous
->isNot(tok::kw_return
) ||
1674 (Style
.Language
!= FormatStyle::LK_Java
&& PrecedenceLevel
> 0)) &&
1675 (Style
.AlignAfterOpenBracket
!= FormatStyle::BAS_DontAlign
||
1676 PrecedenceLevel
!= prec::Comma
|| Current
.NestingLevel
== 0)) {
1677 NewParenState
.Indent
= std::max(
1678 std::max(State
.Column
, NewParenState
.Indent
), CurrentState
.LastSpace
);
1681 // Special case for generic selection expressions, its comma-separated
1682 // expressions are not aligned to the opening paren like regular calls, but
1683 // rather continuation-indented relative to the _Generic keyword.
1684 if (Previous
&& Previous
->endsSequence(tok::l_paren
, tok::kw__Generic
))
1685 NewParenState
.Indent
= CurrentState
.LastSpace
;
1687 if ((shouldUnindentNextOperator(Current
) ||
1689 (PrecedenceLevel
== prec::Conditional
&&
1690 Previous
->is(tok::question
) && Previous
->is(TT_ConditionalExpr
)))) &&
1692 // If BreakBeforeBinaryOperators is set, un-indent a bit to account for
1693 // the operator and keep the operands aligned.
1694 if (Style
.AlignOperands
== FormatStyle::OAS_AlignAfterOperator
)
1695 NewParenState
.UnindentOperator
= true;
1696 // Mark indentation as alignment if the expression is aligned.
1697 if (Style
.AlignOperands
!= FormatStyle::OAS_DontAlign
)
1698 NewParenState
.IsAligned
= true;
1701 // Do not indent relative to the fake parentheses inserted for "." or "->".
1702 // This is a special case to make the following to statements consistent:
1703 // OuterFunction(InnerFunctionCall( // break
1704 // ParameterToInnerFunction));
1705 // OuterFunction(SomeObject.InnerFunctionCall( // break
1706 // ParameterToInnerFunction));
1707 if (PrecedenceLevel
> prec::Unknown
)
1708 NewParenState
.LastSpace
= std::max(NewParenState
.LastSpace
, State
.Column
);
1709 if (PrecedenceLevel
!= prec::Conditional
&&
1710 Current
.isNot(TT_UnaryOperator
) &&
1711 Style
.AlignAfterOpenBracket
!= FormatStyle::BAS_DontAlign
) {
1712 NewParenState
.StartOfFunctionCall
= State
.Column
;
1715 // Indent conditional expressions, unless they are chained "else-if"
1716 // conditionals. Never indent expression where the 'operator' is ',', ';' or
1717 // an assignment (i.e. *I <= prec::Assignment) as those have different
1718 // indentation rules. Indent other expression, unless the indentation needs
1720 if (PrecedenceLevel
== prec::Conditional
&& Previous
&&
1721 Previous
->is(tok::colon
) && Previous
->is(TT_ConditionalExpr
) &&
1722 &PrecedenceLevel
== &Current
.FakeLParens
.back() &&
1723 !CurrentState
.IsWrappedConditional
) {
1724 NewParenState
.IsChainedConditional
= true;
1725 NewParenState
.UnindentOperator
= State
.Stack
.back().UnindentOperator
;
1726 } else if (PrecedenceLevel
== prec::Conditional
||
1727 (!SkipFirstExtraIndent
&& PrecedenceLevel
> prec::Assignment
&&
1728 !Current
.isTrailingComment())) {
1729 NewParenState
.Indent
+= Style
.ContinuationIndentWidth
;
1731 if ((Previous
&& !Previous
->opensScope()) || PrecedenceLevel
!= prec::Comma
)
1732 NewParenState
.BreakBeforeParameter
= false;
1733 State
.Stack
.push_back(NewParenState
);
1734 SkipFirstExtraIndent
= false;
1738 void ContinuationIndenter::moveStatePastFakeRParens(LineState
&State
) {
1739 for (unsigned i
= 0, e
= State
.NextToken
->FakeRParens
; i
!= e
; ++i
) {
1740 unsigned VariablePos
= State
.Stack
.back().VariablePos
;
1741 if (State
.Stack
.size() == 1) {
1742 // Do not pop the last element.
1745 State
.Stack
.pop_back();
1746 State
.Stack
.back().VariablePos
= VariablePos
;
1749 if (State
.NextToken
->ClosesRequiresClause
&& Style
.IndentRequiresClause
) {
1750 // Remove the indentation of the requires clauses (which is not in Indent,
1751 // but in LastSpace).
1752 State
.Stack
.back().LastSpace
-= Style
.IndentWidth
;
1756 void ContinuationIndenter::moveStatePastScopeOpener(LineState
&State
,
1758 const FormatToken
&Current
= *State
.NextToken
;
1759 if (!Current
.opensScope())
1762 const auto &CurrentState
= State
.Stack
.back();
1764 // Don't allow '<' or '(' in C# generic type constraints to start new scopes.
1765 if (Current
.isOneOf(tok::less
, tok::l_paren
) &&
1766 CurrentState
.IsCSharpGenericTypeConstraint
) {
1770 if (Current
.MatchingParen
&& Current
.is(BK_Block
)) {
1771 moveStateToNewBlock(State
);
1776 unsigned LastSpace
= CurrentState
.LastSpace
;
1777 bool AvoidBinPacking
;
1778 bool BreakBeforeParameter
= false;
1779 unsigned NestedBlockIndent
= std::max(CurrentState
.StartOfFunctionCall
,
1780 CurrentState
.NestedBlockIndent
);
1781 if (Current
.isOneOf(tok::l_brace
, TT_ArrayInitializerLSquare
) ||
1782 opensProtoMessageField(Current
, Style
)) {
1783 if (Current
.opensBlockOrBlockTypeList(Style
)) {
1784 NewIndent
= Style
.IndentWidth
+
1785 std::min(State
.Column
, CurrentState
.NestedBlockIndent
);
1786 } else if (Current
.is(tok::l_brace
)) {
1788 CurrentState
.LastSpace
+ Style
.BracedInitializerIndentWidth
.value_or(
1789 Style
.ContinuationIndentWidth
);
1791 NewIndent
= CurrentState
.LastSpace
+ Style
.ContinuationIndentWidth
;
1793 const FormatToken
*NextNonComment
= Current
.getNextNonComment();
1794 bool EndsInComma
= Current
.MatchingParen
&&
1795 Current
.MatchingParen
->Previous
&&
1796 Current
.MatchingParen
->Previous
->is(tok::comma
);
1797 AvoidBinPacking
= EndsInComma
|| Current
.is(TT_DictLiteral
) ||
1798 Style
.Language
== FormatStyle::LK_Proto
||
1799 Style
.Language
== FormatStyle::LK_TextProto
||
1800 !Style
.BinPackArguments
||
1801 (NextNonComment
&& NextNonComment
->isOneOf(
1802 TT_DesignatedInitializerPeriod
,
1803 TT_DesignatedInitializerLSquare
));
1804 BreakBeforeParameter
= EndsInComma
;
1805 if (Current
.ParameterCount
> 1)
1806 NestedBlockIndent
= std::max(NestedBlockIndent
, State
.Column
+ 1);
1809 Style
.ContinuationIndentWidth
+
1810 std::max(CurrentState
.LastSpace
, CurrentState
.StartOfFunctionCall
);
1812 // Ensure that different different brackets force relative alignment, e.g.:
1813 // void SomeFunction(vector< // break
1815 // FIXME: We likely want to do this for more combinations of brackets.
1816 if (Current
.is(tok::less
) && Current
.ParentBracket
== tok::l_paren
) {
1817 NewIndent
= std::max(NewIndent
, CurrentState
.Indent
);
1818 LastSpace
= std::max(LastSpace
, CurrentState
.Indent
);
1822 Current
.MatchingParen
&&
1823 Current
.MatchingParen
->getPreviousNonComment() &&
1824 Current
.MatchingParen
->getPreviousNonComment()->is(tok::comma
);
1826 // If ObjCBinPackProtocolList is unspecified, fall back to BinPackParameters
1827 // for backwards compatibility.
1828 bool ObjCBinPackProtocolList
=
1829 (Style
.ObjCBinPackProtocolList
== FormatStyle::BPS_Auto
&&
1830 Style
.BinPackParameters
) ||
1831 Style
.ObjCBinPackProtocolList
== FormatStyle::BPS_Always
;
1833 bool BinPackDeclaration
=
1834 (State
.Line
->Type
!= LT_ObjCDecl
&& Style
.BinPackParameters
) ||
1835 (State
.Line
->Type
== LT_ObjCDecl
&& ObjCBinPackProtocolList
);
1837 bool GenericSelection
=
1838 Current
.getPreviousNonComment() &&
1839 Current
.getPreviousNonComment()->is(tok::kw__Generic
);
1842 (CurrentState
.IsCSharpGenericTypeConstraint
) || GenericSelection
||
1843 (Style
.isJavaScript() && EndsInComma
) ||
1844 (State
.Line
->MustBeDeclaration
&& !BinPackDeclaration
) ||
1845 (!State
.Line
->MustBeDeclaration
&& !Style
.BinPackArguments
) ||
1846 (Style
.ExperimentalAutoDetectBinPacking
&&
1847 (Current
.is(PPK_OnePerLine
) ||
1848 (!BinPackInconclusiveFunctions
&& Current
.is(PPK_Inconclusive
))));
1850 if (Current
.is(TT_ObjCMethodExpr
) && Current
.MatchingParen
&&
1851 Style
.ObjCBreakBeforeNestedBlockParam
) {
1852 if (Style
.ColumnLimit
) {
1853 // If this '[' opens an ObjC call, determine whether all parameters fit
1854 // into one line and put one per line if they don't.
1855 if (getLengthToMatchingParen(Current
, State
.Stack
) + State
.Column
>
1856 getColumnLimit(State
)) {
1857 BreakBeforeParameter
= true;
1860 // For ColumnLimit = 0, we have to figure out whether there is or has to
1861 // be a line break within this call.
1862 for (const FormatToken
*Tok
= &Current
;
1863 Tok
&& Tok
!= Current
.MatchingParen
; Tok
= Tok
->Next
) {
1864 if (Tok
->MustBreakBefore
||
1865 (Tok
->CanBreakBefore
&& Tok
->NewlinesBefore
> 0)) {
1866 BreakBeforeParameter
= true;
1873 if (Style
.isJavaScript() && EndsInComma
)
1874 BreakBeforeParameter
= true;
1876 // Generally inherit NoLineBreak from the current scope to nested scope.
1877 // However, don't do this for non-empty nested blocks, dict literals and
1878 // array literals as these follow different indentation rules.
1880 Current
.Children
.empty() &&
1881 !Current
.isOneOf(TT_DictLiteral
, TT_ArrayInitializerLSquare
) &&
1882 (CurrentState
.NoLineBreak
|| CurrentState
.NoLineBreakInOperand
||
1883 (Current
.is(TT_TemplateOpener
) &&
1884 CurrentState
.ContainsUnwrappedBuilder
));
1885 State
.Stack
.push_back(
1886 ParenState(&Current
, NewIndent
, LastSpace
, AvoidBinPacking
, NoLineBreak
));
1887 auto &NewState
= State
.Stack
.back();
1888 NewState
.NestedBlockIndent
= NestedBlockIndent
;
1889 NewState
.BreakBeforeParameter
= BreakBeforeParameter
;
1890 NewState
.HasMultipleNestedBlocks
= (Current
.BlockParameterCount
> 1);
1892 if (Style
.BraceWrapping
.BeforeLambdaBody
&& Current
.Next
&&
1893 Current
.is(tok::l_paren
)) {
1894 // Search for any parameter that is a lambda.
1895 FormatToken
const *next
= Current
.Next
;
1897 if (next
->is(TT_LambdaLSquare
)) {
1898 NewState
.HasMultipleNestedBlocks
= true;
1905 NewState
.IsInsideObjCArrayLiteral
= Current
.is(TT_ArrayInitializerLSquare
) &&
1907 Current
.Previous
->is(tok::at
);
1910 void ContinuationIndenter::moveStatePastScopeCloser(LineState
&State
) {
1911 const FormatToken
&Current
= *State
.NextToken
;
1912 if (!Current
.closesScope())
1915 // If we encounter a closing ), ], } or >, we can remove a level from our
1917 if (State
.Stack
.size() > 1 &&
1918 (Current
.isOneOf(tok::r_paren
, tok::r_square
, TT_TemplateString
) ||
1919 (Current
.is(tok::r_brace
) && State
.NextToken
!= State
.Line
->First
) ||
1920 State
.NextToken
->is(TT_TemplateCloser
) ||
1921 (Current
.is(tok::greater
) && Current
.is(TT_DictLiteral
)))) {
1922 State
.Stack
.pop_back();
1925 auto &CurrentState
= State
.Stack
.back();
1927 // Reevaluate whether ObjC message arguments fit into one line.
1928 // If a receiver spans multiple lines, e.g.:
1929 // [[object block:^{
1932 // BreakBeforeParameter is calculated based on an incorrect assumption
1933 // (it is checked whether the whole expression fits into one line without
1934 // considering a line break inside a message receiver).
1935 // We check whether arguments fit after receiver scope closer (into the same
1937 if (CurrentState
.BreakBeforeParameter
&& Current
.MatchingParen
&&
1938 Current
.MatchingParen
->Previous
) {
1939 const FormatToken
&CurrentScopeOpener
= *Current
.MatchingParen
->Previous
;
1940 if (CurrentScopeOpener
.is(TT_ObjCMethodExpr
) &&
1941 CurrentScopeOpener
.MatchingParen
) {
1942 int NecessarySpaceInLine
=
1943 getLengthToMatchingParen(CurrentScopeOpener
, State
.Stack
) +
1944 CurrentScopeOpener
.TotalLength
- Current
.TotalLength
- 1;
1945 if (State
.Column
+ Current
.ColumnWidth
+ NecessarySpaceInLine
<=
1946 Style
.ColumnLimit
) {
1947 CurrentState
.BreakBeforeParameter
= false;
1952 if (Current
.is(tok::r_square
)) {
1953 // If this ends the array subscript expr, reset the corresponding value.
1954 const FormatToken
*NextNonComment
= Current
.getNextNonComment();
1955 if (NextNonComment
&& NextNonComment
->isNot(tok::l_square
))
1956 CurrentState
.StartOfArraySubscripts
= 0;
1960 void ContinuationIndenter::moveStateToNewBlock(LineState
&State
) {
1961 if (Style
.LambdaBodyIndentation
== FormatStyle::LBI_OuterScope
&&
1962 State
.NextToken
->is(TT_LambdaLBrace
) &&
1963 !State
.Line
->MightBeFunctionDecl
) {
1964 State
.Stack
.back().NestedBlockIndent
= State
.FirstIndent
;
1966 unsigned NestedBlockIndent
= State
.Stack
.back().NestedBlockIndent
;
1967 // ObjC block sometimes follow special indentation rules.
1968 unsigned NewIndent
=
1969 NestedBlockIndent
+ (State
.NextToken
->is(TT_ObjCBlockLBrace
)
1970 ? Style
.ObjCBlockIndentWidth
1971 : Style
.IndentWidth
);
1972 State
.Stack
.push_back(ParenState(State
.NextToken
, NewIndent
,
1973 State
.Stack
.back().LastSpace
,
1974 /*AvoidBinPacking=*/true,
1975 /*NoLineBreak=*/false));
1976 State
.Stack
.back().NestedBlockIndent
= NestedBlockIndent
;
1977 State
.Stack
.back().BreakBeforeParameter
= true;
1980 static unsigned getLastLineEndColumn(StringRef Text
, unsigned StartColumn
,
1982 encoding::Encoding Encoding
) {
1983 size_t LastNewlinePos
= Text
.find_last_of("\n");
1984 if (LastNewlinePos
== StringRef::npos
) {
1985 return StartColumn
+
1986 encoding::columnWidthWithTabs(Text
, StartColumn
, TabWidth
, Encoding
);
1988 return encoding::columnWidthWithTabs(Text
.substr(LastNewlinePos
),
1989 /*StartColumn=*/0, TabWidth
, Encoding
);
1993 unsigned ContinuationIndenter::reformatRawStringLiteral(
1994 const FormatToken
&Current
, LineState
&State
,
1995 const FormatStyle
&RawStringStyle
, bool DryRun
, bool Newline
) {
1996 unsigned StartColumn
= State
.Column
- Current
.ColumnWidth
;
1997 StringRef OldDelimiter
= *getRawStringDelimiter(Current
.TokenText
);
1998 StringRef NewDelimiter
=
1999 getCanonicalRawStringDelimiter(Style
, RawStringStyle
.Language
);
2000 if (NewDelimiter
.empty())
2001 NewDelimiter
= OldDelimiter
;
2002 // The text of a raw string is between the leading 'R"delimiter(' and the
2003 // trailing 'delimiter)"'.
2004 unsigned OldPrefixSize
= 3 + OldDelimiter
.size();
2005 unsigned OldSuffixSize
= 2 + OldDelimiter
.size();
2006 // We create a virtual text environment which expects a null-terminated
2007 // string, so we cannot use StringRef.
2008 std::string RawText
= std::string(
2009 Current
.TokenText
.substr(OldPrefixSize
).drop_back(OldSuffixSize
));
2010 if (NewDelimiter
!= OldDelimiter
) {
2011 // Don't update to the canonical delimiter 'deli' if ')deli"' occurs in the
2013 std::string CanonicalDelimiterSuffix
= (")" + NewDelimiter
+ "\"").str();
2014 if (StringRef(RawText
).contains(CanonicalDelimiterSuffix
))
2015 NewDelimiter
= OldDelimiter
;
2018 unsigned NewPrefixSize
= 3 + NewDelimiter
.size();
2019 unsigned NewSuffixSize
= 2 + NewDelimiter
.size();
2021 // The first start column is the column the raw text starts after formatting.
2022 unsigned FirstStartColumn
= StartColumn
+ NewPrefixSize
;
2024 // The next start column is the intended indentation a line break inside
2025 // the raw string at level 0. It is determined by the following rules:
2026 // - if the content starts on newline, it is one level more than the current
2028 // - if the content does not start on a newline, it is the first start
2030 // These rules have the advantage that the formatted content both does not
2031 // violate the rectangle rule and visually flows within the surrounding
2033 bool ContentStartsOnNewline
= Current
.TokenText
[OldPrefixSize
] == '\n';
2034 // If this token is the last parameter (checked by looking if it's followed by
2035 // `)` and is not on a newline, the base the indent off the line's nested
2036 // block indent. Otherwise, base the indent off the arguments indent, so we
2039 // fffffffffff(1, 2, 3, R"pb(
2043 // fffffffffff(1, 2, 3,
2049 // fffffffffff(1, 2, 3,
2055 unsigned CurrentIndent
=
2056 (!Newline
&& Current
.Next
&& Current
.Next
->is(tok::r_paren
))
2057 ? State
.Stack
.back().NestedBlockIndent
2058 : State
.Stack
.back().Indent
;
2059 unsigned NextStartColumn
= ContentStartsOnNewline
2060 ? CurrentIndent
+ Style
.IndentWidth
2063 // The last start column is the column the raw string suffix starts if it is
2064 // put on a newline.
2065 // The last start column is the intended indentation of the raw string postfix
2066 // if it is put on a newline. It is determined by the following rules:
2067 // - if the raw string prefix starts on a newline, it is the column where
2068 // that raw string prefix starts, and
2069 // - if the raw string prefix does not start on a newline, it is the current
2071 unsigned LastStartColumn
=
2072 Current
.NewlinesBefore
? FirstStartColumn
- NewPrefixSize
: CurrentIndent
;
2074 std::pair
<tooling::Replacements
, unsigned> Fixes
= internal::reformat(
2075 RawStringStyle
, RawText
, {tooling::Range(0, RawText
.size())},
2076 FirstStartColumn
, NextStartColumn
, LastStartColumn
, "<stdin>",
2077 /*Status=*/nullptr);
2079 auto NewCode
= applyAllReplacements(RawText
, Fixes
.first
);
2080 tooling::Replacements NoFixes
;
2082 return addMultilineToken(Current
, State
);
2084 if (NewDelimiter
!= OldDelimiter
) {
2085 // In 'R"delimiter(...', the delimiter starts 2 characters after the start
2087 SourceLocation PrefixDelimiterStart
=
2088 Current
.Tok
.getLocation().getLocWithOffset(2);
2089 auto PrefixErr
= Whitespaces
.addReplacement(tooling::Replacement(
2090 SourceMgr
, PrefixDelimiterStart
, OldDelimiter
.size(), NewDelimiter
));
2093 << "Failed to update the prefix delimiter of a raw string: "
2094 << llvm::toString(std::move(PrefixErr
)) << "\n";
2096 // In 'R"delimiter(...)delimiter"', the suffix delimiter starts at
2097 // position length - 1 - |delimiter|.
2098 SourceLocation SuffixDelimiterStart
=
2099 Current
.Tok
.getLocation().getLocWithOffset(Current
.TokenText
.size() -
2100 1 - OldDelimiter
.size());
2101 auto SuffixErr
= Whitespaces
.addReplacement(tooling::Replacement(
2102 SourceMgr
, SuffixDelimiterStart
, OldDelimiter
.size(), NewDelimiter
));
2105 << "Failed to update the suffix delimiter of a raw string: "
2106 << llvm::toString(std::move(SuffixErr
)) << "\n";
2109 SourceLocation OriginLoc
=
2110 Current
.Tok
.getLocation().getLocWithOffset(OldPrefixSize
);
2111 for (const tooling::Replacement
&Fix
: Fixes
.first
) {
2112 auto Err
= Whitespaces
.addReplacement(tooling::Replacement(
2113 SourceMgr
, OriginLoc
.getLocWithOffset(Fix
.getOffset()),
2114 Fix
.getLength(), Fix
.getReplacementText()));
2116 llvm::errs() << "Failed to reformat raw string: "
2117 << llvm::toString(std::move(Err
)) << "\n";
2121 unsigned RawLastLineEndColumn
= getLastLineEndColumn(
2122 *NewCode
, FirstStartColumn
, Style
.TabWidth
, Encoding
);
2123 State
.Column
= RawLastLineEndColumn
+ NewSuffixSize
;
2124 // Since we're updating the column to after the raw string literal here, we
2125 // have to manually add the penalty for the prefix R"delim( over the column
2127 unsigned PrefixExcessCharacters
=
2128 StartColumn
+ NewPrefixSize
> Style
.ColumnLimit
2129 ? StartColumn
+ NewPrefixSize
- Style
.ColumnLimit
2132 ContentStartsOnNewline
|| (NewCode
->find('\n') != std::string::npos
);
2134 // Break before further function parameters on all levels.
2135 for (ParenState
&Paren
: State
.Stack
)
2136 Paren
.BreakBeforeParameter
= true;
2138 return Fixes
.second
+ PrefixExcessCharacters
* Style
.PenaltyExcessCharacter
;
2141 unsigned ContinuationIndenter::addMultilineToken(const FormatToken
&Current
,
2143 // Break before further function parameters on all levels.
2144 for (ParenState
&Paren
: State
.Stack
)
2145 Paren
.BreakBeforeParameter
= true;
2147 unsigned ColumnsUsed
= State
.Column
;
2148 // We can only affect layout of the first and the last line, so the penalty
2149 // for all other lines is constant, and we ignore it.
2150 State
.Column
= Current
.LastLineColumnWidth
;
2152 if (ColumnsUsed
> getColumnLimit(State
))
2153 return Style
.PenaltyExcessCharacter
* (ColumnsUsed
- getColumnLimit(State
));
2157 unsigned ContinuationIndenter::handleEndOfLine(const FormatToken
&Current
,
2158 LineState
&State
, bool DryRun
,
2159 bool AllowBreak
, bool Newline
) {
2160 unsigned Penalty
= 0;
2161 // Compute the raw string style to use in case this is a raw string literal
2162 // that can be reformatted.
2163 auto RawStringStyle
= getRawStringStyle(Current
, State
);
2164 if (RawStringStyle
&& !Current
.Finalized
) {
2165 Penalty
= reformatRawStringLiteral(Current
, State
, *RawStringStyle
, DryRun
,
2167 } else if (Current
.IsMultiline
&& Current
.isNot(TT_BlockComment
)) {
2168 // Don't break multi-line tokens other than block comments and raw string
2169 // literals. Instead, just update the state.
2170 Penalty
= addMultilineToken(Current
, State
);
2171 } else if (State
.Line
->Type
!= LT_ImportStatement
) {
2172 // We generally don't break import statements.
2173 LineState OriginalState
= State
;
2175 // Whether we force the reflowing algorithm to stay strictly within the
2177 bool Strict
= false;
2178 // Whether the first non-strict attempt at reflowing did intentionally
2179 // exceed the column limit.
2180 bool Exceeded
= false;
2181 std::tie(Penalty
, Exceeded
) = breakProtrudingToken(
2182 Current
, State
, AllowBreak
, /*DryRun=*/true, Strict
);
2184 // If non-strict reflowing exceeds the column limit, try whether strict
2185 // reflowing leads to an overall lower penalty.
2186 LineState StrictState
= OriginalState
;
2187 unsigned StrictPenalty
=
2188 breakProtrudingToken(Current
, StrictState
, AllowBreak
,
2189 /*DryRun=*/true, /*Strict=*/true)
2191 Strict
= StrictPenalty
<= Penalty
;
2193 Penalty
= StrictPenalty
;
2194 State
= StrictState
;
2198 // If we're not in dry-run mode, apply the changes with the decision on
2199 // strictness made above.
2200 breakProtrudingToken(Current
, OriginalState
, AllowBreak
, /*DryRun=*/false,
2204 if (State
.Column
> getColumnLimit(State
)) {
2205 unsigned ExcessCharacters
= State
.Column
- getColumnLimit(State
);
2206 Penalty
+= Style
.PenaltyExcessCharacter
* ExcessCharacters
;
2211 // Returns the enclosing function name of a token, or the empty string if not
2213 static StringRef
getEnclosingFunctionName(const FormatToken
&Current
) {
2214 // Look for: 'function(' or 'function<templates>(' before Current.
2215 auto Tok
= Current
.getPreviousNonComment();
2216 if (!Tok
|| Tok
->isNot(tok::l_paren
))
2218 Tok
= Tok
->getPreviousNonComment();
2221 if (Tok
->is(TT_TemplateCloser
)) {
2222 Tok
= Tok
->MatchingParen
;
2224 Tok
= Tok
->getPreviousNonComment();
2226 if (!Tok
|| Tok
->isNot(tok::identifier
))
2228 return Tok
->TokenText
;
2231 std::optional
<FormatStyle
>
2232 ContinuationIndenter::getRawStringStyle(const FormatToken
&Current
,
2233 const LineState
&State
) {
2234 if (!Current
.isStringLiteral())
2235 return std::nullopt
;
2236 auto Delimiter
= getRawStringDelimiter(Current
.TokenText
);
2238 return std::nullopt
;
2239 auto RawStringStyle
= RawStringFormats
.getDelimiterStyle(*Delimiter
);
2240 if (!RawStringStyle
&& Delimiter
->empty()) {
2241 RawStringStyle
= RawStringFormats
.getEnclosingFunctionStyle(
2242 getEnclosingFunctionName(Current
));
2244 if (!RawStringStyle
)
2245 return std::nullopt
;
2246 RawStringStyle
->ColumnLimit
= getColumnLimit(State
);
2247 return RawStringStyle
;
2250 std::unique_ptr
<BreakableToken
>
2251 ContinuationIndenter::createBreakableToken(const FormatToken
&Current
,
2252 LineState
&State
, bool AllowBreak
) {
2253 unsigned StartColumn
= State
.Column
- Current
.ColumnWidth
;
2254 if (Current
.isStringLiteral()) {
2255 // Strings in JSON cannot be broken. Breaking strings in JavaScript is
2256 // disabled for now.
2257 if (Style
.isJson() || Style
.isJavaScript() || !Style
.BreakStringLiterals
||
2262 // Don't break string literals inside preprocessor directives (except for
2263 // #define directives, as their contents are stored in separate lines and
2264 // are not affected by this check).
2265 // This way we avoid breaking code with line directives and unknown
2266 // preprocessor directives that contain long string literals.
2267 if (State
.Line
->Type
== LT_PreprocessorDirective
)
2269 // Exempts unterminated string literals from line breaking. The user will
2270 // likely want to terminate the string before any line breaking is done.
2271 if (Current
.IsUnterminatedLiteral
)
2273 // Don't break string literals inside Objective-C array literals (doing so
2274 // raises the warning -Wobjc-string-concatenation).
2275 if (State
.Stack
.back().IsInsideObjCArrayLiteral
)
2278 // The "DPI"/"DPI-C" in SystemVerilog direct programming interface
2279 // imports/exports cannot be split, e.g.
2280 // `import "DPI" function foo();`
2281 // FIXME: make this use same infra as C++ import checks
2282 if (Style
.isVerilog() && Current
.Previous
&&
2283 Current
.Previous
->isOneOf(tok::kw_export
, Keywords
.kw_import
)) {
2286 StringRef Text
= Current
.TokenText
;
2288 // We need this to address the case where there is an unbreakable tail only
2289 // if certain other formatting decisions have been taken. The
2290 // UnbreakableTailLength of Current is an overapproximation in that case and
2291 // we need to be correct here.
2292 unsigned UnbreakableTailLength
= (State
.NextToken
&& canBreak(State
))
2294 : Current
.UnbreakableTailLength
;
2296 if (Style
.isVerilog() || Style
.Language
== FormatStyle::LK_Java
||
2297 Style
.isJavaScript() || Style
.isCSharp()) {
2298 BreakableStringLiteralUsingOperators::QuoteStyleType QuoteStyle
;
2299 if (Style
.isJavaScript() && Text
.startswith("'") && Text
.endswith("'")) {
2300 QuoteStyle
= BreakableStringLiteralUsingOperators::SingleQuotes
;
2301 } else if (Style
.isCSharp() && Text
.startswith("@\"") &&
2302 Text
.endswith("\"")) {
2303 QuoteStyle
= BreakableStringLiteralUsingOperators::AtDoubleQuotes
;
2304 } else if (Text
.startswith("\"") && Text
.endswith("\"")) {
2305 QuoteStyle
= BreakableStringLiteralUsingOperators::DoubleQuotes
;
2309 return std::make_unique
<BreakableStringLiteralUsingOperators
>(
2310 Current
, QuoteStyle
,
2311 /*UnindentPlus=*/shouldUnindentNextOperator(Current
), StartColumn
,
2312 UnbreakableTailLength
, State
.Line
->InPPDirective
, Encoding
, Style
);
2317 // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
2318 // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
2319 // reduce the overhead) for each FormatToken, which is a string, so that we
2320 // don't run multiple checks here on the hot path.
2321 if ((Text
.endswith(Postfix
= "\"") &&
2322 (Text
.startswith(Prefix
= "@\"") || Text
.startswith(Prefix
= "\"") ||
2323 Text
.startswith(Prefix
= "u\"") || Text
.startswith(Prefix
= "U\"") ||
2324 Text
.startswith(Prefix
= "u8\"") ||
2325 Text
.startswith(Prefix
= "L\""))) ||
2326 (Text
.startswith(Prefix
= "_T(\"") && Text
.endswith(Postfix
= "\")"))) {
2327 return std::make_unique
<BreakableStringLiteral
>(
2328 Current
, StartColumn
, Prefix
, Postfix
, UnbreakableTailLength
,
2329 State
.Line
->InPPDirective
, Encoding
, Style
);
2331 } else if (Current
.is(TT_BlockComment
)) {
2332 if (!Style
.ReflowComments
||
2333 // If a comment token switches formatting, like
2334 // /* clang-format on */, we don't want to break it further,
2335 // but we may still want to adjust its indentation.
2336 switchesFormatting(Current
)) {
2339 return std::make_unique
<BreakableBlockComment
>(
2340 Current
, StartColumn
, Current
.OriginalColumn
, !Current
.Previous
,
2341 State
.Line
->InPPDirective
, Encoding
, Style
, Whitespaces
.useCRLF());
2342 } else if (Current
.is(TT_LineComment
) &&
2343 (!Current
.Previous
||
2344 Current
.Previous
->isNot(TT_ImplicitStringLiteral
))) {
2345 bool RegularComments
= [&]() {
2346 for (const FormatToken
*T
= &Current
; T
&& T
->is(TT_LineComment
);
2348 if (!(T
->TokenText
.startswith("//") || T
->TokenText
.startswith("#")))
2353 if (!Style
.ReflowComments
||
2354 CommentPragmasRegex
.match(Current
.TokenText
.substr(2)) ||
2355 switchesFormatting(Current
) || !RegularComments
) {
2358 return std::make_unique
<BreakableLineCommentSection
>(
2359 Current
, StartColumn
, /*InPPDirective=*/false, Encoding
, Style
);
2364 std::pair
<unsigned, bool>
2365 ContinuationIndenter::breakProtrudingToken(const FormatToken
&Current
,
2366 LineState
&State
, bool AllowBreak
,
2367 bool DryRun
, bool Strict
) {
2368 std::unique_ptr
<const BreakableToken
> Token
=
2369 createBreakableToken(Current
, State
, AllowBreak
);
2372 assert(Token
->getLineCount() > 0);
2373 unsigned ColumnLimit
= getColumnLimit(State
);
2374 if (Current
.is(TT_LineComment
)) {
2375 // We don't insert backslashes when breaking line comments.
2376 ColumnLimit
= Style
.ColumnLimit
;
2378 if (ColumnLimit
== 0) {
2379 // To make the rest of the function easier set the column limit to the
2380 // maximum, if there should be no limit.
2381 ColumnLimit
= std::numeric_limits
<decltype(ColumnLimit
)>::max();
2383 if (Current
.UnbreakableTailLength
>= ColumnLimit
)
2385 // ColumnWidth was already accounted into State.Column before calling
2386 // breakProtrudingToken.
2387 unsigned StartColumn
= State
.Column
- Current
.ColumnWidth
;
2388 unsigned NewBreakPenalty
= Current
.isStringLiteral()
2389 ? Style
.PenaltyBreakString
2390 : Style
.PenaltyBreakComment
;
2391 // Stores whether we intentionally decide to let a line exceed the column
2393 bool Exceeded
= false;
2394 // Stores whether we introduce a break anywhere in the token.
2395 bool BreakInserted
= Token
->introducesBreakBeforeToken();
2396 // Store whether we inserted a new line break at the end of the previous
2398 bool NewBreakBefore
= false;
2399 // We use a conservative reflowing strategy. Reflow starts after a line is
2400 // broken or the corresponding whitespace compressed. Reflow ends as soon as a
2401 // line that doesn't get reflown with the previous line is reached.
2402 bool Reflow
= false;
2403 // Keep track of where we are in the token:
2404 // Where we are in the content of the current logical line.
2405 unsigned TailOffset
= 0;
2406 // The column number we're currently at.
2407 unsigned ContentStartColumn
=
2408 Token
->getContentStartColumn(0, /*Break=*/false);
2409 // The number of columns left in the current logical line after TailOffset.
2410 unsigned RemainingTokenColumns
=
2411 Token
->getRemainingLength(0, TailOffset
, ContentStartColumn
);
2412 // Adapt the start of the token, for example indent.
2414 Token
->adaptStartOfLine(0, Whitespaces
);
2416 unsigned ContentIndent
= 0;
2417 unsigned Penalty
= 0;
2418 LLVM_DEBUG(llvm::dbgs() << "Breaking protruding token at column "
2419 << StartColumn
<< ".\n");
2420 for (unsigned LineIndex
= 0, EndIndex
= Token
->getLineCount();
2421 LineIndex
!= EndIndex
; ++LineIndex
) {
2422 LLVM_DEBUG(llvm::dbgs()
2423 << " Line: " << LineIndex
<< " (Reflow: " << Reflow
<< ")\n");
2424 NewBreakBefore
= false;
2425 // If we did reflow the previous line, we'll try reflowing again. Otherwise
2426 // we'll start reflowing if the current line is broken or whitespace is
2428 bool TryReflow
= Reflow
;
2429 // Break the current token until we can fit the rest of the line.
2430 while (ContentStartColumn
+ RemainingTokenColumns
> ColumnLimit
) {
2431 LLVM_DEBUG(llvm::dbgs() << " Over limit, need: "
2432 << (ContentStartColumn
+ RemainingTokenColumns
)
2433 << ", space: " << ColumnLimit
2434 << ", reflown prefix: " << ContentStartColumn
2435 << ", offset in line: " << TailOffset
<< "\n");
2436 // If the current token doesn't fit, find the latest possible split in the
2437 // current line so that breaking at it will be under the column limit.
2438 // FIXME: Use the earliest possible split while reflowing to correctly
2439 // compress whitespace within a line.
2440 BreakableToken::Split Split
=
2441 Token
->getSplit(LineIndex
, TailOffset
, ColumnLimit
,
2442 ContentStartColumn
, CommentPragmasRegex
);
2443 if (Split
.first
== StringRef::npos
) {
2444 // No break opportunity - update the penalty and continue with the next
2446 if (LineIndex
< EndIndex
- 1) {
2447 // The last line's penalty is handled in addNextStateToQueue() or when
2448 // calling replaceWhitespaceAfterLastLine below.
2449 Penalty
+= Style
.PenaltyExcessCharacter
*
2450 (ContentStartColumn
+ RemainingTokenColumns
- ColumnLimit
);
2452 LLVM_DEBUG(llvm::dbgs() << " No break opportunity.\n");
2455 assert(Split
.first
!= 0);
2457 if (Token
->supportsReflow()) {
2458 // Check whether the next natural split point after the current one can
2459 // still fit the line, either because we can compress away whitespace,
2460 // or because the penalty the excess characters introduce is lower than
2461 // the break penalty.
2462 // We only do this for tokens that support reflowing, and thus allow us
2463 // to change the whitespace arbitrarily (e.g. comments).
2464 // Other tokens, like string literals, can be broken on arbitrary
2467 // First, compute the columns from TailOffset to the next possible split
2471 // // Some text that breaks
2474 // ^-------- to split columns
2476 // ^--------------- to next split columns
2477 unsigned ToSplitColumns
= Token
->getRangeLength(
2478 LineIndex
, TailOffset
, Split
.first
, ContentStartColumn
);
2479 LLVM_DEBUG(llvm::dbgs() << " ToSplit: " << ToSplitColumns
<< "\n");
2481 BreakableToken::Split NextSplit
= Token
->getSplit(
2482 LineIndex
, TailOffset
+ Split
.first
+ Split
.second
, ColumnLimit
,
2483 ContentStartColumn
+ ToSplitColumns
+ 1, CommentPragmasRegex
);
2484 // Compute the columns necessary to fit the next non-breakable sequence
2485 // into the current line.
2486 unsigned ToNextSplitColumns
= 0;
2487 if (NextSplit
.first
== StringRef::npos
) {
2488 ToNextSplitColumns
= Token
->getRemainingLength(LineIndex
, TailOffset
,
2489 ContentStartColumn
);
2491 ToNextSplitColumns
= Token
->getRangeLength(
2492 LineIndex
, TailOffset
,
2493 Split
.first
+ Split
.second
+ NextSplit
.first
, ContentStartColumn
);
2495 // Compress the whitespace between the break and the start of the next
2496 // unbreakable sequence.
2497 ToNextSplitColumns
=
2498 Token
->getLengthAfterCompression(ToNextSplitColumns
, Split
);
2499 LLVM_DEBUG(llvm::dbgs()
2500 << " ContentStartColumn: " << ContentStartColumn
<< "\n");
2501 LLVM_DEBUG(llvm::dbgs()
2502 << " ToNextSplit: " << ToNextSplitColumns
<< "\n");
2503 // If the whitespace compression makes us fit, continue on the current
2505 bool ContinueOnLine
=
2506 ContentStartColumn
+ ToNextSplitColumns
<= ColumnLimit
;
2507 unsigned ExcessCharactersPenalty
= 0;
2508 if (!ContinueOnLine
&& !Strict
) {
2509 // Similarly, if the excess characters' penalty is lower than the
2510 // penalty of introducing a new break, continue on the current line.
2511 ExcessCharactersPenalty
=
2512 (ContentStartColumn
+ ToNextSplitColumns
- ColumnLimit
) *
2513 Style
.PenaltyExcessCharacter
;
2514 LLVM_DEBUG(llvm::dbgs()
2515 << " Penalty excess: " << ExcessCharactersPenalty
2516 << "\n break : " << NewBreakPenalty
<< "\n");
2517 if (ExcessCharactersPenalty
< NewBreakPenalty
) {
2519 ContinueOnLine
= true;
2522 if (ContinueOnLine
) {
2523 LLVM_DEBUG(llvm::dbgs() << " Continuing on line...\n");
2524 // The current line fits after compressing the whitespace - reflow
2525 // the next line into it if possible.
2528 Token
->compressWhitespace(LineIndex
, TailOffset
, Split
,
2531 // When we continue on the same line, leave one space between content.
2532 ContentStartColumn
+= ToSplitColumns
+ 1;
2533 Penalty
+= ExcessCharactersPenalty
;
2534 TailOffset
+= Split
.first
+ Split
.second
;
2535 RemainingTokenColumns
= Token
->getRemainingLength(
2536 LineIndex
, TailOffset
, ContentStartColumn
);
2540 LLVM_DEBUG(llvm::dbgs() << " Breaking...\n");
2541 // Update the ContentIndent only if the current line was not reflown with
2542 // the previous line, since in that case the previous line should still
2543 // determine the ContentIndent. Also never intent the last line.
2545 ContentIndent
= Token
->getContentIndent(LineIndex
);
2546 LLVM_DEBUG(llvm::dbgs()
2547 << " ContentIndent: " << ContentIndent
<< "\n");
2548 ContentStartColumn
= ContentIndent
+ Token
->getContentStartColumn(
2549 LineIndex
, /*Break=*/true);
2551 unsigned NewRemainingTokenColumns
= Token
->getRemainingLength(
2552 LineIndex
, TailOffset
+ Split
.first
+ Split
.second
,
2553 ContentStartColumn
);
2554 if (NewRemainingTokenColumns
== 0) {
2555 // No content to indent.
2557 ContentStartColumn
=
2558 Token
->getContentStartColumn(LineIndex
, /*Break=*/true);
2559 NewRemainingTokenColumns
= Token
->getRemainingLength(
2560 LineIndex
, TailOffset
+ Split
.first
+ Split
.second
,
2561 ContentStartColumn
);
2564 // When breaking before a tab character, it may be moved by a few columns,
2565 // but will still be expanded to the next tab stop, so we don't save any
2567 if (NewRemainingTokenColumns
>= RemainingTokenColumns
) {
2568 // FIXME: Do we need to adjust the penalty?
2572 LLVM_DEBUG(llvm::dbgs() << " Breaking at: " << TailOffset
+ Split
.first
2573 << ", " << Split
.second
<< "\n");
2575 Token
->insertBreak(LineIndex
, TailOffset
, Split
, ContentIndent
,
2579 Penalty
+= NewBreakPenalty
;
2580 TailOffset
+= Split
.first
+ Split
.second
;
2581 RemainingTokenColumns
= NewRemainingTokenColumns
;
2582 BreakInserted
= true;
2583 NewBreakBefore
= true;
2585 // In case there's another line, prepare the state for the start of the next
2587 if (LineIndex
+ 1 != EndIndex
) {
2588 unsigned NextLineIndex
= LineIndex
+ 1;
2589 if (NewBreakBefore
) {
2590 // After breaking a line, try to reflow the next line into the current
2591 // one once RemainingTokenColumns fits.
2595 // We decided that we want to try reflowing the next line into the
2597 // We will now adjust the state as if the reflow is successful (in
2598 // preparation for the next line), and see whether that works. If we
2599 // decide that we cannot reflow, we will later reset the state to the
2600 // start of the next line.
2602 // As we did not continue breaking the line, RemainingTokenColumns is
2603 // known to fit after ContentStartColumn. Adapt ContentStartColumn to
2604 // the position at which we want to format the next line if we do
2606 // When we reflow, we need to add a space between the end of the current
2607 // line and the next line's start column.
2608 ContentStartColumn
+= RemainingTokenColumns
+ 1;
2609 // Get the split that we need to reflow next logical line into the end
2610 // of the current one; the split will include any leading whitespace of
2611 // the next logical line.
2612 BreakableToken::Split SplitBeforeNext
=
2613 Token
->getReflowSplit(NextLineIndex
, CommentPragmasRegex
);
2614 LLVM_DEBUG(llvm::dbgs()
2615 << " Size of reflown text: " << ContentStartColumn
2616 << "\n Potential reflow split: ");
2617 if (SplitBeforeNext
.first
!= StringRef::npos
) {
2618 LLVM_DEBUG(llvm::dbgs() << SplitBeforeNext
.first
<< ", "
2619 << SplitBeforeNext
.second
<< "\n");
2620 TailOffset
= SplitBeforeNext
.first
+ SplitBeforeNext
.second
;
2621 // If the rest of the next line fits into the current line below the
2622 // column limit, we can safely reflow.
2623 RemainingTokenColumns
= Token
->getRemainingLength(
2624 NextLineIndex
, TailOffset
, ContentStartColumn
);
2626 if (ContentStartColumn
+ RemainingTokenColumns
> ColumnLimit
) {
2627 LLVM_DEBUG(llvm::dbgs()
2628 << " Over limit after reflow, need: "
2629 << (ContentStartColumn
+ RemainingTokenColumns
)
2630 << ", space: " << ColumnLimit
2631 << ", reflown prefix: " << ContentStartColumn
2632 << ", offset in line: " << TailOffset
<< "\n");
2633 // If the whole next line does not fit, try to find a point in
2634 // the next line at which we can break so that attaching the part
2635 // of the next line to that break point onto the current line is
2636 // below the column limit.
2637 BreakableToken::Split Split
=
2638 Token
->getSplit(NextLineIndex
, TailOffset
, ColumnLimit
,
2639 ContentStartColumn
, CommentPragmasRegex
);
2640 if (Split
.first
== StringRef::npos
) {
2641 LLVM_DEBUG(llvm::dbgs() << " Did not find later break\n");
2644 // Check whether the first split point gets us below the column
2645 // limit. Note that we will execute this split below as part of
2646 // the normal token breaking and reflow logic within the line.
2647 unsigned ToSplitColumns
= Token
->getRangeLength(
2648 NextLineIndex
, TailOffset
, Split
.first
, ContentStartColumn
);
2649 if (ContentStartColumn
+ ToSplitColumns
> ColumnLimit
) {
2650 LLVM_DEBUG(llvm::dbgs() << " Next split protrudes, need: "
2651 << (ContentStartColumn
+ ToSplitColumns
)
2652 << ", space: " << ColumnLimit
);
2653 unsigned ExcessCharactersPenalty
=
2654 (ContentStartColumn
+ ToSplitColumns
- ColumnLimit
) *
2655 Style
.PenaltyExcessCharacter
;
2656 if (NewBreakPenalty
< ExcessCharactersPenalty
)
2662 LLVM_DEBUG(llvm::dbgs() << "not found.\n");
2666 // If we didn't reflow into the next line, the only space to consider is
2667 // the next logical line. Reset our state to match the start of the next
2670 ContentStartColumn
=
2671 Token
->getContentStartColumn(NextLineIndex
, /*Break=*/false);
2672 RemainingTokenColumns
= Token
->getRemainingLength(
2673 NextLineIndex
, TailOffset
, ContentStartColumn
);
2674 // Adapt the start of the token, for example indent.
2676 Token
->adaptStartOfLine(NextLineIndex
, Whitespaces
);
2678 // If we found a reflow split and have added a new break before the next
2679 // line, we are going to remove the line break at the start of the next
2680 // logical line. For example, here we'll add a new line break after
2681 // 'text', and subsequently delete the line break between 'that' and
2683 // // some text that
2688 // When adding the line break, we also added the penalty for it, so we
2689 // need to subtract that penalty again when we remove the line break due
2691 if (NewBreakBefore
) {
2692 assert(Penalty
>= NewBreakPenalty
);
2693 Penalty
-= NewBreakPenalty
;
2696 Token
->reflow(NextLineIndex
, Whitespaces
);
2701 BreakableToken::Split SplitAfterLastLine
=
2702 Token
->getSplitAfterLastLine(TailOffset
);
2703 if (SplitAfterLastLine
.first
!= StringRef::npos
) {
2704 LLVM_DEBUG(llvm::dbgs() << "Replacing whitespace after last line.\n");
2706 // We add the last line's penalty here, since that line is going to be split
2708 Penalty
+= Style
.PenaltyExcessCharacter
*
2709 (ContentStartColumn
+ RemainingTokenColumns
- ColumnLimit
);
2712 Token
->replaceWhitespaceAfterLastLine(TailOffset
, SplitAfterLastLine
,
2715 ContentStartColumn
=
2716 Token
->getContentStartColumn(Token
->getLineCount() - 1, /*Break=*/true);
2717 RemainingTokenColumns
= Token
->getRemainingLength(
2718 Token
->getLineCount() - 1,
2719 TailOffset
+ SplitAfterLastLine
.first
+ SplitAfterLastLine
.second
,
2720 ContentStartColumn
);
2723 State
.Column
= ContentStartColumn
+ RemainingTokenColumns
-
2724 Current
.UnbreakableTailLength
;
2726 if (BreakInserted
) {
2728 Token
->updateAfterBroken(Whitespaces
);
2730 // If we break the token inside a parameter list, we need to break before
2731 // the next parameter on all levels, so that the next parameter is clearly
2732 // visible. Line comments already introduce a break.
2733 if (Current
.isNot(TT_LineComment
))
2734 for (ParenState
&Paren
: State
.Stack
)
2735 Paren
.BreakBeforeParameter
= true;
2737 if (Current
.is(TT_BlockComment
))
2738 State
.NoContinuation
= true;
2740 State
.Stack
.back().LastSpace
= StartColumn
;
2743 Token
->updateNextToken(State
);
2745 return {Penalty
, Exceeded
};
2748 unsigned ContinuationIndenter::getColumnLimit(const LineState
&State
) const {
2749 // In preprocessor directives reserve two chars for trailing " \".
2750 return Style
.ColumnLimit
- (State
.Line
->InPPDirective
? 2 : 0);
2753 bool ContinuationIndenter::nextIsMultilineString(const LineState
&State
) {
2754 const FormatToken
&Current
= *State
.NextToken
;
2755 if (!Current
.isStringLiteral() || Current
.is(TT_ImplicitStringLiteral
))
2757 // We never consider raw string literals "multiline" for the purpose of
2758 // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased
2759 // (see TokenAnnotator::mustBreakBefore().
2760 if (Current
.TokenText
.startswith("R\""))
2762 if (Current
.IsMultiline
)
2764 if (Current
.getNextNonComment() &&
2765 Current
.getNextNonComment()->isStringLiteral()) {
2766 return true; // Implicit concatenation.
2768 if (Style
.ColumnLimit
!= 0 && Style
.BreakStringLiterals
&&
2769 State
.Column
+ Current
.ColumnWidth
+ Current
.UnbreakableTailLength
>
2770 Style
.ColumnLimit
) {
2771 return true; // String will be split.
2776 } // namespace format
2777 } // namespace clang