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