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

source code of clang/lib/Format/ContinuationIndenter.cpp