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