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