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